diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index 246ae818b..484a511c5 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -94,6 +94,7 @@ int main(int argc, char *argv[]) /* let user override default by cmdline */ uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled); + if (argvoffset < 0) exit(1); @@ -130,7 +131,7 @@ int main(int argc, char *argv[]) uint16_t port = htons(atoi(argv[argvoffset + 2])); uint8_t *bootstrap_key = hex_string_to_bin(argv[argvoffset + 3]); int res = DHT_bootstrap_from_address(dht, argv[argvoffset + 1], - ipv6enabled, port, bootstrap_key); + ipv6enabled, port, bootstrap_key); free(bootstrap_key); if (!res) { @@ -151,6 +152,7 @@ int main(int argc, char *argv[]) } do_DHT(dht); + if (last_LANdiscovery + (is_waiting_for_dht_connection ? 5 : LAN_DISCOVERY_INTERVAL) < unix_time()) { send_LANdiscovery(htons(PORT), dht->c); last_LANdiscovery = unix_time(); diff --git a/testing/DHT_test.c b/testing/DHT_test.c index 9eab60dea..d76057c15 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c @@ -141,6 +141,7 @@ int main(int argc, char *argv[]) /* let user override default by cmdline */ uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled); + if (argvoffset < 0) exit(1); @@ -177,6 +178,7 @@ int main(int argc, char *argv[]) unsigned char *binary_string = hex_string_to_bin(argv[argvoffset + 3]); int res = DHT_bootstrap_from_address(dht, argv[argvoffset + 1], ipv6enabled, port, binary_string); free(binary_string); + if (!res) { printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]); return 1; diff --git a/testing/Lossless_UDP_testclient.c b/testing/Lossless_UDP_testclient.c index 155a412a1..3c52c6d6d 100644 --- a/testing/Lossless_UDP_testclient.c +++ b/testing/Lossless_UDP_testclient.c @@ -155,6 +155,7 @@ int main(int argc, char *argv[]) /* let user override default by cmdline */ uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled); + if (argvoffset < 0) exit(1); @@ -184,10 +185,12 @@ int main(int argc, char *argv[]) IP_Port serverip; ip_init(&serverip.ip, ipv6enabled); + if (!addr_resolve(argv[argvoffset + 1], &serverip.ip, NULL)) { printf("Failed to convert \"%s\" into an IP address.\n", argv[argvoffset + 1]); return 1; } + serverip.port = htons(atoi(argv[argvoffset + 2])); printip(serverip); diff --git a/testing/Lossless_UDP_testserver.c b/testing/Lossless_UDP_testserver.c index 0fd4edd96..9d061c0cf 100644 --- a/testing/Lossless_UDP_testserver.c +++ b/testing/Lossless_UDP_testserver.c @@ -151,6 +151,7 @@ int main(int argc, char *argv[]) /* let user override default by cmdline */ uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled); + if (argvoffset < 0) exit(1); diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index 06f8bdbf7..7b94364ae 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c @@ -98,6 +98,7 @@ int main(int argc, char *argv[]) /* let user override default by cmdline */ uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled); + if (argvoffset < 0) exit(1); @@ -120,14 +121,16 @@ int main(int argc, char *argv[]) uint16_t port = htons(atoi(argv[argvoffset + 2])); uint8_t *bootstrap_key = hex_string_to_bin(argv[argvoffset + 3]); int res = DHT_bootstrap_from_address(m->dht, argv[argvoffset + 1], - ipv6enabled, port, bootstrap_key); + ipv6enabled, port, bootstrap_key); free(bootstrap_key); + if (!res) { printf("Failed to convert \"%s\" into an IP address. Exiting...\n", argv[argvoffset + 1]); exit(1); } } else { FILE *file = fopen(argv[argvoffset + 1], "rb"); + if ( file == NULL ) { printf("Failed to open \"%s\" - does it exist?\n", argv[argvoffset + 1]); return 1; diff --git a/testing/misc_tools.c b/testing/misc_tools.c index 824200d83..81f5ed8a2 100644 --- a/testing/misc_tools.c +++ b/testing/misc_tools.c @@ -51,10 +51,12 @@ unsigned char *hex_string_to_bin(char hex_string[]) int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled) { int argvoffset = 0, argi; - for(argi = 1; argi < argc; argi++) + + for (argi = 1; argi < argc; argi++) if (!strncasecmp(argv[argi], "--ipv", 5)) { if (argv[argi][5] && !argv[argi][6]) { char c = argv[argi][5]; + if (c == '4') *ipv6enabled = 0; else if (c == '6') @@ -63,8 +65,7 @@ int cmdline_parsefor_ipv46(int argc, char **argv, uint8_t *ipv6enabled) printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]); return -1; } - } - else { + } else { printf("Invalid argument: %s. Try --ipv4 or --ipv6!\n", argv[argi]); return -1; } diff --git a/testing/nTox.c b/testing/nTox.c index 70dc5c77e..93130307c 100644 --- a/testing/nTox.c +++ b/testing/nTox.c @@ -582,6 +582,7 @@ int main(int argc, char *argv[]) /* let user override default by cmdline */ uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */ int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled); + if (argvoffset < 0) exit(1); @@ -641,6 +642,7 @@ int main(int argc, char *argv[]) } nodelay(stdscr, TRUE); + while (1) { if (on == 0 && tox_isconnected(m)) { new_lines("[i] connected to DHT\n[i] define username with /n"); diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 255074b0d..ff20c892b 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -169,12 +169,12 @@ void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key); * if ipv6enabled is 0 (zero), the resolving sticks STRICTLY to IPv4 addresses * if ipv6enabled is not 0 (zero), the resolving looks for IPv6 addresses first, * then IPv4 addresses. - * + * * returns 1 if the address could be converted into an IP address * returns 0 otherwise */ int DHT_bootstrap_from_address(DHT *dht, const char *address, uint8_t ipv6enabled, - uint16_t port, uint8_t *public_key); + uint16_t port, uint8_t *public_key); /* Add nodes to the toping list. * All nodes in this list are pinged every TIME_TOPING seconds diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index 92e42e0f5..ed8dd229b 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -35,7 +35,7 @@ * return higher than 0 on success. * return 0 on error. */ -static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, uint8_t * data, uint16_t length) +static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, uint8_t *data, uint16_t length) { /* Not sure how many platforms this will run on, * so it's wrapped in __linux for now. @@ -63,16 +63,17 @@ static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, uint8_t * d } for (i = 0; i < count; i++) { - if (ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) { - return 1; - } + if (ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) { + return 1; + } - /* Just to clarify where we're getting the values from. */ - sock_holder = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr; - if (sock_holder != NULL) { - IP_Port ip_port = {{{{sock_holder->sin_addr.s_addr}}, port, 0}}; - sendpacket(net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES); - } + /* Just to clarify where we're getting the values from. */ + sock_holder = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr; + + if (sock_holder != NULL) { + IP_Port ip_port = {{{{sock_holder->sin_addr.s_addr}}, port, 0}}; + sendpacket(net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES); + } } close(sock); @@ -87,6 +88,7 @@ static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast) ip_reset(&ip); #ifdef TOX_ENABLE_IPV6 + if (family_socket == AF_INET6) { if (family_broadcast == AF_INET6) { ip.family = AF_INET6; @@ -96,25 +98,26 @@ static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast) ip.ip6.s6_addr[ 0] = 0xFF; ip.ip6.s6_addr[ 1] = 0x02; ip.ip6.s6_addr[15] = 0x01; - } - else if (family_broadcast == AF_INET) { + } else if (family_broadcast == AF_INET) { ip.family = AF_INET6; ip.ip6.s6_addr32[0] = 0; ip.ip6.s6_addr32[1] = 0; ip.ip6.s6_addr32[2] = htonl(0xFFFF); ip.ip6.s6_addr32[3] = INADDR_BROADCAST; } - } - else if (family_socket == AF_INET) { + } else if (family_socket == AF_INET) { if (family_broadcast == AF_INET) { ip.family = AF_INET; ip.ip4.uint32 = INADDR_BROADCAST; } } + #else + if (family_socket == AF_INET) if (family_broadcast == AF_INET) ip.uint32 = INADDR_BROADCAST; + #endif return ip; @@ -126,11 +129,13 @@ static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast) static int LAN_ip(IP ip) { #ifdef TOX_ENABLE_IPV6 + if (ip.family == AF_INET) { IP4 ip4 = ip.ip4; #else - IP4 ip4 = ip; + IP4 ip4 = ip; #endif + /* Loopback. */ if (ip4.uint8[0] == 127) return 0; @@ -151,13 +156,14 @@ static int LAN_ip(IP ip) if (ip4.uint8[0] == 169 && ip4.uint8[1] == 254 && ip4.uint8[2] != 0 && ip4.uint8[2] != 255) return 0; + #ifdef TOX_ENABLE_IPV6 - } - else if (ip.family == AF_INET6) { + } else if (ip.family == AF_INET6) + { /* autogenerated for each interface: FE80::* (up to FEBF::*) /* FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */ if (((ip.ip6.s6_addr[0] == 0xFF) && (ip.ip6.s6_addr[1] < 3) && (ip.ip6.s6_addr[15] == 1)) || - ((ip.ip6.s6_addr[0] == 0xFE) && ((ip.ip6.s6_addr[1] & 0xC0) == 0x80))) + ((ip.ip6.s6_addr[0] == 0xFE) && ((ip.ip6.s6_addr[1] & 0xC0) == 0x80))) return 0; /* embedded IPv4-in-IPv6 */ @@ -168,6 +174,7 @@ static int LAN_ip(IP ip) return LAN_ip(ip4); } } + #endif return -1; @@ -203,9 +210,11 @@ int send_LANdiscovery(uint16_t port, Net_Crypto *c) ip_port.port = port; #ifdef TOX_ENABLE_IPV6 + /* IPv6 multicast */ if (c->lossless_udp->net->family == AF_INET6) { ip_port.ip = broadcast_ip(AF_INET6, AF_INET6); + if (ip_isset(&ip_port.ip)) if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES) > 0) res = 1; @@ -216,6 +225,7 @@ int send_LANdiscovery(uint16_t port, Net_Crypto *c) #else ip_port.ip = broadcast_ip(AF_INET, AF_INET); #endif + if (ip_isset(&ip_port.ip)) if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES)) res = 1; diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 55b273535..eb18e3a33 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -1225,11 +1225,13 @@ static time_t lastdump = 0; static char IDString[CLIENT_ID_SIZE * 2 + 1]; static char *ID2String(uint8_t *client_id) { - uint32_t i; - for(i = 0; i < CLIENT_ID_SIZE; i++) - sprintf(&IDString[i], "%02X", client_id[i]); - IDString[CLIENT_ID_SIZE * 2] = 0; - return IDString; + uint32_t i; + + for (i = 0; i < CLIENT_ID_SIZE; i++) + sprintf(&IDString[i], "%02X", client_id[i]); + + IDString[CLIENT_ID_SIZE * 2] = 0; + return IDString; } #endif @@ -1246,73 +1248,85 @@ void doMessenger(Messenger *m) LANdiscovery(m); #ifdef LOGGING - if (now() > lastdump + DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS) { - loglog(" = = = = = = = = \n"); - lastdump = now(); - uint32_t client, last_pinged; - for(client = 0; client < LCLIENT_LIST; client++) { - Client_data *cptr = &m->dht->close_clientlist[client]; - if (ip_isset(&cptr->ip_port.ip)) { - last_pinged = lastdump - cptr->last_pinged; - if (last_pinged > 999) - last_pinged = 999; - snprintf(logbuffer, sizeof(logbuffer), "C[%2u] %s:%u [%3u] %s\n", - client, ip_ntoa(&cptr->ip_port.ip), ntohs(cptr->ip_port.port), - last_pinged, ID2String(cptr->client_id)); - loglog(logbuffer); - } - } + if (now() > lastdump + DUMPING_CLIENTS_FRIENDS_EVERY_N_SECONDS) { + loglog(" = = = = = = = = \n"); - loglog(" = = = = = = = = \n"); + lastdump = now(); + uint32_t client, last_pinged; - uint32_t num_friends = MIN(m->numfriends, m->dht->num_friends); - if (m->numfriends != m->dht->num_friends) { - sprintf(logbuffer, "Friend num in DHT %u != friend num in msger %u\n", - m->dht->num_friends, m->numfriends); - loglog(logbuffer); - } + for (client = 0; client < LCLIENT_LIST; client++) { + Client_data *cptr = &m->dht->close_clientlist[client]; - uint32_t friend, ping_lastrecv; - for(friend = 0; friend < num_friends; friend++) { - Friend *msgfptr = &m->friendlist[friend]; - DHT_Friend *dhtfptr = &m->dht->friends_list[friend]; - if (memcmp(msgfptr->client_id, dhtfptr->client_id, CLIENT_ID_SIZE)) { - if (sizeof(logbuffer) > 2 * CLIENT_ID_SIZE + 64) { - sprintf(logbuffer, "F[%2u] ID(m) %s != ID(d) ", friend, - ID2String(msgfptr->client_id)); - strcat(logbuffer + strlen(logbuffer), ID2String(dhtfptr->client_id)); - strcat(logbuffer + strlen(logbuffer), "\n"); - } - else - sprintf(logbuffer, "F[%2u] ID(m) != ID(d) ", friend); + if (ip_isset(&cptr->ip_port.ip)) { + last_pinged = lastdump - cptr->last_pinged; - loglog(logbuffer); - } + if (last_pinged > 999) + last_pinged = 999; - ping_lastrecv = lastdump - msgfptr->ping_lastrecv; - if (ping_lastrecv > 999) - ping_lastrecv = 999; - snprintf(logbuffer, sizeof(logbuffer), "F[%2u] <%s> %02u [%03u] %s\n", - friend, msgfptr->name, msgfptr->crypt_connection_id, - ping_lastrecv, ID2String(msgfptr->client_id)); - loglog(logbuffer); + snprintf(logbuffer, sizeof(logbuffer), "C[%2u] %s:%u [%3u] %s\n", + client, ip_ntoa(&cptr->ip_port.ip), ntohs(cptr->ip_port.port), + last_pinged, ID2String(cptr->client_id)); + loglog(logbuffer); + } + } - for(client = 0; client < MAX_FRIEND_CLIENTS; client++) { - Client_data *cptr = &dhtfptr->client_list[client]; - last_pinged = lastdump - cptr->last_pinged; - if (last_pinged > 999) - last_pinged = 999; - snprintf(logbuffer, sizeof(logbuffer), "F[%2u] => C[%2u] %s:%u [%3u] %s\n", - friend, client, ip_ntoa(&cptr->ip_port.ip), - ntohs(cptr->ip_port.port), last_pinged, - ID2String(cptr->client_id)); - loglog(logbuffer); - } - } + loglog(" = = = = = = = = \n"); + + uint32_t num_friends = MIN(m->numfriends, m->dht->num_friends); + + if (m->numfriends != m->dht->num_friends) { + sprintf(logbuffer, "Friend num in DHT %u != friend num in msger %u\n", + m->dht->num_friends, m->numfriends); + loglog(logbuffer); + } + + uint32_t friend, ping_lastrecv; + + for (friend = 0; friend < num_friends; friend++) { + Friend *msgfptr = &m->friendlist[friend]; + DHT_Friend *dhtfptr = &m->dht->friends_list[friend]; + + if (memcmp(msgfptr->client_id, dhtfptr->client_id, CLIENT_ID_SIZE)) { + if (sizeof(logbuffer) > 2 * CLIENT_ID_SIZE + 64) { + sprintf(logbuffer, "F[%2u] ID(m) %s != ID(d) ", friend, + ID2String(msgfptr->client_id)); + strcat(logbuffer + strlen(logbuffer), ID2String(dhtfptr->client_id)); + strcat(logbuffer + strlen(logbuffer), "\n"); + } else + sprintf(logbuffer, "F[%2u] ID(m) != ID(d) ", friend); + + loglog(logbuffer); + } + + ping_lastrecv = lastdump - msgfptr->ping_lastrecv; + + if (ping_lastrecv > 999) + ping_lastrecv = 999; + + snprintf(logbuffer, sizeof(logbuffer), "F[%2u] <%s> %02u [%03u] %s\n", + friend, msgfptr->name, msgfptr->crypt_connection_id, + ping_lastrecv, ID2String(msgfptr->client_id)); + loglog(logbuffer); + + for (client = 0; client < MAX_FRIEND_CLIENTS; client++) { + Client_data *cptr = &dhtfptr->client_list[client]; + last_pinged = lastdump - cptr->last_pinged; + + if (last_pinged > 999) + last_pinged = 999; + + snprintf(logbuffer, sizeof(logbuffer), "F[%2u] => C[%2u] %s:%u [%3u] %s\n", + friend, client, ip_ntoa(&cptr->ip_port.ip), + ntohs(cptr->ip_port.port), last_pinged, + ID2String(cptr->client_id)); + loglog(logbuffer); + } + } + + loglog(" = = = = = = = = \n"); + } - loglog(" = = = = = = = = \n"); - } #endif } @@ -1410,6 +1424,7 @@ int Messenger_load(Messenger *m, uint8_t *data, uint32_t length) memcpy(temp, data, size); uint32_t i; + for (i = 0; i < num; ++i) { if (temp[i].status >= 3) { int fnum = m_addfriend_norequest(m, temp[i].client_id); diff --git a/toxcore/ping.c b/toxcore/ping.c index 113702bfd..56ce2f593 100644 --- a/toxcore/ping.c +++ b/toxcore/ping.c @@ -113,7 +113,7 @@ bool is_pinging(void *ping, IP_Port ipp, uint64_t ping_id) // O(n) TODO: Repl /* ping_id = 0 means match any id. */ if ((!ip_isset(&ipp.ip) || ipport_equal(&png->pings[id].ipp, &ipp)) && - (png->pings[id].id == ping_id || ping_id == 0)) { + (png->pings[id].id == ping_id || ping_id == 0)) { return true; } } diff --git a/toxcore/tox.c b/toxcore/tox.c index 68b1e6e90..80d646269 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -450,7 +450,7 @@ void tox_bootstrap_from_ip(void *tox, IP_Port ip_port, uint8_t *public_key) DHT_bootstrap(m->dht, ip_port, public_key); } int tox_bootstrap_from_address(void *tox, const char *address, - uint8_t ipv6enabled, uint16_t port, uint8_t *public_key) + uint8_t ipv6enabled, uint16_t port, uint8_t *public_key) { Messenger *m = tox; return DHT_bootstrap_from_address(m->dht, address, ipv6enabled, port, public_key); diff --git a/toxcore/tox.h b/toxcore/tox.h index c52a644fe..a15d7ea0d 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h @@ -409,12 +409,12 @@ void tox_bootstrap_from_ip(Tox *tox, tox_IP_Port ip_port, uint8_t *public_key); * if ipv6enabled is 0 (zero), the resolving sticks STRICTLY to IPv4 addresses * if ipv6enabled is not 0 (zero), the resolving looks for IPv6 addresses first, * then IPv4 addresses. - * + * * returns 1 if the address could be converted into an IP address * returns 0 otherwise */ int tox_bootstrap_from_address(Tox *tox, const char *address, uint8_t ipv6enabled, - uint16_t port, uint8_t *public_key); + uint16_t port, uint8_t *public_key); /* return 0 if we are not connected to the DHT. * return 1 if we are.