summaryrefslogtreecommitdiff
path: root/toxcore/network.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/network.c')
-rw-r--r--toxcore/network.c36
1 files changed, 25 insertions, 11 deletions
diff --git a/toxcore/network.c b/toxcore/network.c
index 5d308507..a5786801 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -309,31 +309,43 @@ void networking_poll(Networking_Core *net)
309 * returns 1 if there is socket activity (i.e. tox_do() should be called) 309 * returns 1 if there is socket activity (i.e. tox_do() should be called)
310 * 310 *
311 */ 311 */
312int networking_wait(Networking_Core *net, uint16_t milliseconds) 312int networking_wait(Networking_Core *net, uint32_t sendqueue_len, uint16_t milliseconds)
313{ 313{
314 sock_t sock = net->sock;
314 /* WIN32: supported since Win2K, but might need some adjustements */ 315 /* WIN32: supported since Win2K, but might need some adjustements */
315 /* UNIX: this should work for any remotely Unix'ish system */ 316 /* UNIX: this should work for any remotely Unix'ish system */
316 int nfds = 1 + net->sock; 317 int nfds = 1 + sock;
317 318
318 /* the FD_ZERO calls might be superfluous */ 319 /* the FD_ZERO calls might be superfluous */
319 fd_set readfds; 320 fd_set readfds;
320 FD_ZERO(&readfds); 321 FD_ZERO(&readfds);
321 FD_SET(net->sock, &readfds); 322 FD_SET(sock, &readfds);
322 323
323 fd_set writefds; 324 fd_set writefds;
324 FD_ZERO(&writefds); 325 FD_ZERO(&writefds);
325 FD_SET(net->sock, &writefds); 326 /* add only if we have packets queued, signals that a write won't block */
327 if (sendqueue_len > 0)
328 FD_SET(sock, &writefds);
326 329
327 fd_set exceptfds; 330 fd_set exceptfds;
328 FD_ZERO(&exceptfds); 331 FD_ZERO(&exceptfds);
329 FD_SET(net->sock, &exceptfds); 332 FD_SET(sock, &exceptfds);
330 333
331 struct timeval timeout; 334 struct timeval timeout;
332 timeout.tv_sec = 0; 335 timeout.tv_sec = 0;
333 timeout.tv_usec = milliseconds * 1000; 336 timeout.tv_usec = milliseconds * 1000;
334 337
338#ifdef LOGGING
339 errno = 0;
340#endif
335 /* returns -1 on error, 0 on timeout, the socket on activity */ 341 /* returns -1 on error, 0 on timeout, the socket on activity */
336 int res = select(nfds, &readfds, &writefds, &exceptfds, &timeout); 342 int res = select(nfds, &readfds, &writefds, &exceptfds, &timeout);
343#ifdef LOGGING
344 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),
346 FD_ISSET(sock, &exceptfds));
347 loglog(logbuffer);
348#endif
337 349
338 return res > 0 ? 1 : 0; 350 return res > 0 ? 1 : 0;
339}; 351};
@@ -486,6 +498,7 @@ Networking_Core *new_networking(IP ip, uint16_t port)
486 { 498 {
487 char ipv6only = 0; 499 char ipv6only = 0;
488#ifdef LOGGING 500#ifdef LOGGING
501 errno = 0;
489 int res = 502 int res =
490#endif 503#endif
491 setsockopt(temp->sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, sizeof(ipv6only)); 504 setsockopt(temp->sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, sizeof(ipv6only));
@@ -509,6 +522,7 @@ Networking_Core *new_networking(IP ip, uint16_t port)
509 mreq.ipv6mr_multiaddr.s6_addr[15] = 0x01; 522 mreq.ipv6mr_multiaddr.s6_addr[15] = 0x01;
510 mreq.ipv6mr_interface = 0; 523 mreq.ipv6mr_interface = 0;
511#ifdef LOGGING 524#ifdef LOGGING
525 errno = 0;
512 res = 526 res =
513#endif 527#endif
514 setsockopt(temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq)); 528 setsockopt(temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (char *)&mreq, sizeof(mreq));
@@ -961,23 +975,23 @@ int addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra)
961static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res) 975static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res)
962{ 976{
963 if (res < 0) 977 if (res < 0)
964 snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x\n", 978 snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3u%c %s:%u (%d: %s) | %04x%04x\n",
965 buffer[0], message, buflen < 999 ? buflen : 999, 'E', 979 buffer[0], message, buflen < 999 ? buflen : 999, 'E',
966 ip_ntoa(&ip_port->ip), ntohs(ip_port->port), errno, 980 ip_ntoa(&ip_port->ip), ntohs(ip_port->port), errno,
967 strerror(errno), buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0, 981 strerror(errno), buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0,
968 buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0); 982 (buflen > 7) ? ntohl(*(uint32_t *)(&buffer[5])) : 0);
969 else if ((res > 0) && ((size_t)res <= buflen)) 983 else if ((res > 0) && ((size_t)res <= buflen))
970 snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x\n", 984 snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3u%c %s:%u (%d: %s) | %04x%04x\n",
971 buffer[0], message, res < 999 ? res : 999, (size_t)res < buflen ? '<' : '=', 985 buffer[0], message, res < 999 ? res : 999, (size_t)res < buflen ? '<' : '=',
972 ip_ntoa(&ip_port->ip), ntohs(ip_port->port), 0, 986 ip_ntoa(&ip_port->ip), ntohs(ip_port->port), 0,
973 "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0, 987 "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0,
974 buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0); 988 (buflen > 7) ? ntohl(*(uint32_t *)(&buffer[5])) : 0);
975 else /* empty or overwrite */ 989 else /* empty or overwrite */
976 snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %u%c%u %s:%u (%u: %s) | %04x%04x\n", 990 snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %u%c%u %s:%u (%d: %s) | %04x%04x\n",
977 buffer[0], message, res, !res ? '!' : '>', buflen, 991 buffer[0], message, res, !res ? '!' : '>', buflen,
978 ip_ntoa(&ip_port->ip), ntohs(ip_port->port), 0, 992 ip_ntoa(&ip_port->ip), ntohs(ip_port->port), 0,
979 "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0, 993 "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0,
980 buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0); 994 (buflen > 7) ? ntohl(*(uint32_t *)(&buffer[5])) : 0);
981 995
982 logbuffer[sizeof(logbuffer) - 1] = 0; 996 logbuffer[sizeof(logbuffer) - 1] = 0;
983 loglog(logbuffer); 997 loglog(logbuffer);