Merge pull request #415 from slvr/pr

Beginning to centralise packet handling
This commit is contained in:
irungentoo 2013-08-10 14:21:06 -07:00
commit 6c2b2806d1
18 changed files with 117 additions and 141 deletions

View File

@ -554,7 +554,7 @@ static int sendnodes(IP_Port ip_port, uint8_t * public_key, uint8_t * client_id,
return sendpacket(ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len); return sendpacket(ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len);
} }
static int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) static int handle_getnodes(IP_Port source, uint8_t * packet, uint32_t length)
{ {
uint64_t ping_id; uint64_t ping_id;
@ -586,7 +586,7 @@ static int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source)
return 0; return 0;
} }
static int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) static int handle_sendnodes(IP_Port source, uint8_t * packet, uint32_t length)
{ {
uint64_t ping_id; uint64_t ping_id;
uint32_t cid_size = 1 + CLIENT_ID_SIZE; uint32_t cid_size = 1 + CLIENT_ID_SIZE;
@ -930,7 +930,7 @@ static int send_NATping(uint8_t * public_key, uint64_t ping_id, uint8_t type)
} }
/* Handle a received ping request for */ /* Handle a received ping request for */
static int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source) static int handle_NATping(IP_Port source, uint8_t * packet, uint32_t length)
{ {
if (length < crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING if (length < crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING
|| length > MAX_DATA_SIZE + ENCRYPTION_PADDING) || length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
@ -1074,30 +1074,13 @@ static void doNAT(void)
/*----------------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------------*/
/*-----------------------END OF NAT PUNCHING FUNCTIONS------------------------------*/ /*-----------------------END OF NAT PUNCHING FUNCTIONS------------------------------*/
int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) void DHT_init(void)
{ {
switch (packet[0]) { networking_registerhandler(0, &handle_ping_request);
case 0: networking_registerhandler(1, &handle_ping_response);
return handle_ping_request(packet, length, source); networking_registerhandler(2, &handle_getnodes);
networking_registerhandler(3, &handle_sendnodes);
case 1: networking_registerhandler(254, &handle_NATping);
return handle_ping_response(packet, length, source);
case 2:
return handle_getnodes(packet, length, source);
case 3:
return handle_sendnodes(packet, length, source);
case 254:
return handle_NATping(packet, length, source);
default:
return 1;
}
return 0;
} }
void doDHT(void) void doDHT(void)

View File

@ -58,11 +58,6 @@ IP_Port DHT_getfriendip(uint8_t *client_id);
/* Run this function at least a couple times per second (It's the main loop) */ /* Run this function at least a couple times per second (It's the main loop) */
void doDHT(void); void doDHT(void);
/* if we receive a DHT packet we call this function so it can be handled.
return 0 if packet is handled correctly.
return 1 if it didn't handle the packet or if the packet was shit. */
int DHT_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
/* Use this function to bootstrap the client /* Use this function to bootstrap the client
Sends a get nodes request to the given node with ip port and public_key */ Sends a get nodes request to the given node with ip port and public_key */
void DHT_bootstrap(IP_Port ip_port, uint8_t *public_key); void DHT_bootstrap(IP_Port ip_port, uint8_t *public_key);
@ -93,6 +88,9 @@ uint32_t DHT_size(void);
/* save the DHT in data where data is an array of size DHT_size() */ /* save the DHT in data where data is an array of size DHT_size() */
void DHT_save(uint8_t *data); void DHT_save(uint8_t *data);
/* init DHT */
void DHT_init(void);
/* load the DHT from data of size size; /* load the DHT from data of size size;
return -1 if failure return -1 if failure
return 0 if success */ return 0 if success */

View File

@ -111,7 +111,7 @@ static int LAN_ip(IP ip)
return -1; return -1;
} }
static int handle_LANdiscovery(uint8_t *packet, uint32_t length, IP_Port source) static int handle_LANdiscovery(IP_Port source, uint8_t *packet, uint32_t length)
{ {
if (LAN_ip(source.ip) == -1) if (LAN_ip(source.ip) == -1)
return 1; return 1;
@ -125,16 +125,14 @@ static int handle_LANdiscovery(uint8_t *packet, uint32_t length, IP_Port source)
int send_LANdiscovery(uint16_t port) int send_LANdiscovery(uint16_t port)
{ {
uint8_t data[crypto_box_PUBLICKEYBYTES + 1]; uint8_t data[crypto_box_PUBLICKEYBYTES + 1];
data[0] = 32; data[0] = 33;
memcpy(data + 1, self_public_key, crypto_box_PUBLICKEYBYTES); memcpy(data + 1, self_public_key, crypto_box_PUBLICKEYBYTES);
IP_Port ip_port = {broadcast_ip(), port}; IP_Port ip_port = {broadcast_ip(), port};
return sendpacket(ip_port, data, 1 + crypto_box_PUBLICKEYBYTES); return sendpacket(ip_port, data, 1 + crypto_box_PUBLICKEYBYTES);
} }
int LANdiscovery_handlepacket(uint8_t *packet, uint32_t length, IP_Port source) void LANdiscovery_init(void)
{ {
if (packet[0] == 32) networking_registerhandler(33, &handle_LANdiscovery);
return handle_LANdiscovery(packet, length, source);
return 1;
} }

View File

@ -43,10 +43,8 @@ extern "C" {
int send_LANdiscovery(uint16_t port); int send_LANdiscovery(uint16_t port);
/* if we receive a packet we call this function so it can be handled. /* sets up packet handlers */
return 0 if packet is handled correctly. void LANdiscovery_init(void);
return 1 if it didn't handle the packet or if the packet was shit. */
int LANdiscovery_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);

View File

@ -555,7 +555,7 @@ static int send_DATA(uint32_t connection_id)
/* Return 0 if handled correctly, 1 if packet is bad. */ /* Return 0 if handled correctly, 1 if packet is bad. */
static int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) static int handle_handshake(IP_Port source, uint8_t * packet, uint32_t length)
{ {
if (length != (1 + 4 + 4)) if (length != (1 + 4 + 4))
return 1; return 1;
@ -669,7 +669,7 @@ static int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packet
return 1; return 1;
} }
static int handle_SYNC(uint8_t *packet, uint32_t length, IP_Port source) static int handle_SYNC(IP_Port source, uint8_t *packet, uint32_t length)
{ {
if (!SYNC_valid(length)) if (!SYNC_valid(length))
@ -742,7 +742,7 @@ static int add_recv(int connection_id, uint32_t data_num, uint8_t *data, uint16_
return 0; return 0;
} }
static int handle_data(uint8_t *packet, uint32_t length, IP_Port source) static int handle_data(IP_Port source, uint8_t *packet, uint32_t length)
{ {
int connection = getconnection_id(source); int connection = getconnection_id(source);
@ -770,23 +770,11 @@ static int handle_data(uint8_t *packet, uint32_t length, IP_Port source)
* END of packet handling functions * END of packet handling functions
*/ */
int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source) void LosslessUDP_init(void)
{ {
switch (packet[0]) { networking_registerhandler(16, &handle_handshake);
case 16: networking_registerhandler(17, &handle_SYNC);
return handle_handshake(packet, length, source); networking_registerhandler(18, &handle_data);
case 17:
return handle_SYNC(packet, length, source);
case 18:
return handle_data(packet, length, source);
default:
return 1;
}
return 0;
} }
/* /*

View File

@ -113,11 +113,9 @@ int is_connected(int connection_id);
void doLossless_UDP(void); void doLossless_UDP(void);
/* /*
* If we receive a Lossless_UDP packet, call this function so it can be handled. * This function sets up LosslessUDP packet handling.
* Return 0 if packet is handled correctly.
* Return 1 if it didn't handle the packet or if the packet was shit.
*/ */
int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source); void LosslessUDP_init(void);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -518,6 +518,11 @@ int initMessenger(void)
if(init_networking(ip,PORT) == -1) if(init_networking(ip,PORT) == -1)
return -1; return -1;
DHT_init();
LosslessUDP_init();
friendreq_init();
LANdiscovery_init();
return 0; return 0;
} }
@ -684,29 +689,8 @@ static void LANdiscovery(void)
/* the main loop that needs to be run at least 200 times per second. */ /* the main loop that needs to be run at least 200 times per second. */
void doMessenger(void) void doMessenger(void)
{ {
IP_Port ip_port; networking_poll();
uint8_t data[MAX_UDP_PACKET_SIZE];
uint32_t length;
while (receivepacket(&ip_port, data, &length) != -1) {
#ifdef DEBUG
/* if(rand() % 3 != 1) //simulate packet loss */
/* { */
if (DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port) &&
friendreq_handlepacket(data, length, ip_port) && LANdiscovery_handlepacket(data, length, ip_port))
/* if packet is discarded */
printf("Received unhandled packet with length: %u\n", length);
else
printf("Received handled packet with length: %u\n", length);
/* } */
printf("Status: %u %u %u\n",friendlist[0].status ,is_cryptoconnected(friendlist[0].crypt_connection_id), friendlist[0].crypt_connection_id);
#else
DHT_handlepacket(data, length, ip_port);
LosslessUDP_handlepacket(data, length, ip_port);
friendreq_handlepacket(data, length, ip_port);
LANdiscovery_handlepacket(data, length, ip_port);
#endif
}
doDHT(); doDHT();
doLossless_UDP(); doLossless_UDP();
doNetCrypto(); doNetCrypto();

View File

@ -101,7 +101,7 @@ static int request_received(uint8_t * client_id)
} }
int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) static int friendreq_handlepacket(IP_Port source, uint8_t * packet, uint32_t length)
{ {
if (packet[0] == 32) { if (packet[0] == 32) {
if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING || if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING ||
@ -129,3 +129,8 @@ int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
} }
return 1; return 1;
} }
void friendreq_init(void)
{
networking_registerhandler(32, &friendreq_handlepacket);
}

