-int handle_new_meta_connection(int sock)
-{
- connection_t *c;
- sockaddr_t sa;
- int fd, len = sizeof(sa);
-cp
- if((fd = accept(sock, &sa.sa, &len)) < 0)
- {
- syslog(LOG_ERR, _("Accepting a new connection failed: %s"), strerror(errno));
- return -1;
- }
-
- sockaddrunmap(&sa);
-
- c = new_connection();
- c->outcipher = myself->connection->outcipher;
- c->outdigest = myself->connection->outdigest;
- c->outmaclength = myself->connection->outmaclength;
- c->outcompression = myself->connection->outcompression;
-
- c->address = sa;
- c->hostname = sockaddr2hostname(&sa);
- c->socket = fd;
- c->last_ping_time = now;
-
- if(debug_lvl >= DEBUG_CONNECTIONS)
- syslog(LOG_NOTICE, _("Connection from %s"), c->hostname);
-
- connection_add(c);
-
- c->allow_request = ID;
- send_id(c);
-cp
- return 0;
+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(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);