2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS WinSock 2 API
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
;
79 * Collect number up to ``.''.
80 * Values are specified as for C:
81 * 0x=hex, 0=octal, other=decimal.
85 if (*++cp
== 'x' || *cp
== 'X')
92 val
= (val
* base
) + (c
- '0');
96 if (base
== 16 && isxdigit(c
)) {
97 val
= (val
<< 4) + (c
+ 10 - (islower(c
) ? 'a' : 'A'));
107 * a.b.c (with c treated as 16-bits)
108 * a.b (with b treated as 24 bits)
110 if (pp
>= parts
+ 4) return (INADDR_NONE
);
116 * Check for trailing characters.
118 if (*cp
&& !isspace((UCHAR
)*cp
)) return (INADDR_NONE
);
122 * Concoct the address according to
123 * the number of parts specified.
125 n
= (u_long
)(pp
- parts
);
128 case 1: /* a -- 32 bits */
132 case 2: /* a.b -- 8.24 bits */
133 val
= (parts
[0] << 24) | (parts
[1] & 0xffffff);
136 case 3: /* a.b.c -- 8.8.16 bits */
137 val
= (parts
[0] << 24) | ((parts
[1] & 0xff) << 16) |
141 case 4: /* a.b.c.d -- 8.8.8.8 bits */
142 val
= (parts
[0] << 24) | ((parts
[1] & 0xff) << 16) |
143 ((parts
[2] & 0xff) << 8) | (parts
[3] & 0xff);
158 inet_ntoa(IN IN_ADDR in
)
164 BOOL ManualLoad
= FALSE
;
167 DPRINT("inet_ntoa: %lx\n", in
);
170 if ((ErrorCode
= WsApiProlog(&Process
, &Thread
)) != ERROR_SUCCESS
)
172 DPRINT("MANUAL LOAD\n");
174 /* Only fail if the error wasn't related to a missing WSAStartup */
175 if (ErrorCode
!= WSANOTINITIALISED
)
178 SetLastError(ErrorCode
);
182 /* Apps aren't expected to call WSAStartup for this API, so we will */
183 if ((ErrorCode
= WSAStartup(MAKEWORD(2,2), &WsaData
)) != ERROR_SUCCESS
)
186 SetLastError(ErrorCode
);
190 /* Try the prolog again */
192 if ((ErrorCode
= WsApiProlog(&Process
, &Thread
)) != ERROR_SUCCESS
)
194 /* Failed again... */
196 SetLastError(ErrorCode
);
202 _itoa(in
.S_un
.S_addr
& 0xFF, b
, 10);
204 _itoa((in
.S_un
.S_addr
>> 8) & 0xFF, b
, 10);
207 _itoa((in
.S_un
.S_addr
>> 16) & 0xFF, b
, 10);
210 _itoa((in
.S_un
.S_addr
>> 24) & 0xFF, b
, 10);
214 /* Cleanup the manual load */
215 if (ManualLoad
) WSACleanup();
217 /* Return the buffer */
226 htonl(IN ULONG hostlong
)
228 return DH2N(hostlong
);
236 htons(IN USHORT hostshort
)
238 return WH2N(hostshort
);
246 ntohl(IN ULONG netlong
)
248 return DN2H(netlong
);
256 ntohs(IN USHORT netshort
)
258 return WN2H(netshort
);
266 WSAHtonl(IN SOCKET s
,
268 OUT ULONG FAR
* lpnetlong
)
272 DPRINT("WSAHtonl: %p, %lx, %p\n", s
, hostlong
, lpnetlong
);
274 /* Check for WSAStartup */
275 if ((ErrorCode
= WsQuickProlog()) == ERROR_SUCCESS
)
277 /* Make sure we got a parameter */
281 SetLastError(WSAEFAULT
);
285 /* Get the Socket Context */
286 if ((Socket
= WsSockGetSocket(s
)))
288 /* Check which byte order to use */
289 if (Socket
->CatalogEntry
->ProtocolInfo
.iNetworkByteOrder
==
292 /* No conversion needed */
293 *lpnetlong
= hostlong
;
298 *lpnetlong
= DN2H(hostlong
);
301 /* Dereference the socket */
302 WsSockDereference(Socket
);
305 return ERROR_SUCCESS
;
309 /* Set the error code */
310 ErrorCode
= WSAENOTSOCK
;
314 /* Return with error */
315 SetLastError(ErrorCode
);
324 WSAHtons(IN SOCKET s
,
326 OUT USHORT FAR
* lpnetshort
)
330 DPRINT("WSAHtons: %p, %lx, %p\n", s
, hostshort
, lpnetshort
);
332 /* Check for WSAStartup */
333 if ((ErrorCode
= WsQuickProlog()) == ERROR_SUCCESS
)
335 /* Make sure we got a parameter */
339 SetLastError(WSAEFAULT
);
343 /* Get the Socket Context */
344 if ((Socket
= WsSockGetSocket(s
)))
346 /* Check which byte order to use */
347 if (Socket
->CatalogEntry
->ProtocolInfo
.iNetworkByteOrder
==
350 /* No conversion needed */
351 *lpnetshort
= hostshort
;
356 *lpnetshort
= WN2H(hostshort
);
359 /* Dereference the socket */
360 WsSockDereference(Socket
);
363 return ERROR_SUCCESS
;
367 /* Set the error code */
368 ErrorCode
= WSAENOTSOCK
;
372 /* Return with error */
373 SetLastError(ErrorCode
);
382 WSANtohl(IN SOCKET s
,
384 OUT ULONG FAR
* lphostlong
)
388 DPRINT("WSANtohl: %p, %lx, %p\n", s
, netlong
, lphostlong
);
390 /* Check for WSAStartup */
391 if ((ErrorCode
= WsQuickProlog()) == ERROR_SUCCESS
)
393 /* Make sure we got a parameter */
397 SetLastError(WSAEFAULT
);
401 /* Get the Socket Context */
402 if ((Socket
= WsSockGetSocket(s
)))
404 /* Check which byte order to use */
405 if (Socket
->CatalogEntry
->ProtocolInfo
.iNetworkByteOrder
==
408 /* No conversion needed */
409 *lphostlong
= netlong
;
414 *lphostlong
= DN2H(netlong
);
417 /* Dereference the socket */
418 WsSockDereference(Socket
);
421 return ERROR_SUCCESS
;
425 /* Set the error code */
426 ErrorCode
= WSAENOTSOCK
;
430 /* Return with error */
431 SetLastError(ErrorCode
);
440 WSANtohs(IN SOCKET s
,
442 OUT USHORT FAR
* lphostshort
)
446 DPRINT("WSANtohs: %p, %lx, %p\n", s
, netshort
, lphostshort
);
448 /* Check for WSAStartup */
449 if ((ErrorCode
= WsQuickProlog()) == ERROR_SUCCESS
)
451 /* Make sure we got a parameter */
455 SetLastError(WSAEFAULT
);
459 /* Get the Socket Context */
460 if ((Socket
= WsSockGetSocket(s
)))
462 /* Check which byte order to use */
463 if (Socket
->CatalogEntry
->ProtocolInfo
.iNetworkByteOrder
==
466 /* No conversion needed */
467 *lphostshort
= netshort
;
472 *lphostshort
= WN2H(netshort
);
475 /* Dereference the socket */
476 WsSockDereference(Socket
);
479 return ERROR_SUCCESS
;
483 /* Set the error code */
484 ErrorCode
= WSAENOTSOCK
;
488 /* Return with error */
489 SetLastError(ErrorCode
);