summaryrefslogtreecommitdiff
path: root/toxcore/LAN_discovery.c
blob: 8998e0a97300af51f45e1486b0129bb49983c48f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*  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/>.
 *
 */

#include "LAN_discovery.h"

#define MAX_INTERFACES 16

#ifdef __linux
/* Get the first working broadcast address that's not from "lo".
 *
 *  return higher than 0 on success.
 *  return 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. */
    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        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);

    if (ioctl(sock, SIOCGIFCONF, &ifconf) < 0) {
        perror("get_broadcast: ioctl() error");
        return 0;
    }

    for (i = 0; i < count; i++) {
        /* Skip the loopback interface, as it's useless. */
        if (strcmp(i_faces[i].ifr_name, "lo") != 0) {
            if (ioctl(sock, SIOCGIFBRDADDR, &i_faces[i]) < 0) {
                perror("[!] get_broadcast: ioctl error");
                return 0;
            }

            /* Just to clarify where we're getting the values from. */
            sock_holder = (struct sockaddr_in *)&i_faces[i].ifr_broadaddr;
            break;
        }
    }

    close(sock);

    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)
{
    IP ip;
#ifdef __linux
    ip.uint32 = get_broadcast();

    if (ip.uint32 == 0)
        ip.uint32 = ~0; /* Error occured, but try anyway? */

#else
    ip.uint32 = ~0;
#endif
    return ip;
}

/*  return 0 if ip is a LAN ip.
 *  return -1 if it is not.
 */
static int LAN_ip(IP ip)
{
    if (ip.uint8[0] == 127) /* Loopback. */
        return 0;

    if (ip.uint8[0] == 10) /* 10.0.0.0 to 10.255.255.255 range. */
        return 0;

    if (ip.uint8[0] == 172 && ip.uint8[1] >= 16 && ip.uint8[1] <= 31) /* 172.16.0.0 to 172.31.255.255 range. */
        return 0;

    if (ip.uint8[0] == 192 && ip.uint8[1] == 168) /* 192.168.0.0 to 192.168.255.255 range. */
        return 0;

    if (ip.uint8[0] == 169 && ip.uint8[1] == 254 && ip.uint8[2] != 0
            && ip.uint8[2] != 255)/* 169.254.1.0 to 169.254.254.255 range. */
        return 0;

    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);
    IP_Port ip_port = {{broadcast_ip(), port, 0}};
    return sendpacket(c->lossless_udp->net->sock, ip_port, data, 1 + crypto_box_PUBLICKEYBYTES);
}


void LANdiscovery_init(DHT *dht)
{
    networking_registerhandler(dht->c->lossless_udp->net, NET_PACKET_LAN_DISCOVERY, &handle_LANdiscovery, dht);
}