ReactOS 0.4.17-dev-470-gf9e3448
addrconv.c File Reference
#include <ws2_32.h>
#include <debug.h>
Include dependency graph for addrconv.c:

Go to the source code of this file.

Macros

#define NDEBUG
 
#define DN2H(dw)
 
#define DH2N(dw)
 
#define WN2H(w)
 
#define WH2N(w)
 

Functions

CHAR FAR *WSAAPI inet_ntoa (IN IN_ADDR in)
 
ULONG WSAAPI htonl (IN ULONG hostlong)
 
USHORT WSAAPI htons (IN USHORT hostshort)
 
ULONG WSAAPI ntohl (IN ULONG netlong)
 
USHORT WSAAPI ntohs (IN USHORT netshort)
 
INT WSAAPI WSAHtonl (IN SOCKET s, IN ULONG hostlong, OUT ULONG FAR *lpnetlong)
 
INT WSAAPI WSAHtons (IN SOCKET s, IN USHORT hostshort, OUT USHORT FAR *lpnetshort)
 
INT WSAAPI WSANtohl (IN SOCKET s, IN ULONG netlong, OUT ULONG FAR *lphostlong)
 
INT WSAAPI WSANtohs (IN SOCKET s, IN USHORT netshort, OUT USHORT FAR *lphostshort)
 
PCSTR WSAAPI inet_ntop (_In_ INT Family, _In_ const VOID *pAddr, _Out_writes_(StringBufSize) PSTR pStringBuf, _In_ size_t StringBufSize)
 
PCWSTR WSAAPI InetNtopW (_In_ INT Family, _In_ const VOID *pAddr, _Out_writes_(StringBufSize) PWSTR pStringBuf, _In_ size_t StringBufSize)
 
INT WSAAPI inet_pton (_In_ INT Family, _In_ PCSTR pszAddrString, _Out_writes_bytes_(sizeof(IN_ADDR6)) PVOID pAddrBuf)
 
INT WSAAPI InetPtonW (_In_ INT Family, _In_ PCWSTR pszAddrString, _Out_writes_bytes_(sizeof(IN_ADDR6)) PVOID pAddrBuf)
 

Macro Definition Documentation

◆ DH2N

#define DH2N (   dw)
Value:
((((dw) & 0xFF000000L) >> 24) | \
(((dw) & 0x00FF0000L) >> 8) | \
(((dw) & 0x0000FF00L) << 8) | \
(((dw) & 0x000000FFL) << 24))
REFIID LPVOID DWORD_PTR dw
Definition: atlbase.h:40

Definition at line 28 of file addrconv.c.

◆ DN2H

#define DN2H (   dw)
Value:
((((dw) & 0xFF000000L) >> 24) | \
(((dw) & 0x00FF0000L) >> 8) | \
(((dw) & 0x0000FF00L) << 8) | \
(((dw) & 0x000000FFL) << 24))

Definition at line 21 of file addrconv.c.

◆ NDEBUG

#define NDEBUG

Definition at line 13 of file addrconv.c.

◆ WH2N

#define WH2N (   w)
Value:
((((w) & 0xFF00) >> 8) | \
(((w) & 0x00FF) << 8))
GLubyte GLubyte GLubyte GLubyte w
Definition: glext.h:6102

Definition at line 40 of file addrconv.c.

◆ WN2H

#define WN2H (   w)
Value:
((((w) & 0xFF00) >> 8) | \
(((w) & 0x00FF) << 8))

Definition at line 35 of file addrconv.c.

Function Documentation

◆ htonl()

ULONG WSAAPI htonl ( IN ULONG  hostlong)

Definition at line 139 of file addrconv.c.

140{
141 return DH2N(hostlong);
142}
#define DH2N(dw)
Definition: addrconv.c:28

◆ htons()

USHORT WSAAPI htons ( IN USHORT  hostshort)

Definition at line 149 of file addrconv.c.

150{
151 return WH2N(hostshort);
152}
#define WH2N(w)
Definition: addrconv.c:40

◆ inet_ntoa()

CHAR FAR *WSAAPI inet_ntoa ( IN IN_ADDR  in)

Definition at line 71 of file addrconv.c.

