diff options
Diffstat (limited to 'toxcore/network.c')
-rw-r--r-- | toxcore/network.c | 17 |
1 files changed, 10 insertions, 7 deletions
diff --git a/toxcore/network.c b/toxcore/network.c index 4b6dc3be..4f6b0aae 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -154,14 +154,15 @@ static int receivepacket(sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t | |||
154 | #ifdef WIN32 | 154 | #ifdef WIN32 |
155 | int addrlen = sizeof(addr); | 155 | int addrlen = sizeof(addr); |
156 | #else | 156 | #else |
157 | uint32_t addrlen = sizeof(addr); | 157 | socklen_t addrlen = sizeof(addr); |
158 | #endif | 158 | #endif |
159 | (*(int32_t *)length) = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); | 159 | *length = 0; |
160 | int fail_or_len = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); | ||
160 | 161 | ||
161 | if (*(int32_t *)length <= 0) { | 162 | if (fail_or_len <= 0) { |
162 | #ifdef LOGGING | 163 | #ifdef LOGGING |
163 | 164 | ||
164 | if ((length < 0) && (errno != EWOULDBLOCK)) { | 165 | if ((fail_or_len < 0) && (errno != EWOULDBLOCK)) { |
165 | sprintf(logbuffer, "Unexpected error reading from socket: %u, %s\n", errno, strerror(errno)); | 166 | sprintf(logbuffer, "Unexpected error reading from socket: %u, %s\n", errno, strerror(errno)); |
166 | loglog(logbuffer); | 167 | loglog(logbuffer); |
167 | } | 168 | } |
@@ -170,6 +171,8 @@ static int receivepacket(sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t | |||
170 | return -1; /* Nothing received or empty packet. */ | 171 | return -1; /* Nothing received or empty packet. */ |
171 | } | 172 | } |
172 | 173 | ||
174 | *length = (uint32_t)fail_or_len; | ||
175 | |||
173 | #ifdef TOX_ENABLE_IPV6 | 176 | #ifdef TOX_ENABLE_IPV6 |
174 | 177 | ||
175 | if (addr.ss_family == AF_INET) { | 178 | if (addr.ss_family == AF_INET) { |
@@ -863,15 +866,15 @@ static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *i | |||
863 | ip_ntoa(&ip_port->ip), ntohs(ip_port->port), errno, | 866 | ip_ntoa(&ip_port->ip), ntohs(ip_port->port), errno, |
864 | strerror(errno), buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0, | 867 | strerror(errno), buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0, |
865 | buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0); | 868 | buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0); |
866 | else if ((res > 0) && (res <= buflen)) | 869 | else if ((res > 0) && ((size_t)res <= buflen)) |
867 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x\n", | 870 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x\n", |
868 | buffer[0], message, res < 999 ? res : 999, res < buflen ? '<' : '=', | 871 | buffer[0], message, res < 999 ? res : 999, (size_t)res < buflen ? '<' : '=', |
869 | ip_ntoa(&ip_port->ip), ntohs(ip_port->port), 0, | 872 | ip_ntoa(&ip_port->ip), ntohs(ip_port->port), 0, |
870 | "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0, | 873 | "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0, |
871 | buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0); | 874 | buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0); |
872 | else /* empty or overwrite */ | 875 | else /* empty or overwrite */ |
873 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %u%c%u %s:%u (%u: %s) | %04x%04x\n", | 876 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %u%c%u %s:%u (%u: %s) | %04x%04x\n", |
874 | buffer[0], message, res, !res ? '0' : '>', buflen, | 877 | buffer[0], message, res, !res ? '!' : '>', buflen, |
875 | ip_ntoa(&ip_port->ip), ntohs(ip_port->port), 0, | 878 | ip_ntoa(&ip_port->ip), ntohs(ip_port->port), 0, |
876 | "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0, | 879 | "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0, |
877 | buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0); | 880 | buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0); |