From a128e3ff8f09250757ed4ed73fcc088cb5fd15ee Mon Sep 17 00:00:00 2001 From: "Coren[m]" Date: Mon, 9 Sep 2013 13:56:03 +0200 Subject: [PATCH] network.h: - global: added sock_t - Networking_Core: added family (currently always AF_INET) and port - sendpacket(): changed signature to require (Networking_Core *) instead of sock_t *.c: - sendpacket()-calls: replaced *->net->sock with *->net --- toxcore/DHT.c | 10 +++++----- toxcore/LAN_discovery.c | 2 +- toxcore/Lossless_UDP.c | 6 +++--- toxcore/friend_requests.c | 2 +- toxcore/network.c | 17 ++++++++--------- toxcore/network.h | 21 +++++++++------------ toxcore/ping.c | 4 ++-- 7 files changed, 29 insertions(+), 33 deletions(-) diff --git a/toxcore/DHT.c b/toxcore/DHT.c index e2d91256..e664c21c 100644 --- a/toxcore/DHT.c +++ b/toxcore/DHT.c @@ -518,7 +518,7 @@ static int getnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cli memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); - return sendpacket(dht->c->lossless_udp->net->sock, ip_port, data, sizeof(data)); + return sendpacket(dht->c->lossless_udp->net, ip_port, data, sizeof(data)); } /* Send a send nodes response. */ @@ -563,7 +563,7 @@ static int sendnodes(DHT *dht, IP_Port ip_port, uint8_t *public_key, uint8_t *cl memcpy(data + 1 + CLIENT_ID_SIZE, nonce, crypto_box_NONCEBYTES); memcpy(data + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES, encrypt, len); - return sendpacket(dht->c->lossless_udp->net->sock, ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len); + return sendpacket(dht->c->lossless_udp->net, ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len); } static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32_t length) @@ -819,7 +819,7 @@ int route_packet(DHT *dht, uint8_t *client_id, uint8_t *packet, uint32_t length) for (i = 0; i < LCLIENT_LIST; ++i) { if (id_equal(client_id, dht->close_clientlist[i].client_id)) - return sendpacket(dht->c->lossless_udp->net->sock, dht->close_clientlist[i].ip_port, packet, length); + return sendpacket(dht->c->lossless_udp->net, dht->close_clientlist[i].ip_port, packet, length); } return -1; @@ -891,7 +891,7 @@ int route_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint32_t lengt /* If ip is not zero and node is good. */ if (client->ret_ip_port.ip.uint32 != 0 && !is_timeout(temp_time, client->ret_timestamp, BAD_NODE_TIMEOUT)) { - int retval = sendpacket(dht->c->lossless_udp->net->sock, client->ip_port, packet, length); + int retval = sendpacket(dht->c->lossless_udp->net, client->ip_port, packet, length); if ((unsigned int)retval == length) ++sent; @@ -933,7 +933,7 @@ static int routeone_tofriend(DHT *dht, uint8_t *friend_id, uint8_t *packet, uint if (n < 1) return 0; - int retval = sendpacket(dht->c->lossless_udp->net->sock, ip_list[rand() % n], packet, length); + int retval = sendpacket(dht->c->lossless_udp->net, ip_list[rand() % n], packet, length); if ((unsigned int)retval == length) return 1; diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index 1980cd93..fe9f748b 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c @@ -136,7 +136,7 @@ int send_LANdiscovery(uint16_t port, Net_Crypto *c) send_broadcasts(c->lossless_udp->net, port, data, 1 + crypto_box_PUBLICKEYBYTES); #endif IP_Port ip_port = {{broadcast_ip(), port, 0}}; - return sendpacket(c->lossless_udp->net->sock, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES); + return sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES); } diff --git a/toxcore/Lossless_UDP.c b/toxcore/Lossless_UDP.c index fbb473e7..f66d5b62 100644 --- a/toxcore/Lossless_UDP.c +++ b/toxcore/Lossless_UDP.c @@ -429,7 +429,7 @@ static int send_handshake(Lossless_UDP *ludp, IP_Port ip_port, uint32_t handshak temp = htonl(handshake_id2); memcpy(packet + 5, &temp, 4); - return sendpacket(ludp->net->sock, ip_port, packet, sizeof(packet)); + return sendpacket(ludp->net, ip_port, packet, sizeof(packet)); } static int send_SYNC(Lossless_UDP *ludp, int connection_id) @@ -456,7 +456,7 @@ static int send_SYNC(Lossless_UDP *ludp, int connection_id) index += 4; memcpy(packet + index, requested, 4 * number); - return sendpacket(ludp->net->sock, ip_port, packet, (number * 4 + 4 + 4 + 2)); + return sendpacket(ludp->net, ip_port, packet, (number * 4 + 4 + 4 + 2)); } @@ -471,7 +471,7 @@ static int send_data_packet(Lossless_UDP *ludp, int connection_id, uint32_t pack temp = htonl(packet_num); memcpy(packet + 1, &temp, 4); memcpy(packet + 5, connection->sendbuffer[index].data, connection->sendbuffer[index].size); - return sendpacket(ludp->net->sock, connection->ip_port, packet, 1 + 4 + connection->sendbuffer[index].size); + return sendpacket(ludp->net, connection->ip_port, packet, 1 + 4 + connection->sendbuffer[index].size); } /* Sends 1 data packet. */ diff --git a/toxcore/friend_requests.c b/toxcore/friend_requests.c index b01015c4..51a8112f 100644 --- a/toxcore/friend_requests.c +++ b/toxcore/friend_requests.c @@ -56,7 +56,7 @@ int send_friendrequest(DHT *dht, uint8_t *public_key, uint32_t nospam_num, uint8 return -1; if (ip_port.ip.uint32 != 0) { - if (sendpacket(dht->c->lossless_udp->net->sock, ip_port, packet, len) != -1) + if (sendpacket(dht->c->lossless_udp->net, ip_port, packet, len) != -1) return 0; return -1; diff --git a/toxcore/network.c b/toxcore/network.c index ed3dff8a..53b7e3b5 100644 --- a/toxcore/network.c +++ b/toxcore/network.c @@ -64,14 +64,10 @@ uint32_t random_int(void) /* Basic network functions: * Function to send packet(data) of length length to ip_port. */ -#ifdef WIN32 -int sendpacket(unsigned int sock, IP_Port ip_port, uint8_t *data, uint32_t length) -#else -int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length) -#endif +int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t length) { ADDR addr = {AF_INET, ip_port.port, ip_port.ip, {0}}; - return sendto(sock, (char *) data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); + return sendto(net->sock, (char *) data, length, 0, (struct sockaddr *)&addr, sizeof(addr)); } /* Function to receive data @@ -171,7 +167,8 @@ Networking_Core *new_networking(IP ip, uint16_t port) if (temp == NULL) return NULL; - temp->sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); + temp->family = AF_INET; + temp->sock = socket(temp->family, SOCK_DGRAM, IPPROTO_UDP); /* Check for socket error. */ #ifdef WIN32 @@ -219,8 +216,10 @@ Networking_Core *new_networking(IP ip, uint16_t port) #endif /* Bind our socket to port PORT and address 0.0.0.0 */ - ADDR addr = {AF_INET, htons(port), ip, {0}}; - bind(temp->sock, (struct sockaddr *)&addr, sizeof(addr)); + ADDR addr = {temp->family, htons(port), ip, {0}}; + if (!bind(temp->sock, (struct sockaddr *)&addr, sizeof(addr))) + temp->port = port; + return temp; } diff --git a/toxcore/network.h b/toxcore/network.h index 2d08e88d..8d494957 100644 --- a/toxcore/network.h +++ b/toxcore/network.h @@ -39,6 +39,8 @@ #include #include +typedef unsigned int sock_t; + #else // Linux includes #include @@ -50,6 +52,8 @@ #include #include +typedef int sock_t; + #endif #ifndef VANILLA_NACL @@ -117,13 +121,11 @@ typedef struct { typedef struct { Packet_Handles packethandlers[256]; - /* Our UDP socket. */ -#ifdef WIN32 - unsigned int sock; -#else - int sock; -#endif + /* Our UDP socket. */ + sa_family_t family; + uint16_t port; + sock_t sock; } Networking_Core; /* return current time in milleseconds since the epoch. */ @@ -137,12 +139,7 @@ uint32_t random_int(void); /* Basic network functions: */ /* Function to send packet(data) of length length to ip_port. */ -#ifdef WIN32 -int sendpacket(unsigned int sock, IP_Port ip_port, uint8_t *data, uint32_t length); -#else -int sendpacket(int sock, IP_Port ip_port, uint8_t *data, uint32_t length); -#endif - +int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t length); /* Function to call when packet beginning with byte is received. */ void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handler_callback cb, void *object); diff --git a/toxcore/ping.c b/toxcore/ping.c index 3b39d911..5f405b52 100644 --- a/toxcore/ping.c +++ b/toxcore/ping.c @@ -147,7 +147,7 @@ int send_ping_request(void *ping, Net_Crypto *c, IP_Port ipp, uint8_t *client_id if (rc != sizeof(ping_id) + ENCRYPTION_PADDING) return 1; - return sendpacket(c->lossless_udp->net->sock, ipp, pk, sizeof(pk)); + return sendpacket(c->lossless_udp->net, ipp, pk, sizeof(pk)); } int send_ping_response(Net_Crypto *c, IP_Port ipp, uint8_t *client_id, uint64_t ping_id) @@ -172,7 +172,7 @@ int send_ping_response(Net_Crypto *c, IP_Port ipp, uint8_t *client_id, uint64_t if (rc != sizeof(ping_id) + ENCRYPTION_PADDING) return 1; - return sendpacket(c->lossless_udp->net->sock, ipp, pk, sizeof(pk)); + return sendpacket(c->lossless_udp->net, ipp, pk, sizeof(pk)); } int handle_ping_request(void *object, IP_Port source, uint8_t *packet, uint32_t length)