]> git.meshlink.io Git - meshlink-tiny/blobdiff - src/net_socket.c
Add a metering test.
[meshlink-tiny] / src / net_socket.c
index 29e37254986b2ab96b2f79ec09b2393f481951fe..902cbe71d8f277c4ee509107ac6c28ea1a0b255e 100644 (file)
@@ -19,7 +19,6 @@
 
 #include "system.h"
 
-#include "adns.h"
 #include "conf.h"
 #include "connection.h"
 #include "list.h"
@@ -41,8 +40,6 @@
 #define MSG_NOSIGNAL 0
 #endif
 
-static const int max_connection_burst = 100;
-
 /* Setup sockets */
 
 static void configure_tcp(connection_t *c) {
@@ -177,7 +174,7 @@ static void handle_meta_io(event_loop_t *loop, void *data, int flags) {
 // Build a list of recently seen addresses.
 static struct addrinfo *get_recent_addresses(node_t *n) {
        struct addrinfo *ai = NULL;
-       struct addrinfo *aip;
+       struct addrinfo *aip = NULL;
 
        for(int i = 0; i < 5; i++) {
                if(!n->recent[i].sa.sa_family) {
@@ -214,28 +211,6 @@ static void free_known_addresses(struct addrinfo *ai) {
        }
 }
 
-static void canonical_resolve_cb(meshlink_handle_t *mesh, char *host, char *serv, void *data, struct addrinfo *ai, int err) {
-       (void)serv;
-       (void)err;
-       node_t *n = data;
-
-       free(host);
-       free(serv);
-
-       for list_each(outgoing_t, outgoing, mesh->outgoings) {
-               if(outgoing->node == n) {
-                       if(outgoing->state == OUTGOING_CANONICAL_RESOLVE) {
-                               outgoing->ai = ai;
-                               outgoing->aip = NULL;
-                               outgoing->state = OUTGOING_CANONICAL;
-                               do_outgoing_connection(mesh, outgoing);
-                       }
-
-                       return;
-               }
-       }
-}
-
 static bool get_next_outgoing_address(meshlink_handle_t *mesh, outgoing_t *outgoing) {
        (void)mesh;
 
@@ -255,15 +230,15 @@ static bool get_next_outgoing_address(meshlink_handle_t *mesh, outgoing_t *outgo
 
                        if(port) {
                                *port++ = 0;
-                               port = xstrdup(port);
-                               adns_queue(mesh, address, port, canonical_resolve_cb, outgoing->node, 2);
-                               return false;
+                               outgoing->ai = str2addrinfo(address, port, SOCK_STREAM);
+                               outgoing->aip = NULL;
+                               outgoing->state = OUTGOING_CANONICAL;
                        } else {
                                logger(mesh, MESHLINK_ERROR, "Canonical address for %s is missing port number", n->name);
-                               free(address);
                                outgoing->state = OUTGOING_RECENT;
                        }
 
+                       free(address);
                } else {
                        outgoing->state = OUTGOING_RECENT;
                }
@@ -314,6 +289,22 @@ static bool get_next_outgoing_address(meshlink_handle_t *mesh, outgoing_t *outgo
        return false;
 }
 
+static void free_outgoing(outgoing_t *outgoing) {
+       meshlink_handle_t *mesh = outgoing->node->mesh;
+
+       timeout_del(&mesh->loop, &outgoing->ev);
+
+       if(outgoing->ai) {
+               if(outgoing->state == OUTGOING_RECENT || outgoing->state == OUTGOING_KNOWN) {
+                       free_known_addresses(outgoing->ai);
+               } else {
+                       freeaddrinfo(outgoing->ai);
+               }
+       }
+
+       free(outgoing);
+}
+
 void do_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
 begin:
 
@@ -322,7 +313,8 @@ begin:
                        /* We are waiting for a callback from the ADNS thread */
                } else if(outgoing->state == OUTGOING_NO_KNOWN_ADDRESSES) {
                        logger(mesh, MESHLINK_ERROR, "No known addresses for %s", outgoing->node->name);
-                       list_delete(mesh->outgoings, outgoing);
+                       free_outgoing(outgoing);
+                       mesh->outgoing = NULL;
                } else {
                        logger(mesh, MESHLINK_ERROR, "Could not set up a meta connection to %s", outgoing->node->name);
                        retry_outgoing(mesh, outgoing);
@@ -434,116 +426,13 @@ void setup_outgoing_connection(meshlink_handle_t *mesh, outgoing_t *outgoing) {
        do_outgoing_connection(mesh, outgoing);
 }
 
-/// Delayed close of a filedescriptor.
-static void tarpit(meshlink_handle_t *mesh, int fd) {
-       if(!fd) {
-               return;
-       }
-
-       if(mesh->pits[mesh->next_pit]) {
-               closesocket(mesh->pits[mesh->next_pit]);
-       }
-
-       mesh->pits[mesh->next_pit++] = fd;
-
-       if(mesh->next_pit >= (int)(sizeof mesh->pits / sizeof mesh->pits[0])) {
-               mesh->next_pit = 0;
-       }
-}
-
-/*
-  accept a new tcp connect and create a
-  new connection
-*/
-void handle_new_meta_connection(event_loop_t *loop, void *data, int flags) {
-       (void)flags;
-       meshlink_handle_t *mesh = loop->data;
-       listen_socket_t *l = data;
-       connection_t *c;
-       sockaddr_t sa;
-       int fd;
-       socklen_t len = sizeof(sa);
-
-       memset(&sa, 0, sizeof(sa));
-
-       fd = accept(l->tcp.fd, &sa.sa, &len);
-
-       if(fd < 0) {
-               if(sockwouldblock(errno)) {
-                       return;
-               }
-
-               if(errno == EINVAL) { // TODO: check if Windows agrees
-                       event_loop_stop(loop);
-                       return;
-               }
-
-               logger(mesh, MESHLINK_ERROR, "Accepting a new connection failed: %s", sockstrerror(sockerrno));
-               return;
-       }
-
-       sockaddrunmap(&sa);
-
-       /* Rate limit incoming connections to max_connection_burst/second. */
-
-       if(mesh->loop.now.tv_sec != mesh->connection_burst_time) {
-               mesh->connection_burst_time = mesh->loop.now.tv_sec;
-               mesh->connection_burst = 0;
-       }
-
-       if(mesh->connection_burst >= max_connection_burst) {
-               tarpit(mesh, fd);
-               return;
-       }
-
-       mesh->connection_burst++;
-
-       // Accept the new connection
-
-       c = new_connection();
-       c->name = xstrdup("<unknown>");
-
-       c->address = sa;
-       c->socket = fd;
-       c->last_ping_time = mesh->loop.now.tv_sec;
-
-       char *hostname = sockaddr2hostname(&sa);
-       logger(mesh, MESHLINK_INFO, "Connection from %s", hostname);
-       free(hostname);
-
-       io_add(&mesh->loop, &c->io, handle_meta_io, c, c->socket, IO_READ);
-
-       configure_tcp(c);
-
-       connection_add(mesh, c);
-
-       c->allow_request = ID;
-       send_id(mesh, c);
-}
-
-static void free_outgoing(outgoing_t *outgoing) {
-       meshlink_handle_t *mesh = outgoing->node->mesh;
-
-       timeout_del(&mesh->loop, &outgoing->ev);
-
-       if(outgoing->ai) {
-               if(outgoing->state == OUTGOING_RECENT || outgoing->state == OUTGOING_KNOWN) {
-                       free_known_addresses(outgoing->ai);
-               } else {
-                       freeaddrinfo(outgoing->ai);
-               }
-       }
-
-       free(outgoing);
-}
-
 void init_outgoings(meshlink_handle_t *mesh) {
-       mesh->outgoings = list_alloc((list_action_t)free_outgoing);
+       mesh->outgoing = NULL;
 }
 
 void exit_outgoings(meshlink_handle_t *mesh) {
-       if(mesh->outgoings) {
-               list_delete_list(mesh->outgoings);
-               mesh->outgoings = NULL;
+       if(mesh->outgoing) {
+               free_outgoing(mesh->outgoing);
+               mesh->outgoing = NULL;
        }
 }