summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorAndrew <AndehMor@gmail.com>2013-07-20 10:39:05 -0400
committerAndrew <AndehMor@gmail.com>2013-07-20 10:39:05 -0400
commit5101ef756a18baf5d7e794d15577fa73ef2a18fa (patch)
tree1ef0695b89d90d4bf3db60ef33a242f060a6256b /core
parente02620c7bed8764421b9b4c258e594369615da39 (diff)
Fixed comments in other files as per request of jvrv
Diffstat (limited to 'core')
-rw-r--r--core/net_crypto.c248
-rw-r--r--core/net_crypto.h108
-rw-r--r--core/network.c56
-rw-r--r--core/network.h215
4 files changed, 332 insertions, 295 deletions
diff --git a/core/net_crypto.c b/core/net_crypto.c
index 62df614e..c819c8c4 100644
--- a/core/net_crypto.c
+++ b/core/net_crypto.c
@@ -28,23 +28,23 @@
28#include "net_crypto.h" 28#include "net_crypto.h"
29 29
30 30
31//Our public and secret keys. 31/* Our public and secret keys. */
32uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; 32uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
33uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; 33uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
34 34
35 35
36typedef struct 36typedef struct
37{ 37{
38 uint8_t public_key[crypto_box_PUBLICKEYBYTES];//the real public key of the peer. 38 uint8_t public_key[crypto_box_PUBLICKEYBYTES]; /* the real public key of the peer. */
39 uint8_t recv_nonce[crypto_box_NONCEBYTES];//nonce of received packets 39 uint8_t recv_nonce[crypto_box_NONCEBYTES]; /* nonce of received packets */
40 uint8_t sent_nonce[crypto_box_NONCEBYTES];//nonce of sent packets. 40 uint8_t sent_nonce[crypto_box_NONCEBYTES]; /* nonce of sent packets. */
41 uint8_t sessionpublic_key[crypto_box_PUBLICKEYBYTES];//our public key for this session. 41 uint8_t sessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* our public key for this session. */
42 uint8_t sessionsecret_key[crypto_box_SECRETKEYBYTES];//our private key for this session. 42 uint8_t sessionsecret_key[crypto_box_SECRETKEYBYTES];our private key for this session.
43 uint8_t peersessionpublic_key[crypto_box_PUBLICKEYBYTES];//The public key of the peer. 43 uint8_t peersessionpublic_key[crypto_box_PUBLICKEYBYTES]; /* The public key of the peer. */
44 uint8_t status;//0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet 44 uint8_t status;/* 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet
45 //(we have received a handshake but no empty data packet), 3 if the connection is established. 45 (we have received a handshake but no empty data packet), 3 if the connection is established.
46 //4 if the connection is timed out. 46 4 if the connection is timed out. */
47 uint16_t number; //Lossless_UDP connection number corresponding to this connection. 47 uint16_t number; /* Lossless_UDP connection number corresponding to this connection. */
48 48
49}Crypto_Connection; 49}Crypto_Connection;
50 50
@@ -54,18 +54,18 @@ static Crypto_Connection crypto_connections[MAX_CRYPTO_CONNECTIONS];
54 54
55#define MAX_FRIEND_REQUESTS 32 55#define MAX_FRIEND_REQUESTS 32
56 56
57//keeps track of the connection numbers for friends request so we can check later if they were sent 57/* keeps track of the connection numbers for friends request so we can check later if they were sent */
58static int outbound_friendrequests[MAX_FRIEND_REQUESTS]; 58static int outbound_friendrequests[MAX_FRIEND_REQUESTS];
59 59
60#define MAX_INCOMING 64 60#define MAX_INCOMING 64
61 61
62//keeps track of the connection numbers for friends request so we can check later if they were sent 62/* keeps track of the connection numbers for friends request so we can check later if they were sent */
63static int incoming_connections[MAX_INCOMING]; 63static int incoming_connections[MAX_INCOMING];
64 64
65//encrypts plain of length length to encrypted of length + 16 using the 65/* encrypts plain of length length to encrypted of length + 16 using the
66//public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce 66 public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce
67//return -1 if there was a problem. 67 return -1 if there was a problem.
68//return length of encrypted data if everything was fine. 68 return length of encrypted data if everything was fine. */
69int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, 69int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
70 uint8_t * plain, uint32_t length, uint8_t * encrypted) 70 uint8_t * plain, uint32_t length, uint8_t * encrypted)
71{ 71{
@@ -78,24 +78,24 @@ int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
78 uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES]; 78 uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES];
79 uint8_t zeroes[crypto_box_BOXZEROBYTES] = {0}; 79 uint8_t zeroes[crypto_box_BOXZEROBYTES] = {0};
80 80
81 memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length);//pad the message with 32 0 bytes. 81 memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length); /* pad the message with 32 0 bytes. */
82 82
83 crypto_box(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce, public_key, secret_key); 83 crypto_box(temp_encrypted, temp_plain, length + crypto_box_ZEROBYTES, nonce, public_key, secret_key);
84 84
85 //if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero 85 /* if encryption is successful the first crypto_box_BOXZEROBYTES of the message will be zero */
86 if(memcmp(temp_encrypted, zeroes, crypto_box_BOXZEROBYTES) != 0) 86 if(memcmp(temp_encrypted, zeroes, crypto_box_BOXZEROBYTES) != 0)
87 { 87 {
88 return -1; 88 return -1;
89 } 89 }
90 //unpad the encrypted message 90 /* unpad the encrypted message */
91 memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES); 91 memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES);
92 return length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES; 92 return length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES;
93} 93}
94 94
95//decrypts encrypted of length length to plain of length length - 16 using the 95/* decrypts encrypted of length length to plain of length length - 16 using the
96//public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce 96 public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce
97//return -1 if there was a problem(decryption failed) 97 return -1 if there was a problem(decryption failed)
98//return length of plain data if everything was fine. 98 return length of plain data if everything was fine. */
99int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, 99int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
100 uint8_t * encrypted, uint32_t length, uint8_t * plain) 100 uint8_t * encrypted, uint32_t length, uint8_t * plain)
101{ 101{
@@ -107,30 +107,30 @@ int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
107 uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES] = {0}; 107 uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES] = {0};
108 uint8_t zeroes[crypto_box_ZEROBYTES] = {0}; 108 uint8_t zeroes[crypto_box_ZEROBYTES] = {0};
109 109
110 memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length);//pad the message with 16 0 bytes. 110 memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); /* pad the message with 16 0 bytes. */
111 111
112 if(crypto_box_open(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES, 112 if(crypto_box_open(temp_plain, temp_encrypted, length + crypto_box_BOXZEROBYTES,
113 nonce, public_key, secret_key) == -1) 113 nonce, public_key, secret_key) == -1)
114 { 114 {
115 return -1; 115 return -1;
116 } 116 }
117 //if decryption is successful the first crypto_box_ZEROBYTES of the message will be zero 117 /* if decryption is successful the first crypto_box_ZEROBYTES of the message will be zero */
118 if(memcmp(temp_plain, zeroes, crypto_box_ZEROBYTES) != 0) 118 if(memcmp(temp_plain, zeroes, crypto_box_ZEROBYTES) != 0)
119 { 119 {
120 return -1; 120 return -1;
121 } 121 }
122 //unpad the plain message 122 /* unpad the plain message */
123 memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES); 123 memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES);
124 return length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES; 124 return length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES;
125} 125}
126 126
127//increment the given nonce by 1 127/* increment the given nonce by 1 */
128void increment_nonce(uint8_t * nonce) 128void increment_nonce(uint8_t * nonce)
129{ 129{
130 uint32_t i; 130 uint32_t i;
131 for(i = 0; i < crypto_box_NONCEBYTES; i++) 131 for(i = 0; i < crypto_box_NONCEBYTES; ++i)
132 { 132 {
133 nonce[i]++; 133 ++nonce[i];
134 if(nonce[i] != 0) 134 if(nonce[i] != 0)
135 { 135 {
136 break; 136 break;
@@ -138,20 +138,20 @@ void increment_nonce(uint8_t * nonce)
138 } 138 }
139} 139}
140 140
141//fill the given nonce with random bytes. 141/* fill the given nonce with random bytes.
142//TODO: make this more optimized 142 TODO: make this more optimized */
143void random_nonce(uint8_t * nonce) 143void random_nonce(uint8_t * nonce)
144{ 144{
145 uint32_t i; 145 uint32_t i;
146 for(i = 0; i < crypto_box_NONCEBYTES; i++) 146 for(i = 0; i < crypto_box_NONCEBYTES; ++i)
147 { 147 {
148 nonce[i] = random_int() % 256; 148 nonce[i] = random_int() % 256;
149 } 149 }
150} 150}
151 151
152//return 0 if there is no received data in the buffer 152/* return 0 if there is no received data in the buffer
153//return -1 if the packet was discarded. 153 return -1 if the packet was discarded.
154//return length of received data if successful 154 return length of received data if successful */
155int read_cryptpacket(int crypt_connection_id, uint8_t * data) 155int read_cryptpacket(int crypt_connection_id, uint8_t * data)
156{ 156{
157 if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) 157 if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
@@ -184,8 +184,8 @@ int read_cryptpacket(int crypt_connection_id, uint8_t * data)
184} 184}
185 185
186 186
187//return 0 if data could not be put in packet queue 187/* return 0 if data could not be put in packet queue
188//return 1 if data was put into the queue 188 return 1 if data was put into the queue */
189int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length) 189int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length)
190{ 190{
191 if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) 191 if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
@@ -217,10 +217,10 @@ int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length)
217 return 1; 217 return 1;
218} 218}
219 219
220//send a friend request to peer with public_key and ip_port. 220/* send a friend request to peer with public_key and ip_port.
221//Data represents the data we send with the friends request. 221 Data represents the data we send with the friends request.
222//returns -1 on failure 222 returns -1 on failure
223//returns a positive friend request id that can be used later to see if it was sent correctly on success. 223 returns a positive friend request id that can be used later to see if it was sent correctly on success. */
224int send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, uint32_t length) 224int send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, uint32_t length)
225{ 225{
226 if(length > MAX_DATA_SIZE - 1 - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES) 226 if(length > MAX_DATA_SIZE - 1 - crypto_box_PUBLICKEYBYTES - crypto_box_NONCEBYTES)
@@ -228,7 +228,7 @@ int send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, ui
228 return -1; 228 return -1;
229 } 229 }
230 uint32_t i; 230 uint32_t i;
231 for(i = 0; i < MAX_FRIEND_REQUESTS; i++) 231 for(i = 0; i < MAX_FRIEND_REQUESTS; ++i)
232 { 232 {
233 if(outbound_friendrequests[i] == -1) 233 if(outbound_friendrequests[i] == -1)
234 { 234 {
@@ -264,10 +264,10 @@ int send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, ui
264 return -1; 264 return -1;
265} 265}
266 266
267//return -1 if failure 267/* return -1 if failure
268//return 0 if connection is still trying to send the request. 268 return 0 if connection is still trying to send the request.
269//return 1 if sent correctly 269 return 1 if sent correctly
270//return 2 if connection timed out 270 return 2 if connection timed out */
271int check_friendrequest(int friend_request) 271int check_friendrequest(int friend_request)
272{ 272{
273 if(friend_request < 0 || friend_request > MAX_FRIEND_REQUESTS) 273 if(friend_request < 0 || friend_request > MAX_FRIEND_REQUESTS)
@@ -299,9 +299,9 @@ int check_friendrequest(int friend_request)
299 return 0; 299 return 0;
300} 300}
301 301
302//Send a crypto handshake packet containing an encrypted secret nonce and session public key 302/* Send a crypto handshake packet containing an encrypted secret nonce and session public key
303//to peer with connection_id and public_key 303 to peer with connection_id and public_key
304//the packet is encrypted with a random nonce which is sent in plain text with the packet 304 the packet is encrypted with a random nonce which is sent in plain text with the packet */
305int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key) 305int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key)
306{ 306{
307 uint8_t temp_data[MAX_DATA_SIZE]; 307 uint8_t temp_data[MAX_DATA_SIZE];
@@ -324,9 +324,9 @@ int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secr
324 return write_packet(connection_id, temp_data, len + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES); 324 return write_packet(connection_id, temp_data, len + 1 + crypto_box_PUBLICKEYBYTES + crypto_box_NONCEBYTES);
325} 325}
326 326
327//Extract secret nonce, session public key and public_key from a packet(data) with length length 327/* Extract secret nonce, session public key and public_key from a packet(data) with length length
328//return 1 if successful 328 return 1 if successful
329//return 0 if failure 329 return 0 if failure */
330int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce, 330int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce,
331 uint8_t * session_key, uint8_t * data, uint16_t length) 331 uint8_t * session_key, uint8_t * data, uint16_t length)
332{ 332{
@@ -359,13 +359,13 @@ int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce,
359} 359}
360 360
361 361
362//puts the public key of the friend if public_key, the data from the request 362/* puts the public key of the friend if public_key, the data from the request
363//in data if a friend request was sent to us and returns the length of the data. 363 in data if a friend request was sent to us and returns the length of the data.
364//return -1 if no valid friend requests. 364 return -1 if no valid friend requests. */
365int handle_friendrequest(uint8_t * public_key, uint8_t * data) 365int handle_friendrequest(uint8_t * public_key, uint8_t * data)
366{ 366{
367 uint32_t i; 367 uint32_t i;
368 for(i = 0; i < MAX_INCOMING; i++) 368 for(i = 0; i < MAX_INCOMING; ++i)
369 { 369 {
370 if(incoming_connections[i] != -1) 370 if(incoming_connections[i] != -1)
371 { 371 {
@@ -384,12 +384,12 @@ int handle_friendrequest(uint8_t * public_key, uint8_t * data)
384 if(len1 != -1) 384 if(len1 != -1)
385 { 385 {
386 kill_connection(incoming_connections[i]); 386 kill_connection(incoming_connections[i]);
387 //kill_connection_in(incoming_connections[i], 1); //conection is useless now, kill it in 1 seconds 387 /* kill_connection_in(incoming_connections[i], 1); //conection is useless now, kill it in 1 seconds */
388 incoming_connections[i] = -1; 388 incoming_connections[i] = -1;
389 return len1; 389 return len1;
390 } 390 }
391 } 391 }
392 kill_connection(incoming_connections[i]); //conection is useless now, kill it. 392 kill_connection(incoming_connections[i]); /* conection is useless now, kill it. */
393 incoming_connections[i] = -1; 393 incoming_connections[i] = -1;
394 } 394 }
395 } 395 }
@@ -397,13 +397,13 @@ int handle_friendrequest(uint8_t * public_key, uint8_t * data)
397 return -1; 397 return -1;
398} 398}
399 399
400//get crypto connection id from public key of peer 400/* get crypto connection id from public key of peer
401//return -1 if there are no connections like we are looking for 401 return -1 if there are no connections like we are looking for
402//return id if it found it 402 return id if it found it */
403int getcryptconnection_id(uint8_t * public_key) 403int getcryptconnection_id(uint8_t * public_key)
404{ 404{
405 uint32_t i; 405 uint32_t i;
406 for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++) 406 for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
407 { 407 {
408 if(crypto_connections[i].status > 0) 408 if(crypto_connections[i].status > 0)
409 { 409 {
@@ -417,9 +417,9 @@ int getcryptconnection_id(uint8_t * public_key)
417} 417}
418 418
419 419
420//Start a secure connection with other peer who has public_key and ip_port 420/* Start a secure connection with other peer who has public_key and ip_port
421//returns -1 if failure 421 returns -1 if failure
422//returns crypt_connection_id of the initialized connection if everything went well. 422 returns crypt_connection_id of the initialized connection if everything went well. */
423int crypto_connect(uint8_t * public_key, IP_Port ip_port) 423int crypto_connect(uint8_t * public_key, IP_Port ip_port)
424{ 424{
425 uint32_t i; 425 uint32_t i;
@@ -432,7 +432,7 @@ int crypto_connect(uint8_t * public_key, IP_Port ip_port)
432 return -1; 432 return -1;
433 } 433 }
434 } 434 }
435 for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++) 435 for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
436 { 436 {
437 if(crypto_connections[i].status == 0) 437 if(crypto_connections[i].status == 0)
438 { 438 {
@@ -453,23 +453,23 @@ int crypto_connect(uint8_t * public_key, IP_Port ip_port)
453 increment_nonce(crypto_connections[i].recv_nonce); 453 increment_nonce(crypto_connections[i].recv_nonce);
454 return i; 454 return i;
455 } 455 }
456 return -1;//this should never happen. 456 return -1; /* this should never happen. */
457 } 457 }
458 } 458 }
459 return -1; 459 return -1;
460} 460}
461 461
462//handle an incoming connection 462/* handle an incoming connection
463//return -1 if no crypto inbound connection 463 return -1 if no crypto inbound connection
464//return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection 464 return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection
465//Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce 465 Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce
466//and the session public key for the connection in session_key 466 and the session public key for the connection in session_key
467//to accept it see: accept_crypto_inbound(...) 467 to accept it see: accept_crypto_inbound(...)
468//to refuse it just call kill_connection(...) on the connection id 468 to refuse it just call kill_connection(...) on the connection id */
469int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key) 469int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key)
470{ 470{
471 uint32_t i; 471 uint32_t i;
472 for(i = 0; i < MAX_INCOMING; i++) 472 for(i = 0; i < MAX_INCOMING; ++i)
473 { 473 {
474 if(incoming_connections[i] != -1) 474 if(incoming_connections[i] != -1)
475 { 475 {
@@ -486,7 +486,7 @@ int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * sessi
486 if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) 486 if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len))
487 { 487 {
488 int connection_id = incoming_connections[i]; 488 int connection_id = incoming_connections[i];
489 incoming_connections[i] = -1;//remove this connection from the incoming connection list. 489 incoming_connections[i] = -1; /* remove this connection from the incoming connection list. */
490 return connection_id; 490 return connection_id;
491 } 491 }
492 } 492 }
@@ -495,9 +495,9 @@ int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * sessi
495 return -1; 495 return -1;
496} 496}
497 497
498//kill a crypto connection 498/* kill a crypto connection
499//return 0 if killed successfully 499 return 0 if killed successfully
500//return 1 if there was a problem. 500 return 1 if there was a problem. */
501int crypto_kill(int crypt_connection_id) 501int crypto_kill(int crypt_connection_id)
502{ 502{
503 if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS) 503 if(crypt_connection_id < 0 || crypt_connection_id >= MAX_CRYPTO_CONNECTIONS)
@@ -515,9 +515,9 @@ int crypto_kill(int crypt_connection_id)
515} 515}
516 516
517 517
518//accept an incoming connection using the parameters provided by crypto_inbound 518/* accept an incoming connection using the parameters provided by crypto_inbound
519//return -1 if not successful 519 return -1 if not successful
520//returns the crypt_connection_id if successful 520 returns the crypt_connection_id if successful */
521int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key) 521int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key)
522{ 522{
523 uint32_t i; 523 uint32_t i;
@@ -530,7 +530,7 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec
530 { 530 {
531 return -1; 531 return -1;
532 }*/ 532 }*/
533 for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++) 533 for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
534 { 534 {
535 if(crypto_connections[i].status == 0) 535 if(crypto_connections[i].status == 0)
536 { 536 {
@@ -549,20 +549,20 @@ int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * sec
549 { 549 {
550 increment_nonce(crypto_connections[i].recv_nonce); 550 increment_nonce(crypto_connections[i].recv_nonce);
551 uint32_t zero = 0; 551 uint32_t zero = 0;
552 crypto_connections[i].status = 3;//connection status needs to be 3 for write_cryptpacket() to work 552 crypto_connections[i].status = 3; /* connection status needs to be 3 for write_cryptpacket() to work */
553 write_cryptpacket(i, ((uint8_t *)&zero), sizeof(zero)); 553 write_cryptpacket(i, ((uint8_t *)&zero), sizeof(zero));
554 crypto_connections[i].status = 2;//set it to its proper value right after. 554 crypto_connections[i].status = 2; /* set it to its proper value right after. */
555 return i; 555 return i;
556 } 556 }
557 return -1;//this should never happen. 557 return -1; /* this should never happen. */
558 } 558 }
559 } 559 }
560 return -1; 560 return -1;
561} 561}
562 562
563//return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet 563/* return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet
564//(we have received a handshake but no empty data packet), 3 if the connection is established. 564 (we have received a handshake but no empty data packet), 3 if the connection is established.
565//4 if the connection is timed out and waiting to be killed 565 4 if the connection is timed out and waiting to be killed */
566int is_cryptoconnected(int crypt_connection_id) 566int is_cryptoconnected(int crypt_connection_id)
567{ 567{
568 if(crypt_connection_id >= 0 && crypt_connection_id < MAX_CRYPTO_CONNECTIONS) 568 if(crypt_connection_id >= 0 && crypt_connection_id < MAX_CRYPTO_CONNECTIONS)
@@ -573,37 +573,37 @@ int is_cryptoconnected(int crypt_connection_id)
573} 573}
574 574
575 575
576//Generate our public and private keys 576/* Generate our public and private keys
577//Only call this function the first time the program starts. 577 Only call this function the first time the program starts. */
578void new_keys() 578void new_keys()
579{ 579{
580 crypto_box_keypair(self_public_key,self_secret_key); 580 crypto_box_keypair(self_public_key,self_secret_key);
581} 581}
582 582
583//save the public and private keys to the keys array 583/* save the public and private keys to the keys array
584//Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES 584 Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */
585void save_keys(uint8_t * keys) 585void save_keys(uint8_t * keys)
586{ 586{
587 memcpy(keys, self_public_key, crypto_box_PUBLICKEYBYTES); 587 memcpy(keys, self_public_key, crypto_box_PUBLICKEYBYTES);
588 memcpy(keys + crypto_box_PUBLICKEYBYTES, self_secret_key, crypto_box_SECRETKEYBYTES); 588 memcpy(keys + crypto_box_PUBLICKEYBYTES, self_secret_key, crypto_box_SECRETKEYBYTES);
589} 589}
590 590
591//load the public and private keys from the keys array 591/* load the public and private keys from the keys array
592//Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES 592 Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */
593void load_keys(uint8_t * keys) 593void load_keys(uint8_t * keys)
594{ 594{
595 memcpy(self_public_key, keys, crypto_box_PUBLICKEYBYTES); 595 memcpy(self_public_key, keys, crypto_box_PUBLICKEYBYTES);
596 memcpy(self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES); 596 memcpy(self_secret_key, keys + crypto_box_PUBLICKEYBYTES, crypto_box_SECRETKEYBYTES);
597} 597}
598 598
599//TODO: optimize this 599/* TODO: optimize this
600//adds an incoming connection to the incoming_connection list. 600 adds an incoming connection to the incoming_connection list.
601//returns 0 if successful 601 returns 0 if successful
602//returns 1 if failure 602 returns 1 if failure */
603int new_incoming(int id) 603int new_incoming(int id)
604{ 604{
605 uint32_t i; 605 uint32_t i;
606 for(i = 0; i < MAX_INCOMING; i++) 606 for(i = 0; i < MAX_INCOMING; ++i)
607 { 607 {
608 if(incoming_connections[i] == -1) 608 if(incoming_connections[i] == -1)
609 { 609 {
@@ -614,8 +614,8 @@ int new_incoming(int id)
614 return 1; 614 return 1;
615} 615}
616 616
617//TODO: optimize this 617/* TODO: optimize this
618//handle all new incoming connections. 618 handle all new incoming connections. */
619static void handle_incomings() 619static void handle_incomings()
620{ 620{
621 int income; 621 int income;
@@ -629,11 +629,11 @@ static void handle_incomings()
629 } 629 }
630} 630}
631 631
632//handle received packets for not yet established crypto connections. 632/* handle received packets for not yet established crypto connections. */
633static void receive_crypto() 633static void receive_crypto()
634{ 634{
635 uint32_t i; 635 uint32_t i;
636 for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++) 636 for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
637 { 637 {
638 if(crypto_connections[i].status == 1) 638 if(crypto_connections[i].status == 1)
639 { 639 {
@@ -643,12 +643,12 @@ static void receive_crypto()
643 uint8_t session_key[crypto_box_PUBLICKEYBYTES]; 643 uint8_t session_key[crypto_box_PUBLICKEYBYTES];
644 uint16_t len; 644 uint16_t len;
645 if(id_packet(crypto_connections[i].number) == 1) 645 if(id_packet(crypto_connections[i].number) == 1)
646 //if the packet is a friend request drop it (because we are already friends) 646 /* if the packet is a friend request drop it (because we are already friends) */
647 { 647 {
648 len = read_packet(crypto_connections[i].number, temp_data); 648 len = read_packet(crypto_connections[i].number, temp_data);
649 649
650 } 650 }
651 if(id_packet(crypto_connections[i].number) == 2)//handle handshake packet. 651 if(id_packet(crypto_connections[i].number) == 2) /* handle handshake packet. */
652 { 652 {
653 len = read_packet(crypto_connections[i].number, temp_data); 653 len = read_packet(crypto_connections[i].number, temp_data);
654 if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len)) 654 if(handle_cryptohandshake(public_key, secret_nonce, session_key, temp_data, len))
@@ -659,16 +659,16 @@ static void receive_crypto()
659 memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES); 659 memcpy(crypto_connections[i].peersessionpublic_key, session_key, crypto_box_PUBLICKEYBYTES);
660 increment_nonce(crypto_connections[i].sent_nonce); 660 increment_nonce(crypto_connections[i].sent_nonce);
661 uint32_t zero = 0; 661 uint32_t zero = 0;
662 crypto_connections[i].status = 3;//connection status needs to be 3 for write_cryptpacket() to work 662 crypto_connections[i].status = 3; /* connection status needs to be 3 for write_cryptpacket() to work */
663 write_cryptpacket(i, ((uint8_t *)&zero), sizeof(zero)); 663 write_cryptpacket(i, ((uint8_t *)&zero), sizeof(zero));
664 crypto_connections[i].status = 2;//set it to its proper value right after. 664 crypto_connections[i].status = 2; /* set it to its proper value right after. */
665 } 665 }
666 } 666 }
667 } 667 }
668 else if(id_packet(crypto_connections[i].number) != -1) 668 else if(id_packet(crypto_connections[i].number) != -1)
669 { 669 {
670 //This should not happen 670 /* This should not happen
671 //kill the connection if it does 671 kill the connection if it does */
672 crypto_kill(crypto_connections[i].number); 672 crypto_kill(crypto_connections[i].number);
673 } 673 }
674 674
@@ -689,35 +689,35 @@ static void receive_crypto()
689 increment_nonce(crypto_connections[i].recv_nonce); 689 increment_nonce(crypto_connections[i].recv_nonce);
690 crypto_connections[i].status = 3; 690 crypto_connections[i].status = 3;
691 691
692 //connection is accepted so we disable the auto kill by setting it to about 1 month from now. 692 /* connection is accepted so we disable the auto kill by setting it to about 1 month from now. */
693 kill_connection_in(crypto_connections[i].number, 3000000); 693 kill_connection_in(crypto_connections[i].number, 3000000);
694 } 694 }
695 else 695 else
696 { 696 {
697 //This should not happen 697 /* This should not happen
698 //kill the connection if it does 698 kill the connection if it does */
699 crypto_kill(crypto_connections[i].number); 699 crypto_kill(crypto_connections[i].number);
700 } 700 }
701 } 701 }
702 else if(id_packet(crypto_connections[i].number) != -1) 702 else if(id_packet(crypto_connections[i].number) != -1)
703 { 703 {
704 //This should not happen 704 /* This should not happen
705 //kill the connection if it does 705 kill the connection if it does */
706 crypto_kill(crypto_connections[i].number); 706 crypto_kill(crypto_connections[i].number);
707 } 707 }
708 } 708 }
709 } 709 }
710} 710}
711 711
712//run this to (re)initialize net_crypto 712/* run this to (re)initialize net_crypto
713//sets all the global connection variables to their default values. 713 sets all the global connection variables to their default values. */
714void initNetCrypto() 714void initNetCrypto()
715{ 715{
716 memset(crypto_connections, 0 ,sizeof(crypto_connections)); 716 memset(crypto_connections, 0 ,sizeof(crypto_connections));
717 memset(outbound_friendrequests, -1 ,sizeof(outbound_friendrequests)); 717 memset(outbound_friendrequests, -1 ,sizeof(outbound_friendrequests));
718 memset(incoming_connections, -1 ,sizeof(incoming_connections)); 718 memset(incoming_connections, -1 ,sizeof(incoming_connections));
719 uint32_t i; 719 uint32_t i;
720 for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++) 720 for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
721 { 721 {
722 crypto_connections[i].number = ~0; 722 crypto_connections[i].number = ~0;
723 } 723 }
@@ -726,7 +726,7 @@ void initNetCrypto()
726static void killTimedout() 726static void killTimedout()
727{ 727{
728 uint32_t i; 728 uint32_t i;
729 for(i = 0; i < MAX_CRYPTO_CONNECTIONS; i++) 729 for(i = 0; i < MAX_CRYPTO_CONNECTIONS; ++i)
730 { 730 {
731 if(crypto_connections[i].status != 0 && is_connected(crypto_connections[i].number) == 4) 731 if(crypto_connections[i].status != 0 && is_connected(crypto_connections[i].number) == 4)
732 { 732 {
@@ -740,12 +740,12 @@ static void killTimedout()
740 } 740 }
741} 741}
742 742
743//main loop 743/* main loop */
744void doNetCrypto() 744void doNetCrypto()
745{ 745{
746 //TODO:check if friend requests were sent correctly 746 /* TODO:check if friend requests were sent correctly
747 //handle new incoming connections 747 handle new incoming connections
748 //handle friend requests 748 handle friend requests */
749 handle_incomings(); 749 handle_incomings();
750 receive_crypto(); 750 receive_crypto();
751 killTimedout(); 751 killTimedout();
diff --git a/core/net_crypto.h b/core/net_crypto.h
index df2cb9ca..0bf21f60 100644
--- a/core/net_crypto.h
+++ b/core/net_crypto.h
@@ -26,111 +26,111 @@
26 26
27#include "Lossless_UDP.h" 27#include "Lossless_UDP.h"
28 28
29//Our public key. 29/* Our public key. */
30extern uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; 30extern uint8_t self_public_key[crypto_box_PUBLICKEYBYTES];
31extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; 31extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES];
32 32
33#define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES) 33#define ENCRYPTION_PADDING (crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES)
34 34
35//encrypts plain of length length to encrypted of length + 16 using the 35/* encrypts plain of length length to encrypted of length + 16 using the
36//public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce 36 public key(32 bytes) of the receiver and the secret key of the sender and a 24 byte nonce
37//return -1 if there was a problem. 37 return -1 if there was a problem.
38//return length of encrypted data if everything was fine. 38 return length of encrypted data if everything was fine. */
39int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, 39int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
40 uint8_t * plain, uint32_t length, uint8_t * encrypted); 40 uint8_t * plain, uint32_t length, uint8_t * encrypted);
41 41
42 42
43//decrypts encrypted of length length to plain of length length - 16 using the 43/* decrypts encrypted of length length to plain of length length - 16 using the
44//public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce 44 public key(32 bytes) of the sender, the secret key of the receiver and a 24 byte nonce
45//return -1 if there was a problem(decryption failed) 45 return -1 if there was a problem(decryption failed)
46//return length of plain data if everything was fine. 46 return length of plain data if everything was fine. */
47int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, 47int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce,
48 uint8_t * encrypted, uint32_t length, uint8_t * plain); 48 uint8_t * encrypted, uint32_t length, uint8_t * plain);
49 49
50 50
51//fill the given nonce with random bytes. 51/* fill the given nonce with random bytes. */
52void random_nonce(uint8_t * nonce); 52void random_nonce(uint8_t * nonce);
53 53
54 54
55//return 0 if there is no received data in the buffer 55/* return 0 if there is no received data in the buffer
56//return -1 if the packet was discarded. 56 return -1 if the packet was discarded.
57//return length of received data if successful 57 return length of received data if successful */
58int read_cryptpacket(int crypt_connection_id, uint8_t * data); 58int read_cryptpacket(int crypt_connection_id, uint8_t * data);
59 59
60 60
61//return 0 if data could not be put in packet queue 61/* return 0 if data could not be put in packet queue
62//return 1 if data was put into the queue 62 return 1 if data was put into the queue */
63int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length); 63int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length);
64 64
65//send a friend request to peer with public_key and ip_port. 65/* send a friend request to peer with public_key and ip_port.
66//Data represents the data we send with the friends request. 66 Data represents the data we send with the friends request.
67//returns -1 on failure 67 returns -1 on failure
68//returns a positive friend request id that can be used later to see if it was sent correctly on success. 68 returns a positive friend request id that can be used later to see if it was sent correctly on success. */
69int send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, uint32_t length); 69int send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, uint32_t length);
70 70
71 71
72//return -1 if failure 72/* return -1 if failure
73//return 0 if connection is still trying to send the request. 73 return 0 if connection is still trying to send the request.
74//return 1 if sent correctly 74 return 1 if sent correctly
75//return 2 if connection timed out 75 return 2 if connection timed out */
76int check_friendrequest(int friend_request); 76int check_friendrequest(int friend_request);
77 77
78 78
79//puts the public key of the friend if public_key, the data from the request 79/* puts the public key of the friend if public_key, the data from the request
80//in data if a friend request was sent to us and returns the length of the data. 80 in data if a friend request was sent to us and returns the length of the data.
81//return -1 if no valid friend requests. 81 return -1 if no valid friend requests. */
82int handle_friendrequest(uint8_t * public_key, uint8_t * data); 82int handle_friendrequest(uint8_t * public_key, uint8_t * data);
83 83
84 84
85//Start a secure connection with other peer who has public_key and ip_port 85/* Start a secure connection with other peer who has public_key and ip_port
86//returns -1 if failure 86 returns -1 if failure
87//returns crypt_connection_id of the initialized connection if everything went well. 87 returns crypt_connection_id of the initialized connection if everything went well. */
88int crypto_connect(uint8_t * public_key, IP_Port ip_port); 88int crypto_connect(uint8_t * public_key, IP_Port ip_port);
89 89
90 90
91//kill a crypto connection 91/* kill a crypto connection
92//return 0 if killed successfully 92 return 0 if killed successfully
93//return 1 if there was a problem. 93 return 1 if there was a problem. */
94int crypto_kill(int crypt_connection_id); 94int crypto_kill(int crypt_connection_id);
95 95
96//handle an incoming connection 96/* handle an incoming connection
97//return -1 if no crypto inbound connection 97 return -1 if no crypto inbound connection
98//return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection 98 return incoming connection id (Lossless_UDP one) if there is an incoming crypto connection
99//Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce 99 Put the public key of the peer in public_key, the secret_nonce from the handshake into secret_nonce
100//and the session public key for the connection in session_key 100 and the session public key for the connection in session_key
101//to accept it see: accept_crypto_inbound(...) 101 to accept it see: accept_crypto_inbound(...)
102//to refuse it just call kill_connection(...) on the connection id 102 to refuse it just call kill_connection(...) on the connection id */
103int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key); 103int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key);
104 104
105 105
106//accept an incoming connection using the parameters provided by crypto_inbound 106/* accept an incoming connection using the parameters provided by crypto_inbound
107//return -1 if not successful 107 return -1 if not successful
108//returns the crypt_connection_id if successful 108 returns the crypt_connection_id if successful */
109int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key); 109int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key);
110 110
111//return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet 111/* return 0 if no connection, 1 we have sent a handshake, 2 if connexion is not confirmed yet
112//(we have received a handshake but no empty data packet), 3 if the connection is established. 112 (we have received a handshake but no empty data packet), 3 if the connection is established.
113//4 if the connection is timed out and waiting to be killed 113 4 if the connection is timed out and waiting to be killed */
114int is_cryptoconnected(int crypt_connection_id); 114int is_cryptoconnected(int crypt_connection_id);
115 115
116 116
117//Generate our public and private keys 117/* Generate our public and private keys
118//Only call this function the first time the program starts. 118 Only call this function the first time the program starts. */
119void new_keys(); 119void new_keys();
120 120
121//save the public and private keys to the keys array 121/* save the public and private keys to the keys array
122//Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES 122 Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */
123void save_keys(uint8_t * keys); 123void save_keys(uint8_t * keys);
124 124
125//load the public and private keys from the keys array 125/* load the public and private keys from the keys array
126//Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES 126 Length must be crypto_box_PUBLICKEYBYTES + crypto_box_SECRETKEYBYTES */
127void load_keys(uint8_t * keys); 127void load_keys(uint8_t * keys);
128 128
129//run this to (re)initialize net_crypto 129/* run this to (re)initialize net_crypto
130//sets all the global connection variables to their default values. 130 sets all the global connection variables to their default values. */
131void initNetCrypto(); 131void initNetCrypto();
132 132
133//main loop 133/* main loop */
134void doNetCrypto(); 134void doNetCrypto();
135 135
136 136
diff --git a/core/network.c b/core/network.c
index c08b3512..dbe4574c 100644
--- a/core/network.c
+++ b/core/network.c
@@ -25,12 +25,12 @@
25#include "network.h" 25#include "network.h"
26 26
27 27
28//returns current UNIX time in microseconds (us). 28/* returns current UNIX time in microseconds (us). */
29uint64_t current_time() 29uint64_t current_time()
30{ 30{
31 uint64_t time; 31 uint64_t time;
32 #ifdef WIN32 32 #ifdef WIN32
33 //This probably works fine 33 /* This probably works fine */
34 FILETIME ft; 34 FILETIME ft;
35 GetSystemTimeAsFileTime(&ft); 35 GetSystemTimeAsFileTime(&ft);
36 time = ft.dwHighDateTime; 36 time = ft.dwHighDateTime;
@@ -48,8 +48,8 @@ uint64_t current_time()
48 48
49} 49}
50 50
51//return a random number 51/* return a random number
52//NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary 52 NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary */
53uint32_t random_int() 53uint32_t random_int()
54{ 54{
55 #ifndef VANILLA_NACL 55 #ifndef VANILLA_NACL
@@ -60,11 +60,11 @@ uint32_t random_int()
60 #endif 60 #endif
61} 61}
62 62
63//our UDP socket, a global variable. 63/* our UDP socket, a global variable. */
64static int sock; 64static int sock;
65 65
66//Basic network functions: 66/* Basic network functions:
67//Function to send packet(data) of length length to ip_port 67 Function to send packet(data) of length length to ip_port */
68int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length) 68int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length)
69{ 69{
70 ADDR addr = {AF_INET, ip_port.port, ip_port.ip}; 70 ADDR addr = {AF_INET, ip_port.port, ip_port.ip};
@@ -72,10 +72,10 @@ int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length)
72 72
73} 73}
74 74
75//Function to receive data, ip and port of sender is put into ip_port 75/* Function to receive data, ip and port of sender is put into ip_port
76//the packet data into data 76 the packet data into data
77//the packet length into length. 77 the packet length into length.
78//dump all empty packets. 78 dump all empty packets. */
79int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length) 79int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
80{ 80{
81 ADDR addr; 81 ADDR addr;
@@ -87,8 +87,8 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
87 (*(int32_t *)length) = recvfrom(sock,(char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); 87 (*(int32_t *)length) = recvfrom(sock,(char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
88 if(*(int32_t *)length <= 0) 88 if(*(int32_t *)length <= 0)
89 { 89 {
90 //nothing received 90 /* nothing received
91 //or empty packet 91 or empty packet */
92 return -1; 92 return -1;
93 } 93 }
94 ip_port->ip = addr.ip; 94 ip_port->ip = addr.ip;
@@ -97,12 +97,12 @@ int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
97 97
98} 98}
99 99
100//initialize networking 100/* initialize networking
101//bind to ip and port 101 bind to ip and port
102//ip must be in network order EX: 127.0.0.1 = (7F000001) 102 ip must be in network order EX: 127.0.0.1 = (7F000001)
103//port is in host byte order (this means don't worry about it) 103 port is in host byte order (this means don't worry about it)
104//returns 0 if no problems 104 returns 0 if no problems
105//TODO: add something to check if there are errors 105 TODO: add something to check if there are errors */
106int init_networking(IP ip ,uint16_t port) 106int init_networking(IP ip ,uint16_t port)
107{ 107{
108 #ifdef WIN32 108 #ifdef WIN32
@@ -117,11 +117,11 @@ int init_networking(IP ip ,uint16_t port)
117 #endif 117 #endif
118 srand((uint32_t)current_time()); 118 srand((uint32_t)current_time());
119 119
120 //initialize our socket 120 /* initialize our socket */
121 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); 121 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
122 122
123 //Functions to increase the size of the send and receive UDP buffers 123 /* Functions to increase the size of the send and receive UDP buffers
124 //NOTE: uncomment if necessary 124 NOTE: uncomment if necessary */
125 /* 125 /*
126 int n = 1024 * 1024 * 2; 126 int n = 1024 * 1024 * 2;
127 if(setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&n, sizeof(n)) == -1) 127 if(setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&n, sizeof(n)) == -1)
@@ -134,28 +134,28 @@ int init_networking(IP ip ,uint16_t port)
134 return -1; 134 return -1;
135 }*/ 135 }*/
136 136
137 //Set socket nonblocking 137 /* Set socket nonblocking */
138 #ifdef WIN32 138 #ifdef WIN32
139 //I think this works for windows 139 /* I think this works for windows */
140 u_long mode = 1; 140 u_long mode = 1;
141 //ioctl(sock, FIONBIO, &mode); 141 /* ioctl(sock, FIONBIO, &mode); */
142 ioctlsocket(sock, FIONBIO, &mode); 142 ioctlsocket(sock, FIONBIO, &mode);
143 #else 143 #else
144 fcntl(sock, F_SETFL, O_NONBLOCK, 1); 144 fcntl(sock, F_SETFL, O_NONBLOCK, 1);
145 #endif 145 #endif
146 146
147 //Bind our socket to port PORT and address 0.0.0.0 147 /* Bind our socket to port PORT and address 0.0.0.0 */
148 ADDR addr = {AF_INET, htons(port), ip}; 148 ADDR addr = {AF_INET, htons(port), ip};
149 bind(sock, (struct sockaddr*)&addr, sizeof(addr)); 149 bind(sock, (struct sockaddr*)&addr, sizeof(addr));
150 return 0; 150 return 0;
151 151
152} 152}
153 153
154//function to cleanup networking stuff 154/* function to cleanup networking stuff */
155void shutdown_networking() 155void shutdown_networking()
156{ 156{
157 #ifdef WIN32 157 #ifdef WIN32
158 WSACleanup(); 158 WSACleanup();
159 #endif 159 #endif
160 return; 160 return;
161} \ No newline at end of file 161}
diff --git a/core/network.h b/core/network.h
index 33f11239..8f88940c 100644
--- a/core/network.h
+++ b/core/network.h
@@ -1,6 +1,6 @@
1/* network.h 1/* network.h
2* 2*
3* Datatypes, functions and includes for the core networking. 3* Functions for the core networking.
4* 4*
5 5
6 Copyright (C) 2013 Tox project All Rights Reserved. 6 Copyright (C) 2013 Tox project All Rights Reserved.
@@ -21,104 +21,141 @@
21 along with Tox. If not, see <http://www.gnu.org/licenses/>. 21 along with Tox. If not, see <http://www.gnu.org/licenses/>.
22 22
23*/ 23*/
24
25
26#ifndef NETWORK_H
27#define NETWORK_H
28
29#include <stdlib.h>
30#include <stdio.h>
31#include <stdint.h>
32#include <string.h>
33#include <time.h>
34
35
36
37#ifdef WIN32 //Put win32 includes here
38
39#include <winsock2.h>
40#include <windows.h>
41
42#undef VANILLA_NACL//make sure on windows we use libsodium
43
44#else //Linux includes
45
46#include <fcntl.h>
47#include <sys/socket.h>
48#include <netinet/in.h>
49#include <errno.h>
50#include <sys/time.h>
51 24
25#include "network.h"
52 26
53#endif
54 27
55#ifndef VANILLA_NACL 28/* returns current UNIX time in microseconds (us). */
56//we use libsodium by default 29uint64_t current_time()
57#include <sodium.h> 30{
58#else 31 uint64_t time;
59 32 #ifdef WIN32
60//TODO: Including stuff like this is bad. This needs fixing. 33 /* This probably works fine */
61//We keep support for the original NaCl for now. 34 FILETIME ft;
62#include "../nacl/build/Linux/include/amd64/crypto_box.h" 35 GetSystemTimeAsFileTime(&ft);
63 36 time = ft.dwHighDateTime;
64#endif 37 time <<=32;
38 time |= ft.dwLowDateTime;
39 time -= 116444736000000000UL;
40 return time/10;
41 #else
42 struct timeval a;
43 gettimeofday(&a, NULL);
44 time = 1000000UL*a.tv_sec + a.tv_usec;
45 return time;
46 #endif
47
48
49}
65 50
51/* return a random number
52 NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary */
53uint32_t random_int()
54{
55 #ifndef VANILLA_NACL
56 /* NOTE: this function comes from libsodium */
57 return randombytes_random();
58 #else
59 return random();
60 #endif
61}
66 62
67#define MAX_UDP_PACKET_SIZE 65507 63/* our UDP socket, a global variable. */
64static int sock;
68 65
69typedef union 66/* Basic network functions:
67 Function to send packet(data) of length length to ip_port */
68int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length)
70{ 69{
71 uint8_t c[4]; 70 ADDR addr = {AF_INET, ip_port.port, ip_port.ip};
72 uint16_t s[2]; 71 return sendto(sock,(char *) data, length, 0, (struct sockaddr *)&addr, sizeof(addr));
73 uint32_t i; 72
74}IP; 73}
75 74
76typedef struct 75/* Function to receive data, ip and port of sender is put into ip_port
76 the packet data into data
77 the packet length into length.
78 dump all empty packets. */
79int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length)
77{ 80{
78 IP ip; 81 ADDR addr;
79 uint16_t port; 82 #ifdef WIN32
80 //not used for anything right now 83 int addrlen = sizeof(addr);
81 uint16_t padding; 84 #else
82}IP_Port; 85 uint32_t addrlen = sizeof(addr);
83 86 #endif
84typedef struct 87 (*(int32_t *)length) = recvfrom(sock,(char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
88 if(*(int32_t *)length <= 0)
89 {
90 /* nothing received
91 or empty packet */
92 return -1;
93 }
94 ip_port->ip = addr.ip;
95 ip_port->port = addr.port;
96 return 0;
97
98}
99
100/* initialize networking
101 bind to ip and port
102 ip must be in network order EX: 127.0.0.1 = (7F000001)
103 port is in host byte order (this means don't worry about it)
104 returns 0 if no problems
105 TODO: add something to check if there are errors */
106int init_networking(IP ip ,uint16_t port)
85{ 107{
86 int16_t family; 108 #ifdef WIN32
87 uint16_t port; 109 WSADATA wsaData;
88 IP ip; 110 if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR)
89 uint8_t zeroes[8]; 111 {
90 #ifdef ENABLE_IPV6 112 return -1;
91 uint8_t zeroes2[12]; 113 }
114
115 #else
116 srandom((uint32_t)current_time());
92 #endif 117 #endif
93}ADDR; 118 srand((uint32_t)current_time());
94 119
95 120 /* initialize our socket */
96//returns current time in milleseconds since the epoch. 121 sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
97uint64_t current_time(); 122
98 123 /* Functions to increase the size of the send and receive UDP buffers
99//return a random number 124 NOTE: uncomment if necessary
100//NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary 125
101uint32_t random_int(); 126 int n = 1024 * 1024 * 2;
102 127 if(setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&n, sizeof(n)) == -1)
103//Basic network functions: 128 {
104 129 return -1;
105//Function to send packet(data) of length length to ip_port 130 }
106int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length); 131
107 132 if(setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char*)&n, sizeof(n)) == -1)
108//Function to receive data, ip and port of sender is put into ip_port 133 {
109//the packet data into data 134 return -1;
110//the packet length into length. 135 }*/
111int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length); 136
112 137 /*Set socket nonblocking */
113//initialize networking 138 #ifdef WIN32
114//bind to ip and port 139 /* I think this works for windows */
115//ip must be in network order EX: 127.0.0.1 = (7F000001) 140 u_long mode = 1;
116//port is in host byte order (this means don't worry about it) 141 /* ioctl(sock, FIONBIO, &mode); */
117//returns 0 if no problems 142 ioctlsocket(sock, FIONBIO, &mode);
118//TODO: add something to check if there are errors 143 #else
119int init_networking(IP ip ,uint16_t port); 144 fcntl(sock, F_SETFL, O_NONBLOCK, 1);
145 #endif
146
147 /* Bind our socket to port PORT and address 0.0.0.0 */
148 ADDR addr = {AF_INET, htons(port), ip};
149 bind(sock, (struct sockaddr*)&addr, sizeof(addr));
150 return 0;
120 151
152}
121 153
122//function to cleanup networking stuff(doesn't do much right now) 154/* function to cleanup networking stuff */
123void shutdown_networking(); 155void shutdown_networking()
124#endif 156{
157 #ifdef WIN32
158 WSACleanup();
159 #endif
160 return;
161}