/* LAN_discovery.c * * LAN discovery implementation. * * Copyright (C) 2013 Tox project All Rights Reserved. * * This file is part of Tox. * * Tox is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Tox is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Tox. If not, see . * */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "LAN_discovery.h" #define MAX_INTERFACES 16 #ifdef __linux #ifndef TOX_ENABLE_IPV6 /* Send packet to all broadcast addresses * * return higher than 0 on success. * return 0 on error. * * TODO: Make this work with IPv6 and remove the #ifndef TOX_ENABLE_IPV6. */ static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, uint8_t *data, uint16_t length) { /* Not sure how many platforms this will run on, * so it's wrapped in __linux for now. */ struct sockaddr_in *sock_holder = NULL; struct ifreq i_faces[MAX_INTERFACES]; struct ifconf ifconf; int count = 0; int sock = 0; int i = 0; /* Configure ifconf for the ioctl call. */ if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { return 1; } memset(i_faces, 0, sizeof(struct ifreq) * MAX_INTERFACES); ifconf.ifc_buf = (char *)i_faces; ifconf.ifc_len = sizeof(i_faces); count = ifconf.ifc_len / sizeof(struct ifreq); if (ioctl(sock, SIOCGIFCONF, &ifconf) < 0) { return 1; } for (i = 0; i < count; i++) { if (ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) { return 1; } /* Just to clarify where we're getting the values from. */ sock_holder = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr; if (sock_holder != NULL) { IP_Port ip_port = {{{{sock_holder->sin_addr.s_addr}}, port, 0}}; sendpacket(net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES); } } close(sock); return 0; } #endif #endif /* Return the broadcast ip. */ static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast) { IP ip; ip_reset(&ip); #ifdef TOX_ENABLE_IPV6 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.uint8[ 0] = 0xFF; ip.ip6.uint8[ 1] = 0x02; ip.ip6.uint8[15] = 0x01; } else if (family_broadcast == AF_INET) { ip.family = AF_INET6; ip.ip6.uint32[0] = 0; ip.ip6.uint32[1] = 0; ip.ip6.uint32[2] = htonl(0xFFFF); ip.ip6.uint32[3] = INADDR_BROADCAST; } } else if (family_socket == AF_INET) { if (family_broadcast == AF_INET) { ip.family = AF_INET; ip.ip4.uint32 = INADDR_BROADCAST; } } #else if (family_socket == AF_INET) if (family_broadcast == AF_INET) ip.uint32 = INADDR_BROADCAST; #endif return ip; } /* return 0 if ip is a LAN ip. * return -1 if it is not. */ static int LAN_ip(IP ip) { #ifdef TOX_ENABLE_IPV6 if (ip.family == AF_INET) { IP4 ip4 = ip.ip4; #else IP4 ip4 = ip; #endif /* Loopback. */ if (ip4.uint8[0] == 127) return 0; /* 10.0.0.0 to 10.255.255.255 range. */ if (ip4.uint8[0] == 10) return 0; /* 172.16.0.0 to 172.31.255.255 range. */ if (ip4.uint8[0] == 172 && ip4.uint8[1] >= 16 && ip4.uint8[1] <= 31) return 0; /* 192.168.0.0 to 192.168.255.255 range. */ if (ip4.uint8[0] == 192 && ip4.uint8[1] == 168) return 0; /* 169.254.1.0 to 169.254.254.255 range. */ if (ip4.uint8[0] == 169 && ip4.uint8[1] == 254 && ip4.uint8[2] != 0 && ip4.uint8[2] != 255) return 0; #ifdef TOX_ENABLE_IPV6 } else if (ip.family == AF_INET6) { /* autogenerated for each interface: FE80::* (up to FEBF::*) FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */ if (((ip.ip6.uint8[0] == 0xFF) && (ip.ip6.uint8[1] < 3) && (ip.ip6.uint8[15] == 1)) || ((ip.ip6.uint8[0] == 0xFE) && ((ip.ip6.uint8[1] & 0xC0) == 0x80))) return 0; /* embedded IPv4-in-IPv6 */ if (IN6_IS_ADDR_V4MAPPED(&ip.ip6.in6_addr)) { IP ip4; ip4.family = AF_INET; ip4.ip4.uint32 = ip.ip6.uint32[3]; return LAN_ip(ip4); } } #endif return -1; } static int handle_LANdiscovery(void *object, IP_Port source, uint8_t *packet, uint32_t length) { DHT *dht = object; if (LAN_ip(source.ip) == -1) return 1; if (length != crypto_box_PUBLICKEYBYTES + 1) return 1; DHT_bootstrap(dht, source, packet + 1); return 0; } 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 #ifndef TOX_ENABLE_IPV6 send_broadcasts(c->lossless_udp->net, port, data, 1 + crypto_box_PUBLICKEYBYTES); #endif #endif int res = -1; IP_Port ip_port; ip_port.port = port; #ifdef TOX_ENABLE_IPV6 /* IPv6 multicast */ if (c->lossless_udp->net->family == AF_INET6) { ip_port.ip = broadcast_ip(AF_INET6, AF_INET6); if (ip_isset(&ip_port.ip)) if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES) > 0) res = 1; } /* IPv4 broadcast (has to be IPv4-in-IPv6 mapping if socket is AF_INET6 */ ip_port.ip = broadcast_ip(c->lossless_udp->net->family, AF_INET); #else ip_port.ip = broadcast_ip(AF_INET, AF_INET); #endif if (ip_isset(&ip_port.ip)) if (sendpacket(c->lossless_udp->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES)) res = 1; return res; } void LANdiscovery_init(DHT *dht) { networking_registerhandler(dht->c->lossless_udp->net, NET_PACKET_LAN_DISCOVERY, &handle_LANdiscovery, dht); }