summaryrefslogtreecommitdiff
path: root/toxcore/network.c
diff options
context:
space:
mode:
Diffstat (limited to 'toxcore/network.c')
-rw-r--r--toxcore/network.c76
1 files changed, 68 insertions, 8 deletions
diff --git a/toxcore/network.c b/toxcore/network.c
index 2969f3ac..0b5eba61 100644
--- a/toxcore/network.c
+++ b/toxcore/network.c
@@ -26,6 +26,7 @@
26#endif 26#endif
27 27
28#include "network.h" 28#include "network.h"
29#include "util.h"
29 30
30/* return current UNIX time in microseconds (us). */ 31/* return current UNIX time in microseconds (us). */
31uint64_t current_time(void) 32uint64_t current_time(void)
@@ -61,6 +62,10 @@ uint32_t random_int(void)
61#endif 62#endif
62} 63}
63 64
65#ifdef LOGGING
66static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res);
67#endif
68
64/* Basic network functions: 69/* Basic network functions:
65 * Function to send packet(data) of length length to ip_port. 70 * Function to send packet(data) of length length to ip_port.
66 */ 71 */
@@ -122,7 +127,11 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t le
122 } 127 }
123#endif 128#endif
124 129
125 return sendto(net->sock, (char *) data, length, 0, (struct sockaddr *)&addr, addrsize); 130 int res = sendto(net->sock, (char *) data, length, 0, (struct sockaddr *)&addr, addrsize);
131#ifdef LOGGING
132 loglogdata("O=>", data, length, &ip_port, res);
133#endif
134 return res;
126} 135}
127 136
128/* Function to receive data 137/* Function to receive data
@@ -131,11 +140,7 @@ int sendpacket(Networking_Core *net, IP_Port ip_port, uint8_t *data, uint32_t le
131 * Packet length is put into length. 140 * Packet length is put into length.
132 * Dump all empty packets. 141 * Dump all empty packets.
133 */ 142 */
134#ifdef WIN32 143static int receivepacket(sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t *length)
135static int receivepacket(unsigned int sock, IP_Port *ip_port, uint8_t *data, uint32_t *length)
136#else
137static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *length)
138#endif
139{ 144{
140 struct sockaddr_storage addr; 145 struct sockaddr_storage addr;
141#ifdef WIN32 146#ifdef WIN32
@@ -145,8 +150,13 @@ static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *le
145#endif 150#endif
146 (*(int32_t *)length) = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); 151 (*(int32_t *)length) = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
147 152
148 if (*(int32_t *)length <= 0) 153 if (*(int32_t *)length <= 0) {
154#ifdef LOGGING
155 if ((length < 0) && (errno != EWOULDBLOCK))
156 sprintf(logbuffer, "Unexpected error reading from socket: %u, %s\n", errno, strerror(errno));
157#endif
149 return -1; /* Nothing received or empty packet. */ 158 return -1; /* Nothing received or empty packet. */
159 }
150 160
151#ifdef TOX_ENABLE_IPV6 161#ifdef TOX_ENABLE_IPV6
152 if (addr.ss_family == AF_INET) { 162 if (addr.ss_family == AF_INET) {
@@ -173,6 +183,10 @@ static int receivepacket(int sock, IP_Port *ip_port, uint8_t *data, uint32_t *le
173 return -1; 183 return -1;
174#endif 184#endif
175 185
186#ifdef LOGGING
187 loglogdata("=>O", data, *length, ip_port, 0);
188#endif
189
176 return 0; 190 return 0;
177} 191}
178 192
@@ -307,6 +321,10 @@ Networking_Core *new_networking(IP ip, uint16_t port)
307 fcntl(temp->sock, F_SETFL, O_NONBLOCK, 1); 321 fcntl(temp->sock, F_SETFL, O_NONBLOCK, 1);
308#endif 322#endif
309 323
324#ifdef LOGGING
325 loginit(ntohs(port));
326#endif
327
310 /* Bind our socket to port PORT and the given IP address (usually 0.0.0.0 or ::) */ 328 /* Bind our socket to port PORT and the given IP address (usually 0.0.0.0 or ::) */
311 uint16_t *portptr = NULL; 329 uint16_t *portptr = NULL;
312 struct sockaddr_storage addr; 330 struct sockaddr_storage addr;
@@ -346,9 +364,33 @@ Networking_Core *new_networking(IP ip, uint16_t port)
346 if (ip.family == AF_INET6) { 364 if (ip.family == AF_INET6) {
347 char ipv6only = 0; 365 char ipv6only = 0;
348 int res = setsockopt(temp->sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, sizeof(ipv6only)); 366 int res = setsockopt(temp->sock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&ipv6only, sizeof(ipv6only));
367#ifdef LOGGING
368 if (res < 0) {
369 sprintf(logbuffer, "Failed to enable dual-stack on IPv6 socket, won't be able to receive from/send to IPv4 addresses. (%u, %s)\n",
370 errno, strerror(errno));
371 loglog(logbuffer);
372 }
373 else
374 loglog("Embedded IPv4 addresses enabled successfully.\n");
375#endif
376
377 /* multicast local nodes */
378 struct ipv6_mreq mreq;
379 memset(&mreq, 0, sizeof(mreq));
380 mreq.ipv6mr_multiaddr.s6_addr[ 0] = 0xFF;
381 mreq.ipv6mr_multiaddr.s6_addr[ 1] = 0x02;
382 mreq.ipv6mr_multiaddr.s6_addr[15] = 0x01;
383 mreq.ipv6mr_interface = 0;
384 res = setsockopt(temp->sock, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
385#ifdef LOGGING
349 if (res < 0) { 386 if (res < 0) {
350 /* add log message*/ 387 sprintf(logbuffer, "Failed to activate local multicast membership. (%u, %s)\n",
388 errno, strerror(errno));
389 loglog(logbuffer);
351 } 390 }
391 else
392 loglog("Local multicast group FF02::1 joined successfully.\n");
393#endif
352 } 394 }
353#endif 395#endif
354 396
@@ -363,6 +405,10 @@ Networking_Core *new_networking(IP ip, uint16_t port)
363 if (!res) 405 if (!res)
364 { 406 {
365 temp->port = *portptr; 407 temp->port = *portptr;
408#ifdef LOGGING
409 sprintf(logbuffer, "Bound successfully to %s:%u.\n", ip_ntoa(&ip), ntohs(temp->port));
410 loglog(logbuffer);
411#endif
366 return temp; 412 return temp;
367 } 413 }
368 414
@@ -733,3 +779,17 @@ int addr_resolve_or_parse_ip(const char *address, IP *to)
733 779
734 return 1; 780 return 1;
735}; 781};
782
783#ifdef LOGGING
784static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res)
785{
786 snprintf(logbuffer, sizeof(logbuffer), "[%2u] %3u%c %s %s:%u (%u: %s) | %04x%04x\n",
787 buffer[0], res < 0 ? (buflen & 0xFF) : res,
788 res < 0 ? '-' : (res == buflen ? '=' : '+'),
789 message, ip_ntoa(&ip_port->ip), ntohs(ip_port->port), res < 0 ? errno : 0,
790 res < 0 ? strerror(errno) : "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0,
791 buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0);
792 logbuffer[sizeof(logbuffer) - 1] = 0;
793 loglog(logbuffer);
794}
795#endif