mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
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:
parent
97ad1e62b6
commit
b63e4ad88f
|
@ -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)
|
||||
],
|
||||
[
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue
Block a user