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 ******************************************************************/
15 /* DEFINES *******************************************************************/
19 /* DWORD network to host byte order conversion for little endian machines */
21 ((((dw) & 0xFF000000L) >> 24) | \
22 (((dw) & 0x00FF0000L) >> 8) | \
23 (((dw) & 0x0000FF00L) << 8) | \
24 (((dw) & 0x000000FFL) << 24))
26 /* DWORD host to network byte order conversion for little endian machines */
28 ((((dw) & 0xFF000000L) >> 24) | \
29 (((dw) & 0x00FF0000L) >> 8) | \
30 (((dw) & 0x0000FF00L) << 8) | \
31 (((dw) & 0x000000FFL) << 24))
33 /* WORD network to host order conversion for little endian machines */
35 ((((w) & 0xFF00) >> 8) | \
36 (((w) & 0x00FF) << 8))
38 /* WORD host to network byte order conversion for little endian machines */
40 ((((w) & 0xFF00) >> 8) | \
41 (((w) & 0x00FF) << 8))
45 /* DWORD network to host byte order conversion for big endian machines */
49 /* DWORD host to network byte order conversion big endian machines */
53 /* WORD network to host order conversion for big endian machines */
57 /* WORD host to network byte order conversion for big endian machines */
63 /* FUNCTIONS *****************************************************************/
70 inet_addr(IN CONST CHAR FAR
* cp
)
72 register u_long val
, base
, n
;
73 register unsigned char c
;
74 u_long parts
[4], *pp
= parts
;
78 * Collect number up to ``.''.
79 * Values are specified as for C:
80 * 0x=hex, 0=octal, other=decimal.
84 if (*++cp
== 'x' || *cp
== 'X')
91 val
= (val
* base
) + (c
- '0');
95 if (base
== 16 && isxdigit(c
)) {
96 val
= (val
<< 4) + (c
+ 10 - (islower(c
) ? 'a' : 'A'));
106 * a.b.c (with c treated as 16-bits)
107 * a.b (with b treated as 24 bits)
109 if (pp
>= parts
+ 4) return (INADDR_NONE
);
115 * Check for trailing characters.
117 if (*cp
&& !isspace((UCHAR
)*cp
)) return (INADDR_NONE
);
121 * Concoct the address according to
122 * the number of parts specified.
124 n
= (u_long
)(pp
- parts
);
127 case 1: /* a -- 32 bits */
131 case 2: /* a.b -- 8.24 bits */
132 val
= (parts
[0] << 24) | (parts
[1] & 0xffffff);
135 case 3: /* a.b.c -- 8.8.16 bits */
136 val
= (parts
[0] << 24) | ((parts
[1] & 0xff) << 16) |
140 case 4: /* a.b.c.d -- 8.8.8.8 bits */
141 val
= (parts
[0] << 24) | ((parts
[1] & 0xff) << 16) |
142 ((parts
[2] & 0xff) << 8) | (parts
[3] & 0xff);
157 inet_ntoa(IN IN_ADDR in
)
163 BOOL ManualLoad
= FALSE
;
166 DPRINT("inet_ntoa: %lx\n", in
);
169 if ((ErrorCode
= WsApiProlog(&Process
, &Thread
)) != ERROR_SUCCESS
)
171 DPRINT("MANUAL LOAD\n");
173 /* Only fail if the error wasn't related to a missing WSAStartup */
174 if (ErrorCode
!= WSANOTINITIALISED
)
177 SetLastError(ErrorCode
);
181 /* Apps aren't expected to call WSAStartup for this API, so we will */
182 if ((ErrorCode
= WSAStartup(MAKEWORD(2,2), &WsaData
)) != ERROR_SUCCESS
)
185 SetLastError(ErrorCode
);
189 /* Try the prolog again */
191 if ((ErrorCode
= WsApiProlog(&Process
, &Thread
)) != ERROR_SUCCESS
)
193 /* Failed again... */
195 SetLastError(ErrorCode
);
201 _itoa(in
.S_un
.S_addr
& 0xFF, b
, 10);
203 _itoa((in
.S_un
.S_addr
>> 8) & 0xFF, b
, 10);
206 _itoa((in
.S_un
.S_addr
>> 16) & 0xFF, b
, 10);
209 _itoa((in
.S_un
.S_addr
>> 24) & 0xFF, b
, 10);
213 /* Cleanup the manual load */
214 if (ManualLoad
) WSACleanup();
216 /* Return the buffer */
225 htonl(IN ULONG hostlong
)
227 return DH2N(hostlong
);
235 htons(IN USHORT hostshort
)
237 return WH2N(hostshort
);
245 ntohl(IN ULONG netlong
)
247 return DN2H(netlong
);
255 ntohs(IN USHORT netshort
)
257 return WN2H(netshort
);
265 WSAHtonl(IN SOCKET s
,
267 OUT ULONG FAR
* lpnetlong
)
271 DPRINT("WSAHtonl: %p, %lx, %p\n", s
, hostlong
, lpnetlong
);
273 /* Check for WSAStartup */
274 if ((ErrorCode
= WsQuickProlog()) == ERROR_SUCCESS
)
276 /* Make sure we got a parameter */
280 SetLastError(WSAEFAULT
);
284 /* Get the Socket Context */
285 if ((Socket
= WsSockGetSocket(s
)))
287 /* Check which byte order to use */
288 if (Socket
->CatalogEntry
->ProtocolInfo
.iNetworkByteOrder
==
291 /* No conversion needed */
292 *lpnetlong
= hostlong
;
297 *lpnetlong
= DN2H(hostlong
);
300 /* Dereference the socket */
301 WsSockDereference(Socket
);
304 return ERROR_SUCCESS
;
308 /* Set the error code */
309 ErrorCode
= WSAENOTSOCK
;
313 /* Return with error */
314 SetLastError(ErrorCode
);
323 WSAHtons(IN SOCKET s
,
325 OUT USHORT FAR
* lpnetshort
)
329 DPRINT("WSAHtons: %p, %lx, %p\n", s
, hostshort
, lpnetshort
);
331 /* Check for WSAStartup */
332 if ((ErrorCode
= WsQuickProlog()) == ERROR_SUCCESS
)
334 /* Make sure we got a parameter */
338 SetLastError(WSAEFAULT
);
342 /* Get the Socket Context */
343 if ((Socket
= WsSockGetSocket(s
)))
345 /* Check which byte order to use */
346 if (Socket
->CatalogEntry
->ProtocolInfo
.iNetworkByteOrder
==
349 /* No conversion needed */
350 *lpnetshort
= hostshort
;
355 *lpnetshort
= WN2H(hostshort
);
358 /* Dereference the socket */
359 WsSockDereference(Socket
);
362 return ERROR_SUCCESS
;
366 /* Set the error code */
367 ErrorCode
= WSAENOTSOCK
;
371 /* Return with error */
372 SetLastError(ErrorCode
);
381 WSANtohl(IN SOCKET s
,
383 OUT ULONG FAR
* lphostlong
)
387 DPRINT("WSANtohl: %p, %lx, %p\n", s
, netlong
, lphostlong
);
389 /* Check for WSAStartup */
390 if ((ErrorCode
= WsQuickProlog()) == ERROR_SUCCESS
)
392 /* Make sure we got a parameter */
396 SetLastError(WSAEFAULT
);
400 /* Get the Socket Context */
401 if ((Socket
= WsSockGetSocket(s
)))
403 /* Check which byte order to use */
404 if (Socket
->CatalogEntry
->ProtocolInfo
.iNetworkByteOrder
==
407 /* No conversion needed */
408 *lphostlong
= netlong
;
413 *lphostlong
= DN2H(netlong
);
416 /* Dereference the socket */
417 WsSockDereference(Socket
);
420 return ERROR_SUCCESS
;
424 /* Set the error code */
425 ErrorCode
= WSAENOTSOCK
;
429 /* Return with error */
430 SetLastError(ErrorCode
);
439 WSANtohs(IN SOCKET s
,
441 OUT USHORT FAR
* lphostshort
)
445 DPRINT("WSANtohs: %p, %lx, %p\n", s
, netshort
, lphostshort
);
447 /* Check for WSAStartup */
448 if ((ErrorCode
= WsQuickProlog()) == ERROR_SUCCESS
)
450 /* Make sure we got a parameter */
454 SetLastError(WSAEFAULT
);
458 /* Get the Socket Context */
459 if ((Socket
= WsSockGetSocket(s
)))
461 /* Check which byte order to use */
462 if (Socket
->CatalogEntry
->ProtocolInfo
.iNetworkByteOrder
==
465 /* No conversion needed */
466 *lphostshort
= netshort
;
471 *lphostshort
= WN2H(netshort
);
474 /* Dereference the socket */
475 WsSockDereference(Socket
);
478 return ERROR_SUCCESS
;
482 /* Set the error code */
483 ErrorCode
= WSAENOTSOCK
;
487 /* Return with error */
488 SetLastError(ErrorCode
);