summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core/Lossless_UDP.c14
-rw-r--r--core/net_crypto.c14
-rw-r--r--testing/nTox.c76
3 files changed, 49 insertions, 55 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
diff --git a/testing/nTox.c b/testing/nTox.c
index 81cac0b6..24d40ead 100644
--- a/testing/nTox.c
+++ b/testing/nTox.c
@@ -44,13 +44,37 @@ int x, y;
44uint8_t pending_requests[256][CLIENT_ID_SIZE]; 44uint8_t pending_requests[256][CLIENT_ID_SIZE];
45uint8_t num_requests = 0; 45uint8_t num_requests = 0;
46 46
47void get_id(char *data)
48{
49 char idstring0[200];
50 char idstring1[PUB_KEY_BYTES][5];
51 char idstring2[PUB_KEY_BYTES][5];
52 int i = 0;
53 for(i = 0; i < PUB_KEY_BYTES; i++)
54 {
55 if (self_public_key[i] < (PUB_KEY_BYTES / 2))
56 strcpy(idstring1[i],"0");
57 else
58 strcpy(idstring1[i], "");
59 sprintf(idstring2[i], "%hhX",self_public_key[i]);
60 }
61 strcpy(idstring0,"[i] ID: ");
62 int j = 0;
63 for (j = 0; j < PUB_KEY_BYTES; j++) {
64 strcat(idstring0,idstring1[j]);
65 strcat(idstring0,idstring2[j]);
66 }
67
68 memcpy(data, idstring0, strlen(idstring0));
69}
70
47void new_lines(char *line) 71void new_lines(char *line)
48{ 72{
49 int i; 73 int i = 0;
50 for (i = HISTORY-1; i > 0; i--) 74 for (i = HISTORY-1; i > 0; i--)
51 strcpy(lines[i], lines[i-1]); 75 strncpy(lines[i], lines[i-1], STRING_LENGTH - 1);
52 76
53 strcpy(lines[0], line); 77 strncpy(lines[0], line, STRING_LENGTH - 1);
54 do_refresh(); 78 do_refresh();
55} 79}
56 80
@@ -216,26 +240,9 @@ void line_eval(char lines[HISTORY][STRING_LENGTH], char *line)
216 new_lines("[i] /l list (list friends), /h for help, /i for info, /n nick (to change nickname), /q (to quit)"); 240 new_lines("[i] /l list (list friends), /h for help, /i for info, /n nick (to change nickname), /q (to quit)");
217 } 241 }
218 else if (inpt_command == 'i') { //info 242 else if (inpt_command == 'i') { //info
219 char idstring0[200]; 243 char idstring[200];
220 char idstring1[PUB_KEY_BYTES][5]; 244 get_id(idstring);
221 char idstring2[PUB_KEY_BYTES][5]; 245 new_lines(idstring);
222 int i;
223 for (i = 0; i < PUB_KEY_BYTES; i++)
224 {
225 if (self_public_key[i] < (PUB_KEY_BYTES/2))
226 strcpy(idstring1[i],"0");
227 else
228 strcpy(idstring1[i], "");
229 sprintf(idstring2[i], "%hhX", self_public_key[i]);
230 }
231 //
232 strcpy(idstring0,"[i] ID: ");
233 int j;
234 for (j = 0; j < PUB_KEY_BYTES; j++) {
235 strcat(idstring0,idstring1[j]);
236 strcat(idstring0,idstring2[j]);
237 }
238 new_lines(idstring0);
239 } 246 }
240 247
241 else if (inpt_command == 'q') { //exit 248 else if (inpt_command == 'q') { //exit
@@ -396,29 +403,14 @@ int main(int argc, char *argv[])
396 m_callback_friendmessage(print_message); 403 m_callback_friendmessage(print_message);
397 m_callback_namechange(print_nickchange); 404 m_callback_namechange(print_nickchange);
398 m_callback_userstatus(print_statuschange); 405 m_callback_userstatus(print_statuschange);
399 char idstring0[200]; 406
400 char idstring1[PUB_KEY_BYTES][5]; 407 char idstring[200];
401 char idstring2[PUB_KEY_BYTES][5]; 408 get_id(idstring);
402 int i;
403 for(i = 0; i < PUB_KEY_BYTES; i++)
404 {
405 if (self_public_key[i] < (PUB_KEY_BYTES / 2))
406 strcpy(idstring1[i],"0");
407 else
408 strcpy(idstring1[i], "");
409 sprintf(idstring2[i], "%hhX",self_public_key[i]);
410 }
411 strcpy(idstring0,"[i] your ID: ");
412 int j;
413 for (j = 0; j < PUB_KEY_BYTES; j++) {
414 strcat(idstring0,idstring1[j]);
415 strcat(idstring0,idstring2[j]);
416 }
417 initscr(); 409 initscr();
418 noecho(); 410 noecho();
419 raw(); 411 raw();
420 getmaxyx(stdscr, y, x); 412 getmaxyx(stdscr, y, x);
421 new_lines(idstring0); 413 new_lines(idstring);
422 new_lines(help); 414 new_lines(help);
423 strcpy(line, ""); 415 strcpy(line, "");
424 IP_Port bootstrap_ip_port; 416 IP_Port bootstrap_ip_port;