2013-07-27 05:06:03 +08:00
|
|
|
/* LAN_discovery.c
|
2013-07-27 20:43:36 +08:00
|
|
|
*
|
2013-07-27 05:06:03 +08:00
|
|
|
* LAN discovery implementation.
|
2013-07-27 20:43:36 +08:00
|
|
|
*
|
2013-07-27 05:06:03 +08:00
|
|
|
* 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 <http://www.gnu.org/licenses/>.
|
2013-07-27 20:43:36 +08:00
|
|
|
*
|
2013-07-26 22:24:56 +08:00
|
|
|
*/
|
|
|
|
|
2013-09-05 18:02:26 +08:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2013-07-26 22:31:49 +08:00
|
|
|
#include "LAN_discovery.h"
|
2013-10-25 04:32:28 +08:00
|
|
|
#include "util.h"
|
2013-07-26 22:24:56 +08:00
|
|
|
|
2013-08-02 04:25:22 +08:00
|
|
|
#define MAX_INTERFACES 16
|
2013-07-26 22:24:56 +08:00
|
|
|
|
2013-10-20 22:56:12 +08:00
|
|
|
|
|
|
|
static int broadcast_count = -1;
|
|
|
|
static IP_Port broadcast_ip_port[MAX_INTERFACES];
|
|
|
|
|
2014-07-21 08:50:53 +08:00
|
|
|
#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
|
|
|
|
|
|
|
|
#include <iphlpapi.h>
|
|
|
|
|
|
|
|
static void fetch_broadcast_info(uint16_t port)
|
|
|
|
{
|
|
|
|
broadcast_count = 0;
|
|
|
|
|
|
|
|
IP_ADAPTER_INFO *pAdapterInfo = malloc(sizeof(pAdapterInfo));
|
|
|
|
unsigned long ulOutBufLen = sizeof(pAdapterInfo);
|
|
|
|
|
|
|
|
if (pAdapterInfo == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
|
|
|
|
free(pAdapterInfo);
|
|
|
|
pAdapterInfo = malloc(ulOutBufLen);
|
|
|
|
|
|
|
|
if (pAdapterInfo == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if ((ret = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
|
|
|
|
IP_ADAPTER_INFO *pAdapter = pAdapterInfo;
|
|
|
|
|
|
|
|
while (pAdapter) {
|
|
|
|
IP gateway = {0}, subnet_mask = {0};
|
|
|
|
|
|
|
|
if (addr_parse_ip(pAdapter->IpAddressList.IpMask.String, &subnet_mask)
|
|
|
|
&& addr_parse_ip(pAdapter->GatewayList.IpAddress.String, &gateway)) {
|
|
|
|
if (gateway.family == AF_INET && subnet_mask.family == AF_INET) {
|
|
|
|
IP_Port *ip_port = &broadcast_ip_port[broadcast_count];
|
|
|
|
ip_port->ip.family = AF_INET;
|
|
|
|
uint32_t gateway_ip = ntohl(gateway.ip4.uint32), subnet_ip = ntohl(subnet_mask.ip4.uint32);
|
|
|
|
uint32_t broadcast_ip = gateway_ip + ~subnet_ip - 1;
|
|
|
|
ip_port->ip.ip4.uint32 = htonl(broadcast_ip);
|
|
|
|
ip_port->port = port;
|
|
|
|
broadcast_count++;
|
|
|
|
|
|
|
|
if (broadcast_count >= MAX_INTERFACES) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pAdapter = pAdapter->Next;
|
|
|
|
}
|
|
|
|
}
|
2014-10-18 04:03:02 +08:00
|
|
|
|
2014-10-17 15:22:18 +08:00
|
|
|
if (pAdapterInfo) {
|
|
|
|
free(pAdapterInfo);
|
|
|
|
}
|
2014-07-21 08:50:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#elif defined(__linux__)
|
|
|
|
|
2013-10-20 22:56:12 +08:00
|
|
|
static void fetch_broadcast_info(uint16_t port)
|
2013-08-02 04:25:22 +08:00
|
|
|
{
|
2013-08-30 05:17:51 +08:00
|
|
|
/* Not sure how many platforms this will run on,
|
|
|
|
* so it's wrapped in __linux for now.
|
2013-10-20 22:56:12 +08:00
|
|
|
* Definitely won't work like this on Windows...
|
2013-08-30 05:17:51 +08:00
|
|
|
*/
|
2013-10-20 22:56:12 +08:00
|
|
|
broadcast_count = 0;
|
|
|
|
sock_t sock = 0;
|
2013-10-21 04:00:30 +08:00
|
|
|
|
2013-10-20 22:56:12 +08:00
|
|
|
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
|
|
|
|
return;
|
2013-08-02 04:25:22 +08:00
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* Configure ifconf for the ioctl call. */
|
2013-10-20 22:56:12 +08:00
|
|
|
struct ifreq i_faces[MAX_INTERFACES];
|
2013-08-02 04:25:22 +08:00
|
|
|
memset(i_faces, 0, sizeof(struct ifreq) * MAX_INTERFACES);
|
|
|
|
|
2013-10-20 22:56:12 +08:00
|
|
|
struct ifconf ifconf;
|
2013-08-02 04:25:22 +08:00
|
|
|
ifconf.ifc_buf = (char *)i_faces;
|
|
|
|
ifconf.ifc_len = sizeof(i_faces);
|
2013-08-17 01:11:09 +08:00
|
|
|
|
|
|
|
if (ioctl(sock, SIOCGIFCONF, &ifconf) < 0) {
|
2013-10-20 22:56:12 +08:00
|
|
|
close(sock);
|
|
|
|
return;
|
2013-08-02 04:25:22 +08:00
|
|
|
}
|
|
|
|
|
2013-10-20 22:56:12 +08:00
|
|
|
/* ifconf.ifc_len is set by the ioctl() to the actual length used;
|
|
|
|
* on usage of the complete array the call should be repeated with
|
|
|
|
* a larger array, not done (640kB and 16 interfaces shall be
|
|
|
|
* enough, for everybody!)
|
|
|
|
*/
|
|
|
|
int i, count = ifconf.ifc_len / sizeof(struct ifreq);
|
2013-10-21 04:00:30 +08:00
|
|
|
|
2013-08-17 01:11:09 +08:00
|
|
|
for (i = 0; i < count; i++) {
|
2013-10-20 22:56:12 +08:00
|
|
|
/* there are interfaces with are incapable of broadcast */
|
|
|
|
if (ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* moot check: only AF_INET returned (backwards compat.) */
|
|
|
|
if (i_faces[i].ifr_broadaddr.sa_family != AF_INET)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
struct sockaddr_in *sock4 = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr;
|
2013-10-21 04:00:30 +08:00
|
|
|
|
2014-04-27 00:30:29 +08:00
|
|
|
if (broadcast_count >= MAX_INTERFACES) {
|
|
|
|
close(sock);
|
2013-10-21 04:00:30 +08:00
|
|
|
return;
|
2014-04-27 00:30:29 +08:00
|
|
|
}
|
2013-10-21 04:00:30 +08:00
|
|
|
|
2013-10-20 22:56:12 +08:00
|
|
|
IP_Port *ip_port = &broadcast_ip_port[broadcast_count];
|
|
|
|
ip_port->ip.family = AF_INET;
|
|
|
|
ip_port->ip.ip4.in_addr = sock4->sin_addr;
|
|
|
|
ip_port->port = port;
|
|
|
|
broadcast_count++;
|
2013-08-02 04:25:22 +08:00
|
|
|
}
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-02 04:25:22 +08:00
|
|
|
close(sock);
|
|
|
|
}
|
2013-10-20 22:56:12 +08:00
|
|
|
|
2014-07-21 08:50:53 +08:00
|
|
|
#else //TODO: Other platforms?
|
|
|
|
|
|
|
|
static void fetch_broadcast_info(uint16_t port)
|
|
|
|
{
|
|
|
|
broadcast_count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2013-10-20 22:56:12 +08:00
|
|
|
/* Send packet to all IPv4 broadcast addresses
|
|
|
|
*
|
|
|
|
* return 1 if sent to at least one broadcast target.
|
|
|
|
* return 0 on failure to find any valid broadcast target.
|
|
|
|
*/
|
2014-06-14 04:55:15 +08:00
|
|
|
static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, const uint8_t *data, uint16_t length)
|
2013-10-20 22:56:12 +08:00
|
|
|
{
|
|
|
|
/* fetch only once? on every packet? every X seconds?
|
|
|
|
* old: every packet, new: once */
|
|
|
|
if (broadcast_count < 0)
|
|
|
|
fetch_broadcast_info(port);
|
|
|
|
|
|
|
|
if (!broadcast_count)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
int i;
|
|
|
|
|
2013-10-21 04:00:30 +08:00
|
|
|
for (i = 0; i < broadcast_count; i++)
|
2013-11-20 00:18:30 +08:00
|
|
|
sendpacket(net, broadcast_ip_port[i], data, length);
|
2013-10-20 22:56:12 +08:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2013-08-02 04:25:22 +08:00
|
|
|
|
2013-08-30 05:17:51 +08:00
|
|
|
/* Return the broadcast ip. */
|
2013-09-11 17:44:05 +08:00
|
|
|
static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast)
|
2013-07-26 22:24:56 +08:00
|
|
|
{
|
2013-09-10 22:36:20 +08:00
|
|
|
IP ip;
|
|
|
|
ip_reset(&ip);
|
|
|
|
|
2013-09-11 17:44:05 +08:00
|
|
|
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 */
|
2013-09-15 21:31:27 +08:00
|
|
|
ip.ip6.uint8[ 0] = 0xFF;
|
|
|
|
ip.ip6.uint8[ 1] = 0x02;
|
|
|
|
ip.ip6.uint8[15] = 0x01;
|
2013-09-15 00:42:17 +08:00
|
|
|
} else if (family_broadcast == AF_INET) {
|
2013-09-11 17:44:05 +08:00
|
|
|
ip.family = AF_INET6;
|
2013-09-15 21:31:27 +08:00
|
|
|
ip.ip6.uint32[0] = 0;
|
|
|
|
ip.ip6.uint32[1] = 0;
|
|
|
|
ip.ip6.uint32[2] = htonl(0xFFFF);
|
|
|
|
ip.ip6.uint32[3] = INADDR_BROADCAST;
|
2013-09-11 17:44:05 +08:00
|
|
|
}
|
2013-09-15 00:42:17 +08:00
|
|
|
} else if (family_socket == AF_INET) {
|
2013-09-11 17:44:05 +08:00
|
|
|
if (family_broadcast == AF_INET) {
|
|
|
|
ip.family = AF_INET;
|
|
|
|
ip.ip4.uint32 = INADDR_BROADCAST;
|
|
|
|
}
|
2013-09-10 22:36:20 +08:00
|
|
|
}
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-07-26 22:24:56 +08:00
|
|
|
return ip;
|
|
|
|
}
|
|
|
|
|
2013-09-02 20:41:43 +08:00
|
|
|
/* return 0 if ip is a LAN ip.
|
|
|
|
* return -1 if it is not.
|
2013-08-30 05:17:51 +08:00
|
|
|
*/
|
2013-09-28 03:34:13 +08:00
|
|
|
int LAN_ip(IP ip)
|
2013-07-26 22:24:56 +08:00
|
|
|
{
|
2013-09-10 22:36:20 +08:00
|
|
|
if (ip.family == AF_INET) {
|
|
|
|
IP4 ip4 = ip.ip4;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-09-10 22:36:20 +08:00
|
|
|
/* 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;
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-12-06 03:46:50 +08:00
|
|
|
/* RFC 6598: 100.64.0.0 to 100.127.255.255 (100.64.0.0/10)
|
|
|
|
* (shared address space to stack another layer of NAT) */
|
|
|
|
if ((ip4.uint8[0] == 100) && ((ip4.uint8[1] & 0xC0) == 0x40))
|
|
|
|
return 0;
|
|
|
|
|
2013-10-20 22:56:12 +08:00
|
|
|
} else if (ip.family == AF_INET6) {
|
|
|
|
|
2013-09-10 22:36:20 +08:00
|
|
|
/* autogenerated for each interface: FE80::* (up to FEBF::*)
|
2013-09-15 07:15:26 +08:00
|
|
|
FF02::1 is - according to RFC 4291 - multicast all-nodes link-local */
|
2013-09-15 21:31:27 +08:00
|
|
|
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)))
|
2013-09-10 22:36:20 +08:00
|
|
|
return 0;
|
2013-09-13 00:39:21 +08:00
|
|
|
|
|
|
|
/* embedded IPv4-in-IPv6 */
|
2014-06-08 10:56:52 +08:00
|
|
|
if (IPV6_IPV4_IN_V6(ip.ip6)) {
|
2013-09-13 00:39:21 +08:00
|
|
|
IP ip4;
|
|
|
|
ip4.family = AF_INET;
|
2013-09-15 21:31:27 +08:00
|
|
|
ip4.ip4.uint32 = ip.ip6.uint32[3];
|
2013-09-13 00:39:21 +08:00
|
|
|
return LAN_ip(ip4);
|
|
|
|
}
|
2013-11-14 06:50:59 +08:00
|
|
|
|
|
|
|
/* localhost in IPv6 (::1) */
|
2014-06-08 10:56:52 +08:00
|
|
|
if (ip.ip6.uint64[0] == 0 && ip.ip6.uint32[2] == 0 && ip.ip6.uint32[3] == htonl(1))
|
2013-11-14 06:50:59 +08:00
|
|
|
return 0;
|
2013-09-10 22:36:20 +08:00
|
|
|
}
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-07-26 22:24:56 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-06-14 04:55:15 +08:00
|
|
|
static int handle_LANdiscovery(void *object, IP_Port source, const uint8_t *packet, uint32_t length)
|
2013-07-26 22:24:56 +08:00
|
|
|
{
|
2013-08-21 00:08:55 +08:00
|
|
|
DHT *dht = object;
|
2013-08-21 07:37:05 +08:00
|
|
|
|
2013-07-27 11:07:25 +08:00
|
|
|
if (LAN_ip(source.ip) == -1)
|
2013-07-26 22:24:56 +08:00
|
|
|
return 1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-07-27 11:07:25 +08:00
|
|
|
if (length != crypto_box_PUBLICKEYBYTES + 1)
|
2013-07-26 22:24:56 +08:00
|
|
|
return 1;
|
2013-08-17 01:11:09 +08:00
|
|
|
|
2013-08-21 00:08:55 +08:00
|
|
|
DHT_bootstrap(dht, source, packet + 1);
|
2013-07-26 22:24:56 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-18 02:35:40 +08:00
|
|
|
int send_LANdiscovery(uint16_t port, DHT *dht)
|
2013-07-26 22:24:56 +08:00
|
|
|
{
|
|
|
|
uint8_t data[crypto_box_PUBLICKEYBYTES + 1];
|
2013-08-21 15:55:43 +08:00
|
|
|
data[0] = NET_PACKET_LAN_DISCOVERY;
|
2014-01-18 02:35:40 +08:00
|
|
|
id_copy(data + 1, dht->self_public_key);
|
2013-09-11 17:44:05 +08:00
|
|
|
|
2014-01-18 02:35:40 +08:00
|
|
|
send_broadcasts(dht->net, port, data, 1 + crypto_box_PUBLICKEYBYTES);
|
2014-07-21 08:50:53 +08:00
|
|
|
|
2013-09-11 17:44:05 +08:00
|
|
|
int res = -1;
|
2013-09-10 22:36:20 +08:00
|
|
|
IP_Port ip_port;
|
|
|
|
ip_port.port = port;
|
2013-09-11 17:44:05 +08:00
|
|
|
|
2013-09-12 02:50:15 +08:00
|
|
|
/* IPv6 multicast */
|
2014-01-18 02:35:40 +08:00
|
|
|
if (dht->net->family == AF_INET6) {
|
2013-09-12 02:50:15 +08:00
|
|
|
ip_port.ip = broadcast_ip(AF_INET6, AF_INET6);
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-09-11 17:44:05 +08:00
|
|
|
if (ip_isset(&ip_port.ip))
|
2014-01-18 02:35:40 +08:00
|
|
|
if (sendpacket(dht->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES) > 0)
|
2013-09-11 17:44:05 +08:00
|
|
|
res = 1;
|
|
|
|
}
|
|
|
|
|
2013-09-12 02:50:15 +08:00
|
|
|
/* IPv4 broadcast (has to be IPv4-in-IPv6 mapping if socket is AF_INET6 */
|
2014-01-18 02:35:40 +08:00
|
|
|
ip_port.ip = broadcast_ip(dht->net->family, AF_INET);
|
2013-09-15 00:42:17 +08:00
|
|
|
|
2013-09-11 17:44:05 +08:00
|
|
|
if (ip_isset(&ip_port.ip))
|
2014-01-18 02:35:40 +08:00
|
|
|
if (sendpacket(dht->net, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES))
|
2013-09-11 17:44:05 +08:00
|
|
|
res = 1;
|
|
|
|
|
|
|
|
return res;
|
2013-07-26 22:24:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-08-21 00:08:55 +08:00
|
|
|
void LANdiscovery_init(DHT *dht)
|
2013-07-26 22:24:56 +08:00
|
|
|
{
|
2014-01-18 02:35:40 +08:00
|
|
|
networking_registerhandler(dht->net, NET_PACKET_LAN_DISCOVERY, &handle_LANdiscovery, dht);
|
2013-07-26 22:24:56 +08:00
|
|
|
}
|