diff options
author | Coren[m] <Break@Ocean> | 2013-09-11 11:44:05 +0200 |
---|---|---|
committer | Coren[m] <Break@Ocean> | 2013-09-11 11:44:05 +0200 |
commit | 5e076e35d92f8dc6d28060afee5160339cad11a3 (patch) | |
tree | 0d08f095c8f270db894ec75e13cbc2ef18f25306 | |
parent | 85912418db41d692b78b6bfe7a567ee2ca24e742 (diff) |
network.c: logging more details, fixing poll
LAN_discovery.c: IPv6: send both v6 multicast and v4 broadcast if socket allows
-rw-r--r-- | toxcore/LAN_discovery.c | 64 | ||||
-rw-r--r-- | toxcore/network.c | 40 |
2 files changed, 76 insertions, 28 deletions
diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c index 4cbe3177..933c2402 100644 --- a/toxcore/LAN_discovery.c +++ b/toxcore/LAN_discovery.c | |||
@@ -81,27 +81,35 @@ static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, uint8_t * d | |||
81 | #endif | 81 | #endif |
82 | 82 | ||
83 | /* Return the broadcast ip. */ | 83 | /* Return the broadcast ip. */ |
84 | static IP broadcast_ip(sa_family_t sa_family) | 84 | static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast) |
85 | { | 85 | { |
86 | IP ip; | 86 | IP ip; |
87 | ip_reset(&ip); | 87 | ip_reset(&ip); |
88 | 88 | ||
89 | #ifdef TOX_ENABLE_IPV6 | 89 | #ifdef TOX_ENABLE_IPV6 |
90 | if (sa_family == AF_INET) | 90 | if (family_socket == AF_INET6) { |
91 | { | 91 | if (family_broadcast == AF_INET6) { |
92 | ip.family = AF_INET; | 92 | ip.family = AF_INET6; |
93 | ip.ip4.uint32 = INADDR_BROADCAST; | 93 | /* FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */ |
94 | /* FE80::*: MUST be exact, for that we would need to look over all | ||
95 | * interfaces and check in which status they are */ | ||
96 | ip.ip6.s6_addr[ 0] = 0xFF; | ||
97 | ip.ip6.s6_addr[ 1] = 0x02; | ||
98 | ip.ip6.s6_addr[15] = 0x01; | ||
99 | } | ||
100 | else if (family_broadcast == AF_INET) { | ||
101 | ip.family = AF_INET6; | ||
102 | ip.ip6.s6_addr32[0] = 0; | ||
103 | ip.ip6.s6_addr32[1] = 0; | ||
104 | ip.ip6.s6_addr32[2] = htonl(0xFFFF); | ||
105 | ip.ip6.s6_addr32[3] = INADDR_BROADCAST; | ||
106 | } | ||
94 | } | 107 | } |
95 | 108 | else if (family_socket == AF_INET) { | |
96 | if (sa_family == AF_INET6) | 109 | if (family_broadcast == AF_INET) { |
97 | { | 110 | ip.family = AF_INET; |
98 | ip.family = AF_INET6; | 111 | ip.ip4.uint32 = INADDR_BROADCAST; |
99 | /* FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */ | 112 | } |
100 | /* FE80::*: MUST be exact, for that we would need to look over all | ||
101 | * interfaces and check in which status they are */ | ||
102 | ip.ip6.s6_addr[ 0] = 0xFF; | ||
103 | ip.ip6.s6_addr[ 1] = 0x02; | ||
104 | ip.ip6.s6_addr[15] = 0x01; | ||
105 | } | 113 | } |
106 | #else | 114 | #else |
107 | ip.uint32 = INADDR_BROADCAST; | 115 | ip.uint32 = INADDR_BROADCAST; |
@@ -175,13 +183,35 @@ int send_LANdiscovery(uint16_t port, Net_Crypto *c) | |||
175 | uint8_t data[crypto_box_PUBLICKEYBYTES + 1]; | 183 | uint8_t data[crypto_box_PUBLICKEYBYTES + 1]; |
176 | data[0] = NET_PACKET_LAN_DISCOVERY; | 184 | data[0] = NET_PACKET_LAN_DISCOVERY; |
177 | memcpy(data + 1, c->self_public_key, crypto_box_PUBLICKEYBYTES); | 185 | memcpy(data + 1, c->self_public_key, crypto_box_PUBLICKEYBYTES); |
186 | |||
178 | #ifdef __linux | 187 | #ifdef __linux |
179 | send_broadcasts(c->lossless_udp->net, port, data, 1 + crypto_box_PUBLICKEYBYTES); | 188 | send_broadcasts(c->lossless_udp->net, port, data, 1 + crypto_box_PUBLICKEYBYTES); |
180 | #endif | 189 | #endif |
190 | |||
191 | int res = -1; | ||
181 | IP_Port ip_port; | 192 | IP_Port ip_port; |
182 | ip_port.ip = broadcast_ip(c->lossless_udp->net->family); | ||
183 | ip_port.port = port; | 193 | ip_port.port = port; |
184 | return sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES); | 194 | |
195 | #ifdef TOX_ENABLE_IPV6 | ||
196 | if (c->lossless_udp->net->family == AF_INET6) { | ||
197 | ip_port.ip = broadcast_ip(c->lossless_udp->net->family, AF_INET6); | ||
198 | if (ip_isset(&ip_port.ip)) | ||
199 | if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES) > 0) | ||
200 | res = 1; | ||
201 | } | ||
202 | |||
203 | ip_port.ip = broadcast_ip(c->lossless_udp->net->family, AF_INET); | ||
204 | if (ip_isset(&ip_port.ip)) | ||
205 | if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES)) | ||
206 | res = 1; | ||
207 | #else | ||
208 | ip_port.ip = broadcast_ip(c->lossless_udp->net->family, AF_INET); | ||
209 | if (ip_isset(&ip_port.ip)) | ||
210 | if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES)) | ||
211 | res = 1; | ||
212 | #endif | ||
213 | |||
214 | return res; | ||
185 | } | 215 | } |
186 | 216 | ||
187 | 217 | ||
diff --git a/toxcore/network.c b/toxcore/network.c index 7eacd0ca..8d5b25b4 100644 --- a/toxcore/network.c +++ b/toxcore/network.c | |||
@@ -148,12 +148,15 @@ static int receivepacket(sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t | |||
148 | #else | 148 | #else |
149 | uint32_t addrlen = sizeof(addr); | 149 | uint32_t addrlen = sizeof(addr); |
150 | #endif | 150 | #endif |
151 | uint32_t bufflen = *length; | ||
151 | (*(int32_t *)length) = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); | 152 | (*(int32_t *)length) = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen); |
152 | 153 | ||
153 | if (*(int32_t *)length <= 0) { | 154 | if (*(int32_t *)length <= 0) { |
154 | #ifdef LOGGING | 155 | #ifdef LOGGING |
155 | if ((length < 0) && (errno != EWOULDBLOCK)) | 156 | if ((length < 0) && (errno != EWOULDBLOCK)) { |
156 | sprintf(logbuffer, "Unexpected error reading from socket: %u, %s\n", errno, strerror(errno)); | 157 | sprintf(logbuffer, "Unexpected error reading from socket: %u, %s\n", errno, strerror(errno)); |
158 | loglog(logbuffer); | ||
159 | } | ||
157 | #endif | 160 | #endif |
158 | return -1; /* Nothing received or empty packet. */ | 161 | return -1; /* Nothing received or empty packet. */ |
159 | } | 162 | } |
@@ -184,7 +187,7 @@ static int receivepacket(sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t | |||
184 | #endif | 187 | #endif |
185 | 188 | ||
186 | #ifdef LOGGING | 189 | #ifdef LOGGING |
187 | loglogdata("=>O", data, *length, ip_port, 0); | 190 | loglogdata("=>O", data, bufflen, ip_port, *length); |
188 | #endif | 191 | #endif |
189 | 192 | ||
190 | return 0; | 193 | return 0; |
@@ -202,10 +205,12 @@ void networking_poll(Networking_Core *net) | |||
202 | uint8_t data[MAX_UDP_PACKET_SIZE]; | 205 | uint8_t data[MAX_UDP_PACKET_SIZE]; |
203 | uint32_t length; | 206 | uint32_t length; |
204 | 207 | ||
205 | while (receivepacket(net->sock, &ip_port, data, &length) != -1) { | 208 | while (length = sizeof(data), receivepacket(net->sock, &ip_port, data, &length) != -1) { |
206 | if (length < 1) continue; | 209 | if (length < 1) |
210 | continue; | ||
207 | 211 | ||
208 | if (!(net->packethandlers[data[0]].function)) continue; | 212 | if (!(net->packethandlers[data[0]].function)) |
213 | continue; | ||
209 | 214 | ||
210 | net->packethandlers[data[0]].function(net->packethandlers[data[0]].object, ip_port, data, length); | 215 | net->packethandlers[data[0]].function(net->packethandlers[data[0]].object, ip_port, data, length); |
211 | } | 216 | } |
@@ -792,12 +797,25 @@ int addr_resolve_or_parse_ip(const char *address, IP *to) | |||
792 | #ifdef LOGGING | 797 | #ifdef LOGGING |
793 | static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res) | 798 | static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res) |
794 | { | 799 | { |
795 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %3u%c %s %s:%u (%u: %s) | %04x%04x\n", | 800 | if (res < 0) |
796 | buffer[0], res < 0 ? (buflen & 0xFF) : res, | 801 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x\n", |
797 | res < 0 ? '-' : (res == buflen ? '=' : '+'), | 802 | buffer[0], message, buflen < 999 ? buflen : 999, 'E', |
798 | message, ip_ntoa(&ip_port->ip), ntohs(ip_port->port), res < 0 ? errno : 0, | 803 | ip_ntoa(&ip_port->ip), ntohs(ip_port->port), errno, |
799 | res < 0 ? strerror(errno) : "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0, | 804 | strerror(errno), buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0, |
800 | buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0); | 805 | buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0); |
806 | else if ((res > 0) && (res <= buflen)) | ||
807 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x\n", | ||
808 | buffer[0], message, res < 999 ? res : 999, res < buflen ? '<' : '=', | ||
809 | ip_ntoa(&ip_port->ip), ntohs(ip_port->port), 0, | ||
810 | "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0, | ||
811 | buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0); | ||
812 | else /* empty or overwrite */ | ||
813 | snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %u%c%u %s:%u (%u: %s) | %04x%04x\n", | ||
814 | buffer[0], message, res, !res ? '0' : '>', buflen, | ||
815 | ip_ntoa(&ip_port->ip), ntohs(ip_port->port), 0, | ||
816 | "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0, | ||
817 | buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0); | ||
818 | |||
801 | logbuffer[sizeof(logbuffer) - 1] = 0; | 819 | logbuffer[sizeof(logbuffer) - 1] = 0; |
802 | loglog(logbuffer); | 820 | loglog(logbuffer); |
803 | } | 821 | } |