LAN discovery should now work on windows machines with multiple

ethernet devices.

Added some code to get real adapter broadcast addresses on windows.
This commit is contained in:
irungentoo 2014-07-20 20:50:53 -04:00
parent 97ad1e62b6
commit b63e4ad88f
No known key found for this signature in database
GPG Key ID: 10349DC9BED89E98
2 changed files with 72 additions and 5 deletions

View File

@ -662,7 +662,7 @@ fi
if test "x$WIN32" = "xyes"; then
AC_CHECK_LIB(ws2_32, main,
[
WINSOCK2_LIBS="-lws2_32"
WINSOCK2_LIBS="-liphlpapi -lws2_32"
AC_SUBST(WINSOCK2_LIBS)
],
[

View File

@ -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 <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) {
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;