Attempt to get the siocgifconf_nt to work using the GetAdaptersAddresses, unsuccessful so far

This commit is contained in:
Fabrizio Bertocci 2021-06-14 08:04:31 -04:00
parent 3929d7c726
commit b9b1e4f3b5
5 changed files with 117 additions and 10 deletions

View file

@ -43,6 +43,8 @@ LIBC_CALLS_A_DIRECTDEPS = \
LIBC_NT_ADVAPI32 \ LIBC_NT_ADVAPI32 \
LIBC_NT_KERNEL32 \ LIBC_NT_KERNEL32 \
LIBC_NT_NTDLL \ LIBC_NT_NTDLL \
LIBC_NT_WS2_32 \
LIBC_NT_IPHLPAPI \
LIBC_STR \ LIBC_STR \
LIBC_STUBS \ LIBC_STUBS \
LIBC_SYSV_CALLS \ LIBC_SYSV_CALLS \

View file

@ -16,20 +16,102 @@
TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
PERFORMANCE OF THIS SOFTWARE. PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "libc/bits/weaken.h"
#include "libc/calls/calls.h" #include "libc/calls/calls.h"
#include "libc/calls/internal.h" #include "libc/calls/internal.h"
#include "libc/sysv/consts/o.h" #include "libc/sysv/consts/o.h"
#include "libc/sysv/consts/af.h"
#include "libc/sock/sock.h" #include "libc/sock/sock.h"
#include "libc/sock/internal.h" #include "libc/sock/internal.h"
#include "libc/sysv/errfuns.h" #include "libc/sysv/errfuns.h"
#include "libc/nt/winsock.h" #include "libc/nt/winsock.h"
#include "libc/nt/errors.h"
// TODO: Remove me // TODO: Remove me
//#include "libc/stdio/stdio.h" #include "libc/bits/weaken.h"
#include "libc/stdio/stdio.h"
#define PRINTF weaken(printf)
#define MAX_INTERFACES 32 #define MAX_INTERFACES 32
#if 1
#define GAA_FLAG_SKIP_UNICAST 0x0001
#define GAA_FLAG_SKIP_ANYCAST 0x0002
#define GAA_FLAG_SKIP_MULTICAST 0x0004
#define GAA_FLAG_SKIP_DNS_SERVER 0x0008
#define GAA_FLAG_INCLUDE_PREFIX 0x0010
#define GAA_FLAG_SKIP_FRIENDLY_NAME 0x0020
#define GAA_FLAG_INCLUDE_WINS_INFO 0x0040
#define GAA_FLAG_INCLUDE_GATEWAYS 0x0080
#define GAA_FLAG_INCLUDE_ALL_INTERFACES 0x0100
#define GAA_FLAG_INCLUDE_ALL_COMPARTMENTS 0x0200
#define GAA_FLAG_INCLUDE_TUNNEL_BINDINGORDER 0x0400
#define GAA_FLAG_SKIP_DNS_INFO 0x0800
typedef struct _IP_ADAPTER_ADDRESSES_XP {
union {
uint64_t Alignment;
struct {
uint32_t Length;
uint32_t IfIndex;
};
};
struct _IP_ADAPTER_ADDRESSES_XP *Next;
char * AdapterName;
void * FirstUnicastAddress;
void * FirstAnycastAddress;
void * FirstMulticastAddress;
void * FirstDnsServerAddress;
wchar_t *DnsSuffix;
wchar_t *Description;
wchar_t *FriendlyName;
/* Filler */
} IP_ADAPTER_ADDRESSES_XP, *PIP_ADAPTER_ADDRESSES_XP;
extern unsigned long GetAdaptersAddresses(
uint32_t Family,
uint32_t Flags,
void * Reserved,
char *AdapterAddresses,
uint32_t *SizePointer);
textwindows int ioctl_siocgifconf_nt(int fd, struct ifconf *ifc) {
uint32_t rv, size;
char * adapter_addresses;
struct _IP_ADAPTER_ADDRESSES_XP *aa;
PRINTF("FABDEBUG> ioctl(SIOCGIFCONF) for Windows... printf=%p, GetAdaptersAddresses=%p\n", weaken(printf), weaken(GetAdaptersAddresses));
if (!weaken(GetAdaptersAddresses)) {
PRINTF("FABDEBUG> NULL GetAdaptersAddresses\n");
return enomem();
}
rv = weaken(GetAdaptersAddresses)(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, NULL, &size);
if (rv != kNtErrorBufferOverflow) {
PRINTF("FABDEBUG> GetAdaptersAddresses failed %d\n", WSAGetLastError());
return ebadf();
}
PRINTF("FABDEBUG> size=%lu\n", size);
adapter_addresses = (char *)weaken(malloc)(size);
if (!adapter_addresses) {
return enomem();
}
rv = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, adapter_addresses, &size);
if (rv != kNtErrorSuccess) {
PRINTF("FABDEBUG> GetAdaptersAddresses failed %d\n", WSAGetLastError());
weaken(free)(adapter_addresses);
return efault();
}
for (aa = (struct _IP_ADAPTER_ADDRESSES_XP *)adapter_addresses; aa != NULL; aa = aa->Next) {
PRINTF("FABDEBUG> Adapter name = %s", aa->AdapterName);
}
weaken(free)(adapter_addresses);
return 0;
}
#else
/* Reference: /* Reference:
* - Description of ioctls: https://docs.microsoft.com/en-us/windows/win32/winsock/winsock-ioctls * - Description of ioctls: https://docs.microsoft.com/en-us/windows/win32/winsock/winsock-ioctls
* - Structure INTERFACE_INFO: https://docs.microsoft.com/en-us/windows/win32/api/ws2ipdef/ns-ws2ipdef-interface_info * - Structure INTERFACE_INFO: https://docs.microsoft.com/en-us/windows/win32/api/ws2ipdef/ns-ws2ipdef-interface_info
@ -47,18 +129,19 @@ textwindows int ioctl_siocgifconf_nt(int fd, struct ifconf *ifc) {
return ebadf(); return ebadf();
} }
ret = weaken(WSAIoctl)(g_fds.p[fd].handle, kNtSioGetInterfaceList, NULL, 0, &iflist, sizeof(iflist), &dwBytes, NULL, NULL); ret = WSAIoctl(g_fds.p[fd].handle, kNtSioGetInterfaceList, NULL, 0, &iflist, sizeof(iflist), &dwBytes, NULL, NULL);
if (ret == -1) { if (ret == -1) {
PRINTF("FABDEBUG> WSAIoctl failed %d\n", WSAGetLastError());
return weaken(__winsockerr)(); return weaken(__winsockerr)();
} }
PRINTF("FABDEBUG> WSAIoctl success\n");
count = dwBytes / sizeof(struct NtInterfaceInfo); count = dwBytes / sizeof(struct NtInterfaceInfo);
/* PRINTF("CI> SIO_GET_INTERFACE_LIST success:\n");
printf("CI> SIO_GET_INTERFACE_LIST success:\n");
for (i = 0; i < count; ++i) { for (i = 0; i < count; ++i) {
printf("CI>\t #i: %08x\n", i, iflist[i].iiAddress.sin_addr.s_addr); PRINTF("CI>\t #i: addr=%08x, flags=%08x, bcast=%08x\n", i, iflist[i].iiAddress.sin_addr.s_addr, iflist[i].iiFlags, iflist[i].iiBroadcastAddress.sin_addr.s_addr);
} }
*/
return ret; return ret;
} }
#endif

