diff options
-rw-r--r-- | auto_tests/messenger_test.c | 4 | ||||
-rw-r--r-- | toxcore/Messenger.c | 18 | ||||
-rw-r--r-- | toxcore/Messenger.h | 12 | ||||
-rw-r--r-- | toxcore/network.c | 86 | ||||
-rw-r--r-- | toxcore/network.h | 12 | ||||
-rw-r--r-- | toxcore/tox.c | 19 | ||||
-rw-r--r-- | toxcore/tox.h | 18 |
7 files changed, 94 insertions, 75 deletions
diff --git a/auto_tests/messenger_test.c b/auto_tests/messenger_test.c index 6ede62ef..605fa4f3 100644 --- a/auto_tests/messenger_test.c +++ b/auto_tests/messenger_test.c | |||
@@ -253,7 +253,7 @@ START_TEST(test_dht_state_saveloadsave) | |||
253 | char msg[128]; | 253 | char msg[128]; |
254 | size_t offset = res >> 4; | 254 | size_t offset = res >> 4; |
255 | uint8_t *ptr = buffer + extra + offset; | 255 | uint8_t *ptr = buffer + extra + offset; |
256 | sprintf(msg, "Failed to load back stored buffer: 0x%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx @%u/%u, code %d", | 256 | sprintf(msg, "Failed to load back stored buffer: 0x%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx @%zu/%zu, code %d", |
257 | ptr[-2], ptr[-1], ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], offset, size, res & 0x0F); | 257 | ptr[-2], ptr[-1], ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], offset, size, res & 0x0F); |
258 | ck_assert_msg(res == 0, msg); | 258 | ck_assert_msg(res == 0, msg); |
259 | } | 259 | } |
@@ -295,7 +295,7 @@ START_TEST(test_messenger_state_saveloadsave) | |||
295 | char msg[128]; | 295 | char msg[128]; |
296 | size_t offset = res >> 4; | 296 | size_t offset = res >> 4; |
297 | uint8_t *ptr = buffer + extra + offset; | 297 | uint8_t *ptr = buffer + extra + offset; |
298 | sprintf(msg, "Failed to load back stored buffer: 0x%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx @%u/%u, code %d", | 298 | sprintf(msg, "Failed to load back stored buffer: 0x%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx%02hhx @%zu/%zu, code %d", |
299 | ptr[-2], ptr[-1], ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], offset, size, res & 0x0F); | 299 | ptr[-2], ptr[-1], ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], offset, size, res & 0x0F); |
300 | ck_assert_msg(res == 0, msg); | 300 | ck_assert_msg(res == 0, msg); |
301 | } | 301 | } |
diff --git a/toxcore/Messenger.c b/toxcore/Messenger.c index 09095e5f..e4387fcb 100644 --- a/toxcore/Messenger.c +++ b/toxcore/Messenger.c | |||
@@ -1390,18 +1390,16 @@ void doMessenger(Messenger *m) | |||
1390 | } | 1390 | } |
1391 | 1391 | ||
1392 | /* | 1392 | /* |
1393 | * Waits for something to happen on the socket for up to milliseconds milliseconds | 1393 | * functions to avoid excessive polling |
1394 | * *** Function MUSTN'T poll. *** | ||
1395 | * The function mustn't modify anything at all, so it can be called completely | ||
1396 | * asynchronously without any worry. | ||
1397 | * | ||
1398 | * returns 0 if the timeout was reached | ||
1399 | * returns 1 if there is socket activity (i.e. tox_do() should be called) | ||
1400 | * | ||
1401 | */ | 1394 | */ |
1402 | int waitMessenger(Messenger *m, uint16_t milliseconds) | 1395 | int waitprepareMessenger(Messenger *m, uint8_t *data, uint16_t *lenptr) |
1396 | { | ||
1397 | return networking_wait_prepare(m->net, sendqueue_total(m->net_crypto->lossless_udp), data, lenptr); | ||
1398 | } | ||
1399 | |||
1400 | int waitexecuteMessenger(Messenger *m, uint8_t *data, uint16_t len, uint16_t milliseconds) | ||
1403 | { | 1401 | { |
1404 | return networking_wait(m->net, sendqueue_total(m->net_crypto->lossless_udp), milliseconds); | 1402 | return networking_wait_execute(data, len, milliseconds); |
1405 | }; | 1403 | }; |
1406 | 1404 | ||
1407 | /* return size of the messenger data (for saving) */ | 1405 | /* return size of the messenger data (for saving) */ |
diff --git a/toxcore/Messenger.h b/toxcore/Messenger.h index 27e2c502..80450b1a 100644 --- a/toxcore/Messenger.h +++ b/toxcore/Messenger.h | |||
@@ -456,16 +456,10 @@ void cleanupMessenger(Messenger *M); | |||
456 | void doMessenger(Messenger *m); | 456 | void doMessenger(Messenger *m); |
457 | 457 | ||
458 | /* | 458 | /* |
459 | * Waits for something to happen on the socket for up to milliseconds milliseconds | 459 | * functions to avoid excessive polling |
460 | * *** Function MUSTN'T poll. *** | ||
461 | * The function mustn't modify anything at all, so it can be called completely | ||
462 | * asynchronously without any worry. | ||
463 | * | ||
464 | * returns 0 if the timeout was reached | ||
465 | * returns 1 if there is socket activity (i.e. tox_do() should be called) | ||
466 | * | ||
467 | */ | 460 | */ |
468 | int waitMessenger(Messenger *m, uint16_t milliseconds); | 461 | int waitprepareMessenger(Messenger *m, uint8_t *data, uint16_t *lenptr); |
462 | int waitexecuteMessenger(Messenger *m, uint8_t *data, uint16_t len, uint16_t milliseconds); | ||
469 | 463 | ||
470 | /* SAVING AND LOADING FUNCTIONS: */ | 464 | /* SAVING AND LOADING FUNCTIONS: */ |
471 | 465 | ||
diff --git a/toxcore/network.c b/toxcore/network.c index a5786801..3e533799 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -25,6 +25,10 @@ | |||
25 | #include "config.h" | 25 | #include "config.h" |
26 | #endif | 26 | #endif |
27 | 27 | ||
28 | #ifndef WIN32 | ||
29 | #include <errno.h> | ||
30 | #endif | ||
31 | |||
28 | #include "network.h" | 32 | #include "network.h" |
29 | #include "util.h" | 33 | #include "util.h" |
30 | 34 | ||
@@ -300,36 +304,53 @@ void networking_poll(Networking_Core *net) | |||
300 | } | 304 | } |
301 | 305 | ||
302 | /* | 306 | /* |
303 | * Waits for something to happen on the socket for up to milliseconds milliseconds | 307 | * function to avoid excessive polling |
304 | * *** Function MUSTN'T poll. *** | ||
305 | * The function mustn't modify anything at all, so it can be called completely | ||
306 | * asynchronously without any worry. | ||
307 | * | ||
308 | * returns 0 if the timeout was reached | ||
309 | * returns 1 if there is socket activity (i.e. tox_do() should be called) | ||
310 | * | ||
311 | */ | 308 | */ |
312 | int networking_wait(Networking_Core *net, uint32_t sendqueue_len, uint16_t milliseconds) | 309 | typedef struct |
310 | { | ||
311 | sock_t sock; | ||
312 | uint32_t sendqueue_length; | ||
313 | } select_info; | ||
314 | |||
315 | int networking_wait_prepare(Networking_Core *net, uint32_t sendqueue_length, uint8_t *data, uint16_t *lenptr) | ||
316 | { | ||
317 | if ((data == NULL) || (*lenptr < sizeof(select_info))) | ||
318 | { | ||
319 | *lenptr = sizeof(select_info); | ||
320 | return 0; | ||
321 | } | ||
322 | |||
323 | *lenptr = sizeof(select_info); | ||
324 | select_info *s = (select_info *)data; | ||
325 | s->sock = net->sock; | ||
326 | s->sendqueue_length = sendqueue_length; | ||
327 | |||
328 | return 1; | ||
329 | } | ||
330 | |||
331 | int networking_wait_execute(uint8_t *data, uint16_t len, uint16_t milliseconds) | ||
313 | { | 332 | { |
314 | sock_t sock = net->sock; | ||
315 | /* WIN32: supported since Win2K, but might need some adjustements */ | 333 | /* WIN32: supported since Win2K, but might need some adjustements */ |
316 | /* UNIX: this should work for any remotely Unix'ish system */ | 334 | /* UNIX: this should work for any remotely Unix'ish system */ |
317 | int nfds = 1 + sock; | 335 | |
336 | select_info *s = (select_info *)data; | ||
337 | |||
338 | int nfds = 1 + s->sock; | ||
318 | 339 | ||
319 | /* the FD_ZERO calls might be superfluous */ | 340 | /* the FD_ZERO calls might be superfluous */ |
320 | fd_set readfds; | 341 | fd_set readfds; |
321 | FD_ZERO(&readfds); | 342 | FD_ZERO(&readfds); |
322 | FD_SET(sock, &readfds); | 343 | FD_SET(s->sock, &readfds); |
323 | 344 | ||
324 | fd_set writefds; | 345 | fd_set writefds; |
325 | FD_ZERO(&writefds); | 346 | FD_ZERO(&writefds); |
326 | /* add only if we have packets queued, signals that a write won't block */ | 347 | /* add only if we have packets queued, signals that a write won't block */ |
327 | if (sendqueue_len > 0) | 348 | if (s->sendqueue_length > 0) |
328 | FD_SET(sock, &writefds); | 349 | FD_SET(s->sock, &writefds); |
329 | 350 | ||
330 | fd_set exceptfds; | 351 | fd_set exceptfds; |
331 | FD_ZERO(&exceptfds); | 352 | FD_ZERO(&exceptfds); |
332 | FD_SET(sock, &exceptfds); | 353 | FD_SET(s->sock, &exceptfds); |
333 | 354 | ||
334 | struct timeval timeout; | 355 | struct timeval timeout; |
335 | timeout.tv_sec = 0; | 356 | timeout.tv_sec = 0; |
@@ -342,8 +363,8 @@ int networking_wait(Networking_Core *net, uint32_t sendqueue_len, uint16_t milli | |||
342 | int res = select(nfds, &readfds, &writefds, &exceptfds, &timeout); | 363 | int res = select(nfds, &readfds, &writefds, &exceptfds, &timeout); |
343 | #ifdef LOGGING | 364 | #ifdef LOGGING |
344 | sprintf(logbuffer, "select(%d): %d (%d, %s) - %d %d %d\n", milliseconds, res, errno, | 365 | sprintf(logbuffer, "select(%d): %d (%d, %s) - %d %d %d\n", milliseconds, res, errno, |
345 | strerror(errno), FD_ISSET(sock, &readfds), FD_ISSET(sock, &writefds), | 366 | strerror(errno), FD_ISSET(s->sock, &readfds), FD_ISSET(s->sock, &writefds), |
346 | FD_ISSET(sock, &exceptfds)); | 367 | FD_ISSET(s->sock, &exceptfds)); |
347 | loglog(logbuffer); | 368 | loglog(logbuffer); |
348 | #endif | 369 | #endif |
349 | 370 | ||
@@ -972,26 +993,27 @@ int addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra) | |||
972 | }; | 993 | }; |
973 | 994 | ||
974 | #ifdef LOGGING | 995 | #ifdef LOGGING |
996 | static char errmsg_ok[3] = "OK"; | ||
975 | static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res) | 997 | static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res) |
976 | { | 998 | { |
999 | uint16_t port = ntohs(ip_port->port); | ||
1000 | uint32_t data[2]; | ||
1001 | data[0] = buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0; | ||
1002 | data[1] = buflen > 7 ? ntohl(*(uint32_t *)&buffer[5]) : 0; | ||
977 | if (res < 0) | 1003 | if (res < 0) |
978 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3u%c %s:%u (%d: %s) | %04x%04x\n", | 1004 | { |
979 | buffer[0], message, buflen < 999 ? buflen : 999, 'E', | 1005 | int written = snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3hu%c %s:%hu (%u: %s) | %04x%04x\n", |
980 | ip_ntoa(&ip_port->ip), ntohs(ip_port->port), errno, | 1006 | buffer[0], message, (buflen < 999 ? (uint16_t)buflen : 999), 'E', |
981 | strerror(errno), buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0, | 1007 | ip_ntoa(&ip_port->ip), port, errno, strerror(errno), data[0], data[1]); |
982 | (buflen > 7) ? ntohl(*(uint32_t *)(&buffer[5])) : 0); | 1008 | } |
983 | else if ((res > 0) && ((size_t)res <= buflen)) | 1009 | else if ((res > 0) && ((size_t)res <= buflen)) |
984 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3u%c %s:%u (%d: %s) | %04x%04x\n", | 1010 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3zu%c %s:%hu (%u: %s) | %04x%04x\n", |
985 | buffer[0], message, res < 999 ? res : 999, (size_t)res < buflen ? '<' : '=', | 1011 | buffer[0], message, (res < 999 ? (size_t)res : 999), ((size_t)res < buflen ? '<' : '='), |
986 | ip_ntoa(&ip_port->ip), ntohs(ip_port->port), 0, | 1012 | ip_ntoa(&ip_port->ip), port, 0, errmsg_ok, data[0], data[1]); |
987 | "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0, | ||
988 | (buflen > 7) ? ntohl(*(uint32_t *)(&buffer[5])) : 0); | ||
989 | else /* empty or overwrite */ | 1013 | else /* empty or overwrite */ |
990 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %u%c%u %s:%u (%d: %s) | %04x%04x\n", | 1014 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %zu%c%zu %s:%hu (%u: %s) | %04x%04x\n", |
991 | buffer[0], message, res, !res ? '!' : '>', buflen, | 1015 | buffer[0], message, (size_t)res, (!res ? '!' : '>'), buflen, |
992 | ip_ntoa(&ip_port->ip), ntohs(ip_port->port), 0, | 1016 | ip_ntoa(&ip_port->ip), port, 0, errmsg_ok, data[0], data[1]); |
993 | "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0, | ||
994 | (buflen > 7) ? ntohl(*(uint32_t *)(&buffer[5])) : 0); | ||
995 | 1017 | ||
996 | logbuffer[sizeof(logbuffer) - 1] = 0; | 1018 | logbuffer[sizeof(logbuffer) - 1] = 0; |
997 | loglog(logbuffer); | 1019 | loglog(logbuffer); |
diff --git a/toxcore/network.h b/toxcore/network.h index d143b48d..54df7d2e 100644 --- a/toxcore/network.h +++ b/toxcore/network.h | |||
@@ -265,16 +265,10 @@ void networking_registerhandler(Networking_Core *net, uint8_t byte, packet_handl | |||
265 | void networking_poll(Networking_Core *net); | 265 | void networking_poll(Networking_Core *net); |
266 | 266 | ||
267 | /* | 267 | /* |
268 | * Waits for something to happen on the socket for up to milliseconds milliseconds | 268 | * functions to avoid excessive polling |
269 | * *** Function MUSTN'T poll. *** | ||
270 | * The function mustn't modify anything at all, so it can be called completely | ||
271 | * asynchronously without any worry. | ||
272 | * | ||
273 | * returns 0 if the timeout was reached | ||
274 | * returns 1 if there is socket activity (i.e. tox_do() should be called) | ||
275 | * | ||
276 | */ | 269 | */ |
277 | int networking_wait(Networking_Core *net, uint32_t sendqueue_len, uint16_t milliseconds); | 270 | int networking_wait_prepare(Networking_Core *net, uint32_t sendqueue_length, uint8_t *data, uint16_t *lenptr); |
271 | int networking_wait_execute(uint8_t *data, uint16_t len, uint16_t milliseconds); | ||
278 | 272 | ||
279 | /* Initialize networking. | 273 | /* Initialize networking. |
280 | * bind to ip and port. | 274 | * bind to ip and port. |
diff --git a/toxcore/tox.c b/toxcore/tox.c index 080909d3..b2107389 100644 --- a/toxcore/tox.c +++ b/toxcore/tox.c | |||
@@ -537,19 +537,18 @@ void tox_do(Tox *tox) | |||
537 | } | 537 | } |
538 | 538 | ||
539 | /* | 539 | /* |
540 | * Waits for something to happen on the socket for up to milliseconds milliseconds | 540 | * functions to avoid excessive polling |
541 | * *** Function MUSTN'T poll. *** | ||
542 | * The function mustn't modify anything at all, so it can be called completely | ||
543 | * asynchronously without any worry. | ||
544 | * | ||
545 | * returns 0 if the timeout was reached | ||
546 | * returns 1 if there is socket activity (i.e. tox_do() should be called) | ||
547 | * | ||
548 | */ | 541 | */ |
549 | int tox_wait(Tox *tox, uint16_t milliseconds) | 542 | int tox_wait_prepare(Tox *tox, uint8_t *data, uint16_t *lenptr) |
543 | { | ||
544 | Messenger *m = tox; | ||
545 | waitprepareMessenger(m, data, lenptr); | ||
546 | } | ||
547 | |||
548 | int tox_wait_execute(Tox *tox, uint8_t *data, uint16_t len, uint16_t milliseconds) | ||
550 | { | 549 | { |
551 | Messenger *m = tox; | 550 | Messenger *m = tox; |
552 | waitMessenger(m, milliseconds); | 551 | waitexecuteMessenger(m, data, len, milliseconds); |
553 | } | 552 | } |
554 | 553 | ||
555 | /* SAVING AND LOADING FUNCTIONS: */ | 554 | /* SAVING AND LOADING FUNCTIONS: */ |
diff --git a/toxcore/tox.h b/toxcore/tox.h index e497e6f5..3278a9d3 100644 --- a/toxcore/tox.h +++ b/toxcore/tox.h | |||
@@ -482,16 +482,28 @@ void tox_kill(Tox *tox); | |||
482 | void tox_do(Tox *tox); | 482 | void tox_do(Tox *tox); |
483 | 483 | ||
484 | /* | 484 | /* |
485 | * tox_wait_prepare(): function should be called under lock | ||
486 | * Prepares the data required to call tox_wait_execute() asynchronously | ||
487 | * | ||
488 | * data[] is reserved and kept by the caller | ||
489 | * len is in/out: in = reserved data[], out = required data[] | ||
490 | * | ||
491 | * returns 1 on success | ||
492 | * returns 0 on failure (length is insufficient) | ||
493 | * | ||
494 | * tox_wait_execute(): function can be called asynchronously | ||
485 | * Waits for something to happen on the socket for up to milliseconds milliseconds. | 495 | * Waits for something to happen on the socket for up to milliseconds milliseconds. |
486 | * *** Function MUSTN'T poll. *** | 496 | * *** Function MUSTN'T poll. *** |
487 | * The function mustn't modify anything at all, so it can be called completely | 497 | * The function mustn't modify anything at all, so it can be called completely |
488 | * asynchronously without any worry. | 498 | * asynchronously without any worry. |
489 | * | 499 | * |
490 | * returns 0 if the timeout was reached | 500 | * returns 1 if there is socket activity (i.e. tox_do() should be called) |
491 | * returns 1 if there is socket activity (i.e. tox_do() should be called) | 501 | * returns 0 if the timeout was reached |
502 | * returns -1 if data was NULL or len too short | ||
492 | * | 503 | * |
493 | */ | 504 | */ |
494 | int tox_wait(Tox *tox, uint16_t milliseconds); | 505 | int tox_wait_prepare(Tox *tox, uint8_t *data, uint16_t *lenptr); |
506 | int tox_wait_execute(Tox *tox, uint8_t *data, uint16_t len, uint16_t milliseconds); | ||
495 | 507 | ||
496 | /* SAVING AND LOADING FUNCTIONS: */ | 508 | /* SAVING AND LOADING FUNCTIONS: */ |
497 | 509 | ||