View File

@ -39,10 +39,8 @@ int send_friendrequest(uint8_t *public_key, uint8_t *data, uint32_t length);
function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t));
/* if we receive a packet we call this function so it can be handled. /* sets up friendreq packet handlers */
return 0 if packet is handled correctly. void friendreq_init(void);
return 1 if it didn't handle the packet or if the packet was shit. */
int friendreq_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -71,7 +71,7 @@ int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length)
the packet data into data the packet data into data
the packet length into length. the packet length into length.
dump all empty packets. */ dump all empty packets. */
int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length) static int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
{ {
ADDR addr; ADDR addr;
#ifdef WIN32 #ifdef WIN32
@ -88,6 +88,27 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
return 0; return 0;
} }
static packet_handler_callback packethandlers[256] = {0};
void networking_registerhandler(uint8_t byte, packet_handler_callback cb)
{
packethandlers[byte] = cb;
}
void networking_poll()
{
IP_Port ip_port;
uint8_t data[MAX_UDP_PACKET_SIZE];
uint32_t length;
while (receivepacket(&ip_port, data, &length) != -1)
{
if (length < 1) continue;
if (!packethandlers[data[0]]) continue;
packethandlers[data[0]](ip_port, data, length);
}
}
/* initialize networking /* initialize networking
bind to ip and port bind to ip and port
ip must be in network order EX: 127.0.0.1 = (7F000001) ip must be in network order EX: 127.0.0.1 = (7F000001)
@ -149,7 +170,6 @@ int init_networking(IP ip, uint16_t port)
bind(sock, (struct sockaddr*)&addr, sizeof(addr)); bind(sock, (struct sockaddr*)&addr, sizeof(addr));
return 0; return 0;
} }
/* function to cleanup networking stuff */ /* function to cleanup networking stuff */

