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
This commit is contained in:
Coren[m] 2013-09-09 13:56:03 +02:00
parent 0d41d7f9c0
commit a128e3ff8f
7 changed files with 29 additions and 33 deletions

View File

@ -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;

View File

@ -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);
}

View File

@ -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. */

View File

@ -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;

View File

@ -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;
}

View File

@ -39,6 +39,8 @@
#include <windows.h>
#include <ws2tcpip.h>
typedef unsigned int sock_t;
#else // Linux includes
#include <fcntl.h>
@ -50,6 +52,8 @@
#include <netdb.h>
#include <unistd.h>
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);

View File

@ -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)