hton.h

Go to the documentation of this file.
00001 /*
00002 The following macro definitions convert to and from the network standard byte
00003 order. The macros with their name in lower case guarantee to evaluate their
00004 argument exactly once. The function of the macros is encoded in their names;
00005 htons means convert a (unsigned) short in host byte order to network byte order.
00006 */
00007 
00008 #ifndef _NET__HTON_H
00009 #define _NET__HTON_H
00010 
00011 #include <minix/sys_config.h>
00012 
00013 extern u16_t _tmp;
00014 extern u32_t _tmp_l;
00015 
00016 /* Find out about the byte order. */
00017 
00018 /* assume <minix/config.h> is included, let's check */
00019 #if (_MINIX_CHIP == 0)
00020 #include "_MINIX_CHIP macro not set, include <minix/config.h>"
00021 #endif
00022 
00023 #if (_MINIX_CHIP == _CHIP_INTEL)
00024 #define LITTLE_ENDIAN   1
00025 #endif
00026 
00027 #if (_MINIX_CHIP == _CHIP_M68000 || _MINIX_CHIP == _CHIP_SPARC)
00028 #define BIG_ENDIAN      1
00029 #endif
00030 
00031 #if (LITTLE_ENDIAN) && (BIG_ENDIAN)
00032 #include "both LITTLE_ENDIAN and BIG_ENDIAN are defined"
00033                         /* LITTLE_ENDIAN and BIG_ENDIAN are both defined */
00034 #endif
00035 
00036 #if !(LITTLE_ENDIAN) && !(BIG_ENDIAN)
00037 #include "neither LITTLE_ENDIAN nor BIG_ENDIAN is defined"
00038                         /* LITTLE_ENDIAN and BIG_ENDIAN are both NOT defined */
00039 #endif
00040 
00041 #if LITTLE_ENDIAN
00042 #define HTONS(x) ( ( (((unsigned short)(x)) >>8) & 0xff) | \
00043                 ((((unsigned short)(x)) & 0xff)<<8) )
00044 #define NTOHS(x) ( ( (((unsigned short)(x)) >>8) & 0xff) | \
00045                 ((((unsigned short)(x)) & 0xff)<<8) )
00046 #define HTONL(x) ((((x)>>24) & 0xffL) | (((x)>>8) & 0xff00L) | \
00047                 (((x)<<8) & 0xff0000L) | (((x)<<24) & 0xff000000L))
00048 #define NTOHL(x) ((((x)>>24) & 0xffL) | (((x)>>8) & 0xff00L) | \
00049                 (((x)<<8) & 0xff0000L) | (((x)<<24) & 0xff000000L))
00050 
00051 #if _WORD_SIZE > 2
00052 #define htons(x) (_tmp=(x), ((_tmp>>8) & 0xff) | ((_tmp<<8) & 0xff00))
00053 #define ntohs(x) (_tmp=(x), ((_tmp>>8) & 0xff) | ((_tmp<<8) & 0xff00))
00054 #define htonl(x) (_tmp_l=(x), ((_tmp_l>>24) & 0xffL) | \
00055                 ((_tmp_l>>8) & 0xff00L) | \
00056                 ((_tmp_l<<8) & 0xff0000L) | ((_tmp_l<<24) & 0xff000000L))
00057 #define ntohl(x) (_tmp_l=(x), ((_tmp_l>>24) & 0xffL) \
00058                 | ((_tmp_l>>8) & 0xff00L) | \
00059                 ((_tmp_l<<8) & 0xff0000L) | ((_tmp_l<<24) & 0xff000000L))
00060 
00061 #else /* _WORD_SIZE == 2 */
00062 /* The above macros are too unwieldy for a 16-bit machine. */
00063 u16_t htons(u16_t x);
00064 u16_t ntohs(u16_t x);
00065 u32_t htonl(u32_t x);
00066 u32_t ntohl(u32_t x);
00067 #endif /* _WORD_SIZE == 2 */
00068 
00069 #endif /* LITTLE_ENDIAN */
00070 
00071 #if BIG_ENDIAN
00072 #define htons(x) (x)
00073 #define HTONS(x) (x)
00074 #define ntohs(x) (x)
00075 #define NTOHS(x) (x)
00076 #define htonl(x) (x)
00077 #define HTONL(x) (x)
00078 #define ntohl(x) (x)
00079 #define NTOHL(x) (x)
00080 #endif /* BIG_ENDIAN */
00081 
00082 #endif /* _NET__HTON_H */

Generated on Fri Apr 14 22:57:21 2006 for minix by  doxygen 1.4.6