From b19a9e54643eceaa52e71c0f38fa2ecae5a6809e Mon Sep 17 00:00:00 2001 From: Diadlo Date: Sat, 7 Jan 2017 11:28:53 +0300 Subject: [PATCH] Add part of platform-independent network API implementation socket -> net_socket htons -> net_htons htonl -> net_htonl connect -> net_connect sendto -> net_sendto_ip4 getaddrinfo -> net_getipport sa_family_t -> Family --- auto_tests/TCP_test.c | 47 ++-- auto_tests/crypto_test.c | 4 +- auto_tests/dht_test.c | 2 +- auto_tests/network_test.c | 68 +++--- other/DHT_bootstrap.c | 6 +- other/bootstrap_daemon/src/config.c | 2 +- other/bootstrap_daemon/src/tox-bootstrapd.c | 4 +- other/bootstrap_node_packets.c | 2 +- testing/DHT_test.c | 10 +- testing/Messenger_test.c | 2 +- testing/dns3_test.c | 33 +-- toxav/audio.c | 2 +- toxav/bwcontroller.c | 8 +- toxav/groupav.c | 4 +- toxav/rtp.c | 52 ++--- toxav/toxav.c | 2 +- toxcore/DHT.c | 28 +-- toxcore/DHT.h | 2 +- toxcore/LAN_discovery.c | 14 +- toxcore/Messenger.c | 20 +- toxcore/TCP_client.c | 30 +-- toxcore/TCP_server.c | 37 +-- toxcore/crypto_core.c | 2 +- toxcore/friend_connection.c | 2 +- toxcore/group.c | 50 ++--- toxcore/net_crypto.c | 12 +- toxcore/network.c | 235 ++++++++++++++++++-- toxcore/network.h | 54 ++++- toxcore/tox.c | 84 ++----- 29 files changed, 480 insertions(+), 338 deletions(-) diff --git a/auto_tests/TCP_test.c b/auto_tests/TCP_test.c index a5a70c4b9..bc684af96 100644 --- a/auto_tests/TCP_test.c +++ b/auto_tests/TCP_test.c @@ -38,13 +38,15 @@ START_TEST(test_basic) ck_assert_msg(tcp_s != NULL, "Failed to create TCP relay server"); ck_assert_msg(tcp_server_listen_count(tcp_s) == NUM_PORTS, "Failed to bind to all ports"); - Socket sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); - struct sockaddr_in6 addr6_loopback = {0}; - addr6_loopback.sin6_family = AF_INET6; - addr6_loopback.sin6_port = htons(ports[rand() % NUM_PORTS]); - addr6_loopback.sin6_addr = in6addr_loopback; + Socket sock = net_socket(TOX_AF_INET6, TOX_SOCK_STREAM, TOX_PROTO_TCP); + IP_Port ip_port_loopback; + ip_port_loopback.ip.family = AF_INET6; + ip_port_loopback.ip.ip6.uint64[0] = 0; + ip_port_loopback.ip.ip6.uint64[1] = 0; + ip_port_loopback.ip.ip6.uint8[15] = 1; // ::1 + ip_port_loopback.port = net_htons(ports[rand() % NUM_PORTS]); - int ret = connect(sock, (struct sockaddr *)&addr6_loopback, sizeof(addr6_loopback)); + int ret = net_connect(sock, ip_port_loopback); ck_assert_msg(ret == 0, "Failed to connect to TCP relay server"); uint8_t f_public_key[CRYPTO_PUBLIC_KEY_SIZE]; @@ -93,7 +95,7 @@ START_TEST(test_basic) memcpy(r_req_p + 1, f_public_key, CRYPTO_PUBLIC_KEY_SIZE); uint8_t r_req[2 + 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE]; uint16_t size = 1 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE; - size = htons(size); + size = net_htons(size); encrypt_data_symmetric(f_shared_key, f_nonce, r_req_p, 1 + CRYPTO_PUBLIC_KEY_SIZE, r_req + 2); increment_nonce(f_nonce); memcpy(r_req, &size, 2); @@ -113,7 +115,7 @@ START_TEST(test_basic) ck_assert_msg(recv_data_len == 2 + 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, "recv Failed. %u", recv_data_len); memcpy(&size, packet_resp, 2); - ck_assert_msg(ntohs(size) == 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, "Wrong packet size."); + ck_assert_msg(net_ntohs(size) == 2 + CRYPTO_PUBLIC_KEY_SIZE + CRYPTO_MAC_SIZE, "Wrong packet size."); uint8_t packet_resp_plain[4096]; ret = decrypt_data_symmetric(f_shared_key, f_nonce_r, packet_resp + 2, recv_data_len - 2, packet_resp_plain); ck_assert_msg(ret != -1, "decryption failed"); @@ -136,13 +138,16 @@ struct sec_TCP_con { static struct sec_TCP_con *new_TCP_con(TCP_Server *tcp_s) { struct sec_TCP_con *sec_c = (struct sec_TCP_con *)malloc(sizeof(struct sec_TCP_con)); - Socket sock = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP); - struct sockaddr_in6 addr6_loopback = {0}; - addr6_loopback.sin6_family = AF_INET6; - addr6_loopback.sin6_port = htons(ports[rand() % NUM_PORTS]); - addr6_loopback.sin6_addr = in6addr_loopback; + Socket sock = net_socket(TOX_AF_INET6, TOX_SOCK_STREAM, TOX_PROTO_TCP); - int ret = connect(sock, (struct sockaddr *)&addr6_loopback, sizeof(addr6_loopback)); + IP_Port ip_port_loopback; + ip_port_loopback.ip.family = AF_INET6; + ip_port_loopback.ip.ip6.uint64[0] = 0; + ip_port_loopback.ip.ip6.uint64[1] = 0; + ip_port_loopback.ip.ip6.uint8[15] = 1; // ::1 + ip_port_loopback.port = net_htons(ports[rand() % NUM_PORTS]); + + int ret = net_connect(sock, ip_port_loopback); ck_assert_msg(ret == 0, "Failed to connect to TCP relay server"); uint8_t f_secret_key[CRYPTO_SECRET_KEY_SIZE]; @@ -190,7 +195,7 @@ static int write_packet_TCP_secure_connection(struct sec_TCP_con *con, uint8_t * { VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE); - uint16_t c_length = htons(length + CRYPTO_MAC_SIZE); + uint16_t c_length = net_htons(length + CRYPTO_MAC_SIZE); memcpy(packet, &c_length, sizeof(uint16_t)); int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t)); @@ -405,7 +410,7 @@ START_TEST(test_client) crypto_new_keypair(f_public_key, f_secret_key); IP_Port ip_port_tcp_s; - ip_port_tcp_s.port = htons(ports[rand() % NUM_PORTS]); + ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); ip_port_tcp_s.ip.family = AF_INET6; get_ip6(&ip_port_tcp_s.ip.ip6, &in6addr_loopback); TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, 0); @@ -435,7 +440,7 @@ START_TEST(test_client) uint8_t f2_public_key[CRYPTO_PUBLIC_KEY_SIZE]; uint8_t f2_secret_key[CRYPTO_SECRET_KEY_SIZE]; crypto_new_keypair(f2_public_key, f2_secret_key); - ip_port_tcp_s.port = htons(ports[rand() % NUM_PORTS]); + ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); TCP_Client_Connection *conn2 = new_TCP_connection(ip_port_tcp_s, self_public_key, f2_public_key, f2_secret_key, 0); routing_response_handler(conn, response_callback, (char *)conn + 2); routing_status_handler(conn, status_callback, (void *)2); @@ -503,7 +508,7 @@ START_TEST(test_client_invalid) crypto_new_keypair(f_public_key, f_secret_key); IP_Port ip_port_tcp_s; - ip_port_tcp_s.port = htons(ports[rand() % NUM_PORTS]); + ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); ip_port_tcp_s.ip.family = AF_INET6; get_ip6(&ip_port_tcp_s.ip.ip6, &in6addr_loopback); TCP_Client_Connection *conn = new_TCP_connection(ip_port_tcp_s, self_public_key, f_public_key, f_secret_key, 0); @@ -572,7 +577,7 @@ START_TEST(test_tcp_connection) IP_Port ip_port_tcp_s; - ip_port_tcp_s.port = htons(ports[rand() % NUM_PORTS]); + ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); ip_port_tcp_s.ip.family = AF_INET6; get_ip6(&ip_port_tcp_s.ip.ip6, &in6addr_loopback); @@ -581,7 +586,7 @@ START_TEST(test_tcp_connection) ck_assert_msg(add_tcp_relay_connection(tc_1, connection, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0, "Could not add tcp relay to connection\n"); - ip_port_tcp_s.port = htons(ports[rand() % NUM_PORTS]); + ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); connection = new_tcp_connection_to(tc_2, tcp_connections_public_key(tc_1), 123); ck_assert_msg(connection == 0, "Connection id wrong"); ck_assert_msg(add_tcp_relay_connection(tc_2, connection, ip_port_tcp_s, tcp_server_public_key(tcp_s)) == 0, @@ -681,7 +686,7 @@ START_TEST(test_tcp_connection2) IP_Port ip_port_tcp_s; - ip_port_tcp_s.port = htons(ports[rand() % NUM_PORTS]); + ip_port_tcp_s.port = net_htons(ports[rand() % NUM_PORTS]); ip_port_tcp_s.ip.family = AF_INET6; get_ip6(&ip_port_tcp_s.ip.ip6, &in6addr_loopback); diff --git a/auto_tests/crypto_test.c b/auto_tests/crypto_test.c index 7c5338e2c..952b03129 100644 --- a/auto_tests/crypto_test.c +++ b/auto_tests/crypto_test.c @@ -276,7 +276,7 @@ static void increment_nonce_number_cmp(uint8_t *nonce, uint32_t num) { uint32_t num1, num2; memcpy(&num1, nonce + (CRYPTO_NONCE_SIZE - sizeof(num1)), sizeof(num1)); - num1 = ntohl(num1); + num1 = net_ntohl(num1); num2 = num + num1; if (num2 < num1) { @@ -291,7 +291,7 @@ static void increment_nonce_number_cmp(uint8_t *nonce, uint32_t num) } } - num2 = htonl(num2); + num2 = net_htonl(num2); memcpy(nonce + (CRYPTO_NONCE_SIZE - sizeof(num2)), &num2, sizeof(num2)); } diff --git a/auto_tests/dht_test.c b/auto_tests/dht_test.c index 633e70021..479b0eb88 100644 --- a/auto_tests/dht_test.c +++ b/auto_tests/dht_test.c @@ -623,7 +623,7 @@ loop_top: IP_Port ip_port; ip_init(&ip_port.ip, 1); ip_port.ip.ip6.uint8[15] = 1; - ip_port.port = htons(DHT_DEFAULT_PORT + i); + ip_port.port = net_htons(DHT_DEFAULT_PORT + i); DHT_bootstrap(dhts[(i - 1) % NUM_DHT], ip_port, dhts[i]->self_public_key); } diff --git a/auto_tests/network_test.c b/auto_tests/network_test.c index ea08c1b82..9ae1ddcb9 100644 --- a/auto_tests/network_test.c +++ b/auto_tests/network_test.c @@ -21,7 +21,7 @@ START_TEST(test_addr_resolv_localhost) * normally this should happen automatically * cygwin doesn't do it for every network related function though * e.g. not for getaddrinfo... */ - socket(0, 0, 0); + net_socket(0, 0, 0); errno = 0; #endif @@ -35,12 +35,10 @@ START_TEST(test_addr_resolv_localhost) ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); - if (res > 0) { - ck_assert_msg(ip.family == AF_INET, "Expected family AF_INET, got %u.", ip.family); - struct in_addr addr; - fill_addr4(ip.ip4, &addr); - ck_assert_msg(ip.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(addr)); - } + char ip_str[IP_NTOA_LEN]; + ck_assert_msg(ip.family == AF_INET, "Expected family AF_INET, got %u.", ip.family); + ck_assert_msg(ip.ip4.uint32 == net_htonl(0x7F000001), "Expected 127.0.0.1, got %s.", + ip_ntoa(&ip, ip_str, sizeof(ip_str))); ip_init(&ip, 1); // ipv6enabled = 1 res = addr_resolve(localhost, &ip, NULL); @@ -52,35 +50,29 @@ START_TEST(test_addr_resolv_localhost) ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); - if (res > 0) { - char ip_str[IP_NTOA_LEN]; - ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6 (%u), got %u.", AF_INET6, ip.family); - ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", - ip_ntoa(&ip, ip_str, sizeof(ip_str))); - } + ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6 (%u), got %u.", AF_INET6, ip.family); + ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", + ip_ntoa(&ip, ip_str, sizeof(ip_str))); - if (!localhost_split) { - ip_init(&ip, 1); // ipv6enabled = 1 - ip.family = AF_UNSPEC; - IP extra; - ip_reset(&extra); - res = addr_resolve(localhost, &ip, &extra); - ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); - - if (res > 0) { - char ip_str[IP_NTOA_LEN]; - ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6 (%u), got %u.", AF_INET6, ip.family); - ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", - ip_ntoa(&ip, ip_str, sizeof(ip_str))); - - struct in_addr addr; - fill_addr4(ip.ip4, &addr); - ck_assert_msg(extra.family == AF_INET, "Expected family AF_INET (%u), got %u.", AF_INET, extra.family); - ck_assert_msg(extra.ip4.uint32 == htonl(0x7F000001), "Expected 127.0.0.1, got %s.", inet_ntoa(addr)); - } - } else { + if (localhost_split) { printf("Localhost seems to be split in two.\n"); + return; } + + ip_init(&ip, 1); // ipv6enabled = 1 + ip.family = AF_UNSPEC; + IP extra; + ip_reset(&extra); + res = addr_resolve(localhost, &ip, &extra); + ck_assert_msg(res > 0, "Resolver failed: %u, %s (%x, %x)", errno, strerror(errno)); + + ck_assert_msg(ip.family == AF_INET6, "Expected family AF_INET6 (%u), got %u.", AF_INET6, ip.family); + ck_assert_msg(!memcmp(&ip.ip6, &in6addr_loopback, sizeof(IP6)), "Expected ::1, got %s.", + ip_ntoa(&ip, ip_str, sizeof(ip_str))); + + ck_assert_msg(extra.family == AF_INET, "Expected family AF_INET (%u), got %u.", AF_INET, extra.family); + ck_assert_msg(extra.ip4.uint32 == net_htonl(0x7F000001), "Expected 127.0.0.1, got %s.", + ip_ntoa(&ip, ip_str, sizeof(ip_str))); } END_TEST @@ -101,18 +93,18 @@ START_TEST(test_ip_equal) ck_assert_msg(res == 0, "ip_equal(NULL, PTR): expected result 0, got %u.", res); ip1.family = AF_INET; - ip1.ip4.uint32 = htonl(0x7F000001); + ip1.ip4.uint32 = net_htonl(0x7F000001); res = ip_equal(&ip1, &ip2); ck_assert_msg(res == 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_UNSPEC, 0} ): expected result 0, got %u.", res); ip2.family = AF_INET; - ip2.ip4.uint32 = htonl(0x7F000001); + ip2.ip4.uint32 = net_htonl(0x7F000001); res = ip_equal(&ip1, &ip2); ck_assert_msg(res != 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET, 127.0.0.1} ): expected result != 0, got 0."); - ip2.ip4.uint32 = htonl(0x7F000002); + ip2.ip4.uint32 = net_htonl(0x7F000002); res = ip_equal(&ip1, &ip2); ck_assert_msg(res == 0, "ip_equal( {AF_INET, 127.0.0.1}, {AF_INET, 127.0.0.2} ): expected result 0, got %u.", res); @@ -120,8 +112,8 @@ START_TEST(test_ip_equal) ip2.family = AF_INET6; ip2.ip6.uint32[0] = 0; ip2.ip6.uint32[1] = 0; - ip2.ip6.uint32[2] = htonl(0xFFFF); - ip2.ip6.uint32[3] = htonl(0x7F000001); + ip2.ip6.uint32[2] = net_htonl(0xFFFF); + ip2.ip6.uint32[3] = net_htonl(0x7F000001); ck_assert_msg(IPV6_IPV4_IN_V6(ip2.ip6) != 0, "IPV6_IPV4_IN_V6(::ffff:127.0.0.1): expected != 0, got 0."); diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index d7e66d33d..a339119f7 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -166,11 +166,11 @@ int main(int argc, char *argv[]) fclose(file); printf("\n"); - printf("Port: %u\n", ntohs(dht->net->port)); + printf("Port: %u\n", net_ntohs(dht->net->port)); if (argc > argvoffset + 3) { printf("Trying to bootstrap into the network...\n"); - uint16_t port = htons(atoi(argv[argvoffset + 2])); + uint16_t port = net_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); @@ -196,7 +196,7 @@ int main(int argc, char *argv[]) do_DHT(dht); if (is_timeout(last_LANdiscovery, is_waiting_for_dht_connection ? 5 : LAN_DISCOVERY_INTERVAL)) { - send_LANdiscovery(htons(PORT), dht); + send_LANdiscovery(net_htons(PORT), dht); last_LANdiscovery = unix_time(); } diff --git a/other/bootstrap_daemon/src/config.c b/other/bootstrap_daemon/src/config.c index aa8978a5d..b45b05f9b 100644 --- a/other/bootstrap_daemon/src/config.c +++ b/other/bootstrap_daemon/src/config.c @@ -406,7 +406,7 @@ int bootstrap_from_config(const char *cfg_file_path, DHT *dht, int enable_ipv6) } bs_public_key_bin = hex_string_to_bin(bs_public_key); - address_resolved = DHT_bootstrap_from_address(dht, bs_address, enable_ipv6, htons(bs_port), + address_resolved = DHT_bootstrap_from_address(dht, bs_address, enable_ipv6, net_htons(bs_port), bs_public_key_bin); free(bs_public_key_bin); diff --git a/other/bootstrap_daemon/src/tox-bootstrapd.c b/other/bootstrap_daemon/src/tox-bootstrapd.c index 1c4b11b67..9f67458b3 100644 --- a/other/bootstrap_daemon/src/tox-bootstrapd.c +++ b/other/bootstrap_daemon/src/tox-bootstrapd.c @@ -312,7 +312,7 @@ int main(int argc, char *argv[]) print_public_key(dht->self_public_key); uint64_t last_LANdiscovery = 0; - const uint16_t htons_port = htons(port); + const uint16_t net_htons_port = net_htons(port); int waiting_for_dht_connection = 1; @@ -325,7 +325,7 @@ int main(int argc, char *argv[]) do_DHT(dht); if (enable_lan_discovery && is_timeout(last_LANdiscovery, LAN_DISCOVERY_INTERVAL)) { - send_LANdiscovery(htons_port, dht); + send_LANdiscovery(net_htons_port, dht); last_LANdiscovery = unix_time(); } diff --git a/other/bootstrap_node_packets.c b/other/bootstrap_node_packets.c index 09404d6ec..614cb7204 100644 --- a/other/bootstrap_node_packets.c +++ b/other/bootstrap_node_packets.c @@ -61,7 +61,7 @@ int bootstrap_set_callbacks(Networking_Core *net, uint32_t version, uint8_t *mot return -1; } - bootstrap_version = htonl(version); + bootstrap_version = net_htonl(version); memcpy(bootstrap_motd, motd, motd_length); bootstrap_motd_length = motd_length; diff --git a/testing/DHT_test.c b/testing/DHT_test.c index c42dc0538..536217fea 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c @@ -88,16 +88,16 @@ static void print_assoc(IPPTsPng *assoc, uint8_t ours) { IP_Port *ipp = &assoc->ip_port; char ip_str[IP_NTOA_LEN]; - printf("\nIP: %s Port: %u", ip_ntoa(&ipp->ip, ip_str, sizeof(ip_str)), ntohs(ipp->port)); + printf("\nIP: %s Port: %u", ip_ntoa(&ipp->ip, ip_str, sizeof(ip_str)), net_ntohs(ipp->port)); printf("\nTimestamp: %llu", (long long unsigned int) assoc->timestamp); printf("\nLast pinged: %llu\n", (long long unsigned int) assoc->last_pinged); ipp = &assoc->ret_ip_port; if (ours) { - printf("OUR IP: %s Port: %u\n", ip_ntoa(&ipp->ip, ip_str, sizeof(ip_str)), ntohs(ipp->port)); + printf("OUR IP: %s Port: %u\n", ip_ntoa(&ipp->ip, ip_str, sizeof(ip_str)), net_ntohs(ipp->port)); } else { - printf("RET IP: %s Port: %u\n", ip_ntoa(&ipp->ip, ip_str, sizeof(ip_str)), ntohs(ipp->port)); + printf("RET IP: %s Port: %u\n", ip_ntoa(&ipp->ip, ip_str, sizeof(ip_str)), net_ntohs(ipp->port)); } printf("Timestamp: %llu\n", (long long unsigned int) assoc->ret_timestamp); @@ -138,7 +138,7 @@ static void print_friendlist(DHT *dht) int friendok = DHT_getfriendip(dht, dht->friends_list[k].public_key, &p_ip); char ip_str[IP_NTOA_LEN]; - printf("\nIP: %s:%u (%d)", ip_ntoa(&p_ip.ip, ip_str, sizeof(ip_str)), ntohs(p_ip.port), friendok); + printf("\nIP: %s:%u (%d)", ip_ntoa(&p_ip.ip, ip_str, sizeof(ip_str)), net_ntohs(p_ip.port), friendok); printf("\nCLIENTS IN LIST:\n\n"); @@ -227,7 +227,7 @@ int main(int argc, char *argv[]) perror("Initialization"); - uint16_t port = htons(atoi(argv[argvoffset + 2])); + uint16_t port = net_htons(atoi(argv[argvoffset + 2])); 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); diff --git a/testing/Messenger_test.c b/testing/Messenger_test.c index 467d7988c..c1a63da8d 100644 --- a/testing/Messenger_test.c +++ b/testing/Messenger_test.c @@ -126,7 +126,7 @@ int main(int argc, char *argv[]) } if (argc == argvoffset + 4) { - uint16_t port = htons(atoi(argv[argvoffset + 2])); + uint16_t port = net_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); diff --git a/testing/dns3_test.c b/testing/dns3_test.c index 021a09f96..37bbfadbf 100644 --- a/testing/dns3_test.c +++ b/testing/dns3_test.c @@ -50,7 +50,7 @@ int main(int argc, char *argv[]) IP ip = {0}; ip.family = AF_INET; - Socket sock = socket(ip.family, SOCK_DGRAM, IPPROTO_UDP); + Socket sock = net_socket(ip.family, TOX_SOCK_DGRAM, TOX_PROTO_UDP); if (!sock_valid(sock)) { return -1; @@ -60,29 +60,9 @@ int main(int argc, char *argv[]) return -1; } - struct sockaddr_in target; - - size_t addrsize = sizeof(struct sockaddr_in); - - target.sin_family = AF_INET; - - fill_addr4(ip.ip4, &target.sin_addr); - - target.sin_port = htons(53); - - uint8_t string[1024] = {0}; - - void *d = tox_dns3_new(hex_string_to_bin(argv[2])); - - unsigned int i; - uint32_t request_id; - - /* - for (i = 0; i < 255; ++i) { - tox_generate_dns3_string(d, string, sizeof(string), &request_id, string, i); - printf("%s\n", string); - }*/ + uint8_t string[1024] = {0}; + void *d = tox_dns3_new(hex_string_to_bin(argv[2])); int len = tox_generate_dns3_string(d, string + 1, sizeof(string) - 1, &request_id, (uint8_t *)argv[3], strlen(argv[3])); if (len == -1) { @@ -96,7 +76,11 @@ int main(int argc, char *argv[]) uint8_t id = rand(); uint32_t p_len = create_packet(packet, string, strlen((char *)string), id); - if (sendto(sock, (char *)packet, p_len, 0, (struct sockaddr *)&target, addrsize) != p_len) { + IP_Port ip_port; + ip_port.port = 53; + ip_port.ip = ip; + + if (net_sendto_ip4(sock, (char *)packet, p_len, ip_port) != p_len) { return -1; } @@ -107,6 +91,7 @@ int main(int argc, char *argv[]) return -1; } + unsigned int i; for (i = r_len - 1; i != 0 && buffer[i] != '='; --i) { ; } diff --git a/toxav/audio.c b/toxav/audio.c index 71dc53eac..eb5d03336 100644 --- a/toxav/audio.c +++ b/toxav/audio.c @@ -165,7 +165,7 @@ void ac_iterate(ACSession *ac) /* Pick up sampling rate from packet */ memcpy(&ac->lp_sampling_rate, msg->data, 4); - ac->lp_sampling_rate = ntohl(ac->lp_sampling_rate); + ac->lp_sampling_rate = net_ntohl(ac->lp_sampling_rate); ac->lp_channel_count = opus_packet_get_nb_channels(msg->data + 4); diff --git a/toxav/bwcontroller.c b/toxav/bwcontroller.c index 397cebff0..91e502b22 100644 --- a/toxav/bwcontroller.c +++ b/toxav/bwcontroller.c @@ -169,8 +169,8 @@ void send_update(BWController *bwc) struct BWCMessage *b_msg = (struct BWCMessage *)(p_msg + 1); p_msg[0] = BWC_PACKET_ID; - b_msg->lost = htonl(bwc->cycle.lost); - b_msg->recv = htonl(bwc->cycle.recv); + b_msg->lost = net_htonl(bwc->cycle.lost); + b_msg->recv = net_htonl(bwc->cycle.recv); if (-1 == m_send_custom_lossy_packet(bwc->m, bwc->friend_number, p_msg, sizeof(p_msg))) { LOGGER_WARNING(bwc->m->log, "BWC send failed (len: %d)! std error: %s", sizeof(p_msg), strerror(errno)); @@ -192,8 +192,8 @@ static int on_update(BWController *bwc, const struct BWCMessage *msg) bwc->cycle.lru = current_time_monotonic(); - uint32_t recv = ntohl(msg->recv); - uint32_t lost = ntohl(msg->lost); + uint32_t recv = net_ntohl(msg->recv); + uint32_t lost = net_ntohl(msg->lost); LOGGER_DEBUG(bwc->m->log, "recved: %u lost: %u", recv, lost); diff --git a/toxav/groupav.c b/toxav/groupav.c index 2e77ec530..418a42aa0 100644 --- a/toxav/groupav.c +++ b/toxav/groupav.c @@ -405,7 +405,7 @@ static int handle_group_audio_packet(void *object, int groupnumber, int friendgr uint16_t sequnum; memcpy(&sequnum, packet, sizeof(sequnum)); - pk->sequnum = ntohs(sequnum); + pk->sequnum = net_ntohs(sequnum); pk->length = length - sizeof(uint16_t); memcpy(pk->data, packet + sizeof(uint16_t), length - sizeof(uint16_t)); @@ -513,7 +513,7 @@ static int send_audio_packet(Group_Chats *g_c, int groupnumber, uint8_t *packet, VLA(uint8_t, data, 1 + sizeof(uint16_t) + length); data[0] = GROUP_AUDIO_PACKET_ID; - uint16_t sequnum = htons(group_av->audio_sequnum); + uint16_t sequnum = net_htons(group_av->audio_sequnum); memcpy(data + 1, &sequnum, sizeof(sequnum)); memcpy(data + 1 + sizeof(sequnum), packet, length); diff --git a/toxav/rtp.c b/toxav/rtp.c index 9403a43de..6f939082b 100644 --- a/toxav/rtp.c +++ b/toxav/rtp.c @@ -131,12 +131,12 @@ int rtp_send_data(RTPSession *session, const uint8_t *data, uint16_t length, Log header->ma = 0; header->pt = session->payload_type % 128; - header->sequnum = htons(session->sequnum); - header->timestamp = htonl(current_time_monotonic()); - header->ssrc = htonl(session->ssrc); + header->sequnum = net_htons(session->sequnum); + header->timestamp = net_htonl(current_time_monotonic()); + header->ssrc = net_htonl(session->ssrc); header->cpart = 0; - header->tlen = htons(length); + header->tlen = net_htons(length); if (MAX_CRYPTO_DATA_SIZE > length + sizeof(struct RTPHeader) + 1) { @@ -170,7 +170,7 @@ int rtp_send_data(RTPSession *session, const uint8_t *data, uint16_t length, Log } sent += piece; - header->cpart = htons(sent); + header->cpart = net_htons(sent); } /* Send remaining */ @@ -194,10 +194,10 @@ int rtp_send_data(RTPSession *session, const uint8_t *data, uint16_t length, Log static bool chloss(const RTPSession *session, const struct RTPHeader *header) { - if (ntohl(header->timestamp) < session->rtimestamp) { + if (net_ntohl(header->timestamp) < session->rtimestamp) { uint16_t hosq, lost = 0; - hosq = ntohs(header->sequnum); + hosq = net_ntohs(header->sequnum); lost = (hosq > session->rsequnum) ? (session->rsequnum + 65535) - hosq : @@ -224,12 +224,12 @@ static struct RTPMessage *new_message(size_t allocate_len, const uint8_t *data, msg->len = data_length - sizeof(struct RTPHeader); memcpy(&msg->header, data, data_length); - msg->header.sequnum = ntohs(msg->header.sequnum); - msg->header.timestamp = ntohl(msg->header.timestamp); - msg->header.ssrc = ntohl(msg->header.ssrc); + msg->header.sequnum = net_ntohs(msg->header.sequnum); + msg->header.timestamp = net_ntohl(msg->header.timestamp); + msg->header.ssrc = net_ntohl(msg->header.ssrc); - msg->header.cpart = ntohs(msg->header.cpart); - msg->header.tlen = ntohs(msg->header.tlen); + msg->header.cpart = net_ntohs(msg->header.cpart); + msg->header.tlen = net_ntohs(msg->header.tlen); return msg; } @@ -255,14 +255,14 @@ int handle_rtp_packet(Messenger *m, uint32_t friendnumber, const uint8_t *data, return -1; } - if (ntohs(header->cpart) >= ntohs(header->tlen)) { + if (net_ntohs(header->cpart) >= net_ntohs(header->tlen)) { /* Never allow this case to happen */ return -1; } bwc_feed_avg(session->bwc, length); - if (ntohs(header->tlen) == length - sizeof(struct RTPHeader)) { + if (net_ntohs(header->tlen) == length - sizeof(struct RTPHeader)) { /* The message is sent in single part */ /* Only allow messages which have arrived in order; @@ -273,8 +273,8 @@ int handle_rtp_packet(Messenger *m, uint32_t friendnumber, const uint8_t *data, } /* Message is not late; pick up the latest parameters */ - session->rsequnum = ntohs(header->sequnum); - session->rtimestamp = ntohl(header->timestamp); + session->rsequnum = net_ntohs(header->sequnum); + session->rtimestamp = net_ntohl(header->timestamp); bwc_add_recv(session->bwc, length); @@ -311,20 +311,20 @@ int handle_rtp_packet(Messenger *m, uint32_t friendnumber, const uint8_t *data, * processing message */ - if (session->mp->header.sequnum == ntohs(header->sequnum) && - session->mp->header.timestamp == ntohl(header->timestamp)) { + if (session->mp->header.sequnum == net_ntohs(header->sequnum) && + session->mp->header.timestamp == net_ntohl(header->timestamp)) { /* First case */ /* Make sure we have enough allocated memory */ if (session->mp->header.tlen - session->mp->len < length - sizeof(struct RTPHeader) || - session->mp->header.tlen <= ntohs(header->cpart)) { + session->mp->header.tlen <= net_ntohs(header->cpart)) { /* There happened to be some corruption on the stream; * continue wihtout this part */ return 0; } - memcpy(session->mp->data + ntohs(header->cpart), data + sizeof(struct RTPHeader), + memcpy(session->mp->data + net_ntohs(header->cpart), data + sizeof(struct RTPHeader), length - sizeof(struct RTPHeader)); session->mp->len += length - sizeof(struct RTPHeader); @@ -346,7 +346,7 @@ int handle_rtp_packet(Messenger *m, uint32_t friendnumber, const uint8_t *data, } else { /* Second case */ - if (session->mp->header.timestamp > ntohl(header->timestamp)) { + if (session->mp->header.timestamp > net_ntohl(header->timestamp)) { /* The received message part is from the old message; * discard it. */ @@ -386,22 +386,22 @@ NEW_MULTIPARTED: } /* Message is not late; pick up the latest parameters */ - session->rsequnum = ntohs(header->sequnum); - session->rtimestamp = ntohl(header->timestamp); + session->rsequnum = net_ntohs(header->sequnum); + session->rtimestamp = net_ntohl(header->timestamp); bwc_add_recv(session->bwc, length); /* Again, only store message if handler is present */ if (session->mcb) { - session->mp = new_message(ntohs(header->tlen) + sizeof(struct RTPHeader), data, length); + session->mp = new_message(net_ntohs(header->tlen) + sizeof(struct RTPHeader), data, length); /* Reposition data if necessary */ - if (ntohs(header->cpart)) { + if (net_ntohs(header->cpart)) { ; } - memmove(session->mp->data + ntohs(header->cpart), session->mp->data, session->mp->len); + memmove(session->mp->data + net_ntohs(header->cpart), session->mp->data, session->mp->len); } } diff --git a/toxav/toxav.c b/toxav/toxav.c index 58db4597c..deb71c496 100644 --- a/toxav/toxav.c +++ b/toxav/toxav.c @@ -706,7 +706,7 @@ bool toxav_audio_send_frame(ToxAV *av, uint32_t friend_number, const int16_t *pc VLA(uint8_t, dest, sample_count + sizeof(sampling_rate)); /* This is more than enough always */ - sampling_rate = htonl(sampling_rate); + sampling_rate = net_htonl(sampling_rate); memcpy(dest, &sampling_rate, sizeof(sampling_rate)); int vrc = opus_encode(call->audio.second->encoder, pcm, sample_count, dest + sizeof(sampling_rate), SIZEOF_VLA(dest) - sizeof(sampling_rate)); diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 7064d3d5a..9cfe1ae12 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -541,9 +541,9 @@ static int client_or_ip_port_in_list(Logger *log, Client_data *list, uint16_t le char ip_str[IP_NTOA_LEN]; LOGGER_TRACE(log, "coipil[%u]: switching ipv4 from %s:%u to %s:%u", i, ip_ntoa(&list[i].assoc4.ip_port.ip, ip_str, sizeof(ip_str)), - ntohs(list[i].assoc4.ip_port.port), + net_ntohs(list[i].assoc4.ip_port.port), ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), - ntohs(ip_port.port)); + net_ntohs(ip_port.port)); } if (LAN_ip(list[i].assoc4.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) { @@ -558,9 +558,9 @@ static int client_or_ip_port_in_list(Logger *log, Client_data *list, uint16_t le char ip_str[IP_NTOA_LEN]; LOGGER_TRACE(log, "coipil[%u]: switching ipv6 from %s:%u to %s:%u", i, ip_ntoa(&list[i].assoc6.ip_port.ip, ip_str, sizeof(ip_str)), - ntohs(list[i].assoc6.ip_port.port), + net_ntohs(list[i].assoc6.ip_port.port), ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), - ntohs(ip_port.port)); + net_ntohs(ip_port.port)); } if (LAN_ip(list[i].assoc6.ip_port.ip) != 0 && LAN_ip(ip_port.ip) == 0) { @@ -687,7 +687,7 @@ static uint8_t hardening_correct(const Hardening *h) * helper for get_close_nodes(). argument list is a monster :D */ static void get_close_nodes_inner(const uint8_t *public_key, Node_format *nodes_list, - sa_family_t sa_family, const Client_data *client_list, uint32_t client_list_length, + Family sa_family, const Client_data *client_list, uint32_t client_list_length, uint32_t *num_nodes_ptr, uint8_t is_LAN, uint8_t want_good) { if ((sa_family != AF_INET) && (sa_family != AF_INET6) && (sa_family != 0)) { @@ -758,7 +758,7 @@ static void get_close_nodes_inner(const uint8_t *public_key, Node_format *nodes_ * want_good : do we want only good nodes as checked with the hardening returned or not? */ static int get_somewhat_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, - sa_family_t sa_family, uint8_t is_LAN, uint8_t want_good) + Family sa_family, uint8_t is_LAN, uint8_t want_good) { uint32_t num_nodes = 0, i; get_close_nodes_inner(public_key, nodes_list, sa_family, @@ -784,7 +784,7 @@ static int get_somewhat_close_nodes(const DHT *dht, const uint8_t *public_key, N return num_nodes; } -int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, sa_family_t sa_family, +int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, Family sa_family, uint8_t is_LAN, uint8_t want_good) { memset(nodes_list, 0, MAX_SENT_NODES * sizeof(Node_format)); @@ -2093,7 +2093,7 @@ static uint16_t NAT_getports(uint16_t *portlist, IP_Port *ip_portlist, uint16_t for (i = 0; i < len; ++i) { if (ip_equal(&ip_portlist[i].ip, &ip)) { - portlist[num] = ntohs(ip_portlist[i].port); + portlist[num] = net_ntohs(ip_portlist[i].port); ++num; } } @@ -2124,7 +2124,7 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, if (i == numports) { /* If all ports are the same, only try that one port. */ IP_Port pinging; ip_copy(&pinging.ip, &ip); - pinging.port = htons(firstport); + pinging.port = net_htons(firstport); send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].public_key); } else { for (i = dht->friends_list[friend_num].nat.punching_index; i != top; ++i) { @@ -2132,7 +2132,7 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1); IP_Port pinging; ip_copy(&pinging.ip, &ip); - pinging.port = htons(port); + pinging.port = net_htons(port); send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].public_key); } @@ -2146,7 +2146,7 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports, ip_copy(&pinging.ip, &ip); for (i = dht->friends_list[friend_num].nat.punching_index2; i != top; ++i) { - pinging.port = htons(port + i); + pinging.port = net_htons(port + i); send_ping_request(dht->ping, pinging, dht->friends_list[friend_num].public_key); } @@ -2262,7 +2262,7 @@ static int send_hardening_getnode_res(const DHT *dht, const Node_format *sendto, } /* TODO(irungentoo): improve */ -static IPPTsPng *get_closelist_IPPTsPng(DHT *dht, const uint8_t *public_key, sa_family_t sa_family) +static IPPTsPng *get_closelist_IPPTsPng(DHT *dht, const uint8_t *public_key, Family sa_family) { uint32_t i; @@ -2393,7 +2393,7 @@ static int handle_hardening(void *object, IP_Port source, const uint8_t *source_ /* Return a random node from all the nodes we are connected to. * TODO(irungentoo): improve this function. */ -static Node_format random_node(DHT *dht, sa_family_t sa_family) +static Node_format random_node(DHT *dht, Family sa_family) { uint8_t id[CRYPTO_PUBLIC_KEY_SIZE]; uint32_t i; @@ -2499,7 +2499,7 @@ static void do_hardening(DHT *dht) for (i = 0; i < LCLIENT_LIST * 2; ++i) { IPPTsPng *cur_iptspng; - sa_family_t sa_family; + Family sa_family; uint8_t *public_key = dht->close_clientlist[i / 2].public_key; if (i % 2 == 0) { diff --git a/toxcore/DHT.h b/toxcore/DHT.h index 3b058c9ba..e70094cd6 100644 --- a/toxcore/DHT.h +++ b/toxcore/DHT.h @@ -356,7 +356,7 @@ bool node_addable_to_close_list(DHT *dht, const uint8_t *public_key, IP_Port ip_ * return the number of nodes returned. * */ -int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, sa_family_t sa_family, +int get_close_nodes(const DHT *dht, const uint8_t *public_key, Node_format *nodes_list, Family sa_family, uint8_t is_LAN, uint8_t want_good); diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index e26f7eea9..e685ae0e7 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -86,9 +86,9 @@ static void fetch_broadcast_info(uint16_t port) if (gateway.family == AF_INET && subnet_mask.family == AF_INET) { IP_Port *ip_port = &ip_ports[count]; ip_port->ip.family = AF_INET; - uint32_t gateway_ip = ntohl(gateway.ip4.uint32), subnet_ip = ntohl(subnet_mask.ip4.uint32); + uint32_t gateway_ip = net_ntohl(gateway.ip4.uint32), subnet_ip = net_ntohl(subnet_mask.ip4.uint32); uint32_t broadcast_ip = gateway_ip + ~subnet_ip - 1; - ip_port->ip.ip4.uint32 = htonl(broadcast_ip); + ip_port->ip.ip4.uint32 = net_htonl(broadcast_ip); ip_port->port = port; count++; @@ -124,7 +124,7 @@ static void fetch_broadcast_info(uint16_t port) broadcast_count = 0; Socket sock = 0; - if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + if ((sock = net_socket(TOX_AF_INET, TOX_SOCK_STREAM, 0)) < 0) { return; } @@ -174,7 +174,7 @@ static void fetch_broadcast_info(uint16_t port) IP_Port *ip_port = &ip_ports[count]; ip_port->ip.family = AF_INET; - get_ip4(&ip_port->ip.ip4, &sock4->sin_addr); + ip_port->ip.ip4.uint32 = sock4->sin_addr.s_addr; if (ip_port->ip.ip4.uint32 == 0) { continue; @@ -228,7 +228,7 @@ static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, const uint8 } /* Return the broadcast ip. */ -static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast) +static IP broadcast_ip(Family family_socket, Family family_broadcast) { IP ip; ip_reset(&ip); @@ -246,7 +246,7 @@ static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast) ip.family = AF_INET6; ip.ip6.uint32[0] = 0; ip.ip6.uint32[1] = 0; - ip.ip6.uint32[2] = htonl(0xFFFF); + ip.ip6.uint32[2] = net_htonl(0xFFFF); ip.ip6.uint32[3] = INADDR_BROADCAST; } } else if (family_socket == AF_INET) { @@ -279,7 +279,7 @@ bool Local_ip(IP ip) } /* localhost in IPv6 (::1) */ - if (ip.ip6.uint64[0] == 0 && ip.ip6.uint32[2] == 0 && ip.ip6.uint32[3] == htonl(1)) { + if (ip.ip6.uint64[0] == 0 && ip.ip6.uint32[2] == 0 && ip.ip6.uint32[3] == net_htonl(1)) { return 1; } } diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index b13465ed9..5807127f9 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c @@ -1113,7 +1113,7 @@ static int file_sendrequest(const Messenger *m, int32_t friendnumber, uint8_t fi VLA(uint8_t, packet, 1 + sizeof(file_type) + sizeof(filesize) + FILE_ID_LENGTH + filename_length); packet[0] = filenumber; - file_type = htonl(file_type); + file_type = net_htonl(file_type); memcpy(packet + 1, &file_type, sizeof(file_type)); host_to_net((uint8_t *)&filesize, sizeof(filesize)); memcpy(packet + 1 + sizeof(file_type), &filesize, sizeof(filesize)); @@ -2256,7 +2256,7 @@ static int handle_packet(void *object, int i, const uint8_t *temp, uint16_t len, } memcpy(&file_type, data + 1, sizeof(file_type)); - file_type = ntohl(file_type); + file_type = net_ntohl(file_type); memcpy(&filesize, data + 1 + sizeof(uint32_t), sizeof(filesize)); net_to_host((uint8_t *) &filesize, sizeof(filesize)); @@ -2573,7 +2573,7 @@ void do_messenger(Messenger *m, void *userdata) char id_str[IDSTRING_LEN]; LOGGER_TRACE(m->log, "C[%2u] %s:%u [%3u] %s", client, ip_ntoa(&assoc->ip_port.ip, ip_str, sizeof(ip_str)), - ntohs(assoc->ip_port.port), last_pinged, + net_ntohs(assoc->ip_port.port), last_pinged, id_to_string(cptr->public_key, id_str, sizeof(id_str))); } } @@ -2653,7 +2653,7 @@ void do_messenger(Messenger *m, void *userdata) char id_str[IDSTRING_LEN]; LOGGER_TRACE(m->log, "F[%2u] => C[%2u] %s:%u [%3u] %s", friend_idx, client, ip_ntoa(&assoc->ip_port.ip, ip_str, sizeof(ip_str)), - ntohs(assoc->ip_port.port), last_pinged, + net_ntohs(assoc->ip_port.port), last_pinged, id_to_string(cptr->public_key, id_str, sizeof(id_str))); } } @@ -2779,13 +2779,13 @@ static uint32_t friends_list_save(const Messenger *m, uint8_t *data) MIN(SAVED_FRIEND_REQUEST_SIZE, MAX_FRIEND_REQUEST_DATA_SIZE)); memcpy(temp.info, m->friendlist[i].info, friendrequest_length); - temp.info_size = htons(m->friendlist[i].info_size); + temp.info_size = net_htons(m->friendlist[i].info_size); temp.friendrequest_nospam = m->friendlist[i].friendrequest_nospam; } else { memcpy(temp.name, m->friendlist[i].name, m->friendlist[i].name_length); - temp.name_length = htons(m->friendlist[i].name_length); + temp.name_length = net_htons(m->friendlist[i].name_length); memcpy(temp.statusmessage, m->friendlist[i].statusmessage, m->friendlist[i].statusmessage_length); - temp.statusmessage_length = htons(m->friendlist[i].statusmessage_length); + temp.statusmessage_length = net_htons(m->friendlist[i].statusmessage_length); temp.userstatus = m->friendlist[i].userstatus; uint8_t last_seen_time[sizeof(uint64_t)]; @@ -2866,8 +2866,8 @@ static int friends_list_load(Messenger *m, const uint8_t *data, uint32_t length) continue; } - setfriendname(m, fnum, temp.name, ntohs(temp.name_length)); - set_friend_statusmessage(m, fnum, temp.statusmessage, ntohs(temp.statusmessage_length)); + setfriendname(m, fnum, temp.name, net_ntohs(temp.name_length)); + set_friend_statusmessage(m, fnum, temp.statusmessage, net_ntohs(temp.statusmessage_length)); set_friend_userstatus(m, fnum, temp.userstatus); uint8_t last_seen_time[sizeof(uint64_t)]; memcpy(last_seen_time, &temp.last_seen_time, sizeof(uint64_t)); @@ -2880,7 +2880,7 @@ static int friends_list_load(Messenger *m, const uint8_t *data, uint32_t length) memcpy(address + CRYPTO_PUBLIC_KEY_SIZE, &(temp.friendrequest_nospam), sizeof(uint32_t)); uint16_t checksum = address_checksum(address, FRIEND_ADDRESS_SIZE - sizeof(checksum)); memcpy(address + CRYPTO_PUBLIC_KEY_SIZE + sizeof(uint32_t), &checksum, sizeof(checksum)); - m_addfriend(m, address, temp.info, ntohs(temp.info_size)); + m_addfriend(m, address, temp.info, net_ntohs(temp.info_size)); } } diff --git a/toxcore/TCP_client.c b/toxcore/TCP_client.c index 0910534dc..3e6de14a7 100644 --- a/toxcore/TCP_client.c +++ b/toxcore/TCP_client.c @@ -42,30 +42,8 @@ static int connect_sock_to(Socket sock, IP_Port ip_port, TCP_Proxy_Info *proxy_i ip_port = proxy_info->ip_port; } - struct sockaddr_storage addr = {0}; - - size_t addrsize; - - if (ip_port.ip.family == AF_INET) { - struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; - - addrsize = sizeof(struct sockaddr_in); - addr4->sin_family = AF_INET; - fill_addr4(ip_port.ip.ip4, &addr4->sin_addr); - addr4->sin_port = ip_port.port; - } else if (ip_port.ip.family == AF_INET6) { - struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; - - addrsize = sizeof(struct sockaddr_in6); - addr6->sin6_family = AF_INET6; - fill_addr6(ip_port.ip.ip6, &addr6->sin6_addr); - addr6->sin6_port = ip_port.port; - } else { - return 0; - } - /* nonblocking socket, connect will never return success */ - connect(sock, (struct sockaddr *)&addr, addrsize); + net_connect(sock, ip_port); return 1; } @@ -84,7 +62,7 @@ static int proxy_http_generate_connection_request(TCP_Client_Connection *TCP_con return 0; } - const uint16_t port = ntohs(TCP_conn->ip_port.port); + const uint16_t port = net_ntohs(TCP_conn->ip_port.port); const int written = snprintf((char *)TCP_conn->last_packet, MAX_PACKET_SIZE, "%s%s:%hu%s%s:%hu%s", one, ip, port, two, ip, port, three); @@ -389,7 +367,7 @@ static int write_packet_TCP_secure_connection(TCP_Client_Connection *con, const VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE); - uint16_t c_length = htons(length + CRYPTO_MAC_SIZE); + uint16_t c_length = net_htons(length + CRYPTO_MAC_SIZE); memcpy(packet, &c_length, sizeof(uint16_t)); int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t)); @@ -653,7 +631,7 @@ TCP_Client_Connection *new_TCP_connection(IP_Port ip_port, const uint8_t *public family = proxy_info->ip_port.ip.family; } - Socket sock = socket(family, SOCK_STREAM, IPPROTO_TCP); + Socket sock = net_socket(family, TOX_SOCK_STREAM, TOX_PROTO_TCP); if (!sock_valid(sock)) { return NULL; diff --git a/toxcore/TCP_server.c b/toxcore/TCP_server.c index 10f64d38b..d2b082069 100644 --- a/toxcore/TCP_server.c +++ b/toxcore/TCP_server.c @@ -69,39 +69,12 @@ size_t tcp_server_listen_count(const TCP_Server *tcp_server) return tcp_server->num_listening_socks; } -/* this is needed to compile on Android below API 21 -*/ +/* This is needed to compile on Android below API 21 + */ #ifndef EPOLLRDHUP #define EPOLLRDHUP 0x2000 #endif -/* return 1 on success - * return 0 on failure - */ -static int bind_to_port(Socket sock, int family, uint16_t port) -{ - struct sockaddr_storage addr = {0}; - size_t addrsize; - - if (family == AF_INET) { - struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; - - addrsize = sizeof(struct sockaddr_in); - addr4->sin_family = AF_INET; - addr4->sin_port = htons(port); - } else if (family == AF_INET6) { - struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; - - addrsize = sizeof(struct sockaddr_in6); - addr6->sin6_family = AF_INET6; - addr6->sin6_port = htons(port); - } else { - return 0; - } - - return (bind(sock, (struct sockaddr *)&addr, addrsize) == 0); -} - /* Set the size of the connection list to numfriends. * * return -1 if realloc fails. @@ -265,7 +238,7 @@ uint16_t read_TCP_length(Socket sock) return 0; } - length = ntohs(length); + length = net_ntohs(length); if (length > MAX_PACKET_SIZE) { return ~0; @@ -460,7 +433,7 @@ static int write_packet_TCP_secure_connection(TCP_Secure_Connection *con, const VLA(uint8_t, packet, sizeof(uint16_t) + length + CRYPTO_MAC_SIZE); - uint16_t c_length = htons(length + CRYPTO_MAC_SIZE); + uint16_t c_length = net_htons(length + CRYPTO_MAC_SIZE); memcpy(packet, &c_length, sizeof(uint16_t)); int len = encrypt_data_symmetric(con->shared_key, con->sent_nonce, data, length, packet + sizeof(uint16_t)); @@ -1005,7 +978,7 @@ static int accept_connection(TCP_Server *TCP_server, Socket sock) static Socket new_listening_TCP_socket(int family, uint16_t port) { - Socket sock = socket(family, SOCK_STREAM, IPPROTO_TCP); + Socket sock = net_socket(family, TOX_SOCK_STREAM, TOX_PROTO_TCP); if (!sock_valid(sock)) { return ~0; diff --git a/toxcore/crypto_core.c b/toxcore/crypto_core.c index 03150396a..ebbb1314f 100644 --- a/toxcore/crypto_core.c +++ b/toxcore/crypto_core.c @@ -218,7 +218,7 @@ void increment_nonce_number(uint8_t *nonce, uint32_t host_order_num) * that loop bounds and their potential underflow or overflow * are independent of user-controlled input (you may have heard of the Heartbleed bug). */ - const uint32_t big_endian_num = htonl(host_order_num); + const uint32_t big_endian_num = net_htonl(host_order_num); const uint8_t *const num_vec = (const uint8_t *) &big_endian_num; uint8_t num_as_nonce[crypto_box_NONCEBYTES] = {0}; num_as_nonce[crypto_box_NONCEBYTES - 4] = num_vec[0]; diff --git a/toxcore/friend_connection.c b/toxcore/friend_connection.c index a63855afa..0fbae4061 100644 --- a/toxcore/friend_connection.c +++ b/toxcore/friend_connection.c @@ -841,7 +841,7 @@ Friend_Connections *new_friend_connections(Onion_Client *onion_c, bool local_dis static void LANdiscovery(Friend_Connections *fr_c) { if (fr_c->last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { - send_LANdiscovery(htons(TOX_PORT_DEFAULT), fr_c->dht); + send_LANdiscovery(net_htons(TOX_PORT_DEFAULT), fr_c->dht); fr_c->last_LANdiscovery = unix_time(); } } diff --git a/toxcore/group.c b/toxcore/group.c index 5d8a6ac21..73aca8754 100644 --- a/toxcore/group.c +++ b/toxcore/group.c @@ -968,7 +968,7 @@ static unsigned int send_packet_group_peer(Friend_Connections *fr_c, int friendc return 0; } - group_num = htons(group_num); + group_num = net_htons(group_num); VLA(uint8_t, packet, 1 + sizeof(uint16_t) + length); packet[0] = packet_id; memcpy(packet + 1, &group_num, sizeof(uint16_t)); @@ -989,7 +989,7 @@ static unsigned int send_lossy_group_peer(Friend_Connections *fr_c, int friendco return 0; } - group_num = htons(group_num); + group_num = net_htons(group_num); VLA(uint8_t, packet, 1 + sizeof(uint16_t) + length); packet[0] = packet_id; memcpy(packet + 1, &group_num, sizeof(uint16_t)); @@ -1020,7 +1020,7 @@ int invite_friend(Group_Chats *g_c, int32_t friendnumber, int groupnumber) uint8_t invite[INVITE_PACKET_SIZE]; invite[0] = INVITE_ID; - uint16_t groupchat_num = htons((uint16_t)groupnumber); + uint16_t groupchat_num = net_htons((uint16_t)groupnumber); memcpy(invite + 1, &groupchat_num, sizeof(groupchat_num)); memcpy(invite + 1 + sizeof(groupchat_num), g->identifier, GROUP_IDENTIFIER_LENGTH); @@ -1074,7 +1074,7 @@ int join_groupchat(Group_Chats *g_c, int32_t friendnumber, uint8_t expected_type Group_c *g = &g_c->chats[groupnumber]; - uint16_t group_num = htons(groupnumber); + uint16_t group_num = net_htons(groupnumber); g->status = GROUPCHAT_STATUS_VALID; g->number_joined = -1; memcpy(g->real_pk, g_c->m->net_crypto->self_public_key, CRYPTO_PUBLIC_KEY_SIZE); @@ -1087,7 +1087,7 @@ int join_groupchat(Group_Chats *g_c, int32_t friendnumber, uint8_t expected_type if (send_conference_invite_packet(g_c->m, friendnumber, response, sizeof(response))) { uint16_t other_groupnum; memcpy(&other_groupnum, data, sizeof(other_groupnum)); - other_groupnum = ntohs(other_groupnum); + other_groupnum = net_ntohs(other_groupnum); memcpy(g->identifier, data + sizeof(uint16_t), GROUP_IDENTIFIER_LENGTH); int close_index = add_conn_to_groupchat(g_c, friendcon_id, groupnumber, 0, 1); @@ -1241,7 +1241,7 @@ static int group_new_peer_send(const Group_Chats *g_c, int groupnumber, uint16_t { uint8_t packet[GROUP_MESSAGE_NEW_PEER_LENGTH]; - peer_num = htons(peer_num); + peer_num = net_htons(peer_num); memcpy(packet, &peer_num, sizeof(uint16_t)); memcpy(packet + sizeof(uint16_t), real_pk, CRYPTO_PUBLIC_KEY_SIZE); memcpy(packet + sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE, temp_pk, CRYPTO_PUBLIC_KEY_SIZE); @@ -1264,7 +1264,7 @@ static int group_kill_peer_send(const Group_Chats *g_c, int groupnumber, uint16_ { uint8_t packet[GROUP_MESSAGE_KILL_PEER_LENGTH]; - peer_num = htons(peer_num); + peer_num = net_htons(peer_num); memcpy(packet, &peer_num, sizeof(uint16_t)); if (send_message_group(g_c, groupnumber, GROUP_MESSAGE_KILL_PEER_ID, packet, sizeof(packet)) > 0) { @@ -1412,7 +1412,7 @@ static void handle_friend_invite_packet(Messenger *m, uint32_t friendnumber, con uint16_t other_groupnum, groupnum; memcpy(&groupnum, data + 1 + sizeof(uint16_t), sizeof(uint16_t)); - groupnum = ntohs(groupnum); + groupnum = net_ntohs(groupnum); Group_c *g = get_group_c(g_c, groupnum); @@ -1440,7 +1440,7 @@ static void handle_friend_invite_packet(Messenger *m, uint32_t friendnumber, con } memcpy(&other_groupnum, data + 1, sizeof(uint16_t)); - other_groupnum = ntohs(other_groupnum); + other_groupnum = net_ntohs(other_groupnum); int friendcon_id = getfriendcon_id(m, friendnumber); uint8_t real_pk[CRYPTO_PUBLIC_KEY_SIZE], temp_pk[CRYPTO_PUBLIC_KEY_SIZE]; @@ -1507,7 +1507,7 @@ static unsigned int count_close_connected(Group_c *g) static int send_packet_online(Friend_Connections *fr_c, int friendcon_id, uint16_t group_num, uint8_t *identifier) { uint8_t packet[1 + ONLINE_PACKET_DATA_SIZE]; - group_num = htons(group_num); + group_num = net_htons(group_num); packet[0] = PACKET_ID_ONLINE_PACKET; memcpy(packet + 1, &group_num, sizeof(uint16_t)); memcpy(packet + 1 + sizeof(uint16_t), identifier, GROUP_IDENTIFIER_LENGTH); @@ -1526,7 +1526,7 @@ static int handle_packet_online(Group_Chats *g_c, int friendcon_id, const uint8_ int groupnumber = get_group_num(g_c, data + sizeof(uint16_t)); uint16_t other_groupnum; memcpy(&other_groupnum, data, sizeof(uint16_t)); - other_groupnum = ntohs(other_groupnum); + other_groupnum = net_ntohs(other_groupnum); Group_c *g = get_group_c(g_c, groupnumber); @@ -1626,7 +1626,7 @@ static unsigned int send_peers(Group_Chats *g_c, int groupnumber, int friendcon_ p = packet + 1; } - uint16_t peer_num = htons(g->group[i].peer_number); + uint16_t peer_num = net_htons(g->group[i].peer_number); memcpy(p, &peer_num, sizeof(peer_num)); p += sizeof(peer_num); memcpy(p, g->group[i].real_pk, CRYPTO_PUBLIC_KEY_SIZE); @@ -1672,7 +1672,7 @@ static int handle_send_peers(Group_Chats *g_c, int groupnumber, const uint8_t *d while ((unsigned int)(length - (d - data)) >= sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE * 2 + 1) { uint16_t peer_num; memcpy(&peer_num, d, sizeof(peer_num)); - peer_num = ntohs(peer_num); + peer_num = net_ntohs(peer_num); d += sizeof(uint16_t); int peer_index = addpeer(g_c, groupnumber, d, d + CRYPTO_PUBLIC_KEY_SIZE, peer_num, userdata, true); @@ -1903,7 +1903,7 @@ static int send_message_group(const Group_Chats *g_c, int groupnumber, uint8_t m } VLA(uint8_t, packet, sizeof(uint16_t) + sizeof(uint32_t) + 1 + len); - uint16_t peer_num = htons(g->peer_number); + uint16_t peer_num = net_htons(g->peer_number); memcpy(packet, &peer_num, sizeof(peer_num)); ++g->message_number; @@ -1912,7 +1912,7 @@ static int send_message_group(const Group_Chats *g_c, int groupnumber, uint8_t m ++g->message_number; } - uint32_t message_num = htonl(g->message_number); + uint32_t message_num = net_htonl(g->message_number); memcpy(packet + sizeof(uint16_t), &message_num, sizeof(message_num)); packet[sizeof(uint16_t) + sizeof(uint32_t)] = message_id; @@ -1971,9 +1971,9 @@ int send_group_lossy_packet(const Group_Chats *g_c, int groupnumber, const uint8 } VLA(uint8_t, packet, sizeof(uint16_t) * 2 + length); - uint16_t peer_number = htons(g->peer_number); + uint16_t peer_number = net_htons(g->peer_number); memcpy(packet, &peer_number, sizeof(uint16_t)); - uint16_t message_num = htons(g->lossy_message_number); + uint16_t message_num = net_htons(g->lossy_message_number); memcpy(packet + sizeof(uint16_t), &message_num, sizeof(uint16_t)); memcpy(packet + sizeof(uint16_t) * 2, data, length); @@ -2000,7 +2000,7 @@ static void handle_message_packet_group(Group_Chats *g_c, int groupnumber, const uint16_t peer_number; memcpy(&peer_number, data, sizeof(uint16_t)); - peer_number = ntohs(peer_number); + peer_number = net_ntohs(peer_number); int index = get_peer_index(g, peer_number); @@ -2013,7 +2013,7 @@ static void handle_message_packet_group(Group_Chats *g_c, int groupnumber, const uint32_t message_number; memcpy(&message_number, data + sizeof(uint16_t), sizeof(message_number)); - message_number = ntohl(message_number); + message_number = net_ntohl(message_number); if (g->group[index].last_message_number == 0) { g->group[index].last_message_number = message_number; @@ -2045,7 +2045,7 @@ static void handle_message_packet_group(Group_Chats *g_c, int groupnumber, const uint16_t new_peer_number; memcpy(&new_peer_number, msg_data, sizeof(uint16_t)); - new_peer_number = ntohs(new_peer_number); + new_peer_number = net_ntohs(new_peer_number); addpeer(g_c, groupnumber, msg_data + sizeof(uint16_t), msg_data + sizeof(uint16_t) + CRYPTO_PUBLIC_KEY_SIZE, new_peer_number, userdata, true); } @@ -2058,7 +2058,7 @@ static void handle_message_packet_group(Group_Chats *g_c, int groupnumber, const uint16_t kill_peer_number; memcpy(&kill_peer_number, msg_data, sizeof(uint16_t)); - kill_peer_number = ntohs(kill_peer_number); + kill_peer_number = net_ntohs(kill_peer_number); if (peer_number == kill_peer_number) { delpeer(g_c, groupnumber, index, userdata); @@ -2142,7 +2142,7 @@ static int handle_packet(void *object, int friendcon_id, const uint8_t *data, ui uint16_t groupnumber; memcpy(&groupnumber, data + 1, sizeof(uint16_t)); - groupnumber = ntohs(groupnumber); + groupnumber = net_ntohs(groupnumber); Group_c *g = get_group_c(g_c, groupnumber); if (!g) { @@ -2252,9 +2252,9 @@ static int handle_lossy(void *object, int friendcon_id, const uint8_t *data, uin memcpy(&groupnumber, data + 1, sizeof(uint16_t)); memcpy(&peer_number, data + 1 + sizeof(uint16_t), sizeof(uint16_t)); memcpy(&message_number, data + 1 + sizeof(uint16_t) * 2, sizeof(uint16_t)); - groupnumber = ntohs(groupnumber); - peer_number = ntohs(peer_number); - message_number = ntohs(message_number); + groupnumber = net_ntohs(groupnumber); + peer_number = net_ntohs(peer_number); + message_number = net_ntohs(message_number); Group_c *g = get_group_c(g_c, groupnumber); diff --git a/toxcore/net_crypto.c b/toxcore/net_crypto.c index ecd0177c1..0390e44e7 100644 --- a/toxcore/net_crypto.c +++ b/toxcore/net_crypto.c @@ -931,8 +931,8 @@ static int send_data_packet_helper(Net_Crypto *c, int crypt_connection_id, uint3 return -1; } - num = htonl(num); - buffer_start = htonl(buffer_start); + num = net_htonl(num); + buffer_start = net_htonl(buffer_start); uint16_t padding_length = (MAX_CRYPTO_DATA_SIZE - length) % CRYPTO_MAX_PADDING; VLA(uint8_t, packet, sizeof(uint32_t) + sizeof(uint32_t) + padding_length + length); memcpy(packet, &buffer_start, sizeof(uint32_t)); @@ -1042,7 +1042,7 @@ static uint16_t get_nonce_uint16(const uint8_t *nonce) { uint16_t num; memcpy(&num, nonce + (CRYPTO_NONCE_SIZE - sizeof(uint16_t)), sizeof(uint16_t)); - return ntohs(num); + return net_ntohs(num); } #define DATA_NUM_THRESHOLD 21845 @@ -1072,7 +1072,7 @@ static int handle_data_packet(const Net_Crypto *c, int crypt_connection_id, uint uint16_t num_cur_nonce = get_nonce_uint16(nonce); uint16_t num; memcpy(&num, packet + 1, sizeof(uint16_t)); - num = ntohs(num); + num = net_ntohs(num); uint16_t diff = num - num_cur_nonce; increment_nonce_number(nonce, diff); int len = decrypt_data_symmetric(conn->shared_key, nonce, packet + 1 + sizeof(uint16_t), @@ -1343,8 +1343,8 @@ static int handle_data_packet_core(Net_Crypto *c, int crypt_connection_id, const uint32_t buffer_start, num; memcpy(&buffer_start, data, sizeof(uint32_t)); memcpy(&num, data + sizeof(uint32_t), sizeof(uint32_t)); - buffer_start = ntohl(buffer_start); - num = ntohl(num); + buffer_start = net_ntohl(buffer_start); + num = net_ntohl(num); uint64_t rtt_calc_time = 0; diff --git a/toxcore/network.c b/toxcore/network.c index 6df13fb81..8008156b6 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -55,6 +55,7 @@ #include #include #include +#include #include #include #include @@ -70,7 +71,7 @@ #define EWOULDBLOCK WSAEWOULDBLOCK #endif -static const char *inet_ntop(sa_family_t family, const void *addr, char *buf, size_t bufsize) +static const char *inet_ntop(Family family, const void *addr, char *buf, size_t bufsize) { if (family == AF_INET) { struct sockaddr_in saddr; @@ -105,7 +106,7 @@ static const char *inet_ntop(sa_family_t family, const void *addr, char *buf, si return NULL; } -static int inet_pton(sa_family_t family, const char *addrString, void *addrbuf) +static int inet_pton(Family family, const char *addrString, void *addrbuf) { if (family == AF_INET) { struct sockaddr_in saddr; @@ -140,6 +141,10 @@ static int inet_pton(sa_family_t family, const char *addrString, void *addrbuf) #endif +static int make_proto(int proto); +static int make_socktype(int type); +static int make_family(int family); + /* Check if socket is valid. * * return 1 if valid @@ -302,11 +307,11 @@ uint64_t current_time_monotonic(void) static uint32_t data_0(uint16_t buflen, const uint8_t *buffer) { - return buflen > 4 ? ntohl(*(const uint32_t *)&buffer[1]) : 0; + return buflen > 4 ? net_ntohl(*(const uint32_t *)&buffer[1]) : 0; } static uint32_t data_1(uint16_t buflen, const uint8_t *buffer) { - return buflen > 7 ? ntohl(*(const uint32_t *)&buffer[5]) : 0; + return buflen > 7 ? net_ntohl(*(const uint32_t *)&buffer[5]) : 0; } static void loglogdata(Logger *log, const char *message, const uint8_t *buffer, @@ -317,17 +322,17 @@ static void loglogdata(Logger *log, const char *message, const uint8_t *buffer, if (res < 0) { /* Windows doesn't necessarily know %zu */ LOGGER_TRACE(log, "[%2u] %s %3hu%c %s:%hu (%u: %s) | %04x%04x", buffer[0], message, (buflen < 999 ? (uint16_t)buflen : 999), 'E', - ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), ntohs(ip_port.port), errno, + ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), net_ntohs(ip_port.port), errno, strerror(errno), data_0(buflen, buffer), data_1(buflen, buffer)); } else if ((res > 0) && ((size_t)res <= buflen)) { LOGGER_TRACE(log, "[%2u] %s %3zu%c %s:%hu (%u: %s) | %04x%04x", buffer[0], message, (res < 999 ? (size_t)res : 999), ((size_t)res < buflen ? '<' : '='), - ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), ntohs(ip_port.port), 0, "OK", + ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), net_ntohs(ip_port.port), 0, "OK", data_0(buflen, buffer), data_1(buflen, buffer)); } else { /* empty or overwrite */ LOGGER_TRACE(log, "[%2u] %s %zu%c%zu %s:%hu (%u: %s) | %04x%04x", buffer[0], message, (size_t)res, (!res ? '!' : '>'), buflen, - ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), ntohs(ip_port.port), 0, "OK", + ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), net_ntohs(ip_port.port), 0, "OK", data_0(buflen, buffer), data_1(buflen, buffer)); } } @@ -388,7 +393,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, const uint8_t *data, uint1 ip6.uint32[0] = 0; ip6.uint32[1] = 0; - ip6.uint32[2] = htonl(0xFFFF); + ip6.uint32[2] = net_htonl(0xFFFF); ip6.uint32[3] = ip_port.ip.ip4.uint32; fill_addr6(ip6, &addr6->sin6_addr); @@ -621,7 +626,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 /* Initialize our socket. */ /* add log message what we're creating */ - temp->sock = socket(temp->family, SOCK_DGRAM, IPPROTO_UDP); + temp->sock = net_socket(temp->family, TOX_SOCK_DGRAM, TOX_PROTO_UDP); /* Check for socket error. */ if (!sock_valid(temp->sock)) { @@ -732,7 +737,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 * it worked ok (which it did previously without a successful bind) */ uint16_t port_to_try = port_from; - *portptr = htons(port_to_try); + *portptr = net_htons(port_to_try); int tries; for (tries = port_from; tries <= port_to; tries++) { @@ -743,7 +748,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 char ip_str[IP_NTOA_LEN]; LOGGER_DEBUG(log, "Bound successfully to %s:%u", ip_ntoa(&ip, ip_str, sizeof(ip_str)), - ntohs(temp->port)); + net_ntohs(temp->port)); /* errno isn't reset on success, only set on failure, the failed * binds with parallel clients yield a -EPERM to the outside if @@ -765,7 +770,7 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1 port_to_try = port_from; } - *portptr = htons(port_to_try); + *portptr = net_htons(port_to_try); } char ip_str[IP_NTOA_LEN]; @@ -947,16 +952,18 @@ const char *ip_ntoa(const IP *ip, char *ip_str, size_t length) if (ip) { if (ip->family == AF_INET) { /* returns standard quad-dotted notation */ - const struct in_addr *addr = (const struct in_addr *)&ip->ip4; + struct in_addr addr; + fill_addr4(ip->ip4, &addr); ip_str[0] = 0; - inet_ntop(ip->family, addr, ip_str, length); + inet_ntop(ip->family, &addr, ip_str, length); } else if (ip->family == AF_INET6) { /* returns hex-groups enclosed into square brackets */ - const struct in6_addr *addr = (const struct in6_addr *)&ip->ip6; + struct in6_addr addr; + fill_addr6(ip->ip6, &addr); ip_str[0] = '['; - inet_ntop(ip->family, addr, &ip_str[1], length - 3); + inet_ntop(ip->family, &addr, &ip_str[1], length - 3); size_t len = strlen(ip_str); ip_str[len] = ']'; ip_str[len + 1] = 0; @@ -1067,7 +1074,7 @@ int addr_resolve(const char *address, IP *to, IP *extra) return 0; } - sa_family_t family = to->family; + Family family = to->family; struct addrinfo *server = NULL; struct addrinfo *walker = NULL; @@ -1175,3 +1182,197 @@ int addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra) return 1; } + +int net_connect(Socket sock, IP_Port ip_port) +{ + struct sockaddr_storage addr = {0}; + size_t addrsize; + + if (ip_port.ip.family == AF_INET) { + struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; + + addrsize = sizeof(struct sockaddr_in); + addr4->sin_family = AF_INET; + fill_addr4(ip_port.ip.ip4, &addr4->sin_addr); + addr4->sin_port = ip_port.port; + } else if (ip_port.ip.family == AF_INET6) { + struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; + + addrsize = sizeof(struct sockaddr_in6); + addr6->sin6_family = AF_INET6; + fill_addr6(ip_port.ip.ip6, &addr6->sin6_addr); + addr6->sin6_port = ip_port.port; + } else { + return 0; + } + + return connect(sock, (struct sockaddr *)&addr, addrsize); +} + +int32_t net_getipport(const char* node, IP_Port** res, int type) +{ + struct addrinfo *infos; + int ret = getaddrinfo(node, NULL, NULL, &infos); + if (ret != 0) { + return -1; + } + + struct addrinfo *cur; + int count = 0; + for (cur = infos; count < INT32_MAX && cur != NULL; cur = cur->ai_next) { + if (cur->ai_socktype && type > 0 && cur->ai_socktype != type) { + continue; + } + + if (cur->ai_family != AF_INET && cur->ai_family != AF_INET6) { + continue; + } + + count++; + } + + if (count == INT32_MAX) { + return -1; + } + + *res = (IP_Port*)malloc(sizeof(IP_Port) * count); + if (*res == NULL) { + return -1; + } + + IP_Port *ip_port = *res; + for (cur = infos; cur != NULL; cur = cur->ai_next) { + ip_port->ip.family = cur->ai_family; + + if (cur->ai_socktype && type > 0 && cur->ai_socktype != type) { + continue; + } + + if (cur->ai_family == AF_INET) { + struct sockaddr_in *addr = (struct sockaddr_in *)cur->ai_addr; + memcpy(&ip_port->ip.ip4, &addr->sin_addr, sizeof(IP4)); + } else if (cur->ai_family == AF_INET6) { + struct sockaddr_in6 *addr = (struct sockaddr_in6 *)cur->ai_addr; + memcpy(&ip_port->ip.ip6, &addr->sin6_addr, sizeof(IP6)); + } else { + continue; + } + + ip_port++; + } + + freeaddrinfo(infos); + + return count; +} + +void net_freeipport(IP_Port* ip_ports) +{ + free(ip_ports); +} + +/* return 1 on success + * return 0 on failure + */ +int bind_to_port(Socket sock, int family, uint16_t port) +{ + struct sockaddr_storage addr = {0}; + size_t addrsize; + + if (family == AF_INET) { + struct sockaddr_in *addr4 = (struct sockaddr_in *)&addr; + + addrsize = sizeof(struct sockaddr_in); + addr4->sin_family = AF_INET; + addr4->sin_port = net_htons(port); + } else if (family == AF_INET6) { + struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *)&addr; + + addrsize = sizeof(struct sockaddr_in6); + addr6->sin6_family = AF_INET6; + addr6->sin6_port = net_htons(port); + } else { + return 0; + } + + return (bind(sock, (struct sockaddr *)&addr, addrsize) == 0); +} + +static int make_family(int family) +{ + switch (family) { + case TOX_AF_INET: + return AF_INET; + case TOX_AF_INET6: + return AF_INET6; + default: + return family; + } +} + +static int make_socktype(int type) +{ + switch (type) { + case TOX_SOCK_STREAM: + return SOCK_STREAM; + case TOX_SOCK_DGRAM: + return SOCK_DGRAM; + default: + return type; + } +} + +static int make_proto(int proto) +{ + switch (proto) { + case TOX_PROTO_TCP: + return IPPROTO_TCP; + case TOX_PROTO_UDP: + return IPPROTO_UDP; + default: + return proto; + } +} + +Socket net_socket(int domain, int type, int protocol) +{ + int platform_domain = make_family(domain); + int platform_type = make_socktype(type); + int platform_prot = make_proto(protocol); + return socket(platform_domain, platform_type, platform_prot); +} + +/* TODO: Remove, when tox DNS support will be removed. + * Used only by dns3_test.c + */ +size_t net_sendto_ip4(Socket sock, const char* buf, size_t n, IP_Port ip_port) +{ + struct sockaddr_in target; + size_t addrsize = sizeof(target); + target.sin_family = make_family(ip_port.ip.family); + target.sin_port = net_htons(ip_port.port); + fill_addr4(ip_port.ip.ip4, &target.sin_addr); + + return (size_t)sendto(sock, buf, n, 0, (struct sockaddr *)&target, addrsize); +} + +uint32_t net_htonl(uint32_t hostlong) +{ + return htonl(hostlong); +} + +uint16_t net_htons(uint16_t hostshort) +{ + return htons(hostshort); +} + +uint32_t net_ntohl(uint32_t hostlong) +{ + return ntohl(hostlong); +} + +uint16_t net_ntohs(uint16_t hostshort) +{ + return ntohs(hostshort); +} + diff --git a/toxcore/network.h b/toxcore/network.h index bcd1abd1b..e6d7bbef9 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -53,9 +53,6 @@ #include #include -/* sa_family_t is the sockaddr_in / sockaddr_in6 family field */ -typedef short sa_family_t; - #else // Linux includes #include @@ -66,8 +63,12 @@ typedef short sa_family_t; struct in_addr; struct in6_addr; +struct addrinfo; + +typedef short Family; typedef int Socket; +Socket net_socket(int domain, int type, int protocol); #define MAX_UDP_PACKET_SIZE 2048 @@ -107,6 +108,18 @@ typedef enum NET_PACKET_TYPE { #define TOX_PORTRANGE_TO 33545 #define TOX_PORT_DEFAULT TOX_PORTRANGE_FROM +/* Redefinitions of variables for safe transfer over wire. */ +#define TOX_AF_INET 2 +#define TOX_AF_INET6 10 +#define TOX_TCP_INET 130 +#define TOX_TCP_INET6 138 + +#define TOX_SOCK_STREAM 1 +#define TOX_SOCK_DGRAM 2 + +#define TOX_PROTO_TCP 1 +#define TOX_PROTO_UDP 2 + /* TCP related */ #define TCP_ONION_FAMILY (AF_INET6 + 1) #define TCP_INET (AF_INET6 + 2) @@ -151,6 +164,13 @@ void get_ip6(IP6 *ip, const struct in6_addr *addr); void fill_addr4(IP4 ip, struct in_addr *addr); void fill_addr6(IP6 ip, struct in6_addr *addr); +/* Convert values between host and network byte order. + */ +uint32_t net_htonl(uint32_t hostlong); +uint16_t net_htons(uint16_t hostshort); +uint32_t net_ntohl(uint32_t hostlong); +uint16_t net_ntohs(uint16_t hostshort); + /* Does the IP6 struct a contain an IPv4 address in an IPv6 one? */ #define IPV6_IPV4_IN_V6(a) ((a.uint64[0] == 0) && (a.uint32[2] == htonl (0xffff))) @@ -292,7 +312,7 @@ typedef struct { Logger *log; Packet_Handles packethandlers[256]; - sa_family_t family; + Family family; uint16_t port; /* Our UDP socket. */ Socket sock; @@ -358,6 +378,32 @@ void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handl /* Call this several times a second. */ void networking_poll(Networking_Core *net, void *userdata); +/* Connect a socket to the address specified by the ip_port. */ +int net_connect(Socket sock, IP_Port ip_port); + +/* High-level getaddrinfo implementation. + * Given node, which identifies an Internet host, net_getipport() fills an array + * with one or more IP_Port structures, each of which contains an Internet + * address that can be specified by calling net_connect(), the port is ignored. + * + * Skip all addresses with socktype != type (use type = -1 to get all addresses) + * To correctly deallocate array memory use net_freeipport() + * + * return number of elements in res array. + */ +int32_t net_getipport(const char* node, IP_Port** res, int type); + +/* Deallocates memory allocated by net_getipport + */ +void net_freeipport(IP_Port* ip_ports); + +/* return 1 on success + * return 0 on failure + */ +int bind_to_port(Socket sock, int family, uint16_t port); + +size_t net_sendto_ip4(Socket sock, const char* buf, size_t n, IP_Port ip_port); + /* Initialize networking. * bind to ip and port. * ip must be in network order EX: 127.0.0.1 = (7F000001). diff --git a/toxcore/tox.c b/toxcore/tox.c index adc537536..ed7419953 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -162,7 +162,7 @@ Tox *tox_new(const struct Tox_Options *options, TOX_ERR_NEW *error) return NULL; } - m_options.proxy_info.ip_port.port = htons(tox_options_get_proxy_port(options)); + m_options.proxy_info.ip_port.port = net_htons(tox_options_get_proxy_port(options)); } } @@ -233,43 +233,24 @@ bool tox_bootstrap(Tox *tox, const char *address, uint16_t port, const uint8_t * return 0; } - struct addrinfo *root, *info; + IP_Port *root; - if (getaddrinfo(address, NULL, NULL, &root) != 0) { + int32_t count = net_getipport(address, &root, SOCK_DGRAM); + if (count == -1) { SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); return 0; } - info = root; - - unsigned int count = 0; - - do { - IP_Port ip_port; - ip_port.port = htons(port); - ip_port.ip.family = info->ai_family; - - if (info->ai_socktype && info->ai_socktype != SOCK_DGRAM) { - continue; - } - - if (info->ai_family == AF_INET) { - struct sockaddr_in *addr = (struct sockaddr_in *)info->ai_addr; - get_ip4(&ip_port.ip.ip4, &addr->sin_addr); - } else if (info->ai_family == AF_INET6) { - struct sockaddr_in6 *addr = (struct sockaddr_in6 *)info->ai_addr; - get_ip6(&ip_port.ip.ip6, &addr->sin6_addr); - } else { - continue; - } + unsigned int i; + for (i = 0; i < count; i++) { + root[i].port = htons(port); Messenger *m = tox; - onion_add_bs_path_node(m->onion_c, ip_port, public_key); - DHT_bootstrap(m->dht, ip_port, public_key); - ++count; - } while ((info = info->ai_next)); + onion_add_bs_path_node(m->onion_c, root[i], public_key); + DHT_bootstrap(m->dht, root[i], public_key); + } - freeaddrinfo(root); + net_freeipport(root); if (count) { SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK); @@ -293,42 +274,23 @@ bool tox_add_tcp_relay(Tox *tox, const char *address, uint16_t port, const uint8 return 0; } - struct addrinfo *root, *info; + IP_Port *root; - if (getaddrinfo(address, NULL, NULL, &root) != 0) { + int32_t count = net_getipport(address, &root, SOCK_STREAM); + if (count == -1) { SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); return 0; } - info = root; - - unsigned int count = 0; - - do { - IP_Port ip_port; - ip_port.port = htons(port); - ip_port.ip.family = info->ai_family; - - if (info->ai_socktype && info->ai_socktype != SOCK_STREAM) { - continue; - } - - if (info->ai_family == AF_INET) { - struct sockaddr_in *addr = (struct sockaddr_in *)info->ai_addr; - get_ip4(&ip_port.ip.ip4, &addr->sin_addr); - } else if (info->ai_family == AF_INET6) { - struct sockaddr_in6 *addr = (struct sockaddr_in6 *)info->ai_addr; - get_ip6(&ip_port.ip.ip6, &addr->sin6_addr); - } else { - continue; - } + unsigned int i; + for (i = 0; i < count; i++) { + root[i].port = htons(port); Messenger *m = tox; - add_tcp_relay(m->net_crypto, ip_port, public_key); - ++count; - } while ((info = info->ai_next)); + add_tcp_relay(m->net_crypto, root[i], public_key); + } - freeaddrinfo(root); + net_freeipport(root); if (count) { SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_OK); @@ -387,13 +349,13 @@ void tox_self_get_address(const Tox *tox, uint8_t *address) void tox_self_set_nospam(Tox *tox, uint32_t nospam) { Messenger *m = tox; - set_nospam(&(m->fr), htonl(nospam)); + set_nospam(&(m->fr), net_htonl(nospam)); } uint32_t tox_self_get_nospam(const Tox *tox) { const Messenger *m = tox; - return ntohl(get_nospam(&(m->fr))); + return net_ntohl(get_nospam(&(m->fr))); } void tox_self_get_public_key(const Tox *tox, uint8_t *public_key) @@ -1558,7 +1520,7 @@ void tox_self_get_dht_id(const Tox *tox, uint8_t *dht_id) uint16_t tox_self_get_udp_port(const Tox *tox, TOX_ERR_GET_PORT *error) { const Messenger *m = tox; - uint16_t port = htons(m->net->port); + uint16_t port = net_htons(m->net->port); if (port) { SET_ERROR_PARAMETER(error, TOX_ERR_GET_PORT_OK);