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;
return true;
}
- if(!try_bind(port)) {
+ if(!try_bind(mesh, port)) {
meshlink_errno = MESHLINK_ENETWORK;
return false;
}
extern void finish_connecting(struct meshlink_handle *mesh, struct connection_t *);
extern void do_outgoing_connection(struct meshlink_handle *mesh, struct outgoing_t *);
extern void handle_new_meta_connection(struct event_loop_t *loop, void *, int);
+extern int setup_tcp_listen_socket(struct meshlink_handle *mesh, const struct addrinfo *aip) __attribute__((__warn_unused_result__));
+extern int setup_udp_listen_socket(struct meshlink_handle *mesh, const struct addrinfo *aip) __attribute__((__warn_unused_result__));
extern bool send_sptps_data(void *handle, uint8_t type, const void *data, size_t len);
extern bool receive_sptps_record(void *handle, uint8_t type, const void *data, uint16_t len) __attribute__((__warn_unused_result__));
extern void send_packet(struct meshlink_handle *mesh, struct node_t *, struct vpn_packet_t *);
return true;
}
-static int setup_tcp_listen_socket(meshlink_handle_t *mesh, const struct addrinfo *aip) {
+int setup_tcp_listen_socket(meshlink_handle_t *mesh, const struct addrinfo *aip) {
int nfd = socket(aip->ai_family, SOCK_STREAM, IPPROTO_TCP);
if(nfd == -1) {
return nfd;
}
-static int setup_udp_socket(meshlink_handle_t *mesh, const struct addrinfo *aip) {
+int setup_udp_listen_socket(meshlink_handle_t *mesh, const struct addrinfo *aip) {
int nfd = socket(aip->ai_family, SOCK_DGRAM, IPPROTO_UDP);
if(nfd == -1) {
/* If TCP worked, then we require that UDP works as well. */
- int udp_fd = setup_udp_socket(mesh, aip);
+ int udp_fd = setup_udp_listen_socket(mesh, aip);
if(udp_fd == -1) {
closesocket(tcp_fd);