72{
76 WSADATA WsaData;
77 BOOL ManualLoad = FALSE;
78 CHAR b[10];
79 PCHAR p;
80 DPRINT("inet_ntoa: %lx\n", in);
81
82 /* Enter prolog */
84 {
85 DPRINT("MANUAL LOAD\n");
86
87 /* Only fail if the error wasn't related to a missing WSAStartup */
89 {
90 /* Fail */
92 return NULL;
93 }
94
95 /* Apps aren't expected to call WSAStartup for this API, so we will */
96 if ((ErrorCode = WSAStartup(MAKEWORD(2,2), &WsaData)) != ERROR_SUCCESS)
97 {
98 /* We failed */
100 return NULL;
101 }
102
103 /* Try the prolog again */
104 ManualLoad = TRUE;
106 {
107 /* Failed again... */
108 WSACleanup();
110 return NULL;
111 }
112 }
113
114 p = Thread->Buffer;
115 _itoa(in.S_un.S_addr & 0xFF, b, 10);
116 strcpy(p, b);
117 _itoa((in.S_un.S_addr >> 8) & 0xFF, b, 10);
118 strcat(p, ".");
119 strcat(p, b);
120 _itoa((in.S_un.S_addr >> 16) & 0xFF, b, 10);
121 strcat(p, ".");
122 strcat(p, b);
123 _itoa((in.S_un.S_addr >> 24) & 0xFF, b, 10);
124 strcat(p, ".");
125 strcat(p, b);
126
127 /* Cleanup the manual load */
128 if (ManualLoad) WSACleanup();
129
130 /* Return the buffer */
131 return p;
132}
#define ERROR_SUCCESS
Definition: deptool.c:10
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define SetLastError(x)
Definition: compat.h:752
unsigned int BOOL
Definition: ntddk_ex.h:94
_In_opt_ PFILE_OBJECT _In_opt_ PETHREAD Thread
Definition: fltkernel.h:2653
_Must_inspect_result_ _In_ PLARGE_INTEGER _In_ PLARGE_INTEGER _In_ ULONG _In_ PFILE_OBJECT _In_ PVOID Process
Definition: fsrtlfuncs.h:223
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
GLuint in
Definition: glext.h:9616
GLfloat GLfloat p
Definition: glext.h:8902
_In_ NDIS_ERROR_CODE ErrorCode
Definition: ndis.h:4436
char CHAR
Definition: pedump.c:57
_itoa
Definition: stdlib.h:642
strcat
Definition: string.h:92
strcpy
Definition: string.h:131
#define DPRINT
Definition: sndvol32.h:73
#define MAKEWORD(a, b)
Definition: typedefs.h:248
int32_t INT
Definition: typedefs.h:58
char * PCHAR
Definition: typedefs.h:51
INT WINAPI WSAStartup(IN WORD wVersionRequested, OUT LPWSADATA lpWSAData)
Definition: startup.c:113
#define WSANOTINITIALISED
Definition: winerror.h:2894
int PASCAL FAR WSACleanup(void)
Definition: startup.c:60
INT WSAAPI WsApiProlog(OUT PWSPROCESS *Process, OUT PWSTHREAD *Thread)
Definition: wsautil.c:91

Referenced by AcceptConnections(), AddrToAddrStr(), addserver(), adns__vdiag(), adns_processreadable(), ccf_nameserver(), CreatePersistentIpForwardEntry(), cs_inaddr(), csp_addr(), DeleteActiveRoutes(), DeleteCustomRoutes(), DisplayArpEntries(), get_server(), GetHostByName(), hookup(), inet_ntop(), main(), MyInetAddr(), NetBTSendNameQuery(), OpenControlConnection(), piaddr(), pretty_print_option(), print_getaddrinfo(), PrintActiveRoutes(), PrintEntries(), processRequest(), send_decline(), send_discover(), send_release(), send_request(), simple_mixed_client(), START_TEST(), test_gethostbyname(), toIPAddressString(), write_packet(), WspiapiLegacyGetAddrInfo(), and WspiapiLegacyGetNameInfo().

◆ inet_ntop()

PCSTR WSAAPI inet_ntop ( _In_ INT  Family,
_In_ const VOID pAddr,
_Out_writes_(StringBufSize) PSTR  pStringBuf,
_In_ size_t  StringBufSize 
)

Definition at line 408 of file addrconv.c.

