From ac290489c5d2210aa47c912d99dfbdd8f7c5aa76 Mon Sep 17 00:00:00 2001 From: Shpoike Date: Fri, 7 Dec 2018 04:14:56 +0000 Subject: [PATCH] Fix linux compile issue. --- Quake/net_udp.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ Quake/net_udp.h | 3 +++ 2 files changed, 50 insertions(+) diff --git a/Quake/net_udp.c b/Quake/net_udp.c index 60477707..c8a485c4 100644 --- a/Quake/net_udp.c +++ b/Quake/net_udp.c @@ -841,3 +841,50 @@ int UDP6_GetAddrFromName (const char *name, struct qsockaddr *addr) //============================================================================= +#ifdef __linux__ //sadly there is no posix standard for querying all ipv4+ipv6 addresses. +#include +static struct ifaddrs *iflist; +double iftime; //requery sometimes. +static int UDP_GetAddresses(qhostaddr_t *addresses, int maxaddresses, int fam) +{ + struct ifaddrs *ifa; + int result = 0; + double time = Sys_DoubleTime(); + if (time - iftime > 1 && iflist) + { + freeifaddrs(iflist); + iflist = NULL; + } + if (!iflist) + { + iftime = time; + getifaddrs(&iflist); + } + + for (ifa = iflist; ifa && result < maxaddresses; ifa = ifa->ifa_next) + { + //can happen if the interface is not bound. + if (ifa->ifa_addr == NULL) + continue; + if (fam == ifa->ifa_addr->sa_family) + q_strlcpy(addresses[result++], UDP_AddrToString((struct qsockaddr*)ifa->ifa_addr, false), sizeof(addresses[0])); + } + return result; +} +#else +//for other systems, like macs, where we don't know how to query this stuff properly. +//FIXME: there is a posix standard for ipv4 at least. +static int UDP_GetAddresses(qhostaddr_t *addresses, int maxaddresses, int fam) +{ + return 0; +} +#endif +int UDP4_GetAddresses (qhostaddr_t *addresses, int maxaddresses) +{ + return UDP_GetAddresses(addresses, maxaddresses, AF_INET); +} +int UDP6_GetAddresses (qhostaddr_t *addresses, int maxaddresses) +{ + return UDP_GetAddresses(addresses, maxaddresses, AF_INET6); +} + diff --git a/Quake/net_udp.h b/Quake/net_udp.h index 013315c2..c5c58210 100644 --- a/Quake/net_udp.h +++ b/Quake/net_udp.h @@ -40,6 +40,8 @@ int UDP4_GetAddrFromName (const char *name, struct qsockaddr *addr); int UDP_AddrCompare (struct qsockaddr *addr1, struct qsockaddr *addr2); int UDP_GetSocketPort (struct qsockaddr *addr); int UDP_SetSocketPort (struct qsockaddr *addr, int port); +int UDP4_GetAddresses (qhostaddr_t *addresses, int maxaddresses); + sys_socket_t UDP6_Init (void); void UDP6_Shutdown (void); @@ -59,6 +61,7 @@ int UDP6_GetAddrFromName (const char *name, struct qsockaddr *addr); int UDP_AddrCompare (struct qsockaddr *addr1, struct qsockaddr *addr2); int UDP_GetSocketPort (struct qsockaddr *addr); int UDP_SetSocketPort (struct qsockaddr *addr, int port); +int UDP6_GetAddresses (qhostaddr_t *addresses, int maxaddresses); #endif /* __net_udp_h */