Removed packet structs.

They were causing problems on certain compiler configurations.
This commit is contained in:
irungentoo 2013-08-29 19:06:09 -04:00
parent ea994606fe
commit df4b1c6ee5
5 changed files with 60 additions and 57 deletions

View File

@ -24,7 +24,6 @@
/*----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------------*/
#include "DHT.h" #include "DHT.h"
#include "packets.h"
#include "ping.h" #include "ping.h"
/* The number of seconds for a non responsive node to become bad. */ /* The number of seconds for a non responsive node to become bad. */
@ -563,7 +562,7 @@ static int handle_getnodes(void *object, IP_Port source, uint8_t *packet, uint32
memcpy(&ping_id, plain, sizeof(ping_id)); memcpy(&ping_id, plain, sizeof(ping_id));
sendnodes(dht, source, packet + 1, plain + sizeof(ping_id), ping_id); sendnodes(dht, source, packet + 1, plain + sizeof(ping_id), ping_id);
// send_ping_request(dht, source, (clientid_t*) (packet + 1)); /* TODO: make this smarter? */ //send_ping_request(dht, source, packet + 1); /* TODO: make this smarter? */
return 0; return 0;
} }
@ -606,7 +605,7 @@ static int handle_sendnodes(void *object, IP_Port source, uint8_t *packet, uint3
uint32_t i; uint32_t i;
for (i = 0; i < num_nodes; ++i) { for (i = 0; i < num_nodes; ++i) {
send_ping_request(dht->ping, dht->c, nodes_list[i].ip_port, (clientid_t *) &nodes_list[i].client_id); send_ping_request(dht->ping, dht->c, nodes_list[i].ip_port, nodes_list[i].client_id);
returnedip_ports(dht, nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1); returnedip_ports(dht, nodes_list[i].ip_port, nodes_list[i].client_id, packet + 1);
} }
@ -713,7 +712,7 @@ static void do_DHT_friends(DHT *dht)
if (!is_timeout(temp_time, dht->friends_list[i].client_list[j].timestamp, Kill_NODE_TIMEOUT)) { if (!is_timeout(temp_time, dht->friends_list[i].client_list[j].timestamp, Kill_NODE_TIMEOUT)) {
if ((dht->friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) { if ((dht->friends_list[i].client_list[j].last_pinged + PING_INTERVAL) <= temp_time) {
send_ping_request(dht->ping, dht->c, dht->friends_list[i].client_list[j].ip_port, send_ping_request(dht->ping, dht->c, dht->friends_list[i].client_list[j].ip_port,
(clientid_t *) &dht->friends_list[i].client_list[j].client_id ); dht->friends_list[i].client_list[j].client_id );
dht->friends_list[i].client_list[j].last_pinged = temp_time; dht->friends_list[i].client_list[j].last_pinged = temp_time;
} }
@ -751,7 +750,7 @@ static void do_Close(DHT *dht)
if (!is_timeout(temp_time, dht->close_clientlist[i].timestamp, Kill_NODE_TIMEOUT)) { if (!is_timeout(temp_time, dht->close_clientlist[i].timestamp, Kill_NODE_TIMEOUT)) {
if ((dht->close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) { if ((dht->close_clientlist[i].last_pinged + PING_INTERVAL) <= temp_time) {
send_ping_request(dht->ping, dht->c, dht->close_clientlist[i].ip_port, send_ping_request(dht->ping, dht->c, dht->close_clientlist[i].ip_port,
(clientid_t *) &dht->close_clientlist[i].client_id ); dht->close_clientlist[i].client_id );
dht->close_clientlist[i].last_pinged = temp_time; dht->close_clientlist[i].last_pinged = temp_time;
} }
@ -775,7 +774,7 @@ static void do_Close(DHT *dht)
void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key) void DHT_bootstrap(DHT *dht, IP_Port ip_port, uint8_t *public_key)
{ {
getnodes(dht, ip_port, public_key, dht->c->self_public_key); getnodes(dht, ip_port, public_key, dht->c->self_public_key);
send_ping_request(dht->ping, dht->c, ip_port, (clientid_t *) public_key); send_ping_request(dht->ping, dht->c, ip_port, public_key);
} }
/* Send the given packet to node with client_id /* Send the given packet to node with client_id
@ -1042,7 +1041,7 @@ static void punch_holes(DHT *dht, IP ip, uint16_t *port_list, uint16_t numports,
/* TODO: improve port guessing algorithm */ /* TODO: improve port guessing algorithm */
uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1); uint16_t port = port_list[(i / 2) % numports] + (i / (2 * numports)) * ((i % 2) ? -1 : 1);
IP_Port pinging = {ip, htons(port)}; IP_Port pinging = {ip, htons(port)};
send_ping_request(dht->ping, dht->c, pinging, (clientid_t *) &dht->friends_list[friend_num].client_id); send_ping_request(dht->ping, dht->c, pinging, dht->friends_list[friend_num].client_id);
} }
dht->friends_list[friend_num].punching_index = i; dht->friends_list[friend_num].punching_index = i;
@ -1143,7 +1142,7 @@ static void do_toping(DHT *dht)
if (dht->toping[i].ip_port.ip.i == 0) if (dht->toping[i].ip_port.ip.i == 0)
return; return;
send_ping_request(dht->ping, dht->c, dht->toping[i].ip_port, (clientid_t *) dht->toping[i].client_id); send_ping_request(dht->ping, dht->c, dht->toping[i].ip_port, dht->toping[i].client_id);
dht->toping[i].ip_port.ip.i = 0; dht->toping[i].ip_port.ip.i = 0;
} }
} }

