1 | /*
|
---|
2 | The following macro definitions convert to and from the network standard byte
|
---|
3 | order. The macros with their name in lower case guarantee to evaluate their
|
---|
4 | argument exactly once. The function of the macros is encoded in their names;
|
---|
5 | htons means convert a (unsigned) short in host byte order to network byte order.
|
---|
6 | */
|
---|
7 |
|
---|
8 | #ifndef _NET__HTON_H
|
---|
9 | #define _NET__HTON_H
|
---|
10 |
|
---|
11 | #include <minix/sys_config.h>
|
---|
12 |
|
---|
13 | extern u16_t _tmp;
|
---|
14 | extern u32_t _tmp_l;
|
---|
15 |
|
---|
16 | /* Find out about the byte order. */
|
---|
17 |
|
---|
18 | /* assume <minix/config.h> is included, let's check */
|
---|
19 | #if (_MINIX_CHIP == 0)
|
---|
20 | #include "_MINIX_CHIP macro not set, include <minix/config.h>"
|
---|
21 | #endif
|
---|
22 |
|
---|
23 | #if (_MINIX_CHIP == _CHIP_INTEL)
|
---|
24 | #define LITTLE_ENDIAN 1
|
---|
25 | #endif
|
---|
26 |
|
---|
27 | #if (_MINIX_CHIP == _CHIP_M68000 || _MINIX_CHIP == _CHIP_SPARC)
|
---|
28 | #define BIG_ENDIAN 1
|
---|
29 | #endif
|
---|
30 |
|
---|
31 | #if (LITTLE_ENDIAN) && (BIG_ENDIAN)
|
---|
32 | #include "both LITTLE_ENDIAN and BIG_ENDIAN are defined"
|
---|
33 | /* LITTLE_ENDIAN and BIG_ENDIAN are both defined */
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | #if !(LITTLE_ENDIAN) && !(BIG_ENDIAN)
|
---|
37 | #include "neither LITTLE_ENDIAN nor BIG_ENDIAN is defined"
|
---|
38 | /* LITTLE_ENDIAN and BIG_ENDIAN are both NOT defined */
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | #if LITTLE_ENDIAN
|
---|
42 | #define HTONS(x) ( ( (((unsigned short)(x)) >>8) & 0xff) | \
|
---|
43 | ((((unsigned short)(x)) & 0xff)<<8) )
|
---|
44 | #define NTOHS(x) ( ( (((unsigned short)(x)) >>8) & 0xff) | \
|
---|
45 | ((((unsigned short)(x)) & 0xff)<<8) )
|
---|
46 | #define HTONL(x) ((((x)>>24) & 0xffL) | (((x)>>8) & 0xff00L) | \
|
---|
47 | (((x)<<8) & 0xff0000L) | (((x)<<24) & 0xff000000L))
|
---|
48 | #define NTOHL(x) ((((x)>>24) & 0xffL) | (((x)>>8) & 0xff00L) | \
|
---|
49 | (((x)<<8) & 0xff0000L) | (((x)<<24) & 0xff000000L))
|
---|
50 |
|
---|
51 | #if _WORD_SIZE > 2
|
---|
52 | #define htons(x) (_tmp=(x), ((_tmp>>8) & 0xff) | ((_tmp<<8) & 0xff00))
|
---|
53 | #define ntohs(x) (_tmp=(x), ((_tmp>>8) & 0xff) | ((_tmp<<8) & 0xff00))
|
---|
54 | #define htonl(x) (_tmp_l=(x), ((_tmp_l>>24) & 0xffL) | \
|
---|
55 | ((_tmp_l>>8) & 0xff00L) | \
|
---|
56 | ((_tmp_l<<8) & 0xff0000L) | ((_tmp_l<<24) & 0xff000000L))
|
---|
57 | #define ntohl(x) (_tmp_l=(x), ((_tmp_l>>24) & 0xffL) \
|
---|
58 | | ((_tmp_l>>8) & 0xff00L) | \
|
---|
59 | ((_tmp_l<<8) & 0xff0000L) | ((_tmp_l<<24) & 0xff000000L))
|
---|
60 |
|
---|
61 | #else /* _WORD_SIZE == 2 */
|
---|
62 | /* The above macros are too unwieldy for a 16-bit machine. */
|
---|
63 | u16_t htons(u16_t x);
|
---|
64 | u16_t ntohs(u16_t x);
|
---|
65 | u32_t htonl(u32_t x);
|
---|
66 | u32_t ntohl(u32_t x);
|
---|
67 | #endif /* _WORD_SIZE == 2 */
|
---|
68 |
|
---|
69 | #endif /* LITTLE_ENDIAN */
|
---|
70 |
|
---|
71 | #if BIG_ENDIAN
|
---|
72 | #define htons(x) (x)
|
---|
73 | #define HTONS(x) (x)
|
---|
74 | #define ntohs(x) (x)
|
---|
75 | #define NTOHS(x) (x)
|
---|
76 | #define htonl(x) (x)
|
---|
77 | #define HTONL(x) (x)
|
---|
78 | #define ntohl(x) (x)
|
---|
79 | #define NTOHL(x) (x)
|
---|
80 | #endif /* BIG_ENDIAN */
|
---|
81 |
|
---|
82 | #endif /* _NET__HTON_H */
|
---|