mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
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:
parent
85912418db
commit
5e076e35d9
|
@ -81,27 +81,35 @@ static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, uint8_t * d
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Return the broadcast ip. */
|
/* 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 ip;
|
||||||
ip_reset(&ip);
|
ip_reset(&ip);
|
||||||
|
|
||||||
#ifdef TOX_ENABLE_IPV6
|
#ifdef TOX_ENABLE_IPV6
|
||||||
if (sa_family == AF_INET)
|
if (family_socket == AF_INET6) {
|
||||||
{
|
if (family_broadcast == AF_INET6) {
|
||||||
ip.family = AF_INET;
|
ip.family = AF_INET6;
|
||||||
ip.ip4.uint32 = INADDR_BROADCAST;
|
/* 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
else if (family_socket == AF_INET) {
|
||||||
if (sa_family == AF_INET6)
|
if (family_broadcast == AF_INET) {
|
||||||
{
|
ip.family = AF_INET;
|
||||||
ip.family = AF_INET6;
|
ip.ip4.uint32 = INADDR_BROADCAST;
|
||||||
/* 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
|
#else
|
||||||
ip.uint32 = INADDR_BROADCAST;
|
ip.uint32 = INADDR_BROADCAST;
|
||||||
|
@ -175,13 +183,35 @@ int send_LANdiscovery(uint16_t port, Net_Crypto *c)
|
||||||
uint8_t data[crypto_box_PUBLICKEYBYTES + 1];
|
uint8_t data[crypto_box_PUBLICKEYBYTES + 1];
|
||||||
data[0] = NET_PACKET_LAN_DISCOVERY;
|
data[0] = NET_PACKET_LAN_DISCOVERY;
|
||||||
memcpy(data + 1, c->self_public_key, crypto_box_PUBLICKEYBYTES);
|
memcpy(data + 1, c->self_public_key, crypto_box_PUBLICKEYBYTES);
|
||||||
|
|
||||||
#ifdef __linux
|
#ifdef __linux
|
||||||
send_broadcasts(c->lossless_udp->net, port, data, 1 + crypto_box_PUBLICKEYBYTES);
|
send_broadcasts(c->lossless_udp->net, port, data, 1 + crypto_box_PUBLICKEYBYTES);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
int res = -1;
|
||||||
IP_Port ip_port;
|
IP_Port ip_port;
|
||||||
ip_port.ip = broadcast_ip(c->lossless_udp->net->family);
|
|
||||||
ip_port.port = port;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -148,12 +148,15 @@ static int receivepacket(sock_t sock, IP_Port *ip_port, uint8_t *data, uint32_t
|
||||||
#else
|
#else
|
||||||
uint32_t addrlen = sizeof(addr);
|
uint32_t addrlen = sizeof(addr);
|
||||||
#endif
|
#endif
|
||||||
|
uint32_t bufflen = *length;
|
||||||
(*(int32_t *)length) = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
|
(*(int32_t *)length) = recvfrom(sock, (char *) data, MAX_UDP_PACKET_SIZE, 0, (struct sockaddr *)&addr, &addrlen);
|
||||||
|
|
||||||
if (*(int32_t *)length <= 0) {
|
if (*(int32_t *)length <= 0) {
|
||||||
#ifdef LOGGING
|
#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));
|
sprintf(logbuffer, "Unexpected error reading from socket: %u, %s\n", errno, strerror(errno));
|
||||||
|
loglog(logbuffer);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
return -1; /* Nothing received or empty packet. */
|
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
|
#endif
|
||||||
|
|
||||||
#ifdef LOGGING
|
#ifdef LOGGING
|
||||||
loglogdata("=>O", data, *length, ip_port, 0);
|
loglogdata("=>O", data, bufflen, ip_port, *length);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -202,10 +205,12 @@ void networking_poll(Networking_Core *net)
|
||||||
uint8_t data[MAX_UDP_PACKET_SIZE];
|
uint8_t data[MAX_UDP_PACKET_SIZE];
|
||||||
uint32_t length;
|
uint32_t length;
|
||||||
|
|
||||||
while (receivepacket(net->sock, &ip_port, data, &length) != -1) {
|
while (length = sizeof(data), receivepacket(net->sock, &ip_port, data, &length) != -1) {
|
||||||
if (length < 1) continue;
|
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);
|
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
|
#ifdef LOGGING
|
||||||
static void loglogdata(char *message, uint8_t *buffer, size_t buflen, IP_Port *ip_port, ssize_t res)
|
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",
|
if (res < 0)
|
||||||
buffer[0], res < 0 ? (buflen & 0xFF) : res,
|
snprintf(logbuffer, sizeof(logbuffer), "[%2u] %s %3u%c %s:%u (%u: %s) | %04x%04x\n",
|
||||||
res < 0 ? '-' : (res == buflen ? '=' : '+'),
|
buffer[0], message, buflen < 999 ? buflen : 999, 'E',
|
||||||
message, ip_ntoa(&ip_port->ip), ntohs(ip_port->port), res < 0 ? errno : 0,
|
ip_ntoa(&ip_port->ip), ntohs(ip_port->port), errno,
|
||||||
res < 0 ? strerror(errno) : "OK", buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0,
|
strerror(errno), buflen > 4 ? ntohl(*(uint32_t *)&buffer[1]) : 0,
|
||||||
buflen > 7 ? ntohl(*(uint32_t *)(&buffer[5])) : 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;
|
logbuffer[sizeof(logbuffer) - 1] = 0;
|
||||||
loglog(logbuffer);
|
loglog(logbuffer);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user