diff options
author | irungentoo <irungentoo@gmail.com> | 2013-07-20 08:25:19 -0700 |
---|---|---|
committer | irungentoo <irungentoo@gmail.com> | 2013-07-20 08:25:19 -0700 |
commit | 72ea431489f1eff56c7409c6cacd55f819684c55 (patch) | |
tree | ac1cef0b790abf72f3e03672bce3593c7a3d010a | |
parent | 11e94066f710a602a9a071b028036a2b35de2741 (diff) | |
parent | 9616cc6a89247e0b13eb575f070467ba72a2024c (diff) |
Merge pull request #62 from Captainhat/master
More comments in core fixed
-rw-r--r-- | core/net_crypto.c | 222 | ||||
-rw-r--r-- | core/net_crypto.h | 108 | ||||
-rw-r--r-- | core/network.c | 56 | ||||
-rw-r--r-- | core/network.h | 42 |
4 files changed, 214 insertions, 214 deletions
diff --git a/core/net_crypto.c b/core/net_crypto.c index fe03b5ee..270c969a 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. */ |
32 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | 32 | uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; |
33 | uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; | 33 | uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; |
34 | 34 | ||
35 | 35 | ||
36 | typedef struct | 36 | typedef 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 */ |
58 | static int outbound_friendrequests[MAX_FRIEND_REQUESTS]; | 58 | static 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 */ |
63 | static int incoming_connections[MAX_INCOMING]; | 63 | static 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. */ |
69 | int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | 69 | int 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. */ |
99 | int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | 99 | int 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,24 +107,24 @@ 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 */ |
128 | void increment_nonce(uint8_t * nonce) | 128 | void increment_nonce(uint8_t * nonce) |
129 | { | 129 | { |
130 | uint32_t i; | 130 | uint32_t i; |
@@ -138,8 +138,8 @@ 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 */ |
143 | void random_nonce(uint8_t * nonce) | 143 | void random_nonce(uint8_t * nonce) |
144 | { | 144 | { |
145 | uint32_t i; | 145 | uint32_t i; |
@@ -149,9 +149,9 @@ void random_nonce(uint8_t * nonce) | |||
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 */ |
155 | int read_cryptpacket(int crypt_connection_id, uint8_t * data) | 155 | int 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 */ |
189 | int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length) | 189 | int 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. */ |
224 | int send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, uint32_t length) | 224 | int 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) |
@@ -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 */ |
271 | int check_friendrequest(int friend_request) | 271 | int 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 */ |
305 | int send_cryptohandshake(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key) | 305 | int 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 */ |
330 | int handle_cryptohandshake(uint8_t * public_key, uint8_t * secret_nonce, | 330 | int 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,9 +359,9 @@ 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. */ |
365 | int handle_friendrequest(uint8_t * public_key, uint8_t * data) | 365 | int handle_friendrequest(uint8_t * public_key, uint8_t * data) |
366 | { | 366 | { |
367 | uint32_t i; | 367 | uint32_t i; |
@@ -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,9 +397,9 @@ 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 */ |
403 | int getcryptconnection_id(uint8_t * public_key) | 403 | int getcryptconnection_id(uint8_t * public_key) |
404 | { | 404 | { |
405 | uint32_t i; | 405 | uint32_t i; |
@@ -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. */ |
423 | int crypto_connect(uint8_t * public_key, IP_Port ip_port) | 423 | int crypto_connect(uint8_t * public_key, IP_Port ip_port) |
424 | { | 424 | { |
425 | uint32_t i; | 425 | uint32_t i; |
@@ -453,19 +453,19 @@ 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 */ |
469 | int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key) | 469 | int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key) |
470 | { | 470 | { |
471 | uint32_t i; | 471 | uint32_t i; |
@@ -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. */ |
501 | int crypto_kill(int crypt_connection_id) | 501 | int 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 */ |
521 | int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key) | 521 | int 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; |
@@ -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 */ |
566 | int is_cryptoconnected(int crypt_connection_id) | 566 | int 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,33 +573,33 @@ 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. */ |
578 | void new_keys() | 578 | void 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 */ |
585 | void save_keys(uint8_t * keys) | 585 | void 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 */ |
593 | void load_keys(uint8_t * keys) | 593 | void 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 */ |
603 | int new_incoming(int id) | 603 | int new_incoming(int id) |
604 | { | 604 | { |
605 | uint32_t i; | 605 | uint32_t i; |
@@ -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. */ |
619 | static void handle_incomings() | 619 | static void handle_incomings() |
620 | { | 620 | { |
621 | int income; | 621 | int income; |
@@ -629,7 +629,7 @@ 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. */ |
633 | static void receive_crypto() | 633 | static void receive_crypto() |
634 | { | 634 | { |
635 | uint32_t i; | 635 | uint32_t i; |
@@ -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,28 +689,28 @@ 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. */ |
714 | void initNetCrypto() | 714 | void initNetCrypto() |
715 | { | 715 | { |
716 | memset(crypto_connections, 0 ,sizeof(crypto_connections)); | 716 | memset(crypto_connections, 0 ,sizeof(crypto_connections)); |
@@ -740,12 +740,12 @@ static void killTimedout() | |||
740 | } | 740 | } |
741 | } | 741 | } |
742 | 742 | ||
743 | //main loop | 743 | /* main loop */ |
744 | void doNetCrypto() | 744 | void 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. */ |
30 | extern uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; | 30 | extern uint8_t self_public_key[crypto_box_PUBLICKEYBYTES]; |
31 | extern uint8_t self_secret_key[crypto_box_SECRETKEYBYTES]; | 31 | extern 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. */ |
39 | int encrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | 39 | int 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. */ |
47 | int decrypt_data(uint8_t * public_key, uint8_t * secret_key, uint8_t * nonce, | 47 | int 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. */ |
52 | void random_nonce(uint8_t * nonce); | 52 | void 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 */ |
58 | int read_cryptpacket(int crypt_connection_id, uint8_t * data); | 58 | int 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 */ |
63 | int write_cryptpacket(int crypt_connection_id, uint8_t * data, uint32_t length); | 63 | int 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. */ |
69 | int send_friendrequest(uint8_t * public_key, IP_Port ip_port, uint8_t * data, uint32_t length); | 69 | int 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 */ |
76 | int check_friendrequest(int friend_request); | 76 | int 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. */ |
82 | int handle_friendrequest(uint8_t * public_key, uint8_t * data); | 82 | int 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. */ |
88 | int crypto_connect(uint8_t * public_key, IP_Port ip_port); | 88 | int 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. */ |
94 | int crypto_kill(int crypt_connection_id); | 94 | int 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 */ |
103 | int crypto_inbound(uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key); | 103 | int 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 */ |
109 | int accept_crypto_inbound(int connection_id, uint8_t * public_key, uint8_t * secret_nonce, uint8_t * session_key); | 109 | int 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 */ |
114 | int is_cryptoconnected(int crypt_connection_id); | 114 | int 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. */ |
119 | void new_keys(); | 119 | void 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 */ |
123 | void save_keys(uint8_t * keys); | 123 | void 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 */ |
127 | void load_keys(uint8_t * keys); | 127 | void 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. */ |
131 | void initNetCrypto(); | 131 | void initNetCrypto(); |
132 | 132 | ||
133 | //main loop | 133 | /* main loop */ |
134 | void doNetCrypto(); | 134 | void 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). */ |
29 | uint64_t current_time() | 29 | uint64_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 */ |
53 | uint32_t random_int() | 53 | uint32_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. */ |
64 | static int sock; | 64 | static 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 */ |
68 | int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length) | 68 | int 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. */ |
79 | int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length) | 79 | int 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 */ |
106 | int init_networking(IP ip ,uint16_t port) | 106 | int 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 */ |
155 | void shutdown_networking() | 155 | void 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..fe0c48eb 100644 --- a/core/network.h +++ b/core/network.h | |||
@@ -34,12 +34,12 @@ | |||
34 | 34 | ||
35 | 35 | ||
36 | 36 | ||
37 | #ifdef WIN32 //Put win32 includes here | 37 | #ifdef WIN32 /* Put win32 includes here */ |
38 | 38 | ||
39 | #include <winsock2.h> | 39 | #include <winsock2.h> |
40 | #include <windows.h> | 40 | #include <windows.h> |
41 | 41 | ||
42 | #undef VANILLA_NACL//make sure on windows we use libsodium | 42 | #undef VANILLA_NACL /* make sure on windows we use libsodium */ |
43 | 43 | ||
44 | #else //Linux includes | 44 | #else //Linux includes |
45 | 45 | ||
@@ -53,12 +53,12 @@ | |||
53 | #endif | 53 | #endif |
54 | 54 | ||
55 | #ifndef VANILLA_NACL | 55 | #ifndef VANILLA_NACL |
56 | //we use libsodium by default | 56 | /* we use libsodium by default */ |
57 | #include <sodium.h> | 57 | #include <sodium.h> |
58 | #else | 58 | #else |
59 | 59 | ||
60 | //TODO: Including stuff like this is bad. This needs fixing. | 60 | /* TODO: Including stuff like this is bad. This needs fixing. |
61 | //We keep support for the original NaCl for now. | 61 | We keep support for the original NaCl for now. */ |
62 | #include "../nacl/build/Linux/include/amd64/crypto_box.h" | 62 | #include "../nacl/build/Linux/include/amd64/crypto_box.h" |
63 | 63 | ||
64 | #endif | 64 | #endif |
@@ -77,7 +77,7 @@ typedef struct | |||
77 | { | 77 | { |
78 | IP ip; | 78 | IP ip; |
79 | uint16_t port; | 79 | uint16_t port; |
80 | //not used for anything right now | 80 | /* not used for anything right now */ |
81 | uint16_t padding; | 81 | uint16_t padding; |
82 | }IP_Port; | 82 | }IP_Port; |
83 | 83 | ||
@@ -93,32 +93,32 @@ typedef struct | |||
93 | }ADDR; | 93 | }ADDR; |
94 | 94 | ||
95 | 95 | ||
96 | //returns current time in milleseconds since the epoch. | 96 | /* returns current time in milleseconds since the epoch. */ |
97 | uint64_t current_time(); | 97 | uint64_t current_time(); |
98 | 98 | ||
99 | //return a random number | 99 | /* return a random number |
100 | //NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary | 100 | NOTE: this function should probably not be used where cryptographic randomness is absolutely necessary */ |
101 | uint32_t random_int(); | 101 | uint32_t random_int(); |
102 | 102 | ||
103 | //Basic network functions: | 103 | /* Basic network functions: */ |
104 | 104 | ||
105 | //Function to send packet(data) of length length to ip_port | 105 | /* Function to send packet(data) of length length to ip_port */ |
106 | int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length); | 106 | int sendpacket(IP_Port ip_port, uint8_t * data, uint32_t length); |
107 | 107 | ||
108 | //Function to receive data, ip and port of sender is put into ip_port | 108 | /* Function to receive data, ip and port of sender is put into ip_port |
109 | //the packet data into data | 109 | the packet data into data |
110 | //the packet length into length. | 110 | the packet length into length. */ |
111 | int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length); | 111 | int receivepacket(IP_Port * ip_port, uint8_t * data, uint32_t * length); |
112 | 112 | ||
113 | //initialize networking | 113 | /* initialize networking |
114 | //bind to ip and port | 114 | bind to ip and port |
115 | //ip must be in network order EX: 127.0.0.1 = (7F000001) | 115 | ip must be in network order EX: 127.0.0.1 = (7F000001) |
116 | //port is in host byte order (this means don't worry about it) | 116 | port is in host byte order (this means don't worry about it) |
117 | //returns 0 if no problems | 117 | returns 0 if no problems |
118 | //TODO: add something to check if there are errors | 118 | TODO: add something to check if there are errors */ |
119 | int init_networking(IP ip ,uint16_t port); | 119 | int init_networking(IP ip ,uint16_t port); |
120 | 120 | ||
121 | 121 | ||
122 | //function to cleanup networking stuff(doesn't do much right now) | 122 | /* function to cleanup networking stuff(doesn't do much right now) */ |
123 | void shutdown_networking(); | 123 | void shutdown_networking(); |
124 | #endif | 124 | #endif |