413{
415 ULONG BufSize = StringBufSize;
416
417 switch (Family)
418 {
419 case AF_INET:
420 Status = RtlIpv4AddressToStringExA(pAddr, 0, pStringBuf, &BufSize);
421 break;
422 case AF_INET6:
423 Status = RtlIpv6AddressToStringExA(pAddr, 0, 0, pStringBuf, &BufSize);
424 break;
425 default:
427 return NULL;
428 }
429
430 if (!NT_SUCCESS(Status))
431 {
433 return NULL;
434 }
435
436 return pStringBuf;
437}
#define BufSize
Definition: FsRtlTunnel.c:28
LONG NTSTATUS
Definition: precomp.h:26
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define AF_INET
Definition: tcpip.h:117
PWDF_CHILD_ADDRESS_DESCRIPTION_HEADER pAddr
Status
Definition: gdiplustypes.h:24
NTSYSAPI NTSTATUS NTAPI RtlIpv4AddressToStringExA(_In_ const struct in_addr *Address, _In_ USHORT Port, _Out_writes_to_(*AddressStringLength, *AddressStringLength) PCHAR AddressString, _Inout_ PULONG AddressStringLength)
Definition: network.c:186
NTSYSAPI NTSTATUS NTAPI RtlIpv6AddressToStringExA(_In_ const struct in6_addr *Address, _In_ ULONG ScopeId, _In_ USHORT Port, _Out_writes_to_(*AddressStringLength, *AddressStringLength) PSTR AddressString, _Inout_ PULONG AddressStringLength)
Definition: network.c:510
static int Family
Definition: ping.c:62
uint32_t ULONG
Definition: typedefs.h:59
#define WSAEINVAL
Definition: winerror.h:2845
#define WSAEAFNOSUPPORT
Definition: winerror.h:2863
#define AF_INET6
Definition: winsock.h:363

◆ inet_pton()

INT WSAAPI inet_pton ( _In_ INT  Family,
_In_ PCSTR  pszAddrString,
_Out_writes_bytes_(sizeof(IN_ADDR6)) PVOID  pAddrBuf 
)

Definition at line 474 of file addrconv.c.

478{
480 PCSTR ch;
481
482 if (!pszAddrString || !pAddrBuf)
483 {
485 return -1;
486 }
487
488 switch (Family)
489 {
490 case AF_INET:
491 Status = RtlIpv4StringToAddressA(pszAddrString, TRUE, &ch, pAddrBuf);
492 break;
493 case AF_INET6:
494 Status = RtlIpv6StringToAddressA(pszAddrString, &ch, pAddrBuf);
495 break;
496 default:
498 return -1;
499 }
500
501 if (!NT_SUCCESS(Status) || (*ch != 0))
502 {
503 return 0;
504 }
505
506 return 1;
507}
unsigned char ch[4][2]
Definition: console.c:118
NTSYSAPI NTSTATUS NTAPI RtlIpv4StringToAddressA(_In_ PCSTR String, _In_ BOOLEAN Strict, _Out_ PCSTR *Terminator, _Out_ struct in_addr *Addr)
Definition: network.c:318
NTSYSAPI NTSTATUS NTAPI RtlIpv6StringToAddressA(_In_ PCSTR String, _Out_ PCSTR *Terminator, _Out_ struct in6_addr *Addr)
Definition: network.c:725
const char * PCSTR
Definition: typedefs.h:52
#define WSAEFAULT
Definition: winerror.h:2844

Referenced by __rpc_uaddr2taddr_af().

◆ InetNtopW()

PCWSTR WSAAPI InetNtopW ( _In_ INT  Family,
_In_ const VOID pAddr,
_Out_writes_(StringBufSize) PWSTR  pStringBuf,
_In_ size_t  StringBufSize 
)

Definition at line 441 of file addrconv.c.

