2018-07-30 04:38:14 +00:00
|
|
|
#if defined(__MINGW32__) && !defined(_WIN64)
|
|
|
|
/*
|
|
|
|
* All the user-mode scaffolding necessary to backport GetAdaptersAddresses(2))
|
|
|
|
* to the NT 5.x series. See further comments for any limitations.
|
2018-08-20 10:38:25 +00:00
|
|
|
* NOTE: this is dead code, i haven't had time to debug it yet due to illness.
|
|
|
|
* For now, downlevel platforms use GetAdaptersInfo(2) which is inet4 only.
|
|
|
|
* -despair86 20/08/18
|
2018-07-30 04:38:14 +00:00
|
|
|
*/
|
|
|
|
#include <assert.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2018-08-03 06:03:34 +00:00
|
|
|
// apparently mingw-w64 loses its shit over this
|
|
|
|
// but only for 32-bit builds, naturally
|
|
|
|
#ifdef WIN32_LEAN_AND_MEAN
|
|
|
|
#undef WIN32_LEAN_AND_MEAN
|
|
|
|
#endif
|
|
|
|
|
2018-08-11 17:54:28 +00:00
|
|
|
// these need to be in a specific order
|
2018-07-30 04:38:14 +00:00
|
|
|
#include <windows.h>
|
|
|
|
#include <winternl.h>
|
2018-08-22 17:06:12 +00:00
|
|
|
#include <tdi.h>
|
2018-07-30 04:38:14 +00:00
|
|
|
#include "win32_intrnl.h"
|
|
|
|
|
|
|
|
const PWCHAR TcpFileName = L"\\Device\\Tcp";
|
|
|
|
|
|
|
|
// from ntdll.dll
|
|
|
|
typedef void(FAR PASCAL *pRtlInitUString)(UNICODE_STRING *, const WCHAR *);
|
|
|
|
typedef NTSTATUS(FAR PASCAL *pNTOpenFile)(HANDLE *, ACCESS_MASK,
|
|
|
|
OBJECT_ATTRIBUTES *,
|
|
|
|
IO_STATUS_BLOCK *, ULONG, ULONG);
|
|
|
|
typedef NTSTATUS(FAR PASCAL *pNTClose)(HANDLE);
|
|
|
|
|
|
|
|
#define FSCTL_TCP_BASE FILE_DEVICE_NETWORK
|
|
|
|
|
|
|
|
#define _TCP_CTL_CODE(Function, Method, Access) \
|
2018-08-08 17:43:46 +00:00
|
|
|
CTL_CODE(FSCTL_TCP_BASE, Function, Method, Access)
|
2018-07-30 04:38:14 +00:00
|
|
|
|
|
|
|
#define IOCTL_TCP_QUERY_INFORMATION_EX \
|
2018-08-08 17:43:46 +00:00
|
|
|
_TCP_CTL_CODE(0, METHOD_NEITHER, FILE_ANY_ACCESS)
|
2018-07-30 04:38:14 +00:00
|
|
|
|
|
|
|
typedef struct _InterfaceIndexTable
|
|
|
|
{
|
|
|
|
DWORD numIndexes;
|
|
|
|
DWORD numAllocated;
|
|
|
|
DWORD indexes[1];
|
|
|
|
} InterfaceIndexTable;
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
tdiGetMibForIfEntity(HANDLE tcpFile, TDIEntityID *ent,
|
|
|
|
IFEntrySafelySized *entry)
|
|
|
|
{
|
2018-11-08 00:09:49 +00:00
|
|
|
TCP_REQUEST_QUERY_INFORMATION_EX req;
|
2018-07-30 04:38:14 +00:00
|
|
|
NTSTATUS status = 0;
|
|
|
|
DWORD returnSize;
|
|
|
|
|
2018-08-08 17:43:46 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "TdiGetMibForIfEntity(tcpFile %x,entityId %x)\n",
|
|
|
|
(int)tcpFile, (int)ent->tei_instance);
|
|
|
|
#endif
|
2018-07-30 04:38:14 +00:00
|
|
|
|
2018-11-08 00:09:49 +00:00
|
|
|
memset(&req, 0, sizeof(req));
|
2018-07-30 04:38:14 +00:00
|
|
|
req.ID.toi_class = INFO_CLASS_PROTOCOL;
|
|
|
|
req.ID.toi_type = INFO_TYPE_PROVIDER;
|
|
|
|
req.ID.toi_id = 1;
|
|
|
|
req.ID.toi_entity = *ent;
|
|
|
|
|
|
|
|
status =
|
|
|
|
DeviceIoControl(tcpFile, IOCTL_TCP_QUERY_INFORMATION_EX, &req,
|
|
|
|
sizeof(req), entry, sizeof(*entry), &returnSize, NULL);
|
|
|
|
|
|
|
|
if(!status)
|
|
|
|
{
|
|
|
|
perror("IOCTL Failed\n");
|
|
|
|
return 0xc0000001;
|
|
|
|
}
|
|
|
|
|
|
|
|
fprintf(stderr,
|
2018-08-08 17:43:46 +00:00
|
|
|
"TdiGetMibForIfEntity() => {\n"
|
|
|
|
" if_index ....................... %lx\n"
|
|
|
|
" if_type ........................ %lx\n"
|
|
|
|
" if_mtu ......................... %ld\n"
|
|
|
|
" if_speed ....................... %lx\n"
|
|
|
|
" if_physaddrlen ................. %ld\n",
|
|
|
|
entry->ent.if_index, entry->ent.if_type, entry->ent.if_mtu,
|
|
|
|
entry->ent.if_speed, entry->ent.if_physaddrlen);
|
2018-07-30 04:38:14 +00:00
|
|
|
fprintf(stderr,
|
2018-08-08 17:43:46 +00:00
|
|
|
" if_physaddr .................... %02x:%02x:%02x:%02x:%02x:%02x\n"
|
|
|
|
" if_descr ....................... %s\n",
|
|
|
|
entry->ent.if_physaddr[0] & 0xff, entry->ent.if_physaddr[1] & 0xff,
|
|
|
|
entry->ent.if_physaddr[2] & 0xff, entry->ent.if_physaddr[3] & 0xff,
|
|
|
|
entry->ent.if_physaddr[4] & 0xff, entry->ent.if_physaddr[5] & 0xff,
|
|
|
|
entry->ent.if_descr);
|
2018-07-30 04:38:14 +00:00
|
|
|
fprintf(stderr, "} status %08lx\n", status);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS
|
|
|
|
tdiGetSetOfThings(HANDLE tcpFile, DWORD toiClass, DWORD toiType, DWORD toiId,
|
|
|
|
DWORD teiEntity, DWORD teiInstance, DWORD fixedPart,
|
|
|
|
DWORD entrySize, PVOID *tdiEntitySet, PDWORD numEntries)
|
|
|
|
{
|
2018-11-08 00:09:49 +00:00
|
|
|
TCP_REQUEST_QUERY_INFORMATION_EX req;
|
2018-07-30 04:38:14 +00:00
|
|
|
PVOID entitySet = 0;
|
|
|
|
NTSTATUS status = 0;
|
|
|
|
DWORD allocationSizeForEntityArray = entrySize * MAX_TDI_ENTITIES,
|
|
|
|
arraySize = entrySize * MAX_TDI_ENTITIES;
|
|
|
|
|
2018-11-08 00:09:49 +00:00
|
|
|
memset(&req, 0, sizeof(req));
|
2018-07-30 04:38:14 +00:00
|
|
|
req.ID.toi_class = toiClass;
|
|
|
|
req.ID.toi_type = toiType;
|
|
|
|
req.ID.toi_id = toiId;
|
|
|
|
req.ID.toi_entity.tei_entity = teiEntity;
|
|
|
|
req.ID.toi_entity.tei_instance = teiInstance;
|
|
|
|
|
|
|
|
/* There's a subtle problem here...
|
|
|
|
* If an interface is added at this exact instant, (as if by a PCMCIA
|
|
|
|
* card insertion), the array will still not have enough entries after
|
|
|
|
* have allocated it after the first DeviceIoControl call.
|
|
|
|
*
|
|
|
|
* We'll get around this by repeating until the number of interfaces
|
|
|
|
* stabilizes.
|
|
|
|
*/
|
|
|
|
do
|
|
|
|
{
|
|
|
|
status =
|
|
|
|
DeviceIoControl(tcpFile, IOCTL_TCP_QUERY_INFORMATION_EX, &req,
|
|
|
|
sizeof(req), 0, 0, &allocationSizeForEntityArray, NULL);
|
|
|
|
|
|
|
|
if(!status)
|
|
|
|
return 0xc0000001;
|
|
|
|
|
2018-08-04 00:06:11 +00:00
|
|
|
arraySize = allocationSizeForEntityArray;
|
2018-07-30 04:38:14 +00:00
|
|
|
entitySet = HeapAlloc(GetProcessHeap(), 0, arraySize);
|
|
|
|
|
|
|
|
if(!entitySet)
|
|
|
|
{
|
|
|
|
status = ((NTSTATUS)0xC000009A);
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
status = DeviceIoControl(tcpFile, IOCTL_TCP_QUERY_INFORMATION_EX, &req,
|
|
|
|
sizeof(req), entitySet, arraySize,
|
|
|
|
&allocationSizeForEntityArray, NULL);
|
|
|
|
|
|
|
|
/* This is why we have the loop -- we might have added an adapter */
|
|
|
|
if(arraySize == allocationSizeForEntityArray)
|
|
|
|
break;
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, entitySet);
|
|
|
|
entitySet = 0;
|
|
|
|
|
|
|
|
if(!status)
|
|
|
|
return 0xc0000001;
|
|
|
|
} while(TRUE); /* We break if the array we received was the size we
|
|
|
|
* expected. Therefore, we got here because it wasn't */
|
|
|
|
|
|
|
|
*numEntries = (arraySize - fixedPart) / entrySize;
|
|
|
|
*tdiEntitySet = entitySet;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static NTSTATUS
|
2018-08-08 17:43:46 +00:00
|
|
|
tdiGetEntityIDSet(HANDLE tcpFile, TDIEntityID **entitySet, PDWORD numEntities)
|
2018-07-30 04:38:14 +00:00
|
|
|
{
|
2018-08-08 17:43:46 +00:00
|
|
|
NTSTATUS status =
|
|
|
|
tdiGetSetOfThings(tcpFile, INFO_CLASS_GENERIC, INFO_TYPE_PROVIDER,
|
|
|
|
ENTITY_LIST_ID, GENERIC_ENTITY, 0, 0,
|
|
|
|
sizeof(TDIEntityID), (PVOID *)entitySet, numEntities);
|
2018-07-30 04:38:14 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
tdiGetIpAddrsForIpEntity(HANDLE tcpFile, TDIEntityID *ent, IPAddrEntry **addrs,
|
|
|
|
PDWORD numAddrs)
|
|
|
|
{
|
|
|
|
NTSTATUS status;
|
|
|
|
|
2018-10-27 23:28:01 +00:00
|
|
|
#ifdef DEBUG
|
2018-08-08 17:43:46 +00:00
|
|
|
fprintf(stderr, "TdiGetIpAddrsForIpEntity(tcpFile 0x%p, entityId 0x%lx)\n",
|
|
|
|
tcpFile, ent->tei_instance);
|
2018-10-27 23:28:01 +00:00
|
|
|
#endif
|
2018-07-30 04:38:14 +00:00
|
|
|
|
|
|
|
status = tdiGetSetOfThings(tcpFile, INFO_CLASS_PROTOCOL, INFO_TYPE_PROVIDER,
|
2018-08-08 17:43:46 +00:00
|
|
|
0x102, CL_NL_ENTITY, ent->tei_instance, 0,
|
|
|
|
sizeof(IPAddrEntry), (PVOID *)addrs, numAddrs);
|
2018-07-30 04:38:14 +00:00
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VOID
|
|
|
|
tdiFreeThingSet(PVOID things)
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, things);
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
openTcpFile(PHANDLE tcpFile, ACCESS_MASK DesiredAccess)
|
|
|
|
{
|
|
|
|
UNICODE_STRING fileName;
|
|
|
|
OBJECT_ATTRIBUTES objectAttributes;
|
|
|
|
IO_STATUS_BLOCK ioStatusBlock;
|
|
|
|
NTSTATUS status;
|
|
|
|
pRtlInitUString _RtlInitUnicodeString;
|
|
|
|
pNTOpenFile _NTOpenFile;
|
|
|
|
HANDLE ntdll;
|
|
|
|
|
|
|
|
ntdll = GetModuleHandle("ntdll.dll");
|
2018-08-08 17:43:46 +00:00
|
|
|
_RtlInitUnicodeString =
|
|
|
|
(pRtlInitUString)GetProcAddress(ntdll, "RtlInitUnicodeString");
|
2018-07-30 04:38:14 +00:00
|
|
|
_NTOpenFile = (pNTOpenFile)GetProcAddress(ntdll, "NtOpenFile");
|
|
|
|
_RtlInitUnicodeString(&fileName, TcpFileName);
|
|
|
|
InitializeObjectAttributes(&objectAttributes, &fileName, OBJ_CASE_INSENSITIVE,
|
|
|
|
NULL, NULL);
|
|
|
|
status = _NTOpenFile(tcpFile, DesiredAccess | SYNCHRONIZE, &objectAttributes,
|
2018-08-08 17:43:46 +00:00
|
|
|
&ioStatusBlock, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
|
|
|
FILE_SYNCHRONOUS_IO_NONALERT);
|
2018-07-30 04:38:14 +00:00
|
|
|
/* String does not need to be freed: it points to the constant
|
|
|
|
* string we provided */
|
|
|
|
if(!NT_SUCCESS(status))
|
|
|
|
*tcpFile = INVALID_HANDLE_VALUE;
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
VOID
|
|
|
|
closeTcpFile(HANDLE h)
|
|
|
|
{
|
|
|
|
pNTClose _NTClose;
|
|
|
|
HANDLE ntdll = GetModuleHandle("ntdll.dll");
|
2018-08-08 17:43:46 +00:00
|
|
|
_NTClose = (pNTClose)GetProcAddress(ntdll, "NtClose");
|
2018-07-30 04:38:14 +00:00
|
|
|
assert(h != INVALID_HANDLE_VALUE);
|
|
|
|
_NTClose(h);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
isLoopback(HANDLE tcpFile, TDIEntityID *loop_maybe)
|
|
|
|
{
|
|
|
|
IFEntrySafelySized entryInfo;
|
|
|
|
NTSTATUS status;
|
|
|
|
|
|
|
|
status = tdiGetMibForIfEntity(tcpFile, loop_maybe, &entryInfo);
|
|
|
|
|
|
|
|
return NT_SUCCESS(status)
|
|
|
|
&& (entryInfo.ent.if_type == IFENT_SOFTWARE_LOOPBACK);
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
isIpEntity(HANDLE tcpFile, TDIEntityID *ent)
|
|
|
|
{
|
2018-11-08 00:09:49 +00:00
|
|
|
UNREFERENCED_PARAMETER(tcpFile);
|
2018-07-30 04:38:14 +00:00
|
|
|
return (ent->tei_entity == CL_NL_ENTITY || ent->tei_entity == CO_NL_ENTITY);
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
getNthIpEntity(HANDLE tcpFile, DWORD index, TDIEntityID *ent)
|
|
|
|
{
|
|
|
|
DWORD numEntities = 0;
|
|
|
|
DWORD numRoutes = 0;
|
|
|
|
TDIEntityID *entitySet = 0;
|
|
|
|
NTSTATUS status = tdiGetEntityIDSet(tcpFile, &entitySet, &numEntities);
|
2018-11-08 00:09:49 +00:00
|
|
|
unsigned i;
|
2018-07-30 04:38:14 +00:00
|
|
|
|
|
|
|
if(!NT_SUCCESS(status))
|
|
|
|
return status;
|
|
|
|
|
|
|
|
for(i = 0; i < numEntities; i++)
|
|
|
|
{
|
|
|
|
if(isIpEntity(tcpFile, &entitySet[i]))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Entity %d is an IP Entity\n", i);
|
|
|
|
if(numRoutes == index)
|
|
|
|
break;
|
|
|
|
else
|
|
|
|
numRoutes++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(numRoutes == index && i < numEntities)
|
|
|
|
{
|
2018-08-08 17:43:46 +00:00
|
|
|
fprintf(stderr, "Index %lu is entity #%d - %04lx:%08lx\n", index, i,
|
|
|
|
entitySet[i].tei_entity, entitySet[i].tei_instance);
|
2018-07-30 04:38:14 +00:00
|
|
|
memcpy(ent, &entitySet[i], sizeof(*ent));
|
|
|
|
tdiFreeThingSet(entitySet);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tdiFreeThingSet(entitySet);
|
|
|
|
return 0xc000001;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BOOL
|
|
|
|
isInterface(TDIEntityID *if_maybe)
|
|
|
|
{
|
|
|
|
return if_maybe->tei_entity == IF_ENTITY;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
getInterfaceInfoSet(HANDLE tcpFile, IFInfo **infoSet, PDWORD numInterfaces)
|
|
|
|
{
|
|
|
|
DWORD numEntities;
|
|
|
|
TDIEntityID *entIDSet = NULL;
|
|
|
|
NTSTATUS status = tdiGetEntityIDSet(tcpFile, &entIDSet, &numEntities);
|
|
|
|
IFInfo *infoSetInt = 0;
|
2018-11-08 00:09:49 +00:00
|
|
|
int curInterf = 0;
|
|
|
|
unsigned i;
|
2018-07-30 04:38:14 +00:00
|
|
|
|
|
|
|
if(!NT_SUCCESS(status))
|
|
|
|
{
|
2018-08-08 17:43:46 +00:00
|
|
|
fprintf(stderr, "getInterfaceInfoSet: tdiGetEntityIDSet() failed: 0x%lx\n",
|
|
|
|
status);
|
2018-07-30 04:38:14 +00:00
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
infoSetInt = HeapAlloc(GetProcessHeap(), 0, sizeof(IFInfo) * numEntities);
|
|
|
|
|
|
|
|
if(infoSetInt)
|
|
|
|
{
|
|
|
|
for(i = 0; i < numEntities; i++)
|
|
|
|
{
|
|
|
|
if(isInterface(&entIDSet[i]))
|
|
|
|
{
|
|
|
|
infoSetInt[curInterf].entity_id = entIDSet[i];
|
|
|
|
status = tdiGetMibForIfEntity(tcpFile, &entIDSet[i],
|
|
|
|
&infoSetInt[curInterf].if_info);
|
|
|
|
fprintf(stderr, "tdiGetMibForIfEntity: %08lx\n", status);
|
|
|
|
if(NT_SUCCESS(status))
|
|
|
|
{
|
|
|
|
DWORD numAddrs;
|
|
|
|
IPAddrEntry *addrs;
|
|
|
|
TDIEntityID ip_ent;
|
2018-11-08 00:09:49 +00:00
|
|
|
unsigned j;
|
2018-07-30 04:38:14 +00:00
|
|
|
|
|
|
|
status = getNthIpEntity(tcpFile, curInterf, &ip_ent);
|
|
|
|
if(NT_SUCCESS(status))
|
|
|
|
status =
|
|
|
|
tdiGetIpAddrsForIpEntity(tcpFile, &ip_ent, &addrs, &numAddrs);
|
|
|
|
for(j = 0; NT_SUCCESS(status) && j < numAddrs; j++)
|
|
|
|
{
|
2018-08-08 17:43:46 +00:00
|
|
|
fprintf(stderr, "ADDR %d: index %ld (target %ld)\n", j,
|
|
|
|
addrs[j].iae_index,
|
|
|
|
infoSetInt[curInterf].if_info.ent.if_index);
|
2018-07-30 04:38:14 +00:00
|
|
|
if(addrs[j].iae_index == infoSetInt[curInterf].if_info.ent.if_index)
|
|
|
|
{
|
|
|
|
memcpy(&infoSetInt[curInterf].ip_addr, &addrs[j],
|
|
|
|
sizeof(addrs[j]));
|
|
|
|
curInterf++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(NT_SUCCESS(status))
|
|
|
|
tdiFreeThingSet(addrs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tdiFreeThingSet(entIDSet);
|
|
|
|
|
|
|
|
if(NT_SUCCESS(status))
|
|
|
|
{
|
|
|
|
*infoSet = infoSetInt;
|
|
|
|
*numInterfaces = curInterf;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, infoSetInt);
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tdiFreeThingSet(entIDSet);
|
|
|
|
return ((NTSTATUS)0xC000009A);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
getInterfaceInfoByName(HANDLE tcpFile, char *name, IFInfo *info)
|
|
|
|
{
|
|
|
|
IFInfo *ifInfo;
|
|
|
|
DWORD numInterfaces;
|
2018-11-08 00:09:49 +00:00
|
|
|
unsigned i;
|
2018-07-30 04:38:14 +00:00
|
|
|
NTSTATUS status = getInterfaceInfoSet(tcpFile, &ifInfo, &numInterfaces);
|
|
|
|
|
|
|
|
if(NT_SUCCESS(status))
|
|
|
|
{
|
|
|
|
for(i = 0; i < numInterfaces; i++)
|
|
|
|
{
|
|
|
|
if(!strcmp((PCHAR)ifInfo[i].if_info.ent.if_descr, name))
|
|
|
|
{
|
|
|
|
memcpy(info, &ifInfo[i], sizeof(*info));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, ifInfo);
|
|
|
|
|
|
|
|
return i < numInterfaces ? 0 : 0xc0000001;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
getInterfaceInfoByIndex(HANDLE tcpFile, DWORD index, IFInfo *info)
|
|
|
|
{
|
|
|
|
IFInfo *ifInfo;
|
|
|
|
DWORD numInterfaces;
|
|
|
|
NTSTATUS status = getInterfaceInfoSet(tcpFile, &ifInfo, &numInterfaces);
|
2018-11-08 00:09:49 +00:00
|
|
|
unsigned i;
|
2018-07-30 04:38:14 +00:00
|
|
|
|
|
|
|
if(NT_SUCCESS(status))
|
|
|
|
{
|
|
|
|
for(i = 0; i < numInterfaces; i++)
|
|
|
|
{
|
|
|
|
if(ifInfo[i].if_info.ent.if_index == index)
|
|
|
|
{
|
|
|
|
memcpy(info, &ifInfo[i], sizeof(*info));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, ifInfo);
|
|
|
|
|
|
|
|
return i < numInterfaces ? 0 : 0xc0000001;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
NTSTATUS
|
|
|
|
getIPAddrEntryForIf(HANDLE tcpFile, char *name, DWORD index, IFInfo *ifInfo)
|
|
|
|
{
|
|
|
|
NTSTATUS status = name ? getInterfaceInfoByName(tcpFile, name, ifInfo)
|
|
|
|
: getInterfaceInfoByIndex(tcpFile, index, ifInfo);
|
|
|
|
|
|
|
|
if(!NT_SUCCESS(status))
|
|
|
|
{
|
2018-08-08 17:43:46 +00:00
|
|
|
fprintf(stderr, "getIPAddrEntryForIf returning %lx\n", status);
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
InterfaceIndexTable *
|
|
|
|
getInterfaceIndexTableInt(BOOL nonLoopbackOnly)
|
|
|
|
{
|
|
|
|
DWORD numInterfaces, curInterface = 0;
|
2018-11-08 00:09:49 +00:00
|
|
|
unsigned i;
|
2018-07-30 04:38:14 +00:00
|
|
|
IFInfo *ifInfo;
|
|
|
|
InterfaceIndexTable *ret = 0;
|
|
|
|
HANDLE tcpFile;
|
|
|
|
NTSTATUS status = openTcpFile(&tcpFile, FILE_READ_DATA);
|
|
|
|
|
2018-10-27 23:28:01 +00:00
|
|
|
ifInfo = NULL;
|
|
|
|
|
2018-07-30 04:38:14 +00:00
|
|
|
if(NT_SUCCESS(status))
|
|
|
|
{
|
|
|
|
status = getInterfaceInfoSet(tcpFile, &ifInfo, &numInterfaces);
|
|
|
|
|
2018-08-08 17:43:46 +00:00
|
|
|
fprintf(stderr, "InterfaceInfoSet: %08lx, %04lx:%08lx\n", status,
|
|
|
|
ifInfo->entity_id.tei_entity, ifInfo->entity_id.tei_instance);
|
2018-07-30 04:38:14 +00:00
|
|
|
|
|
|
|
if(NT_SUCCESS(status))
|
|
|
|
{
|
|
|
|
ret = (InterfaceIndexTable *)calloc(
|
|
|
|
1, sizeof(InterfaceIndexTable) + (numInterfaces - 1) * sizeof(DWORD));
|
|
|
|
|
|
|
|
if(ret)
|
|
|
|
{
|
|
|
|
ret->numAllocated = numInterfaces;
|
2018-08-08 17:43:46 +00:00
|
|
|
fprintf(stderr, "NumInterfaces = %ld\n", numInterfaces);
|
2018-07-30 04:38:14 +00:00
|
|
|
|
|
|
|
for(i = 0; i < numInterfaces; i++)
|
|
|
|
{
|
2018-08-08 17:43:46 +00:00
|
|
|
fprintf(stderr, "Examining interface %d\n", i);
|
2018-07-30 04:38:14 +00:00
|
|
|
if(!nonLoopbackOnly || !isLoopback(tcpFile, &ifInfo[i].entity_id))
|
|
|
|
{
|
2018-08-08 17:43:46 +00:00
|
|
|
fprintf(stderr, "Interface %d matches (%ld)\n", i, curInterface);
|
2018-07-30 04:38:14 +00:00
|
|
|
ret->indexes[curInterface++] = ifInfo[i].if_info.ent.if_index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret->numIndexes = curInterface;
|
|
|
|
}
|
|
|
|
|
|
|
|
tdiFreeThingSet(ifInfo);
|
|
|
|
}
|
|
|
|
closeTcpFile(tcpFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
InterfaceIndexTable *
|
|
|
|
getInterfaceIndexTable(void)
|
|
|
|
{
|
|
|
|
return getInterfaceIndexTableInt(FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2018-10-26 04:27:38 +00:00
|
|
|
// there's probably an use case for a _newer_ implementation
|
|
|
|
// of pthread_setname_np(3), in fact, I may just merge _this_
|
|
|
|
// upstream...
|
|
|
|
#if 0
|
2018-07-30 04:38:14 +00:00
|
|
|
#include <windows.h>
|
2018-08-20 10:38:25 +00:00
|
|
|
|
|
|
|
typedef HRESULT(FAR PASCAL *p_SetThreadDescription)(void *, const wchar_t *);
|
2018-07-30 04:38:14 +00:00
|
|
|
#define EXCEPTION_SET_THREAD_NAME ((DWORD)0x406D1388)
|
2018-08-20 10:38:25 +00:00
|
|
|
|
2018-07-30 04:38:14 +00:00
|
|
|
typedef struct _THREADNAME_INFO
|
|
|
|
{
|
|
|
|
DWORD dwType; /* must be 0x1000 */
|
|
|
|
LPCSTR szName; /* pointer to name (in user addr space) */
|
|
|
|
DWORD dwThreadID; /* thread ID (-1=caller thread) */
|
|
|
|
DWORD dwFlags; /* reserved for future use, must be zero */
|
|
|
|
} THREADNAME_INFO;
|
|
|
|
|
|
|
|
void
|
|
|
|
SetThreadName(DWORD dwThreadID, LPCSTR szThreadName)
|
|
|
|
{
|
|
|
|
THREADNAME_INFO info;
|
|
|
|
DWORD infosize;
|
2018-08-20 10:38:25 +00:00
|
|
|
HANDLE hThread;
|
|
|
|
/* because loonix is SHIT and limits thread names to 16 bytes */
|
|
|
|
wchar_t thr_name_w[16];
|
|
|
|
p_SetThreadDescription _SetThreadDescription;
|
2018-08-20 10:52:47 +00:00
|
|
|
|
|
|
|
/* current win10 flights now have a new named-thread API, let's try to use
|
|
|
|
* that first! */
|
2018-08-20 10:38:25 +00:00
|
|
|
/* first, dlsym(2) the new call from system library */
|
2018-08-27 13:44:16 +00:00
|
|
|
hThread = NULL;
|
2018-08-20 10:52:47 +00:00
|
|
|
_SetThreadDescription = (p_SetThreadDescription)GetProcAddress(
|
|
|
|
GetModuleHandle("kernel32"), "SetThreadDescription");
|
2018-08-20 10:38:25 +00:00
|
|
|
if(_SetThreadDescription)
|
2018-07-30 04:38:14 +00:00
|
|
|
{
|
2018-08-21 21:25:39 +00:00
|
|
|
/* grab another reference to the thread */
|
2018-08-20 10:38:25 +00:00
|
|
|
hThread = OpenThread(THREAD_SET_LIMITED_INFORMATION, FALSE, dwThreadID);
|
2018-08-21 21:25:39 +00:00
|
|
|
/* windows takes unicode, our input is utf-8 or plain ascii */
|
2018-08-20 10:38:25 +00:00
|
|
|
MultiByteToWideChar(CP_ACP, 0, szThreadName, -1, thr_name_w, 16);
|
|
|
|
if(hThread)
|
|
|
|
_SetThreadDescription(hThread, thr_name_w);
|
|
|
|
else
|
2018-08-20 10:52:47 +00:00
|
|
|
goto old; /* for whatever reason, we couldn't get a handle to the thread.
|
|
|
|
Just use the old method. */
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
2018-08-20 10:38:25 +00:00
|
|
|
else
|
2018-07-30 04:38:14 +00:00
|
|
|
{
|
2018-08-20 10:38:25 +00:00
|
|
|
old:
|
|
|
|
info.dwType = 0x1000;
|
|
|
|
info.szName = szThreadName;
|
|
|
|
info.dwThreadID = dwThreadID;
|
|
|
|
info.dwFlags = 0;
|
|
|
|
|
|
|
|
infosize = sizeof(info) / sizeof(DWORD);
|
|
|
|
|
|
|
|
__try
|
|
|
|
{
|
|
|
|
RaiseException(EXCEPTION_SET_THREAD_NAME, 0, infosize, (DWORD *)&info);
|
|
|
|
}
|
|
|
|
__except(EXCEPTION_EXECUTE_HANDLER)
|
|
|
|
{
|
|
|
|
}
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
2018-08-21 21:25:39 +00:00
|
|
|
/* clean up */
|
2018-08-20 11:58:18 +00:00
|
|
|
if(hThread)
|
|
|
|
CloseHandle(hThread);
|
2018-07-30 04:38:14 +00:00
|
|
|
}
|
2018-11-08 00:09:49 +00:00
|
|
|
#endif
|