From 6052b1f119d2b1494a7f2691f4dfed235b560636 Mon Sep 17 00:00:00 2001 From: slvr Date: Sat, 10 Aug 2013 00:30:18 +0100 Subject: [PATCH 1/8] 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 +++++++++++++++++++++++++++++++ other/DHT_bootstrap.c | 12 +++---- testing/DHT_test.c | 11 +++++- testing/Lossless_UDP_testclient.c | 12 ++++--- testing/Lossless_UDP_testserver.c | 23 ++++++------ 18 files changed, 178 insertions(+), 129 deletions(-) create mode 100644 core/substrate.h 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 + diff --git a/other/DHT_bootstrap.c b/other/DHT_bootstrap.c index befe2225..4b0adeff 100644 --- a/other/DHT_bootstrap.c +++ b/other/DHT_bootstrap.c @@ -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(); diff --git a/testing/DHT_test.c b/testing/DHT_test.c index c8feaf4b..350093fd 100644 --- a/testing/DHT_test.c +++ b/testing/DHT_test.c @@ -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; } diff --git a/testing/Lossless_UDP_testclient.c b/testing/Lossless_UDP_testclient.c index 78aff1a3..bb287f70 100644 --- a/testing/Lossless_UDP_testclient.c +++ b/testing/Lossless_UDP_testclient.c @@ -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); diff --git a/testing/Lossless_UDP_testserver.c b/testing/Lossless_UDP_testserver.c index f4b144b4..dee9e7b1 100644 --- a/testing/Lossless_UDP_testserver.c +++ b/testing/Lossless_UDP_testserver.c @@ -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(); From 448d0124cdb98142a0fa89087a6b49b0c6063700 Mon Sep 17 00:00:00 2001 From: slvr Date: Sat, 10 Aug 2013 00:49:20 +0100 Subject: [PATCH 2/8] networking_poll: DHT_bootstrap_daemon --- .../DHT_bootstrap_daemon.c | 20 ++++--------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c b/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c index 48152744..6ce542c1 100644 --- a/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c +++ b/other/bootstrap_serverdaemon/DHT_bootstrap_daemon.c @@ -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); } From 951dbc1258b094e307957b4fd423e81c9a4778ee Mon Sep 17 00:00:00 2001 From: slvr Date: Sat, 10 Aug 2013 00:54:43 +0100 Subject: [PATCH 3/8] Updated initMessenger --- core/Messenger.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/core/Messenger.c b/core/Messenger.c index 082b10d2..a7d21e1a 100644 --- a/core/Messenger.c +++ b/core/Messenger.c @@ -517,6 +517,11 @@ int initMessenger(void) if(init_networking(ip,PORT) == -1) return -1; + DHT_init(); + LosslessUDP_init(); + friendreq_init(); + LANdiscovery_init(); + return 0; } @@ -684,7 +689,7 @@ static void LANdiscovery(void) void doMessenger(void) { networking_poll(); - + doDHT(); doLossless_UDP(); doNetCrypto(); @@ -693,14 +698,6 @@ void doMessenger(void) LANdiscovery(); } -void Messenger_init(void) -{ - DHT_init(); - LosslessUDP_init(); - friendreq_init(); - LANdiscovery_init(); -} - /* returns the size of the messenger data (for saving) */ uint32_t Messenger_size(void) { From 75c2dd4cb9ffe2c5413bcdd2c64e7737a4bc4e42 Mon Sep 17 00:00:00 2001 From: slvr Date: Sat, 10 Aug 2013 01:01:57 +0100 Subject: [PATCH 4/8] fixed typo: handle_ping_request -> handle_ping_response --- core/DHT.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/DHT.c b/core/DHT.c index 2c47d2f1..85c86289 100644 --- a/core/DHT.c +++ b/core/DHT.c @@ -1077,7 +1077,7 @@ static void doNAT(void) void DHT_init(void) { networking_registerhandler(0, &handle_ping_request); - networking_registerhandler(1, &handle_ping_request); + networking_registerhandler(1, &handle_ping_response); networking_registerhandler(2, &handle_getnodes); networking_registerhandler(3, &handle_sendnodes); networking_registerhandler(254, &handle_NATping); From da01bafc83045faf48d4d4f16b2ccc13a0af68c7 Mon Sep 17 00:00:00 2001 From: slvr Date: Sat, 10 Aug 2013 01:08:18 +0100 Subject: [PATCH 5/8] core/substrate.h wasn't supposed to be here --- core/substrate.h | 58 ------------------------------------------------ 1 file changed, 58 deletions(-) delete mode 100644 core/substrate.h diff --git a/core/substrate.h b/core/substrate.h deleted file mode 100644 index f6cab2a6..00000000 --- a/core/substrate.h +++ /dev/null @@ -1,58 +0,0 @@ -/* 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 - From 8f6f8e221d5a86c811f7fb7978d724bd12a9bf6a Mon Sep 17 00:00:00 2001 From: slvr Date: Sat, 10 Aug 2013 03:02:00 +0100 Subject: [PATCH 6/8] networking_poll: receivepacket(&ip_port, data, &length) == -1 --- core/network.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/network.c b/core/network.c index 4dd6257a..89697e76 100644 --- a/core/network.c +++ b/core/network.c @@ -101,7 +101,7 @@ void networking_poll() uint8_t data[MAX_UDP_PACKET_SIZE]; uint32_t length; - while (receivepacket(&ip_port, data, &length)) + while (receivepacket(&ip_port, data, &length) != -1) { if (length < 1) continue; if (!packethandlers[data[0]]) continue; From 7f27fcaf37144354bca31109f71d2db5af9d4582 Mon Sep 17 00:00:00 2001 From: slvr Date: Sat, 10 Aug 2013 11:18:25 +0100 Subject: [PATCH 7/8] tabs -> spaces --- core/DHT.c | 10 +++++----- core/LAN_discovery.c | 2 +- core/Lossless_UDP.c | 6 +++--- core/Messenger.c | 22 +++++++++++----------- core/friend_requests.c | 2 +- core/network.c | 22 +++++++++++----------- testing/Lossless_UDP_testclient.c | 2 +- testing/Lossless_UDP_testserver.c | 2 +- 8 files changed, 34 insertions(+), 34 deletions(-) diff --git a/core/DHT.c b/core/DHT.c index 85c86289..d359076d 100644 --- a/core/DHT.c +++ b/core/DHT.c @@ -1076,11 +1076,11 @@ static void doNAT(void) void DHT_init(void) { - 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); + 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) diff --git a/core/LAN_discovery.c b/core/LAN_discovery.c index 43eb7d42..67dc656b 100644 --- a/core/LAN_discovery.c +++ b/core/LAN_discovery.c @@ -134,5 +134,5 @@ int send_LANdiscovery(uint16_t port) void LANdiscovery_init(void) { - networking_registerhandler(33, &handle_LANdiscovery); + networking_registerhandler(33, &handle_LANdiscovery); } diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c index d72a2c63..002e2cf8 100644 --- a/core/Lossless_UDP.c +++ b/core/Lossless_UDP.c @@ -772,9 +772,9 @@ static int handle_data(IP_Port source, uint8_t *packet, uint32_t length) void LosslessUDP_init(void) { - networking_registerhandler(16, &handle_handshake); - networking_registerhandler(17, &handle_SYNC); - networking_registerhandler(18, &handle_data); + networking_registerhandler(16, &handle_handshake); + networking_registerhandler(17, &handle_SYNC); + networking_registerhandler(18, &handle_data); } /* diff --git a/core/Messenger.c b/core/Messenger.c index a7d21e1a..bc18c86a 100644 --- a/core/Messenger.c +++ b/core/Messenger.c @@ -517,10 +517,10 @@ int initMessenger(void) if(init_networking(ip,PORT) == -1) return -1; - DHT_init(); - LosslessUDP_init(); - friendreq_init(); - LANdiscovery_init(); + DHT_init(); + LosslessUDP_init(); + friendreq_init(); + LANdiscovery_init(); return 0; } @@ -688,14 +688,14 @@ static void LANdiscovery(void) /* the main loop that needs to be run at least 200 times per second. */ void doMessenger(void) { - networking_poll(); + networking_poll(); - doDHT(); - doLossless_UDP(); - doNetCrypto(); - doInbound(); - doFriends(); - LANdiscovery(); + doDHT(); + doLossless_UDP(); + doNetCrypto(); + doInbound(); + doFriends(); + LANdiscovery(); } /* returns the size of the messenger data (for saving) */ diff --git a/core/friend_requests.c b/core/friend_requests.c index 013232ee..422cc028 100644 --- a/core/friend_requests.c +++ b/core/friend_requests.c @@ -132,5 +132,5 @@ static int friendreq_handlepacket(IP_Port source, uint8_t * packet, uint32_t len void friendreq_init(void) { - networking_registerhandler(32, &friendreq_handlepacket); + networking_registerhandler(32, &friendreq_handlepacket); } diff --git a/core/network.c b/core/network.c index 89697e76..7e3b344a 100644 --- a/core/network.c +++ b/core/network.c @@ -92,21 +92,21 @@ static packet_handler_callback packethandlers[256] = {0}; void networking_registerhandler(uint8_t byte, packet_handler_callback cb) { - packethandlers[byte] = 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); - } + 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 diff --git a/testing/Lossless_UDP_testclient.c b/testing/Lossless_UDP_testclient.c index bb287f70..7efafa4f 100644 --- a/testing/Lossless_UDP_testclient.c +++ b/testing/Lossless_UDP_testclient.c @@ -136,7 +136,7 @@ void Lossless_UDP() /* } */ /* }*/ - networking_poll(); + networking_poll(); doLossless_UDP(); diff --git a/testing/Lossless_UDP_testserver.c b/testing/Lossless_UDP_testserver.c index dee9e7b1..bc4fed7a 100644 --- a/testing/Lossless_UDP_testserver.c +++ b/testing/Lossless_UDP_testserver.c @@ -132,7 +132,7 @@ void Lossless_UDP() //} // } - networking_poll(); + networking_poll(); doLossless_UDP(); } From 5b6465528f88c4ed45fe57d5c2c21270f9109bb9 Mon Sep 17 00:00:00 2001 From: slvr Date: Sat, 10 Aug 2013 19:55:54 +0100 Subject: [PATCH 8/8] handle_ping_response function signature correction --- core/ping.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/ping.c b/core/ping.c index eac71032..24ece06a 100644 --- a/core/ping.c +++ b/core/ping.c @@ -190,7 +190,7 @@ int handle_ping_request(IP_Port source, uint8_t* packet, uint32_t length) 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;