Replace NULL address family filter by supported address families

Signed-off-by: Jeroen Koekkoek <jeroen@koekkoek.nl>
This commit is contained in:
Jeroen Koekkoek 2018-10-24 16:31:43 +02:00
parent 25198e565b
commit 961706bb48
4 changed files with 29 additions and 5 deletions

View file

@ -141,7 +141,7 @@ extern "C" {
OSAPI_EXPORT _Success_(return == 0) int OSAPI_EXPORT _Success_(return == 0) int
os_getifaddrs( os_getifaddrs(
_Inout_ os_ifaddrs_t **ifap, _Inout_ os_ifaddrs_t **ifap,
_In_opt_ const int *const afs); _In_opt_ const int *afs);
/** /**
* @brief Free os_ifaddrs_t structure list allocated by os_getifaddrs() * @brief Free os_ifaddrs_t structure list allocated by os_getifaddrs()

View file

@ -45,6 +45,18 @@ const os_in6_addr os_in6addr_loopback = { { { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }
#define OS_INET_PTON inet_pton #define OS_INET_PTON inet_pton
#endif #endif
const int afs[] = {
#ifdef __linux
AF_PACKET,
#endif /* __linux */
#if OS_SOCKET_HAS_IPV6
AF_INET6,
#endif /* OS_SOCKET_HAS_IPV6 */
AF_INET
};
const int *const os_supp_afs = afs;
size_t size_t
os_sockaddr_get_size(const os_sockaddr *const sa) os_sockaddr_get_size(const os_sockaddr *const sa)
{ {

View file

@ -14,6 +14,8 @@
#include "os/os.h" #include "os/os.h"
extern const int *const os_supp_afs;
static int static int
copyaddr(os_ifaddrs_t **ifap, const struct ifaddrs *sys_ifa) copyaddr(os_ifaddrs_t **ifap, const struct ifaddrs *sys_ifa)
{ {
@ -55,7 +57,7 @@ copyaddr(os_ifaddrs_t **ifap, const struct ifaddrs *sys_ifa)
_Success_(return == 0) int _Success_(return == 0) int
os_getifaddrs( os_getifaddrs(
_Inout_ os_ifaddrs_t **ifap, _Inout_ os_ifaddrs_t **ifap,
_In_opt_ const int *const afs) _In_opt_ const int *afs)
{ {
int err = 0; int err = 0;
int use; int use;
@ -65,6 +67,10 @@ os_getifaddrs(
assert(ifap != NULL); assert(ifap != NULL);
if (afs == NULL) {
afs = os_supp_afs;
}
if (getifaddrs(&sys_ifa_root) == -1) { if (getifaddrs(&sys_ifa_root) == -1) {
err = errno; err = errno;
} else { } else {
@ -76,7 +82,7 @@ os_getifaddrs(
{ {
sa = sys_ifa->ifa_addr; sa = sys_ifa->ifa_addr;
if (sa != NULL) { if (sa != NULL) {
use = (afs == NULL); use = 0;
for (int i = 0; !use && afs[i] != 0; i++) { for (int i = 0; !use && afs[i] != 0; i++) {
use = (sa->sa_family == afs[i]); use = (sa->sa_family == afs[i]);
} }

View file

@ -14,6 +14,8 @@
#include "os/os.h" #include "os/os.h"
extern const int *const os_supp_afs;
static int static int
getifaces(PIP_ADAPTER_ADDRESSES *ptr) getifaces(PIP_ADAPTER_ADDRESSES *ptr)
{ {
@ -207,7 +209,7 @@ copyaddr(
_Success_(return == 0) int _Success_(return == 0) int
os_getifaddrs( os_getifaddrs(
_Inout_ os_ifaddrs_t **ifap, _Inout_ os_ifaddrs_t **ifap,
_In_opt_ const int *const afs) _In_opt_ const int *afs)
{ {
int err = 0; int err = 0;
int use; int use;
@ -219,6 +221,10 @@ os_getifaddrs(
assert(ifap != NULL); assert(ifap != NULL);
if (afs == NULL) {
afs = os_supp_afs;
}
ifa = ifa_root = ifa_next = NULL; ifa = ifa_root = ifa_next = NULL;
if ((err = getifaces(&ifaces)) == 0 && if ((err = getifaces(&ifaces)) == 0 &&
@ -230,7 +236,7 @@ os_getifaddrs(
addr = addr->Next) addr = addr->Next)
{ {
sa = (struct sockaddr *)addr->Address.lpSockaddr; sa = (struct sockaddr *)addr->Address.lpSockaddr;
use = (afs == NULL); use = 0;
for (int i = 0; !use && afs[i] != 0; i++) { for (int i = 0; !use && afs[i] != 0; i++) {
use = (afs[i] == sa->sa_family); use = (afs[i] == sa->sa_family);
} }