File:  [ELWIX - Embedded LightWeight unIX -] / embedaddon / ntp / include / ntp_net.h
Revision 1.1.1.1 (vendor branch): download - view: text, annotated - select for diffs - revision graph
Tue May 29 12:08:38 2012 UTC (12 years, 5 months ago) by misho
Branches: ntp, MAIN
CVS tags: v4_2_6p5p0, v4_2_6p5, HEAD
ntp 4.2.6p5

    1: /*
    2:  * ntp_net.h - definitions for NTP network stuff
    3:  */
    4: 
    5: #ifndef NTP_NET_H
    6: #define NTP_NET_H
    7: 
    8: #include <sys/types.h>
    9: #ifdef HAVE_SYS_SOCKET_H
   10: #include <sys/socket.h>
   11: #endif
   12: #ifdef HAVE_NET_IF_H
   13: #include <net/if.h>
   14: #endif
   15: #ifdef HAVE_NETINET_IN_H
   16: #include <netinet/in.h>
   17: #endif
   18: #ifdef HAVE_NET_IF_VAR_H
   19: #include <net/if_var.h>
   20: #endif
   21: #ifdef HAVE_NETINET_IN_VAR_H
   22: #include <netinet/in_var.h>
   23: #endif
   24: 
   25: #include "ntp_rfc2553.h"
   26: 
   27: typedef union {
   28: 	struct sockaddr		sa;
   29: 	struct sockaddr_in	sa4;
   30: 	struct sockaddr_in6	sa6;
   31: } sockaddr_u;
   32: 
   33: /*
   34:  * Utilities for manipulating sockaddr_u v4/v6 unions
   35:  */
   36: #define SOCK_ADDR4(psau)	((psau)->sa4.sin_addr)
   37: #define SOCK_ADDR6(psau)	((psau)->sa6.sin6_addr)
   38: 
   39: #define PSOCK_ADDR4(psau)	(&SOCK_ADDR4(psau))
   40: #define PSOCK_ADDR6(psau)	(&SOCK_ADDR6(psau))
   41: 
   42: #define AF(psau)		((psau)->sa.sa_family)
   43: 
   44: #define IS_IPV4(psau)		(AF_INET == AF(psau))
   45: #define IS_IPV6(psau)		(AF_INET6 == AF(psau))
   46: 
   47: /* sockaddr_u v4 address in network byte order */
   48: #define	NSRCADR(psau)		(SOCK_ADDR4(psau).s_addr)
   49: 
   50: /* sockaddr_u v4 address in host byte order */
   51: #define	SRCADR(psau)		(ntohl(NSRCADR(psau)))
   52: 
   53: /* sockaddr_u v6 address in network byte order */
   54: #define NSRCADR6(psau)		(SOCK_ADDR6(psau).s6_addr)
   55: 
   56: /* assign sockaddr_u v4 address from host byte order */
   57: #define	SET_ADDR4(psau, addr4)	(NSRCADR(psau) = htonl(addr4))
   58: 
   59: /* assign sockaddr_u v4 address from network byte order */
   60: #define SET_ADDR4N(psau, addr4n) (NSRCADR(psau) = (addr4n));
   61: 
   62: /* assign sockaddr_u v6 address from network byte order */
   63: #define SET_ADDR6N(psau, s6_addr)				\
   64: 	(SOCK_ADDR6(psau) = (s6_addr))
   65: 
   66: /* sockaddr_u v4/v6 port in network byte order */
   67: #define	NSRCPORT(psau)		((psau)->sa4.sin_port)
   68: 
   69: /* sockaddr_u v4/v6 port in host byte order */
   70: #define	SRCPORT(psau)		(ntohs(NSRCPORT(psau)))
   71: 
   72: /* assign sockaddr_u v4/v6 port from host byte order */
   73: #define SET_PORT(psau, port)	(NSRCPORT(psau) = htons(port))
   74: 
   75: /* sockaddr_u v6 scope */
   76: #define SCOPE_VAR(psau)		((psau)->sa6.sin6_scope_id)
   77: 
   78: #ifdef ISC_PLATFORM_HAVESCOPEID
   79: /* v4/v6 scope (always zero for v4) */
   80: # define SCOPE(psau)		(IS_IPV4(psau)			\
   81: 				    ? 0				\
   82: 				    : SCOPE_VAR(psau))
   83: 
   84: /* are two v6 sockaddr_u scopes equal? */
   85: # define SCOPE_EQ(psau1, psau2)					\
   86: 	(SCOPE_VAR(psau1) == SCOPE_VAR(psau2))
   87: 
   88: /* assign scope if supported */
   89: # define SET_SCOPE(psau, s)					\
   90: 	do							\
   91: 		if (IS_IPV6(psau))				\
   92: 			SCOPE_VAR(psau) = (s);			\
   93: 	while (0)
   94: #else	/* ISC_PLATFORM_HAVESCOPEID not defined */
   95: # define SCOPE(psau)		(0)
   96: # define SCOPE_EQ(psau1, psau2)	(1)
   97: # define SET_SCOPE(psau, s)	do { } while (0)
   98: #endif	/* ISC_PLATFORM_HAVESCOPEID */
   99: 
  100: /* v4/v6 is multicast address */
  101: #define IS_MCAST(psau)						\
  102: 	(IS_IPV4(psau)						\
  103: 	    ? IN_CLASSD(SRCADR(psau))				\
  104: 	    : IN6_IS_ADDR_MULTICAST(PSOCK_ADDR6(psau)))
  105: 
  106: /* v6 is interface ID scope universal, as with MAC-derived addresses */
  107: #define IS_IID_UNIV(psau)					\
  108: 	(!!(0x02 & NSRCADR6(psau)[8]))
  109: 
  110: #define SIZEOF_INADDR(fam)					\
  111: 	((AF_INET == (fam))					\
  112: 	    ? sizeof(struct in_addr)				\
  113: 	    : sizeof(struct in6_addr))
  114: 
  115: #define SIZEOF_SOCKADDR(fam)					\
  116: 	((AF_INET == (fam))					\
  117: 	    ? sizeof(struct sockaddr_in)			\
  118: 	    : sizeof(struct sockaddr_in6))
  119: 
  120: #define SOCKLEN(psau)						\
  121: 	(IS_IPV4(psau)						\
  122: 	    ? sizeof((psau)->sa4)				\
  123: 	    : sizeof((psau)->sa6))
  124: 
  125: #define ZERO_SOCK(psau)						\
  126: 	memset((psau), 0, sizeof(*(psau)))
  127: 
  128: /* blast a byte value across sockaddr_u v6 address */
  129: #define	MEMSET_ADDR6(psau, v)					\
  130: 	memset((psau)->sa6.sin6_addr.s6_addr, (v),		\
  131: 		sizeof((psau)->sa6.sin6_addr.s6_addr))
  132: 
  133: #define SET_ONESMASK(psau)					\
  134: 	do {							\
  135: 		if (IS_IPV6(psau))				\
  136: 			MEMSET_ADDR6((psau), 0xff);		\
  137: 		else						\
  138: 			NSRCADR(psau) = 0xffffffff;		\
  139: 	} while(0)
  140: 
  141: /* zero sockaddr_u, fill in family and all-ones (host) mask */
  142: #define SET_HOSTMASK(psau, family)				\
  143: 	do {							\
  144: 		ZERO_SOCK(psau);				\
  145: 		AF(psau) = (family);				\
  146: 		SET_ONESMASK(psau);				\
  147: 	} while (0)
  148: 
  149: /* 
  150:  * compare two in6_addr returning negative, 0, or positive.
  151:  * ADDR6_CMP is negative if *pin6A is lower than *pin6B, zero if they
  152:  * are equal, positive if *pin6A is higher than *pin6B.  IN6ADDR_ANY
  153:  * is the lowest address (128 zero bits).
  154:  */
  155: #define	ADDR6_CMP(pin6A, pin6B)					\
  156: 	memcmp((pin6A)->s6_addr, (pin6B)->s6_addr,		\
  157: 	       sizeof(pin6A)->s6_addr)
  158: 
  159: /* compare two in6_addr for equality only */
  160: #if !defined(SYS_WINNT) || !defined(in_addr6)
  161: #define ADDR6_EQ(pin6A, pin6B)					\
  162: 	(!ADDR6_CMP(pin6A, pin6B))
  163: #else
  164: #define ADDR6_EQ(pin6A, pin6B)					\
  165: 	IN6_ADDR_EQUAL(pin6A, pin6B)
  166: #endif
  167: 
  168: /* compare a in6_addr with socket address */
  169: #define	S_ADDR6_EQ(psau, pin6)					\
  170: 	ADDR6_EQ(&(psau)->sa6.sin6_addr, pin6)
  171: 
  172: /* are two sockaddr_u's addresses equal? (port excluded) */
  173: #define SOCK_EQ(psau1, psau2)					\
  174: 	((AF(psau1) != AF(psau2))				\
  175: 	     ? 0						\
  176: 	     : IS_IPV4(psau1)					\
  177: 		   ? (NSRCADR(psau1) == NSRCADR(psau2))		\
  178: 		   : (S_ADDR6_EQ((psau1), PSOCK_ADDR6(psau2))	\
  179: 		      && SCOPE_EQ((psau1), (psau2))))
  180: 
  181: /* are two sockaddr_u's addresses and ports equal? */
  182: #define ADDR_PORT_EQ(psau1, psau2)				\
  183: 	((NSRCPORT(psau1) != NSRCPORT(psau2)			\
  184: 	     ? 0						\
  185: 	     : SOCK_EQ((psau1), (psau2))))
  186: 
  187: /* is sockaddr_u address unspecified? */
  188: #define SOCK_UNSPEC(psau)					\
  189: 	(IS_IPV4(psau)						\
  190: 	    ? !NSRCADR(psau)					\
  191: 	    : IN6_IS_ADDR_UNSPECIFIED(PSOCK_ADDR6(psau)))
  192: 
  193: /* just how unspecified do you mean? (scope 0/unspec too) */
  194: #define SOCK_UNSPEC_S(psau)					\
  195: 	(SOCK_UNSPEC(psau) && !SCOPE(psau))
  196: 
  197: /* choose a default net interface (struct interface) for v4 or v6 */
  198: #define ANY_INTERFACE_BYFAM(family)				\
  199: 	((AF_INET == family)					\
  200: 	     ? any_interface					\
  201: 	     : any6_interface)
  202: 
  203: /* choose a default interface for addresses' protocol (addr family) */
  204: #define ANY_INTERFACE_CHOOSE(psau)				\
  205: 	ANY_INTERFACE_BYFAM(AF(psau))
  206: 
  207: 
  208: /*
  209:  * We tell reference clocks from real peers by giving the reference
  210:  * clocks an address of the form 127.127.t.u, where t is the type and
  211:  * u is the unit number.  We define some of this here since we will need
  212:  * some sanity checks to make sure this address isn't interpretted as
  213:  * that of a normal peer.
  214:  */
  215: #define	REFCLOCK_ADDR	0x7f7f0000	/* 127.127.0.0 */
  216: #define	REFCLOCK_MASK	0xffff0000	/* 255.255.0.0 */
  217: 
  218: #ifdef REFCLOCK
  219: #define	ISREFCLOCKADR(srcadr)					\
  220: 	(IS_IPV4(srcadr) &&					\
  221: 	 (SRCADR(srcadr) & REFCLOCK_MASK) == REFCLOCK_ADDR)
  222: #else
  223: #define ISREFCLOCKADR(srcadr)		(0)
  224: #endif
  225: 
  226: /*
  227:  * Macro for checking for invalid addresses.  This is really, really
  228:  * gross, but is needed so no one configures a host on net 127 now that
  229:  * we're encouraging it the the configuration file.
  230:  */
  231: #define	LOOPBACKADR	0x7f000001
  232: #define	LOOPNETMASK	0xff000000
  233: 
  234: #define	ISBADADR(srcadr)					\
  235: 	(IS_IPV4(srcadr)					\
  236: 	 && ((SRCADR(srcadr) & LOOPNETMASK)			\
  237: 	     == (LOOPBACKADR & LOOPNETMASK))			\
  238: 	 && SRCADR(srcadr) != LOOPBACKADR)
  239: 
  240: 
  241: #endif /* NTP_NET_H */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>