summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorslvr <slvr@unsafeio.com>2013-08-10 00:30:18 +0100
committerslvr <slvr@unsafeio.com>2013-08-10 00:43:50 +0100
commit6052b1f119d2b1494a7f2691f4dfed235b560636 (patch)
treeb3f24c586180ba526baf33e3d3814d3c10b8821d /core
parent1ec95162a0b0c4f54bb97ca39684573e73beac87 (diff)
network_registerhandler
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.c43
-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.c2
-rw-r--r--core/ping.h4
-rw-r--r--core/substrate.h58
14 files changed, 143 insertions, 106 deletions
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,
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_request);
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..43eb7d42 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..d72a2c63 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 5f33886f..082b10d2 100644
--- a/core/Messenger.c
+++ b/core/Messenger.c
@@ -683,35 +683,22 @@ static void LANdiscovery(void)
683/* the main loop that needs to be run at least 200 times per second. */ 683/* the main loop that needs to be run at least 200 times per second. */
684void doMessenger(void) 684void doMessenger(void)
685{ 685{
686 IP_Port ip_port; 686 networking_poll();
687 uint8_t data[MAX_UDP_PACKET_SIZE];
688 uint32_t length;
689 while (receivepacket(&ip_port, data, &length) != -1) {
690#ifdef DEBUG
691 /* if(rand() % 3 != 1) //simulate packet loss */
692 /* { */
693 if (DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port) &&
694 friendreq_handlepacket(data, length, ip_port) && LANdiscovery_handlepacket(data, length, ip_port))
695 /* if packet is discarded */
696 printf("Received unhandled packet with length: %u\n", length);
697 else
698 printf("Received handled packet with length: %u\n", length);
699 /* } */
700 printf("Status: %u %u %u\n",friendlist[0].status ,is_cryptoconnected(friendlist[0].crypt_connection_id), friendlist[0].crypt_connection_id);
701#else
702 DHT_handlepacket(data, length, ip_port);
703 LosslessUDP_handlepacket(data, length, ip_port);
704 friendreq_handlepacket(data, length, ip_port);
705 LANdiscovery_handlepacket(data, length, ip_port);
706#endif
707 687
708 } 688 doDHT();
709 doDHT(); 689 doLossless_UDP();
710 doLossless_UDP(); 690 doNetCrypto();
711 doNetCrypto(); 691 doInbound();
712 doInbound(); 692 doFriends();
713 doFriends(); 693 LANdiscovery();
714 LANdiscovery(); 694}
695
696void Messenger_init(void)
697{
698 DHT_init();
699 LosslessUDP_init();
700 friendreq_init();
701 LANdiscovery_init();
715} 702}
716 703
717/* returns the size of the messenger data (for saving) */ 704/* 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)
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..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)
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))
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..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)
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;
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);
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 @@
1/* substrate.h
2 * The communications hub
3 * See also: http://http://wiki.tox.im/index.php/Proposal:Slvr_Protocol_Rewrite
4 *
5 * Copyright (C) 2013 Tox project All Rights Reserved.
6 *
7 * This file is part of Tox.
8 *
9 * Tox is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * Tox is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with Tox. If not, see <http://www.gnu.org/licenses/>.
21 *
22 */
23
24#include "network.h"
25
26#ifdef __cplusplus
27//extern "C" {
28#endif
29
30/* Type Definitions */
31
32typedef uint8_t channel_t;
33
34typedef struct {
35 byte a[32];
36} address_t;
37
38typedef struct {
39
40} connection_t;
41
42typedef void(*channel_recv_callback_t)(connection_t*, byte*, size_t, uint64_t);
43
44typedef void(*on_connection_callback_t)(connection_t*);
45
46/* Globals */
47
48extern address_t self_public_key;
49
50/* Functions */
51
52void substrate_init(byte* keydata);
53
54
55#ifdef __cplusplus
56//}
57#endif
58