diff --git a/testing/dns3_test.c b/testing/dns3_test.c index 37bbfadb..821f3b4b 100644 --- a/testing/dns3_test.c +++ b/testing/dns3_test.c @@ -92,6 +92,7 @@ int main(int argc, char *argv[]) } unsigned int i; + for (i = r_len - 1; i != 0 && buffer[i] != '='; --i) { ; } diff --git a/toxcore/network.c b/toxcore/network.c index 92a04276..69dacee4 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -1209,16 +1209,19 @@ int net_connect(Socket sock, IP_Port ip_port) return connect(sock, (struct sockaddr *)&addr, addrsize); } -int32_t net_getipport(const char* node, IP_Port** res, int type) +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; @@ -1235,12 +1238,14 @@ int32_t net_getipport(const char* node, IP_Port** res, int type) return -1; } - *res = (IP_Port*)malloc(sizeof(IP_Port) * count); + *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) { if (cur->ai_socktype && type > 0 && cur->ai_socktype != type) { continue; @@ -1266,7 +1271,7 @@ int32_t net_getipport(const char* node, IP_Port** res, int type) return count; } -void net_freeipport(IP_Port* ip_ports) +void net_freeipport(IP_Port *ip_ports) { free(ip_ports); } @@ -1301,36 +1306,42 @@ int bind_to_port(Socket sock, int family, uint16_t port) 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; + 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; + 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; + case TOX_PROTO_TCP: + return IPPROTO_TCP; + + case TOX_PROTO_UDP: + return IPPROTO_UDP; + + default: + return proto; } } @@ -1345,7 +1356,7 @@ Socket net_socket(int domain, int type, int protocol) /* 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) +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); diff --git a/toxcore/network.h b/toxcore/network.h index e6d7bbef..92e5bbcf 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -391,18 +391,18 @@ int net_connect(Socket sock, IP_Port ip_port); * * return number of elements in res array. */ -int32_t net_getipport(const char* node, IP_Port** res, int type); +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); +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); +size_t net_sendto_ip4(Socket sock, const char *buf, size_t n, IP_Port ip_port); /* Initialize networking. * bind to ip and port. diff --git a/toxcore/tox.c b/toxcore/tox.c index ed741995..fe34c284 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c @@ -236,12 +236,14 @@ bool tox_bootstrap(Tox *tox, const char *address, uint16_t port, const uint8_t * IP_Port *root; int32_t count = net_getipport(address, &root, SOCK_DGRAM); + if (count == -1) { SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); return 0; } unsigned int i; + for (i = 0; i < count; i++) { root[i].port = htons(port); @@ -277,12 +279,14 @@ bool tox_add_tcp_relay(Tox *tox, const char *address, uint16_t port, const uint8 IP_Port *root; int32_t count = net_getipport(address, &root, SOCK_STREAM); + if (count == -1) { SET_ERROR_PARAMETER(error, TOX_ERR_BOOTSTRAP_BAD_HOST); return 0; } unsigned int i; + for (i = 0; i < count; i++) { root[i].port = htons(port); @@ -1513,7 +1517,7 @@ void tox_self_get_dht_id(const Tox *tox, uint8_t *dht_id) { if (dht_id) { const Messenger *m = tox; - memcpy(dht_id , m->dht->self_public_key, CRYPTO_PUBLIC_KEY_SIZE); + memcpy(dht_id, m->dht->self_public_key, CRYPTO_PUBLIC_KEY_SIZE); } }