network_registerhandler

This commit is contained in:
slvr 2013-08-10 00:30:18 +01:00
parent 1ec95162a0
commit 6052b1f119
18 changed files with 178 additions and 129 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_request);
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

@ -683,29 +683,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();
@ -714,6 +693,14 @@ void doMessenger(void)
LANdiscovery(); LANdiscovery();
} }
void Messenger_init(void)
{
DHT_init();
LosslessUDP_init();
friendreq_init();
LANdiscovery_init();
}
/* returns the size of the messenger data (for saving) */ /* returns the size of the messenger data (for saving) */
uint32_t Messenger_size(void) uint32_t Messenger_size(void)
{ {

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))
{
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;

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);

58
core/substrate.h Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*
*/
#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

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

@ -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();