View file

@ -35,7 +35,7 @@
int ioctl_default(int, uint64_t, void *) hidden; int ioctl_default(int, uint64_t, void *) hidden;
int ioctl_siocgifconf_nt(int, struct ifconf *) hidden; int ioctl_siocgifconf_nt(int, struct ifconf *) hidden;
int ioctl_siocgifaddr_nt(int, struct ifconf *) hidden; //int ioctl_siocgifaddr_nt(int, struct ifconf *) hidden;
static int ioctl_siocgifconf_sysv(int fd, struct ifconf *ifc) { static int ioctl_siocgifconf_sysv(int fd, struct ifconf *ifc) {
if (IsBsd()) { if (IsBsd()) {
@ -123,8 +123,7 @@ int ioctl_siocgifconf(int fd, void *ifc) {
if (!IsWindows()) { if (!IsWindows()) {
return ioctl_siocgifconf_sysv(fd, (struct ifconf *)ifc); return ioctl_siocgifconf_sysv(fd, (struct ifconf *)ifc);
} else { } else {
return enotsup(); return ioctl_siocgifconf_nt(fd, ifc);
//return ioctl_siocgifconf_nt(fd, ifc);
} }
} }

View file

@ -7797,3 +7797,5 @@ imp 'ReleaseSRWLockExclusive' ReleaseSRWLockExclusive kernel32 0 1 # Kern
imp 'ReleaseSRWLockShared' ReleaseSRWLockShared kernel32 0 1 # KernelBase imp 'ReleaseSRWLockShared' ReleaseSRWLockShared kernel32 0 1 # KernelBase
imp 'TryAcquireSRWLockExclusive' TryAcquireSRWLockExclusive kernel32 0 1 # KernelBase imp 'TryAcquireSRWLockExclusive' TryAcquireSRWLockExclusive kernel32 0 1 # KernelBase
imp 'TryAcquireSRWLockShared' TryAcquireSRWLockShared kernel32 0 1 # KernelBase imp 'TryAcquireSRWLockShared' TryAcquireSRWLockShared kernel32 0 1 # KernelBase
imp 'GetAdaptersAddresses' GetAdaptersAddresses iphlpapi 67 5

View file

@ -252,6 +252,27 @@ $(LIBC_NT_WS2_32_A).pkg: \
#─────────────────────────────────────────────────────────────────────────────── #───────────────────────────────────────────────────────────────────────────────
LIBC_NT_ARTIFACTS += LIBC_NT_IPHLPAPI_A
LIBC_NT_IPHLPAPI = $(LIBC_NT_IPHLPAPI_A_DEPS) $(LIBC_NT_IPHLPAPI_A)
LIBC_NT_IPHLPAPI_A = o/$(MODE)/libc/nt/iphlpapi.a
LIBC_NT_IPHLPAPI_A_SRCS := $(wildcard libc/nt/iphlpapi/*.s)
LIBC_NT_IPHLPAPI_A_OBJS = $(LIBC_NT_IPHLPAPI_A_SRCS:%.s=o/$(MODE)/%.o)
LIBC_NT_IPHLPAPI_A_CHECKS = $(LIBC_IPHLPAPI_32_A).pkg
LIBC_NT_IPHLPAPI_A_DIRECTDEPS = LIBC_NT_KERNEL32
LIBC_NT_IPHLPAPI_A_DEPS := \
$(call uniq,$(foreach x,$(LIBC_NT_IPHLPAPI_A_DIRECTDEPS),$($(x))))
$(LIBC_NT_IPHLPAPI_A): \
libc/nt/iphlpapi/ \
$(LIBC_NT_IPHLPAPI_A).pkg \
$(LIBC_NT_IPHLPAPI_A_OBJS)
$(LIBC_NT_IPHLPAPI_A).pkg: \
$(LIBC_NT_IPHLPAPI_A_OBJS) \
$(foreach x,$(LIBC_NT_IPHLPAPI_A_DIRECTDEPS),$($(x)_A).pkg)
#───────────────────────────────────────────────────────────────────────────────
LIBC_NT_ARTIFACTS += LIBC_NT_MSWSOCK_A LIBC_NT_ARTIFACTS += LIBC_NT_MSWSOCK_A
LIBC_NT_MSWSOCK = $(LIBC_NT_MSWSOCK_A_DEPS) $(LIBC_NT_MSWSOCK_A) LIBC_NT_MSWSOCK = $(LIBC_NT_MSWSOCK_A_DEPS) $(LIBC_NT_MSWSOCK_A)
LIBC_NT_MSWSOCK_A = o/$(MODE)/libc/nt/MsWSock.a LIBC_NT_MSWSOCK_A = o/$(MODE)/libc/nt/MsWSock.a