diff options
-rw-r--r-- | core/DHT.h | 20 | ||||
-rw-r--r-- | core/LAN_discovery.c | 20 | ||||
-rw-r--r-- | core/LAN_discovery.h | 2 | ||||
-rw-r--r-- | core/Lossless_UDP.c | 174 | ||||
-rw-r--r-- | core/Lossless_UDP.h | 6 | ||||
-rw-r--r-- | core/Messenger.c | 168 | ||||
-rw-r--r-- | core/Messenger.h | 20 | ||||
-rw-r--r-- | core/friend_requests.c | 50 | ||||
-rw-r--r-- | core/friend_requests.h | 4 | ||||
-rw-r--r-- | core/net_crypto.c | 176 | ||||
-rw-r--r-- | core/net_crypto.h | 24 | ||||
-rw-r--r-- | core/network.c | 13 | ||||
-rw-r--r-- | core/network.h | 21 | ||||
-rw-r--r-- | testing/DHT_sendfiletest.c | 60 | ||||
-rw-r--r-- | testing/Lossless_UDP_testclient.c | 40 | ||||
-rw-r--r-- | testing/Lossless_UDP_testserver.c | 22 | ||||
-rw-r--r-- | testing/nTox.c | 50 |
17 files changed, 412 insertions, 458 deletions
@@ -40,13 +40,13 @@ extern "C" { | |||
40 | client_id must be CLIENT_ID_SIZE bytes long. | 40 | client_id must be CLIENT_ID_SIZE bytes long. |
41 | returns 0 if success | 41 | returns 0 if success |
42 | returns 1 if failure (friends list is full) */ | 42 | returns 1 if failure (friends list is full) */ |
43 | int DHT_addfriend(uint8_t * client_id); | 43 | int DHT_addfriend(uint8_t *client_id); |
44 | 44 | ||
45 | /* Delete a friend from the friends list | 45 | /* Delete a friend from the friends list |
46 | client_id must be CLIENT_ID_SIZE bytes long. | 46 | client_id must be CLIENT_ID_SIZE bytes long. |
47 | returns 0 if success | 47 | returns 0 if success |
48 | returns 1 if failure (client_id not in friends list) */ | 48 | returns 1 if failure (client_id not in friends list) */ |
49 | int DHT_delfriend(uint8_t * client_id); | 49 | int DHT_delfriend(uint8_t *client_id); |
50 | 50 | ||
51 | /* Get ip of friend | 51 | /* Get ip of friend |
52 | client_id must be CLIENT_ID_SIZE bytes long. | 52 | client_id must be CLIENT_ID_SIZE bytes long. |
@@ -55,7 +55,7 @@ int DHT_delfriend(uint8_t * client_id); | |||
55 | returns ip if success | 55 | returns ip if success |
56 | returns ip of 0 if failure (This means the friend is either offline or we have not found him yet.) | 56 | returns ip of 0 if failure (This means the friend is either offline or we have not found him yet.) |
57 | returns ip of 1 if friend is not in list. */ | 57 | returns ip of 1 if friend is not in list. */ |
58 | IP_Port DHT_getfriendip(uint8_t * client_id); | 58 | IP_Port DHT_getfriendip(uint8_t *client_id); |
59 | 59 | ||
60 | /* Run this function at least a couple times per second (It's the main loop) */ | 60 | /* Run this function at least a couple times per second (It's the main loop) */ |
61 | void doDHT(); | 61 | void doDHT(); |
@@ -63,21 +63,21 @@ void doDHT(); | |||
63 | /* if we receive a DHT packet we call this function so it can be handled. | 63 | /* if we receive a DHT packet we call this function so it can be handled. |
64 | return 0 if packet is handled correctly. | 64 | return 0 if packet is handled correctly. |
65 | return 1 if it didn't handle the packet or if the packet was shit. */ | 65 | return 1 if it didn't handle the packet or if the packet was shit. */ |
66 | int DHT_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); | 66 | int DHT_handlepacket(uint8_t *packet, uint32_t length, IP_Port source); |
67 | 67 | ||
68 | /* Use this function to bootstrap the client | 68 | /* Use this function to bootstrap the client |
69 | Sends a get nodes request to the given node with ip port and public_key */ | 69 | Sends a get nodes request to the given node with ip port and public_key */ |
70 | void DHT_bootstrap(IP_Port ip_port, uint8_t * public_key); | 70 | void DHT_bootstrap(IP_Port ip_port, uint8_t *public_key); |
71 | 71 | ||
72 | /* ROUTING FUNCTIONS */ | 72 | /* ROUTING FUNCTIONS */ |
73 | 73 | ||
74 | /* send the given packet to node with client_id | 74 | /* send the given packet to node with client_id |
75 | returns -1 if failure */ | 75 | returns -1 if failure */ |
76 | int route_packet(uint8_t * client_id, uint8_t * packet, uint32_t length); | 76 | int route_packet(uint8_t *client_id, uint8_t *packet, uint32_t length); |
77 | 77 | ||
78 | /* Send the following packet to everyone who tells us they are connected to friend_id | 78 | /* Send the following packet to everyone who tells us they are connected to friend_id |
79 | returns the number of nodes it sent the packet to */ | 79 | returns the number of nodes it sent the packet to */ |
80 | int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length); | 80 | int route_tofriend(uint8_t *friend_id, uint8_t *packet, uint32_t length); |
81 | 81 | ||
82 | /* NAT PUNCHING FUNCTIONS */ | 82 | /* NAT PUNCHING FUNCTIONS */ |
83 | 83 | ||
@@ -85,7 +85,7 @@ int route_tofriend(uint8_t * friend_id, uint8_t * packet, uint32_t length); | |||
85 | ip_portlist must be at least MAX_FRIEND_CLIENTS big | 85 | ip_portlist must be at least MAX_FRIEND_CLIENTS big |
86 | returns the number of ips returned | 86 | returns the number of ips returned |
87 | returns -1 if no such friend*/ | 87 | returns -1 if no such friend*/ |
88 | int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id); | 88 | int friend_ips(IP_Port *ip_portlist, uint8_t *friend_id); |
89 | 89 | ||
90 | /* SAVE/LOAD functions */ | 90 | /* SAVE/LOAD functions */ |
91 | 91 | ||
@@ -93,12 +93,12 @@ int friend_ips(IP_Port * ip_portlist, uint8_t * friend_id); | |||
93 | uint32_t DHT_size(); | 93 | uint32_t DHT_size(); |
94 | 94 | ||
95 | /* save the DHT in data where data is an array of size DHT_size() */ | 95 | /* save the DHT in data where data is an array of size DHT_size() */ |
96 | void DHT_save(uint8_t * data); | 96 | void DHT_save(uint8_t *data); |
97 | 97 | ||
98 | /* load the DHT from data of size size; | 98 | /* load the DHT from data of size size; |
99 | return -1 if failure | 99 | return -1 if failure |
100 | return 0 if success */ | 100 | return 0 if success */ |
101 | int DHT_load(uint8_t * data, uint32_t size); | 101 | int DHT_load(uint8_t *data, uint32_t size); |
102 | 102 | ||
103 | /* returns 0 if we are not connected to the DHT | 103 | /* returns 0 if we are not connected to the DHT |
104 | returns 1 if we are */ | 104 | returns 1 if we are */ |
diff --git a/core/LAN_discovery.c b/core/LAN_discovery.c index 3cfcb067..1ac13c33 100644 --- a/core/LAN_discovery.c +++ b/core/LAN_discovery.c | |||
@@ -37,24 +37,24 @@ IP broadcast_ip() | |||
37 | return -1 if it is not */ | 37 | return -1 if it is not */ |
38 | int LAN_ip(IP ip) | 38 | int LAN_ip(IP ip) |
39 | { | 39 | { |
40 | if(ip.c[0] == 127)/* Loopback */ | 40 | if (ip.c[0] == 127)/* Loopback */ |
41 | return 0; | 41 | return 0; |
42 | if(ip.c[0] == 10)/* 10.0.0.0 to 10.255.255.255 range */ | 42 | if (ip.c[0] == 10)/* 10.0.0.0 to 10.255.255.255 range */ |
43 | return 0; | 43 | return 0; |
44 | if(ip.c[0] == 172 && ip.c[1] >= 16 && ip.c[1] <= 31)/* 172.16.0.0 to 172.31.255.255 range */ | 44 | if (ip.c[0] == 172 && ip.c[1] >= 16 && ip.c[1] <= 31)/* 172.16.0.0 to 172.31.255.255 range */ |
45 | return 0; | 45 | return 0; |
46 | if(ip.c[0] == 192 && ip.c[1] == 168) /* 192.168.0.0 to 192.168.255.255 range */ | 46 | if (ip.c[0] == 192 && ip.c[1] == 168) /* 192.168.0.0 to 192.168.255.255 range */ |
47 | return 0; | 47 | return 0; |
48 | if(ip.c[0] == 169 && ip.c[1] == 254 && ip.c[2] != 0 && ip.c[2] != 255)/* 169.254.1.0 to 169.254.254.255 range */ | 48 | if (ip.c[0] == 169 && ip.c[1] == 254 && ip.c[2] != 0 && ip.c[2] != 255)/* 169.254.1.0 to 169.254.254.255 range */ |
49 | return 0; | 49 | return 0; |
50 | return -1; | 50 | return -1; |
51 | } | 51 | } |
52 | 52 | ||
53 | int handle_LANdiscovery(uint8_t * packet, uint32_t length, IP_Port source) | 53 | int handle_LANdiscovery(uint8_t *packet, uint32_t length, IP_Port source) |
54 | { | 54 | { |
55 | if(LAN_ip(source.ip) == -1) | 55 | if (LAN_ip(source.ip) == -1) |
56 | return 1; | 56 | return 1; |
57 | if(length != crypto_box_PUBLICKEYBYTES + 1) | 57 | if (length != crypto_box_PUBLICKEYBYTES + 1) |
58 | return 1; | 58 | return 1; |
59 | DHT_bootstrap(source, packet + 1); | 59 | DHT_bootstrap(source, packet + 1); |
60 | return 0; | 60 | return 0; |
@@ -71,9 +71,9 @@ int send_LANdiscovery(uint16_t port) | |||
71 | } | 71 | } |
72 | 72 | ||
73 | 73 | ||
74 | int LANdiscovery_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | 74 | int LANdiscovery_handlepacket(uint8_t *packet, uint32_t length, IP_Port source) |
75 | { | 75 | { |
76 | if(packet[0] == 32) | 76 | if (packet[0] == 32) |
77 | return handle_LANdiscovery(packet, length, source); | 77 | return handle_LANdiscovery(packet, length, source); |
78 | return 1; | 78 | return 1; |
79 | } | 79 | } |
diff --git a/core/LAN_discovery.h b/core/LAN_discovery.h index 655830f9..cca5bd30 100644 --- a/core/LAN_discovery.h +++ b/core/LAN_discovery.h | |||
@@ -39,7 +39,7 @@ int send_LANdiscovery(uint16_t port); | |||
39 | /* if we receive a packet we call this function so it can be handled. | 39 | /* if we receive a packet we call this function so it can be handled. |
40 | return 0 if packet is handled correctly. | 40 | return 0 if packet is handled correctly. |
41 | return 1 if it didn't handle the packet or if the packet was shit. */ | 41 | return 1 if it didn't handle the packet or if the packet was shit. */ |
42 | int LANdiscovery_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); | 42 | int LANdiscovery_handlepacket(uint8_t *packet, uint32_t length, IP_Port source); |
43 | 43 | ||
44 | 44 | ||
45 | 45 | ||
diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c index 43f61b5b..f9d20b2f 100644 --- a/core/Lossless_UDP.c +++ b/core/Lossless_UDP.c | |||
@@ -44,14 +44,12 @@ timeout per connection is randomly set between CONNEXION_TIMEOUT and 2*CONNEXION | |||
44 | /* initial send rate of data. */ | 44 | /* initial send rate of data. */ |
45 | #define DATA_SYNC_RATE 30 | 45 | #define DATA_SYNC_RATE 30 |
46 | 46 | ||
47 | typedef struct | 47 | typedef struct { |
48 | { | ||
49 | uint8_t data[MAX_DATA_SIZE]; | 48 | uint8_t data[MAX_DATA_SIZE]; |
50 | uint16_t size; | 49 | uint16_t size; |
51 | }Data; | 50 | }Data; |
52 | 51 | ||
53 | typedef struct | 52 | typedef struct { |
54 | { | ||
55 | IP_Port ip_port; | 53 | IP_Port ip_port; |
56 | uint8_t status; /* 0 if connection is dead, 1 if attempting handshake, | 54 | uint8_t status; /* 0 if connection is dead, 1 if attempting handshake, |
57 | 2 if handshake is done (we start sending SYNC packets) | 55 | 2 if handshake is done (we start sending SYNC packets) |
@@ -97,13 +95,13 @@ static Connection connections[MAX_CONNECTIONS]; | |||
97 | /* get connection id from IP_Port | 95 | /* get connection id from IP_Port |
98 | return -1 if there are no connections like we are looking for | 96 | return -1 if there are no connections like we are looking for |
99 | return id if it found it */ | 97 | return id if it found it */ |
100 | int getconnection_id(IP_Port ip_port) | 98 | int getconnection_id(IP_Port ip_port) { |
101 | { | ||
102 | uint32_t i; | 99 | uint32_t i; |
103 | for(i = 0; i < MAX_CONNECTIONS; ++i) | 100 | for (i = 0; i < MAX_CONNECTIONS; ++i) { |
104 | if(connections[i].ip_port.ip.i == ip_port.ip.i && | 101 | if (connections[i].ip_port.ip.i == ip_port.ip.i && |
105 | connections[i].ip_port.port == ip_port.port && connections[i].status > 0) | 102 | connections[i].ip_port.port == ip_port.port && connections[i].status > 0) |
106 | return i; | 103 | return i; |
104 | } | ||
107 | return -1; | 105 | return -1; |
108 | } | 106 | } |
109 | 107 | ||
@@ -116,12 +114,12 @@ static uint32_t randtable[6][256]; | |||
116 | uint32_t handshake_id(IP_Port source) | 114 | uint32_t handshake_id(IP_Port source) |
117 | { | 115 | { |
118 | uint32_t id = 0, i; | 116 | uint32_t id = 0, i; |
119 | for(i = 0; i < 6; ++i) { | 117 | for (i = 0; i < 6; ++i) { |
120 | if(randtable[i][((uint8_t *)&source)[i]] == 0) | 118 | if(randtable[i][((uint8_t *)&source)[i]] == 0) |
121 | randtable[i][((uint8_t *)&source)[i]] = random_int(); | 119 | randtable[i][((uint8_t *)&source)[i]] = random_int(); |
122 | id ^= randtable[i][((uint8_t *)&source)[i]]; | 120 | id ^= randtable[i][((uint8_t *)&source)[i]]; |
123 | } | 121 | } |
124 | if(id == 0) /* id can't be zero */ | 122 | if (id == 0) /* id can't be zero */ |
125 | id = 1; | 123 | id = 1; |
126 | return id; | 124 | return id; |
127 | } | 125 | } |
@@ -141,10 +139,10 @@ void change_handshake(IP_Port source) | |||
141 | int new_connection(IP_Port ip_port) | 139 | int new_connection(IP_Port ip_port) |
142 | { | 140 | { |
143 | int connect = getconnection_id(ip_port); | 141 | int connect = getconnection_id(ip_port); |
144 | if(connect != -1) | 142 | if (connect != -1) |
145 | return connect; | 143 | return connect; |
146 | uint32_t i; | 144 | uint32_t i; |
147 | for(i = 0; i < MAX_CONNECTIONS; ++i) { | 145 | for (i = 0; i < MAX_CONNECTIONS; ++i) { |
148 | if(connections[i].status == 0) { | 146 | if(connections[i].status == 0) { |
149 | memset(&connections[i], 0, sizeof(Connection)); | 147 | memset(&connections[i], 0, sizeof(Connection)); |
150 | connections[i].ip_port = ip_port; | 148 | connections[i].ip_port = ip_port; |
@@ -173,11 +171,11 @@ int new_connection(IP_Port ip_port) | |||
173 | return -1 if it could not initialize the connection. */ | 171 | return -1 if it could not initialize the connection. */ |
174 | int new_inconnection(IP_Port ip_port) | 172 | int new_inconnection(IP_Port ip_port) |
175 | { | 173 | { |
176 | if(getconnection_id(ip_port) != -1) | 174 | if (getconnection_id(ip_port) != -1) |
177 | return -1; | 175 | return -1; |
178 | uint32_t i; | 176 | uint32_t i; |
179 | for(i = 0; i < MAX_CONNECTIONS; ++i) { | 177 | for (i = 0; i < MAX_CONNECTIONS; ++i) { |
180 | if(connections[i].status == 0) { | 178 | if (connections[i].status == 0) { |
181 | memset(&connections[i], 0, sizeof(Connection)); | 179 | memset(&connections[i], 0, sizeof(Connection)); |
182 | connections[i].ip_port = ip_port; | 180 | connections[i].ip_port = ip_port; |
183 | connections[i].status = 2; | 181 | connections[i].status = 2; |
@@ -202,11 +200,12 @@ int new_inconnection(IP_Port ip_port) | |||
202 | int incoming_connection() | 200 | int incoming_connection() |
203 | { | 201 | { |
204 | uint32_t i; | 202 | uint32_t i; |
205 | for(i = 0; i < MAX_CONNECTIONS; ++i) | 203 | for (i = 0; i < MAX_CONNECTIONS; ++i) { |
206 | if(connections[i].inbound == 2) { | 204 | if (connections[i].inbound == 2) { |
207 | connections[i].inbound = 1; | 205 | connections[i].inbound = 1; |
208 | return i; | 206 | return i; |
209 | } | 207 | } |
208 | } | ||
210 | return -1; | 209 | return -1; |
211 | } | 210 | } |
212 | 211 | ||
@@ -214,12 +213,13 @@ int incoming_connection() | |||
214 | return 0 if killed successfully */ | 213 | return 0 if killed successfully */ |
215 | int kill_connection(int connection_id) | 214 | int kill_connection(int connection_id) |
216 | { | 215 | { |
217 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) | 216 | if (connection_id >= 0 && connection_id < MAX_CONNECTIONS) { |
218 | if(connections[connection_id].status > 0) { | 217 | if (connections[connection_id].status > 0) { |
219 | connections[connection_id].status = 0; | 218 | connections[connection_id].status = 0; |
220 | change_handshake(connections[connection_id].ip_port); | 219 | change_handshake(connections[connection_id].ip_port); |
221 | return 0; | 220 | return 0; |
222 | } | 221 | } |
222 | } | ||
223 | return -1; | 223 | return -1; |
224 | } | 224 | } |
225 | 225 | ||
@@ -228,11 +228,12 @@ int kill_connection(int connection_id) | |||
228 | return 0 if it will kill it */ | 228 | return 0 if it will kill it */ |
229 | int kill_connection_in(int connection_id, uint32_t seconds) | 229 | int kill_connection_in(int connection_id, uint32_t seconds) |
230 | { | 230 | { |
231 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) | 231 | if (connection_id >= 0 && connection_id < MAX_CONNECTIONS) { |
232 | if(connections[connection_id].status > 0) { | 232 | if (connections[connection_id].status > 0) { |
233 | connections[connection_id].killat = current_time() + 1000000UL*seconds; | 233 | connections[connection_id].killat = current_time() + 1000000UL*seconds; |
234 | return 0; | 234 | return 0; |
235 | } | 235 | } |
236 | } | ||
236 | return -1; | 237 | return -1; |
237 | } | 238 | } |
238 | 239 | ||
@@ -244,7 +245,7 @@ int kill_connection_in(int connection_id, uint32_t seconds) | |||
244 | return 4 if timed out and waiting to be killed */ | 245 | return 4 if timed out and waiting to be killed */ |
245 | int is_connected(int connection_id) | 246 | int is_connected(int connection_id) |
246 | { | 247 | { |
247 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) | 248 | if (connection_id >= 0 && connection_id < MAX_CONNECTIONS) |
248 | return connections[connection_id].status; | 249 | return connections[connection_id].status; |
249 | return 0; | 250 | return 0; |
250 | } | 251 | } |
@@ -252,7 +253,7 @@ int is_connected(int connection_id) | |||
252 | /* returns the ip_port of the corresponding connection. */ | 253 | /* returns the ip_port of the corresponding connection. */ |
253 | IP_Port connection_ip(int connection_id) | 254 | IP_Port connection_ip(int connection_id) |
254 | { | 255 | { |
255 | if(connection_id >= 0 && connection_id < MAX_CONNECTIONS) | 256 | if (connection_id >= 0 && connection_id < MAX_CONNECTIONS) |
256 | return connections[connection_id].ip_port; | 257 | return connections[connection_id].ip_port; |
257 | IP_Port zero = {{{0}}, 0}; | 258 | IP_Port zero = {{{0}}, 0}; |
258 | return zero; | 259 | return zero; |
@@ -274,7 +275,7 @@ uint32_t recvqueue(int connection_id) | |||
274 | return -1 if no packet in queue */ | 275 | return -1 if no packet in queue */ |
275 | char id_packet(int connection_id) | 276 | char id_packet(int connection_id) |
276 | { | 277 | { |
277 | if(recvqueue(connection_id) != 0 && connections[connection_id].status != 0) | 278 | if (recvqueue(connection_id) != 0 && connections[connection_id].status != 0) |
278 | return connections[connection_id].recvbuffer[connections[connection_id].successful_read % MAX_QUEUE_NUM].data[0]; | 279 | return connections[connection_id].recvbuffer[connections[connection_id].successful_read % MAX_QUEUE_NUM].data[0]; |
279 | return -1; | 280 | return -1; |
280 | } | 281 | } |
@@ -283,7 +284,7 @@ char id_packet(int connection_id) | |||
283 | return length of received packet if successful */ | 284 | return length of received packet if successful */ |
284 | int read_packet(int connection_id, uint8_t * data) | 285 | int read_packet(int connection_id, uint8_t * data) |
285 | { | 286 | { |
286 | if(recvqueue(connection_id) != 0) { | 287 | if (recvqueue(connection_id) != 0) { |
287 | uint16_t index = connections[connection_id].successful_read % MAX_QUEUE_NUM; | 288 | uint16_t index = connections[connection_id].successful_read % MAX_QUEUE_NUM; |
288 | uint16_t size = connections[connection_id].recvbuffer[index].size; | 289 | uint16_t size = connections[connection_id].recvbuffer[index].size; |
289 | memcpy(data, connections[connection_id].recvbuffer[index].data, size); | 290 | memcpy(data, connections[connection_id].recvbuffer[index].data, size); |
@@ -298,11 +299,11 @@ int read_packet(int connection_id, uint8_t * data) | |||
298 | return 1 if data was put into the queue */ | 299 | return 1 if data was put into the queue */ |
299 | int write_packet(int connection_id, uint8_t * data, uint32_t length) | 300 | int write_packet(int connection_id, uint8_t * data, uint32_t length) |
300 | { | 301 | { |
301 | if(length > MAX_DATA_SIZE) | 302 | if (length > MAX_DATA_SIZE) |
302 | return 0; | 303 | return 0; |
303 | if(length == 0) | 304 | if (length == 0) |
304 | return 0; | 305 | return 0; |
305 | if(sendqueue(connection_id) < BUFFER_PACKET_NUM) { | 306 | if (sendqueue(connection_id) < BUFFER_PACKET_NUM) { |
306 | uint32_t index = connections[connection_id].sendbuff_packetnum % MAX_QUEUE_NUM; | 307 | uint32_t index = connections[connection_id].sendbuff_packetnum % MAX_QUEUE_NUM; |
307 | memcpy(connections[connection_id].sendbuffer[index].data, data, length); | 308 | memcpy(connections[connection_id].sendbuffer[index].data, data, length); |
308 | connections[connection_id].sendbuffer[index].size = length; | 309 | connections[connection_id].sendbuffer[index].size = length; |
@@ -318,14 +319,15 @@ uint32_t missing_packets(int connection_id, uint32_t * requested) | |||
318 | uint32_t number = 0; | 319 | uint32_t number = 0; |
319 | uint32_t i; | 320 | uint32_t i; |
320 | uint32_t temp; | 321 | uint32_t temp; |
321 | if(recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1)) /* don't request packets if the buffer is full. */ | 322 | if (recvqueue(connection_id) >= (BUFFER_PACKET_NUM - 1)) /* don't request packets if the buffer is full. */ |
322 | return 0; | 323 | return 0; |
323 | for(i = connections[connection_id].recv_packetnum; i != connections[connection_id].osent_packetnum; i++ ) | 324 | for (i = connections[connection_id].recv_packetnum; i != connections[connection_id].osent_packetnum; i++) { |
324 | if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size == 0) { | 325 | if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size == 0) { |
325 | temp = htonl(i); | 326 | temp = htonl(i); |
326 | memcpy(requested + number, &temp, 4); | 327 | memcpy(requested + number, &temp, 4); |
327 | ++number; | 328 | ++number; |
328 | } | 329 | } |
330 | } | ||
329 | if(number == 0) | 331 | if(number == 0) |
330 | connections[connection_id].recv_packetnum = connections[connection_id].osent_packetnum; | 332 | connections[connection_id].recv_packetnum = connections[connection_id].osent_packetnum; |
331 | return number; | 333 | return number; |
@@ -393,14 +395,14 @@ int send_DATA(uint32_t connection_id) | |||
393 | { | 395 | { |
394 | int ret; | 396 | int ret; |
395 | uint32_t buffer[BUFFER_PACKET_NUM]; | 397 | uint32_t buffer[BUFFER_PACKET_NUM]; |
396 | if(connections[connection_id].num_req_paquets > 0) { | 398 | if (connections[connection_id].num_req_paquets > 0) { |
397 | ret = send_data_packet(connection_id, connections[connection_id].req_packets[0]); | 399 | ret = send_data_packet(connection_id, connections[connection_id].req_packets[0]); |
398 | connections[connection_id].num_req_paquets--; | 400 | connections[connection_id].num_req_paquets--; |
399 | memcpy(buffer, connections[connection_id].req_packets + 1, connections[connection_id].num_req_paquets * 4); | 401 | memcpy(buffer, connections[connection_id].req_packets + 1, connections[connection_id].num_req_paquets * 4); |
400 | memcpy(connections[connection_id].req_packets, buffer, connections[connection_id].num_req_paquets * 4); | 402 | memcpy(connections[connection_id].req_packets, buffer, connections[connection_id].num_req_paquets * 4); |
401 | return ret; | 403 | return ret; |
402 | } | 404 | } |
403 | if(connections[connection_id].sendbuff_packetnum != connections[connection_id].sent_packetnum) { | 405 | if (connections[connection_id].sendbuff_packetnum != connections[connection_id].sent_packetnum) { |
404 | ret = send_data_packet(connection_id, connections[connection_id].sent_packetnum); | 406 | ret = send_data_packet(connection_id, connections[connection_id].sent_packetnum); |
405 | connections[connection_id].sent_packetnum++; | 407 | connections[connection_id].sent_packetnum++; |
406 | return ret; | 408 | return ret; |
@@ -415,7 +417,7 @@ int send_DATA(uint32_t connection_id) | |||
415 | return 0 if handled correctly, 1 if packet is bad. */ | 417 | return 0 if handled correctly, 1 if packet is bad. */ |
416 | int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) | 418 | int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) |
417 | { | 419 | { |
418 | if(length != (1 + 4 + 4)) | 420 | if (length != (1 + 4 + 4)) |
419 | return 1; | 421 | return 1; |
420 | uint32_t temp; | 422 | uint32_t temp; |
421 | uint32_t handshake_id1, handshake_id2; | 423 | uint32_t handshake_id1, handshake_id2; |
@@ -425,13 +427,13 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) | |||
425 | memcpy(&temp, packet + 5, 4); | 427 | memcpy(&temp, packet + 5, 4); |
426 | handshake_id2 = ntohl(temp); | 428 | handshake_id2 = ntohl(temp); |
427 | 429 | ||
428 | if(handshake_id2 == 0) { | 430 | if (handshake_id2 == 0) { |
429 | send_handshake(source, handshake_id(source), handshake_id1); | 431 | send_handshake(source, handshake_id(source), handshake_id1); |
430 | return 0; | 432 | return 0; |
431 | } | 433 | } |
432 | if(is_connected(connection) != 1) | 434 | if (is_connected(connection) != 1) |
433 | return 1; | 435 | return 1; |
434 | if(handshake_id2 == connections[connection].handshake_id1) { /* if handshake_id2 is what we sent previously as handshake_id1 */ | 436 | if (handshake_id2 == connections[connection].handshake_id1) { /* if handshake_id2 is what we sent previously as handshake_id1 */ |
435 | connections[connection].status = 2; | 437 | connections[connection].status = 2; |
436 | /* NOTE: is this necessary? | 438 | /* NOTE: is this necessary? |
437 | connections[connection].handshake_id2 = handshake_id1; */ | 439 | connections[connection].handshake_id2 = handshake_id1; */ |
@@ -448,10 +450,10 @@ int handle_handshake(uint8_t * packet, uint32_t length, IP_Port source) | |||
448 | 0 if not. */ | 450 | 0 if not. */ |
449 | int SYNC_valid(uint32_t length) | 451 | int SYNC_valid(uint32_t length) |
450 | { | 452 | { |
451 | if(length < 4 + 4 + 2) | 453 | if (length < 4 + 4 + 2) |
452 | return 0; | 454 | return 0; |
453 | if(length > (BUFFER_PACKET_NUM*4 + 4 + 4 + 2) || | 455 | if (length > (BUFFER_PACKET_NUM*4 + 4 + 4 + 2) || |
454 | ((length - 4 - 4 - 2) % 4) != 0) | 456 | ((length - 4 - 4 - 2) % 4) != 0) |
455 | return 0; | 457 | return 0; |
456 | return 1; | 458 | return 1; |
457 | } | 459 | } |
@@ -459,9 +461,9 @@ int SYNC_valid(uint32_t length) | |||
459 | /* case 1: */ | 461 | /* case 1: */ |
460 | int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) | 462 | int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnum) |
461 | { | 463 | { |
462 | if(handshake_id(source) == recv_packetnum) { | 464 | if (handshake_id(source) == recv_packetnum) { |
463 | int x = new_inconnection(source); | 465 | int x = new_inconnection(source); |
464 | if(x != -1) { | 466 | if (x != -1) { |
465 | connections[x].orecv_packetnum = recv_packetnum; | 467 | connections[x].orecv_packetnum = recv_packetnum; |
466 | connections[x].sent_packetnum = recv_packetnum; | 468 | connections[x].sent_packetnum = recv_packetnum; |
467 | connections[x].sendbuff_packetnum = recv_packetnum; | 469 | connections[x].sendbuff_packetnum = recv_packetnum; |
@@ -479,7 +481,7 @@ int handle_SYNC1(IP_Port source, uint32_t recv_packetnum, uint32_t sent_packetnu | |||
479 | /* case 2: */ | 481 | /* case 2: */ |
480 | int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum) | 482 | int handle_SYNC2(int connection_id, uint8_t counter, uint32_t recv_packetnum, uint32_t sent_packetnum) |
481 | { | 483 | { |
482 | if(recv_packetnum == connections[connection_id].orecv_packetnum) { | 484 | if (recv_packetnum == connections[connection_id].orecv_packetnum) { |
483 | /* && sent_packetnum == connections[connection_id].osent_packetnum) */ | 485 | /* && sent_packetnum == connections[connection_id].osent_packetnum) */ |
484 | connections[connection_id].status = 3; | 486 | connections[connection_id].status = 3; |
485 | connections[connection_id].recv_counter = counter; | 487 | connections[connection_id].recv_counter = counter; |
@@ -499,14 +501,14 @@ int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, ui | |||
499 | uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); */ | 501 | uint32_t comp_2 = (sent_packetnum - connections[connection_id].successful_read); */ |
500 | uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum); | 502 | uint32_t comp_1 = (recv_packetnum - connections[connection_id].orecv_packetnum); |
501 | uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum); | 503 | uint32_t comp_2 = (sent_packetnum - connections[connection_id].osent_packetnum); |
502 | if(comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) { /* packet valid */ | 504 | if (comp_1 <= BUFFER_PACKET_NUM && comp_2 <= BUFFER_PACKET_NUM && comp_counter < 10 && comp_counter != 0) { /* packet valid */ |
503 | connections[connection_id].orecv_packetnum = recv_packetnum; | 505 | connections[connection_id].orecv_packetnum = recv_packetnum; |
504 | connections[connection_id].osent_packetnum = sent_packetnum; | 506 | connections[connection_id].osent_packetnum = sent_packetnum; |
505 | connections[connection_id].successful_sent = recv_packetnum; | 507 | connections[connection_id].successful_sent = recv_packetnum; |
506 | connections[connection_id].last_recvSYNC = current_time(); | 508 | connections[connection_id].last_recvSYNC = current_time(); |
507 | connections[connection_id].recv_counter = counter; | 509 | connections[connection_id].recv_counter = counter; |
508 | ++connections[connection_id].send_counter; | 510 | ++connections[connection_id].send_counter; |
509 | for(i = 0; i < number; ++i) { | 511 | for (i = 0; i < number; ++i) { |
510 | temp = ntohl(req_packets[i]); | 512 | temp = ntohl(req_packets[i]); |
511 | memcpy(connections[connection_id].req_packets + i, &temp, 4 * number); | 513 | memcpy(connections[connection_id].req_packets + i, &temp, 4 * number); |
512 | } | 514 | } |
@@ -516,10 +518,10 @@ int handle_SYNC3(int connection_id, uint8_t counter, uint32_t recv_packetnum, ui | |||
516 | return 1; | 518 | return 1; |
517 | } | 519 | } |
518 | 520 | ||
519 | int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source) | 521 | int handle_SYNC(uint8_t *packet, uint32_t length, IP_Port source) |
520 | { | 522 | { |
521 | 523 | ||
522 | if(!SYNC_valid(length)) | 524 | if (!SYNC_valid(length)) |
523 | return 1; | 525 | return 1; |
524 | int connection = getconnection_id(source); | 526 | int connection = getconnection_id(source); |
525 | uint8_t counter; | 527 | uint8_t counter; |
@@ -533,39 +535,39 @@ int handle_SYNC(uint8_t * packet, uint32_t length, IP_Port source) | |||
533 | recv_packetnum = ntohl(temp); | 535 | recv_packetnum = ntohl(temp); |
534 | memcpy(&temp,packet + 6, 4); | 536 | memcpy(&temp,packet + 6, 4); |
535 | sent_packetnum = ntohl(temp); | 537 | sent_packetnum = ntohl(temp); |
536 | if(number != 0) | 538 | if (number != 0) |
537 | memcpy(req_packets, packet + 10, 4 * number); | 539 | memcpy(req_packets, packet + 10, 4 * number); |
538 | if(connection == -1) | 540 | if (connection == -1) |
539 | return handle_SYNC1(source, recv_packetnum, sent_packetnum); | 541 | return handle_SYNC1(source, recv_packetnum, sent_packetnum); |
540 | if(connections[connection].status == 2) | 542 | if (connections[connection].status == 2) |
541 | return handle_SYNC2(connection, counter, recv_packetnum, sent_packetnum); | 543 | return handle_SYNC2(connection, counter, recv_packetnum, sent_packetnum); |
542 | if(connections[connection].status == 3) | 544 | if (connections[connection].status == 3) |
543 | return handle_SYNC3(connection, counter, recv_packetnum, sent_packetnum, req_packets, number); | 545 | return handle_SYNC3(connection, counter, recv_packetnum, sent_packetnum, req_packets, number); |
544 | return 0; | 546 | return 0; |
545 | } | 547 | } |
546 | 548 | ||
547 | /* add a packet to the received buffer and set the recv_packetnum of the connection to its proper value. | 549 | /* add a packet to the received buffer and set the recv_packetnum of the connection to its proper value. |
548 | return 1 if data was too big, 0 if not. */ | 550 | return 1 if data was too big, 0 if not. */ |
549 | int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size) | 551 | int add_recv(int connection_id, uint32_t data_num, uint8_t *data, uint16_t size) |
550 | { | 552 | { |
551 | if(size > MAX_DATA_SIZE) | 553 | if (size > MAX_DATA_SIZE) |
552 | return 1; | 554 | return 1; |
553 | 555 | ||
554 | uint32_t i; | 556 | uint32_t i; |
555 | uint32_t maxnum = connections[connection_id].successful_read + BUFFER_PACKET_NUM; | 557 | uint32_t maxnum = connections[connection_id].successful_read + BUFFER_PACKET_NUM; |
556 | uint32_t sent_packet = data_num - connections[connection_id].osent_packetnum; | 558 | uint32_t sent_packet = data_num - connections[connection_id].osent_packetnum; |
557 | for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) { | 559 | for (i = connections[connection_id].recv_packetnum; i != maxnum; ++i) { |
558 | if(i == data_num) { | 560 | if (i == data_num) { |
559 | memcpy(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size); | 561 | memcpy(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].data, data, size); |
560 | connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size; | 562 | connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size = size; |
561 | connections[connection_id].last_recvdata = current_time(); | 563 | connections[connection_id].last_recvdata = current_time(); |
562 | if(sent_packet < BUFFER_PACKET_NUM) | 564 | if (sent_packet < BUFFER_PACKET_NUM) |
563 | connections[connection_id].osent_packetnum = data_num; | 565 | connections[connection_id].osent_packetnum = data_num; |
564 | break; | 566 | break; |
565 | } | 567 | } |
566 | } | 568 | } |
567 | for(i = connections[connection_id].recv_packetnum; i != maxnum; ++i) { | 569 | for (i = connections[connection_id].recv_packetnum; i != maxnum; ++i) { |
568 | if(connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size != 0) | 570 | if (connections[connection_id].recvbuffer[i % MAX_QUEUE_NUM].size != 0) |
569 | connections[connection_id].recv_packetnum = i; | 571 | connections[connection_id].recv_packetnum = i; |
570 | else | 572 | else |
571 | break; | 573 | break; |
@@ -574,17 +576,17 @@ int add_recv(int connection_id, uint32_t data_num, uint8_t * data, uint16_t size | |||
574 | return 0; | 576 | return 0; |
575 | } | 577 | } |
576 | 578 | ||
577 | int handle_data(uint8_t * packet, uint32_t length, IP_Port source) | 579 | int handle_data(uint8_t *packet, uint32_t length, IP_Port source) |
578 | { | 580 | { |
579 | int connection = getconnection_id(source); | 581 | int connection = getconnection_id(source); |
580 | 582 | ||
581 | if(connection == -1) | 583 | if (connection == -1) |
582 | return 1; | 584 | return 1; |
583 | 585 | ||
584 | if(connections[connection].status != 3) /* Drop the data packet if connection is not connected. */ | 586 | if (connections[connection].status != 3) /* Drop the data packet if connection is not connected. */ |
585 | return 1; | 587 | return 1; |
586 | 588 | ||
587 | if(length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1) | 589 | if (length > 1 + 4 + MAX_DATA_SIZE || length < 1 + 4 + 1) |
588 | return 1; | 590 | return 1; |
589 | uint32_t temp; | 591 | uint32_t temp; |
590 | uint32_t number; | 592 | uint32_t number; |
@@ -597,9 +599,9 @@ int handle_data(uint8_t * packet, uint32_t length, IP_Port source) | |||
597 | 599 | ||
598 | /* END of packet handling functions */ | 600 | /* END of packet handling functions */ |
599 | 601 | ||
600 | int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | 602 | int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source) |
601 | { | 603 | { |
602 | switch (packet[0]) { | 604 | switch (packet[0]) { //TODO: check if no break statement is correct??? |
603 | case 16: | 605 | case 16: |
604 | return handle_handshake(packet, length, source); | 606 | return handle_handshake(packet, length, source); |
605 | 607 | ||
@@ -622,19 +624,19 @@ void doNew() | |||
622 | { | 624 | { |
623 | uint32_t i; | 625 | uint32_t i; |
624 | uint64_t temp_time = current_time(); | 626 | uint64_t temp_time = current_time(); |
625 | for(i = 0; i < MAX_CONNECTIONS; ++i) { | 627 | for (i = 0; i < MAX_CONNECTIONS; ++i) { |
626 | if(connections[i].status == 1) | 628 | if (connections[i].status == 1) |
627 | if((connections[i].last_sent + (1000000UL/connections[i].SYNC_rate)) <= temp_time) { | 629 | if ((connections[i].last_sent + (1000000UL/connections[i].SYNC_rate)) <= temp_time) { |
628 | send_handshake(connections[i].ip_port, connections[i].handshake_id1, 0); | 630 | send_handshake(connections[i].ip_port, connections[i].handshake_id1, 0); |
629 | connections[i].last_sent = temp_time; | 631 | connections[i].last_sent = temp_time; |
630 | } | 632 | } |
631 | 633 | ||
632 | /* kill all timed out connections */ | 634 | /* kill all timed out connections */ |
633 | if( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time && | 635 | if ( connections[i].status > 0 && (connections[i].last_recvSYNC + connections[i].timeout * 1000000UL) < temp_time && |
634 | connections[i].status != 4) | 636 | connections[i].status != 4) |
635 | /* kill_connection(i); */ | 637 | /* kill_connection(i); */ |
636 | connections[i].status = 4; | 638 | connections[i].status = 4; |
637 | if(connections[i].status > 0 && connections[i].killat < temp_time) | 639 | if (connections[i].status > 0 && connections[i].killat < temp_time) |
638 | kill_connection(i); | 640 | kill_connection(i); |
639 | } | 641 | } |
640 | } | 642 | } |
@@ -643,9 +645,9 @@ void doSYNC() | |||
643 | { | 645 | { |
644 | uint32_t i; | 646 | uint32_t i; |
645 | uint64_t temp_time = current_time(); | 647 | uint64_t temp_time = current_time(); |
646 | for(i = 0; i < MAX_CONNECTIONS; ++i) { | 648 | for (i = 0; i < MAX_CONNECTIONS; ++i) { |
647 | if(connections[i].status == 2 || connections[i].status == 3) | 649 | if (connections[i].status == 2 || connections[i].status == 3) |
648 | if((connections[i].last_SYNC + (1000000UL/connections[i].SYNC_rate)) <= temp_time) { | 650 | if ((connections[i].last_SYNC + (1000000UL/connections[i].SYNC_rate)) <= temp_time) { |
649 | send_SYNC(i); | 651 | send_SYNC(i); |
650 | connections[i].last_SYNC = temp_time; | 652 | connections[i].last_SYNC = temp_time; |
651 | } | 653 | } |
@@ -657,10 +659,10 @@ void doData() | |||
657 | uint32_t i; | 659 | uint32_t i; |
658 | uint64_t j; | 660 | uint64_t j; |
659 | uint64_t temp_time = current_time(); | 661 | uint64_t temp_time = current_time(); |
660 | for(i = 0; i < MAX_CONNECTIONS; ++i) | 662 | for (i = 0; i < MAX_CONNECTIONS; ++i) |
661 | if(connections[i].status == 3 && sendqueue(i) != 0) | 663 | if (connections[i].status == 3 && sendqueue(i) != 0) |
662 | if((connections[i].last_sent + (1000000UL/connections[i].data_rate)) <= temp_time) { | 664 | if ((connections[i].last_sent + (1000000UL/connections[i].data_rate)) <= temp_time) { |
663 | for(j = connections[i].last_sent; j < temp_time; j += (1000000UL/connections[i].data_rate)) | 665 | for (j = connections[i].last_sent; j < temp_time; j += (1000000UL/connections[i].data_rate)) |
664 | send_DATA(i); | 666 | send_DATA(i); |
665 | connections[i].last_sent = temp_time; | 667 | connections[i].last_sent = temp_time; |
666 | } | 668 | } |
@@ -675,15 +677,15 @@ void adjustRates() | |||
675 | { | 677 | { |
676 | uint32_t i; | 678 | uint32_t i; |
677 | uint64_t temp_time = current_time(); | 679 | uint64_t temp_time = current_time(); |
678 | for(i = 0; i < MAX_CONNECTIONS; ++i) { | 680 | for (i = 0; i < MAX_CONNECTIONS; ++i) { |
679 | if(connections[i].status == 1 || connections[i].status == 2) | 681 | if (connections[i].status == 1 || connections[i].status == 2) |
680 | connections[i].SYNC_rate = MAX_SYNC_RATE; | 682 | connections[i].SYNC_rate = MAX_SYNC_RATE; |
681 | if(connections[i].status == 3) { | 683 | if (connections[i].status == 3) { |
682 | if(sendqueue(i) != 0) { | 684 | if (sendqueue(i) != 0) { |
683 | connections[i].data_rate = (BUFFER_PACKET_NUM - connections[i].num_req_paquets) * MAX_SYNC_RATE; | 685 | connections[i].data_rate = (BUFFER_PACKET_NUM - connections[i].num_req_paquets) * MAX_SYNC_RATE; |
684 | connections[i].SYNC_rate = MAX_SYNC_RATE; | 686 | connections[i].SYNC_rate = MAX_SYNC_RATE; |
685 | } | 687 | } |
686 | else if(connections[i].last_recvdata + 1000000UL > temp_time) | 688 | else if (connections[i].last_recvdata + 1000000UL > temp_time) |
687 | connections[i].SYNC_rate = MAX_SYNC_RATE; | 689 | connections[i].SYNC_rate = MAX_SYNC_RATE; |
688 | else | 690 | else |
689 | connections[i].SYNC_rate = SYNC_RATE; | 691 | connections[i].SYNC_rate = SYNC_RATE; |
diff --git a/core/Lossless_UDP.h b/core/Lossless_UDP.h index a9f1bb15..d27fea79 100644 --- a/core/Lossless_UDP.h +++ b/core/Lossless_UDP.h | |||
@@ -69,11 +69,11 @@ char id_packet(int connection_id); | |||
69 | 69 | ||
70 | /* return 0 if there is no received data in the buffer. | 70 | /* return 0 if there is no received data in the buffer. |
71 | return length of received packet if successful */ | 71 | return length of received packet if successful */ |
72 | int read_packet(int connection_id, uint8_t * data); | 72 | int read_packet(int connection_id, uint8_t *data); |
73 | 73 | ||
74 | /* return 0 if data could not be put in packet queue | 74 | /* return 0 if data could not be put in packet queue |
75 | return 1 if data was put into the queue */ | 75 | return 1 if data was put into the queue */ |
76 | int write_packet(int connection_id, uint8_t * data, uint32_t length); | 76 | int write_packet(int connection_id, uint8_t *data, uint32_t length); |
77 | 77 | ||
78 | /* returns the number of packets in the queue waiting to be successfully sent. */ | 78 | /* returns the number of packets in the queue waiting to be successfully sent. */ |
79 | uint32_t sendqueue(int connection_id); | 79 | uint32_t sendqueue(int connection_id); |
@@ -97,7 +97,7 @@ void doLossless_UDP(); | |||
97 | /* if we receive a Lossless_UDP packet we call this function so it can be handled. | 97 | /* if we receive a Lossless_UDP packet we call this function so it can be handled. |
98 | return 0 if packet is handled correctly. | 98 | return 0 if packet is handled correctly. |
99 | return 1 if it didn't handle the packet or if the packet was shit. */ | 99 | return 1 if it didn't handle the packet or if the packet was shit. */ |
100 | int LosslessUDP_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); | 100 | int LosslessUDP_handlepacket(uint8_t *packet, uint32_t length, IP_Port source); |
101 | 101 | ||
102 | #ifdef __cplusplus | 102 | #ifdef __cplusplus |
103 | } | 103 | } |
diff --git a/core/Messenger.c b/core/Messenger.c index 69d33172..fd95869f 100644 --- a/core/Messenger.c +++ b/core/Messenger.c | |||
@@ -24,8 +24,7 @@ | |||
24 | #include "Messenger.h" | 24 | #include "Messenger.h" |
25 | #define MIN(a,b) (((a)<(b))?(a):(b)) | 25 | #define MIN(a,b) (((a)<(b))?(a):(b)) |
26 | 26 | ||
27 | typedef struct | 27 | typedef struct { |
28 | { | ||
29 | uint8_t client_id[CLIENT_ID_SIZE]; | 28 | uint8_t client_id[CLIENT_ID_SIZE]; |
30 | int crypt_connection_id; | 29 | int crypt_connection_id; |
31 | int friend_request_id; /* id of the friend request corresponding to the current friend request to the current friend. */ | 30 | int friend_request_id; /* id of the friend request corresponding to the current friend request to the current friend. */ |
@@ -37,7 +36,7 @@ typedef struct | |||
37 | uint16_t userstatus_length; | 36 | uint16_t userstatus_length; |
38 | uint8_t userstatus_sent; | 37 | uint8_t userstatus_sent; |
39 | uint16_t info_size; /* length of the info */ | 38 | uint16_t info_size; /* length of the info */ |
40 | }Friend; | 39 | } Friend; |
41 | 40 | ||
42 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | 41 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; |
43 | 42 | ||
@@ -57,14 +56,15 @@ static uint32_t numfriends; | |||
57 | 56 | ||
58 | /* return the friend id associated to that public key. | 57 | /* return the friend id associated to that public key. |
59 | return -1 if no such friend */ | 58 | return -1 if no such friend */ |
60 | int getfriend_id(uint8_t * client_id) | 59 | int getfriend_id(uint8_t *client_id) |
61 | { | 60 | { |
62 | uint32_t i; | 61 | uint32_t i; |
63 | 62 | ||
64 | for(i = 0; i < numfriends; ++i) | 63 | for (i = 0; i < numfriends; ++i) { |
65 | if(friendlist[i].status > 0) | 64 | if (friendlist[i].status > 0) |
66 | if(memcmp(client_id, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) | 65 | if (memcmp(client_id, friendlist[i].client_id, crypto_box_PUBLICKEYBYTES) == 0) |
67 | return i; | 66 | return i; |
67 | } | ||
68 | 68 | ||
69 | return -1; | 69 | return -1; |
70 | } | 70 | } |
@@ -73,12 +73,12 @@ int getfriend_id(uint8_t * client_id) | |||
73 | make sure that client_id is of size CLIENT_ID_SIZE. | 73 | make sure that client_id is of size CLIENT_ID_SIZE. |
74 | return 0 if success | 74 | return 0 if success |
75 | return -1 if failure. */ | 75 | return -1 if failure. */ |
76 | int getclient_id(int friend_id, uint8_t * client_id) | 76 | int getclient_id(int friend_id, uint8_t *client_id) |
77 | { | 77 | { |
78 | if(friend_id >= numfriends || friend_id < 0) | 78 | if (friend_id >= numfriends || friend_id < 0) |
79 | return -1; | 79 | return -1; |
80 | 80 | ||
81 | if(friendlist[friend_id].status > 0) { | 81 | if (friendlist[friend_id].status > 0) { |
82 | memcpy(client_id, friendlist[friend_id].client_id, CLIENT_ID_SIZE); | 82 | memcpy(client_id, friendlist[friend_id].client_id, CLIENT_ID_SIZE); |
83 | return 0; | 83 | return 0; |
84 | } | 84 | } |
@@ -92,20 +92,18 @@ int getclient_id(int friend_id, uint8_t * client_id) | |||
92 | data is the data and length is the length | 92 | data is the data and length is the length |
93 | returns the friend number if success | 93 | returns the friend number if success |
94 | return -1 if failure. */ | 94 | return -1 if failure. */ |
95 | int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length) | 95 | int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length) |
96 | { | 96 | { |
97 | if(length == 0 || length >= | 97 | if (length == 0 || length >= |
98 | (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES)) | 98 | (MAX_DATA_SIZE - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES)) |
99 | return -1; | 99 | return -1; |
100 | if(memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) | 100 | if (memcmp(client_id, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) |
101 | return -1; | 101 | return -1; |
102 | if(getfriend_id(client_id) != -1) | 102 | if (getfriend_id(client_id) != -1) |
103 | return -1; | 103 | return -1; |
104 | uint32_t i; | 104 | uint32_t i; |
105 | for(i = 0; i <= numfriends; ++i) | 105 | for (i = 0; i <= numfriends; ++i) { |
106 | { | 106 | if(friendlist[i].status == 0) { |
107 | if(friendlist[i].status == 0) | ||
108 | { | ||
109 | DHT_addfriend(client_id); | 107 | DHT_addfriend(client_id); |
110 | friendlist[i].status = 1; | 108 | friendlist[i].status = 1; |
111 | friendlist[i].crypt_connection_id = -1; | 109 | friendlist[i].crypt_connection_id = -1; |
@@ -125,13 +123,11 @@ int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length) | |||
125 | 123 | ||
126 | int m_addfriend_norequest(uint8_t * client_id) | 124 | int m_addfriend_norequest(uint8_t * client_id) |
127 | { | 125 | { |
128 | if(getfriend_id(client_id) != -1) | 126 | if (getfriend_id(client_id) != -1) |
129 | return -1; | 127 | return -1; |
130 | uint32_t i; | 128 | uint32_t i; |
131 | for(i = 0; i <= numfriends; ++i) | 129 | for (i = 0; i <= numfriends; ++i) { |
132 | { | 130 | if(friendlist[i].status == 0) { |
133 | if(friendlist[i].status == 0) | ||
134 | { | ||
135 | DHT_addfriend(client_id); | 131 | DHT_addfriend(client_id); |
136 | friendlist[i].status = 2; | 132 | friendlist[i].status = 2; |
137 | friendlist[i].crypt_connection_id = -1; | 133 | friendlist[i].crypt_connection_id = -1; |
@@ -151,7 +147,7 @@ int m_addfriend_norequest(uint8_t * client_id) | |||
151 | return -1 if failure */ | 147 | return -1 if failure */ |
152 | int m_delfriend(int friendnumber) | 148 | int m_delfriend(int friendnumber) |
153 | { | 149 | { |
154 | if(friendnumber >= numfriends || friendnumber < 0) | 150 | if (friendnumber >= numfriends || friendnumber < 0) |
155 | return -1; | 151 | return -1; |
156 | 152 | ||
157 | DHT_delfriend(friendlist[friendnumber].client_id); | 153 | DHT_delfriend(friendlist[friendnumber].client_id); |
@@ -159,9 +155,10 @@ int m_delfriend(int friendnumber) | |||
159 | free(friendlist[friendnumber].userstatus); | 155 | free(friendlist[friendnumber].userstatus); |
160 | memset(&friendlist[friendnumber], 0, sizeof(Friend)); | 156 | memset(&friendlist[friendnumber], 0, sizeof(Friend)); |
161 | uint32_t i; | 157 | uint32_t i; |
162 | for(i = numfriends; i != 0; --i) | 158 | for (i = numfriends; i != 0; --i) { |
163 | if(friendlist[i].status != 0) | 159 | if (friendlist[i].status != 0) |
164 | break; | 160 | break; |
161 | } | ||
165 | numfriends = i; | 162 | numfriends = i; |
166 | return 0; | 163 | return 0; |
167 | } | 164 | } |
@@ -173,7 +170,7 @@ int m_delfriend(int friendnumber) | |||
173 | return 0 if there is no friend with that number */ | 170 | return 0 if there is no friend with that number */ |
174 | int m_friendstatus(int friendnumber) | 171 | int m_friendstatus(int friendnumber) |
175 | { | 172 | { |
176 | if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) | 173 | if (friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) |
177 | return 0; | 174 | return 0; |
178 | return friendlist[friendnumber].status; | 175 | return friendlist[friendnumber].status; |
179 | } | 176 | } |
@@ -181,11 +178,11 @@ int m_friendstatus(int friendnumber) | |||
181 | /* send a text chat message to an online friend | 178 | /* send a text chat message to an online friend |
182 | return 1 if packet was successfully put into the send queue | 179 | return 1 if packet was successfully put into the send queue |
183 | return 0 if it was not */ | 180 | return 0 if it was not */ |
184 | int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length) | 181 | int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length) |
185 | { | 182 | { |
186 | if(friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) | 183 | if (friendnumber < 0 || friendnumber >= MAX_NUM_FRIENDS) |
187 | return 0; | 184 | return 0; |
188 | if(length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) | 185 | if (length >= MAX_DATA_SIZE || friendlist[friendnumber].status != 4) |
189 | /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */ | 186 | /* this does not mean the maximum message length is MAX_DATA_SIZE - 1, it is actually 17 bytes less. */ |
190 | return 0; | 187 | return 0; |
191 | uint8_t temp[MAX_DATA_SIZE]; | 188 | uint8_t temp[MAX_DATA_SIZE]; |
@@ -208,7 +205,7 @@ static int m_sendname(int friendnumber, uint8_t * name) | |||
208 | return -1 if failure */ | 205 | return -1 if failure */ |
209 | static int setfriendname(int friendnumber, uint8_t * name) | 206 | static int setfriendname(int friendnumber, uint8_t * name) |
210 | { | 207 | { |
211 | if(friendnumber >= numfriends || friendnumber < 0) | 208 | if (friendnumber >= numfriends || friendnumber < 0) |
212 | return -1; | 209 | return -1; |
213 | memcpy(friendlist[friendnumber].name, name, MAX_NAME_LENGTH); | 210 | memcpy(friendlist[friendnumber].name, name, MAX_NAME_LENGTH); |
214 | return 0; | 211 | return 0; |
@@ -220,11 +217,11 @@ static int setfriendname(int friendnumber, uint8_t * name) | |||
220 | return -1 if failure */ | 217 | return -1 if failure */ |
221 | int setname(uint8_t * name, uint16_t length) | 218 | int setname(uint8_t * name, uint16_t length) |
222 | { | 219 | { |
223 | if(length > MAX_NAME_LENGTH) | 220 | if (length > MAX_NAME_LENGTH) |
224 | return -1; | 221 | return -1; |
225 | memcpy(self_name, name, length); | 222 | memcpy(self_name, name, length); |
226 | uint32_t i; | 223 | uint32_t i; |
227 | for(i = 0; i < numfriends; ++i) | 224 | for (i = 0; i < numfriends; ++i) |
228 | friendlist[i].name_sent = 0; | 225 | friendlist[i].name_sent = 0; |
229 | return 0; | 226 | return 0; |
230 | } | 227 | } |
@@ -236,7 +233,7 @@ int setname(uint8_t * name, uint16_t length) | |||
236 | return -1 if failure */ | 233 | return -1 if failure */ |
237 | int getname(int friendnumber, uint8_t * name) | 234 | int getname(int friendnumber, uint8_t * name) |
238 | { | 235 | { |
239 | if(friendnumber >= numfriends || friendnumber < 0) | 236 | if (friendnumber >= numfriends || friendnumber < 0) |
240 | return -1; | 237 | return -1; |
241 | memcpy(name, friendlist[friendnumber].name, MAX_NAME_LENGTH); | 238 | memcpy(name, friendlist[friendnumber].name, MAX_NAME_LENGTH); |
242 | return 0; | 239 | return 0; |
@@ -244,7 +241,7 @@ int getname(int friendnumber, uint8_t * name) | |||
244 | 241 | ||
245 | int m_set_userstatus(uint8_t *status, uint16_t length) | 242 | int m_set_userstatus(uint8_t *status, uint16_t length) |
246 | { | 243 | { |
247 | if(length > MAX_USERSTATUS_LENGTH) | 244 | if (length > MAX_USERSTATUS_LENGTH) |
248 | return -1; | 245 | return -1; |
249 | uint8_t *newstatus = calloc(length, 1); | 246 | uint8_t *newstatus = calloc(length, 1); |
250 | memcpy(newstatus, status, length); | 247 | memcpy(newstatus, status, length); |
@@ -253,7 +250,7 @@ int m_set_userstatus(uint8_t *status, uint16_t length) | |||
253 | self_userstatus_len = length; | 250 | self_userstatus_len = length; |
254 | 251 | ||
255 | uint32_t i; | 252 | uint32_t i; |
256 | for(i = 0; i < numfriends; ++i) | 253 | for (i = 0; i < numfriends; ++i) |
257 | friendlist[i].userstatus_sent = 0; | 254 | friendlist[i].userstatus_sent = 0; |
258 | return 0; | 255 | return 0; |
259 | } | 256 | } |
@@ -262,7 +259,7 @@ int m_set_userstatus(uint8_t *status, uint16_t length) | |||
262 | guaranteed to be at most MAX_USERSTATUS_LENGTH */ | 259 | guaranteed to be at most MAX_USERSTATUS_LENGTH */ |
263 | int m_get_userstatus_size(int friendnumber) | 260 | int m_get_userstatus_size(int friendnumber) |
264 | { | 261 | { |
265 | if(friendnumber >= numfriends || friendnumber < 0) | 262 | if (friendnumber >= numfriends || friendnumber < 0) |
266 | return -1; | 263 | return -1; |
267 | return friendlist[friendnumber].userstatus_length; | 264 | return friendlist[friendnumber].userstatus_length; |
268 | } | 265 | } |
@@ -271,7 +268,7 @@ int m_get_userstatus_size(int friendnumber) | |||
271 | bytes, use m_get_userstatus_size to find out how much you need to allocate */ | 268 | bytes, use m_get_userstatus_size to find out how much you need to allocate */ |
272 | int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen) | 269 | int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen) |
273 | { | 270 | { |
274 | if(friendnumber >= numfriends || friendnumber < 0) | 271 | if (friendnumber >= numfriends || friendnumber < 0) |
275 | return -1; | 272 | return -1; |
276 | memset(buf, 0, maxlen); | 273 | memset(buf, 0, maxlen); |
277 | memcpy(buf, friendlist[friendnumber].userstatus, MIN(maxlen, MAX_USERSTATUS_LENGTH) - 1); | 274 | memcpy(buf, friendlist[friendnumber].userstatus, MIN(maxlen, MAX_USERSTATUS_LENGTH) - 1); |
@@ -290,7 +287,7 @@ static int send_userstatus(int friendnumber, uint8_t * status, uint16_t length) | |||
290 | 287 | ||
291 | static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t length) | 288 | static int set_friend_userstatus(int friendnumber, uint8_t * status, uint16_t length) |
292 | { | 289 | { |
293 | if(friendnumber >= numfriends || friendnumber < 0) | 290 | if (friendnumber >= numfriends || friendnumber < 0) |
294 | return -1; | 291 | return -1; |
295 | uint8_t *newstatus = calloc(length, 1); | 292 | uint8_t *newstatus = calloc(length, 1); |
296 | memcpy(newstatus, status, length); | 293 | memcpy(newstatus, status, length); |
@@ -350,35 +347,29 @@ int initMessenger() | |||
350 | return 0; | 347 | return 0; |
351 | } | 348 | } |
352 | 349 | ||
350 | //TODO: make this function not suck. | ||
353 | static void doFriends() | 351 | static void doFriends() |
354 | {/* TODO: add incoming connections and some other stuff. */ | 352 | {/* TODO: add incoming connections and some other stuff. */ |
355 | uint32_t i; | 353 | uint32_t i; |
356 | int len; | 354 | int len; |
357 | uint8_t temp[MAX_DATA_SIZE]; | 355 | uint8_t temp[MAX_DATA_SIZE]; |
358 | for(i = 0; i < numfriends; ++i) | 356 | for (i = 0; i < numfriends; ++i) { |
359 | { | 357 | if (friendlist[i].status == 1) { |
360 | if(friendlist[i].status == 1) | ||
361 | { | ||
362 | int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); | 358 | int fr = send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); |
363 | if(fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case | 359 | if (fr == 0) /* TODO: This needs to be fixed so that it sends the friend requests a couple of times in case of packet loss */ |
364 | of packet loss */ | ||
365 | friendlist[i].status = 2; | 360 | friendlist[i].status = 2; |
366 | else | 361 | else if (fr > 0) |
367 | if(fr > 0) | ||
368 | friendlist[i].status = 2; | 362 | friendlist[i].status = 2; |
369 | } | 363 | } |
370 | if(friendlist[i].status == 2 || friendlist[i].status == 3) /* friend is not online */ | 364 | if (friendlist[i].status == 2 || friendlist[i].status == 3) { /* friend is not online */ |
371 | { | 365 | if (friendlist[i].status == 2) { |
372 | if(friendlist[i].status == 2) | 366 | if (friendlist[i].friend_request_id + 10 < unix_time()) { /*I know this is hackish but it should work.*/ |
373 | { | ||
374 | if(friendlist[i].friend_request_id + 10 < unix_time()) /*I know this is hackish but it should work.*/ | ||
375 | { | ||
376 | send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); | 367 | send_friendrequest(friendlist[i].client_id, friendlist[i].info, friendlist[i].info_size); |
377 | friendlist[i].friend_request_id = unix_time(); | 368 | friendlist[i].friend_request_id = unix_time(); |
378 | } | 369 | } |
379 | } | 370 | } |
380 | IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); | 371 | IP_Port friendip = DHT_getfriendip(friendlist[i].client_id); |
381 | switch(is_cryptoconnected(friendlist[i].crypt_connection_id)) { | 372 | switch (is_cryptoconnected(friendlist[i].crypt_connection_id)) { |
382 | case 0: | 373 | case 0: |
383 | if (friendip.ip.i > 1) | 374 | if (friendip.ip.i > 1) |
384 | friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); | 375 | friendlist[i].crypt_connection_id = crypto_connect(friendlist[i].client_id, friendip); |
@@ -394,24 +385,23 @@ static void doFriends() | |||
394 | break; | 385 | break; |
395 | } | 386 | } |
396 | } | 387 | } |
397 | while(friendlist[i].status == 4) /* friend is online */ | 388 | while (friendlist[i].status == 4) { /* friend is online */ |
398 | { | 389 | if (friendlist[i].name_sent == 0) { |
399 | if(friendlist[i].name_sent == 0) | 390 | if (m_sendname(i, self_name)) |
400 | if(m_sendname(i, self_name)) | ||
401 | friendlist[i].name_sent = 1; | 391 | friendlist[i].name_sent = 1; |
402 | if(friendlist[i].userstatus_sent == 0) | 392 | } |
403 | if(send_userstatus(i, self_userstatus, self_userstatus_len)) | 393 | if (friendlist[i].userstatus_sent == 0) { |
394 | if (send_userstatus(i, self_userstatus, self_userstatus_len)) | ||
404 | friendlist[i].userstatus_sent = 1; | 395 | friendlist[i].userstatus_sent = 1; |
396 | } | ||
405 | len = read_cryptpacket(friendlist[i].crypt_connection_id, temp); | 397 | len = read_cryptpacket(friendlist[i].crypt_connection_id, temp); |
406 | if(len > 0) | 398 | if (len > 0) { |
407 | { | 399 | switch (temp[0]) { |
408 | switch(temp[0]) { | ||
409 | case PACKET_ID_NICKNAME: { | 400 | case PACKET_ID_NICKNAME: { |
410 | if (len != MAX_NAME_LENGTH + 1) break; | 401 | if (len != MAX_NAME_LENGTH + 1) |
402 | break; | ||
411 | if(friend_namechange_isset) | 403 | if(friend_namechange_isset) |
412 | { | ||
413 | friend_namechange(i, temp + 1, MAX_NAME_LENGTH); /* TODO: use the actual length */ | 404 | friend_namechange(i, temp + 1, MAX_NAME_LENGTH); /* TODO: use the actual length */ |
414 | } | ||
415 | memcpy(friendlist[i].name, temp + 1, MAX_NAME_LENGTH); | 405 | memcpy(friendlist[i].name, temp + 1, MAX_NAME_LENGTH); |
416 | friendlist[i].name[MAX_NAME_LENGTH - 1] = 0; /* make sure the NULL terminator is present. */ | 406 | friendlist[i].name[MAX_NAME_LENGTH - 1] = 0; /* make sure the NULL terminator is present. */ |
417 | break; | 407 | break; |
@@ -419,25 +409,21 @@ static void doFriends() | |||
419 | case PACKET_ID_USERSTATUS: { | 409 | case PACKET_ID_USERSTATUS: { |
420 | uint8_t *status = calloc(MIN(len - 1, MAX_USERSTATUS_LENGTH), 1); | 410 | uint8_t *status = calloc(MIN(len - 1, MAX_USERSTATUS_LENGTH), 1); |
421 | memcpy(status, temp + 1, MIN(len - 1, MAX_USERSTATUS_LENGTH)); | 411 | memcpy(status, temp + 1, MIN(len - 1, MAX_USERSTATUS_LENGTH)); |
422 | if(friend_statuschange_isset) | 412 | if (friend_statuschange_isset) |
423 | { | 413 | friend_statuschange(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH)); |
424 | friend_statuschange(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH)); | ||
425 | } | ||
426 | set_friend_userstatus(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH)); | 414 | set_friend_userstatus(i, status, MIN(len - 1, MAX_USERSTATUS_LENGTH)); |
427 | free(status); | 415 | free(status); |
428 | break; | 416 | break; |
429 | } | 417 | } |
430 | case PACKET_ID_MESSAGE: { | 418 | case PACKET_ID_MESSAGE: { |
431 | if(friend_message_isset) | 419 | if (friend_message_isset) |
432 | (*friend_message)(i, temp + 1, len - 1); | 420 | (*friend_message)(i, temp + 1, len - 1); |
433 | break; | 421 | break; |
434 | } | 422 | } |
435 | } | 423 | } |
436 | } | 424 | } |
437 | else | 425 | else { |
438 | { | 426 | if (is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) { /* if the connection timed out, kill it */ |
439 | if(is_cryptoconnected(friendlist[i].crypt_connection_id) == 4) /* if the connection timed out, kill it */ | ||
440 | { | ||
441 | crypto_kill(friendlist[i].crypt_connection_id); | 427 | crypto_kill(friendlist[i].crypt_connection_id); |
442 | friendlist[i].crypt_connection_id = -1; | 428 | friendlist[i].crypt_connection_id = -1; |
443 | friendlist[i].status = 3; | 429 | friendlist[i].status = 3; |
@@ -454,11 +440,9 @@ static void doInbound() | |||
454 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 440 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
455 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; | 441 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; |
456 | int inconnection = crypto_inbound(public_key, secret_nonce, session_key); | 442 | int inconnection = crypto_inbound(public_key, secret_nonce, session_key); |
457 | if(inconnection != -1) | 443 | if (inconnection != -1) { |
458 | { | ||
459 | int friend_id = getfriend_id(public_key); | 444 | int friend_id = getfriend_id(public_key); |
460 | if(friend_id != -1) | 445 | if (friend_id != -1) { |
461 | { | ||
462 | crypto_kill(friendlist[friend_id].crypt_connection_id); | 446 | crypto_kill(friendlist[friend_id].crypt_connection_id); |
463 | friendlist[friend_id].crypt_connection_id = | 447 | friendlist[friend_id].crypt_connection_id = |
464 | accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); | 448 | accept_crypto_inbound(inconnection, public_key, secret_nonce, session_key); |
@@ -476,8 +460,7 @@ static uint32_t last_LANdiscovery; | |||
476 | /*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/ | 460 | /*Send a LAN discovery packet every LAN_DISCOVERY_INTERVAL seconds*/ |
477 | static void LANdiscovery() | 461 | static void LANdiscovery() |
478 | { | 462 | { |
479 | if(last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) | 463 | if (last_LANdiscovery + LAN_DISCOVERY_INTERVAL < unix_time()) { |
480 | { | ||
481 | send_LANdiscovery(htons(PORT)); | 464 | send_LANdiscovery(htons(PORT)); |
482 | last_LANdiscovery = unix_time(); | 465 | last_LANdiscovery = unix_time(); |
483 | } | 466 | } |
@@ -490,12 +473,11 @@ void doMessenger() | |||
490 | IP_Port ip_port; | 473 | IP_Port ip_port; |
491 | uint8_t data[MAX_UDP_PACKET_SIZE]; | 474 | uint8_t data[MAX_UDP_PACKET_SIZE]; |
492 | uint32_t length; | 475 | uint32_t length; |
493 | while(receivepacket(&ip_port, data, &length) != -1) | 476 | while (receivepacket(&ip_port, data, &length) != -1) { |
494 | { | ||
495 | #ifdef DEBUG | 477 | #ifdef DEBUG |
496 | /* if(rand() % 3 != 1) //simulate packet loss */ | 478 | /* if(rand() % 3 != 1) //simulate packet loss */ |
497 | /* { */ | 479 | /* { */ |
498 | if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port) && | 480 | if (DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port) && |
499 | friendreq_handlepacket(data, length, ip_port) && LANdiscovery_handlepacket(data, length, ip_port)) | 481 | friendreq_handlepacket(data, length, ip_port) && LANdiscovery_handlepacket(data, length, ip_port)) |
500 | /* if packet is discarded */ | 482 | /* if packet is discarded */ |
501 | printf("Received unhandled packet with length: %u\n", length); | 483 | printf("Received unhandled packet with length: %u\n", length); |
@@ -527,7 +509,7 @@ uint32_t Messenger_size() | |||
527 | } | 509 | } |
528 | 510 | ||
529 | /* save the messenger in data of size Messenger_size() */ | 511 | /* save the messenger in data of size Messenger_size() */ |
530 | void Messenger_save(uint8_t * data) | 512 | void Messenger_save(uint8_t *data) |
531 | { | 513 | { |
532 | save_keys(data); | 514 | save_keys(data); |
533 | data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; | 515 | data += crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES; |
@@ -545,9 +527,9 @@ void Messenger_save(uint8_t * data) | |||
545 | /* load the messenger from data of size length. */ | 527 | /* load the messenger from data of size length. */ |
546 | int Messenger_load(uint8_t * data, uint32_t length) | 528 | int Messenger_load(uint8_t * data, uint32_t length) |
547 | { | 529 | { |
548 | if(length == ~0) | 530 | if (length == ~0) |
549 | return -1; | 531 | return -1; |
550 | if(length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2) | 532 | if (length < crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2) |
551 | return -1; | 533 | return -1; |
552 | length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2; | 534 | length -= crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES + sizeof(uint32_t) * 2; |
553 | load_keys(data); | 535 | load_keys(data); |
@@ -556,15 +538,15 @@ int Messenger_load(uint8_t * data, uint32_t length) | |||
556 | memcpy(&size, data, sizeof(size)); | 538 | memcpy(&size, data, sizeof(size)); |
557 | data += sizeof(size); | 539 | data += sizeof(size); |
558 | 540 | ||
559 | if(length < size) | 541 | if (length < size) |
560 | return -1; | 542 | return -1; |
561 | length -= size; | 543 | length -= size; |
562 | if(DHT_load(data, size) == -1) | 544 | if (DHT_load(data, size) == -1) |
563 | return -1; | 545 | return -1; |
564 | data += size; | 546 | data += size; |
565 | memcpy(&size, data, sizeof(size)); | 547 | memcpy(&size, data, sizeof(size)); |
566 | data += sizeof(size); | 548 | data += sizeof(size); |
567 | if(length != size || length % sizeof(Friend) != 0) | 549 | if (length != size || length % sizeof(Friend) != 0) |
568 | return -1; | 550 | return -1; |
569 | 551 | ||
570 | Friend * temp = malloc(size); | 552 | Friend * temp = malloc(size); |
@@ -573,10 +555,8 @@ int Messenger_load(uint8_t * data, uint32_t length) | |||
573 | uint16_t num = size / sizeof(Friend); | 555 | uint16_t num = size / sizeof(Friend); |
574 | 556 | ||
575 | uint32_t i; | 557 | uint32_t i; |
576 | for(i = 0; i < num; ++i) | 558 | for (i = 0; i < num; ++i) { |
577 | { | 559 | if(temp[i].status != 0) { |
578 | if(temp[i].status != 0) | ||
579 | { | ||
580 | int fnum = m_addfriend_norequest(temp[i].client_id); | 560 | int fnum = m_addfriend_norequest(temp[i].client_id); |
581 | setfriendname(fnum, temp[i].name); | 561 | setfriendname(fnum, temp[i].name); |
582 | /* set_friend_userstatus(fnum, temp[i].userstatus, temp[i].userstatus_length); */ | 562 | /* set_friend_userstatus(fnum, temp[i].userstatus, temp[i].userstatus_length); */ |
diff --git a/core/Messenger.h b/core/Messenger.h index 1067d156..0e2606d9 100644 --- a/core/Messenger.h +++ b/core/Messenger.h | |||
@@ -51,23 +51,23 @@ extern "C" { | |||
51 | data is the data and length is the length | 51 | data is the data and length is the length |
52 | returns the friend number if success | 52 | returns the friend number if success |
53 | return -1 if failure. */ | 53 | return -1 if failure. */ |
54 | int m_addfriend(uint8_t * client_id, uint8_t * data, uint16_t length); | 54 | int m_addfriend(uint8_t *client_id, uint8_t *data, uint16_t length); |
55 | 55 | ||
56 | 56 | ||
57 | /* add a friend without sending a friendrequest. | 57 | /* add a friend without sending a friendrequest. |
58 | returns the friend number if success | 58 | returns the friend number if success |
59 | return -1 if failure. */ | 59 | return -1 if failure. */ |
60 | int m_addfriend_norequest(uint8_t * client_id); | 60 | int m_addfriend_norequest(uint8_t *client_id); |
61 | 61 | ||
62 | /* return the friend id associated to that client id. | 62 | /* return the friend id associated to that client id. |
63 | return -1 if no such friend */ | 63 | return -1 if no such friend */ |
64 | int getfriend_id(uint8_t * client_id); | 64 | int getfriend_id(uint8_t *client_id); |
65 | 65 | ||
66 | /* copies the public key associated to that friend id into client_id buffer. | 66 | /* copies the public key associated to that friend id into client_id buffer. |
67 | make sure that client_id is of size CLIENT_ID_SIZE. | 67 | make sure that client_id is of size CLIENT_ID_SIZE. |
68 | return 0 if success | 68 | return 0 if success |
69 | return -1 if failure */ | 69 | return -1 if failure */ |
70 | int getclient_id(int friend_id, uint8_t * client_id); | 70 | int getclient_id(int friend_id, uint8_t *client_id); |
71 | 71 | ||
72 | /* remove a friend */ | 72 | /* remove a friend */ |
73 | int m_delfriend(int friendnumber); | 73 | int m_delfriend(int friendnumber); |
@@ -82,20 +82,20 @@ int m_friendstatus(int friendnumber); | |||
82 | /* send a text chat message to an online friend | 82 | /* send a text chat message to an online friend |
83 | returns 1 if packet was successfully put into the send queue | 83 | returns 1 if packet was successfully put into the send queue |
84 | return 0 if it was not */ | 84 | return 0 if it was not */ |
85 | int m_sendmessage(int friendnumber, uint8_t * message, uint32_t length); | 85 | int m_sendmessage(int friendnumber, uint8_t *message, uint32_t length); |
86 | 86 | ||
87 | /* Set our nickname | 87 | /* Set our nickname |
88 | name must be a string of maximum MAX_NAME_LENGTH length. | 88 | name must be a string of maximum MAX_NAME_LENGTH length. |
89 | return 0 if success | 89 | return 0 if success |
90 | return -1 if failure */ | 90 | return -1 if failure */ |
91 | int setname(uint8_t * name, uint16_t length); | 91 | int setname(uint8_t *name, uint16_t length); |
92 | 92 | ||
93 | /* get name of friendnumber | 93 | /* get name of friendnumber |
94 | put it in name | 94 | put it in name |
95 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. | 95 | name needs to be a valid memory location with a size of at least MAX_NAME_LENGTH (128) bytes. |
96 | return 0 if success | 96 | return 0 if success |
97 | return -1 if failure */ | 97 | return -1 if failure */ |
98 | int getname(int friendnumber, uint8_t * name); | 98 | int getname(int friendnumber, uint8_t *name); |
99 | 99 | ||
100 | /* set our user status | 100 | /* set our user status |
101 | you are responsible for freeing status after | 101 | you are responsible for freeing status after |
@@ -109,7 +109,7 @@ int m_get_userstatus_size(int friendnumber); | |||
109 | 109 | ||
110 | /* copy friendnumber's userstatus into buf, truncating if size is over maxlen | 110 | /* copy friendnumber's userstatus into buf, truncating if size is over maxlen |
111 | get the size you need to allocate from m_get_userstatus_size */ | 111 | get the size you need to allocate from m_get_userstatus_size */ |
112 | int m_copy_userstatus(int friendnumber, uint8_t * buf, uint32_t maxlen); | 112 | int m_copy_userstatus(int friendnumber, uint8_t *buf, uint32_t maxlen); |
113 | 113 | ||
114 | /* set the function that will be executed when a friend request is received. | 114 | /* set the function that will be executed when a friend request is received. |
115 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ | 115 | function format is function(uint8_t * public_key, uint8_t * data, uint16_t length) */ |
@@ -143,10 +143,10 @@ void doMessenger(); | |||
143 | uint32_t Messenger_size(); | 143 | uint32_t Messenger_size(); |
144 | 144 | ||
145 | /* save the messenger in data (must be allocated memory of size Messenger_size()) */ | 145 | /* save the messenger in data (must be allocated memory of size Messenger_size()) */ |
146 | void Messenger_save(uint8_t * data); | 146 | void Messenger_save(uint8_t *data); |
147 | 147 | ||
148 | /* load the messenger from data of size length */ | 148 | /* load the messenger from data of size length */ |
149 | int Messenger_load(uint8_t * data, uint32_t length); | 149 | int Messenger_load(uint8_t *data, uint32_t length); |
150 | 150 | ||
151 | #ifdef __cplusplus | 151 | #ifdef __cplusplus |
152 | } | 152 | } |
diff --git a/core/friend_requests.c b/core/friend_requests.c index d1b0da57..7a2cdc24 100644 --- a/core/friend_requests.c +++ b/core/friend_requests.c | |||
@@ -35,24 +35,23 @@ int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length) | |||
35 | uint8_t packet[MAX_DATA_SIZE]; | 35 | uint8_t packet[MAX_DATA_SIZE]; |
36 | int len = create_request(packet, public_key, data, length, 32); /* 32 is friend request packet id */ | 36 | int len = create_request(packet, public_key, data, length, 32); /* 32 is friend request packet id */ |
37 | 37 | ||
38 | if(len == -1) | 38 | if (len == -1) |
39 | return -1; | 39 | return -1; |
40 | 40 | ||
41 | IP_Port ip_port = DHT_getfriendip(public_key); | 41 | IP_Port ip_port = DHT_getfriendip(public_key); |
42 | 42 | ||
43 | if(ip_port.ip.i == 1) | 43 | if (ip_port.ip.i == 1) |
44 | return -1; | 44 | return -1; |
45 | 45 | ||
46 | if(ip_port.ip.i != 0) | 46 | if (ip_port.ip.i != 0) { |
47 | { | 47 | if (sendpacket(ip_port, packet, len) != -1) |
48 | if(sendpacket(ip_port, packet, len) != -1) | ||
49 | return 0; | 48 | return 0; |
50 | return -1; | 49 | return -1; |
51 | } | 50 | } |
52 | 51 | ||
53 | int num = route_tofriend(public_key, packet, len); | 52 | int num = route_tofriend(public_key, packet, len); |
54 | 53 | ||
55 | if(num == 0) | 54 | if (num == 0) |
56 | return -1; | 55 | return -1; |
57 | 56 | ||
58 | return num; | 57 | return num; |
@@ -62,8 +61,7 @@ static void (*handle_friendrequest)(uint8_t *, uint8_t *, uint16_t); | |||
62 | static uint8_t handle_friendrequest_isset = 0; | 61 | static uint8_t handle_friendrequest_isset = 0; |
63 | 62 | ||
64 | /* set the function that will be executed when a friend request is received. */ | 63 | /* set the function that will be executed when a friend request is received. */ |
65 | void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) | 64 | void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)) { |
66 | { | ||
67 | handle_friendrequest = function; | 65 | handle_friendrequest = function; |
68 | handle_friendrequest_isset = 1; | 66 | handle_friendrequest_isset = 1; |
69 | } | 67 | } |
@@ -78,9 +76,8 @@ static uint8_t recieved_requests[MAX_RECIEVED_STORED][crypto_box_PUBLICKEYBYTES] | |||
78 | static uint16_t recieved_requests_index; | 76 | static uint16_t recieved_requests_index; |
79 | 77 | ||
80 | /*Add to list of recieved friend requests*/ | 78 | /*Add to list of recieved friend requests*/ |
81 | static void addto_recievedlist(uint8_t * client_id) | 79 | static void addto_recievedlist(uint8_t * client_id) { |
82 | { | 80 | if (recieved_requests_index >= MAX_RECIEVED_STORED) |
83 | if(recieved_requests_index >= MAX_RECIEVED_STORED) | ||
84 | recieved_requests_index = 0; | 81 | recieved_requests_index = 0; |
85 | 82 | ||
86 | memcpy(recieved_requests[recieved_requests_index], client_id, crypto_box_PUBLICKEYBYTES); | 83 | memcpy(recieved_requests[recieved_requests_index], client_id, crypto_box_PUBLICKEYBYTES); |
@@ -89,46 +86,43 @@ static void addto_recievedlist(uint8_t * client_id) | |||
89 | 86 | ||
90 | /* Check if a friend request was already recieved | 87 | /* Check if a friend request was already recieved |
91 | return 0 if not, 1 if we did */ | 88 | return 0 if not, 1 if we did */ |
92 | static int request_recieved(uint8_t * client_id) | 89 | static int request_recieved(uint8_t * client_id) { |
93 | { | ||
94 | uint32_t i; | 90 | uint32_t i; |
95 | 91 | ||
96 | for(i = 0; i < MAX_RECIEVED_STORED; ++i) | 92 | for (i = 0; i < MAX_RECIEVED_STORED; ++i) { |
97 | if(memcmp(recieved_requests[i], client_id, crypto_box_PUBLICKEYBYTES) == 0) | 93 | if (memcmp(recieved_requests[i], client_id, crypto_box_PUBLICKEYBYTES) == 0) |
98 | return 1; | 94 | return 1; |
95 | } | ||
99 | 96 | ||
100 | return 0; | 97 | return 0; |
101 | } | 98 | } |
102 | 99 | ||
103 | 100 | ||
104 | int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) | 101 | int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source) { |
105 | { | 102 | if (packet[0] == 32) { |
106 | 103 | if (length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && | |
107 | if(packet[0] == 32) | 104 | length > MAX_DATA_SIZE + ENCRYPTION_PADDING) |
108 | { | ||
109 | if(length <= crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && | ||
110 | length > MAX_DATA_SIZE + ENCRYPTION_PADDING) | ||
111 | return 1; | 105 | return 1; |
112 | if(memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) /* check if request is for us. */ | 106 | if (memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) {// check if request is for us. |
113 | { | 107 | if (handle_friendrequest_isset == 0) |
114 | if(handle_friendrequest_isset == 0) | ||
115 | return 1; | 108 | return 1; |
116 | 109 | ||
117 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 110 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
118 | uint8_t data[MAX_DATA_SIZE]; | 111 | uint8_t data[MAX_DATA_SIZE]; |
119 | int len = handle_request(public_key, data, packet, length); | 112 | int len = handle_request(public_key, data, packet, length); |
120 | 113 | ||
121 | if(len == -1) | 114 | if (len == -1) |
122 | return 1; | 115 | return 1; |
123 | if(request_recieved(public_key)) | 116 | if (request_recieved(public_key)) |
124 | return 1; | 117 | return 1; |
125 | 118 | ||
126 | addto_recievedlist(public_key); | 119 | addto_recievedlist(public_key); |
127 | (*handle_friendrequest)(public_key, data, len); | 120 | (*handle_friendrequest)(public_key, data, len); |
128 | } | 121 | } |
129 | else /* if request is not for us, try routing it. */ | 122 | else {/* if request is not for us, try routing it. */ |
130 | if(route_packet(packet + 1, packet, length) == length) | 123 | if(route_packet(packet + 1, packet, length) == length) |
131 | return 0; | 124 | return 0; |
125 | } | ||
132 | } | 126 | } |
133 | return 1; | 127 | return 1; |
134 | } | 128 | } |
diff --git a/core/friend_requests.h b/core/friend_requests.h index 29bc2b88..8988403c 100644 --- a/core/friend_requests.h +++ b/core/friend_requests.h | |||
@@ -33,7 +33,7 @@ extern "C" { | |||
33 | 33 | ||
34 | /* Try to send a friendrequest to peer with public_key | 34 | /* Try to send a friendrequest to peer with public_key |
35 | data is the data in the request and length is the length. */ | 35 | data is the data in the request and length is the length. */ |
36 | int send_friendrequest(uint8_t * public_key, uint8_t * data, uint32_t length); | 36 | int send_friendrequest(uint8_t *public_key, uint8_t *data, uint32_t length); |
37 | 37 | ||
38 | /* set the function that will be executed when a friend request for us is received. | 38 | /* set the function that will be executed when a friend request for us is received. |
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) */ |
@@ -42,7 +42,7 @@ void callback_friendrequest(void (*function)(uint8_t *, uint8_t *, uint16_t)); | |||
42 | /* if we receive a packet we call this function so it can be handled. | 42 | /* if we receive a packet we call this function so it can be handled. |
43 | return 0 if packet is handled correctly. | 43 | return 0 if packet is handled correctly. |
44 | return 1 if it didn't handle the packet or if the packet was shit. */ | 44 | return 1 if it didn't handle the packet or if the packet was shit. */ |
45 | int friendreq_handlepacket(uint8_t * packet, uint32_t length, IP_Port source); | 45 | int friendreq_handlepacket(uint8_t *packet, uint32_t length, IP_Port source); |
46 | 46 | ||
47 | #ifdef __cplusplus | 47 | #ifdef __cplusplus |
48 | } | 48 | } |
diff --git a/core/net_crypto.c b/core/net_crypto.c index 28cb83e8..c1571467 100644 --- a/core/net_crypto.c +++ b/core/net_crypto.c | |||
@@ -30,8 +30,7 @@ | |||
30 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | 30 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; |
31 | uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; | 31 | uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; |
32 | 32 | ||
33 | typedef struct | 33 | typedef struct { |
34 | { | ||
35 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* the real public key of the peer. */ | 34 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* the real public key of the peer. */ |
36 | uint8_t recv_nonce[crypto_box_NONCEBYTES]; /* nonce of received packets */ | 35 | uint8_t recv_nonce[crypto_box_NONCEBYTES]; /* nonce of received packets */ |
37 | uint8_t sent_nonce[crypto_box_NONCEBYTES]; /* nonce of sent packets. */ | 36 | uint8_t sent_nonce[crypto_box_NONCEBYTES]; /* nonce of sent packets. */ |
@@ -43,7 +42,7 @@ typedef struct | |||
43 | 4 if the connection is timed out. */ | 42 | 4 if the connection is timed out. */ |
44 | uint16_t number; /* Lossless_UDP connection number corresponding to this connection. */ | 43 | uint16_t number; /* Lossless_UDP connection number corresponding to this connection. */ |
45 | 44 | ||
46 | }Crypto_Connection; | 45 | } Crypto_Connection; |
47 | 46 | ||
48 | #define MAX_CRYPTO_CONNECTIONS 256 | 47 | #define MAX_CRYPTO_CONNECTIONS 256 |
49 | 48 | ||
@@ -58,10 +57,10 @@ static int incoming_connections[MAX_INCOMING]; | |||
58 | public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce | 57 | public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce |
59 | return -1 if there was a problem. | 58 | return -1 if there was a problem. |
60 | return length of encrypted data if everything was fine. */ | 59 | return length of encrypted data if everything was fine. */ |
61 | int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | 60 | int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, |
62 | uint8_t * plain, uint32_t length, uint8_t * encrypted) | 61 | uint8_t *plain, uint32_t length, uint8_t *encrypted) |
63 | { | 62 | { |
64 | if(length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE || length == 0) | 63 | if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE || length == 0) |
65 | return -1; | 64 | return -1; |
66 | 65 | ||
67 | uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES] = {0}; | 66 | uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES] = {0}; |
@@ -73,7 +72,7 @@ int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | |||
73 | crypto_box(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce, public_key, secret_key); | 72 | crypto_box(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce, public_key, secret_key); |
74 | 73 | ||
75 | /* if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero */ | 74 | /* if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero */ |
76 | if(memcmp(temp_encrypted, zeroes, crypto_box_BOXZEROBYTES) != 0) | 75 | if (memcmp(temp_encrypted, zeroes, crypto_box_BOXZEROBYTES) != 0) |
77 | return -1; | 76 | return -1; |
78 | 77 | ||
79 | /* unpad the encrypted message */ | 78 | /* unpad the encrypted message */ |
@@ -85,10 +84,10 @@ int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | |||
85 | public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce | 84 | public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce |
86 | return -1 if there was a problem(decryption failed) | 85 | return -1 if there was a problem(decryption failed) |
87 | return length of plain data if everything was fine. */ | 86 | return length of plain data if everything was fine. */ |
88 | int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | 87 | int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, |
89 | uint8_t * encrypted, uint32_t length, uint8_t * plain) | 88 | uint8_t *encrypted, uint32_t length, uint8_t *plain) |
90 | { | 89 | { |
91 | if(length > MAX_DATA_SIZE || length <= crypto_box_BOXZEROBYTES) | 90 | if (length > MAX_DATA_SIZE || length <= crypto_box_BOXZEROBYTES) |
92 | return -1; | 91 | return -1; |
93 | 92 | ||
94 | uint8_t temp_plain[MAX_DATA_SIZE - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES]; | 93 | uint8_t temp_plain[MAX_DATA_SIZE - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES]; |
@@ -97,12 +96,12 @@ int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | |||
97 | 96 | ||
98 | memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); /* pad the message with 16 0 bytes. */ | 97 | memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); /* pad the message with 16 0 bytes. */ |
99 | 98 | ||
100 | if(crypto_box_open(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES, | 99 | if (crypto_box_open(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES, |
101 | nonce, public_key, secret_key) == -1) | 100 | nonce, public_key, secret_key) == -1) |
102 | return -1; | 101 | return -1; |
103 | 102 | ||
104 | /* if decryption is successful the first crypto_box_ZEROBYTES of the message will be zero */ | 103 | /* if decryption is successful the first crypto_box_ZEROBYTES of the message will be zero */ |
105 | if(memcmp(temp_plain, zeroes, crypto_box_ZEROBYTES) != 0) | 104 | if (memcmp(temp_plain, zeroes, crypto_box_ZEROBYTES) != 0) |
106 | return -1; | 105 | return -1; |
107 | 106 | ||
108 | /* unpad the plain message */ | 107 | /* unpad the plain message */ |
@@ -111,10 +110,10 @@ int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | |||
111 | } | 110 | } |
112 | 111 | ||
113 | /* increment the given nonce by 1 */ | 112 | /* increment the given nonce by 1 */ |
114 | void increment_nonce(uint8_t * nonce) | 113 | void increment_nonce(uint8_t *nonce) |
115 | { | 114 | { |
116 | uint32_t i; | 115 | uint32_t i; |
117 | for(i = 0; i < crypto_box_NONCEBYTES; ++i) { | 116 | for (i = 0; i < crypto_box_NONCEBYTES; ++i) { |
118 | ++nonce[i]; | 117 | ++nonce[i]; |
119 | if(nonce[i] != 0) | 118 | if(nonce[i] != 0) |
120 | break; | 119 | break; |
@@ -122,7 +121,7 @@ void increment_nonce(uint8_t * nonce) | |||
122 | } | 121 | } |
123 | 122 | ||
124 | /* fill the given nonce with random bytes. */ | 123 | /* fill the given nonce with random bytes. */ |
125 | void random_nonce(uint8_t * nonce) | 124 | void random_nonce(uint8_t *nonce) |
126 | { | 125 | { |
127 | uint32_t i, temp; | 126 | uint32_t i, temp; |
128 | for (i = 0; i < crypto_box_NONCEBYTES / 4; ++i) { | 127 | for (i = 0; i < crypto_box_NONCEBYTES / 4; ++i) { |
@@ -134,22 +133,22 @@ void random_nonce(uint8_t * nonce) | |||
134 | /* return 0 if there is no received data in the buffer | 133 | /* return 0 if there is no received data in the buffer |
135 | return -1 if the packet was discarded. | 134 | return -1 if the packet was discarded. |
136 | return length of received data if successful */ | 135 | return length of received data if successful */ |
137 | int read_cryptpacket(int crypt_connection_id, uint8_t * data) | 136 | int read_cryptpacket(int crypt_connection_id, uint8_t *data) |
138 | { | 137 | { |
139 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) | 138 | if (crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) |
140 | return 0; | 139 | return 0; |
141 | if(crypto_connections[crypt_connection_id].status != 3) | 140 | if (crypto_connections[crypt_connection_id].status != 3) |
142 | return 0; | 141 | return 0; |
143 | uint8_t temp_data[MAX_DATA_SIZE]; | 142 | uint8_t temp_data[MAX_DATA_SIZE]; |
144 | int length = read_packet(crypto_connections[crypt_connection_id].number, temp_data); | 143 | int length = read_packet(crypto_connections[crypt_connection_id].number, temp_data); |
145 | if(length == 0) | 144 | if (length == 0) |
146 | return 0; | 145 | return 0; |
147 | if(temp_data[0] != 3) | 146 | if (temp_data[0] != 3) |
148 | return -1; | 147 | return -1; |
149 | int len = decrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key, | 148 | int len = decrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key, |
150 | crypto_connections[crypt_connection_id].sessionsecret_key, | 149 | crypto_connections[crypt_connection_id].sessionsecret_key, |
151 | crypto_connections[crypt_connection_id].recv_nonce, temp_data + 1, length - 1, data); | 150 | crypto_connections[crypt_connection_id].recv_nonce, temp_data + 1, length - 1, data); |
152 | if(len != -1) { | 151 | if (len != -1) { |
153 | increment_nonce(crypto_connections[crypt_connection_id].recv_nonce); | 152 | increment_nonce(crypto_connections[crypt_connection_id].recv_nonce); |
154 | return len; | 153 | return len; |
155 | } | 154 | } |
@@ -158,22 +157,22 @@ int read_cryptpacket(int crypt_connection_id, uint8_t * data) | |||
158 | 157 | ||
159 | /* return 0 if data could not be put in packet queue | 158 | /* return 0 if data could not be put in packet queue |
160 | return 1 if data was put into the queue */ | 159 | return 1 if data was put into the queue */ |
161 | int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length) | 160 | int write_cryptpacket(int crypt_connection_id, uint8_t *data, uint32_t length) |
162 | { | 161 | { |
163 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) | 162 | if (crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) |
164 | return 0; | 163 | return 0; |
165 | if(length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) | 164 | if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE - 1) |
166 | return 0; | 165 | return 0; |
167 | if(crypto_connections[crypt_connection_id].status != 3) | 166 | if (crypto_connections[crypt_connection_id].status != 3) |
168 | return 0; | 167 | return 0; |
169 | uint8_t temp_data[MAX_DATA_SIZE]; | 168 | uint8_t temp_data[MAX_DATA_SIZE]; |
170 | int len = encrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key, | 169 | int len = encrypt_data(crypto_connections[crypt_connection_id].peersessionpublic_key, |
171 | crypto_connections[crypt_connection_id].sessionsecret_key, | 170 | crypto_connections[crypt_connection_id].sessionsecret_key, |
172 | crypto_connections[crypt_connection_id].sent_nonce, data, length, temp_data + 1); | 171 | crypto_connections[crypt_connection_id].sent_nonce, data, length, temp_data + 1); |
173 | if(len == -1) | 172 | if (len == -1) |
174 | return 0; | 173 | return 0; |
175 | temp_data[0] = 3; | 174 | temp_data[0] = 3; |
176 | if(write_packet(crypto_connections[crypt_connection_id].number, temp_data, len + 1) == 0) | 175 | if (write_packet(crypto_connections[crypt_connection_id].number, temp_data, len + 1) == 0) |
177 | return 0; | 176 | return 0; |
178 | increment_nonce(crypto_connections[crypt_connection_id].sent_nonce); | 177 | increment_nonce(crypto_connections[crypt_connection_id].sent_nonce); |
179 | return 1; | 178 | return 1; |
@@ -185,15 +184,15 @@ int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length) | |||
185 | request_id is the id of the request (32 = friend request, 254 = ping request) | 184 | request_id is the id of the request (32 = friend request, 254 = ping request) |
186 | returns -1 on failure | 185 | returns -1 on failure |
187 | returns the length of the created packet on success */ | 186 | returns the length of the created packet on success */ |
188 | int create_request(uint8_t * packet, uint8_t * public_key, uint8_t * data, uint32_t length, uint8_t request_id) | 187 | int create_request(uint8_t *packet, uint8_t *public_key, uint8_t *data, uint32_t length, uint8_t request_id) |
189 | { | 188 | { |
190 | if(MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING) | 189 | if (MAX_DATA_SIZE < length + 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + ENCRYPTION_PADDING) |
191 | return -1; | 190 | return -1; |
192 | uint8_t nonce[crypto_box_NONCEBYTES]; | 191 | uint8_t nonce[crypto_box_NONCEBYTES]; |
193 | random_nonce(nonce); | 192 | random_nonce(nonce); |
194 | int len = encrypt_data(public_key, self_secret_key, nonce, data, length, | 193 | int len = encrypt_data(public_key, self_secret_key, nonce, data, length, |
195 | 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet); | 194 | 1 + crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + packet); |
196 | if(len == -1) | 195 | if (len == -1) |
197 | return -1; | 196 | return -1; |
198 | packet[0] = request_id; | 197 | packet[0] = request_id; |
199 | memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES); | 198 | memcpy(packet + 1, public_key, crypto_box_PUBLICKEYBYTES); |
@@ -207,10 +206,10 @@ int create_request(uint8_t * packet, uint8_t * public_key, uint8_t * data, uint3 | |||
207 | in data if a friend or ping request was sent to us and returns the length of the data. | 206 | in data if a friend or ping request was sent to us and returns the length of the data. |
208 | packet is the request packet and length is its length | 207 | packet is the request packet and length is its length |
209 | return -1 if not valid request. */ | 208 | return -1 if not valid request. */ |
210 | int handle_request(uint8_t * public_key, uint8_t * data, uint8_t * packet, uint16_t length) | 209 | int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t length) |
211 | { | 210 | { |
212 | 211 | ||
213 | if(length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && | 212 | if (length > crypto_box_PUBLICKEYBYTES * 2 + crypto_box_NONCEBYTES + 1 + ENCRYPTION_PADDING && |
214 | length <= MAX_DATA_SIZE + ENCRYPTION_PADDING && | 213 | length <= MAX_DATA_SIZE + ENCRYPTION_PADDING && |
215 | memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) | 214 | memcmp(packet + 1, self_public_key, crypto_box_PUBLICKEYBYTES) == 0) |
216 | { | 215 | { |
@@ -230,7 +229,7 @@ int handle_request(uint8_t * public_key, uint8_t * data, uint8_t * packet, uint1 | |||
230 | /* Send a crypto handshake packet containing an encrypted secret nonce and session public key | 229 | /* Send a crypto handshake packet containing an encrypted secret nonce and session public key |
231 | to peer with connection_id and public_key | 230 | to peer with connection_id and public_key |
232 | the packet is encrypted with a random nonce which is sent in plain text with the packet */ | 231 | the packet is encrypted with a random nonce which is sent in plain text with the packet */ |
233 | int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key) | 232 | int send_cryptohandshake(int connection_id, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key) |
234 | { | 233 | { |
235 | uint8_t temp_data[MAX_DATA_SIZE]; | 234 | uint8_t temp_data[MAX_DATA_SIZE]; |
236 | uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; | 235 | uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; |
@@ -242,7 +241,7 @@ int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secr | |||
242 | 241 | ||
243 | int len = encrypt_data(public_key, self_secret_key, nonce, temp, crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, | 242 | int len = encrypt_data(public_key, self_secret_key, nonce, temp, crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES, |
244 | 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + temp_data); | 243 | 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES + temp_data); |
245 | if(len == -1) | 244 | if (len == -1) |
246 | return 0; | 245 | return 0; |
247 | temp_data[0] = 2; | 246 | temp_data[0] = 2; |
248 | memcpy(temp_data + 1, self_public_key, crypto_box_PUBLICKEYBYTES); | 247 | memcpy(temp_data + 1, self_public_key, crypto_box_PUBLICKEYBYTES); |
@@ -253,16 +252,16 @@ int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secr | |||
253 | /* Extract secret nonce, session public key and public_key from a packet(data) with length length | 252 | /* Extract secret nonce, session public key and public_key from a packet(data) with length length |
254 | return 1 if successful | 253 | return 1 if successful |
255 | return 0 if failure */ | 254 | return 0 if failure */ |
256 | int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce, | 255 | int handle_cryptohandshake(uint8_t *public_key, uint8_t *secret_nonce, |
257 | uint8_t * session_key, uint8_t * data, uint16_t length) | 256 | uint8_t *session_key, uint8_t *data, uint16_t length) |
258 | { | 257 | { |
259 | int pad = (- crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES); | 258 | int pad = (- crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES); |
260 | if(length != 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES | 259 | if (length != 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES |
261 | + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad) | 260 | + crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad) |
262 | { | 261 | { |
263 | return 0; | 262 | return 0; |
264 | } | 263 | } |
265 | if(data[0] != 2) | 264 | if (data[0] != 2) |
266 | return 0; | 265 | return 0; |
267 | uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; | 266 | uint8_t temp[crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES]; |
268 | 267 | ||
@@ -272,7 +271,7 @@ int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce, | |||
272 | data + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, | 271 | data + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES, |
273 | crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad, temp); | 272 | crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES + pad, temp); |
274 | 273 | ||
275 | if(len != crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES) | 274 | if (len != crypto_box_NONCEBYTES + crypto_box_PUBLICKEYBYTES) |
276 | return 0; | 275 | return 0; |
277 | 276 | ||
278 | memcpy(secret_nonce, temp, crypto_box_NONCEBYTES); | 277 | memcpy(secret_nonce, temp, crypto_box_NONCEBYTES); |
@@ -283,33 +282,33 @@ int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce, | |||
283 | /* get crypto connection id from public key of peer | 282 | /* get crypto connection id from public key of peer |
284 | return -1 if there are no connections like we are looking for | 283 | return -1 if there are no connections like we are looking for |
285 | return id if it found it */ | 284 | return id if it found it */ |
286 | int getcryptconnection_id(uint8_t * public_key) | 285 | int getcryptconnection_id(uint8_t *public_key) |
287 | { | 286 | { |
288 | uint32_t i; | 287 | uint32_t i; |
289 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) | 288 | for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) { |
290 | if(crypto_connections[i].status > 0) | 289 | if (crypto_connections[i].status > 0) |
291 | if(memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) | 290 | if (memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) |
292 | return i; | 291 | return i; |
292 | } | ||
293 | return -1; | 293 | return -1; |
294 | } | 294 | } |
295 | 295 | ||
296 | /* Start a secure connection with other peer who has public_key and ip_port | 296 | /* Start a secure connection with other peer who has public_key and ip_port |
297 | returns -1 if failure | 297 | returns -1 if failure |
298 | returns crypt_connection_id of the initialized connection if everything went well. */ | 298 | returns crypt_connection_id of the initialized connection if everything went well. */ |
299 | int crypto_connect(uint8_t * public_key, IP_Port ip_port) | 299 | int crypto_connect(uint8_t *public_key, IP_Port ip_port) |
300 | { | 300 | { |
301 | uint32_t i; | 301 | uint32_t i; |
302 | int id = getcryptconnection_id(public_key); | 302 | int id = getcryptconnection_id(public_key); |
303 | if(id != -1) { | 303 | if (id != -1) { |
304 | IP_Port c_ip = connection_ip(crypto_connections[id].number); | 304 | IP_Port c_ip = connection_ip(crypto_connections[id].number); |
305 | if(c_ip.ip.i == ip_port.ip.i && c_ip.port == ip_port.port) | 305 | if(c_ip.ip.i == ip_port.ip.i && c_ip.port == ip_port.port) |
306 | return -1; | 306 | return -1; |
307 | } | 307 | } |
308 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) | 308 | for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) { |
309 | { | 309 | if (crypto_connections[i].status == 0) { |
310 | if(crypto_connections[i].status == 0) { | ||
311 | int id = new_connection(ip_port); | 310 | int id = new_connection(ip_port); |
312 | if(id == -1) | 311 | if (id == -1) |
313 | return -1; | 312 | return -1; |
314 | crypto_connections[i].number = id; | 313 | crypto_connections[i].number = id; |
315 | crypto_connections[i].status = 1; | 314 | crypto_connections[i].status = 1; |
@@ -317,8 +316,8 @@ int crypto_connect(uint8_t * public_key, IP_Port ip_port) | |||
317 | memcpy(crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES); | 316 | memcpy(crypto_connections[i].public_key, public_key, crypto_box_PUBLICKEYBYTES); |
318 | crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key); | 317 | crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key); |
319 | 318 | ||
320 | if(send_cryptohandshake(id, public_key, crypto_connections[i].recv_nonce, | 319 | if (send_cryptohandshake(id, public_key, crypto_connections[i].recv_nonce, |
321 | crypto_connections[i].sessionpublic_key) == 1) | 320 | crypto_connections[i].sessionpublic_key) == 1) |
322 | { | 321 | { |
323 | increment_nonce(crypto_connections[i].recv_nonce); | 322 | increment_nonce(crypto_connections[i].recv_nonce); |
324 | return i; | 323 | return i; |
@@ -336,21 +335,20 @@ int crypto_connect(uint8_t * public_key, IP_Port ip_port) | |||
336 | and the session public key for the connection in session_key | 335 | and the session public key for the connection in session_key |
337 | to accept it see: accept_crypto_inbound(...) | 336 | to accept it see: accept_crypto_inbound(...) |
338 | to refuse it just call kill_connection(...) on the connection id */ | 337 | to refuse it just call kill_connection(...) on the connection id */ |
339 | int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key) | 338 | int crypto_inbound(uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key) |
340 | { | 339 | { |
341 | uint32_t i; | 340 | uint32_t i; |
342 | for(i = 0; i < MAX_INCOMING; ++i) | 341 | for (i = 0; i < MAX_INCOMING; ++i) { |
343 | { | 342 | if (incoming_connections[i] != -1) { |
344 | if(incoming_connections[i] != -1) { | 343 | if (is_connected(incoming_connections[i]) == 4 || is_connected(incoming_connections[i]) == 0) { |
345 | if(is_connected(incoming_connections[i]) == 4 || is_connected(incoming_connections[i]) == 0) { | ||
346 | kill_connection(incoming_connections[i]); | 344 | kill_connection(incoming_connections[i]); |
347 | incoming_connections[i] = -1; | 345 | incoming_connections[i] = -1; |
348 | continue; | 346 | continue; |
349 | } | 347 | } |
350 | if(id_packet(incoming_connections[i]) == 2) { | 348 | if (id_packet(incoming_connections[i]) == 2) { |
351 | uint8_t temp_data[MAX_DATA_SIZE]; | 349 | uint8_t temp_data[MAX_DATA_SIZE]; |
352 | uint16_t len = read_packet(incoming_connections[i], temp_data); | 350 | uint16_t len = read_packet(incoming_connections[i], temp_data); |
353 | if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) { | 351 | if (handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) { |
354 | int connection_id = incoming_connections[i]; | 352 | int connection_id = incoming_connections[i]; |
355 | incoming_connections[i] = -1; /* remove this connection from the incoming connection list. */ | 353 | incoming_connections[i] = -1; /* remove this connection from the incoming connection list. */ |
356 | return connection_id; | 354 | return connection_id; |
@@ -366,9 +364,9 @@ int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * sessi | |||
366 | return 1 if there was a problem. */ | 364 | return 1 if there was a problem. */ |
367 | int crypto_kill(int crypt_connection_id) | 365 | int crypto_kill(int crypt_connection_id) |
368 | { | 366 | { |
369 | if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) | 367 | if (crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) |
370 | return 1; | 368 | return 1; |
371 | if(crypto_connections[crypt_connection_id].status != 0) { | 369 | if (crypto_connections[crypt_connection_id].status != 0) { |
372 | crypto_connections[crypt_connection_id].status = 0; | 370 | crypto_connections[crypt_connection_id].status = 0; |
373 | kill_connection(crypto_connections[crypt_connection_id].number); | 371 | kill_connection(crypto_connections[crypt_connection_id].number); |
374 | crypto_connections[crypt_connection_id].number = ~0; | 372 | crypto_connections[crypt_connection_id].number = ~0; |
@@ -380,18 +378,17 @@ int crypto_kill(int crypt_connection_id) | |||
380 | /* accept an incoming connection using the parameters provided by crypto_inbound | 378 | /* accept an incoming connection using the parameters provided by crypto_inbound |
381 | return -1 if not successful | 379 | return -1 if not successful |
382 | returns the crypt_connection_id if successful */ | 380 | returns the crypt_connection_id if successful */ |
383 | int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key) | 381 | int accept_crypto_inbound(int connection_id, uint8_t *public_key, uint8_t *secret_nonce, uint8_t *session_key) |
384 | { | 382 | { |
385 | uint32_t i; | 383 | uint32_t i; |
386 | if(connection_id == -1) | 384 | if (connection_id == -1) |
387 | return -1; | 385 | return -1; |
388 | /* | 386 | /* |
389 | if(getcryptconnection_id(public_key) != -1) | 387 | if(getcryptconnection_id(public_key) != -1) |
390 | { | 388 | { |
391 | return -1; | 389 | return -1; |
392 | }*/ | 390 | }*/ |
393 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) | 391 | for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) { |
394 | { | ||
395 | if(crypto_connections[i].status == 0) { | 392 | if(crypto_connections[i].status == 0) { |
396 | crypto_connections[i].number = connection_id; | 393 | crypto_connections[i].number = connection_id; |
397 | crypto_connections[i].status = 2; | 394 | crypto_connections[i].status = 2; |
@@ -403,7 +400,7 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec | |||
403 | 400 | ||
404 | crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key); | 401 | crypto_box_keypair(crypto_connections[i].sessionpublic_key, crypto_connections[i].sessionsecret_key); |
405 | 402 | ||
406 | if(send_cryptohandshake(connection_id, public_key, crypto_connections[i].recv_nonce, | 403 | if (send_cryptohandshake(connection_id, public_key, crypto_connections[i].recv_nonce, |
407 | crypto_connections[i].sessionpublic_key) == 1) | 404 | crypto_connections[i].sessionpublic_key) == 1) |
408 | { | 405 | { |
409 | increment_nonce(crypto_connections[i].recv_nonce); | 406 | increment_nonce(crypto_connections[i].recv_nonce); |
@@ -424,7 +421,7 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec | |||
424 | 4 if the connection is timed out and waiting to be killed */ | 421 | 4 if the connection is timed out and waiting to be killed */ |
425 | int is_cryptoconnected(int crypt_connection_id) | 422 | int is_cryptoconnected(int crypt_connection_id) |
426 | { | 423 | { |
427 | if(crypt_connection_id >= 0 && crypt_connection_id < MAX_CRYPTO_CONNECTIONS) | 424 | if (crypt_connection_id >= 0 && crypt_connection_id < MAX_CRYPTO_CONNECTIONS) |
428 | return crypto_connections[crypt_connection_id].status; | 425 | return crypto_connections[crypt_connection_id].status; |
429 | return 0; | 426 | return 0; |
430 | } | 427 | } |
@@ -438,7 +435,7 @@ void new_keys() | |||
438 | 435 | ||
439 | /* save the public and private keys to the keys array | 436 | /* save the public and private keys to the keys array |
440 | Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ | 437 | Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ |
441 | void save_keys(uint8_t * keys) | 438 | void save_keys(uint8_t *keys) |
442 | { | 439 | { |
443 | memcpy(keys, self_public_key, crypto_box_PUBLICKEYBYTES); | 440 | memcpy(keys, self_public_key, crypto_box_PUBLICKEYBYTES); |
444 | memcpy(keys + crypto_box_PUBLICKEYBYTES, self_secret_key, crypto_box_SECRETKEYBYTES); | 441 | memcpy(keys + crypto_box_PUBLICKEYBYTES, self_secret_key, crypto_box_SECRETKEYBYTES); |
@@ -446,7 +443,7 @@ void save_keys(uint8_t * keys) | |||
446 | 443 | ||
447 | /* load the public and private keys from the keys array | 444 | /* load the public and private keys from the keys array |
448 | Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ | 445 | Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */ |
449 | void load_keys(uint8_t * keys) | 446 | void load_keys(uint8_t *keys) |
450 | { | 447 | { |
451 | memcpy(self_public_key, keys, crypto_box_PUBLICKEYBYTES); | 448 | memcpy(self_public_key, keys, crypto_box_PUBLICKEYBYTES); |
452 | memcpy(self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES); | 449 | memcpy(self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES); |
@@ -459,8 +456,8 @@ void load_keys(uint8_t * keys) | |||
459 | int new_incoming(int id) | 456 | int new_incoming(int id) |
460 | { | 457 | { |
461 | uint32_t i; | 458 | uint32_t i; |
462 | for(i = 0; i < MAX_INCOMING; ++i) { | 459 | for (i = 0; i < MAX_INCOMING; ++i) { |
463 | if(incoming_connections[i] == -1) { | 460 | if (incoming_connections[i] == -1) { |
464 | incoming_connections[i] = id; | 461 | incoming_connections[i] = id; |
465 | return 0; | 462 | return 0; |
466 | } | 463 | } |
@@ -473,7 +470,7 @@ int new_incoming(int id) | |||
473 | static void handle_incomings() | 470 | static void handle_incomings() |
474 | { | 471 | { |
475 | int income; | 472 | int income; |
476 | while(1) { | 473 | while (1) { |
477 | income = incoming_connection(); | 474 | income = incoming_connection(); |
478 | if(income == -1 || new_incoming(income) ) | 475 | if(income == -1 || new_incoming(income) ) |
479 | break; | 476 | break; |
@@ -484,22 +481,20 @@ static void handle_incomings() | |||
484 | static void receive_crypto() | 481 | static void receive_crypto() |
485 | { | 482 | { |
486 | uint32_t i; | 483 | uint32_t i; |
487 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) | 484 | for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) { |
488 | { | 485 | if (crypto_connections[i].status == 1) { |
489 | if(crypto_connections[i].status == 1) | ||
490 | { | ||
491 | uint8_t temp_data[MAX_DATA_SIZE]; | 486 | uint8_t temp_data[MAX_DATA_SIZE]; |
492 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; | 487 | uint8_t secret_nonce[crypto_box_NONCEBYTES]; |
493 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; | 488 | uint8_t public_key[crypto_box_PUBLICKEYBYTES]; |
494 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; | 489 | uint8_t session_key[crypto_box_PUBLICKEYBYTES]; |
495 | uint16_t len; | 490 | uint16_t len; |
496 | if(id_packet(crypto_connections[i].number) == 1) | 491 | if (id_packet(crypto_connections[i].number) == 1) |
497 | /* if the packet is a friend request drop it (because we are already friends) */ | 492 | /* if the packet is a friend request drop it (because we are already friends) */ |
498 | len = read_packet(crypto_connections[i].number, temp_data); | 493 | len = read_packet(crypto_connections[i].number, temp_data); |
499 | if(id_packet(crypto_connections[i].number) == 2) { /* handle handshake packet. */ | 494 | if (id_packet(crypto_connections[i].number) == 2) { /* handle handshake packet. */ |
500 | len = read_packet(crypto_connections[i].number, temp_data); | 495 | len = read_packet(crypto_connections[i].number, temp_data); |
501 | if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) { | 496 | if (handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) { |
502 | if(memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) { | 497 | if (memcmp(public_key, crypto_connections[i].public_key, crypto_box_PUBLICKEYBYTES) == 0) { |
503 | memcpy(crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); | 498 | memcpy(crypto_connections[i].sent_nonce, secret_nonce, crypto_box_NONCEBYTES); |
504 | memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES); | 499 | memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES); |
505 | increment_nonce(crypto_connections[i].sent_nonce); | 500 | increment_nonce(crypto_connections[i].sent_nonce); |
@@ -510,15 +505,12 @@ static void receive_crypto() | |||
510 | } | 505 | } |
511 | } | 506 | } |
512 | } | 507 | } |
513 | else if(id_packet(crypto_connections[i].number) != -1) | 508 | else if (id_packet(crypto_connections[i].number) != -1) // This should not happen kill the connection if it does |
514 | /* This should not happen | ||
515 | kill the connection if it does */ | ||
516 | crypto_kill(crypto_connections[i].number); | 509 | crypto_kill(crypto_connections[i].number); |
517 | 510 | ||
518 | } | 511 | } |
519 | if(crypto_connections[i].status == 2) | 512 | if (crypto_connections[i].status == 2) { |
520 | { | 513 | if (id_packet(crypto_connections[i].number) == 3) { |
521 | if(id_packet(crypto_connections[i].number) == 3) { | ||
522 | uint8_t temp_data[MAX_DATA_SIZE]; | 514 | uint8_t temp_data[MAX_DATA_SIZE]; |
523 | uint8_t data[MAX_DATA_SIZE]; | 515 | uint8_t data[MAX_DATA_SIZE]; |
524 | int length = read_packet(crypto_connections[i].number, temp_data); | 516 | int length = read_packet(crypto_connections[i].number, temp_data); |
@@ -526,7 +518,7 @@ static void receive_crypto() | |||
526 | crypto_connections[i].sessionsecret_key, | 518 | crypto_connections[i].sessionsecret_key, |
527 | crypto_connections[i].recv_nonce, temp_data + 1, length - 1, data); | 519 | crypto_connections[i].recv_nonce, temp_data + 1, length - 1, data); |
528 | uint32_t zero = 0; | 520 | uint32_t zero = 0; |
529 | if(len == sizeof(uint32_t) && memcmp(((uint8_t *)&zero), data, sizeof(uint32_t)) == 0) { | 521 | if (len == sizeof(uint32_t) && memcmp(((uint8_t *)&zero), data, sizeof(uint32_t)) == 0) { |
530 | increment_nonce(crypto_connections[i].recv_nonce); | 522 | increment_nonce(crypto_connections[i].recv_nonce); |
531 | crypto_connections[i].status = 3; | 523 | crypto_connections[i].status = 3; |
532 | 524 | ||
@@ -534,9 +526,7 @@ static void receive_crypto() | |||
534 | kill_connection_in(crypto_connections[i].number, 3000000); | 526 | kill_connection_in(crypto_connections[i].number, 3000000); |
535 | } | 527 | } |
536 | else | 528 | else |
537 | /* This should not happen | 529 | crypto_kill(crypto_connections[i].number); // This should not happen kill the connection if it does |
538 | kill the connection if it does */ | ||
539 | crypto_kill(crypto_connections[i].number); | ||
540 | } | 530 | } |
541 | else if(id_packet(crypto_connections[i].number) != -1) | 531 | else if(id_packet(crypto_connections[i].number) != -1) |
542 | /* This should not happen | 532 | /* This should not happen |
@@ -553,17 +543,17 @@ void initNetCrypto() | |||
553 | memset(crypto_connections, 0 ,sizeof(crypto_connections)); | 543 | memset(crypto_connections, 0 ,sizeof(crypto_connections)); |
554 | memset(incoming_connections, -1 ,sizeof(incoming_connections)); | 544 | memset(incoming_connections, -1 ,sizeof(incoming_connections)); |
555 | uint32_t i; | 545 | uint32_t i; |
556 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) | 546 | for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) |
557 | crypto_connections[i].number = ~0; | 547 | crypto_connections[i].number = ~0; |
558 | } | 548 | } |
559 | 549 | ||
560 | static void killTimedout() | 550 | static void killTimedout() |
561 | { | 551 | { |
562 | uint32_t i; | 552 | uint32_t i; |
563 | for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) { | 553 | for (i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i) { |
564 | if(crypto_connections[i].status != 0 && is_connected(crypto_connections[i].number) == 4) | 554 | if (crypto_connections[i].status != 0 && is_connected(crypto_connections[i].number) == 4) |
565 | crypto_connections[i].status = 4; | 555 | crypto_connections[i].status = 4; |
566 | else if(is_connected(crypto_connections[i].number) == 4) { | 556 | else if (is_connected(crypto_connections[i].number) == 4) { |
567 | kill_connection(crypto_connections[i].number); | 557 | kill_connection(crypto_connections[i].number); |
568 | crypto_connections[i].number = ~0; | 558 | crypto_connections[i].number = ~0; |
569 | } | 559 | } |
diff --git a/core/net_crypto.h b/core/net_crypto.h index b5bab17a..d4fe1313 100644 --- a/core/net_crypto.h +++ b/core/net_crypto.h | |||
@@ -40,29 +40,29 @@ extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; | |||
40 | public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce | 40 | public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce |
41 | return -1 if there was a problem. | 41 | return -1 if there was a problem. |
42 | return length of encrypted data if everything was fine. */ | 42 | return length of encrypted data if everything was fine. */ |
43 | int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | 43 | int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, |
44 | uint8_t * plain, uint32_t length, uint8_t * encrypted); | 44 | uint8_t *plain, uint32_t length, uint8_t *encrypted); |
45 | 45 | ||
46 | 46 | ||
47 | /* decrypts encrypted of length length to plain of length length - 16 using the | 47 | /* decrypts encrypted of length length to plain of length length - 16 using the |
48 | public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce | 48 | public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce |
49 | return -1 if there was a problem(decryption failed) | 49 | return -1 if there was a problem(decryption failed) |
50 | return length of plain data if everything was fine. */ | 50 | return length of plain data if everything was fine. */ |
51 | int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | 51 | int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, |
52 | uint8_t * encrypted, uint32_t length, uint8_t * plain); | 52 | uint8_t *encrypted, uint32_t length, uint8_t *plain); |
53 | 53 | ||
54 | 54 | ||
55 | /* fill the given nonce with random bytes. */ | 55 | /* fill the given nonce with random bytes. */ |
56 | void random_nonce(uint8_t * nonce); | 56 | void random_nonce(uint8_t *nonce); |
57 | 57 | ||
58 | /* return 0 if there is no received data in the buffer | 58 | /* return 0 if there is no received data in the buffer |
59 | return -1 if the packet was discarded. | 59 | return -1 if the packet was discarded. |
60 | return length of received data if successful */ | 60 | return length of received data if successful */ |
61 | int read_cryptpacket(int crypt_connection_id, uint8_t * data); | 61 | int read_cryptpacket(int crypt_connection_id, uint8_t *data); |
62 | 62 | ||
63 | /* return 0 if data could not be put in packet queue | 63 | /* return 0 if data could not be put in packet queue |
64 | return 1 if data was put into the queue */ | 64 | return 1 if data was put into the queue */ |
65 | int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length); | 65 | int write_cryptpacket(int crypt_connection_id, uint8_t *data, uint32_t length); |
66 | 66 | ||
67 | /* create a request to peer with public_key. | 67 | /* create a request to peer with public_key. |
68 | packet must be an array of MAX_DATA_SIZE big. | 68 | packet must be an array of MAX_DATA_SIZE big. |
@@ -70,18 +70,18 @@ int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length); | |||
70 | request_id is the id of the request (32 = friend request, 254 = ping request) | 70 | request_id is the id of the request (32 = friend request, 254 = ping request) |
71 | returns -1 on failure | 71 | returns -1 on failure |
72 | returns the length of the created packet on success */ | 72 | returns the length of the created packet on success */ |
73 | int create_request(uint8_t * packet, uint8_t * public_key, uint8_t * data, uint32_t length, uint8_t request_id); | 73 | int create_request(uint8_t *packet, uint8_t * public_key, uint8_t *data, uint32_t length, uint8_t request_id); |
74 | 74 | ||
75 | /* puts the senders public key in the request in public_key, the data from the request | 75 | /* puts the senders public key in the request in public_key, the data from the request |
76 | in data if a friend or ping request was sent to us and returns the length of the data. | 76 | in data if a friend or ping request was sent to us and returns the length of the data. |
77 | packet is the request packet and length is its length | 77 | packet is the request packet and length is its length |
78 | return -1 if not valid request. */ | 78 | return -1 if not valid request. */ |
79 | int handle_request(uint8_t * public_key, uint8_t * data, uint8_t * packet, uint16_t length); | 79 | int handle_request(uint8_t *public_key, uint8_t *data, uint8_t *packet, uint16_t length); |
80 | 80 | ||
81 | /* Start a secure connection with other peer who has public_key and ip_port | 81 | /* Start a secure connection with other peer who has public_key and ip_port |
82 | returns -1 if failure | 82 | returns -1 if failure |
83 | returns crypt_connection_id of the initialized connection if everything went well. */ | 83 | returns crypt_connection_id of the initialized connection if everything went well. */ |
84 | int crypto_connect(uint8_t * public_key, IP_Port ip_port); | 84 | int crypto_connect(uint8_t *public_key, IP_Port ip_port); |
85 | 85 | ||
86 | /* kill a crypto connection | 86 | /* kill a crypto connection |
87 | return 0 if killed successfully | 87 | return 0 if killed successfully |
@@ -95,12 +95,12 @@ int crypto_kill(int crypt_connection_id); | |||
95 | and the session public key for the connection in session_key | 95 | and the session public key for the connection in session_key |
96 | to accept it see: accept_crypto_inbound(...) | 96 | to accept it see: accept_crypto_inbound(...) |
97 | to refuse it just call kill_connection(...) on the connection id */ | 97 | to refuse it just call kill_connection(...) on the connection id */ |
98 | int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key); | 98 | int crypto_inbound(uint8_t *public_key, uint8_t * secret_nonce, uint8_t *session_key); |
99 | 99 | ||
100 | /* accept an incoming connection using the parameters provided by crypto_inbound | 100 | /* accept an incoming connection using the parameters provided by crypto_inbound |
101 | return -1 if not successful | 101 | return -1 if not successful |
102 | returns the crypt_connection_id if successful */ | 102 | returns the crypt_connection_id if successful */ |
103 | int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key); | 103 | int accept_crypto_inbound(int connection_id, uint8_t *public_key, uint8_t * secret_nonce, uint8_t *session_key); |
104 | 104 | ||
105 | /* return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet | 105 | /* return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet |
106 | (we have received a handshake but no empty data packet), 3 if the connection is established. | 106 | (we have received a handshake but no empty data packet), 3 if the connection is established. |
diff --git a/core/network.c b/core/network.c index ec234593..8804ab4e 100644 --- a/core/network.c +++ b/core/network.c | |||
@@ -79,13 +79,10 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length) | |||
79 | #else | 79 | #else |
80 | uint32_t addrlen = sizeof(addr); | 80 | uint32_t addrlen = sizeof(addr); |
81 | #endif | 81 | #endif |
82 | (*(int32_t *)length) = recvfrom(sock,(char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); | 82 | (*(int32_t*)length) = recvfrom(sock,(char*) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr*)&addr, &addrlen); |
83 | if(*(int32_t *)length <= 0) | 83 | if (*(int32_t*)length <= 0) |
84 | { | 84 | return -1; /* nothing received or empty packet */ |
85 | /* nothing received | 85 | |
86 | or empty packet */ | ||
87 | return -1; | ||
88 | } | ||
89 | ip_port->ip = addr.ip; | 86 | ip_port->ip = addr.ip; |
90 | ip_port->port = addr.port; | 87 | ip_port->port = addr.port; |
91 | return 0; | 88 | return 0; |
@@ -101,7 +98,7 @@ int init_networking(IP ip, uint16_t port) | |||
101 | { | 98 | { |
102 | #ifdef WIN32 | 99 | #ifdef WIN32 |
103 | WSADATA wsaData; | 100 | WSADATA wsaData; |
104 | if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) | 101 | if (WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) |
105 | return -1; | 102 | return -1; |
106 | #else | 103 | #else |
107 | srandom((uint32_t)current_time()); | 104 | srandom((uint32_t)current_time()); |
diff --git a/core/network.h b/core/network.h index eaf12003..a59d7c38 100644 --- a/core/network.h +++ b/core/network.h | |||
@@ -69,23 +69,20 @@ extern "C" { | |||
69 | 69 | ||
70 | #define MAX_UDP_PACKET_SIZE 65507 | 70 | #define MAX_UDP_PACKET_SIZE 65507 |
71 | 71 | ||
72 | typedef union | 72 | typedef union { |
73 | { | ||
74 | uint8_t c[4]; | 73 | uint8_t c[4]; |
75 | uint16_t s[2]; | 74 | uint16_t s[2]; |
76 | uint32_t i; | 75 | uint32_t i; |
77 | }IP; | 76 | } IP; |
78 | 77 | ||
79 | typedef struct | 78 | typedef struct { |
80 | { | ||
81 | IP ip; | 79 | IP ip; |
82 | uint16_t port; | 80 | uint16_t port; |
83 | /* not used for anything right now */ | 81 | /* not used for anything right now */ |
84 | uint16_t padding; | 82 | uint16_t padding; |
85 | }IP_Port; | 83 | } IP_Port; |
86 | 84 | ||
87 | typedef struct | 85 | typedef struct { |
88 | { | ||
89 | int16_t family; | 86 | int16_t family; |
90 | uint16_t port; | 87 | uint16_t port; |
91 | IP ip; | 88 | IP ip; |
@@ -93,7 +90,7 @@ typedef struct | |||
93 | #ifdef ENABLE_IPV6 | 90 | #ifdef ENABLE_IPV6 |
94 | uint8_t zeroes2[12]; | 91 | uint8_t zeroes2[12]; |
95 | #endif | 92 | #endif |
96 | }ADDR; | 93 | } ADDR; |
97 | 94 | ||
98 | /* returns current time in milleseconds since the epoch. */ | 95 | /* returns current time in milleseconds since the epoch. */ |
99 | uint64_t current_time(); | 96 | uint64_t current_time(); |
@@ -105,12 +102,12 @@ uint32_t random_int(); | |||
105 | /* Basic network functions: */ | 102 | /* Basic network functions: */ |
106 | 103 | ||
107 | /* Function to send packet(data) of length length to ip_port */ | 104 | /* Function to send packet(data) of length length to ip_port */ |
108 | int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length); | 105 | int sendpacket(IP_Port ip_port, uint8_t *data, uint32_t length); |
109 | 106 | ||
110 | /* Function to receive data, ip and port of sender is put into ip_port | 107 | /* Function to receive data, ip and port of sender is put into ip_port |
111 | the packet data into data | 108 | the packet data into data |
112 | the packet length into length. */ | 109 | the packet length into length. */ |
113 | int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length); | 110 | int receivepacket(IP_Port *ip_port, uint8_t *data, uint32_t *length); |
114 | 111 | ||
115 | /* initialize networking | 112 | /* initialize networking |
116 | bind to ip and port | 113 | bind to ip and port |
@@ -118,7 +115,7 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length); | |||
118 | port is in host byte order (this means don't worry about it) | 115 | port is in host byte order (this means don't worry about it) |
119 | returns 0 if no problems | 116 | returns 0 if no problems |
120 | returns -1 if there were problems */ | 117 | returns -1 if there were problems */ |
121 | int init_networking(IP ip ,uint16_t port); | 118 | int init_networking(IP ip, uint16_t port); |
122 | 119 | ||
123 | /* function to cleanup networking stuff(doesn't do much right now) */ | 120 | /* function to cleanup networking stuff(doesn't do much right now) */ |
124 | void shutdown_networking(); | 121 | void shutdown_networking(); |
diff --git a/testing/DHT_sendfiletest.c b/testing/DHT_sendfiletest.c index 5b5d6c9f..873a73f8 100644 --- a/testing/DHT_sendfiletest.c +++ b/testing/DHT_sendfiletest.c | |||
@@ -96,78 +96,72 @@ int main(int argc, char *argv[]) | |||
96 | uint8_t buffer2[128]; | 96 | uint8_t buffer2[128]; |
97 | int read2 = 0; | 97 | int read2 = 0; |
98 | FILE *file1 = fopen(argv[4], "rb"); | 98 | FILE *file1 = fopen(argv[4], "rb"); |
99 | if ( file1==NULL ){printf("Error opening file.\n");return 1;} | 99 | if (file1 == NULL) { |
100 | printf("Error opening file.\n"); | ||
101 | return 1; | ||
102 | } | ||
100 | FILE *file2 = fopen("received.txt", "wb"); | 103 | FILE *file2 = fopen("received.txt", "wb"); |
101 | if ( file2==NULL ){return 1;} | 104 | if (file2 == NULL) |
105 | return 1; | ||
106 | |||
102 | read1 = fread(buffer1, 1, 128, file1); | 107 | read1 = fread(buffer1, 1, 128, file1); |
103 | 108 | ||
104 | while(1) { | 109 | while (1) { |
105 | 110 | while (receivepacket(&ip_port, data, &length) != -1) { | |
106 | while(receivepacket(&ip_port, data, &length) != -1) { | 111 | if (rand() % 3 != 1) { /* simulate packet loss */ |
107 | if(rand() % 3 != 1) { /* simulate packet loss */ | 112 | if (DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) |
108 | if(DHT_handlepacket(data, length, ip_port) && LosslessUDP_handlepacket(data, length, ip_port)) { | 113 | printf("Received unhandled packet with length: %u\n", length); /* if packet is not recognized */ |
109 | /* if packet is not recognized */ | 114 | else |
110 | printf("Received unhandled packet with length: %u\n", length); | ||
111 | } else { | ||
112 | printf("Received handled packet with length: %u\n", length); | 115 | printf("Received handled packet with length: %u\n", length); |
113 | } | ||
114 | } | 116 | } |
115 | } | 117 | } |
116 | friend_ip = DHT_getfriendip((uint8_t *)argv[3]); | 118 | friend_ip = DHT_getfriendip((uint8_t *)argv[3]); |
117 | if(friend_ip.ip.i != 0) { | 119 | if (friend_ip.ip.i != 0) { |
118 | if(connection == -1) { | 120 | if (connection == -1) { |
119 | printf("Started connecting to friend:"); | 121 | printf("Started connecting to friend:"); |
120 | printip(friend_ip); | 122 | printip(friend_ip); |
121 | connection = new_connection(friend_ip); | 123 | connection = new_connection(friend_ip); |
122 | } | 124 | } |
123 | } | 125 | } |
124 | if(inconnection == -1) { | 126 | if (inconnection == -1) { |
125 | inconnection = incoming_connection(); | 127 | inconnection = incoming_connection(); |
126 | if(inconnection != -1) { | 128 | if (inconnection != -1) { |
127 | printf("Someone connected to us:"); | 129 | printf("Someone connected to us:"); |
128 | printip(connection_ip(inconnection)); | 130 | printip(connection_ip(inconnection)); |
129 | } | 131 | } |
130 | } | 132 | } |
131 | /* if someone connected to us write what he sends to a file */ | 133 | /* if someone connected to us write what he sends to a file */ |
132 | /* also send him our file. */ | 134 | /* also send him our file. */ |
133 | if(inconnection != -1) { | 135 | if (inconnection != -1) { |
134 | if(write_packet(inconnection, buffer1, read1)) { | 136 | if (write_packet(inconnection, buffer1, read1)) { |
135 | printf("Wrote data.\n"); | 137 | printf("Wrote data.\n"); |
136 | read1 = fread(buffer1, 1, 128, file1); | 138 | read1 = fread(buffer1, 1, 128, file1); |
137 | } | 139 | } |
138 | read2 = read_packet(inconnection, buffer2); | 140 | read2 = read_packet(inconnection, buffer2); |
139 | if(read2 != 0) { | 141 | if (read2 != 0) { |
140 | printf("Received data.\n"); | 142 | printf("Received data.\n"); |
141 | if(!fwrite(buffer2, read2, 1, file2)) { | 143 | if (!fwrite(buffer2, read2, 1, file2)) |
142 | printf("file write error\n"); | 144 | printf("file write error\n"); |
143 | } | 145 | if (read2 < 128) { |
144 | if(read2 < 128) { | ||
145 | fclose(file2); | 146 | fclose(file2); |
146 | } | 147 | } |
147 | } | 148 | } |
148 | } | 149 | } |
149 | /* if we are connected to a friend send him data from the file. | 150 | /* if we are connected to a friend send him data from the file. |
150 | * also put what he sends us in a file. */ | 151 | * also put what he sends us in a file. */ |
151 | if(is_connected(connection) == 3) | 152 | if (is_connected(connection) == 3) { |
152 | { | 153 | if (write_packet(0, buffer1, read1)) { |
153 | if(write_packet(0, buffer1, read1)) | ||
154 | { | ||
155 | printf("Wrote data.\n"); | 154 | printf("Wrote data.\n"); |
156 | read1 = fread(buffer1, 1, 128, file1); | 155 | read1 = fread(buffer1, 1, 128, file1); |
157 | } | 156 | } |
158 | read2 = read_packet(0, buffer2); | 157 | read2 = read_packet(0, buffer2); |
159 | if(read2 != 0) | 158 | if (read2 != 0) { |
160 | { | ||
161 | printf("Received data.\n"); | 159 | printf("Received data.\n"); |
162 | if(!fwrite(buffer2, read2, 1, file2)) | 160 | if(!fwrite(buffer2, read2, 1, file2)) |
163 | { | 161 | printf("file write error\n"); |
164 | printf("file write error\n"); | ||
165 | } | ||
166 | if(read2 < 128) | 162 | if(read2 < 128) |
167 | { | ||
168 | fclose(file2); | 163 | fclose(file2); |
169 | } | 164 | } |
170 | } | ||
171 | } | 165 | } |
172 | doDHT(); | 166 | doDHT(); |
173 | doLossless_UDP(); | 167 | doLossless_UDP(); |
diff --git a/testing/Lossless_UDP_testclient.c b/testing/Lossless_UDP_testclient.c index 8f23528c..c1eaa69d 100644 --- a/testing/Lossless_UDP_testclient.c +++ b/testing/Lossless_UDP_testclient.c | |||
@@ -44,13 +44,13 @@ | |||
44 | 44 | ||
45 | #define PORT 33446 | 45 | #define PORT 33446 |
46 | 46 | ||
47 | void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port) | 47 | void printpacket(uint8_t *data, uint32_t length, IP_Port ip_port) |
48 | { | 48 | { |
49 | uint32_t i; | 49 | uint32_t i; |
50 | printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); | 50 | printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); |
51 | printf("--------------------BEGIN-----------------------------\n"); | 51 | printf("--------------------BEGIN-----------------------------\n"); |
52 | for(i = 0; i < length; i++) { | 52 | for (i = 0; i < length; i++) { |
53 | if(data[i] < 16) | 53 | if (data[i] < 16) |
54 | printf("0"); | 54 | printf("0"); |
55 | printf("%hhX",data[i]); | 55 | printf("%hhX",data[i]); |
56 | } | 56 | } |
@@ -59,7 +59,7 @@ void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port) | |||
59 | 59 | ||
60 | void printip(IP_Port ip_port) | 60 | void printip(IP_Port ip_port) |
61 | { | 61 | { |
62 | printf("\nIP: %u.%u.%u.%u Port: %u",ip_port.ip.c[0],ip_port.ip.c[1],ip_port.ip.c[2],ip_port.ip.c[3],ntohs(ip_port.port)); | 62 | printf("\nIP: %u.%u.%u.%u Port: %u", ip_port.ip.c[0], ip_port.ip.c[1], ip_port.ip.c[2], ip_port.ip.c[3], ntohs(ip_port.port)); |
63 | } | 63 | } |
64 | /* | 64 | /* |
65 | void printpackets(Data test) | 65 | void printpackets(Data test) |
@@ -123,16 +123,15 @@ void Lossless_UDP() | |||
123 | IP_Port ip_port; | 123 | IP_Port ip_port; |
124 | uint8_t data[MAX_UDP_PACKET_SIZE]; | 124 | uint8_t data[MAX_UDP_PACKET_SIZE]; |
125 | uint32_t length; | 125 | uint32_t length; |
126 | while(receivepacket(&ip_port, data, &length) != -1) { | 126 | while (receivepacket(&ip_port, data, &length) != -1) { |
127 | printf("packet with length: %u\n", length); | 127 | printf("packet with length: %u\n", length); |
128 | /* if(rand() % 3 != 1)//add packet loss | 128 | /* if(rand() % 3 != 1)//add packet loss |
129 | { */ | 129 | { */ |
130 | if(LosslessUDP_handlepacket(data, length, ip_port)) { | 130 | if (LosslessUDP_handlepacket(data, length, ip_port)) |
131 | printpacket(data, length, ip_port); | 131 | printpacket(data, length, ip_port); |
132 | } else { | 132 | else |
133 | //printconnection(0); | 133 | printf("Received handled packet with length: %u\n", length); //printconnection(0); |
134 | printf("Received handled packet with length: %u\n", length); | 134 | |
135 | } | ||
136 | /* } */ | 135 | /* } */ |
137 | } | 136 | } |
138 | 137 | ||
@@ -151,7 +150,8 @@ int main(int argc, char *argv[]) | |||
151 | int read; | 150 | int read; |
152 | 151 | ||
153 | FILE *file = fopen(argv[3], "rb"); | 152 | FILE *file = fopen(argv[3], "rb"); |
154 | if ( file==NULL ){return 1;} | 153 | if (file == NULL) |
154 | return 1; | ||
155 | 155 | ||
156 | 156 | ||
157 | /* initialize networking */ | 157 | /* initialize networking */ |
@@ -166,14 +166,14 @@ int main(int argc, char *argv[]) | |||
166 | printip(serverip); | 166 | printip(serverip); |
167 | int connection = new_connection(serverip); | 167 | int connection = new_connection(serverip); |
168 | uint64_t timer = current_time(); | 168 | uint64_t timer = current_time(); |
169 | while(1) { | 169 | while (1) { |
170 | /* printconnection(connection); */ | 170 | /* printconnection(connection); */ |
171 | Lossless_UDP(); | 171 | Lossless_UDP(); |
172 | if(is_connected(connection) == 3) { | 172 | if (is_connected(connection) == 3) { |
173 | printf("Connecting took: %llu us\n", (unsigned long long)(current_time() - timer)); | 173 | printf("Connecting took: %llu us\n", (unsigned long long)(current_time() - timer)); |
174 | break; | 174 | break; |
175 | } | 175 | } |
176 | if(is_connected(connection) == 0) { | 176 | if (is_connected(connection) == 0) { |
177 | printf("Connection timeout after: %llu us\n", (unsigned long long)(current_time() - timer)); | 177 | printf("Connection timeout after: %llu us\n", (unsigned long long)(current_time() - timer)); |
178 | return 1; | 178 | return 1; |
179 | } | 179 | } |
@@ -185,19 +185,19 @@ int main(int argc, char *argv[]) | |||
185 | /*read first part of file */ | 185 | /*read first part of file */ |
186 | read = fread(buffer, 1, 512, file); | 186 | read = fread(buffer, 1, 512, file); |
187 | 187 | ||
188 | while(1) { | 188 | while (1) { |
189 | /* printconnection(connection); */ | 189 | /* printconnection(connection); */ |
190 | Lossless_UDP(); | 190 | Lossless_UDP(); |
191 | if(is_connected(connection) == 3) { | 191 | if (is_connected(connection) == 3) { |
192 | 192 | ||
193 | if(write_packet(connection, buffer, read)) { | 193 | if (write_packet(connection, buffer, read)) { |
194 | /* printf("Wrote data.\n"); */ | 194 | /* printf("Wrote data.\n"); */ |
195 | read = fread(buffer, 1, 512, file); | 195 | read = fread(buffer, 1, 512, file); |
196 | 196 | ||
197 | } | 197 | } |
198 | /* printf("%u\n", sendqueue(connection)); */ | 198 | /* printf("%u\n", sendqueue(connection)); */ |
199 | if(sendqueue(connection) == 0) { | 199 | if (sendqueue(connection) == 0) { |
200 | if(read == 0) { | 200 | if (read == 0) { |
201 | printf("Sent file successfully in: %llu us\n", (unsigned long long)(current_time() - timer)); | 201 | printf("Sent file successfully in: %llu us\n", (unsigned long long)(current_time() - timer)); |
202 | break; | 202 | break; |
203 | } | 203 | } |
diff --git a/testing/Lossless_UDP_testserver.c b/testing/Lossless_UDP_testserver.c index a8097048..4b437f0e 100644 --- a/testing/Lossless_UDP_testserver.c +++ b/testing/Lossless_UDP_testserver.c | |||
@@ -45,12 +45,12 @@ | |||
45 | 45 | ||
46 | #define PORT 33445 | 46 | #define PORT 33445 |
47 | 47 | ||
48 | void printpacket(uint8_t * data, uint32_t length, IP_Port ip_port) | 48 | void printpacket(uint8_t *data, uint32_t length, IP_Port ip_port) |
49 | { | 49 | { |
50 | uint32_t i; | 50 | uint32_t i; |
51 | printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); | 51 | printf("UNHANDLED PACKET RECEIVED\nLENGTH:%u\nCONTENTS:\n", length); |
52 | printf("--------------------BEGIN-----------------------------\n"); | 52 | printf("--------------------BEGIN-----------------------------\n"); |
53 | for(i = 0; i < length; i++) { | 53 | for (i = 0; i < length; i++) { |
54 | if(data[i] < 16) | 54 | if(data[i] < 16) |
55 | printf("0"); | 55 | printf("0"); |
56 | printf("%hhX",data[i]); | 56 | printf("%hhX",data[i]); |
@@ -120,10 +120,10 @@ void Lossless_UDP() | |||
120 | IP_Port ip_port; | 120 | IP_Port ip_port; |
121 | uint8_t data[MAX_UDP_PACKET_SIZE]; | 121 | uint8_t data[MAX_UDP_PACKET_SIZE]; |
122 | uint32_t length; | 122 | uint32_t length; |
123 | while(receivepacket(&ip_port, data, &length) != -1) { | 123 | while (receivepacket(&ip_port, data, &length) != -1) { |
124 | //if(rand() % 3 != 1)//add packet loss | 124 | //if(rand() % 3 != 1)//add packet loss |
125 | //{ | 125 | //{ |
126 | if(LosslessUDP_handlepacket(data, length, ip_port)) { | 126 | if (LosslessUDP_handlepacket(data, length, ip_port)) { |
127 | printpacket(data, length, ip_port); | 127 | printpacket(data, length, ip_port); |
128 | } else { | 128 | } else { |
129 | //printconnection(0); | 129 | //printconnection(0); |
@@ -147,7 +147,8 @@ int main(int argc, char *argv[]) | |||
147 | int read; | 147 | int read; |
148 | 148 | ||
149 | FILE *file = fopen(argv[1], "wb"); | 149 | FILE *file = fopen(argv[1], "wb"); |
150 | if ( file==NULL ){return 1;} | 150 | if (file == NULL) |
151 | return 1; | ||
151 | 152 | ||
152 | 153 | ||
153 | //initialize networking | 154 | //initialize networking |
@@ -161,7 +162,7 @@ int main(int argc, char *argv[]) | |||
161 | uint64_t timer = current_time(); | 162 | uint64_t timer = current_time(); |
162 | 163 | ||
163 | 164 | ||
164 | while(1) { | 165 | while (1) { |
165 | Lossless_UDP(); | 166 | Lossless_UDP(); |
166 | connection = incoming_connection(); | 167 | connection = incoming_connection(); |
167 | if(connection != -1) { | 168 | if(connection != -1) { |
@@ -176,17 +177,16 @@ int main(int argc, char *argv[]) | |||
176 | 177 | ||
177 | timer = current_time(); | 178 | timer = current_time(); |
178 | 179 | ||
179 | while(1) { | 180 | while (1) { |
180 | //printconnection(0); | 181 | //printconnection(0); |
181 | Lossless_UDP(); | 182 | Lossless_UDP(); |
182 | if(is_connected(connection) >= 2) { | 183 | if (is_connected(connection) >= 2) { |
183 | kill_connection_in(connection, 3000000); | 184 | kill_connection_in(connection, 3000000); |
184 | read = read_packet(connection, buffer); | 185 | read = read_packet(connection, buffer); |
185 | if(read != 0) { | 186 | if (read != 0) { |
186 | // printf("Recieved data.\n"); | 187 | // printf("Recieved data.\n"); |
187 | if(!fwrite(buffer, read, 1, file)) { | 188 | if (!fwrite(buffer, read, 1, file)) |
188 | printf("file write error\n"); | 189 | printf("file write error\n"); |
189 | } | ||
190 | } | 190 | } |
191 | } | 191 | } |
192 | if(is_connected(connection) == 4) { | 192 | if(is_connected(connection) == 4) { |
diff --git a/testing/nTox.c b/testing/nTox.c index 3a5e9bb3..27654191 100644 --- a/testing/nTox.c +++ b/testing/nTox.c | |||
@@ -39,9 +39,9 @@ int nick_before; | |||
39 | void new_lines(char *line) | 39 | void new_lines(char *line) |
40 | { | 40 | { |
41 | int i; | 41 | int i; |
42 | for (i = HISTORY-1; i > 0; i--) { | 42 | for (i = HISTORY-1; i > 0; i--) |
43 | strcpy(lines[i],lines[i-1]); | 43 | strcpy(lines[i],lines[i-1]); |
44 | } | 44 | |
45 | strcpy(lines[0],line); | 45 | strcpy(lines[0],line); |
46 | do_refresh(); | 46 | do_refresh(); |
47 | } | 47 | } |
@@ -53,8 +53,7 @@ unsigned char * hex_string_to_bin(char hex_string[]) | |||
53 | unsigned char * val = malloc(len); | 53 | unsigned char * val = malloc(len); |
54 | char * pos = hex_string; | 54 | char * pos = hex_string; |
55 | int i=0; | 55 | int i=0; |
56 | while(i < len) | 56 | while(i < len) { |
57 | { | ||
58 | sscanf(pos,"%2hhx",&val[i]); | 57 | sscanf(pos,"%2hhx",&val[i]); |
59 | pos+=2; | 58 | pos+=2; |
60 | i++; | 59 | i++; |
@@ -71,17 +70,18 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) | |||
71 | if (line[1] == 'f') { // add friend command: /f ID | 70 | if (line[1] == 'f') { // add friend command: /f ID |
72 | int i; | 71 | int i; |
73 | char temp_id[128]; | 72 | char temp_id[128]; |
74 | for (i=0; i<128; i++) { | 73 | for (i=0; i<128; i++) |
75 | temp_id[i] = line[i+3]; | 74 | temp_id[i] = line[i+3]; |
76 | } | ||
77 | int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); | 75 | int num = m_addfriend(hex_string_to_bin(temp_id), (uint8_t*)"Install Gentoo", sizeof("Install Gentoo")); |
78 | char numstring[100]; | 76 | char numstring[100]; |
79 | sprintf(numstring, "[i] added friend %d", num); | 77 | sprintf(numstring, "[i] added friend %d", num); |
80 | new_lines(numstring); | 78 | new_lines(numstring); |
81 | do_refresh(); | 79 | do_refresh(); |
82 | } else if (line[1] == 'd') { | 80 | } |
81 | else if (line[1] == 'd') { | ||
83 | doMessenger(); | 82 | doMessenger(); |
84 | } else if (line[1] == 'm') { //message command: /m friendnumber messsage | 83 | } |
84 | else if (line[1] == 'm') { //message command: /m friendnumber messsage | ||
85 | int i; | 85 | int i; |
86 | size_t len = strlen(line); | 86 | size_t len = strlen(line); |
87 | char numstring[len-3]; | 87 | char numstring[len-3]; |
@@ -91,15 +91,15 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) | |||
91 | numstring[i] = line[i+3]; | 91 | numstring[i] = line[i+3]; |
92 | } else { | 92 | } else { |
93 | int j; | 93 | int j; |
94 | for (j=i+1; j<len; j++) { | 94 | for (j=i+1; j<len; j++) |
95 | message[j-i-1] = line[j+3]; | 95 | message[j-i-1] = line[j+3]; |
96 | } | ||
97 | break; | 96 | break; |
98 | } | 97 | } |
99 | } | 98 | } |
100 | int num = atoi(numstring); | 99 | int num = atoi(numstring); |
101 | m_sendmessage(num, (uint8_t*) message, sizeof(message)); | 100 | m_sendmessage(num, (uint8_t*) message, sizeof(message)); |
102 | } else if (line[1] == 'n') { | 101 | } |
102 | else if (line[1] == 'n') { | ||
103 | uint8_t name[MAX_NAME_LENGTH]; | 103 | uint8_t name[MAX_NAME_LENGTH]; |
104 | int i = 0; | 104 | int i = 0; |
105 | size_t len = strlen(line); | 105 | size_t len = strlen(line); |
@@ -112,7 +112,8 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) | |||
112 | char numstring[100]; | 112 | char numstring[100]; |
113 | sprintf(numstring, "[i] changed nick to %s", (char*)name); | 113 | sprintf(numstring, "[i] changed nick to %s", (char*)name); |
114 | new_lines(numstring); | 114 | new_lines(numstring); |
115 | } else if (line[1] == 's') { | 115 | } |
116 | else if (line[1] == 's') { | ||
116 | uint8_t status[MAX_USERSTATUS_LENGTH]; | 117 | uint8_t status[MAX_USERSTATUS_LENGTH]; |
117 | int i = 0; | 118 | int i = 0; |
118 | size_t len = strlen(line); | 119 | size_t len = strlen(line); |
@@ -125,7 +126,8 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line) | |||
125 | char numstring[100]; | 126 | char numstring[100]; |
126 | sprintf(numstring, "[i] changed status to %s", (char*)status); | 127 | sprintf(numstring, "[i] changed status to %s", (char*)status); |
127 | new_lines(numstring); | 128 | new_lines(numstring); |
128 | } else if (line[1] == 'q') { //exit | 129 | } |
130 | else if (line[1] == 'q') { //exit | ||
129 | endwin(); | 131 | endwin(); |
130 | exit(EXIT_SUCCESS); | 132 | exit(EXIT_SUCCESS); |
131 | } | 133 | } |
@@ -139,7 +141,7 @@ void wrap(char output[STRING_LENGTH], char input[STRING_LENGTH], int line_width) | |||
139 | int i = 0; | 141 | int i = 0; |
140 | strcpy(output,input); | 142 | strcpy(output,input); |
141 | size_t len = strlen(output); | 143 | size_t len = strlen(output); |
142 | for (i=line_width; i < len; i = i + line_width) { | 144 | for (i = line_width; i < len; i = i + line_width) { |
143 | while (output[i] != ' ' && i != 0) { | 145 | while (output[i] != ' ' && i != 0) { |
144 | i--; | 146 | i--; |
145 | } | 147 | } |
@@ -155,9 +157,8 @@ int count_lines(char *string) | |||
155 | int i; | 157 | int i; |
156 | int count = 1; | 158 | int count = 1; |
157 | for (i=0; i < len; i++) { | 159 | for (i=0; i < len; i++) { |
158 | if (string[i] == '\n') { | 160 | if (string[i] == '\n') |
159 | count++; | 161 | count++; |
160 | } | ||
161 | } | 162 | } |
162 | return count; | 163 | return count; |
163 | } | 164 | } |
@@ -196,11 +197,11 @@ void do_refresh() | |||
196 | refresh(); | 197 | refresh(); |
197 | } | 198 | } |
198 | 199 | ||
199 | void print_request(uint8_t * public_key, uint8_t * data, uint16_t length) | 200 | void print_request(uint8_t *public_key, uint8_t *data, uint16_t length) |
200 | { | 201 | { |
201 | new_lines("[i] received friend request"); | 202 | new_lines("[i] received friend request"); |
202 | do_refresh(); | 203 | do_refresh(); |
203 | if(memcmp(data , "Install Gentoo", sizeof("Install Gentoo")) == 0 ) | 204 | if (memcmp(data , "Install Gentoo", sizeof("Install Gentoo")) == 0) |
204 | //if the request contained the message of peace the person is obviously a friend so we add him. | 205 | //if the request contained the message of peace the person is obviously a friend so we add him. |
205 | { | 206 | { |
206 | new_lines("[i] friend request accepted"); | 207 | new_lines("[i] friend request accepted"); |
@@ -298,15 +299,14 @@ int main(int argc, char *argv[]) | |||
298 | uint32_t i; | 299 | uint32_t i; |
299 | for(i = 0; i < 32; i++) | 300 | for(i = 0; i < 32; i++) |
300 | { | 301 | { |
301 | if(self_public_key[i] < 16) { | 302 | if(self_public_key[i] < 16) |
302 | strcpy(idstring1[i],"0"); | 303 | strcpy(idstring1[i],"0"); |
303 | } else { | 304 | else |
304 | strcpy(idstring1[i], ""); | 305 | strcpy(idstring1[i], ""); |
305 | } | ||
306 | sprintf(idstring2[i], "%hhX",self_public_key[i]); | 306 | sprintf(idstring2[i], "%hhX",self_public_key[i]); |
307 | } | 307 | } |
308 | strcpy(idstring0,"[i] your ID: "); | 308 | strcpy(idstring0,"[i] your ID: "); |
309 | for(i=0; i<32; i++) { | 309 | for (i=0; i<32; i++) { |
310 | strcat(idstring0,idstring1[i]); | 310 | strcat(idstring0,idstring1[i]); |
311 | strcat(idstring0,idstring2[i]); | 311 | strcat(idstring0,idstring2[i]); |
312 | } | 312 | } |
@@ -320,11 +320,11 @@ int main(int argc, char *argv[]) | |||
320 | IP_Port bootstrap_ip_port; | 320 | IP_Port bootstrap_ip_port; |
321 | bootstrap_ip_port.port = htons(atoi(argv[2])); | 321 | bootstrap_ip_port.port = htons(atoi(argv[2])); |
322 | int resolved_address = resolve_addr(argv[1]); | 322 | int resolved_address = resolve_addr(argv[1]); |
323 | if (resolved_address != -1) { | 323 | if (resolved_address != -1) |
324 | bootstrap_ip_port.ip.i = resolved_address; | 324 | bootstrap_ip_port.ip.i = resolved_address; |
325 | } else { | 325 | else |
326 | exit(1); | 326 | exit(1); |
327 | } | 327 | |
328 | DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); | 328 | DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); |
329 | nodelay(stdscr, TRUE); | 329 | nodelay(stdscr, TRUE); |
330 | while(true) { | 330 | while(true) { |