Use libevent to send MTU probes.
[tinc] / src / net.c
index 3a44ce6..6243bdd 100644 (file)
--- a/src/net.c
+++ b/src/net.c
@@ -128,17 +128,9 @@ static int build_fdset(void)
                        connection_del(c);
                        if(!connection_tree->head)
                                purge();
-               } else {
-                       short events = EV_READ;
-                       if(c->outbuflen > 0)
-                               events |= EV_WRITE;
-                       event_del(&c->ev);
-                       event_set(&c->ev, c->socket, events,
-                                         handle_meta_connection_data, c);
-                       if (event_add(&c->ev, NULL) < 0)
-                               return -1;
                }
        }
+
        return 0;
 }
 
@@ -168,6 +160,8 @@ void terminate_connection(connection_t *c, bool report)
        if(c->socket)
                closesocket(c->socket);
 
+       event_del(&c->ev);
+
        if(c->edge) {
                if(report && !tunnelserver)
                        send_del_edge(broadcast, c->edge);
@@ -274,33 +268,25 @@ void handle_meta_connection_data(int fd, short events, void *data)
        if (c->status.remove)
                return;
 
-       if (events & EV_READ) {
-               if(c->status.connecting) {
-                       c->status.connecting = false;
-                       getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
-
-                       if(!result)
-                               finish_connecting(c);
-                       else {
-                               ifdebug(CONNECTIONS) logger(LOG_DEBUG,
-                                                  _("Error while connecting to %s (%s): %s"),
-                                                  c->name, c->hostname, strerror(result));
-                               closesocket(c->socket);
-                               do_outgoing_connection(c);
-                               return;
-                       }
-               }
-
-               if (!receive_meta(c)) {
-                       terminate_connection(c, c->status.active);
+       if(c->status.connecting) {
+               c->status.connecting = false;
+               getsockopt(c->socket, SOL_SOCKET, SO_ERROR, &result, &len);
+
+               if(!result)
+                       finish_connecting(c);
+               else {
+                       ifdebug(CONNECTIONS) logger(LOG_DEBUG,
+                                          _("Error while connecting to %s (%s): %s"),
+                                          c->name, c->hostname, strerror(result));
+                       closesocket(c->socket);
+                       do_outgoing_connection(c);
                        return;
                }
        }
 
-       if (events & EV_WRITE) {
-               if(!flush_meta(c)) {
-                       terminate_connection(c, c->status.active);
-               }
+       if (!receive_meta(c)) {
+               terminate_connection(c, c->status.active);
+               return;
        }
 }
 
@@ -308,6 +294,50 @@ static void dummy(int a, short b, void *c)
 {
 }
 
+void sighup_handler(int signal, short events, void *data) {
+       connection_t *c;
+       avl_node_t *node;
+       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."));
+               event_loopexit(NULL);
+               return;
+       }
+
+       /* Close connections to hosts that have a changed or deleted host config file */
+       
+       for(node = connection_tree->head; node; node = node->next) {
+               c = node->data;
+               
+               if(c->outgoing) {
+                       free(c->outgoing->name);
+                       if(c->outgoing->ai)
+                               freeaddrinfo(c->outgoing->ai);
+                       free(c->outgoing);
+                       c->outgoing = NULL;
+               }
+               
+               asprintf(&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);
+
+       /* Try to make outgoing connections */
+       
+       try_outgoing_connections();
+}
+
 /*
   this is where it all happens...
 */
@@ -315,15 +345,17 @@ int main_loop(void)
 {
        struct timeval tv;
        int r;
-       time_t last_ping_check, last_config_check, last_graph_dump;
+       time_t last_ping_check;
        tevent_t *event;
        struct event timeout;
+       struct event sighup_event;
 
        cp();
 
+       signal_set(&sighup_event, SIGHUP, sighup_handler, NULL);
+       signal_add(&sighup_event, NULL);
+
        last_ping_check = now;
-       last_config_check = now;
-       last_graph_dump = now;
        
        srand(now);
 
@@ -402,57 +434,9 @@ int main_loop(void)
                        sigalrm = false;
                }
 
-               if(sighup) {
-                       connection_t *c;
-                       avl_node_t *node;
-                       char *fname;
-                       struct stat s;
-                       
-                       sighup = false;
-                       
-                       /* 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."));
-                               return 1;
-                       }
-
-                       /* Close connections to hosts that have a changed or deleted host config file */
-                       
-                       for(node = connection_tree->head; node; node = node->next) {
-                               c = node->data;
-                               
-                               if(c->outgoing) {
-                                       free(c->outgoing->name);
-                                       if(c->outgoing->ai)
-                                               freeaddrinfo(c->outgoing->ai);
-                                       free(c->outgoing);
-                                       c->outgoing = NULL;
-                               }
-                               
-                               asprintf(&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 = now;
-
-                       /* Try to make outgoing connections */
-                       
-                       try_outgoing_connections();
-               }
-               
-               /* Dump graph if wanted every 60 seconds*/
-
-               if(last_graph_dump + 60 < now) {
-                       dump_graph();
-                       last_graph_dump = now;
-               }
        }
 
+       signal_del(&sighup_event);
+
        return 0;
 }