summaryrefslogtreecommitdiff
path: root/toxcore/LAN_discovery.c
diff options
context:
space:
mode:
authorirungentoo <irungentoo@gmail.com>2014-07-20 20:50:53 -0400
committerirungentoo <irungentoo@gmail.com>2014-07-20 20:50:53 -0400
commitb63e4ad88fb93820fb740372f36d74c65b8b7b81 (patch)
treee476ebf2cbb64cda439691dd396d000c7081bd1b /toxcore/LAN_discovery.c
parent97ad1e62b676682bb4f1950023460e326b216841 (diff)
LAN discovery should now work on windows machines with multiple
ethernet devices. Added some code to get real adapter broadcast addresses on windows.
Diffstat (limited to 'toxcore/LAN_discovery.c')
-rw-r--r--toxcore/LAN_discovery.c75
1 files changed, 71 insertions, 4 deletions
diff --git a/toxcore/LAN_discovery.c b/toxcore/LAN_discovery.c
index e1b8534c..420c490d 100644
--- a/toxcore/LAN_discovery.c
+++ b/toxcore/LAN_discovery.c
@@ -30,11 +30,72 @@
30 30
31#define MAX_INTERFACES 16 31#define MAX_INTERFACES 16
32 32
33#ifdef __linux
34 33
35static int broadcast_count = -1; 34static int broadcast_count = -1;
36static IP_Port broadcast_ip_port[MAX_INTERFACES]; 35static IP_Port broadcast_ip_port[MAX_INTERFACES];
37 36
37#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32)
38
39#include <iphlpapi.h>
40
41static void fetch_broadcast_info(uint16_t port)
42{
43 broadcast_count = 0;
44
45 IP_ADAPTER_INFO *pAdapterInfo = malloc(sizeof(pAdapterInfo));
46 unsigned long ulOutBufLen = sizeof(pAdapterInfo);
47
48 if (pAdapterInfo == NULL) {
49 printf("Error allocating memory for pAdapterInfo\n");
50 return;
51 }
52
53 if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) {
54 free(pAdapterInfo);
55 pAdapterInfo = malloc(ulOutBufLen);
56
57 if (pAdapterInfo == NULL) {
58 printf("Error allocating memory needed to call GetAdaptersinfo\n");
59 return;
60 }
61 }
62
63 int ret;
64
65 if ((ret = GetAdaptersInfo(pAdapterInfo, &ulOutBufLen)) == NO_ERROR) {
66 IP_ADAPTER_INFO *pAdapter = pAdapterInfo;
67
68 while (pAdapter) {
69 IP gateway = {0}, subnet_mask = {0};
70
71 if (addr_parse_ip(pAdapter->IpAddressList.IpMask.String, &subnet_mask)
72 && addr_parse_ip(pAdapter->GatewayList.IpAddress.String, &gateway)) {
73 if (gateway.family == AF_INET && subnet_mask.family == AF_INET) {
74 IP_Port *ip_port = &broadcast_ip_port[broadcast_count];
75 ip_port->ip.family = AF_INET;
76 uint32_t gateway_ip = ntohl(gateway.ip4.uint32), subnet_ip = ntohl(subnet_mask.ip4.uint32);
77 uint32_t broadcast_ip = gateway_ip + ~subnet_ip - 1;
78 ip_port->ip.ip4.uint32 = htonl(broadcast_ip);
79 ip_port->port = port;
80 broadcast_count++;
81 printf("broadcast ip: %s\n", ip_ntoa(&ip_port->ip));
82
83 if (broadcast_count >= MAX_INTERFACES) {
84 return;
85 }
86 }
87 }
88
89 pAdapter = pAdapter->Next;
90 }
91 } else {
92 printf("Fetching adapter info failed %i\n", ret);
93 }
94
95}
96
97#elif defined(__linux__)
98
38static void fetch_broadcast_info(uint16_t port) 99static void fetch_broadcast_info(uint16_t port)
39{ 100{
40 /* Not sure how many platforms this will run on, 101 /* Not sure how many platforms this will run on,
@@ -93,6 +154,14 @@ static void fetch_broadcast_info(uint16_t port)
93 close(sock); 154 close(sock);
94} 155}
95 156
157#else //TODO: Other platforms?
158
159static void fetch_broadcast_info(uint16_t port)
160{
161 broadcast_count = 0;
162}
163
164#endif
96/* Send packet to all IPv4 broadcast addresses 165/* Send packet to all IPv4 broadcast addresses
97 * 166 *
98 * return 1 if sent to at least one broadcast target. 167 * return 1 if sent to at least one broadcast target.
@@ -115,7 +184,6 @@ static uint32_t send_broadcasts(Networking_Core *net, uint16_t port, const uint8
115 184
116 return 1; 185 return 1;
117} 186}
118#endif /* __linux */
119 187
120/* Return the broadcast ip. */ 188/* Return the broadcast ip. */
121static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast) 189static IP broadcast_ip(sa_family_t family_socket, sa_family_t family_broadcast)
@@ -228,9 +296,8 @@ int send_LANdiscovery(uint16_t port, DHT *dht)
228 data[0] = NET_PACKET_LAN_DISCOVERY; 296 data[0] = NET_PACKET_LAN_DISCOVERY;
229 id_copy(data + 1, dht->self_public_key); 297 id_copy(data + 1, dht->self_public_key);
230 298
231#ifdef __linux
232 send_broadcasts(dht->net, port, data, 1 + crypto_box_PUBLICKEYBYTES); 299 send_broadcasts(dht->net, port, data, 1 + crypto_box_PUBLICKEYBYTES);
233#endif 300
234 int res = -1; 301 int res = -1;
235 IP_Port ip_port; 302 IP_Port ip_port;
236 ip_port.port = port; 303 ip_port.port = port;