if(mesh->invitation_addresses) {
for list_each(char, combo, mesh->invitation_addresses) {
hostname[n] = xstrdup(combo);
- char *colon = strrchr(hostname[n], ':');
+ char *slash = strrchr(hostname[n], '/');
- if(colon) {
- *colon = 0;
- port[n] = xstrdup(colon + 1);
+ if(slash) {
+ *slash = 0;
+ port[n] = xstrdup(slash + 1);
}
n++;
return hostport;
}
-static bool try_bind(int port) {
+static bool try_bind(meshlink_handle_t *mesh, int port) {
struct addrinfo *ai = NULL;
struct addrinfo hint = {
.ai_flags = AI_PASSIVE,
for(struct addrinfo *aip = ai; aip; aip = aip->ai_next) {
/* Try to bind to TCP. */
- int tcp_fd = socket(aip->ai_family, SOCK_STREAM, IPPROTO_TCP);
+ int tcp_fd = setup_tcp_listen_socket(mesh, aip);
if(tcp_fd == -1) {
- continue;
- }
-
- int result = bind(tcp_fd, aip->ai_addr, aip->ai_addrlen);
- closesocket(tcp_fd);
-
- if(result) {
if(errno == EADDRINUSE) {
/* If this port is in use for any address family, avoid it. */
success = false;
/* If TCP worked, then we require that UDP works as well. */
- int udp_fd = socket(aip->ai_family, SOCK_DGRAM, IPPROTO_UDP);
+ int udp_fd = setup_udp_listen_socket(mesh, aip);
if(udp_fd == -1) {
+ closesocket(tcp_fd);
success = false;
break;
}
- result = bind(udp_fd, aip->ai_addr, aip->ai_addrlen);
+ closesocket(tcp_fd);
closesocket(udp_fd);
-
- if(result) {
- success = false;
- break;
- }
-
success = true;
}
for(int i = 0; i < 1000; i++) {
int port = 0x1000 + prng(mesh, 0x8000);
- if(try_bind(port)) {
+ if(try_bind(mesh, port)) {
free(mesh->myport);
xasprintf(&mesh->myport, "%d", port);
return port;
}
bool success = setup_network(mesh);
- add_local_addresses(mesh);
return success ? arg : NULL;
}
#endif // HAVE_SETNS
#endif // HAVE_SETNS
} else {
success = setup_network(mesh);
- add_local_addresses(mesh);
}
if(!success) {
return false;
}
+ logger(mesh, MESHLINK_DEBUG, "Adding packet of %zu bytes to packet queue", len);
+
// Notify event loop
signal_trigger(&mesh->loop, &mesh->datafromapp);
void meshlink_send_from_queue(event_loop_t *loop, void *data) {
(void)loop;
meshlink_handle_t *mesh = data;
- vpn_packet_t *packet = meshlink_queue_pop(&mesh->outpacketqueue);
-
- if(!packet) {
- return;
- }
- mesh->self->in_packets++;
- mesh->self->in_bytes += packet->len;
- route(mesh, mesh->self, packet);
+ logger(mesh, MESHLINK_DEBUG, "Flushing the packet queue");
- free(packet);
+ for(vpn_packet_t *packet; (packet = meshlink_queue_pop(&mesh->outpacketqueue));) {
+ logger(mesh, MESHLINK_DEBUG, "Removing packet of %d bytes from packet queue", packet->len);
+ mesh->self->in_packets++;
+ mesh->self->in_bytes += packet->len;
+ route(mesh, mesh->self, packet);
+ free(packet);
+ }
}
ssize_t meshlink_get_pmtu(meshlink_handle_t *mesh, meshlink_node_t *destination) {
char *combo;
if(port) {
- xasprintf(&combo, "%s:%s", address, port);
+ xasprintf(&combo, "%s/%s", address, port);
} else {
combo = xstrdup(address);
}
return true;
}
- if(!try_bind(port)) {
+ if(!try_bind(mesh, port)) {
meshlink_errno = MESHLINK_ENETWORK;
return false;
}