View File

@ -10,7 +10,6 @@
#include "DHT.h" #include "DHT.h"
#include "net_crypto.h" #include "net_crypto.h"
#include "packets.h"
#include "network.h" #include "network.h"
#include "util.h" #include "util.h"
@ -116,84 +115,88 @@ bool is_pinging(void *ping, IP_Port ipp, uint64_t ping_id) // O(n) TODO: Repl
return false; return false;
} }
int send_ping_request(void *ping, Net_Crypto *c, IP_Port ipp, clientid_t *client_id) #define DHT_PING_SIZE (1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + sizeof(uint64_t) + ENCRYPTION_PADDING)
int send_ping_request(void *ping, Net_Crypto *c, IP_Port ipp, uint8_t *client_id)
{ {
pingreq_t pk; uint8_t pk[DHT_PING_SIZE];
int rc; int rc;
uint64_t ping_id; uint64_t ping_id;
if (is_pinging(ping, ipp, 0) || id_eq(client_id, (clientid_t *)c->self_public_key)) if (is_pinging(ping, ipp, 0) || id_eq(client_id, c->self_public_key))
return 1; return 1;
// Generate random ping_id. // Generate random ping_id.
ping_id = add_ping(ping, ipp); ping_id = add_ping(ping, ipp);
pk.packet_id = NET_PACKET_PING_REQUEST; pk[0] = NET_PACKET_PING_REQUEST;
id_cpy(&pk.client_id, (clientid_t *)c->self_public_key); // Our pubkey. id_cpy(pk + 1, c->self_public_key); // Our pubkey
random_nonce((uint8_t *) &pk.nonce); // Generate random nonce. random_nonce(pk + 1 + CLIENT_ID_SIZE); // Generate random nonce
/* Encrypt ping_id using recipient privkey. */ // Encrypt ping_id using recipient privkey
rc = encrypt_data((uint8_t *) client_id, rc = encrypt_data(client_id,
c->self_secret_key, c->self_secret_key,
(uint8_t *) &pk.nonce, pk + 1 + CLIENT_ID_SIZE,
(uint8_t *) &ping_id, sizeof(ping_id), (uint8_t *) &ping_id, sizeof(ping_id),
(uint8_t *) &pk.ping_id); pk + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES);
if (rc != sizeof(ping_id) + ENCRYPTION_PADDING) if (rc != sizeof(ping_id) + ENCRYPTION_PADDING)
return 1; return 1;
return sendpacket(c->lossless_udp->net->sock, ipp, (uint8_t *) &pk, sizeof(pk)); return sendpacket(c->lossless_udp->net->sock, ipp, pk, sizeof(pk));
} }
int send_ping_response(Net_Crypto *c, IP_Port ipp, clientid_t *client_id, uint64_t ping_id) int send_ping_response(Net_Crypto *c, IP_Port ipp, uint8_t *client_id, uint64_t ping_id)
{ {
pingres_t pk; uint8_t pk[DHT_PING_SIZE];
int rc; int rc;
if (id_eq(client_id, (clientid_t *)c->self_public_key)) if (id_eq(client_id, c->self_public_key))
return 1; return 1;
pk.packet_id = NET_PACKET_PING_RESPONSE; pk[0] = NET_PACKET_PING_RESPONSE;
id_cpy(&pk.client_id, (clientid_t *)c->self_public_key); // Our pubkey. id_cpy(pk + 1, c->self_public_key); // Our pubkey
random_nonce((uint8_t *) &pk.nonce); // Generate random nonce. random_nonce(pk + 1 + CLIENT_ID_SIZE); // Generate random nonce
/* Encrypt ping_id using recipient privkey */ // Encrypt ping_id using recipient privkey
rc = encrypt_data((uint8_t *) client_id, rc = encrypt_data(client_id,
c->self_secret_key, c->self_secret_key,
(uint8_t *) &pk.nonce, pk + 1 + CLIENT_ID_SIZE,
(uint8_t *) &ping_id, sizeof(ping_id), (uint8_t *) &ping_id, sizeof(ping_id),
(uint8_t *) &pk.ping_id); pk + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES);
if (rc != sizeof(ping_id) + ENCRYPTION_PADDING) if (rc != sizeof(ping_id) + ENCRYPTION_PADDING)
return 1; return 1;
return sendpacket(c->lossless_udp->net->sock, ipp, (uint8_t *) &pk, sizeof(pk)); return sendpacket(c->lossless_udp->net->sock, ipp, pk, sizeof(pk));
} }
int handle_ping_request(void *object, IP_Port source, uint8_t *packet, uint32_t length) int handle_ping_request(void *object, IP_Port source, uint8_t *packet, uint32_t length)
{ {
DHT *dht = object; DHT *dht = object;
pingreq_t *p = (pingreq_t *) packet;
int rc; int rc;
uint64_t ping_id; uint64_t ping_id;
if (length != sizeof(pingreq_t) || id_eq(&p->client_id, (clientid_t *)dht->c->self_public_key)) if (length != DHT_PING_SIZE)
return 1; return 1;
/* Decrypt ping_id. */ if (id_eq(packet + 1, dht->c->self_public_key))
rc = decrypt_data((uint8_t *) &p->client_id, return 1;
// Decrypt ping_id
rc = decrypt_data(packet + 1,
dht->c->self_secret_key, dht->c->self_secret_key,
(uint8_t *) &p->nonce, packet + 1 + CLIENT_ID_SIZE,
(uint8_t *) &p->ping_id, packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
sizeof(ping_id) + ENCRYPTION_PADDING, sizeof(ping_id) + ENCRYPTION_PADDING,
(uint8_t *) &ping_id); (uint8_t *) &ping_id);
if (rc != sizeof(ping_id)) if (rc != sizeof(ping_id))
return 1; return 1;
/* Send response. */ // Send response
send_ping_response(dht->c, source, &p->client_id, ping_id); send_ping_response(dht->c, source, packet + 1, ping_id);
add_toping(dht, (uint8_t *) &p->client_id, source); add_toping(dht, packet + 1, source);
return 0; return 0;
} }
@ -201,18 +204,20 @@ int handle_ping_request(void *object, IP_Port source, uint8_t *packet, uint32_t
int handle_ping_response(void *object, IP_Port source, uint8_t *packet, uint32_t length) int handle_ping_response(void *object, IP_Port source, uint8_t *packet, uint32_t length)
{ {
DHT *dht = object; DHT *dht = object;
pingres_t *p = (pingres_t *) packet;
int rc; int rc;
uint64_t ping_id; uint64_t ping_id;
if (length != sizeof(pingres_t) || id_eq(&p->client_id, (clientid_t *)dht->c->self_public_key)) if (length != DHT_PING_SIZE)
return 1; return 1;
/* Decrypt ping_id. */ if (id_eq(packet + 1, dht->c->self_public_key))
rc = decrypt_data((uint8_t *) &p->client_id, return 1;
// Decrypt ping_id
rc = decrypt_data(packet + 1,
dht->c->self_secret_key, dht->c->self_secret_key,
(uint8_t *) &p->nonce, packet + 1 + CLIENT_ID_SIZE,
(uint8_t *) &p->ping_id, packet + 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES,
sizeof(ping_id) + ENCRYPTION_PADDING, sizeof(ping_id) + ENCRYPTION_PADDING,
(uint8_t *) &ping_id); (uint8_t *) &ping_id);
@ -223,7 +228,7 @@ int handle_ping_response(void *object, IP_Port source, uint8_t *packet, uint32_t
if (!is_pinging(dht->ping, source, ping_id)) if (!is_pinging(dht->ping, source, ping_id))
return 1; return 1;
/* Associate source ip with client_id. */ // Associate source ip with client_id
addto_lists(dht, source, (uint8_t *) &p->client_id); addto_lists(dht, source, packet + 1);
return 0; return 0;
} }

View File

@ -11,7 +11,7 @@ void *new_ping(void);
void kill_ping(void *ping); void kill_ping(void *ping);
uint64_t add_ping(void *ping, IP_Port ipp); uint64_t add_ping(void *ping, IP_Port ipp);
bool is_pinging(void *ping, IP_Port ipp, uint64_t ping_id); bool is_pinging(void *ping, IP_Port ipp, uint64_t ping_id);
int send_ping_request(void *ping, Net_Crypto *c, IP_Port ipp, clientid_t *client_id); int send_ping_request(void *ping, Net_Crypto *c, IP_Port ipp, uint8_t *client_id);
int send_ping_response(Net_Crypto *c, IP_Port ipp, clientid_t *client_id, uint64_t ping_id); int send_ping_response(Net_Crypto *c, IP_Port ipp, uint8_t *client_id, uint64_t ping_id);
int handle_ping_request(void *object, IP_Port source, uint8_t *packet, uint32_t length); int handle_ping_request(void *object, IP_Port source, uint8_t *packet, uint32_t length);
int handle_ping_response(void *object, IP_Port source, uint8_t *packet, uint32_t length); int handle_ping_response(void *object, IP_Port source, uint8_t *packet, uint32_t length);

View File

@ -10,7 +10,6 @@
#include <stdbool.h> #include <stdbool.h>
#include "DHT.h" #include "DHT.h"
#include "packets.h"
uint64_t now() uint64_t now()
{ {
@ -34,12 +33,12 @@ bool ipp_eq(IP_Port a, IP_Port b)
return (a.ip.i == b.ip.i) && (a.port == b.port); return (a.ip.i == b.ip.i) && (a.port == b.port);
} }
bool id_eq(clientid_t *dest, clientid_t *src) bool id_eq(uint8_t *dest, uint8_t *src)
{ {
return memcmp(dest, src, sizeof(clientid_t)) == 0; return memcmp(dest, src, CLIENT_ID_SIZE) == 0;
} }
void id_cpy(clientid_t *dest, clientid_t *src) void id_cpy(uint8_t *dest, uint8_t *src)
{ {
memcpy(dest, src, sizeof(clientid_t)); memcpy(dest, src, CLIENT_ID_SIZE);
} }

View File

@ -8,5 +8,5 @@
uint64_t now(); uint64_t now();
uint64_t random_64b(); uint64_t random_64b();
bool ipp_eq(IP_Port a, IP_Port b); bool ipp_eq(IP_Port a, IP_Port b);
bool id_eq(clientid_t *dest, clientid_t *src); bool id_eq(uint8_t *dest, uint8_t *src);
void id_cpy(clientid_t *dest, clientid_t *src); void id_cpy(uint8_t *dest, uint8_t *src);