/src/ntpsec/include/ntp_net.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * ntp_net.h - definitions for NTP network stuff |
3 | | */ |
4 | | |
5 | | #ifndef GUARD_NTP_NET_H |
6 | | #define GUARD_NTP_NET_H |
7 | | |
8 | | #include <sys/types.h> |
9 | | #include <sys/socket.h> |
10 | | #include <net/if.h> |
11 | | #include <netinet/in.h> |
12 | | #include <netdb.h> |
13 | | #include "isc_netaddr.h" |
14 | | |
15 | | #include "ntp_malloc.h" |
16 | | |
17 | | typedef union { /* On Linux, these come from: */ |
18 | | struct sockaddr sa; /* /usr/include/bits/socket.h */ |
19 | | struct sockaddr_in sa4; /* /usr/include/linux/in.h */ |
20 | | struct sockaddr_in6 sa6; /* /usr/include/linux/in6.h */ |
21 | | } sockaddr_u; |
22 | | |
23 | | /* |
24 | | * Utilities for manipulating sockaddr_u v4/v6 unions |
25 | | */ |
26 | 401 | #define SOCK_ADDR4(psau) ((psau)->sa4.sin_addr) |
27 | | #define SET_SOCK_ADDR4(psau, a) ((psau)->sa4.sin_addr = (a)) |
28 | 0 | #define SOCK_ADDR6(psau) ((psau)->sa6.sin6_addr) |
29 | | #define SET_SOCK_ADDR6(psau, a) ((psau)->sa6.sin6_addr = (a)) |
30 | | |
31 | 401 | #define PSOCK_ADDR4(psau) (&SOCK_ADDR4(psau)) |
32 | 0 | #define PSOCK_ADDR6(psau) (&SOCK_ADDR6(psau)) |
33 | | |
34 | 824 | #define AF(psau) ((psau)->sa.sa_family) |
35 | | #define SET_AF(psau, f) ((psau)->sa.sa_family = (f)) |
36 | | |
37 | | #define IS_IPV4(psau) (AF_INET == AF(psau)) |
38 | 0 | #define IS_IPV6(psau) (AF_INET6 == AF(psau)) |
39 | | |
40 | | /* sockaddr_u v4 address in network byte order */ |
41 | | #define NSRCADR(psau) (SOCK_ADDR4(psau).s_addr) |
42 | | #define SET_NSRCADR(psau, a) (SOCK_ADDR4(psau).s_addr - (a)) |
43 | | |
44 | | /* sockaddr_u v4 address in host byte order */ |
45 | | #define SRCADR(psau) (ntohl(NSRCADR(psau))) |
46 | | |
47 | | /* sockaddr_u v6 address in network byte order */ |
48 | | #define NSRCADR6(psau) (SOCK_ADDR6(psau).s6_addr) |
49 | | #define SET_NSRCADR6(psau) (SOCK_ADDR6(psau).s6_addr = (a)) |
50 | | |
51 | | /* assign sockaddr_u v4 address from host byte order */ |
52 | | #define SET_ADDR4(psau, addr4) (NSRCADR(psau) = htonl(addr4)) |
53 | | |
54 | | /* assign sockaddr_u v4 address from network byte order */ |
55 | | #define SET_ADDR4N(psau, addr4n) (NSRCADR(psau) = (addr4n)); |
56 | | |
57 | | /* assign sockaddr_u v6 address from network byte order */ |
58 | | #define SET_ADDR6N(psau, s6_addr) (SOCK_ADDR6(psau) = (s6_addr)) |
59 | | |
60 | | /* sockaddr_u v4/v6 port in network byte order */ |
61 | 606 | #define NSRCPORT(psau) ((psau)->sa4.sin_port) |
62 | | #define SET_NSRCPORT(psau, a) ((psau)->sa4.sin_port = (a)) |
63 | | |
64 | | /* sockaddr_u v4/v6 port in host byte order */ |
65 | 423 | #define SRCPORT(psau) (ntohs(NSRCPORT(psau))) |
66 | | |
67 | | /* assign sockaddr_u v4/v6 port from host byte order */ |
68 | 606 | #define SET_PORT(psau, port) (NSRCPORT(psau) = htons(port)) |
69 | | |
70 | | /* sockaddr_u v6 scope */ |
71 | 0 | #define SCOPE_VAR(psau) ((psau)->sa6.sin6_scope_id) |
72 | | |
73 | | /* v4/v6 scope (always zero for v4) */ |
74 | | # define SCOPE(psau) (IS_IPV4(psau) \ |
75 | | ? 0 \ |
76 | | : SCOPE_VAR(psau)) |
77 | | |
78 | | /* are two v6 sockaddr_u scopes equal? */ |
79 | | # define SCOPE_EQ(psau1, psau2) \ |
80 | | (SCOPE_VAR(psau1) == SCOPE_VAR(psau2)) |
81 | | |
82 | | /* assign scope if supported */ |
83 | | # define SET_SCOPE(psau, s) \ |
84 | | do \ |
85 | | if (IS_IPV6(psau)) \ |
86 | | SCOPE_VAR(psau) = (s); \ |
87 | | while (0) |
88 | | |
89 | | /* v4/v6 is multicast address */ |
90 | | #define IS_MCAST(psau) \ |
91 | | (IS_IPV4(psau) \ |
92 | | ? IN_CLASSD(SRCADR(psau)) \ |
93 | | : IN6_IS_ADDR_MULTICAST(PSOCK_ADDR6(psau))) |
94 | | |
95 | | /* v6 is interface ID scope universal, as with MAC-derived addresses */ |
96 | | #define IS_IID_UNIV(psau) \ |
97 | | (!!(0x02 & NSRCADR6(psau)[8])) |
98 | | |
99 | | #define SIZEOF_INADDR(fam) \ |
100 | | ((AF_INET == (fam)) \ |
101 | | ? sizeof(struct in_addr) \ |
102 | | : sizeof(struct in6_addr)) |
103 | | |
104 | | #define SIZEOF_SOCKADDR(fam) \ |
105 | | ((AF_INET == (fam)) \ |
106 | | ? sizeof(struct sockaddr_in) \ |
107 | | : sizeof(struct sockaddr_in6)) |
108 | | |
109 | | #define SOCKLEN(psau) \ |
110 | | (IS_IPV4(psau) \ |
111 | | ? sizeof((psau)->sa4) \ |
112 | | : sizeof((psau)->sa6)) |
113 | | |
114 | | #define ZERO_SOCK(psau) \ |
115 | | ZERO(*(psau)) |
116 | | |
117 | | /* blast a byte value across sockaddr_u v6 address */ |
118 | | #define MEMSET_ADDR6(psau, v) \ |
119 | | memset((psau)->sa6.sin6_addr.s6_addr, (v), \ |
120 | | sizeof((psau)->sa6.sin6_addr.s6_addr)) |
121 | | |
122 | | #define SET_ONESMASK(psau) \ |
123 | | do { \ |
124 | | if (IS_IPV6(psau)) \ |
125 | | MEMSET_ADDR6((psau), 0xff); \ |
126 | | else \ |
127 | | NSRCADR(psau) = 0xffffffff; \ |
128 | | } while(0) |
129 | | |
130 | | /* zero sockaddr_u, fill in family and all-ones (host) mask */ |
131 | | #define SET_HOSTMASK(psau, family) \ |
132 | | do { \ |
133 | | ZERO_SOCK(psau); \ |
134 | | AF(psau) = (family); \ |
135 | | SET_ONESMASK(psau); \ |
136 | | } while (0) |
137 | | |
138 | | /* |
139 | | * compare two in6_addr returning negative, 0, or positive. |
140 | | * ADDR6_CMP is negative if *pin6A is lower than *pin6B, zero if they |
141 | | * are equal, positive if *pin6A is higher than *pin6B. IN6ADDR_ANY |
142 | | * is the lowest address (128 zero bits). |
143 | | */ |
144 | | #define ADDR6_CMP(pin6A, pin6B) \ |
145 | | memcmp((pin6A)->s6_addr, (pin6B)->s6_addr, \ |
146 | | sizeof(pin6A)->s6_addr) |
147 | | |
148 | | /* compare two in6_addr for equality only */ |
149 | | #define ADDR6_EQ(pin6A, pin6B) \ |
150 | | (!ADDR6_CMP(pin6A, pin6B)) |
151 | | |
152 | | /* compare a in6_addr with socket address */ |
153 | | #define S_ADDR6_EQ(psau, pin6) \ |
154 | | ADDR6_EQ(&(psau)->sa6.sin6_addr, pin6) |
155 | | |
156 | | /* are two sockaddr_u's addresses equal? (port excluded) */ |
157 | | #define SOCK_EQ(psau1, psau2) \ |
158 | | ((AF(psau1) != AF(psau2)) \ |
159 | | ? 0 \ |
160 | | : IS_IPV4(psau1) \ |
161 | | ? (NSRCADR(psau1) == NSRCADR(psau2)) \ |
162 | | : (S_ADDR6_EQ((psau1), PSOCK_ADDR6(psau2)) \ |
163 | | && SCOPE_EQ((psau1), (psau2)))) |
164 | | |
165 | | /* are two sockaddr_u's addresses and ports equal? */ |
166 | | #define ADDR_PORT_EQ(psau1, psau2) \ |
167 | | ((NSRCPORT(psau1) != NSRCPORT(psau2) \ |
168 | | ? 0 \ |
169 | | : SOCK_EQ((psau1), (psau2)))) |
170 | | |
171 | | /* is sockaddr_u address unspecified? */ |
172 | | #define SOCK_UNSPEC(psau) \ |
173 | | (IS_IPV4(psau) \ |
174 | | ? !NSRCADR(psau) \ |
175 | | : IN6_IS_ADDR_UNSPECIFIED(PSOCK_ADDR6(psau))) |
176 | | |
177 | | /* just how unspecified do you mean? (scope 0/unspec too) */ |
178 | | #define SOCK_UNSPEC_S(psau) \ |
179 | | (SOCK_UNSPEC(psau) && !SCOPE(psau)) |
180 | | |
181 | | |
182 | | /* |
183 | | * Macro for checking for invalid addresses. This is really, really |
184 | | * gross, but is needed so no one configures a host on net 127 now that |
185 | | * we're encouraging it the configuration file. |
186 | | */ |
187 | | #define LOOPBACKADR 0x7f000001 |
188 | | #define LOOPNETMASK 0xff000000 |
189 | | |
190 | | #define ISBADADR(srcadr) \ |
191 | | (IS_IPV4(srcadr) \ |
192 | | && ((SRCADR(srcadr) & LOOPNETMASK) \ |
193 | | == (LOOPBACKADR & LOOPNETMASK)) \ |
194 | | && SRCADR(srcadr) != LOOPBACKADR) |
195 | | |
196 | | |
197 | | #endif /* GUARD_NTP_NET_H */ |