From b63e4ad88fb93820fb740372f36d74c65b8b7b81 Mon Sep 17 00:00:00 2001 From: irungentoo Date: Sun, 20 Jul 2014 20:50:53 -0400 Subject: LAN discovery should now work on windows machines with multiple ethernet devices. Added some code to get real adapter broadcast addresses on windows. --- toxcore/LAN_discovery.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 71 insertions(+), 4 deletions(-) (limited to 'toxcore') 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 @@ #define MAX_INTERFACES 16 -#ifdef __linux static int broadcast_count = -1; static IP_Port broadcast_ip_port[MAX_INTERFACES]; +#if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) + +#include + +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) { + printf("Error allocating memory for pAdapterInfo\n"); + return; + } + + if (GetAdaptersInfo(pAdapterInfo, &ulOutBufLen) == ERROR_BUFFER_OVERFLOW) { + free(pAdapterInfo); + pAdapterInfo = malloc(ulOutBufLen); + + if (pAdapterInfo == NULL) { + printf("Error allocating memory needed to call GetAdaptersinfo\n"); + 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++; + printf("broadcast ip: %s\n", ip_ntoa(&ip_port->ip)); + + if (broadcast_count >= MAX_INTERFACES) { + return; + } + } + } + + pAdapter = pAdapter->Next; + } + } else { + printf("Fetching adapter info failed %i\n", ret); + } + +} + +#elif defined(__linux__) + static void fetch_broadcast_info(uint16_t port) { /* Not sure how many platforms this will run on, @@ -93,6 +154,14 @@ static void fetch_broadcast_info(uint16_t port) close(sock); } +#else //TODO: Other platforms? + +static void fetch_broadcast_info(uint16_t port) +{ + broadcast_count = 0; +} + +#endif /* Send packet to all IPv4 broadcast addresses * * 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 return 1; } -#endif /* __linux */ /* Return the broadcast ip. */ static 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) data[0] = NET_PACKET_LAN_DISCOVERY; id_copy(data + 1, dht->self_public_key); -#ifdef __linux send_broadcasts(dht->net, port, data, 1 + crypto_box_PUBLICKEYBYTES); -#endif + int res = -1; IP_Port ip_port; ip_port.port = port; -- cgit v1.2.3