2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS WinSock 2 API
4 * FILE: dll/win32/ws2_32_new/src/addrconv.c
5 * PURPOSE: Address and Port Conversion Support
6 * PROGRAMMER: Alex Ionescu (alex@relsoft.net)
9 /* INCLUDES ******************************************************************/
16 /* DEFINES *******************************************************************/
20 /* DWORD network to host byte order conversion for little endian machines */
22 ((((dw) & 0xFF000000L) >> 24) | \
23 (((dw) & 0x00FF0000L) >> 8) | \
24 (((dw) & 0x0000FF00L) << 8) | \
25 (((dw) & 0x000000FFL) << 24))
27 /* DWORD host to network byte order conversion for little endian machines */
29 ((((dw) & 0xFF000000L) >> 24) | \
30 (((dw) & 0x00FF0000L) >> 8) | \
31 (((dw) & 0x0000FF00L) << 8) | \
32 (((dw) & 0x000000FFL) << 24))
34 /* WORD network to host order conversion for little endian machines */
36 ((((w) & 0xFF00) >> 8) | \
37 (((w) & 0x00FF) << 8))
39 /* WORD host to network byte order conversion for little endian machines */
41 ((((w) & 0xFF00) >> 8) | \
42 (((w) & 0x00FF) << 8))
46 /* DWORD network to host byte order conversion for big endian machines */
50 /* DWORD host to network byte order conversion big endian machines */
54 /* WORD network to host order conversion for big endian machines */
58 /* WORD host to network byte order conversion for big endian machines */
64 /* FUNCTIONS *****************************************************************/
71 inet_addr(IN CONST CHAR FAR
* cp
)
73 register u_long val
, base
, n
;
74 register unsigned char c
;
75 u_long parts
[4], *pp
= parts
;
76 if (!cp
) return INADDR_ANY
;
77 if (!isdigit(*cp
)) return INADDR_NONE
;
81 * Collect number up to ``.''.
82 * Values are specified as for C:
83 * 0x=hex, 0=octal, other=decimal.
87 if (*++cp
== 'x' || *cp
== 'X')
94 val
= (val
* base
) + (c
- '0');
98 if (base
== 16 && isxdigit(c
)) {
99 val
= (val
<< 4) + (c
+ 10 - (islower(c
) ? 'a' : 'A'));
109 * a.b.c (with c treated as 16-bits)
110 * a.b (with b treated as 24 bits)
112 if (pp
>= parts
+ 4) return (INADDR_NONE
);
118 * Check for trailing characters.
120 if (*cp
&& !isspace((UCHAR
)*cp
)) return (INADDR_NONE
);
124 * Concoct the address according to
125 * the number of parts specified.
127 n
= (u_long
)(pp
- parts
);
130 case 1: /* a -- 32 bits */
134 case 2: /* a.b -- 8.24 bits */
135 val
= (parts
[0] << 24) | (parts
[1] & 0xffffff);
138 case 3: /* a.b.c -- 8.8.16 bits */
139 val
= (parts
[0] << 24) | ((parts
[1] & 0xff) << 16) |
143 case 4: /* a.b.c.d -- 8.8.8.8 bits */
144 val
= (parts
[0] << 24) | ((parts
[1] & 0xff) << 16) |
145 ((parts
[2] & 0xff) << 8) | (parts
[3] & 0xff);
149 return (INADDR_NONE
);
160 inet_ntoa(IN IN_ADDR in
)
166 BOOL ManualLoad
= FALSE
;
169 DPRINT("inet_ntoa: %lx\n", in
);
172 if ((ErrorCode
= WsApiProlog(&Process
, &Thread
)) != ERROR_SUCCESS
)
174 DPRINT("MANUAL LOAD\n");
176 /* Only fail if the error wasn't related to a missing WSAStartup */
177 if (ErrorCode
!= WSANOTINITIALISED
)
180 SetLastError(ErrorCode
);
184 /* Apps aren't expected to call WSAStartup for this API, so we will */
185 if ((ErrorCode
= WSAStartup(MAKEWORD(2,2), &WsaData
)) != ERROR_SUCCESS
)
188 SetLastError(ErrorCode
);
192 /* Try the prolog again */
194 if ((ErrorCode
= WsApiProlog(&Process
, &Thread
)) != ERROR_SUCCESS
)
196 /* Failed again... */
198 SetLastError(ErrorCode
);
204 _itoa(in
.S_un
.S_addr
& 0xFF, b
, 10);
206 _itoa((in
.S_un
.S_addr
>> 8) & 0xFF, b
, 10);
209 _itoa((in
.S_un
.S_addr
>> 16) & 0xFF, b
, 10);
212 _itoa((in
.S_un
.S_addr
>> 24) & 0xFF, b
, 10);
216 /* Cleanup the manual load */
217 if (ManualLoad
) WSACleanup();
219 /* Return the buffer */
228 htonl(IN ULONG hostlong
)
230 return DH2N(hostlong
);
238 htons(IN USHORT hostshort
)
240 return WH2N(hostshort
);
248 ntohl(IN ULONG netlong
)
250 return DN2H(netlong
);
258 ntohs(IN USHORT netshort
)
260 return WN2H(netshort
);
268 WSAHtonl(IN SOCKET s
,
270 OUT ULONG FAR
* lpnetlong
)
274 DPRINT("WSAHtonl: %p, %lx, %p\n", s
, hostlong
, lpnetlong
);
276 /* Check for WSAStartup */
277 if ((ErrorCode
= WsQuickProlog()) == ERROR_SUCCESS
)
279 /* Make sure we got a parameter */
283 SetLastError(WSAEFAULT
);
287 /* Get the Socket Context */
288 if ((Socket
= WsSockGetSocket(s
)))
290 /* Check which byte order to use */
291 if (Socket
->CatalogEntry
->ProtocolInfo
.iNetworkByteOrder
==
294 /* No conversion needed */
295 *lpnetlong
= hostlong
;
300 *lpnetlong
= DN2H(hostlong
);
303 /* Dereference the socket */
304 WsSockDereference(Socket
);
307 return ERROR_SUCCESS
;
311 /* Set the error code */
312 ErrorCode
= WSAENOTSOCK
;
316 /* Return with error */
317 SetLastError(ErrorCode
);
326 WSAHtons(IN SOCKET s
,
328 OUT USHORT FAR
* lpnetshort
)
332 DPRINT("WSAHtons: %p, %lx, %p\n", s
, hostshort
, lpnetshort
);
334 /* Check for WSAStartup */
335 if ((ErrorCode
= WsQuickProlog()) == ERROR_SUCCESS
)
337 /* Make sure we got a parameter */
341 SetLastError(WSAEFAULT
);
345 /* Get the Socket Context */
346 if ((Socket
= WsSockGetSocket(s
)))
348 /* Check which byte order to use */
349 if (Socket
->CatalogEntry
->ProtocolInfo
.iNetworkByteOrder
==
352 /* No conversion needed */
353 *lpnetshort
= hostshort
;
358 *lpnetshort
= WN2H(hostshort
);
361 /* Dereference the socket */
362 WsSockDereference(Socket
);
365 return ERROR_SUCCESS
;
369 /* Set the error code */
370 ErrorCode
= WSAENOTSOCK
;
374 /* Return with error */
375 SetLastError(ErrorCode
);
384 WSANtohl(IN SOCKET s
,
386 OUT ULONG FAR
* lphostlong
)
390 DPRINT("WSANtohl: %p, %lx, %p\n", s
, netlong
, lphostlong
);
392 /* Check for WSAStartup */
393 if ((ErrorCode
= WsQuickProlog()) == ERROR_SUCCESS
)
395 /* Make sure we got a parameter */
399 SetLastError(WSAEFAULT
);
403 /* Get the Socket Context */
404 if ((Socket
= WsSockGetSocket(s
)))
406 /* Check which byte order to use */
407 if (Socket
->CatalogEntry
->ProtocolInfo
.iNetworkByteOrder
==
410 /* No conversion needed */
411 *lphostlong
= netlong
;
416 *lphostlong
= DN2H(netlong
);
419 /* Dereference the socket */
420 WsSockDereference(Socket
);
423 return ERROR_SUCCESS
;
427 /* Set the error code */
428 ErrorCode
= WSAENOTSOCK
;
432 /* Return with error */
433 SetLastError(ErrorCode
);
442 WSANtohs(IN SOCKET s
,
444 OUT USHORT FAR
* lphostshort
)
448 DPRINT("WSANtohs: %p, %lx, %p\n", s
, netshort
, lphostshort
);
450 /* Check for WSAStartup */
451 if ((ErrorCode
= WsQuickProlog()) == ERROR_SUCCESS
)
453 /* Make sure we got a parameter */
457 SetLastError(WSAEFAULT
);
461 /* Get the Socket Context */
462 if ((Socket
= WsSockGetSocket(s
)))
464 /* Check which byte order to use */
465 if (Socket
->CatalogEntry
->ProtocolInfo
.iNetworkByteOrder
==
468 /* No conversion needed */
469 *lphostshort
= netshort
;
474 *lphostshort
= WN2H(netshort
);
477 /* Dereference the socket */
478 WsSockDereference(Socket
);
481 return ERROR_SUCCESS
;
485 /* Set the error code */
486 ErrorCode
= WSAENOTSOCK
;
490 /* Return with error */
491 SetLastError(ErrorCode
);