Use tinc 1.0's event infrastructure to handle timeouts.
[tinc] / src / net_setup.c
index 9c18895..38788f0 100644 (file)
@@ -3,6 +3,7 @@
     Copyright (C) 1998-2005 Ivo Timmermans,
                   2000-2010 Guus Sliepen <guus@tinc-vpn.org>
                   2006      Scott Lamb <slamb@slamb.org>
+                  2010      Brandon Black <blblack@gmail.com>
 
     This program is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
@@ -138,20 +139,17 @@ bool read_rsa_private_key() {
 
 static struct event keyexpire_event;
 
-static void keyexpire_handler(int fd, short events, void *data) {
+static void keyexpire_handler(void *data) {
        regenerate_key();
 }
 
 void regenerate_key() {
-       if(timeout_initialized(&keyexpire_event)) {
-               ifdebug(STATUS) logger(LOG_INFO, "Expiring symmetric keys");
-               event_del(&keyexpire_event);
-               send_key_changed(broadcast, myself);
-       } else {
-               timeout_set(&keyexpire_event, keyexpire_handler, NULL);
-       }
-
-       event_add(&keyexpire_event, &(struct timeval){keylifetime, 0});
+       ifdebug(STATUS) logger(LOG_INFO, "Expiring symmetric keys");
+       event_del(&keyexpire_event);
+       send_key_changed(broadcast, myself);
+       keyexpire_event.time = time(NULL) + keylifetime;
+       keyexpire_event.handler = keyexpire_handler;
+       event_add(&keyexpire_event);
 }
 
 /*
@@ -229,6 +227,7 @@ bool setup_myself(void) {
        struct addrinfo *ai, *aip, hint = {0};
        bool choice;
        int i, err;
+       int replaywin_int;
 
        myself = new_node();
        myself->connection = new_connection();
@@ -358,6 +357,28 @@ bool setup_myself(void) {
        } else
                maxtimeout = 900;
 
+       if(get_config_int(lookup_config(config_tree, "UDPRcvBuf"), &udp_rcvbuf)) {
+               if(udp_rcvbuf <= 0) {
+                       logger(LOG_ERR, "UDPRcvBuf cannot be negative!");
+                       return false;
+               }
+       }
+
+       if(get_config_int(lookup_config(config_tree, "UDPSndBuf"), &udp_sndbuf)) {
+               if(udp_sndbuf <= 0) {
+                       logger(LOG_ERR, "UDPSndBuf cannot be negative!");
+                       return false;
+               }
+       }
+
+       if(get_config_int(lookup_config(config_tree, "ReplayWindow"), &replaywin_int)) {
+               if(replaywin_int < 0) {
+                       logger(LOG_ERR, "ReplayWindow cannot be negative!");
+                       return false;
+               }
+               replaywin = (unsigned)replaywin_int;
+       }
+
        if(get_config_string(lookup_config(config_tree, "AddressFamily"), &afname)) {
                if(!strcasecmp(afname, "IPv4"))
                        addressfamily = AF_INET;
@@ -391,7 +412,7 @@ bool setup_myself(void) {
 
        /* Check if we want to use message authentication codes... */
 
-       if(!get_config_string(lookup_config(myself->connection->config_tree, "Digest"), &digest))
+       if(!get_config_string(lookup_config(config_tree, "Digest"), &digest))
                digest = xstrdup("sha1");
 
        int maclength = 4;
@@ -436,16 +457,6 @@ bool setup_myself(void) {
        if(!setup_device())
                return false;
 
-       if(device_fd >= 0) {
-               event_set(&device_ev, device_fd, EV_READ|EV_PERSIST, handle_device_data, NULL);
-
-               if (event_add(&device_ev, NULL) < 0) {
-                       logger(LOG_ERR, "event_add failed: %s", strerror(errno));
-                       close_device();
-                       return false;
-               }
-       }
-
        /* Run tinc-up script to further initialize the tap interface */
        xasprintf(&envp[0], "NETNAME=%s", netname ? : "");
        xasprintf(&envp[1], "DEVICE=%s", device ? : "");
@@ -496,21 +507,15 @@ bool setup_myself(void) {
                        continue;
                }
 
-               event_set(&listen_socket[listen_sockets].ev_tcp,
-                                 listen_socket[listen_sockets].tcp,
-                                 EV_READ|EV_PERSIST,
-                                 handle_new_meta_connection, NULL);
-               if(event_add(&listen_socket[listen_sockets].ev_tcp, NULL) < 0) {
-                       logger(LOG_ERR, "event_add failed: %s", strerror(errno));
+               memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
+
+               if(!thread_create(&listen_socket[listen_sockets].tcp_thread, handle_new_meta_connection, &listen_socket[listen_sockets])) {
+                       logger(LOG_ERR, "thread_create failed: %s", strerror(errno));
                        abort();
                }
 
-               event_set(&listen_socket[listen_sockets].ev_udp,
-                                 listen_socket[listen_sockets].udp,
-                                 EV_READ|EV_PERSIST,
-                                 handle_incoming_vpn_data, NULL);
-               if(event_add(&listen_socket[listen_sockets].ev_udp, NULL) < 0) {
-                       logger(LOG_ERR, "event_add failed: %s", strerror(errno));
+               if(!thread_create(&listen_socket[listen_sockets].udp_thread, handle_incoming_vpn_data, &listen_socket[listen_sockets])) {
+                       logger(LOG_ERR, "thread_create failed: %s", strerror(errno));
                        abort();
                }
 
@@ -520,7 +525,6 @@ bool setup_myself(void) {
                        free(hostname);
                }
 
-               memcpy(&listen_socket[listen_sockets].sa, aip->ai_addr, aip->ai_addrlen);
                listen_sockets++;
 
                if(listen_sockets >= MAXSOCKETS) {
@@ -597,10 +601,10 @@ void close_network_connections(void) {
        }
 
        for(i = 0; i < listen_sockets; i++) {
-               event_del(&listen_socket[i].ev_tcp);
-               event_del(&listen_socket[i].ev_udp);
                close(listen_socket[i].tcp);
                close(listen_socket[i].udp);
+               thread_destroy(&listen_socket[i].tcp_thread);
+               thread_destroy(&listen_socket[i].udp_thread);
        }
 
        xasprintf(&envp[0], "NETNAME=%s", netname ? : "");