mirror of https://github.com/oxen-io/lokinet
commit
3601df01ce
@ -0,0 +1,315 @@
|
||||
#if defined(__MINGW32__) && !defined(_WIN64)
|
||||
/*
|
||||
* Contains routines missing from WS2_32.DLL until 2006, if yer using
|
||||
* Microsoft C/C++, then this code is irrelevant, as the official
|
||||
* Platform SDK already links against these routines in the correct
|
||||
* libraries.
|
||||
*
|
||||
* -despair86 30/07/18
|
||||
*/
|
||||
|
||||
// these need to be in a specific order
|
||||
#include <assert.h>
|
||||
#include <llarp/net.h>
|
||||
#include <windows.h>
|
||||
#include <iphlpapi.h>
|
||||
#if WINNT_CROSS_COMPILE && !NTSTATUS
|
||||
typedef LONG NTSTATUS;
|
||||
#endif
|
||||
#include "win32_intrnl.h"
|
||||
|
||||
const char *
|
||||
inet_ntop(int af, const void *src, char *dst, size_t size)
|
||||
{
|
||||
int address_length;
|
||||
DWORD string_length = size;
|
||||
struct sockaddr_storage sa;
|
||||
struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
|
||||
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa;
|
||||
|
||||
memset(&sa, 0, sizeof(sa));
|
||||
switch(af)
|
||||
{
|
||||
case AF_INET:
|
||||
address_length = sizeof(struct sockaddr_in);
|
||||
sin->sin_family = af;
|
||||
memcpy(&sin->sin_addr, src, sizeof(struct in_addr));
|
||||
break;
|
||||
|
||||
case AF_INET6:
|
||||
address_length = sizeof(struct sockaddr_in6);
|
||||
sin6->sin6_family = af;
|
||||
memcpy(&sin6->sin6_addr, src, sizeof(struct in6_addr));
|
||||
break;
|
||||
|
||||
default:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if(WSAAddressToString((LPSOCKADDR)&sa, address_length, NULL, dst,
|
||||
&string_length)
|
||||
== 0)
|
||||
{
|
||||
return dst;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int
|
||||
inet_pton(int af, const char *src, void *dst)
|
||||
{
|
||||
int address_length;
|
||||
struct sockaddr_storage sa;
|
||||
struct sockaddr_in *sin = (struct sockaddr_in *)&sa;
|
||||
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&sa;
|
||||
|
||||
switch(af)
|
||||
{
|
||||
case AF_INET:
|
||||
address_length = sizeof(struct sockaddr_in);
|
||||
break;
|
||||
|
||||
case AF_INET6:
|
||||
address_length = sizeof(struct sockaddr_in6);
|
||||
break;
|
||||
|
||||
default:
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(WSAStringToAddress((LPTSTR)src, af, NULL, (LPSOCKADDR)&sa, &address_length)
|
||||
== 0)
|
||||
{
|
||||
switch(af)
|
||||
{
|
||||
case AF_INET:
|
||||
memcpy(dst, &sin->sin_addr, sizeof(struct in_addr));
|
||||
break;
|
||||
|
||||
case AF_INET6:
|
||||
memcpy(dst, &sin6->sin6_addr, sizeof(struct in6_addr));
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
typedef struct _InterfaceIndexTable
|
||||
{
|
||||
DWORD numIndexes;
|
||||
IF_INDEX indexes[1];
|
||||
} InterfaceIndexTable;
|
||||
|
||||
// windows 2000
|
||||
// todo(despair86): implement IPv6 detection using
|
||||
// the ipv6 preview stack/adv net pack from 1999/2001
|
||||
DWORD FAR PASCAL
|
||||
_GetAdaptersAddresses(ULONG Family, ULONG Flags, PVOID Reserved,
|
||||
PIP_ADAPTER_ADDRESSES pAdapterAddresses,
|
||||
PULONG pOutBufLen)
|
||||
{
|
||||
InterfaceIndexTable *indexTable;
|
||||
IFInfo ifInfo;
|
||||
int i;
|
||||
ULONG ret, requiredSize = 0;
|
||||
PIP_ADAPTER_ADDRESSES currentAddress;
|
||||
PUCHAR currentLocation;
|
||||
HANDLE tcpFile;
|
||||
|
||||
(void)(Family);
|
||||
if(!pOutBufLen)
|
||||
return ERROR_INVALID_PARAMETER;
|
||||
if(Reserved)
|
||||
return ERROR_INVALID_PARAMETER;
|
||||
|
||||
indexTable = getInterfaceIndexTable();
|
||||
if(!indexTable)
|
||||
return ERROR_NOT_ENOUGH_MEMORY;
|
||||
|
||||
ret = openTcpFile(&tcpFile, FILE_READ_DATA);
|
||||
if(!NT_SUCCESS(ret))
|
||||
return ERROR_NO_DATA;
|
||||
|
||||
for(i = indexTable->numIndexes; i >= 0; i--)
|
||||
{
|
||||
if(NT_SUCCESS(
|
||||
getIPAddrEntryForIf(tcpFile, NULL, indexTable->indexes[i], &ifInfo)))
|
||||
{
|
||||
/* The whole struct */
|
||||
requiredSize += sizeof(IP_ADAPTER_ADDRESSES);
|
||||
|
||||
/* Friendly name */
|
||||
if(!(Flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
|
||||
requiredSize +=
|
||||
strlen((char *)ifInfo.if_info.ent.if_descr) + 1; // FIXME
|
||||
|
||||
/* Adapter name */
|
||||
requiredSize += strlen((char *)ifInfo.if_info.ent.if_descr) + 1;
|
||||
|
||||
/* Unicast address */
|
||||
if(!(Flags & GAA_FLAG_SKIP_UNICAST))
|
||||
requiredSize += sizeof(IP_ADAPTER_UNICAST_ADDRESS);
|
||||
|
||||
/* FIXME: Implement multicast, anycast, and dns server stuff */
|
||||
|
||||
/* FIXME: Implement dns suffix and description */
|
||||
requiredSize += 2 * sizeof(WCHAR);
|
||||
|
||||
/* We're only going to implement what's required for XP SP0 */
|
||||
}
|
||||
}
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "size: %ld, requiredSize: %ld\n", *pOutBufLen, requiredSize);
|
||||
#endif
|
||||
if(!pAdapterAddresses || *pOutBufLen < requiredSize)
|
||||
{
|
||||
*pOutBufLen = requiredSize;
|
||||
closeTcpFile(tcpFile);
|
||||
free(indexTable);
|
||||
return ERROR_BUFFER_OVERFLOW;
|
||||
}
|
||||
|
||||
RtlZeroMemory(pAdapterAddresses, requiredSize);
|
||||
|
||||
/* Let's set up the pointers */
|
||||
currentAddress = pAdapterAddresses;
|
||||
for(i = indexTable->numIndexes; i >= 0; i--)
|
||||
{
|
||||
if(NT_SUCCESS(
|
||||
getIPAddrEntryForIf(tcpFile, NULL, indexTable->indexes[i], &ifInfo)))
|
||||
{
|
||||
currentLocation =
|
||||
(PUCHAR)currentAddress + (ULONG_PTR)sizeof(IP_ADAPTER_ADDRESSES);
|
||||
|
||||
/* FIXME: Friendly name */
|
||||
if(!(Flags & GAA_FLAG_SKIP_FRIENDLY_NAME))
|
||||
{
|
||||
currentAddress->FriendlyName = (PVOID)currentLocation;
|
||||
currentLocation += sizeof(WCHAR);
|
||||
}
|
||||
|
||||
/* Adapter name */
|
||||
currentAddress->AdapterName = (PVOID)currentLocation;
|
||||
currentLocation += strlen((char *)ifInfo.if_info.ent.if_descr) + 1;
|
||||
|
||||
/* Unicast address */
|
||||
if(!(Flags & GAA_FLAG_SKIP_UNICAST))
|
||||
{
|
||||
currentAddress->FirstUnicastAddress = (PVOID)currentLocation;
|
||||
currentLocation += sizeof(IP_ADAPTER_UNICAST_ADDRESS);
|
||||
currentAddress->FirstUnicastAddress->Address.lpSockaddr =
|
||||
(PVOID)currentLocation;
|
||||
currentLocation += sizeof(struct sockaddr);
|
||||
}
|
||||
|
||||
/* FIXME: Implement multicast, anycast, and dns server stuff */
|
||||
|
||||
/* FIXME: Implement dns suffix and description */
|
||||
currentAddress->DnsSuffix = (PVOID)currentLocation;
|
||||
currentLocation += sizeof(WCHAR);
|
||||
|
||||
currentAddress->Description = (PVOID)currentLocation;
|
||||
currentLocation += sizeof(WCHAR);
|
||||
|
||||
currentAddress->Next = (PVOID)currentLocation;
|
||||
/* Terminate the last address correctly */
|
||||
if(i == 0)
|
||||
currentAddress->Next = NULL;
|
||||
|
||||
/* We're only going to implement what's required for XP SP0 */
|
||||
|
||||
currentAddress = currentAddress->Next;
|
||||
}
|
||||
}
|
||||
|
||||
/* Now again, for real this time */
|
||||
|
||||
currentAddress = pAdapterAddresses;
|
||||
for(i = indexTable->numIndexes; i >= 0; i--)
|
||||
{
|
||||
if(NT_SUCCESS(
|
||||
getIPAddrEntryForIf(tcpFile, NULL, indexTable->indexes[i], &ifInfo)))
|
||||
{
|
||||
/* Make sure we're not looping more than we hoped for */
|
||||
assert(currentAddress);
|
||||
|
||||
/* Alignment information */
|
||||
currentAddress->Length = sizeof(IP_ADAPTER_ADDRESSES);
|
||||
currentAddress->IfIndex = indexTable->indexes[i];
|
||||
|
||||
/* Adapter name */
|
||||
strcpy(currentAddress->AdapterName, (char *)ifInfo.if_info.ent.if_descr);
|
||||
|
||||
if(!(Flags & GAA_FLAG_SKIP_UNICAST))
|
||||
{
|
||||
currentAddress->FirstUnicastAddress->Length =
|
||||
sizeof(IP_ADAPTER_UNICAST_ADDRESS);
|
||||
currentAddress->FirstUnicastAddress->Flags = 0; // FIXME
|
||||
currentAddress->FirstUnicastAddress->Next =
|
||||
NULL; // FIXME: Support more than one address per adapter
|
||||
currentAddress->FirstUnicastAddress->Address.lpSockaddr->sa_family =
|
||||
AF_INET;
|
||||
memcpy(currentAddress->FirstUnicastAddress->Address.lpSockaddr->sa_data,
|
||||
&ifInfo.ip_addr.iae_addr, sizeof(ifInfo.ip_addr.iae_addr));
|
||||
currentAddress->FirstUnicastAddress->Address.iSockaddrLength =
|
||||
sizeof(ifInfo.ip_addr.iae_addr) + sizeof(USHORT);
|
||||
currentAddress->FirstUnicastAddress->PrefixOrigin =
|
||||
IpPrefixOriginOther; // FIXME
|
||||
currentAddress->FirstUnicastAddress->SuffixOrigin =
|
||||
IpSuffixOriginOther; // FIXME
|
||||
currentAddress->FirstUnicastAddress->DadState =
|
||||
IpDadStatePreferred; // FIXME
|
||||
currentAddress->FirstUnicastAddress->ValidLifetime =
|
||||
0xFFFFFFFF; // FIXME
|
||||
currentAddress->FirstUnicastAddress->PreferredLifetime =
|
||||
0xFFFFFFFF; // FIXME
|
||||
currentAddress->FirstUnicastAddress->LeaseLifetime =
|
||||
0xFFFFFFFF; // FIXME
|
||||
}
|
||||
|
||||
/* FIXME: Implement multicast, anycast, and dns server stuff */
|
||||
currentAddress->FirstAnycastAddress = NULL;
|
||||
currentAddress->FirstMulticastAddress = NULL;
|
||||
currentAddress->FirstDnsServerAddress = NULL;
|
||||
|
||||
/* FIXME: Implement dns suffix, description, and friendly name */
|
||||
currentAddress->DnsSuffix[0] = UNICODE_NULL;
|
||||
currentAddress->Description[0] = UNICODE_NULL;
|
||||
currentAddress->FriendlyName[0] = UNICODE_NULL;
|
||||
|
||||
/* Physical Address */
|
||||
memcpy(currentAddress->PhysicalAddress, ifInfo.if_info.ent.if_physaddr,
|
||||
ifInfo.if_info.ent.if_physaddrlen);
|
||||
currentAddress->PhysicalAddressLength = ifInfo.if_info.ent.if_physaddrlen;
|
||||
|
||||
/* Flags */
|
||||
currentAddress->Flags = 0; // FIXME
|
||||
|
||||
/* MTU */
|
||||
currentAddress->Mtu = ifInfo.if_info.ent.if_mtu;
|
||||
|
||||
/* Interface type */
|
||||
currentAddress->IfType = ifInfo.if_info.ent.if_type;
|
||||
|
||||
/* Operational status */
|
||||
if(ifInfo.if_info.ent.if_operstatus >= IF_OPER_STATUS_CONNECTING)
|
||||
currentAddress->OperStatus = IfOperStatusUp;
|
||||
else
|
||||
currentAddress->OperStatus = IfOperStatusDown;
|
||||
|
||||
/* We're only going to implement what's required for XP SP0 */
|
||||
|
||||
/* Move to the next address */
|
||||
currentAddress = currentAddress->Next;
|
||||
}
|
||||
}
|
||||
|
||||
closeTcpFile(tcpFile);
|
||||
free(indexTable);
|
||||
|
||||
return NO_ERROR;
|
||||
}
|
||||
#endif
|
@ -0,0 +1,572 @@
|
||||
#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.
|
||||
* 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
|
||||
*/
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// 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
|
||||
|
||||
// these need to be in a specific order
|
||||
#include <windows.h>
|
||||
#include <winternl.h>
|
||||
#include <tdi.h>
|
||||
#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) \
|
||||
CTL_CODE(FSCTL_TCP_BASE, Function, Method, Access)
|
||||
|
||||
#define IOCTL_TCP_QUERY_INFORMATION_EX \
|
||||
_TCP_CTL_CODE(0, METHOD_NEITHER, FILE_ANY_ACCESS)
|
||||
|
||||
typedef struct _InterfaceIndexTable
|
||||
{
|
||||
DWORD numIndexes;
|
||||
DWORD numAllocated;
|
||||
DWORD indexes[1];
|
||||
} InterfaceIndexTable;
|
||||
|
||||
NTSTATUS
|
||||
tdiGetMibForIfEntity(HANDLE tcpFile, TDIEntityID *ent,
|
||||
IFEntrySafelySized *entry)
|
||||
{
|
||||
TCP_REQUEST_QUERY_INFORMATION_EX req;
|
||||
NTSTATUS status = 0;
|
||||
DWORD returnSize;
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "TdiGetMibForIfEntity(tcpFile %x,entityId %x)\n",
|
||||
(int)tcpFile, (int)ent->tei_instance);
|
||||
#endif
|
||||
|
||||
memset(&req, 0, sizeof(req));
|
||||
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,
|
||||
"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);
|
||||
fprintf(stderr,
|
||||
" 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);
|
||||
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)
|
||||
{
|
||||
TCP_REQUEST_QUERY_INFORMATION_EX req;
|
||||
PVOID entitySet = 0;
|
||||
NTSTATUS status = 0;
|
||||
DWORD allocationSizeForEntityArray = entrySize * MAX_TDI_ENTITIES,
|
||||
arraySize = entrySize * MAX_TDI_ENTITIES;
|
||||
|
||||
memset(&req, 0, sizeof(req));
|
||||
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;
|
||||
|
||||
arraySize = allocationSizeForEntityArray;
|
||||
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
|
||||
tdiGetEntityIDSet(HANDLE tcpFile, TDIEntityID **entitySet, PDWORD numEntities)
|
||||
{
|
||||
NTSTATUS status =
|
||||
tdiGetSetOfThings(tcpFile, INFO_CLASS_GENERIC, INFO_TYPE_PROVIDER,
|
||||
ENTITY_LIST_ID, GENERIC_ENTITY, 0, 0,
|
||||
sizeof(TDIEntityID), (PVOID *)entitySet, numEntities);
|
||||
return status;
|
||||
}
|
||||
|
||||
NTSTATUS
|
||||
tdiGetIpAddrsForIpEntity(HANDLE tcpFile, TDIEntityID *ent, IPAddrEntry **addrs,
|
||||
PDWORD numAddrs)
|
||||
{
|
||||
NTSTATUS status;
|
||||
|
||||
#ifdef DEBUG
|
||||
fprintf(stderr, "TdiGetIpAddrsForIpEntity(tcpFile 0x%p, entityId 0x%lx)\n",
|
||||
tcpFile, ent->tei_instance);
|
||||
#endif
|
||||
|
||||
status = tdiGetSetOfThings(tcpFile, INFO_CLASS_PROTOCOL, INFO_TYPE_PROVIDER,
|
||||
0x102, CL_NL_ENTITY, ent->tei_instance, 0,
|
||||
sizeof(IPAddrEntry), (PVOID *)addrs, numAddrs);
|
||||
|
||||
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");
|
||||
_RtlInitUnicodeString =
|
||||
(pRtlInitUString)GetProcAddress(ntdll, "RtlInitUnicodeString");
|
||||
_NTOpenFile = (pNTOpenFile)GetProcAddress(ntdll, "NtOpenFile");
|
||||
_RtlInitUnicodeString(&fileName, TcpFileName);
|
||||
InitializeObjectAttributes(&objectAttributes, &fileName, OBJ_CASE_INSENSITIVE,
|
||||
NULL, NULL);
|
||||
status = _NTOpenFile(tcpFile, DesiredAccess | SYNCHRONIZE, &objectAttributes,
|
||||
&ioStatusBlock, FILE_SHARE_READ | FILE_SHARE_WRITE,
|
||||
FILE_SYNCHRONOUS_IO_NONALERT);
|
||||
/* 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");
|
||||
_NTClose = (pNTClose)GetProcAddress(ntdll, "NtClose");
|
||||
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)
|
||||
{
|
||||
UNREFERENCED_PARAMETER(tcpFile);
|
||||
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);
|
||||
unsigned i;
|
||||
|
||||
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)
|
||||
{
|
||||
fprintf(stderr, "Index %lu is entity #%d - %04lx:%08lx\n", index, i,
|
||||
entitySet[i].tei_entity, entitySet[i].tei_instance);
|
||||
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;
|
||||
int curInterf = 0;
|
||||
unsigned i;
|
||||
|
||||
if(!NT_SUCCESS(status))
|
||||
{
|
||||
fprintf(stderr, "getInterfaceInfoSet: tdiGetEntityIDSet() failed: 0x%lx\n",
|
||||
status);
|
||||
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;
|
||||
unsigned j;
|
||||
|
||||
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++)
|
||||
{
|
||||
fprintf(stderr, "ADDR %d: index %ld (target %ld)\n", j,
|
||||
addrs[j].iae_index,
|
||||
infoSetInt[curInterf].if_info.ent.if_index);
|
||||
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;
|
||||
unsigned i;
|
||||
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);
|
||||
unsigned i;
|
||||
|
||||
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))
|
||||
{
|
||||
fprintf(stderr, "getIPAddrEntryForIf returning %lx\n", status);
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
InterfaceIndexTable *
|
||||
getInterfaceIndexTableInt(BOOL nonLoopbackOnly)
|
||||
{
|
||||
DWORD numInterfaces, curInterface = 0;
|
||||
unsigned i;
|
||||
IFInfo *ifInfo;
|
||||
InterfaceIndexTable *ret = 0;
|
||||
HANDLE tcpFile;
|
||||
NTSTATUS status = openTcpFile(&tcpFile, FILE_READ_DATA);
|
||||
|
||||
ifInfo = NULL;
|
||||
|
||||
if(NT_SUCCESS(status))
|
||||
{
|
||||
status = getInterfaceInfoSet(tcpFile, &ifInfo, &numInterfaces);
|
||||
|
||||
fprintf(stderr, "InterfaceInfoSet: %08lx, %04lx:%08lx\n", status,
|
||||
ifInfo->entity_id.tei_entity, ifInfo->entity_id.tei_instance);
|
||||
|
||||
if(NT_SUCCESS(status))
|
||||
{
|
||||
ret = (InterfaceIndexTable *)calloc(
|
||||
1, sizeof(InterfaceIndexTable) + (numInterfaces - 1) * sizeof(DWORD));
|
||||
|
||||
if(ret)
|
||||
{
|
||||
ret->numAllocated = numInterfaces;
|
||||
fprintf(stderr, "NumInterfaces = %ld\n", numInterfaces);
|
||||
|
||||
for(i = 0; i < numInterfaces; i++)
|
||||
{
|
||||
fprintf(stderr, "Examining interface %d\n", i);
|
||||
if(!nonLoopbackOnly || !isLoopback(tcpFile, &ifInfo[i].entity_id))
|
||||
{
|
||||
fprintf(stderr, "Interface %d matches (%ld)\n", i, curInterface);
|
||||
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
|
||||
|
||||
// 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
|
||||
#include <windows.h>
|
||||
|
||||
typedef HRESULT(FAR PASCAL *p_SetThreadDescription)(void *, const wchar_t *);
|
||||
#define EXCEPTION_SET_THREAD_NAME ((DWORD)0x406D1388)
|
||||
|
||||
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;
|
||||
HANDLE hThread;
|
||||
/* because loonix is SHIT and limits thread names to 16 bytes */
|
||||
wchar_t thr_name_w[16];
|
||||
p_SetThreadDescription _SetThreadDescription;
|
||||
|
||||
/* current win10 flights now have a new named-thread API, let's try to use
|
||||
* that first! */
|
||||
/* first, dlsym(2) the new call from system library */
|
||||
hThread = NULL;
|
||||
_SetThreadDescription = (p_SetThreadDescription)GetProcAddress(
|
||||
GetModuleHandle("kernel32"), "SetThreadDescription");
|
||||
if(_SetThreadDescription)
|
||||
{
|
||||
/* grab another reference to the thread */
|
||||
hThread = OpenThread(THREAD_SET_LIMITED_INFORMATION, FALSE, dwThreadID);
|
||||
/* windows takes unicode, our input is utf-8 or plain ascii */
|
||||
MultiByteToWideChar(CP_ACP, 0, szThreadName, -1, thr_name_w, 16);
|
||||
if(hThread)
|
||||
_SetThreadDescription(hThread, thr_name_w);
|
||||
else
|
||||
goto old; /* for whatever reason, we couldn't get a handle to the thread.
|
||||
Just use the old method. */
|
||||
}
|
||||
else
|
||||
{
|
||||
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)
|
||||
{
|
||||
}
|
||||
}
|
||||
/* clean up */
|
||||
if(hThread)
|
||||
CloseHandle(hThread);
|
||||
}
|
||||
#endif
|
@ -0,0 +1,110 @@
|
||||
#ifndef WIN32_INTRNL_H
|
||||
#define WIN32_INTRNL_H
|
||||
#if defined(__MINGW32__) && !defined(_WIN64)
|
||||
#ifndef NT_SUCCESS
|
||||
#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
|
||||
#endif
|
||||
#include <tdiinfo.h>
|
||||
|
||||
typedef unsigned long ulong;
|
||||
typedef unsigned short ushort;
|
||||
typedef unsigned char uchar;
|
||||
typedef unsigned int uint;
|
||||
|
||||
/* forward declare, each module has their own idea of what this is */
|
||||
typedef struct _InterfaceIndexTable InterfaceIndexTable;
|
||||
|
||||
typedef struct IFEntry
|
||||
{
|
||||
ulong if_index;
|
||||
ulong if_type;
|
||||
ulong if_mtu;
|
||||
ulong if_speed;
|
||||
ulong if_physaddrlen;
|
||||
uchar if_physaddr[8];
|
||||
ulong if_adminstatus;
|
||||
ulong if_operstatus;
|
||||
ulong if_lastchange;
|
||||
ulong if_inoctets;
|
||||
ulong if_inucastpkts;
|
||||
ulong if_innucastpkts;
|
||||
ulong if_indiscards;
|
||||
ulong if_inerrors;
|
||||
ulong if_inunknownprotos;
|
||||
ulong if_outoctets;
|
||||
ulong if_outucastpkts;
|
||||
ulong if_outnucastpkts;
|
||||
ulong if_outdiscards;
|
||||
ulong if_outerrors;
|
||||
ulong if_outqlen;
|
||||
ulong if_descrlen;
|
||||
uchar if_descr[1];
|
||||
} IFEntry;
|
||||
|
||||
typedef struct IPAddrEntry
|
||||
{
|
||||
ulong iae_addr;
|
||||
ulong iae_index;
|
||||
ulong iae_mask;
|
||||
ulong iae_bcastaddr;
|
||||
ulong iae_reasmsize;
|
||||
ushort iae_context;
|
||||
ushort iae_pad;
|
||||
} IPAddrEntry;
|
||||
|
||||
typedef union _IFEntrySafelySized {
|
||||
CHAR MaxSize[sizeof(DWORD) + sizeof(IFEntry) + 128 + 1];
|
||||
IFEntry ent;
|
||||
} IFEntrySafelySized;
|
||||
|
||||
#ifndef IFENT_SOFTWARE_LOOPBACK
|
||||
#define IFENT_SOFTWARE_LOOPBACK 24 /* This is an SNMP constant from rfc1213 */
|
||||
#endif /*IFENT_SOFTWARE_LOOPBACK*/
|
||||
|
||||
/* Encapsulates information about an interface */
|
||||
typedef struct _IFInfo
|
||||
{
|
||||
TDIEntityID entity_id;
|
||||
IFEntrySafelySized if_info;
|
||||
IPAddrEntry ip_addr;
|
||||
} IFInfo;
|
||||
|
||||
/* functions */
|
||||
NTSTATUS
|
||||
openTcpFile(PHANDLE tcpFile, ACCESS_MASK DesiredAccess);
|
||||
|
||||
VOID
|
||||
closeTcpFile(HANDLE h);
|
||||
|
||||
BOOL
|
||||
isLoopback(HANDLE tcpFile, TDIEntityID* loop_maybe);
|
||||
|
||||
BOOL
|
||||
isIpEntity(HANDLE tcpFile, TDIEntityID* ent);
|
||||
|
||||
NTSTATUS
|
||||
getNthIpEntity(HANDLE tcpFile, DWORD index, TDIEntityID* ent);
|
||||
|
||||
BOOL
|
||||
isInterface(TDIEntityID* if_maybe);
|
||||
|
||||
NTSTATUS
|
||||
getInterfaceInfoSet(HANDLE tcpFile, IFInfo** infoSet, PDWORD numInterfaces);
|
||||
|
||||
NTSTATUS
|
||||
getInterfaceInfoByName(HANDLE tcpFile, char* name, IFInfo* info);
|
||||
|
||||
NTSTATUS
|
||||
getInterfaceInfoByIndex(HANDLE tcpFile, DWORD index, IFInfo* info);
|
||||
|
||||
NTSTATUS
|
||||
getIPAddrEntryForIf(HANDLE tcpFile, char* name, DWORD index, IFInfo* ifInfo);
|
||||
|
||||
InterfaceIndexTable*
|
||||
getInterfaceIndexTableInt(BOOL nonLoopbackOnly);
|
||||
|
||||
InterfaceIndexTable*
|
||||
getInterfaceIndexTable(void);
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,69 @@
|
||||
#ifndef _UP_H_
|
||||
#define _UP_H_
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#define UPOLL_CTL_ADD 1
|
||||
#define UPOLL_CTL_DEL 2
|
||||
#define UPOLL_CTL_MOD 3
|
||||
|
||||
#define UPOLLIN 0x01
|
||||
#define UPOLLOUT 0x02
|
||||
#define UPOLLERR 0x04
|
||||
#define UPOLLET 0x08
|
||||
|
||||
typedef struct upoll upoll_t;
|
||||
|
||||
typedef union upoll_data {
|
||||
void* ptr;
|
||||
intptr_t fd;
|
||||
uint32_t u32;
|
||||
uint64_t u64;
|
||||
} upoll_data_t;
|
||||
|
||||
typedef struct upoll_event
|
||||
{
|
||||
uint32_t events;
|
||||
upoll_data_t data;
|
||||
} upoll_event_t;
|
||||
|
||||
upoll_t*
|
||||
upoll_create(uint32_t size);
|
||||
int
|
||||
upoll_ctl(upoll_t* upq, int op, intptr_t fd, upoll_event_t* event);
|
||||
int
|
||||
upoll_wait(upoll_t* upq, upoll_event_t* events, int maxevents, int timeout);
|
||||
void
|
||||
upoll_destroy(upoll_t* upq);
|
||||
|
||||
intptr_t
|
||||
usocket(int domain, int type, int proto);
|
||||
intptr_t
|
||||
uaccept(intptr_t sock);
|
||||
|
||||
int
|
||||
ubind(intptr_t sock, const char* name, const char* serv);
|
||||
int
|
||||
ulisten(intptr_t sock, int backlog);
|
||||
int
|
||||
uconnect(intptr_t sock, const char* name, const char* serv);
|
||||
int
|
||||
uclose(intptr_t sock);
|
||||
|
||||
/* TCP sockets */
|
||||
int
|
||||
uread(intptr_t fd, char* buf, size_t len);
|
||||
int
|
||||
uwrite(intptr_t fd, const char* buf, size_t len);
|
||||
|
||||
int
|
||||
usocketpair(intptr_t socks[2], int async);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif /* _UP_H_ */
|
@ -0,0 +1,673 @@
|
||||
#ifdef _WIN32
|
||||
/* wake me up inside */
|
||||
#pragma GCC diagnostic ignored "-Wvla"
|
||||
/* emulated epoll, because the native async event system does not do
|
||||
* particularly well with notification
|
||||
*/
|
||||
#include "win32_upoll.h"
|
||||
|
||||
#define uhash_slot(K, S) (((K) ^ (K >> 8)) & (S - 1))
|
||||
|
||||
static uhash_t*
|
||||
uhash_create(uint32_t size)
|
||||
{
|
||||
unsigned int i;
|
||||
size--;
|
||||
size |= size >> 1;
|
||||
size |= size >> 2;
|
||||
size |= size >> 4;
|
||||
size |= size >> 8;
|
||||
size |= size >> 16;
|
||||
size++;
|
||||
|
||||
uhash_t* hash = (uhash_t*)calloc(1, sizeof(uhash_t) + size * sizeof(ulist_t));
|
||||
hash->count = 0;
|
||||
hash->size = size;
|
||||
hash->items = (ulist_t*)(((char*)hash) + sizeof(uhash_t));
|
||||
|
||||
for(i = 0; i < size; i++)
|
||||
{
|
||||
ulist_init(&hash->items[i]);
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
static void*
|
||||
uhash_lookup(uhash_t* hash, intptr_t key)
|
||||
{
|
||||
uint32_t slot = uhash_slot(key, hash->size);
|
||||
ulist_t* q;
|
||||
ulist_scan(q, &hash->items[slot])
|
||||
{
|
||||
uitem_t* i = ulist_data(q, uitem_t, list);
|
||||
if(i->key == key)
|
||||
return i->val;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
static void
|
||||
uhash_insert(uhash_t* hash, intptr_t key, void* val)
|
||||
{
|
||||
uint32_t slot = uhash_slot(key, hash->size);
|
||||
|
||||
uitem_t* item = (uitem_t*)calloc(1, sizeof(uitem_t));
|
||||
ulist_init(&item->list);
|
||||
item->key = key;
|
||||
item->val = val;
|
||||
|
||||
ulist_append(&hash->items[slot], &item->list);
|
||||
}
|
||||
static int
|
||||
uhash_delete(uhash_t* hash, intptr_t key)
|
||||
{
|
||||
uint32_t slot = uhash_slot(key, hash->size);
|
||||
ulist_t* q;
|
||||
ulist_scan(q, &hash->items[slot])
|
||||
{
|
||||
uitem_t* i = ulist_data(q, uitem_t, list);
|
||||
if(i->key == key)
|
||||
{
|
||||
ulist_remove(q);
|
||||
free(q);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
static int
|
||||
uhash_destroy(uhash_t* hash)
|
||||
{
|
||||
int i;
|
||||
for(i = 0; i < hash->size; i++)
|
||||
{
|
||||
while(!ulist_empty(&hash->items[i]))
|
||||
{
|
||||
ulist_t* q = ulist_next(&hash->items[i]);
|
||||
uitem_t* n = ulist_data(q, uitem_t, list);
|
||||
ulist_remove(q);
|
||||
free(n);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
upoll_t*
|
||||
upoll_create(uint32_t size)
|
||||
{
|
||||
assert(size > 0);
|
||||
upoll_t* upq = (upoll_t*)calloc(1, sizeof(upoll_t));
|
||||
|
||||
ulist_init(&upq->alive);
|
||||
|
||||
upq->table = uhash_create(size);
|
||||
return upq;
|
||||
}
|
||||
|
||||
void
|
||||
upoll_destroy(upoll_t* upq)
|
||||
{
|
||||
assert(upq != NULL);
|
||||
uhash_destroy(upq->table);
|
||||
ulist_t* q = NULL;
|
||||
unote_t* n = NULL;
|
||||
while(!ulist_empty(&upq->alive))
|
||||
{
|
||||
q = ulist_next(&upq->alive);
|
||||
n = ulist_data(n, unote_t, queue);
|
||||
ulist_remove(q);
|
||||
free(n);
|
||||
}
|
||||
free(upq);
|
||||
}
|
||||
|
||||
int
|
||||
upoll_ctl(upoll_t* upq, int op, intptr_t fd, upoll_event_t* event)
|
||||
{
|
||||
if(fd < 0)
|
||||
return -EBADF;
|
||||
|
||||
unote_t* note = NULL;
|
||||
switch(op)
|
||||
{
|
||||
case UPOLL_CTL_ADD:
|
||||
{
|
||||
note = (unote_t*)uhash_lookup(upq->table, fd);
|
||||
if(!note)
|
||||
{
|
||||
note = (unote_t*)calloc(1, sizeof(unote_t));
|
||||
note->upoll = upq;
|
||||
ulist_init(¬e->queue);
|
||||
note->event = *event;
|
||||
note->fd = fd;
|
||||
ulist_append(&upq->alive, ¬e->queue);
|
||||
uhash_insert(upq->table, fd, (void*)note);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case UPOLL_CTL_DEL:
|
||||
{
|
||||
note = (unote_t*)uhash_lookup(upq->table, fd);
|
||||
if(!note)
|
||||
return -ENOENT;
|
||||
event = ¬e->event;
|
||||
ulist_remove(¬e->queue);
|
||||
uhash_delete(upq->table, fd);
|
||||
free(note);
|
||||
break;
|
||||
}
|
||||
case UPOLL_CTL_MOD:
|
||||
{
|
||||
note = (unote_t*)uhash_lookup(upq->table, fd);
|
||||
if(!note)
|
||||
return -ENOENT;
|
||||
note->event = *event;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
return -EINVAL;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#if defined(HAVE_POLL)
|
||||
int
|
||||
upoll_wait_poll(upoll_t* upq, upoll_event_t* evs, int nev, int timeout)
|
||||
{
|
||||
/* FD_SETSIZE should be smaller than OPEN_MAX, but OPEN_MAX isn't portable */
|
||||
if(nev > FD_SETSIZE)
|
||||
nev = FD_SETSIZE;
|
||||
|
||||
unote_t* nvec[nev];
|
||||
int r, i, nfds = 0;
|
||||
uint32_t hint;
|
||||
struct pollfd pfds[nev];
|
||||
|
||||
unote_t* n = NULL;
|
||||
ulist_t* s = ulist_mark(&upq->alive);
|
||||
ulist_t* q = ulist_next(&upq->alive);
|
||||
|
||||
while(q != s && nfds < nev)
|
||||
{
|
||||
n = ulist_data(q, unote_t, queue);
|
||||
q = ulist_next(q);
|
||||
|
||||
ulist_remove(&n->queue);
|
||||
ulist_insert(&upq->alive, &n->queue);
|
||||
|
||||
nvec[nfds] = n;
|
||||
pfds[nfds].events = 0;
|
||||
pfds[nfds].fd = n->fd;
|
||||
if(n->event.events & UPOLLIN)
|
||||
{
|
||||
pfds[nfds].events |= POLLIN;
|
||||
}
|
||||
if(n->event.events & UPOLLOUT)
|
||||
{
|
||||
pfds[nfds].events |= POLLOUT;
|
||||
}
|
||||
nfds++;
|
||||
}
|
||||
|
||||
r = poll(pfds, nfds, timeout);
|
||||
if(r < 0)
|
||||
return -errno;
|
||||
|
||||
int e = 0;
|
||||
for(i = 0; i < nfds && e < nev; i++)
|
||||
{
|
||||
hint = 0;
|
||||
if(pfds[i].revents)
|
||||
{
|
||||
n = nvec[i];
|
||||
if(pfds[i].revents & POLLIN)
|
||||
hint |= UPOLLIN;
|
||||
if(pfds[i].revents & POLLOUT)
|
||||
hint |= UPOLLOUT;
|
||||
if(pfds[i].revents & (POLLERR | POLLNVAL | POLLHUP))
|
||||
hint |= (UPOLLERR | UPOLLIN);
|
||||
|
||||
if(hint & UPOLLERR)
|
||||
hint &= ~UPOLLOUT;
|
||||
|
||||
evs[e].data = n->event.data;
|
||||
evs[e].events = hint;
|
||||
++e;
|
||||
}
|
||||
}
|
||||
|
||||
return e;
|
||||
}
|
||||
#else
|
||||
int
|
||||
upoll_wait_select(upoll_t* upq, upoll_event_t* evs, int nev, int timeout)
|
||||
{
|
||||
/* ok we need to test each file descriptor to see whether it is a real file
|
||||
* or a socket. select any file handles (they are always ready)
|
||||
*/
|
||||
if(nev > FD_SETSIZE)
|
||||
nev = FD_SETSIZE;
|
||||
|
||||
unote_t* nvec[nev];
|
||||
int i, maxfd = 0, e = 0, nfds = 0;
|
||||
|
||||
fd_set pollin, pollout, pollerr;
|
||||
|
||||
FD_ZERO(&pollin);
|
||||
FD_ZERO(&pollout);
|
||||
FD_ZERO(&pollerr);
|
||||
|
||||
struct timeval tv;
|
||||
struct timeval* tvp = &tv;
|
||||
|
||||
tv.tv_usec = 0;
|
||||
if(timeout < 0)
|
||||
{
|
||||
tvp = NULL;
|
||||
}
|
||||
else if(timeout == 0)
|
||||
tv.tv_sec = 0;
|
||||
else
|
||||
{
|
||||
tv.tv_sec = (timeout / 1000);
|
||||
tv.tv_usec = (timeout % 1000) * 1000;
|
||||
}
|
||||
|
||||
unote_t* n = NULL;
|
||||
ulist_t* s = ulist_mark(&upq->alive);
|
||||
ulist_t* q = ulist_next(&upq->alive);
|
||||
|
||||
while(q != s && nfds < nev)
|
||||
{
|
||||
n = ulist_data(q, unote_t, queue);
|
||||
q = ulist_next(q);
|
||||
|
||||
ulist_remove(&n->queue);
|
||||
ulist_insert(&upq->alive, &n->queue);
|
||||
|
||||
nvec[nfds] = n;
|
||||
if(n->event.events & UPOLLIN)
|
||||
{
|
||||
FD_SET((SOCKET)n->fd, &pollin);
|
||||
}
|
||||
if(n->event.events & UPOLLOUT)
|
||||
{
|
||||
FD_SET((SOCKET)n->fd, &pollout);
|
||||
}
|
||||
FD_SET((SOCKET)n->fd, &pollerr);
|
||||
if(maxfd < n->fd)
|
||||
maxfd = n->fd;
|
||||
nfds++;
|
||||
}
|
||||
|
||||
#if defined(__WINDOWS__)
|
||||
int rc = select(0, &pollin, &pollout, &pollerr, tvp);
|
||||
if(rc == SOCKET_ERROR)
|
||||
{
|
||||
assert(WSAGetLastError() == WSAENOTSOCK);
|
||||
return -WSAGetLastError();
|
||||
}
|
||||
#else
|
||||
int rc = select(maxfd + 1, &pollin, &pollout, &pollerr, tvp);
|
||||
if(rc == -1)
|
||||
{
|
||||
assert(errno == EINTR || errno == EBADF);
|
||||
return -errno;
|
||||
}
|
||||
#endif
|
||||
e = 0;
|
||||
for(i = 0; i < nfds && e < nev; i++)
|
||||
{
|
||||
uint32_t hint = 0;
|
||||
unote_t* n = nvec[i];
|
||||
if(FD_ISSET(n->fd, &pollin))
|
||||
{
|
||||
hint |= UPOLLIN;
|
||||
}
|
||||
|
||||
if(FD_ISSET(n->fd, &pollerr))
|
||||
{
|
||||
hint |= (UPOLLERR | UPOLLIN);
|
||||
}
|
||||
else if(FD_ISSET(n->fd, &pollout))
|
||||
{
|
||||
hint |= UPOLLOUT;
|
||||
}
|
||||
|
||||
if(hint)
|
||||
{
|
||||
evs[e].data = n->event.data;
|
||||
evs[e].events = hint;
|
||||
++e;
|
||||
}
|
||||
}
|
||||
return e;
|
||||
}
|
||||
#endif
|
||||
|
||||
int
|
||||
upoll_wait(upoll_t* upq, upoll_event_t* evs, int nev, int timeout)
|
||||
{
|
||||
int r = 0;
|
||||
#if defined(HAVE_POLL)
|
||||
r = upoll_wait_poll(upq, evs, nev, timeout);
|
||||
#else
|
||||
r = upoll_wait_select(upq, evs, nev, timeout);
|
||||
#endif
|
||||
return r;
|
||||
}
|
||||
|
||||
intptr_t
|
||||
usocket(int domain, int type, int proto)
|
||||
{
|
||||
intptr_t fd = (intptr_t)socket(domain, type, proto);
|
||||
|
||||
#if defined(__WINDOWS__)
|
||||
if(fd < 0)
|
||||
return -WSAGetLastError();
|
||||
unsigned long flags = 1;
|
||||
int rc = ioctlsocket((SOCKET)fd, FIONBIO, &flags);
|
||||
if(rc < 0)
|
||||
return -WSAGetLastError();
|
||||
#else
|
||||
if(fd < 0)
|
||||
return -errno;
|
||||
int rc = fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
|
||||
if(rc < 0)
|
||||
return -errno;
|
||||
#endif
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
int
|
||||
ubind(intptr_t fd, const char* host, const char* serv)
|
||||
{
|
||||
struct addrinfo* info;
|
||||
struct addrinfo hint;
|
||||
memset(&hint, 0, sizeof(hint));
|
||||
|
||||
int optval = 0;
|
||||
unsigned int optlen = sizeof(optval);
|
||||
#if defined(__WINDOWS__)
|
||||
int rc = getsockopt((SOCKET)fd, SOL_SOCKET, SO_TYPE, (char*)&optval,
|
||||
(int*)&optlen);
|
||||
#else
|
||||
int rc = getsockopt(fd, SOL_SOCKET, SO_TYPE, &optval, &optlen);
|
||||
#endif
|
||||
|
||||
hint.ai_family = AF_INET;
|
||||
hint.ai_socktype = optval;
|
||||
|
||||
rc = getaddrinfo(host, serv, &hint, &info);
|
||||
|
||||
optval = 1;
|
||||
if(!rc)
|
||||
{
|
||||
#if defined(__WINDOWS__)
|
||||
rc = setsockopt((SOCKET)fd, SOL_SOCKET, SO_REUSEADDR, (char*)&optval,
|
||||
sizeof(optval));
|
||||
#else
|
||||
rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
|
||||
#endif
|
||||
if(!rc)
|
||||
rc = bind(fd, info->ai_addr, info->ai_addrlen);
|
||||
}
|
||||
|
||||
freeaddrinfo(info);
|
||||
if(rc)
|
||||
{
|
||||
#if defined(__WINDOWS__)
|
||||
return WSAGetLastError();
|
||||
#else
|
||||
return errno;
|
||||
#endif
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
uconnect(intptr_t fd, const char* host, const char* serv)
|
||||
{
|
||||
struct addrinfo* info;
|
||||
|
||||
struct addrinfo hint;
|
||||
memset(&hint, 0, sizeof(hint));
|
||||
|
||||
int optval = 0;
|
||||
unsigned int optlen;
|
||||
|
||||
#if defined(__WINDOWS__)
|
||||
int rc = getsockopt(fd, SOL_SOCKET, SO_TYPE, (char*)&optval, (int*)&optlen);
|
||||
#else
|
||||
int rc = getsockopt(fd, SOL_SOCKET, SO_TYPE, &optval, &optlen);
|
||||
#endif
|
||||
|
||||
hint.ai_family = AF_INET;
|
||||
hint.ai_socktype = optval;
|
||||
|
||||
rc = getaddrinfo(host, serv, &hint, &info);
|
||||
|
||||
if(!rc)
|
||||
{
|
||||
#if defined(__WINDOWS__)
|
||||
rc = connect((SOCKET)fd, info->ai_addr, info->ai_addrlen);
|
||||
#else
|
||||
rc = connect(fd, info->ai_addr, info->ai_addrlen);
|
||||
#endif
|
||||
}
|
||||
|
||||
freeaddrinfo(info);
|
||||
|
||||
if(rc)
|
||||
{
|
||||
#if defined(__WINDOWS__)
|
||||
if(WSAGetLastError() != WSAEWOULDBLOCK)
|
||||
return WSAGetLastError();
|
||||
#else
|
||||
if(errno != EINPROGRESS)
|
||||
return errno;
|
||||
#endif
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
ulisten(intptr_t sock, int backlog)
|
||||
{
|
||||
return listen(sock, backlog);
|
||||
}
|
||||
|
||||
intptr_t
|
||||
uaccept(intptr_t sock)
|
||||
{
|
||||
struct sockaddr addr;
|
||||
|
||||
addr.sa_family = AF_INET;
|
||||
socklen_t addr_len;
|
||||
|
||||
#if defined(__WINDOWS__)
|
||||
intptr_t fd = (intptr_t)accept((SOCKET)sock, &addr, &addr_len);
|
||||
if(fd == -1)
|
||||
return WSAGetLastError();
|
||||
#else
|
||||
intptr_t fd = accept(sock, &addr, &addr_len);
|
||||
if(fd < 0)
|
||||
return errno;
|
||||
#endif
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
int
|
||||
uclose(intptr_t sock)
|
||||
{
|
||||
#if defined(__WINDOWS__)
|
||||
return closesocket((SOCKET)sock);
|
||||
#else
|
||||
return close(sock);
|
||||
#endif
|
||||
}
|
||||
|
||||
int
|
||||
uread(intptr_t fd, char* buf, size_t len)
|
||||
{
|
||||
return recv(fd, buf, len, 0);
|
||||
}
|
||||
int
|
||||
uwrite(intptr_t fd, const char* buf, size_t len)
|
||||
{
|
||||
return send(fd, buf, len, 0);
|
||||
}
|
||||
|
||||
/* adapted from (renamed make_overlapped to async for allergy reasons): */
|
||||
/* socketpair.c
|
||||
Copyright 2007, 2010 by Nathan C. Myers <ncm@cantrip.org>
|
||||
Redistribution and use in source and binary forms, with or without modification,
|
||||
are permitted provided that the following conditions are met:
|
||||
|
||||
Redistributions of source code must retain the above copyright notice, this
|
||||
list of conditions and the following disclaimer.
|
||||
|
||||
Redistributions in binary form must reproduce the above copyright notice,
|
||||
this list of conditions and the following disclaimer in the documentation
|
||||
and/or other materials provided with the distribution.
|
||||
|
||||
The name of the author must not be used to endorse or promote products
|
||||
derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* Changes:
|
||||
* 2014-02-12: merge David Woodhouse, Ger Hobbelt improvements
|
||||
* git.infradead.org/users/dwmw2/openconnect.git/commitdiff/bdeefa54
|
||||
* github.com/GerHobbelt/selectable-socketpair
|
||||
* always init the socks[] to -1/INVALID_SOCKET on error, both on Win32/64
|
||||
* and UNIX/other platforms
|
||||
* 2013-07-18: Change to BSD 3-clause license
|
||||
* 2010-03-31:
|
||||
* set addr to 127.0.0.1 because win32 getsockname does not always set it.
|
||||
* 2010-02-25:
|
||||
* set SO_REUSEADDR option to avoid leaking some windows resource.
|
||||
* Windows System Error 10049, "Event ID 4226 TCP/IP has reached
|
||||
* the security limit imposed on the number of concurrent TCP connect
|
||||
* attempts." Bleah.
|
||||
* 2007-04-25:
|
||||
* preserve value of WSAGetLastError() on all error returns.
|
||||
* 2007-04-22: (Thanks to Matthew Gregan <kinetik@flim.org>)
|
||||
* s/EINVAL/WSAEINVAL/ fix trivial compile failure
|
||||
* s/socket/WSASocket/ enable creation of sockets suitable as stdin/stdout
|
||||
* of a child process.
|
||||
* add argument make_overlapped
|
||||
*/
|
||||
|
||||
#if defined(__WINDOWS__)
|
||||
int
|
||||
usocketpair(intptr_t socks[2], int async)
|
||||
{
|
||||
union {
|
||||
struct sockaddr_in inaddr;
|
||||
struct sockaddr addr;
|
||||
} a;
|
||||
SOCKET listener;
|
||||
int e;
|
||||
socklen_t addrlen = sizeof(a.inaddr);
|
||||
DWORD flags = (async ? WSA_FLAG_OVERLAPPED : 0);
|
||||
int reuse = 1;
|
||||
|
||||
if(socks == 0)
|
||||
{
|
||||
WSASetLastError(WSAEINVAL);
|
||||
return SOCKET_ERROR;
|
||||
}
|
||||
socks[0] = socks[1] = -1;
|
||||
|
||||
listener = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if(listener == INVALID_SOCKET)
|
||||
return SOCKET_ERROR;
|
||||
|
||||
memset(&a, 0, sizeof(a));
|
||||
a.inaddr.sin_family = AF_INET;
|
||||
a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
a.inaddr.sin_port = 0;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
if(setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, (char*)&reuse,
|
||||
(socklen_t)sizeof(reuse))
|
||||
== -1)
|
||||
break;
|
||||
|
||||
if(bind(listener, &a.addr, sizeof(a.inaddr)) == SOCKET_ERROR)
|
||||
break;
|
||||
|
||||
memset(&a, 0, sizeof(a));
|
||||
if(getsockname(listener, &a.addr, &addrlen) == SOCKET_ERROR)
|
||||
break;
|
||||
// win32 getsockname may only set the port number, p=0.0005.
|
||||
// ( http://msdn.microsoft.com/library/ms738543.aspx ):
|
||||
a.inaddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
||||
a.inaddr.sin_family = AF_INET;
|
||||
|
||||
if(listen(listener, 1) == SOCKET_ERROR)
|
||||
break;
|
||||
|
||||
socks[0] = (intptr_t)WSASocket(AF_INET, SOCK_STREAM, 0, NULL, 0, flags);
|
||||
if(socks[0] == -1)
|
||||
break;
|
||||
if(connect((SOCKET)socks[0], &a.addr, sizeof(a.inaddr)) == SOCKET_ERROR)
|
||||
break;
|
||||
|
||||
socks[1] = (intptr_t)accept(listener, NULL, NULL);
|
||||
if(socks[1] == -1)
|
||||
break;
|
||||
|
||||
closesocket(listener);
|
||||
return 0;
|
||||
}
|
||||
|
||||
e = WSAGetLastError();
|
||||
closesocket(listener);
|
||||
closesocket((SOCKET)socks[0]);
|
||||
closesocket((SOCKET)socks[1]);
|
||||
WSASetLastError(e);
|
||||
socks[0] = socks[1] = -1;
|
||||
return SOCKET_ERROR;
|
||||
}
|
||||
#else
|
||||
int
|
||||
usocketpair(intptr_t socks[2], int dummy)
|
||||
{
|
||||
int sovec[2];
|
||||
if(socks == 0)
|
||||
{
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
dummy = socketpair(AF_LOCAL, SOCK_STREAM, 0, sovec);
|
||||
if(dummy)
|
||||
{
|
||||
socks[0] = socks[1] = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
socks[0] = sovec[0];
|
||||
socks[1] = sovec[1];
|
||||
}
|
||||
return dummy;
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,144 @@
|
||||
#ifndef _UPOLL_H_
|
||||
#define _UPOLL_H_
|
||||
|
||||
#include "win32_up.h"
|
||||
|
||||
#if(defined(__64BIT__) || defined(__x86_64__))
|
||||
#define __IS_64BIT__
|
||||
#else
|
||||
#define __IS_32BIT__
|
||||
#endif
|
||||
|
||||
#if(defined WIN32 || defined _WIN32)
|
||||
#undef __WINDOWS__
|
||||
#define __WINDOWS__
|
||||
#undef _WIN32_WINNT
|
||||
#define _WIN32_WINNT 0x0500
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#if defined(__WINDOWS__)
|
||||
#include <io.h>
|
||||
#include <winsock2.h>
|
||||
#include <ws2tcpip.h>
|
||||
#include <wspiapi.h>
|
||||
#else
|
||||
#include <unistd.h>
|
||||
#include <stdint.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <arpa/inet.h>
|
||||
#endif
|
||||
|
||||
#if defined(__linux__)
|
||||
#undef HAVE_EPOLL
|
||||
#define HAVE_EPOLL 1
|
||||
#elif defined(__unix__) || (defined(__APPLE__) && defined(__MACH__))
|
||||
#undef HAVE_POLL
|
||||
#define HAVE_POLL 1
|
||||
#else
|
||||
#undef HAVE_SELECT
|
||||
#define HAVE_SELECT 1
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_EPOLL)
|
||||
#include <sys/epoll.h>
|
||||
#elif defined(HAVE_POLL)
|
||||
#include <poll.h>
|
||||
#endif
|
||||
|
||||
typedef struct unote unote_t;
|
||||
typedef struct ulist ulist_t;
|
||||
typedef struct uitem uitem_t;
|
||||
typedef struct uhash uhash_t;
|
||||
|
||||
struct ulist
|
||||
{
|
||||
ulist_t* next;
|
||||
ulist_t* prev;
|
||||
};
|
||||
|
||||
struct uitem
|
||||
{
|
||||
ulist_t list;
|
||||
intptr_t key;
|
||||
void* val;
|
||||
};
|
||||
|
||||
struct uhash
|
||||
{
|
||||
uint16_t count;
|
||||
uint16_t size;
|
||||
ulist_t* items;
|
||||
};
|
||||
|
||||
struct upoll
|
||||
{
|
||||
int fd; /* backend fd (epoll, kqueue) */
|
||||
ulist_t alive; /* all notes this queue knows about */
|
||||
uhash_t* table;
|
||||
};
|
||||
|
||||
struct unote
|
||||
{
|
||||
upoll_event_t event;
|
||||
intptr_t fd;
|
||||
ulist_t queue; /* handle for the queue's notes */
|
||||
upoll_t* upoll;
|
||||
};
|
||||
|
||||
#define container_of(ptr, type, member) \
|
||||
((type*)((char*)(ptr)-offsetof(type, member)))
|
||||
|
||||
#define ulist_init(q) \
|
||||
(q)->prev = q; \
|
||||
(q)->next = q
|
||||
|
||||
#define ulist_head(h) (h)->next
|
||||
#define ulist_next(q) (q)->next
|
||||
|
||||
#define ulist_tail(h) (h)->prev
|
||||
#define ulist_prev(q) (q)->prev
|
||||
|
||||
#define ulist_empty(h) (h == (h)->prev)
|
||||
|
||||
#define ulist_append(h, x) \
|
||||
(x)->prev = (h)->prev; \
|
||||
(x)->prev->next = x; \
|
||||
(x)->next = h; \
|
||||
(h)->prev = x
|
||||
|
||||
#define ulist_insert(h, x) \
|
||||
(x)->next = (h)->next; \
|
||||
(x)->next->prev = x; \
|
||||
(x)->prev = h; \
|
||||
(h)->next = x
|
||||
|
||||
#define ulist_remove(x) \
|
||||
(x)->next->prev = (x)->prev; \
|
||||
(x)->prev->next = (x)->next; \
|
||||
(x)->prev = x; \
|
||||
(x)->next = x
|
||||
|
||||
#define ulist_mark(h) (h)
|
||||
|
||||
#define ulist_scan(q, h) \
|
||||
for((q) = ulist_head(h); (q) != ulist_mark(h); (q) = ulist_next(q))
|
||||
|
||||
#define ulist_data(q, type, link) container_of(q, type, link)
|
||||
|
||||
#endif /* _UPOLL_H_ */
|
@ -1,8 +1,33 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
int
|
||||
startWinsock()
|
||||
{
|
||||
WSADATA wsockd;
|
||||
int err;
|
||||
err = ::WSAStartup(MAKEWORD(2, 2), &wsockd);
|
||||
if(err)
|
||||
{
|
||||
perror("Failed to start Windows Sockets");
|
||||
return err;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
int
|
||||
main(int argc, char** argv)
|
||||
{
|
||||
#ifdef _WIN32
|
||||
if(startWinsock())
|
||||
return -1;
|
||||
#endif
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
return RUN_ALL_TESTS();
|
||||
int r = RUN_ALL_TESTS();
|
||||
#ifdef _WIN32
|
||||
WSACleanup();
|
||||
#endif
|
||||
return r;
|
||||
}
|
Loading…
Reference in New Issue