4 * ntp_net.h - definitions for NTP network stuff
10 #include <sys/types.h>
11 #ifdef HAVE_SYS_SOCKET_H
12 #include <sys/socket.h>
14 #ifdef HAVE_NETINET_IN_H
15 #include <netinet/in.h>
18 #include "ntp_rfc2553.h"
22 struct sockaddr_storage sas
;
23 struct sockaddr_in sa4
;
24 struct sockaddr_in6 sa6
;
28 * Utilities for manipulating sockaddr_u v4/v6 unions
30 #define SOCK_ADDR4(psau) ((psau)->sa4.sin_addr)
31 #define SOCK_ADDR6(psau) ((psau)->sa6.sin6_addr)
33 #define PSOCK_ADDR4(psau) (&SOCK_ADDR4(psau))
34 #define PSOCK_ADDR6(psau) (&SOCK_ADDR6(psau))
36 #define AF(psau) ((psau)->sas.ss_family)
38 #define IS_IPV4(psau) (AF_INET == AF(psau))
39 #define IS_IPV6(psau) (AF_INET6 == AF(psau))
41 /* sockaddr_u v4 address in network byte order */
42 #define NSRCADR(psau) (SOCK_ADDR4(psau).s_addr)
44 /* sockaddr_u v4 address in host byte order */
45 #define SRCADR(psau) (ntohl(NSRCADR(psau)))
47 /* sockaddr_u v6 address in network byte order */
48 #define NSRCADR6(psau) (SOCK_ADDR6(psau).s6_addr)
50 /* assign sockaddr_u v4 address from host byte order */
51 #define SET_ADDR4(psau, addr4) (NSRCADR(psau) = htonl(addr4))
53 /* assign sockaddr_u v4 address from network byte order */
54 #define SET_ADDR4N(psau, addr4n) (NSRCADR(psau) = (addr4n));
56 /* assign sockaddr_u v6 address from network byte order */
57 #define SET_ADDR6N(psau, s6_addr) \
58 (SOCK_ADDR6(psau) = (s6_addr))
60 /* sockaddr_u v4/v6 port in network byte order */
61 #define NSRCPORT(psau) ((psau)->sa4.sin_port)
63 /* sockaddr_u v4/v6 port in host byte order */
64 #define SRCPORT(psau) (ntohs(NSRCPORT(psau)))
66 /* assign sockaddr_u v4/v6 port from host byte order */
67 #define SET_PORT(psau, port) (NSRCPORT(psau) = htons(port))
69 /* sockaddr_u v6 scope */
70 #define SCOPE_VAR(psau) ((psau)->sa6.sin6_scope_id)
72 #ifdef ISC_PLATFORM_HAVESCOPEID
73 /* v4/v6 scope (always zero for v4) */
74 # define SCOPE(psau) (IS_IPV4(psau) \
78 /* are two v6 sockaddr_u scopes equal? */
79 # define SCOPE_EQ(psau1, psau2) \
80 (SCOPE_VAR(psau1) == SCOPE_VAR(psau2))
82 /* assign scope if supported */
83 # define SET_SCOPE(psau, s) \
86 SCOPE_VAR(psau) = (s); \
88 #else /* ISC_PLATFORM_HAVESCOPEID not defined */
89 # define SCOPE(psau) (0)
90 # define SCOPE_EQ(psau1, psau2) (1)
91 # define SET_SCOPE(psau, s) do { } while (0)
92 #endif /* ISC_PLATFORM_HAVESCOPEID */
94 /* v4/v6 is multicast address */
95 #define IS_MCAST(psau) \
97 ? IN_CLASSD(SRCADR(psau)) \
98 : IN6_IS_ADDR_MULTICAST(PSOCK_ADDR6(psau)))
100 #define SIZEOF_SOCKADDR(fam) \
101 ((AF_INET == (fam)) \
102 ? sizeof(struct sockaddr_in) \
103 : sizeof(struct sockaddr_in6))
105 #define SOCKLEN(psau) \
107 ? sizeof((psau)->sa4) \
108 : sizeof((psau)->sa6))
110 #define ZERO_SOCK(psau) \
111 memset(&(psau)->sas, 0, sizeof((psau)->sas))
113 /* blast a byte value across sockaddr_u v6 address */
114 #define MEMSET_ADDR6(psau, v) \
115 memset((void *)(psau)->sa6.sin6_addr.s6_addr, (v), \
116 sizeof((psau)->sa6.sin6_addr.s6_addr))
118 #define SET_ONESMASK(psau) \
121 MEMSET_ADDR6((psau), 0xff); \
123 NSRCADR(psau) = 0xffffffff; \
126 /* zero sockaddr_u, fill in family and all-ones (host) mask */
127 #define SET_HOSTMASK(psau, family) \
130 AF(psau) = (family); \
131 SET_ONESMASK(psau); \
134 /* compare a in6_addr with socket address */
135 #if !defined(SYS_WINNT) || !defined(in_addr6)
136 #define S_ADDR6_EQ(psau, my_in6_addr) \
137 (!memcmp(&(psau)->sa6.sin6_addr, \
139 sizeof((psau)->sa6.sin6_addr)))
141 #define S_ADDR6_EQ(psau, my_in6_addr) \
142 IN6_ADDR_EQUAL(&(psau)->sa6.sin6_addr, \
146 /* are two sockaddr_u's addresses equal? */
147 #define SOCK_EQ(psau1, psau2) \
148 ((AF(psau1) != AF(psau2)) \
151 ? (NSRCADR(psau1) == NSRCADR(psau2)) \
152 : (S_ADDR6_EQ((psau1), PSOCK_ADDR6(psau2)) \
153 && SCOPE_EQ((psau1), (psau2))))
155 /* is sockaddr_u address unspecified? */
156 #define SOCK_UNSPEC(psau) \
159 : IN6_IS_ADDR_UNSPECIFIED(PSOCK_ADDR6(psau)))
161 /* just how unspecified do you mean? (scope 0/unspec too) */
162 #define SOCK_UNSPEC_S(psau) \
163 (SOCK_UNSPEC(psau) && !SCOPE(psau))
165 /* choose a default net interface (struct interface) for v4 or v6 */
166 #define ANY_INTERFACE_BYFAM(family) \
167 ((AF_INET == family) \
171 /* choose a default interface for addresses' protocol (addr family) */
172 #define ANY_INTERFACE_CHOOSE(psau) \
173 ANY_INTERFACE_BYFAM(AF(psau))
177 * We tell reference clocks from real peers by giving the reference
178 * clocks an address of the form 127.127.t.u, where t is the type and
179 * u is the unit number. We define some of this here since we will need
180 * some sanity checks to make sure this address isn't interpretted as
181 * that of a normal peer.
183 #define REFCLOCK_ADDR 0x7f7f0000 /* 127.127.0.0 */
184 #define REFCLOCK_MASK 0xffff0000 /* 255.255.0.0 */
187 #define ISREFCLOCKADR(srcadr) \
188 (IS_IPV4(srcadr) && \
189 (SRCADR(srcadr) & REFCLOCK_MASK) == REFCLOCK_ADDR)
191 #define ISREFCLOCKADR(srcadr) (0)
195 * Macro for checking for invalid addresses. This is really, really
196 * gross, but is needed so no one configures a host on net 127 now that
197 * we're encouraging it the the configuration file.
199 #define LOOPBACKADR 0x7f000001
200 #define LOOPNETMASK 0xff000000
202 #define ISBADADR(srcadr) \
204 && ((SRCADR(srcadr) & LOOPNETMASK) \
205 == (LOOPBACKADR & LOOPNETMASK)) \
206 && SRCADR(srcadr) != LOOPBACKADR)
209 #endif /* NTP_NET_H */