network.c: logging more details, fixing poll

LAN_discovery.c: IPv6: send both v6 multicast and v4 broadcast if socket allows
This commit is contained in:
Coren[m] 2013-09-11 11:44:05 +02:00
parent 85912418db
commit 5e076e35d9
2 changed files with 76 additions and 28 deletions

View File

@ -81,27 +81,35 @@ static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, uint8_t * d
#endif
/* Return the broadcast ip. */
static IP broadcast_ip(sa_family_t sa_family)
static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast)
{
IP ip;
ip_reset(&ip);
#ifdef TOX_ENABLE_IPV6
if (sa_family == AF_INET)
{
ip.family = AF_INET;
ip.ip4.uint32 = INADDR_BROADCAST;
if (family_socket == AF_INET6) {
if (family_broadcast == AF_INET6) {
ip.family = AF_INET6;
/* FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */
/* FE80::*: MUST be exact, for that we would need to look over all
* interfaces and check in which status they are */
ip.ip6.s6_addr[ 0] = 0xFF;
ip.ip6.s6_addr[ 1] = 0x02;
ip.ip6.s6_addr[15] = 0x01;
}
else if (family_broadcast == AF_INET) {
ip.family = AF_INET6;
ip.ip6.s6_addr32[0] = 0;
ip.ip6.s6_addr32[1] = 0;
ip.ip6.s6_addr32[2] = htonl(0xFFFF);
ip.ip6.s6_addr32[3] = INADDR_BROADCAST;
}
}
if (sa_family == AF_INET6)
{
ip.family = AF_INET6;
/* FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */
/* FE80::*: MUST be exact, for that we would need to look over all
* interfaces and check in which status they are */
ip.ip6.s6_addr[ 0] = 0xFF;
ip.ip6.s6_addr[ 1] = 0x02;
ip.ip6.s6_addr[15] = 0x01;
else if (family_socket == AF_INET) {
if (family_broadcast == AF_INET) {
ip.family = AF_INET;
ip.ip4.uint32 = INADDR_BROADCAST;
}
}
#else
ip.uint32 = INADDR_BROADCAST;
@ -175,13 +183,35 @@ int send_LANdiscovery(uint16_t port, Net_Crypto *c)
uint8_t data[crypto_box_PUBLICKEYBYTES + 1];
data[0] = NET_PACKET_LAN_DISCOVERY;
memcpy(data + 1, c->self_public_key, crypto_box_PUBLICKEYBYTES);
#ifdef __linux
send_broadcasts(c->lossless_udp->net, port, data, 1 + crypto_box_PUBLICKEYBYTES);
#endif
int res = -1;
IP_Port ip_port;
ip_port.ip = broadcast_ip(c->lossless_udp->net->family);
ip_port.port = port;
return sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES);
#ifdef TOX_ENABLE_IPV6
if (c->lossless_udp->net->family == AF_INET6) {
ip_port.ip = broadcast_ip(c->lossless_udp->net->family, AF_INET6);
if (ip_isset(&ip_port.ip))
if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES) > 0)
res = 1;
}
ip_port.ip = broadcast_ip(c->lossless_udp->net->family, AF_INET);
if (ip_isset(&ip_port.ip))
if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES))
res = 1;
#else
ip_port.ip = broadcast_ip(c->lossless_udp->net->family, AF_INET);
if (ip_isset(&ip_port.ip))
if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES))
res = 1;
#endif
return res;
}

View File

@ -148,12 +148,15 @@ static int receivepacket(sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t
#else
uint32_t addrlen = sizeof(addr);
#endif
uint32_t bufflen = *length;
(*(int32_t *)length) = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
if (*(int32_t *)length <= 0) {
#ifdef LOGGING
if ((length < 0) && (errno != EWOULDBLOCK))
if ((length < 0) && (errno != EWOULDBLOCK)) {
sprintf(logbuffer, "Unexpected error reading from socket: %u, %s\n", errno, strerror(errno));
loglog(logbuffer);
}
#endif
return -1; /* Nothing received or empty packet. */
}
@ -184,7 +187,7 @@ static int receivepacket(sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t
#endif
#ifdef LOGGING
loglogdata("=>O", data, *length, ip_port, 0);
loglogdata("=>O", data, bufflen, ip_port, *length);
#endif
return 0;
@ -202,10 +205,12 @@ void networking_poll(Networking_Core *net)
uint8_t data[MAX_UDP_PACKET_SIZE];
uint32_t length;
while (receivepacket(net->sock, &ip_port, data, &length) != -1) {
if (length < 1) continue;
while (length = sizeof(data), receivepacket(net->sock, &ip_port, data, &length) != -1) {
if (length < 1)
continue;
if (!(net->packethandlers[data[0]].function)) continue;
if (!(net->packethandlers[data[0]].function))
continue;
net->packethandlers[data[0]].function(net->packethandlers[data[0]].object, ip_port, data, length);
}
@ -792,12 +797,25 @@ int addr_resolve_or_parse_ip(const char *address, IP *to)
#ifdef LOGGING
static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res)
{
snprintf(logbuffer, sizeof(logbuffer), "[%2u] %3u%c %s %s:%u (%u: %s) | %04x%04x\n",
buffer[0], res < 0 ? (buflen & 0xFF) : res,
res < 0 ? '-' : (res == buflen ? '=' : '+'),
message, ip_ntoa(&ip_port->ip), ntohs(ip_port->port), res < 0 ? errno : 0,
res < 0 ? strerror(errno) : "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0,
buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0);
if (res < 0)
snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x\n",
buffer[0], message, buflen < 999 ? buflen : 999, 'E',
ip_ntoa(&ip_port->ip), ntohs(ip_port->port), errno,
strerror(errno), buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0,
buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0);
else if ((res > 0) && (res <= buflen))
snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x\n",
buffer[0], message, res < 999 ? res : 999, res < buflen ? '<' : '=',
ip_ntoa(&ip_port->ip), ntohs(ip_port->port), 0,
"OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0,
buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0);
else /* empty or overwrite */
snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %u%c%u %s:%u (%u: %s) | %04x%04x\n",
buffer[0], message, res, !res ? '0' : '>', buflen,
ip_ntoa(&ip_port->ip), ntohs(ip_port->port), 0,
"OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0,
buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 0);
logbuffer[sizeof(logbuffer) - 1] = 0;
loglog(logbuffer);
}