X-Git-Url: https://tinc-vpn.org/git/browse?a=blobdiff_plain;f=src%2Fnet_socket.c;h=8783b01a2b7749149cd087e8ae0d9d875def75ef;hb=391985158c16f4a3894a798f6788cfb23dfcfea9;hp=a8e197b4c430f37dbb1833e31cfacc8b3dfd6ec2;hpb=6debc6c79ba385d35f646e0958f84ace5b8f4b4d;p=tinc diff --git a/src/net_socket.c b/src/net_socket.c index a8e197b4..8783b01a 100644 --- a/src/net_socket.c +++ b/src/net_socket.c @@ -47,7 +47,7 @@ int fwmark; listen_socket_t listen_socket[MAXSOCKETS]; int listen_sockets; -#ifndef HAVE_MINGW +#ifndef HAVE_WINDOWS io_t unix_socket; #endif @@ -176,9 +176,20 @@ static bool bind_to_address(connection_t *c) { return true; } +static bool try_bind(int nfd, const sockaddr_t *sa, const char *type) { + if(!bind(nfd, &sa->sa, SALEN(sa->sa))) { + return true; + } + + closesocket(nfd); + char *addrstr = sockaddr2hostname(sa); + logger(DEBUG_ALWAYS, LOG_ERR, "Can't bind to %s/%s: %s", addrstr, type, sockstrerror(sockerrno)); + free(addrstr); + return false; +} + int setup_listen_socket(const sockaddr_t *sa) { int nfd; - char *addrstr; int option; char *iface; @@ -237,11 +248,7 @@ int setup_listen_socket(const sockaddr_t *sa) { #endif } - if(bind(nfd, &sa->sa, SALEN(sa->sa))) { - closesocket(nfd); - addrstr = sockaddr2hostname(sa); - logger(DEBUG_ALWAYS, LOG_ERR, "Can't bind to %s/tcp: %s", addrstr, sockstrerror(sockerrno)); - free(addrstr); + if(!try_bind(nfd, sa, "tcp")) { return -1; } @@ -282,10 +289,8 @@ static void set_udp_buffer(int nfd, int type, const char *name, int size, bool w } } - int setup_vpn_in_socket(const sockaddr_t *sa) { int nfd; - char *addrstr; int option; nfd = socket(sa->sa.sa_family, SOCK_DGRAM, IPPROTO_UDP); @@ -386,11 +391,7 @@ int setup_vpn_in_socket(const sockaddr_t *sa) { return -1; } - if(bind(nfd, &sa->sa, SALEN(sa->sa))) { - closesocket(nfd); - addrstr = sockaddr2hostname(sa); - logger(DEBUG_ALWAYS, LOG_ERR, "Can't bind to %s/udp: %s", addrstr, sockstrerror(sockerrno)); - free(addrstr); + if(!try_bind(nfd, sa, "udp")) { return -1; } @@ -425,7 +426,7 @@ void finish_connecting(connection_t *c) { } static void do_outgoing_pipe(connection_t *c, const char *command) { -#ifndef HAVE_MINGW +#ifndef HAVE_WINDOWS int fd[2]; if(socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) { @@ -647,12 +648,7 @@ begin: c->last_ping_time = time(NULL); c->status.connecting = true; c->name = xstrdup(outgoing->node->name); -#ifndef DISABLE_LEGACY - c->outcipher = myself->connection->outcipher; - c->outdigest = myself->connection->outdigest; -#endif c->outmaclength = myself->connection->outmaclength; - c->outcompression = myself->connection->outcompression; c->last_ping_time = now.tv_sec; connection_add(c); @@ -760,12 +756,7 @@ void handle_new_meta_connection(void *data, int flags) { c = new_connection(); c->name = xstrdup(""); -#ifndef DISABLE_LEGACY - c->outcipher = myself->connection->outcipher; - c->outdigest = myself->connection->outdigest; -#endif c->outmaclength = myself->connection->outmaclength; - c->outcompression = myself->connection->outcompression; c->address = sa; c->hostname = sockaddr2hostname(&sa); @@ -783,7 +774,7 @@ void handle_new_meta_connection(void *data, int flags) { c->allow_request = ID; } -#ifndef HAVE_MINGW +#ifndef HAVE_WINDOWS /* accept a new UNIX socket connection */