summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/Lossless_UDP.c14
-rw-r--r--core/net_crypto.c14
2 files changed, 15 insertions, 13 deletions
diff --git a/core/Lossless_UDP.c b/core/Lossless_UDP.c
index 4affc38f..33b8eb19 100644
--- a/core/Lossless_UDP.c
+++ b/core/Lossless_UDP.c
@@ -202,15 +202,16 @@ int new_connection(IP_Port ip_port)
202 for (i = 0; i < MAX_CONNECTIONS; ++i) { 202 for (i = 0; i < MAX_CONNECTIONS; ++i) {
203 if(connections[i].status == 0) { 203 if(connections[i].status == 0) {
204 memset(&connections[i], 0, sizeof(Connection)); 204 memset(&connections[i], 0, sizeof(Connection));
205 uint32_t handshake_id1 = handshake_id(ip_port);
205 206
206 connections[i] = (Connection) { 207 connections[i] = (Connection) {
207 .ip_port = ip_port, 208 .ip_port = ip_port,
208 .status = 1, 209 .status = 1,
209 .inbound = 0, 210 .inbound = 0,
210 .handshake_id1 = handshake_id(ip_port), 211 .handshake_id1 = handshake_id1,
211 .sent_packetnum = connections[i].handshake_id1, 212 .sent_packetnum = handshake_id1,
212 .sendbuff_packetnum = connections[i].handshake_id1, 213 .sendbuff_packetnum = handshake_id1,
213 .successful_sent = connections[i].handshake_id1, 214 .successful_sent = handshake_id1,
214 .SYNC_rate = SYNC_RATE, 215 .SYNC_rate = SYNC_RATE,
215 .data_rate = DATA_SYNC_RATE, 216 .data_rate = DATA_SYNC_RATE,
216 .last_recvSYNC = current_time(), 217 .last_recvSYNC = current_time(),
@@ -254,6 +255,7 @@ int new_inconnection(IP_Port ip_port)
254 for (i = 0; i < MAX_CONNECTIONS; ++i) { 255 for (i = 0; i < MAX_CONNECTIONS; ++i) {
255 if (connections[i].status == 0) { 256 if (connections[i].status == 0) {
256 memset(&connections[i], 0, sizeof(Connection)); 257 memset(&connections[i], 0, sizeof(Connection));
258 uint64_t timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT;
257 259
258 connections[i] = (Connection){ 260 connections[i] = (Connection){
259 .ip_port = ip_port, 261 .ip_port = ip_port,
@@ -266,10 +268,10 @@ int new_inconnection(IP_Port ip_port)
266 .send_counter = 127, 268 .send_counter = 127,
267 269
268 /* add randomness to timeout to prevent connections getting stuck in a loop. */ 270 /* add randomness to timeout to prevent connections getting stuck in a loop. */
269 .timeout = CONNEXION_TIMEOUT + rand() % CONNEXION_TIMEOUT, 271 .timeout = timeout,
270 272
271 /* if this connection isn't handled within the timeout kill it. */ 273 /* if this connection isn't handled within the timeout kill it. */
272 .killat = current_time() + 1000000UL*connections[i].timeout 274 .killat = current_time() + 1000000UL*timeout
273 }; 275 };
274 ++connections_number; 276 ++connections_number;
275 return i; 277 return i;
diff --git a/core/net_crypto.c b/core/net_crypto.c
index 31fb24be..3b5b67f4 100644
--- a/core/net_crypto.c
+++ b/core/net_crypto.c
@@ -66,11 +66,11 @@ static int incoming_connections[MAX_INCOMING];
66int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce, 66int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
67 uint8_t *plain, uint32_t length, uint8_t *encrypted) 67 uint8_t *plain, uint32_t length, uint8_t *encrypted)
68{ 68{
69 if (length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES > MAX_DATA_SIZE || length == 0) 69 if (length + crypto_box_MACBYTES > MAX_DATA_SIZE || length == 0)
70 return -1; 70 return -1;
71 71
72 uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_ZEROBYTES - crypto_box_BOXZEROBYTES] = {0}; 72 uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES] = {0};
73 uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES]; 73 uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES];
74 74
75 memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length); /* pad the message with 32 0 bytes. */ 75 memcpy(temp_plain + crypto_box_ZEROBYTES, plain, length); /* pad the message with 32 0 bytes. */
76 76
@@ -87,7 +87,7 @@ int encrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
87 return -1; 87 return -1;
88 88
89 /* unpad the encrypted message */ 89 /* unpad the encrypted message */
90 memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES); 90 memcpy(encrypted, temp_encrypted + crypto_box_BOXZEROBYTES, length + crypto_box_MACBYTES);
91 return length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES; 91 return length - crypto_box_BOXZEROBYTES + crypto_box_ZEROBYTES;
92} 92}
93 93
@@ -101,8 +101,8 @@ int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
101 if (length > MAX_DATA_SIZE || length <= crypto_box_BOXZEROBYTES) 101 if (length > MAX_DATA_SIZE || length <= crypto_box_BOXZEROBYTES)
102 return -1; 102 return -1;
103 103
104 uint8_t temp_plain[MAX_DATA_SIZE - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES]; 104 uint8_t temp_plain[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES];
105 uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_ZEROBYTES] = {0}; 105 uint8_t temp_encrypted[MAX_DATA_SIZE + crypto_box_BOXZEROBYTES] = {0};
106 106
107 memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); /* pad the message with 16 0 bytes. */ 107 memcpy(temp_encrypted + crypto_box_BOXZEROBYTES, encrypted, length); /* pad the message with 16 0 bytes. */
108 108
@@ -121,7 +121,7 @@ int decrypt_data(uint8_t *public_key, uint8_t *secret_key, uint8_t *nonce,
121 return -1; 121 return -1;
122 122
123 /* unpad the plain message */ 123 /* unpad the plain message */
124 memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES); 124 memcpy(plain, temp_plain + crypto_box_ZEROBYTES, length - crypto_box_MACBYTES);
125 return length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES; 125 return length - crypto_box_ZEROBYTES + crypto_box_BOXZEROBYTES;
126} 126}
127 127