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);
}
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;
@ -586,7 +586,7 @@ static int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source)
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;
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 */
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
|| length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
@ -1074,30 +1074,13 @@ static void doNAT(void)
/*----------------------------------------------------------------------------------*/
/*-----------------------END OF NAT PUNCHING FUNCTIONS------------------------------*/
int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source)
void DHT_init(void)
{
switch (packet[0]) {
case 0:
return handle_ping_request(packet, length, source);
case 1:
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;
networking_registerhandler(0, &handle_ping_request);
networking_registerhandler(1, &handle_ping_response);
networking_registerhandler(2, &handle_getnodes);
networking_registerhandler(3, &handle_sendnodes);
networking_registerhandler(254, &handle_NATping);
}
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) */
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
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);
@ -93,6 +88,9 @@ uint32_t DHT_size(void);
/* save the DHT in data where data is an array of size DHT_size() */
void DHT_save(uint8_t *data);
/* init DHT */
void DHT_init(void);
/* load the DHT from data of size size;
return -1 if failure
return 0 if success */

View File

@ -111,7 +111,7 @@ static int LAN_ip(IP ip)
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)
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)
{
uint8_t data[crypto_box_PUBLICKEYBYTES + 1];
data[0] = 32;
data[0] = 33;
memcpy(data + 1, self_public_key, crypto_box_PUBLICKEYBYTES);
IP_Port ip_port = {broadcast_ip(), port};
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)
return handle_LANdiscovery(packet, length, source);
return 1;
networking_registerhandler(33, &handle_LANdiscovery);
}

View File

@ -43,10 +43,8 @@ extern "C" {
int send_LANdiscovery(uint16_t port);
/* if we receive a 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 LANdiscovery_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
/* sets up packet handlers */
void LANdiscovery_init(void);

View File

@ -555,7 +555,7 @@ static int send_DATA(uint32_t connection_id)
/* 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))
return 1;
@ -669,7 +669,7 @@ static int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packet
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))
@ -742,7 +742,7 @@ static int add_recv(int connection_id, uint32_t data_num, uint8_t *data, uint16_
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);
@ -770,23 +770,11 @@ static int handle_data(uint8_t *packet, uint32_t length, IP_Port source)
* END of packet handling functions
*/
int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source)
void LosslessUDP_init(void)
{
switch (packet[0]) {
case 16:
return handle_handshake(packet, length, source);
case 17:
return handle_SYNC(packet, length, source);
case 18:
return handle_data(packet, length, source);
default:
return 1;
}
return 0;
networking_registerhandler(16, &handle_handshake);
networking_registerhandler(17, &handle_SYNC);
networking_registerhandler(18, &handle_data);
}
/*

View File

@ -113,11 +113,9 @@ int is_connected(int connection_id);
void doLossless_UDP(void);
/*
* If we receive a Lossless_UDP packet, 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.
* This function sets up LosslessUDP packet handling.
*/
int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
void LosslessUDP_init(void);
#ifdef __cplusplus
}

View File

@ -518,6 +518,11 @@ int initMessenger(void)
if(init_networking(ip,PORT) == -1)
return -1;
DHT_init();
LosslessUDP_init();
friendreq_init();
LANdiscovery_init();
return 0;
}
@ -684,29 +689,8 @@ static void LANdiscovery(void)
/* the main loop that needs to be run at least 200 times per second. */
void doMessenger(void)
{
IP_Port ip_port;
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
}
networking_poll();
doDHT();
doLossless_UDP();
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 (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;
}
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) */
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.
return 0 if packet is handled correctly.
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);
/* sets up friendreq packet handlers */
void friendreq_init(void);
#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 length into length.
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;
#ifdef WIN32
@ -88,6 +88,27 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
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
bind to ip and port
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));
return 0;
}
/* function to cleanup networking stuff */

View File

@ -94,6 +94,11 @@ typedef struct {
#endif
} 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. */
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 */
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
the packet data into data
the packet length into length. */
int receivepacket(IP_Port *ip_port, uint8_t *data, uint32_t *length);
/* Function to call when packet beginning with byte is received */
void networking_registerhandler(uint8_t byte, packet_handler_callback cb);
/* call this several times a second */
void networking_poll();
/* initialize networking
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));
}
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;
int rc;
@ -190,7 +190,7 @@ int handle_ping_request(uint8_t* packet, uint32_t length, IP_Port source)
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;
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);
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 handle_ping_request(uint8_t* packet, uint32_t length, IP_Port source);
int handle_ping_response(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(IP_Port source, uint8_t* packet, uint32_t length);

View File

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

View File

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

View File

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

View File

@ -120,20 +120,23 @@ void printconnection(int connection_id)
/*run doLossless_UDP(); */
void Lossless_UDP()
{
IP_Port ip_port;
/* IP_Port ip_port;
uint8_t data[MAX_UDP_PACKET_SIZE];
uint32_t length;
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 (LosslessUDP_handlepacket(data, length, ip_port))
printpacket(data, length, ip_port);
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();
@ -181,6 +184,7 @@ int main(int argc, char *argv[])
}
timer = current_time();
LosslessUDP_init();
/*read first part of file */
read = fread(buffer, 1, 512, file);

View File

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