-/*
- check all connections to see if anything
- happened on their sockets
-*/
-void check_network_activity(fd_set *f)
-{
- connection_t *c;
- avl_node_t *node;
- int result, i;
- int len = sizeof(result);
- vpn_packet_t packet;
-cp
- if(FD_ISSET(device_fd, f))
- {
- if(!read_packet(&packet))
- route_outgoing(&packet);
- }
-
- for(node = connection_tree->head; node; node = node->next)
- {
- c = (connection_t *)node->data;
-
- if(c->status.remove)
- continue;
-
- if(FD_ISSET(c->socket, f))
- {
- if(c->status.connecting)
- {
- c->status.connecting = 0;
- getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
- if(!result)
- finish_connecting(c);
- else
- {
- if(debug_lvl >= DEBUG_CONNECTIONS)
- syslog(LOG_DEBUG, _("Error while connecting to %s (%s): %s"), c->name, c->hostname, strerror(result));
- close(c->socket);
- do_outgoing_connection(c);
- continue;
- }
- }
- if(receive_meta(c) < 0)
- {
- terminate_connection(c, c->status.active);
- continue;
- }
- }
- }
-
- for(i = 0; i < listen_sockets; i++)
- {
- if(FD_ISSET(listen_socket[i].udp, f))
- handle_incoming_vpn_data(listen_socket[i].udp);
- if(FD_ISSET(listen_socket[i].tcp, f))
- handle_new_meta_connection(listen_socket[i].tcp);
- }
-cp
+void handle_meta_connection_data(void *data) {
+ connection_t *c = data;
+ int result;
+ socklen_t len = sizeof result;
+
+ if(c->status.connecting) {
+ c->status.connecting = false;
+
+ getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
+
+ if(!result) {
+ mutex_lock(&mutex);
+ finish_connecting(c);
+ mutex_unlock(&mutex);
+ } else {
+ ifdebug(CONNECTIONS) logger(LOG_DEBUG,
+ "Error while connecting to %s (%s): %s",
+ c->name, c->hostname, sockstrerror(result));
+ closesocket(c->socket);
+ mutex_lock(&mutex);
+ do_outgoing_connection(c);
+ mutex_unlock(&mutex);
+ return;
+ }
+ }
+
+ while(true) {
+ if (!receive_meta(c)) {
+ terminate_connection(c, c->status.active);
+ break;
+ }
+ }
+}
+
+int reload_configuration(void) {
+ connection_t *c;
+ splay_node_t *node, *next;
+ char *fname;
+ struct stat s;
+ static time_t last_config_check = 0;
+
+ /* Reread our own configuration file */
+
+ exit_configuration(&config_tree);
+ init_configuration(&config_tree);
+
+ if(!read_server_config()) {
+ logger(LOG_ERR, "Unable to reread configuration file, exitting.");
+ abort();
+ return EINVAL;
+ }
+
+ /* Close connections to hosts that have a changed or deleted host config file */
+
+ for(node = connection_tree->head; node; node = next) {
+ c = node->data;
+ next = node->next;
+
+ if(c->outgoing) {
+ free(c->outgoing->name);
+ if(c->outgoing->ai)
+ freeaddrinfo(c->outgoing->ai);
+ free(c->outgoing);
+ c->outgoing = NULL;
+ }
+
+ xasprintf(&fname, "%s/hosts/%s", confbase, c->name);
+ if(stat(fname, &s) || s.st_mtime > last_config_check)
+ terminate_connection(c, c->status.active);
+ free(fname);
+ }
+
+ last_config_check = time(NULL);
+
+ /* If StrictSubnet is set, expire deleted Subnets and read new ones in */
+
+ if(strictsubnets) {
+ subnet_t *subnet;
+
+
+ for(node = subnet_tree->head; node; node = node->next) {
+ subnet = node->data;
+ subnet->expires = 1;
+ }
+
+ load_all_subnets();
+
+ for(node = subnet_tree->head; node; node = next) {
+ next = node->next;
+ subnet = node->data;
+ if(subnet->expires == 1) {
+ send_del_subnet(broadcast, subnet);
+ if(subnet->owner->status.reachable)
+ subnet_update(subnet->owner, subnet, false);
+ subnet_del(subnet->owner, subnet);
+ } else if(subnet->expires == -1) {
+ subnet->expires = 0;
+ } else {
+ send_add_subnet(broadcast, subnet);
+ if(subnet->owner->status.reachable)
+ subnet_update(subnet->owner, subnet, true);
+ }
+ }
+ }
+
+ /* Try to make outgoing connections */
+
+ try_outgoing_connections();
+
+ return 0;
+}
+
+void retry(void) {
+ connection_t *c;
+ splay_node_t *node;
+
+ for(node = connection_tree->head; node; node = node->next) {
+ c = node->data;
+
+ if(c->outgoing && !c->node) {
+ event_del(&c->outgoing->ev);
+ if(c->status.connecting)
+ close(c->socket);
+ c->outgoing->timeout = 0;
+ do_outgoing_connection(c);
+ }
+ }