toxcore/core/LAN_discovery.c

151 lines
3.8 KiB
C
Raw Normal View History

/* 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 <http://www.gnu.org/licenses/>.
*
2013-07-26 22:24:56 +08:00
*/
2013-07-26 22:31:49 +08:00
#include "LAN_discovery.h"
2013-07-26 22:24:56 +08:00
#define MAX_INTERFACES 16
2013-07-26 22:24:56 +08:00
#ifdef __linux
/* get the first working broadcast address that's not from "lo"
* returns higher than 0 on success
2013-08-02 04:54:04 +08:00
* returns 0 on error */
static uint32_t get_broadcast(void)
{
/* 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 */
2013-08-17 01:11:09 +08:00
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
2013-08-02 04:54:04 +08:00
perror("[!] get_broadcast: socket() error");
return 0;
}
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);
2013-08-17 01:11:09 +08:00
if (ioctl(sock, SIOCGIFCONF, &ifconf) < 0) {
perror("get_broadcast: ioctl() error");
2013-08-02 04:54:04 +08:00
return 0;
}
2013-08-17 01:11:09 +08:00
for (i = 0; i < count; i++) {
/* skip the loopback interface, as it's useless */
2013-08-17 01:11:09 +08:00
if (strcmp(i_faces[i].ifr_name, "lo") != 0) {
if (ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) {
perror("[!] get_broadcast: ioctl error");
2013-08-02 04:54:04 +08:00
return 0;
}
/* just to clarify where we're getting the values from */
sock_holder = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr;
break;
}
}
2013-08-17 01:11:09 +08:00
close(sock);
2013-08-17 01:11:09 +08:00
if (sock_holder == NULL) {
perror("[!] no broadcast device found");
return 0;
}
return sock_holder->sin_addr.s_addr;
}
#endif
/* Return the broadcast ip */
static IP broadcast_ip(void)
2013-07-26 22:24:56 +08:00
{
IP ip;
2013-08-17 01:11:09 +08:00
#ifdef __linux
ip.i = get_broadcast();
2013-08-17 01:11:09 +08:00
if (ip.i == 0)
2013-08-02 04:54:04 +08:00
/* error errored, but try anyway? */
ip.i = ~0;
2013-08-17 01:11:09 +08:00
#else
2013-07-26 22:24:56 +08:00
ip.i = ~0;
2013-08-17 01:11:09 +08:00
#endif
2013-07-26 22:24:56 +08:00
return ip;
}
/*return 0 if ip is a LAN ip
return -1 if it is not */
static int LAN_ip(IP ip)
2013-07-26 22:24:56 +08:00
{
2013-07-27 11:07:25 +08:00
if (ip.c[0] == 127)/* Loopback */
2013-07-26 22:24:56 +08:00
return 0;
2013-08-17 01:11:09 +08:00
2013-07-27 11:07:25 +08:00
if (ip.c[0] == 10)/* 10.0.0.0 to 10.255.255.255 range */
2013-07-26 22:24:56 +08:00
return 0;
2013-08-17 01:11:09 +08:00
2013-07-27 11:07:25 +08:00
if (ip.c[0] == 172 && ip.c[1] >= 16 && ip.c[1] <= 31)/* 172.16.0.0 to 172.31.255.255 range */
2013-07-26 22:24:56 +08:00
return 0;
2013-08-17 01:11:09 +08:00
2013-07-27 11:07:25 +08:00
if (ip.c[0] == 192 && ip.c[1] == 168) /* 192.168.0.0 to 192.168.255.255 range */
2013-07-26 22:24:56 +08:00
return 0;
2013-08-17 01:11:09 +08:00
2013-07-27 11:07:25 +08:00
if (ip.c[0] == 169 && ip.c[1] == 254 && ip.c[2] != 0 && ip.c[2] != 255)/* 169.254.1.0 to 169.254.254.255 range */
2013-07-26 22:24:56 +08:00
return 0;
2013-08-17 01:11:09 +08:00
2013-07-26 22:24:56 +08:00
return -1;
}
2013-08-10 07:30:18 +08:00
static int handle_LANdiscovery(IP_Port source, uint8_t *packet, uint32_t length)
2013-07-26 22:24:56 +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-07-26 22:24:56 +08:00
DHT_bootstrap(source, packet + 1);
return 0;
}
int send_LANdiscovery(uint16_t port)
{
uint8_t data[crypto_box_PUBLICKEYBYTES + 1];
2013-08-10 07:30:18 +08:00
data[0] = 33;
2013-07-26 22:24:56 +08:00
memcpy(data + 1, self_public_key, crypto_box_PUBLICKEYBYTES);
IP_Port ip_port = {broadcast_ip(), port};
return sendpacket(ip_port, data, 1 + crypto_box_PUBLICKEYBYTES);
}
2013-08-10 07:30:18 +08:00
void LANdiscovery_init(void)
2013-07-26 22:24:56 +08:00
{
2013-08-10 18:18:25 +08:00
networking_registerhandler(33, &handle_LANdiscovery);
2013-07-26 22:24:56 +08:00
}