summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2013-08-10 14:21:06 -0700
committerirungentoo <irungentoo@gmail.com>2013-08-10 14:21:06 -0700
commit6c2b2806d10881a4c44a47e877898e9de1cc63c9 (patch)
tree2879741f06260a5390ae619954f53f141ba4ce54 /core
parent341de59bb627600c27ef29b113fa71bac120350f (diff)
parent5b6465528f88c4ed45fe57d5c2c21270f9109bb9 (diff)
Merge pull request #415 from slvr/pr
Beginning to centralise packet handling
Diffstat (limited to 'core')
-rw-r--r--core/DHT.c35
-rw-r--r--core/DHT.h8
-rw-r--r--core/LAN_discovery.c10
-rw-r--r--core/LAN_discovery.h6
-rw-r--r--core/Lossless_UDP.c26
-rw-r--r--core/Lossless_UDP.h6
-rw-r--r--core/Messenger.c30
-rw-r--r--core/friend_requests.c7
-rw-r--r--core/friend_requests.h6
-rw-r--r--core/network.c24
-rw-r--r--core/network.h14
-rw-r--r--core/ping.c4
-rw-r--r--core/ping.h4
13 files changed, 78 insertions, 102 deletions
diff --git a/core/DHT.c b/core/DHT.c
index 7e3af9d2..d359076d 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,
554 return sendpacket(ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len); 554 return sendpacket(ip_port, data, 1 + CLIENT_ID_SIZE + crypto_box_NONCEBYTES + len);
555} 555}
556 556
557static int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source) 557static int handle_getnodes(IP_Port source, uint8_t * packet, uint32_t length)
558{ 558{
559 uint64_t ping_id; 559 uint64_t ping_id;
560 560
@@ -586,7 +586,7 @@ static int handle_getnodes(uint8_t * packet, uint32_t length, IP_Port source)
586 return 0; 586 return 0;
587} 587}
588 588
589static int handle_sendnodes(uint8_t * packet, uint32_t length, IP_Port source) 589static int handle_sendnodes(IP_Port source, uint8_t * packet, uint32_t length)
590{ 590{
591 uint64_t ping_id; 591 uint64_t ping_id;
592 uint32_t cid_size = 1 + CLIENT_ID_SIZE; 592 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)
930} 930}
931 931
932/* Handle a received ping request for */ 932/* Handle a received ping request for */
933static int handle_NATping(uint8_t * packet, uint32_t length, IP_Port source) 933static int handle_NATping(IP_Port source, uint8_t * packet, uint32_t length)
934{ 934{
935 if (length < crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING 935 if (length < crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING
936 || length > MAX_DATA_SIZE + ENCRYPTION_PADDING) 936 || length > MAX_DATA_SIZE + ENCRYPTION_PADDING)
@@ -1074,30 +1074,13 @@ static void doNAT(void)
1074/*----------------------------------------------------------------------------------*/ 1074/*----------------------------------------------------------------------------------*/
1075/*-----------------------END OF NAT PUNCHING FUNCTIONS------------------------------*/ 1075/*-----------------------END OF NAT PUNCHING FUNCTIONS------------------------------*/
1076 1076
1077int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) 1077void DHT_init(void)
1078{ 1078{
1079 switch (packet[0]) { 1079 networking_registerhandler(0, &handle_ping_request);
1080 case 0: 1080 networking_registerhandler(1, &handle_ping_response);
1081 return handle_ping_request(packet, length, source); 1081 networking_registerhandler(2, &handle_getnodes);
1082 1082 networking_registerhandler(3, &handle_sendnodes);
1083 case 1: 1083 networking_registerhandler(254, &handle_NATping);
1084 return handle_ping_response(packet, length, source);
1085
1086 case 2:
1087 return handle_getnodes(packet, length, source);
1088
1089 case 3:
1090 return handle_sendnodes(packet, length, source);
1091
1092 case 254:
1093 return handle_NATping(packet, length, source);
1094
1095 default:
1096 return 1;
1097
1098 }
1099
1100 return 0;
1101} 1084}
1102 1085
1103void doDHT(void) 1086void 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);
58/* Run this function at least a couple times per second (It's the main loop) */ 58/* Run this function at least a couple times per second (It's the main loop) */
59void doDHT(void); 59void doDHT(void);
60 60
61/* if we receive a DHT packet we call this function so it can be handled.
62 return 0 if packet is handled correctly.
63 return 1 if it didn't handle the packet or if the packet was shit. */
64int DHT_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
65
66/* Use this function to bootstrap the client 61/* Use this function to bootstrap the client
67 Sends a get nodes request to the given node with ip port and public_key */ 62 Sends a get nodes request to the given node with ip port and public_key */
68void DHT_bootstrap(IP_Port ip_port, uint8_t *public_key); 63void DHT_bootstrap(IP_Port ip_port, uint8_t *public_key);
@@ -93,6 +88,9 @@ uint32_t DHT_size(void);
93/* save the DHT in data where data is an array of size DHT_size() */ 88/* save the DHT in data where data is an array of size DHT_size() */
94void DHT_save(uint8_t *data); 89void DHT_save(uint8_t *data);
95 90
91/* init DHT */
92void DHT_init(void);
93
96/* load the DHT from data of size size; 94/* load the DHT from data of size size;
97 return -1 if failure 95 return -1 if failure
98 return 0 if success */ 96 return 0 if success */
diff --git a/core/LAN_discovery.c b/core/LAN_discovery.c
index 26b3930c..67dc656b 100644
--- a/core/LAN_discovery.c
+++ b/core/LAN_discovery.c
@@ -111,7 +111,7 @@ static int LAN_ip(IP ip)
111 return -1; 111 return -1;
112} 112}
113 113
114static int handle_LANdiscovery(uint8_t *packet, uint32_t length, IP_Port source) 114static int handle_LANdiscovery(IP_Port source, uint8_t *packet, uint32_t length)
115{ 115{
116 if (LAN_ip(source.ip) == -1) 116 if (LAN_ip(source.ip) == -1)
117 return 1; 117 return 1;
@@ -125,16 +125,14 @@ static int handle_LANdiscovery(uint8_t *packet, uint32_t length, IP_Port source)
125int send_LANdiscovery(uint16_t port) 125int send_LANdiscovery(uint16_t port)
126{ 126{
127 uint8_t data[crypto_box_PUBLICKEYBYTES + 1]; 127 uint8_t data[crypto_box_PUBLICKEYBYTES + 1];
128 data[0] = 32; 128 data[0] = 33;
129 memcpy(data + 1, self_public_key, crypto_box_PUBLICKEYBYTES); 129 memcpy(data + 1, self_public_key, crypto_box_PUBLICKEYBYTES);
130 IP_Port ip_port = {broadcast_ip(), port}; 130 IP_Port ip_port = {broadcast_ip(), port};
131 return sendpacket(ip_port, data, 1 + crypto_box_PUBLICKEYBYTES); 131 return sendpacket(ip_port, data, 1 + crypto_box_PUBLICKEYBYTES);
132} 132}
133 133
134 134
135int LANdiscovery_handlepacket(uint8_t *packet, uint32_t length, IP_Port source) 135void LANdiscovery_init(void)
136{ 136{
137 if (packet[0] == 32) 137 networking_registerhandler(33, &handle_LANdiscovery);
138 return handle_LANdiscovery(packet, length, source);
139 return 1;
140} 138}
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" {
43int send_LANdiscovery(uint16_t port); 43int send_LANdiscovery(uint16_t port);
44 44
45 45
46/* if we receive a packet we call this function so it can be handled. 46/* sets up packet handlers */
47 return 0 if packet is handled correctly. 47void LANdiscovery_init(void);
48 return 1 if it didn't handle the packet or if the packet was shit. */
49int LANdiscovery_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
50 48
51 49
52 50
diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c
index 3a289735..002e2cf8 100644
--- a/core/Lossless_UDP.c
+++ b/core/Lossless_UDP.c
@@ -555,7 +555,7 @@ static int send_DATA(uint32_t connection_id)
555 555
556 556
557/* Return 0 if handled correctly, 1 if packet is bad. */ 557/* Return 0 if handled correctly, 1 if packet is bad. */
558static int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) 558static int handle_handshake(IP_Port source, uint8_t * packet, uint32_t length)
559{ 559{
560 if (length != (1 + 4 + 4)) 560 if (length != (1 + 4 + 4))
561 return 1; 561 return 1;
@@ -669,7 +669,7 @@ static int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packet
669 return 1; 669 return 1;
670} 670}
671 671
672static int handle_SYNC(uint8_t *packet, uint32_t length, IP_Port source) 672static int handle_SYNC(IP_Port source, uint8_t *packet, uint32_t length)
673{ 673{
674 674
675 if (!SYNC_valid(length)) 675 if (!SYNC_valid(length))
@@ -742,7 +742,7 @@ static int add_recv(int connection_id, uint32_t data_num, uint8_t *data, uint16_
742 return 0; 742 return 0;
743} 743}
744 744
745static int handle_data(uint8_t *packet, uint32_t length, IP_Port source) 745static int handle_data(IP_Port source, uint8_t *packet, uint32_t length)
746{ 746{
747 int connection = getconnection_id(source); 747 int connection = getconnection_id(source);
748 748
@@ -770,23 +770,11 @@ static int handle_data(uint8_t *packet, uint32_t length, IP_Port source)
770 * END of packet handling functions 770 * END of packet handling functions
771 */ 771 */
772 772
773int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source) 773void LosslessUDP_init(void)
774{ 774{
775 switch (packet[0]) { 775 networking_registerhandler(16, &handle_handshake);
776 case 16: 776 networking_registerhandler(17, &handle_SYNC);
777 return handle_handshake(packet, length, source); 777 networking_registerhandler(18, &handle_data);
778
779 case 17:
780 return handle_SYNC(packet, length, source);
781
782 case 18:
783 return handle_data(packet, length, source);
784
785 default:
786 return 1;
787 }
788
789 return 0;
790} 778}
791 779
792/* 780/*
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);
113void doLossless_UDP(void); 113void doLossless_UDP(void);
114 114
115/* 115/*
116 * If we receive a Lossless_UDP packet, call this function so it can be handled. 116 * This function sets up LosslessUDP packet handling.
117 * Return 0 if packet is handled correctly.
118 * Return 1 if it didn't handle the packet or if the packet was shit.
119 */ 117 */
120int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source); 118void LosslessUDP_init(void);
121 119
122#ifdef __cplusplus 120#ifdef __cplusplus
123} 121}
diff --git a/core/Messenger.c b/core/Messenger.c
index dd651107..37ab5e2d 100644
--- a/core/Messenger.c
+++ b/core/Messenger.c
@@ -518,6 +518,11 @@ int initMessenger(void)
518 if(init_networking(ip,PORT) == -1) 518 if(init_networking(ip,PORT) == -1)
519 return -1; 519 return -1;
520 520
521 DHT_init();
522 LosslessUDP_init();
523 friendreq_init();
524 LANdiscovery_init();
525
521 return 0; 526 return 0;
522} 527}
523 528
@@ -684,29 +689,8 @@ static void LANdiscovery(void)
684/* the main loop that needs to be run at least 200 times per second. */ 689/* the main loop that needs to be run at least 200 times per second. */
685void doMessenger(void) 690void doMessenger(void)
686{ 691{
687 IP_Port ip_port; 692 networking_poll();
688 uint8_t data[MAX_UDP_PACKET_SIZE]; 693
689 uint32_t length;
690 while (receivepacket(&ip_port, data, &length) != -1) {
691#ifdef DEBUG
692 /* if(rand() % 3 != 1) //simulate packet loss */
693 /* { */
694 if (DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port) &&
695 friendreq_handlepacket(data, length, ip_port) && LANdiscovery_handlepacket(data, length, ip_port))
696 /* if packet is discarded */
697 printf("Received unhandled packet with length: %u\n", length);
698 else
699 printf("Received handled packet with length: %u\n", length);
700 /* } */
701 printf("Status: %u %u %u\n",friendlist[0].status ,is_cryptoconnected(friendlist[0].crypt_connection_id), friendlist[0].crypt_connection_id);
702#else
703 DHT_handlepacket(data, length, ip_port);
704 LosslessUDP_handlepacket(data, length, ip_port);
705 friendreq_handlepacket(data, length, ip_port);
706 LANdiscovery_handlepacket(data, length, ip_port);
707#endif
708
709 }
710 doDHT(); 694 doDHT();
711 doLossless_UDP(); 695 doLossless_UDP();
712 doNetCrypto(); 696 doNetCrypto();
diff --git a/core/friend_requests.c b/core/friend_requests.c
index b8dab87e..422cc028 100644
--- a/core/friend_requests.c
+++ b/core/friend_requests.c
@@ -101,7 +101,7 @@ static int request_received(uint8_t * client_id)
101} 101}
102 102
103 103
104int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) 104static int friendreq_handlepacket(IP_Port source, uint8_t * packet, uint32_t length)
105{ 105{
106 if (packet[0] == 32) { 106 if (packet[0] == 32) {
107 if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING || 107 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)
129 } 129 }
130 return 1; 130 return 1;
131} 131}
132
133void friendreq_init(void)
134{
135 networking_registerhandler(32, &friendreq_handlepacket);
136}
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);
39 function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ 39 function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */
40void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); 40void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t));
41 41
42/* if we receive a packet we call this function so it can be handled. 42/* sets up friendreq packet handlers */
43 return 0 if packet is handled correctly. 43void friendreq_init(void);
44 return 1 if it didn't handle the packet or if the packet was shit. */
45int friendreq_handlepacket(uint8_t *packet, uint32_t length, IP_Port source);
46 44
47#ifdef __cplusplus 45#ifdef __cplusplus
48} 46}
diff --git a/core/network.c b/core/network.c
index 8c6fa7b6..7e3b344a 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)
71 the packet data into data 71 the packet data into data
72 the packet length into length. 72 the packet length into length.
73 dump all empty packets. */ 73 dump all empty packets. */
74int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length) 74static int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
75{ 75{
76 ADDR addr; 76 ADDR addr;
77#ifdef WIN32 77#ifdef WIN32
@@ -88,6 +88,27 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
88 return 0; 88 return 0;
89} 89}
90 90
91static packet_handler_callback packethandlers[256] = {0};
92
93void networking_registerhandler(uint8_t byte, packet_handler_callback cb)
94{
95 packethandlers[byte] = cb;
96}
97
98void networking_poll()
99{
100 IP_Port ip_port;
101 uint8_t data[MAX_UDP_PACKET_SIZE];
102 uint32_t length;
103
104 while (receivepacket(&ip_port, data, &length) != -1)
105 {
106 if (length < 1) continue;
107 if (!packethandlers[data[0]]) continue;
108 packethandlers[data[0]](ip_port, data, length);
109 }
110}
111
91/* initialize networking 112/* initialize networking
92 bind to ip and port 113 bind to ip and port
93 ip must be in network order EX: 127.0.0.1 = (7F000001) 114 ip must be in network order EX: 127.0.0.1 = (7F000001)
@@ -149,7 +170,6 @@ int init_networking(IP ip, uint16_t port)
149 bind(sock, (struct sockaddr*)&addr, sizeof(addr)); 170 bind(sock, (struct sockaddr*)&addr, sizeof(addr));
150 171
151 return 0; 172 return 0;
152
153} 173}
154 174
155/* function to cleanup networking stuff */ 175/* 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 {
94#endif 94#endif
95} ADDR; 95} ADDR;
96 96
97/* Function to receive data, ip and port of sender is put into ip_port
98 the packet data into data
99 the packet length into length. */
100typedef int (*packet_handler_callback)(IP_Port ip_port, uint8_t *data, uint32_t len);
101
97/* returns current time in milleseconds since the epoch. */ 102/* returns current time in milleseconds since the epoch. */
98uint64_t current_time(void); 103uint64_t current_time(void);
99 104
@@ -106,10 +111,11 @@ uint32_t random_int(void);
106/* Function to send packet(data) of length length to ip_port */ 111/* Function to send packet(data) of length length to ip_port */
107int sendpacket(IP_Port ip_port, uint8_t *data, uint32_t length); 112int sendpacket(IP_Port ip_port, uint8_t *data, uint32_t length);
108 113
109/* Function to receive data, ip and port of sender is put into ip_port 114/* Function to call when packet beginning with byte is received */
110 the packet data into data 115void networking_registerhandler(uint8_t byte, packet_handler_callback cb);
111 the packet length into length. */ 116
112int receivepacket(IP_Port *ip_port, uint8_t *data, uint32_t *length); 117/* call this several times a second */
118void networking_poll();
113 119
114/* initialize networking 120/* initialize networking
115 bind to ip and port 121 bind to ip and port
diff --git a/core/ping.c b/core/ping.c
index 6a1fbb7e..24ece06a 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)
163 return sendpacket(ipp, (uint8_t*) &pk, sizeof(pk)); 163 return sendpacket(ipp, (uint8_t*) &pk, sizeof(pk));
164} 164}
165 165
166int handle_ping_request(uint8_t* packet, uint32_t length, IP_Port source) 166int handle_ping_request(IP_Port source, uint8_t* packet, uint32_t length)
167{ 167{
168 pingreq_t* p = (pingreq_t*) packet; 168 pingreq_t* p = (pingreq_t*) packet;
169 int rc; 169 int rc;
@@ -190,7 +190,7 @@ int handle_ping_request(uint8_t* packet, uint32_t length, IP_Port source)
190 return 0; 190 return 0;
191} 191}
192 192
193int handle_ping_response(uint8_t* packet, uint32_t length, IP_Port source) 193int handle_ping_response(IP_Port source, uint8_t* packet, uint32_t length)
194{ 194{
195 pingres_t* p = (pingres_t*) packet; 195 pingres_t* p = (pingres_t*) packet;
196 int rc; 196 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);
12bool is_pinging(IP_Port ipp, uint64_t ping_id); 12bool is_pinging(IP_Port ipp, uint64_t ping_id);
13int send_ping_request(IP_Port ipp, clientid_t* client_id); 13int send_ping_request(IP_Port ipp, clientid_t* client_id);
14int send_ping_response(IP_Port ipp, clientid_t* client_id, uint64_t ping_id); 14int send_ping_response(IP_Port ipp, clientid_t* client_id, uint64_t ping_id);
15int handle_ping_request(uint8_t* packet, uint32_t length, IP_Port source); 15int handle_ping_request(IP_Port source, uint8_t* packet, uint32_t length);
16int handle_ping_response(uint8_t* packet, uint32_t length, IP_Port source); 16int handle_ping_response(IP_Port source, uint8_t* packet, uint32_t length);