View File

@ -94,6 +94,11 @@ typedef struct {
#endif #endif
} ADDR; } ADDR;
/* Function to receive data, ip and port of sender is put into ip_port
the packet data into data
the packet length into length. */
typedef int (*packet_handler_callback)(IP_Port ip_port, uint8_t *data, uint32_t len);
/* returns current time in milleseconds since the epoch. */ /* returns current time in milleseconds since the epoch. */
uint64_t current_time(void); uint64_t current_time(void);
@ -106,10 +111,11 @@ uint32_t random_int(void);
/* Function to send packet(data) of length length to ip_port */ /* Function to send packet(data) of length length to ip_port */
int sendpacket(IP_Port ip_port, uint8_t *data, uint32_t length); int sendpacket(IP_Port ip_port, uint8_t *data, uint32_t length);
/* Function to receive data, ip and port of sender is put into ip_port /* Function to call when packet beginning with byte is received */
the packet data into data void networking_registerhandler(uint8_t byte, packet_handler_callback cb);
the packet length into length. */
int receivepacket(IP_Port *ip_port, uint8_t *data, uint32_t *length); /* call this several times a second */
void networking_poll();
/* initialize networking /* initialize networking
bind to ip and port bind to ip and port

View File

@ -163,7 +163,7 @@ int send_ping_response(IP_Port ipp, clientid_t* client_id, uint64_t ping_id)
return sendpacket(ipp, (uint8_t*) &pk, sizeof(pk)); return sendpacket(ipp, (uint8_t*) &pk, sizeof(pk));
} }
int handle_ping_request(uint8_t* packet, uint32_t length, IP_Port source) int handle_ping_request(IP_Port source, uint8_t* packet, uint32_t length)
{ {
pingreq_t* p = (pingreq_t*) packet; pingreq_t* p = (pingreq_t*) packet;
int rc; int rc;
@ -190,7 +190,7 @@ int handle_ping_request(uint8_t* packet, uint32_t length, IP_Port source)
return 0; return 0;
} }
int handle_ping_response(uint8_t* packet, uint32_t length, IP_Port source) int handle_ping_response(IP_Port source, uint8_t* packet, uint32_t length)
{ {
pingres_t* p = (pingres_t*) packet; pingres_t* p = (pingres_t*) packet;
int rc; int rc;

View File

@ -12,5 +12,5 @@ uint64_t add_ping(IP_Port ipp);
bool is_pinging(IP_Port ipp, uint64_t ping_id); bool is_pinging(IP_Port ipp, uint64_t ping_id);
int send_ping_request(IP_Port ipp, clientid_t* client_id); int send_ping_request(IP_Port ipp, clientid_t* client_id);
int send_ping_response(IP_Port ipp, clientid_t* client_id, uint64_t ping_id); int send_ping_response(IP_Port ipp, clientid_t* client_id, uint64_t ping_id);
int handle_ping_request(uint8_t* packet, uint32_t length, IP_Port source); int handle_ping_request(IP_Port source, uint8_t* packet, uint32_t length);
int handle_ping_response(uint8_t* packet, uint32_t length, IP_Port source); int handle_ping_response(IP_Port source, uint8_t* packet, uint32_t length);

View File

@ -113,9 +113,8 @@ int main(int argc, char *argv[])
free(bootstrap_key); free(bootstrap_key);
} }
IP_Port ip_port; DHT_init();
uint8_t data[MAX_UDP_PACKET_SIZE]; friendreq_init();
uint32_t length;
int is_waiting_for_dht_connection = 1; int is_waiting_for_dht_connection = 1;
while(1) while(1)
@ -127,11 +126,8 @@ int main(int argc, char *argv[])
} }
doDHT(); doDHT();
while(receivepacket(&ip_port, data, &length) != -1) networking_poll();
{
DHT_handlepacket(data, length, ip_port);
friendreq_handlepacket(data, length, ip_port);
}
c_sleep(1); c_sleep(1);
} }
shutdown_networking(); shutdown_networking();

View File

@ -81,10 +81,6 @@ int connect_to_servers(struct server_info_s *info)
int i; int i;
int c; int c;
IP_Port ip_port;
uint8_t data[MAX_UDP_PACKET_SIZE];
uint32_t length;
for(i = 0; i < 32; ++i) { for(i = 0; i < 32; ++i) {
if(info[i].valid) { if(info[i].valid) {
/* Actual bootstrapping code goes here */ /* Actual bootstrapping code goes here */
@ -109,10 +105,7 @@ int connect_to_servers(struct server_info_s *info)
doDHT(); doDHT();
while(receivepacket(&ip_port, data, &length) != -1) networking_poll();
{
DHT_handlepacket(data, length, ip_port);
}
} }
/* This probably never happens */ /* This probably never happens */
@ -337,6 +330,7 @@ int main(int argc, char *argv[]) {
/* Bootstrap the DHT /* Bootstrap the DHT
This one throws odd errors, too. Ignore. I assume they come This one throws odd errors, too. Ignore. I assume they come
from somewhere in the core. */ from somewhere in the core. */
DHT_init();
tmperr = errno; tmperr = errno;
connect_to_servers(server_conf.info); connect_to_servers(server_conf.info);
errno = tmperr; errno = tmperr;
@ -400,19 +394,13 @@ int main(int argc, char *argv[]) {
close(STDERR_FILENO); close(STDERR_FILENO);
/* Main loop */ /* Main loop */
IP_Port ip_port; friendreq_init();
uint8_t data[MAX_UDP_PACKET_SIZE];
uint32_t length;
while(1) while(1)
{ {
doDHT(); doDHT();
while(receivepacket(&ip_port, data, &length) != -1) networking_poll();
{
DHT_handlepacket(data, length, ip_port);
friendreq_handlepacket(data, length, ip_port);
}
usleep(10000); usleep(10000);
} }

View File

@ -156,14 +156,20 @@ int main(int argc, char *argv[])
bootstrap_ip_port.ip.i = inet_addr(argv[1]); bootstrap_ip_port.ip.i = inet_addr(argv[1]);
DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3]));
/*
IP_Port ip_port; IP_Port ip_port;
uint8_t data[MAX_UDP_PACKET_SIZE]; uint8_t data[MAX_UDP_PACKET_SIZE];
uint32_t length; uint32_t length;
*/
DHT_init();
friendreq_init();
while(1) { while(1) {
doDHT(); doDHT();
/* slvrTODO:
while(receivepacket(&ip_port, data, &length) != -1) { while(receivepacket(&ip_port, data, &length) != -1) {
if(DHT_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port)) { if(DHT_handlepacket(data, length, ip_port) && friendreq_handlepacket(data, length, ip_port)) {
//unhandled packet //unhandled packet
@ -172,6 +178,9 @@ int main(int argc, char *argv[])
printf("Received handled packet with length: %u\n", length); printf("Received handled packet with length: %u\n", length);
} }
} }
*/
networking_poll();
print_clientlist(); print_clientlist();
print_friendlist(); print_friendlist();
c_sleep(300); c_sleep(300);

View File

@ -120,20 +120,23 @@ void printconnection(int connection_id)
/*run doLossless_UDP(); */ /*run doLossless_UDP(); */
void Lossless_UDP() void Lossless_UDP()
{ {
IP_Port ip_port; /* IP_Port ip_port;
uint8_t data[MAX_UDP_PACKET_SIZE]; uint8_t data[MAX_UDP_PACKET_SIZE];
uint32_t length; uint32_t length;
while (receivepacket(&ip_port, data, &length) != -1) { while (receivepacket(&ip_port, data, &length) != -1) {
printf("packet with length: %u\n", length); printf("packet with length: %u\n", length); */
/* if(rand() % 3 != 1)//add packet loss /* if(rand() % 3 != 1)//add packet loss
{ */ { */
/*
if (LosslessUDP_handlepacket(data, length, ip_port)) if (LosslessUDP_handlepacket(data, length, ip_port))
printpacket(data, length, ip_port); printpacket(data, length, ip_port);
else else
printf("Received handled packet with length: %u\n", length); //printconnection(0); printf("Received handled packet with length: %u\n", length); //printconnection(0); */
/* } */ /* } */
} /* }*/
networking_poll();
doLossless_UDP(); doLossless_UDP();
@ -181,6 +184,7 @@ int main(int argc, char *argv[])
} }
timer = current_time(); timer = current_time();
LosslessUDP_init();
/*read first part of file */ /*read first part of file */
read = fread(buffer, 1, 512, file); read = fread(buffer, 1, 512, file);

View File

@ -117,20 +117,22 @@ void printconnection(int connection_id)
* run doLossless_UDP(); */ * run doLossless_UDP(); */
void Lossless_UDP() void Lossless_UDP()
{ {
IP_Port ip_port; // IP_Port ip_port;
uint8_t data[MAX_UDP_PACKET_SIZE]; // uint8_t data[MAX_UDP_PACKET_SIZE];
uint32_t length; // uint32_t length;
while (receivepacket(&ip_port, data, &length) != -1) { // while (receivepacket(&ip_port, data, &length) != -1) {
//if(rand() % 3 != 1)//add packet loss //if(rand() % 3 != 1)//add packet loss
//{ //{
if (LosslessUDP_handlepacket(data, length, ip_port)) { // if (LosslessUDP_handlepacket(data, length, ip_port)) {
printpacket(data, length, ip_port); // printpacket(data, length, ip_port);
} else { // } else {
//printconnection(0); //printconnection(0);
printf("Received handled packet with length: %u\n", length); // printf("Received handled packet with length: %u\n", length);
} // }
//} //}
} // }
networking_poll();
doLossless_UDP(); doLossless_UDP();
} }
@ -161,6 +163,7 @@ int main(int argc, char *argv[])
int connection; int connection;
uint64_t timer = current_time(); uint64_t timer = current_time();
LosslessUDP_init();
while (1) { while (1) {
Lossless_UDP(); Lossless_UDP();