From dbd75e903e7931a3f673df6121a0dedab15c0423 Mon Sep 17 00:00:00 2001 From: "Coren[m]" Date: Mon, 9 Sep 2013 16:12:50 +0200 Subject: network.*: - IPAny_Port: analogous to IP_Port - ipport_equal: moved from DHT.c and adapted DHTc.: - ipport_equal renamed to ip4port_equal --- toxcore/DHT.c | 6 +++--- toxcore/network.c | 37 +++++++++++++++++++++++++++++++++++++ toxcore/network.h | 16 ++++++++++++++++ 3 files changed, 56 insertions(+), 3 deletions(-) (limited to 'toxcore') diff --git a/toxcore/DHT.c b/toxcore/DHT.c index 3f3aef05..151f59d3 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -115,7 +115,7 @@ static int client_id_cmp(ClientPair p1, ClientPair p2) return c; } -static int ipport_equal(IP_Port a, IP_Port b) +static int ip4port_equal(IP_Port a, IP_Port b) { return (a.ip.uint32 == b.ip.uint32) && (a.port == b.port); } @@ -144,7 +144,7 @@ static int client_in_list(Client_data *list, uint32_t length, uint8_t *client_id for (i = 0; i < length; ++i) { /* If ip_port is assigned to a different client_id replace it */ - if (ipport_equal(list[i].ip_port, ip_port)) { + if (ip4port_equal(list[i].ip_port, ip_port)) { memcpy(list[i].client_id, client_id, CLIENT_ID_SIZE); } @@ -447,7 +447,7 @@ static int is_gettingnodes(DHT *dht, IP_Port ip_port, uint64_t ping_id) if (!is_timeout(temp_time, dht->send_nodes[i].timestamp, PING_TIMEOUT)) { pinging = 0; - if (ip_port.ip.uint32 != 0 && ipport_equal(dht->send_nodes[i].ip_port, ip_port)) + if (ip_port.ip.uint32 != 0 && ip4port_equal(dht->send_nodes[i].ip_port, ip_port)) ++pinging; if (ping_id != 0 && dht->send_nodes[i].ping_id == ping_id) diff --git a/toxcore/network.c b/toxcore/network.c index 31da833c..1fb5b77a 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -235,6 +235,43 @@ void kill_networking(Networking_Core *net) return; } +/* ip_equal + * compares two IPAny structures + * unset means unequal + * + * returns 0 when not equal or when uninitialized + */ +int ip_equal(IPAny *a, IPAny *b) +{ + if (!a || !b) + return 0; + + if (a->family == AF_INET) + return (a->ip4.in_addr.s_addr == b->ip4.in_addr.s_addr); + + if (a->family == AF_INET6) + return IN6_ARE_ADDR_EQUAL(&a->ip6, &b->ip6); + + return 0; +}; + +/* ipport_equal + * compares two IPAny_Port structures + * unset means unequal + * + * returns 0 when not equal or when uninitialized + */ +int ipport_equal(IPAny_Port *a, IPAny_Port *b) +{ + if (!a || !b) + return 0; + + if (!a->port || (a->port != b->port)) + return 0; + + return ip_equal(&a->ip, &b->ip); +}; + /* ipany_ntoa * converts ip into a string * uses a static buffer, so mustn't used multiple times in the same output diff --git a/toxcore/network.h b/toxcore/network.h index 15ca68e3..d52a02ae 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -115,6 +115,22 @@ typedef union { uint8_t uint8[8]; } IP_Port; + +/* will replace IP_Port as soon as the complete infrastructure is in place + * removed the unused union and padding also */ +typedef struct { + IPAny ip; + uint16_t port; +} IPAny_Port; + +/* ipport_equal + * compares two IPAny_Port structures + * unset means unequal + * + * returns 0 when not equal or when uninitialized + */ +int ipport_equal(IPAny_Port *a, IPAny_Port *b); + typedef struct { int16_t family; uint16_t port; -- cgit v1.2.3