/* run this at startup */ Messenger *initMessenger(void) { Messenger *m = calloc(1, sizeof(Messenger)); if ( ! m ) return NULL; IP ip; ip.i = 0; m->net = new_networking(ip, PORT); if (m->net == NULL) { free(m); return NULL; } m->net_crypto = new_net_crypto(m->net); if (m->net_crypto == NULL) { kill_networking(m->net); free(m); return NULL; } m->dht = new_DHT(m->net_crypto); if (m->dht == NULL) { kill_net_crypto(m->net_crypto); kill_networking(m->net); free(m); return NULL; } new_keys(m->net_crypto); m_set_statusmessage(m, (uint8_t *)"Online", sizeof("Online")); friendreq_init(&(m->fr), m->net_crypto); LANdiscovery_init(m->dht); set_nospam(&(m->fr), random_int()); init_cryptopackets(m->dht); return m; }
/* run this before closing shop */ void cleanupMessenger(Messenger *m) { /* FIXME TODO ideally cleanupMessenger will mirror initMessenger * this requires the other modules to expose cleanup functions */ kill_DHT(m->dht); kill_net_crypto(m->net_crypto); kill_networking(m->net); free(m->friendlist); free(m); }
static void kill_onions(Onions *on) { Networking_Core *net = on->onion->dht->net; DHT *dht = on->onion->dht; Net_Crypto *c = on->onion_c->c; kill_onion_client(on->onion_c); kill_onion_announce(on->onion_a); kill_onion(on->onion); kill_net_crypto(c); kill_DHT(dht); kill_networking(net); free(on); }
void test_addto_lists(IP ip) { Networking_Core *net = new_networking(ip, TOX_PORT_DEFAULT); ck_assert_msg(net != 0, "Failed to create Networking_Core"); DHT *dht = new_DHT(net); ck_assert_msg(dht != 0, "Failed to create DHT"); IP_Port ip_port = { .ip = ip, .port = TOX_PORT_DEFAULT }; uint8_t client_id[CLIENT_ID_SIZE]; int i, used; // check lists filling for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) { randombytes(client_id, sizeof(client_id)); used = addto_lists(dht, ip_port, client_id); ck_assert_msg(used == dht->num_friends + 1, "Wrong number of added clients with existing ip_port"); } for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) { ip_port.port += 1; used = addto_lists(dht, ip_port, client_id); ck_assert_msg(used == dht->num_friends + 1, "Wrong number of added clients with existing client_id"); } for (i = 0; i < MAX(LCLIENT_LIST, MAX_FRIEND_CLIENTS); ++i) { ip_port.port += 1; randombytes(client_id, sizeof(client_id)); used = addto_lists(dht, ip_port, client_id); ck_assert_msg(used >= 1, "Wrong number of added clients"); } /*check: Current behavior if there are two clients with the same id is * to replace the first ip by the second. */ test_addto_lists_update(dht, dht->close_clientlist, LCLIENT_LIST, &ip_port); for (i = 0; i < dht->num_friends; ++i) test_addto_lists_update(dht, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, &ip_port); // check "bad" entries test_addto_lists_bad(dht, dht->close_clientlist, LCLIENT_LIST, &ip_port); for (i = 0; i < dht->num_friends; ++i) test_addto_lists_bad(dht, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, &ip_port); // check "possibly bad" entries /* test_addto_lists_possible_bad(dht, dht->close_clientlist, LCLIENT_LIST, &ip_port, dht->self_public_key); for (i = 0; i < dht->num_friends; ++i) test_addto_lists_possible_bad(dht, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, &ip_port, dht->friends_list[i].client_id); */ // check "good" entries test_addto_lists_good(dht, dht->close_clientlist, LCLIENT_LIST, &ip_port, dht->self_public_key); for (i = 0; i < dht->num_friends; ++i) test_addto_lists_good(dht, dht->friends_list[i].client_list, MAX_FRIEND_CLIENTS, &ip_port, dht->friends_list[i].client_id); kill_DHT(dht); kill_networking(net); }
/* Initialize networking. * Bind to ip and port. * ip must be in network order EX: 127.0.0.1 = (7F000001). * port is in host byte order (this means don't worry about it). * * return Networking_Core object if no problems * return NULL if there are problems. */ 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) { #ifdef DEBUG fprintf(stderr, "Invalid address family: %u\n", ip.family); #endif return NULL; } #endif if (at_startup() != 0) return NULL; Networking_Core *temp = calloc(1, sizeof(Networking_Core)); if (temp == NULL) return NULL; #ifdef TOX_ENABLE_IPV6 temp->family = ip.family; #else temp->family = AF_INET; #endif temp->port = 0; /* Initialize our socket. */ /* add log message what we're creating */ temp->sock = socket(temp->family, SOCK_DGRAM, IPPROTO_UDP); /* Check for socket error. */ #ifdef WIN32 if (temp->sock == INVALID_SOCKET) { /* MSDN recommends this. */ free(temp); return NULL; } #else if (temp->sock < 0) { #ifdef DEBUG fprintf(stderr, "Failed to get a socket?! %u, %s\n", errno, strerror(errno)); #endif free(temp); return NULL; } #endif /* Functions to increase the size of the send and receive UDP buffers. */ int n = 1024 * 1024 * 2; setsockopt(temp->sock, SOL_SOCKET, SO_RCVBUF, (char *)&n, sizeof(n)); setsockopt(temp->sock, SOL_SOCKET, SO_SNDBUF, (char *)&n, sizeof(n)); /* Enable broadcast on socket */ int broadcast = 1; setsockopt(temp->sock, SOL_SOCKET, SO_BROADCAST, (char *)&broadcast, sizeof(broadcast)); /* Set socket nonblocking. */ #ifdef WIN32 /* I think this works for Windows. */ u_long mode = 1; /* ioctl(sock, FIONBIO, &mode); */ ioctlsocket(temp->sock, FIONBIO, &mode); #else fcntl(temp->sock, F_SETFL, O_NONBLOCK, 1); #endif /* Bind our socket to port PORT and the given IP address (usually 0.0.0.0 or ::) */ uint16_t *portptr = NULL; struct sockaddr_storage addr; size_t addrsize; #ifdef TOX_ENABLE_IPV6 if (temp->family == AF_INET) { IP4 ip4 = ip.ip4; #else IP4 ip4 = ip; #endif addrsize = sizeof(struct sockaddr_in); struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; addr4->sin_family = AF_INET; addr4->sin_port = 0; addr4->sin_addr = ip4.in_addr; portptr = &addr4->sin_port; #ifdef TOX_ENABLE_IPV6 } else if (temp->family == AF_INET6) { addrsize = sizeof(struct sockaddr_in6); struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; addr6->sin6_family = AF_INET6; addr6->sin6_port = 0; addr6->sin6_addr = ip.ip6.in6_addr; addr6->sin6_flowinfo = 0; addr6->sin6_scope_id = 0; portptr = &addr6->sin6_port; } else return NULL; if (ip.family == AF_INET6) { char ipv6only = 0; #ifdef LOGGING errno = 0; int res = #endif setsockopt(temp->sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, sizeof(ipv6only)); #ifdef LOGGING if (res < 0) { sprintf(logbuffer, "Failed to enable dual-stack on IPv6 socket, won't be able to receive from/send to IPv4 addresses. (%u, %s)\n", errno, strerror(errno)); loglog(logbuffer); } else loglog("Embedded IPv4 addresses enabled successfully.\n"); #endif /* multicast local nodes */ struct ipv6_mreq mreq; memset(&mreq, 0, sizeof(mreq)); mreq.ipv6mr_multiaddr.s6_addr[ 0] = 0xFF; mreq.ipv6mr_multiaddr.s6_addr[ 1] = 0x02; mreq.ipv6mr_multiaddr.s6_addr[15] = 0x01; mreq.ipv6mr_interface = 0; #ifdef LOGGING errno = 0; res = #endif setsockopt(temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)); #ifdef LOGGING if (res < 0) { sprintf(logbuffer, "Failed to activate local multicast membership. (%u, %s)\n", errno, strerror(errno)); loglog(logbuffer); } else loglog("Local multicast group FF02::1 joined successfully.\n"); #endif } #endif /* a hanging program or a different user might block the standard port; * as long as it isn't a parameter coming from the commandline, * try a few ports after it, to see if we can find a "free" one * * if we go on without binding, the first sendto() automatically binds to * a free port chosen by the system (i.e. anything from 1024 to 65535) * * returning NULL after bind fails has both advantages and disadvantages: * advantage: * we can rely on getting the port in the range 33445..33450, which * enables us to tell joe user to open their firewall to a small range * * disadvantage: * some clients might not test return of tox_new(), blindly assuming that * it worked ok (which it did previously without a successful bind) */ uint16_t port_to_try = port; *portptr = htons(port_to_try); int tries, res; for (tries = TOX_PORTRANGE_FROM; tries <= TOX_PORTRANGE_TO; tries++) { res = bind(temp->sock, (struct sockaddr *)&addr, addrsize); if (!res) { temp->port = *portptr; #ifdef LOGGING loginit(temp->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; } port_to_try++; if (port_to_try > TOX_PORTRANGE_TO) port_to_try = TOX_PORTRANGE_FROM; *portptr = htons(port_to_try); } #ifdef DEBUG fprintf(stderr, "Failed to bind socket: %u, %s (IP/Port: %s:%u\n", errno, strerror(errno), ip_ntoa(&ip), port); #endif kill_networking(temp); return NULL; }
void test_list_main() { DHT *dhts[NUM_DHT]; uint8_t cmp_list1[NUM_DHT][MAX_FRIEND_CLIENTS][crypto_box_PUBLICKEYBYTES + 1]; memset(cmp_list1, 0, sizeof(cmp_list1)); IP ip; ip_init(&ip, 1); unsigned int i, j, k, l; for (i = 0; i < NUM_DHT; ++i) { IP ip; ip_init(&ip, 1); dhts[i] = new_DHT(new_networking(ip, DHT_DEFAULT_PORT + i)); ck_assert_msg(dhts[i] != 0, "Failed to create dht instances %u", i); ck_assert_msg(dhts[i]->net->port != DHT_DEFAULT_PORT + i, "Bound to wrong port"); } for (j = 0; j < NUM_DHT; ++j) { for (i = 1; i < NUM_DHT; ++i) { test_add_to_list(cmp_list1[j], MAX_FRIEND_CLIENTS, dhts[(i + j) % NUM_DHT]->self_public_key, dhts[j]->self_public_key); } } for (j = 0; j < NUM_DHT; ++j) { for (i = 0; i < NUM_DHT; ++i) { if (i == j) continue; IP_Port ip_port; ip_init(&ip_port.ip, 0); ip_port.ip.ip4.uint32 = rand(); ip_port.port = rand() % (UINT16_MAX - 1); ++ip_port.port; addto_lists(dhts[j], ip_port, dhts[i]->self_public_key); } } /* print_pk(dhts[0]->self_public_key); for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { printf("----Entry %u----\n", i); print_pk(cmp_list1[i]); } */ unsigned int m_count = 0; for (l = 0; l < NUM_DHT; ++l) { for (i = 0; i < MAX_FRIEND_CLIENTS; ++i) { for (j = 1; j < NUM_DHT; ++j) { if (memcmp(cmp_list1[l][i], dhts[(l + j) % NUM_DHT]->self_public_key, crypto_box_PUBLICKEYBYTES) != 0) continue; unsigned int count = 0; for (k = 0; k < LCLIENT_LIST; ++k) { if (memcmp(dhts[l]->self_public_key, dhts[(l + j) % NUM_DHT]->close_clientlist[k].public_key, crypto_box_PUBLICKEYBYTES) == 0) ++count; } if (count != 1) { print_pk(dhts[l]->self_public_key); for (k = 0; k < MAX_FRIEND_CLIENTS; ++k) { printf("----Entry %u----\n", k); print_pk(cmp_list1[l][k]); } for (k = 0; k < LCLIENT_LIST; ++k) { printf("----Closel %u----\n", k); print_pk(dhts[(l + j) % NUM_DHT]->close_clientlist[k].public_key); } print_pk(dhts[(l + j) % NUM_DHT]->self_public_key); } ck_assert_msg(count == 1, "Nodes in search don't know ip of friend. %u %u %u", i, j, count); Node_format ln[MAX_SENT_NODES]; int n = get_close_nodes(dhts[(l + j) % NUM_DHT], dhts[l]->self_public_key, ln, 0, 1, 0); ck_assert_msg(n == MAX_SENT_NODES, "bad num close %u | %u %u", n, i, j); count = 0; for (k = 0; k < MAX_SENT_NODES; ++k) { if (memcmp(dhts[l]->self_public_key, ln[k].public_key, crypto_box_PUBLICKEYBYTES) == 0) ++count; } ck_assert_msg(count == 1, "Nodes in search don't know ip of friend. %u %u %u", i, j, count); /* for (k = 0; k < MAX_SENT_NODES; ++k) { printf("----gn %u----\n", k); print_pk(ln[k].public_key); }*/ ++m_count; } } } ck_assert_msg(m_count == (NUM_DHT) * (MAX_FRIEND_CLIENTS), "Bad count. %u != %u", m_count, (NUM_DHT) * (MAX_FRIEND_CLIENTS)); for (i = 0; i < NUM_DHT; ++i) { void *n = dhts[i]->net; kill_DHT(dhts[i]); kill_networking(n); } }