From 6052b1f119d2b1494a7f2691f4dfed235b560636 Mon Sep 17 00:00:00 2001 From: slvr Date: Sat, 10 Aug 2013 00:30:18 +0100 Subject: network_registerhandler --- core/DHT.c | 35 ++++++++---------------------- core/DHT.h | 8 +++---- core/LAN_discovery.c | 10 ++++----- core/LAN_discovery.h | 6 ++---- core/Lossless_UDP.c | 26 ++++++---------------- core/Lossless_UDP.h | 6 ++---- core/Messenger.c | 43 +++++++++++++------------------------ core/friend_requests.c | 7 +++++- core/friend_requests.h | 6 ++---- core/network.c | 24 +++++++++++++++++++-- core/network.h | 14 ++++++++---- core/ping.c | 2 +- core/ping.h | 4 ++-- core/substrate.h | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 14 files changed, 143 insertions(+), 106 deletions(-) create mode 100644 core/substrate.h (limited to 'core') diff --git a/core/DHT.c b/core/DHT.c index 7e3af9d2..2c47d2f1 100644 --- a/core/DHT.c +++ b/core/DHT.c @@ -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_request); + networking_registerhandler(2, &handle_getnodes); + networking_registerhandler(3, &handle_sendnodes); + networking_registerhandler(254, &handle_NATping); } void doDHT(void) diff --git a/core/DHT.h b/core/DHT.h index cb5697ea..0e05f132 100644 --- a/core/DHT.h +++ b/core/DHT.h @@ -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 */ diff --git a/core/LAN_discovery.c b/core/LAN_discovery.c index 26b3930c..43eb7d42 100644 --- a/core/LAN_discovery.c +++ b/core/LAN_discovery.c @@ -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); } diff --git a/core/LAN_discovery.h b/core/LAN_discovery.h index 96a6e6ad..6b5b8c75 100644 --- a/core/LAN_discovery.h +++ b/core/LAN_discovery.h @@ -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); diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c index 3a289735..d72a2c63 100644 --- a/core/Lossless_UDP.c +++ b/core/Lossless_UDP.c @@ -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); } /* diff --git a/core/Lossless_UDP.h b/core/Lossless_UDP.h index b4cb186a..53afe606 100644 --- a/core/Lossless_UDP.h +++ b/core/Lossless_UDP.h @@ -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 } diff --git a/core/Messenger.c b/core/Messenger.c index 5f33886f..082b10d2 100644 --- a/core/Messenger.c +++ b/core/Messenger.c @@ -683,35 +683,22 @@ 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(); - doInbound(); - doFriends(); - LANdiscovery(); + doDHT(); + doLossless_UDP(); + doNetCrypto(); + doInbound(); + doFriends(); + LANdiscovery(); +} + +void Messenger_init(void) +{ + DHT_init(); + LosslessUDP_init(); + friendreq_init(); + LANdiscovery_init(); } /* returns the size of the messenger data (for saving) */ diff --git a/core/friend_requests.c b/core/friend_requests.c index b8dab87e..013232ee 100644 --- a/core/friend_requests.c +++ b/core/friend_requests.c @@ -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); +} diff --git a/core/friend_requests.h b/core/friend_requests.h index 4dfc5130..708d8f66 100644 --- a/core/friend_requests.h +++ b/core/friend_requests.h @@ -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 } diff --git a/core/network.c b/core/network.c index 8c6fa7b6..4dd6257a 100644 --- a/core/network.c +++ b/core/network.c @@ -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)) + { + 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 */ diff --git a/core/network.h b/core/network.h index d3c39333..7c95d84d 100644 --- a/core/network.h +++ b/core/network.h @@ -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 diff --git a/core/ping.c b/core/ping.c index 6a1fbb7e..eac71032 100644 --- a/core/ping.c +++ b/core/ping.c @@ -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; diff --git a/core/ping.h b/core/ping.h index 2cab7d59..1ccfabac 100644 --- a/core/ping.h +++ b/core/ping.h @@ -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); diff --git a/core/substrate.h b/core/substrate.h new file mode 100644 index 00000000..f6cab2a6 --- /dev/null +++ b/core/substrate.h @@ -0,0 +1,58 @@ +/* substrate.h + * The communications hub + * See also: http://http://wiki.tox.im/index.php/Proposal:Slvr_Protocol_Rewrite + * + * Copyright (C) 2013 Tox project All Rights Reserved. + * + * This file is part of Tox. + * + * Tox is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tox is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tox. If not, see . + * + */ + +#include "network.h" + +#ifdef __cplusplus +//extern "C" { +#endif + +/* Type Definitions */ + +typedef uint8_t channel_t; + +typedef struct { + byte a[32]; +} address_t; + +typedef struct { + +} connection_t; + +typedef void(*channel_recv_callback_t)(connection_t*, byte*, size_t, uint64_t); + +typedef void(*on_connection_callback_t)(connection_t*); + +/* Globals */ + +extern address_t self_public_key; + +/* Functions */ + +void substrate_init(byte* keydata); + + +#ifdef __cplusplus +//} +#endif + -- cgit v1.2.3