summaryrefslogtreecommitdiff
path: root/toxcore/network.c
diff options
context:
space:
mode:
authorMaxim Biro <nurupo.contributions@gmail.com>2018-04-07 22:09:42 -0400
committerMaxim Biro <nurupo.contributions@gmail.com>2018-04-17 19:07:50 -0400
commit7d399cedcfd20f0d91a8caf386ae3c63f4dcf285 (patch)
treea0f4134961bb163151532afe3fb5e98927965738 /toxcore/network.c
parent2824daf74a6d2bd60ebaf387a30f1b7719b6b67c (diff)
Improve network error reporting on Windows
Windows doesn't report network errors though errno, it has its own facilities.
Diffstat (limited to 'toxcore/network.c')
-rw-r--r--toxcore/network.c73
1 files changed, 61 insertions, 12 deletions
diff --git a/toxcore/network.c b/toxcore/network.c
index d88ba816..984071d4 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -63,15 +63,15 @@
63#include <sys/time.h> 63#include <sys/time.h>
64#include <sys/types.h> 64#include <sys/types.h>
65 65
66#define TOX_EWOULDBLOCK EWOULDBLOCK
67
66#else 68#else
67 69
68#ifndef IPV6_V6ONLY 70#ifndef IPV6_V6ONLY
69#define IPV6_V6ONLY 27 71#define IPV6_V6ONLY 27
70#endif 72#endif
71 73
72#ifndef EWOULDBLOCK 74#define TOX_EWOULDBLOCK WSAEWOULDBLOCK
73#define EWOULDBLOCK WSAEWOULDBLOCK
74#endif
75 75
76static const char *inet_ntop(Family family, const void *addr, char *buf, size_t bufsize) 76static const char *inet_ntop(Family family, const void *addr, char *buf, size_t bufsize)
77{ 77{
@@ -376,10 +376,13 @@ static void loglogdata(Logger *log, const char *message, const uint8_t *buffer,
376 char ip_str[IP_NTOA_LEN]; 376 char ip_str[IP_NTOA_LEN];
377 377
378 if (res < 0) { /* Windows doesn't necessarily know %zu */ 378 if (res < 0) { /* Windows doesn't necessarily know %zu */
379 int error = net_error();
380 const char *strerror = net_new_strerror(error);
379 LOGGER_TRACE(log, "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x", 381 LOGGER_TRACE(log, "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x",
380 buffer[0], message, (buflen < 999 ? buflen : 999), 'E', 382 buffer[0], message, (buflen < 999 ? buflen : 999), 'E',
381 ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), net_ntohs(ip_port.port), errno, 383 ip_ntoa(&ip_port.ip, ip_str, sizeof(ip_str)), net_ntohs(ip_port.port), error,
382 strerror(errno), data_0(buflen, buffer), data_1(buflen, buffer)); 384 strerror, data_0(buflen, buffer), data_1(buflen, buffer));
385 net_kill_strerror(strerror);
383 } else if ((res > 0) && ((size_t)res <= buflen)) { 386 } else if ((res > 0) && ((size_t)res <= buflen)) {
384 LOGGER_TRACE(log, "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x", 387 LOGGER_TRACE(log, "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x",
385 buffer[0], message, (res < 999 ? res : 999), ((size_t)res < buflen ? '<' : '='), 388 buffer[0], message, (res < 999 ? res : 999), ((size_t)res < buflen ? '<' : '='),
@@ -498,9 +501,12 @@ static int receivepacket(Logger *log, Socket sock, IP_Port *ip_port, uint8_t *da
498 int fail_or_len = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); 501 int fail_or_len = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
499 502
500 if (fail_or_len < 0) { 503 if (fail_or_len < 0) {
504 int error = net_error();
501 505
502 if (fail_or_len < 0 && errno != EWOULDBLOCK) { 506 if (fail_or_len < 0 && error != TOX_EWOULDBLOCK) {
503 LOGGER_ERROR(log, "Unexpected error reading from socket: %u, %s", errno, strerror(errno)); 507 const char *strerror = net_new_strerror(error);
508 LOGGER_ERROR(log, "Unexpected error reading from socket: %u, %s", error, strerror);
509 net_kill_strerror(strerror);
504 } 510 }
505 511
506 return -1; /* Nothing received. */ 512 return -1; /* Nothing received. */
@@ -681,7 +687,10 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
681 687
682 /* Check for socket error. */ 688 /* Check for socket error. */
683 if (!sock_valid(temp->sock)) { 689 if (!sock_valid(temp->sock)) {
684 LOGGER_ERROR(log, "Failed to get a socket?! %u, %s", errno, strerror(errno)); 690 int neterror = net_error();
691 const char *strerror = net_new_strerror(neterror);
692 LOGGER_ERROR(log, "Failed to get a socket?! %d, %s", neterror, strerror);
693 net_kill_strerror(strerror);
685 free(temp); 694 free(temp);
686 695
687 if (error) { 696 if (error) {
@@ -769,8 +778,11 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
769 mreq.ipv6mr_interface = 0; 778 mreq.ipv6mr_interface = 0;
770 int res = setsockopt(temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (const char *)&mreq, sizeof(mreq)); 779 int res = setsockopt(temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, (const char *)&mreq, sizeof(mreq));
771 780
772 LOGGER_DEBUG(log, res < 0 ? "Failed to activate local multicast membership. (%u, %s)" : 781 int neterror = net_error();
773 "Local multicast group FF02::1 joined successfully", errno, strerror(errno)); 782 const char *strerror = net_new_strerror(neterror);
783 LOGGER_DEBUG(log, res < 0 ? "Failed to activate local multicast membership. (%d, %s)" :
784 "Local multicast group FF02::1 joined successfully", neterror, strerror);
785 net_kill_strerror(strerror);
774 } 786 }
775 787
776 /* a hanging program or a different user might block the standard port; 788 /* a hanging program or a different user might block the standard port;
@@ -827,9 +839,11 @@ Networking_Core *new_networking_ex(Logger *log, IP ip, uint16_t port_from, uint1
827 } 839 }
828 840
829 char ip_str[IP_NTOA_LEN]; 841 char ip_str[IP_NTOA_LEN];
830 LOGGER_ERROR(log, "Failed to bind socket: %u, %s IP: %s port_from: %u port_to: %u", errno, strerror(errno), 842 int neterror = net_error();
843 const char *strerror = net_new_strerror(neterror);
844 LOGGER_ERROR(log, "Failed to bind socket: %d, %s IP: %s port_from: %u port_to: %u", neterror, strerror,
831 ip_ntoa(&ip, ip_str, sizeof(ip_str)), port_from, port_to); 845 ip_ntoa(&ip, ip_str, sizeof(ip_str)), port_from, port_to);
832 846 net_kill_strerror(strerror);
833 kill_networking(temp); 847 kill_networking(temp);
834 848
835 if (error) { 849 if (error) {
@@ -1520,3 +1534,38 @@ size_t net_unpack_u64(const uint8_t *bytes, uint64_t *v)
1520 *v = ((uint64_t)hi << 32) | lo; 1534 *v = ((uint64_t)hi << 32) | lo;
1521 return p - bytes; 1535 return p - bytes;
1522} 1536}
1537
1538int net_error(void)
1539{
1540#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
1541 return WSAGetLastError();
1542#else
1543 return errno;
1544#endif
1545}
1546
1547const char *net_new_strerror(int error)
1548{
1549#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
1550 char *str = nullptr;
1551 // Windows API is weird. The 5th function arg is of char* type, but we
1552 // have to pass char** so that it could assign new memory block to our
1553 // pointer, so we have to cast our char** to char* for the compilation
1554 // not to fail (otherwise it would fail to find a variant of this function
1555 // accepting char** as the 5th arg) and Windows inside casts it back
1556 // to char** to do the assignment. So no, this cast you see here, although
1557 // it looks weird, is not a mistake.
1558 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, nullptr,
1559 error, 0, (char *)&str, 0, nullptr);
1560 return str;
1561#else
1562 return strerror(error);
1563#endif
1564}
1565
1566void net_kill_strerror(const char *strerror)
1567{
1568#if defined(_WIN32) || defined(__WIN32__) || defined(WIN32)
1569 LocalFree((char *)strerror);
1570#endif
1571}