]> git.meshlink.io Git - meshlink/blobdiff - src/net_socket.c
Remove global variable "now".
[meshlink] / src / net_socket.c
index 1b55c9423e74744a37390167e44218d4de42b5ed..287d68887fde45ecaa6e4d9b4aa1b6beed054861 100644 (file)
@@ -25,7 +25,6 @@
 #include "avl_tree.h"
 #include "conf.h"
 #include "connection.h"
-#include "tevent.h"
 #include "logger.h"
 #include "meta.h"
 #include "net.h"
@@ -234,10 +233,11 @@ int setup_vpn_in_socket(const sockaddr_t *sa)
        return nfd;
 }
 
-void retry_outgoing(outgoing_t *outgoing)
-{
-       tevent_t *event;
+static void retry_outgoing_handler(int fd, short events, void *data) {
+       do_outgoing_connection(data);
+}
 
+void retry_outgoing(outgoing_t *outgoing) {
        cp();
 
        outgoing->timeout += 5;
@@ -245,11 +245,8 @@ void retry_outgoing(outgoing_t *outgoing)
        if(outgoing->timeout > maxtimeout)
                outgoing->timeout = maxtimeout;
 
-       event = new_tevent();
-       event->handler = (event_handler_t) setup_outgoing_connection;
-       event->time = now + outgoing->timeout;
-       event->data = outgoing;
-       tevent_add(event);
+       timeout_set(&outgoing->ev, retry_outgoing_handler, outgoing);
+       event_add(&outgoing->ev, &(struct timeval){outgoing->timeout, 0});
 
        ifdebug(CONNECTIONS) logger(LOG_NOTICE,
                           _("Trying to re-establish outgoing connection in %d seconds"),
@@ -264,7 +261,8 @@ void finish_connecting(connection_t *c)
 
        configure_tcp(c);
 
-       c->last_ping_time = now;
+       c->last_ping_time = time(NULL);
+       c->status.connecting = false;
 
        send_id(c);
 }
@@ -393,11 +391,18 @@ void setup_outgoing_connection(outgoing_t *outgoing)
        }
 
        c->outgoing = outgoing;
-       c->last_ping_time = now;
+       c->last_ping_time = time(NULL);
 
        connection_add(c);
 
        do_outgoing_connection(c);
+
+       event_set(&c->ev, c->socket, EV_READ | EV_PERSIST, handle_meta_connection_data, c);
+       event_set(&c->outev, c->socket, EV_WRITE | EV_PERSIST, flush_meta, c);
+       if(event_add(&c->ev, NULL) < 0) {
+               logger(LOG_EMERG, _("event_add failed: %s"), strerror(errno));
+               abort();
+       }
 }
 
 /*
@@ -416,8 +421,8 @@ void handle_new_meta_connection(int sock, short events, void *data)
        fd = accept(sock, &sa.sa, &len);
 
        if(fd < 0) {
-               logger(LOG_ERR, _("Accepting a new connection failed: %s"),
-                          strerror(errno));
+               logger(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
+               return;
        }
 
        sockaddrunmap(&sa);
@@ -432,10 +437,18 @@ void handle_new_meta_connection(int sock, short events, void *data)
        c->address = sa;
        c->hostname = sockaddr2hostname(&sa);
        c->socket = fd;
-       c->last_ping_time = now;
+       c->last_ping_time = time(NULL);
 
        ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection from %s"), c->hostname);
 
+       event_set(&c->ev, c->socket, EV_READ | EV_PERSIST, handle_meta_connection_data, c);
+       event_set(&c->outev, c->socket, EV_WRITE | EV_PERSIST, flush_meta, c);
+       if(event_add(&c->ev, NULL) < 0) {
+               logger(LOG_ERR, _("event_add failed: %s"), strerror(errno));
+               connection_del(c);
+               return;
+       }
+               
        configure_tcp(c);
 
        connection_add(c);