From 5869057aba3226bf6d1f996fbd3ba80c97ef359e Mon Sep 17 00:00:00 2001 From: "Coren[m]" Date: Wed, 11 Sep 2013 00:44:05 +0200 Subject: [PATCH] network.c: - reset errno from failed bind() calls if the last one succeeds DHT_bootstrap.c: - move the perror() output next to where it belongs to --- other/DHT_bootstrap.c | 4 ++-- toxcore/network.c | 13 +++++++++++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index 63cb1b75..f5fa9818 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -102,6 +102,8 @@ int main(int argc, char *argv[]) ip_init(&ip, ipv6enabled); DHT *dht = new_DHT(new_net_crypto(new_networking(ip, PORT))); + perror("Initialization"); + manage_keys(dht); printf("Public key: "); uint32_t i; @@ -122,8 +124,6 @@ int main(int argc, char *argv[]) printf("\n"); printf("Port: %u\n", PORT); - perror("Initialization."); - if (argc > argvoffset + 3) { printf("Trying to bootstrap into the network...\n"); uint16_t port = htons(atoi(argv[argvoffset + 2])); diff --git a/toxcore/network.c b/toxcore/network.c index 0b5eba61..7eacd0ca 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -211,7 +211,7 @@ void networking_poll(Networking_Core *net) } } -uint8_t at_startup_ran; +uint8_t at_startup_ran = 0; static int at_startup(void) { if (at_startup_ran != 0) @@ -252,8 +252,10 @@ Networking_Core *new_networking(IP ip, uint16_t port) { #ifdef TOX_ENABLE_IPV6 /* maybe check for invalid IPs like 224+.x.y.z? if there is any IP set ever */ - if (ip.family != AF_INET && ip.family != AF_INET6) + if (ip.family != AF_INET && ip.family != AF_INET6) { + fprintf(stderr, "Invalid address family: %u\n", ip.family); return NULL; + } #endif if (at_startup() != 0) @@ -287,6 +289,7 @@ Networking_Core *new_networking(IP ip, uint16_t port) #else if (temp->sock < 0) { + fprintf(stderr, "Failed to get a scoket?! %u, %s\n", errno, strerror(errno)); free(temp); return NULL; } @@ -409,6 +412,12 @@ Networking_Core *new_networking(IP ip, uint16_t port) sprintf(logbuffer, "Bound successfully to %s:%u.\n", ip_ntoa(&ip), ntohs(temp->port)); loglog(logbuffer); #endif + /* errno isn't reset on success, only set on failure, the failed + * binds with parallel clients yield a -EPERM to the outside if + * errno isn't cleared here */ + if (tries > 0) + errno = 0; + return temp; }