446{
448 ULONG BufSize = StringBufSize;
449
450 switch (Family)
451 {
452 case AF_INET:
453 Status = RtlIpv4AddressToStringExW(pAddr, 0, pStringBuf, &BufSize);
454 break;
455 case AF_INET6:
456 Status = RtlIpv6AddressToStringExW(pAddr, 0, 0, pStringBuf, &BufSize);
457 break;
458 default:
460 return NULL;
461 }
462
463 if (!NT_SUCCESS(Status))
464 {
466 return NULL;
467 }
468
469 return pStringBuf;
470}
NTSYSAPI NTSTATUS NTAPI RtlIpv6AddressToStringExW(_In_ const struct in6_addr *Address, _In_ ULONG ScopeId, _In_ USHORT Port, _Out_writes_to_(*AddressStringLength, *AddressStringLength) PWCHAR AddressString, _Inout_ PULONG AddressStringLength)
Definition: network.c:649
NTSTATUS NTAPI RtlIpv4AddressToStringExW(_In_ const struct in_addr *Address, _In_ USHORT Port, _Out_writes_to_(*AddressStringLength, *AddressStringLength) PWCHAR AddressString, _Inout_ PULONG AddressStringLength)
Definition: network.c:268

◆ InetPtonW()

INT WSAAPI InetPtonW ( _In_ INT  Family,
_In_ PCWSTR  pszAddrString,
_Out_writes_bytes_(sizeof(IN_ADDR6)) PVOID  pAddrBuf 
)

Definition at line 511 of file addrconv.c.

515{
517 PCWSTR ch;
518
519 if (!pszAddrString || !pAddrBuf)
520 {
522 return -1;
523 }
524
525 switch (Family)
526 {
527 case AF_INET:
528 Status = RtlIpv4StringToAddressW(pszAddrString, TRUE, &ch, pAddrBuf);
529 break;
530 case AF_INET6:
531 Status = RtlIpv6StringToAddressW(pszAddrString, &ch, pAddrBuf);
532 break;
533 default:
535 return -1;
536 }
537
538 if (!NT_SUCCESS(Status) || (*ch != 0))
539 {
540 SetLastError(WSAEINVAL); /* Only unicode version sets this error */
541 return 0;
542 }
543
544 return 1;
545}
NTSYSAPI NTSTATUS NTAPI RtlIpv6StringToAddressW(_In_ PCWSTR String, _Out_ PCWSTR *Terminator, _Out_ struct in6_addr *Addr)
Definition: network.c:1005
NTSYSAPI NTSTATUS NTAPI RtlIpv4StringToAddressW(_In_ PCWSTR String, _In_ BOOLEAN Strict, _Out_ PCWSTR *Terminator, _Out_ struct in_addr *Addr)
Definition: network.c:385
const uint16_t * PCWSTR
Definition: typedefs.h:57

◆ ntohl()

ULONG WSAAPI ntohl ( IN ULONG  netlong)

Definition at line 159 of file addrconv.c.

160{
161 return DN2H(netlong);
162}
#define DN2H(dw)
Definition: addrconv.c:21

◆ ntohs()

USHORT WSAAPI ntohs ( IN USHORT  netshort)

Definition at line 169 of file addrconv.c.

170{
171 return WN2H(netshort);
172}
#define WN2H(w)
Definition: addrconv.c:35

◆ WSAHtonl()

INT WSAAPI WSAHtonl ( IN SOCKET  s,
IN ULONG  hostlong,
OUT ULONG FAR lpnetlong 
)

Definition at line 179 of file addrconv.c.

182{
184 PWSSOCKET Socket;
185 DPRINT("WSAHtonl: %p, %lx, %p\n", s, hostlong, lpnetlong);
186
187 /* Check for WSAStartup */
189 {
190 /* Make sure we got a parameter */
191 if (!lpnetlong)
192 {
193 /* Fail */
195 return SOCKET_ERROR;
196 }
197
198 /* Get the Socket Context */
199 if ((Socket = WsSockGetSocket(s)))
200 {
201 /* Check which byte order to use */
204 {
205 /* No conversion needed */
206 *lpnetlong = hostlong;
207 }
208 else
209 {
210 /* Use a swap */
211 *lpnetlong = DN2H(hostlong);
212 }
213
214 /* Dereference the socket */
215 WsSockDereference(Socket);
216
217 /* Return success */
218 return ERROR_SUCCESS;
219 }
220 else
221 {
222 /* Set the error code */
224 }
225 }
226
227 /* Return with error */
229 return SOCKET_ERROR;
230}
GLdouble s
Definition: gl.h:2039
WSAPROTOCOL_INFOW ProtocolInfo
Definition: ws2_32p.h:91
PTCATALOG_ENTRY CatalogEntry
Definition: ws2_32p.h:200
#define WSAENOTSOCK
Definition: winerror.h:2854
#define LITTLEENDIAN
Definition: winsock2.h:452
#define SOCKET_ERROR
Definition: winsock.h:327
FORCEINLINE DWORD WsQuickProlog(VOID)
Definition: ws2_32p.h:892
VOID WSAAPI WsSockDereference(IN PWSSOCKET Socket)
Definition: dsocket.c:205
PWSSOCKET WSAAPI WsSockGetSocket(IN SOCKET Handle)
Definition: dsocket.c:140

