/* close all open network connections */ void close_network_connections(meshlink_handle_t *mesh) { if(mesh->connections) { for(list_node_t *node = mesh->connections->head, *next; node; node = next) { next = node->next; connection_t *c = node->data; c->outgoing = NULL; terminate_connection(mesh, c, false); } } if(mesh->outgoings) list_delete_list(mesh->outgoings); if(mesh->self && mesh->self->connection) { terminate_connection(mesh, mesh->self->connection, false); free_connection(mesh->self->connection); } for(int i = 0; i < mesh->listen_sockets; i++) { io_del(&mesh->loop, &mesh->listen_socket[i].tcp); io_del(&mesh->loop, &mesh->listen_socket[i].udp); close(mesh->listen_socket[i].tcp.fd); close(mesh->listen_socket[i].udp.fd); } exit_requests(mesh); exit_edges(mesh); exit_nodes(mesh); exit_connections(mesh); if(mesh->myport) free(mesh->myport); return; }
void free_connection(connection_t *c) { if(!c) return; cipher_close(c->incipher); digest_close(c->indigest); cipher_close(c->outcipher); digest_close(c->outdigest); sptps_stop(&c->sptps); ecdsa_free(c->ecdsa); rsa_free(c->rsa); free(c->hischallenge); buffer_clear(&c->inbuf); buffer_clear(&c->outbuf); io_del(&c->io); if(c->socket > 0) closesocket(c->socket); free(c->name); free(c->hostname); if(c->config_tree) exit_configuration(&c->config_tree); free(c); }
static void cherr_proc(io_atom *inatom, int flags) { heavy_atom *atom = (heavy_atom*)inatom; char buf[512]; int cnt; if(flags != IO_READ) { log_warn("Got flags=%d in cherr_proc!"); if(!(flags & IO_READ)) { return; } } do { errno = 0; cnt = read(atom->atom.fd, buf, sizeof(buf)); } while(cnt == -1 && errno == EINTR); if(cnt > 0) { // TODO: should probably print the stderr to our stderr (right?) // Yes, but need to suppress // "rz waiting to receive." log_warn("CHILD STDERR fd=%d: <<<%.*s>>>", atom->atom.fd, cnt, buf); } else if(cnt == 0) { // eof on stderr. io_del(&atom->atom); log_info("Closed FD %d: got EOF on child stderr.", atom->atom.fd); atom->atom.fd = -1; } else { log_warn("CHILD STDERR fd=%d:read error: %d (%s)", atom->atom.fd, errno, strerror(errno)); } }
void connection_close(connection *conn) { io_del(&conn->io); close(conn->io.fd); free(conn); }
void connection_del(meshlink_handle_t *mesh, connection_t *c) { io_del(&mesh->loop, &c->io); list_delete(mesh->connections, c); }
void io_socket_close(io_atom *io) { io_del(io); close(io->fd); io->fd = -1; }
int main(int argc, char **argv) { program_name = argv[0]; if(!parse_options(argc, argv)) return 1; make_names(true); if(show_version) { printf("%s version %s (built %s %s, protocol %d.%d)\n", PACKAGE, BUILD_VERSION, BUILD_DATE, BUILD_TIME, PROT_MAJOR, PROT_MINOR); printf("Copyright (C) 1998-2015 Ivo Timmermans, Guus Sliepen and others.\n" "See the AUTHORS file for a complete list.\n\n" "tinc comes with ABSOLUTELY NO WARRANTY. This is free software,\n" "and you are welcome to redistribute it under certain conditions;\n" "see the file COPYING for details.\n"); return 0; } if(show_help) { usage(false); return 0; } #ifdef HAVE_MINGW if(WSAStartup(MAKEWORD(2, 2), &wsa_state)) { logger(DEBUG_ALWAYS, LOG_ERR, "System call `%s' failed: %s", "WSAStartup", winerror(GetLastError())); return 1; } #else // Check if we got an umbilical fd from the process that started us char *umbstr = getenv("TINC_UMBILICAL"); if(umbstr) { umbilical = atoi(umbstr); if(fcntl(umbilical, F_GETFL) < 0) umbilical = 0; #ifdef FD_CLOEXEC if(umbilical) fcntl(umbilical, F_SETFD, FD_CLOEXEC); #endif } #endif openlogger("tinc", use_logfile?LOGMODE_FILE:LOGMODE_STDERR); g_argv = argv; if(getenv("LISTEN_PID") && atoi(getenv("LISTEN_PID")) == getpid()) do_detach = false; #ifdef HAVE_UNSETENV unsetenv("LISTEN_PID"); #endif init_configuration(&config_tree); /* Slllluuuuuuurrrrp! */ gettimeofday(&now, NULL); srand(now.tv_sec + now.tv_usec); crypto_init(); if(!read_server_config()) return 1; #ifdef HAVE_MINGW io_add_event(&stop_io, stop_handler, NULL, WSACreateEvent()); if (stop_io.event == FALSE) abort(); int result; if(!do_detach || !init_service()) { SetConsoleCtrlHandler(console_ctrl_handler, TRUE); result = main2(argc, argv); } else result = 1; if (WSACloseEvent(stop_io.event) == FALSE) abort(); io_del(&stop_io); return result; }