Skip to content

Commit 454de56

Browse files
peteroochHBelusca
authored andcommitted
[WS2_32] Add inet_pton, inet_ntop
1 parent 56229b7 commit 454de56

File tree

3 files changed

+146
-2
lines changed

3 files changed

+146
-2
lines changed

dll/win32/ws2_32/src/addrconv.c

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,3 +492,143 @@ WSANtohs(IN SOCKET s,
492492
return SOCKET_ERROR;
493493
}
494494

495+
PCSTR
496+
WSAAPI
497+
inet_ntop(
498+
_In_ INT Family,
499+
_In_ const VOID *pAddr,
500+
_Out_writes_(StringBufSize) PSTR pStringBuf,
501+
_In_ size_t StringBufSize)
502+
{
503+
NTSTATUS Status;
504+
ULONG BufSize = StringBufSize;
505+
506+
switch (Family)
507+
{
508+
case AF_INET:
509+
Status = RtlIpv4AddressToStringExA(pAddr, 0, pStringBuf, &BufSize);
510+
break;
511+
case AF_INET6:
512+
Status = RtlIpv6AddressToStringExA(pAddr, 0, 0, pStringBuf, &BufSize);
513+
break;
514+
default:
515+
SetLastError(WSAEAFNOSUPPORT);
516+
return NULL;
517+
}
518+
519+
if (!NT_SUCCESS(Status))
520+
{
521+
SetLastError(WSAEINVAL);
522+
return NULL;
523+
}
524+
525+
return pStringBuf;
526+
}
527+
528+
PCWSTR
529+
WSAAPI
530+
InetNtopW(
531+
_In_ INT Family,
532+
_In_ const VOID *pAddr,
533+
_Out_writes_(StringBufSize) PWSTR pStringBuf,
534+
_In_ size_t StringBufSize)
535+
{
536+
NTSTATUS Status;
537+
ULONG BufSize = StringBufSize;
538+
539+
switch (Family)
540+
{
541+
case AF_INET:
542+
Status = RtlIpv4AddressToStringExW(pAddr, 0, pStringBuf, &BufSize);
543+
break;
544+
case AF_INET6:
545+
Status = RtlIpv6AddressToStringExW(pAddr, 0, 0, pStringBuf, &BufSize);
546+
break;
547+
default:
548+
SetLastError(WSAEAFNOSUPPORT);
549+
return NULL;
550+
}
551+
552+
if (!NT_SUCCESS(Status))
553+
{
554+
SetLastError(WSAEINVAL);
555+
return NULL;
556+
}
557+
558+
return pStringBuf;
559+
}
560+
561+
INT
562+
WSAAPI
563+
inet_pton(
564+
_In_ INT Family,
565+
_In_ PCSTR pszAddrString,
566+
_Out_writes_bytes_(sizeof(IN_ADDR6)) PVOID pAddrBuf)
567+
{
568+
NTSTATUS Status;
569+
PCSTR ch;
570+
571+
if (!pszAddrString || !pAddrBuf)
572+
{
573+
SetLastError(WSAEFAULT);
574+
return -1;
575+
}
576+
577+
switch (Family)
578+
{
579+
case AF_INET:
580+
Status = RtlIpv4StringToAddressA(pszAddrString, TRUE, &ch, pAddrBuf);
581+
break;
582+
case AF_INET6:
583+
Status = RtlIpv6StringToAddressA(pszAddrString, &ch, pAddrBuf);
584+
break;
585+
default:
586+
SetLastError(WSAEAFNOSUPPORT);
587+
return -1;
588+
}
589+
590+
if (!NT_SUCCESS(Status) || (*ch != 0))
591+
{
592+
return 0;
593+
}
594+
595+
return 1;
596+
}
597+
598+
INT
599+
WSAAPI
600+
InetPtonW(
601+
_In_ INT Family,
602+
_In_ PCWSTR pszAddrString,
603+
_Out_writes_bytes_(sizeof(IN_ADDR6)) PVOID pAddrBuf)
604+
{
605+
NTSTATUS Status;
606+
PCWSTR ch;
607+
608+
if (!pszAddrString || !pAddrBuf)
609+
{
610+
SetLastError(WSAEFAULT);
611+
return -1;
612+
}
613+
614+
switch (Family)
615+
{
616+
case AF_INET:
617+
Status = RtlIpv4StringToAddressW(pszAddrString, TRUE, &ch, pAddrBuf);
618+
break;
619+
case AF_INET6:
620+
Status = RtlIpv6StringToAddressW(pszAddrString, &ch, pAddrBuf);
621+
break;
622+
default:
623+
SetLastError(WSAEAFNOSUPPORT);
624+
return -1;
625+
}
626+
627+
if (!NT_SUCCESS(Status) || (*ch != 0))
628+
{
629+
SetLastError(WSAEINVAL); /* Only unicode version sets this error */
630+
return 0;
631+
}
632+
633+
return 1;
634+
}

dll/win32/ws2_32/ws2_32.spec

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,7 @@
115115
23 stdcall socket(long long long)
116116
@ stdcall GetAddrInfoW(wstr wstr ptr ptr)
117117
@ stdcall GetNameInfoW(ptr long wstr long wstr long long)
118+
@ stdcall -version=0x600+ inet_ntop(long ptr ptr long)
119+
@ stdcall -version=0x600+ InetNtopW(long ptr ptr long)
120+
@ stdcall -version=0x600+ inet_pton(long str ptr)
121+
@ stdcall -version=0x600+ InetPtonW(long wstr ptr)

sdk/include/psdk/ws2tcpip.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,15 +451,15 @@ PCSTR
451451
WSAAPI
452452
inet_ntop(
453453
_In_ INT Family,
454-
_In_ PVOID pAddr,
454+
_In_ const VOID *pAddr,
455455
_Out_writes_(StringBufSize) PSTR pStringBuf,
456456
_In_ size_t StringBufSize);
457457

458458
PCWSTR
459459
WSAAPI
460460
InetNtopW(
461461
_In_ INT Family,
462-
_In_ PVOID pAddr,
462+
_In_ const VOID *pAddr,
463463
_Out_writes_(StringBufSize) PWSTR pStringBuf,
464464
_In_ size_t StringBufSize);
465465

0 commit comments

Comments
 (0)