diff options
| author | Anton <anton@picapica> | 2017-12-29 19:37:48 +0400 |
|---|---|---|
| committer | Anton <anton@picapica> | 2017-12-29 19:37:48 +0400 |
| commit | c99e6b3a859f109d7cc92822ba0daed0b190a8ec (patch) | |
| tree | 36230fd946ccf76781aac6925d3793888e8c6170 | |
| parent | caae4bb3a47fddf97b6b28e5e6eea3420c8b255c (diff) | |
| download | pica-pica-c99e6b3a859f109d7cc92822ba0daed0b190a8ec.tar.gz pica-pica-c99e6b3a859f109d7cc92822ba0daed0b190a8ec.zip | |
implemented PICA_guess_listening_addr_ipv4() for *nix using getifaddrs()
| -rw-r--r-- | PICA_netconf.c | 44 | ||||
| -rw-r--r-- | PICA_netconf.h | 6 |
2 files changed, 50 insertions, 0 deletions
diff --git a/PICA_netconf.c b/PICA_netconf.c index da48c52..8bf2b4e 100644 --- a/PICA_netconf.c +++ b/PICA_netconf.c @@ -8,6 +8,12 @@ #include <stdio.h> #endif +#ifndef WIN32 +#include <sys/types.h> +#include <ifaddrs.h> +#include <net/if.h> +#endif + #define PREFIX4 htonl(0xf0000000U) #define PREFIX8 htonl(0xff000000U) #define PREFIX12 htonl(0xfff00000U) @@ -34,6 +40,44 @@ int PICA_is_reserved_addr_ipv4(in_addr_t addr) return 0; } +in_addr_t PICA_guess_listening_addr_ipv4() +{ + in_addr_t retaddr = INADDR_ANY; + int found_global = 0; +#ifndef WIN32 + struct ifaddrs *ifaddr, *ifa; + + if (getifaddrs(&ifaddr) == -1) + return INADDR_ANY; + + for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) + { + if (ifa->ifa_addr == NULL) + continue; + + if (ifa->ifa_addr->sa_family != AF_INET) + continue; + + if (ifa->ifa_flags & IFF_LOOPBACK) + continue; + + if (!((ifa->ifa_flags & IFF_RUNNING) && (ifa->ifa_flags & IFF_UP))) + continue; + + if (!found_global) + { + retaddr = ((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr; + + if (!PICA_is_reserved_addr_ipv4(retaddr)) + found_global = 1; + } + } +#else + +#endif + return retaddr; +} + #ifdef HAVE_LIBMINIUPNPC int PICA_upnp_autoconfigure_ipv4(int public_port, int local_port, char *public_ip) { diff --git a/PICA_netconf.h b/PICA_netconf.h index 22c043a..13b4044 100644 --- a/PICA_netconf.h +++ b/PICA_netconf.h @@ -7,8 +7,12 @@ #else +#define WIN32_LEAN_AND_MEAN 1 #define _WIN32_WINNT 0x501 #include <winsock2.h> +#include <ws2ipdef.h> +#include <wincrypt.h> +#include <iphlpapi.h> #include <ws2tcpip.h> typedef u_long in_addr_t; @@ -17,6 +21,8 @@ typedef u_short in_port_t; int PICA_is_reserved_addr_ipv4(in_addr_t); +in_addr_t PICA_guess_listening_addr_ipv4(); + #ifdef HAVE_LIBMINIUPNPC int PICA_upnp_autoconfigure_ipv4(int public_port, int local_port, char *public_ip); #endif |
