TCP/IP笔记一:判断子网类型的几个宏

最近在看《用TCP/IP进行网际互联》和《TCP/IP详解》,以及TCP/IP的几个开源实现:Xinu实现、BSD实现、lwIP实现、uIP实现,做一些小的笔记。、

下面是Xinu中判断子网类型的几个宏:

#if	BYTE_ORDER == BIG_ENDIAN
#define	IP_CLASSA(x) (((x) & 0x80000000) == 0)		/* IP Class A */
#define	IP_CLASSB(x) (((x) & 0xc0000000) == 0x80000000)	/* IP Class B */
#define	IP_CLASSC(x) (((x) & 0xe0000000) == 0xc0000000)	/* IP Class C */
#define	IP_CLASSD(x) (((x) & 0xf0000000) == 0xe0000000)	/* IP Class D */
#define	IP_CLASSE(x) (((x) & 0xf8000000) == 0xf0000000)	/* IP Class E */
#else	/* BYTE_ORDER */
#define	v(x)	(((x) & 0x80) == 0x00)	/* IP Class A address	*/
#define	IP_CLASSB(x)	(((x) & 0xc0) == 0x80)	/* IP Class B address	*/
#define	IP_CLASSC(x)	(((x) & 0xe0) == 0xc0)	/* IP Class C address	*/
#define	IP_CLASSD(x)	(((x) & 0xf0) == 0xe0)	/* IP Class D address	*/
#define	IP_CLASSE(x)	(((x) & 0xf8) == 0xf0)	/* IP Class E address	*/
#endif	/* BYTE_ORDER */

另外还有几个处理本机/网络字节顺序转换的位操作辅助宏:

#if	BYTE_ORDER == LITTLE_ENDIAN
#define hs2net(x) (unsigned) ((((x)>>8) &0xff) | (((x) & 0xff)<<8))
#define	net2hs(x) hs2net(x)

#define hl2net(x)	(((((x)& 0xff)<<24) | ((x)>>24) & 0xff) | \
	(((x) & 0xff0000)>>8) | (((x) & 0xff00)<<8))
#define net2hl(x) hl2net(x)
#endif

#if	BYTE_ORDER == BIG_ENDIAN
#define hs2net(x) (x)
#define net2hs(x) (x)
#define hl2net(x) (x)
#define net2hl(x) (x)
#endif

/* network macros */
#define hi8(x)   (unsigned char)  (((long) (x) >> 16) & 0x00ff)
#define low16(x) (unsigned short) ((long) (x) & 0xffff)


你可能感兴趣的:(c,网络,Class,NetWork,byte,macros)