◆ WSAHtons()

INT WSAAPI WSAHtons ( IN SOCKET  s,
IN USHORT  hostshort,
OUT USHORT FAR lpnetshort 
)

Definition at line 237 of file addrconv.c.

240{
242 PWSSOCKET Socket;
243 DPRINT("WSAHtons: %p, %lx, %p\n", s, hostshort, lpnetshort);
244
245 /* Check for WSAStartup */
247 {
248 /* Make sure we got a parameter */
249 if (!lpnetshort)
250 {
251 /* Fail */
253 return SOCKET_ERROR;
254 }
255
256 /* Get the Socket Context */
257 if ((Socket = WsSockGetSocket(s)))
258 {
259 /* Check which byte order to use */
262 {
263 /* No conversion needed */
264 *lpnetshort = hostshort;
265 }
266 else
267 {
268 /* Use a swap */
269 *lpnetshort = WN2H(hostshort);
270 }
271
272 /* Dereference the socket */
273 WsSockDereference(Socket);
274
275 /* Return success */
276 return ERROR_SUCCESS;
277 }
278 else
279 {
280 /* Set the error code */
282 }
283 }
284
285 /* Return with error */
287 return SOCKET_ERROR;
288}

◆ WSANtohl()

INT WSAAPI WSANtohl ( IN SOCKET  s,
IN ULONG  netlong,
OUT ULONG FAR lphostlong 
)

Definition at line 295 of file addrconv.c.

298{
300 PWSSOCKET Socket;
301 DPRINT("WSANtohl: %p, %lx, %p\n", s, netlong, lphostlong);
302
303 /* Check for WSAStartup */
305 {
306 /* Make sure we got a parameter */
307 if (!lphostlong)
308 {
309 /* Fail */
311 return SOCKET_ERROR;
312 }
313
314 /* Get the Socket Context */
315 if ((Socket = WsSockGetSocket(s)))
316 {
317 /* Check which byte order to use */
320 {
321 /* No conversion needed */
322 *lphostlong = netlong;
323 }
324 else
325 {
326 /* Use a swap */
327 *lphostlong = DN2H(netlong);
328 }
329
330 /* Dereference the socket */
331 WsSockDereference(Socket);
332
333 /* Return success */
334 return ERROR_SUCCESS;
335 }
336 else
337 {
338 /* Set the error code */
340 }
341 }
342
343 /* Return with error */
345 return SOCKET_ERROR;
346}

◆ WSANtohs()

INT WSAAPI WSANtohs ( IN SOCKET  s,
IN USHORT  netshort,
OUT USHORT FAR lphostshort 
)

Definition at line 353 of file addrconv.c.

356{
358 PWSSOCKET Socket;
359 DPRINT("WSANtohs: %p, %lx, %p\n", s, netshort, lphostshort);
360
361 /* Check for WSAStartup */
363 {
364 /* Make sure we got a parameter */
365 if (!lphostshort)
366 {
367 /* Fail */
369 return SOCKET_ERROR;
370 }
371
372 /* Get the Socket Context */
373 if ((Socket = WsSockGetSocket(s)))
374 {
375 /* Check which byte order to use */
378 {
379 /* No conversion needed */
380 *lphostshort = netshort;
381 }
382 else
383 {
384 /* Use a swap */
385 *lphostshort = WN2H(netshort);
386 }
387
388 /* Dereference the socket */
389 WsSockDereference(Socket);
390
391 /* Return success */
392 return ERROR_SUCCESS;
393 }
394 else
395 {
396 /* Set the error code */
398 }
399 }
400
401 /* Return with error */
403 return SOCKET_ERROR;
404}