xref: /freebsd/contrib/ntp/include/ntp_rfc2553.h (revision 9a14aa017b21c292740c00ee098195cd46642730)
1 /*
2  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the project nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /*
31  * Copyright (c) 1982, 1986, 1990, 1993
32  *	The Regents of the University of California.  All rights reserved.
33  *
34  * Redistribution and use in source and binary forms, with or without
35  * modification, are permitted provided that the following conditions
36  * are met:
37  * 1. Redistributions of source code must retain the above copyright
38  *    notice, this list of conditions and the following disclaimer.
39  * 2. Redistributions in binary form must reproduce the above copyright
40  *    notice, this list of conditions and the following disclaimer in the
41  *    documentation and/or other materials provided with the distribution.
42  * 3. All advertising materials mentioning features or use of this software
43  *    must display the following acknowledgement:
44  *	This product includes software developed by the University of
45  *	California, Berkeley and its contributors.
46  * 4. Neither the name of the University nor the names of its contributors
47  *    may be used to endorse or promote products derived from this software
48  *    without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  *
62  *	@(#)in.h	8.3 (Berkeley) 1/3/94
63  */
64 
65 /*
66  * Compatability shims with the rfc2553 API to simplify ntp.
67  */
68 #ifndef _NTP_RFC2553_H_
69 #define _NTP_RFC2553_H_
70 
71 /*
72  * Ensure that we include the configuration file before we check
73  * for IPV6
74  */
75 #include <config.h>
76 
77 #include <netdb.h>
78 
79 #include "ntp_types.h"
80 
81 /*
82  * Don't include any additional IPv6 definitions
83  * We are defining our own here.
84  */
85 #define ISC_IPV6_H 1
86 
87  /*
88  * If various macros are not defined we need to define them
89  */
90 
91 #ifndef AF_INET6
92 #define AF_INET6	AF_MAX
93 #define PF_INET6	AF_INET6
94 #endif
95 
96 #if !defined(_SS_MAXSIZE) && !defined(_SS_ALIGNSIZE)
97 
98 #define	_SS_MAXSIZE	128
99 #define	_SS_ALIGNSIZE	(sizeof(ntp_uint64_t))
100 #ifdef HAVE_SA_LEN_IN_STRUCT_SOCKADDR
101 #define	_SS_PAD1SIZE	(_SS_ALIGNSIZE - sizeof(u_char) - sizeof(ntp_u_int8_t))
102 #define	_SS_PAD2SIZE	(_SS_MAXSIZE - sizeof(u_char) - sizeof(ntp_u_int8_t) - \
103 				_SS_PAD1SIZE - _SS_ALIGNSIZE)
104 #else
105 #define	_SS_PAD1SIZE	(_SS_ALIGNSIZE - sizeof(short))
106 #define	_SS_PAD2SIZE	(_SS_MAXSIZE - sizeof(short) - \
107 				_SS_PAD1SIZE - _SS_ALIGNSIZE)
108 #endif /* HAVE_SA_LEN_IN_STRUCT_SOCKADDR */
109 #endif
110 
111 /*
112  * If we don't have the sockaddr_storage structure
113  * we need to define it
114  */
115 
116 #ifndef HAVE_STRUCT_SOCKADDR_STORAGE
117 struct sockaddr_storage {
118 #ifdef HAVE_SA_LEN_IN_STRUCT_SOCKADDR
119 	ntp_u_int8_t	ss_len;		/* address length */
120 	ntp_u_int8_t	ss_family;	/* address family */
121 #else
122 	short		ss_family;	/* address family */
123 #endif
124 	char		__ss_pad1[_SS_PAD1SIZE];
125 	ntp_uint64_t	__ss_align;	/* force desired structure storage alignment */
126 	char		__ss_pad2[_SS_PAD2SIZE];
127 };
128 #endif
129 
130 /*
131  * Finally if the platform doesn't support IPv6 we need some
132  * additional definitions
133  */
134 
135 /*
136  * Flag values for getaddrinfo()
137  */
138 #ifndef AI_NUMERICHOST
139 #define	AI_PASSIVE	0x00000001 /* get address to use bind() */
140 #define	AI_CANONNAME	0x00000002 /* fill ai_canonname */
141 #define	AI_NUMERICHOST	0x00000004 /* prevent name resolution */
142 /* valid flags for addrinfo */
143 #define AI_MASK \
144     (AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST | AI_ADDRCONFIG)
145 
146 #define	AI_ADDRCONFIG	0x00000400 /* only if any address is assigned */
147 #endif
148 
149 #ifndef ISC_PLATFORM_HAVEIPV6
150 /*
151  * Definition of some useful macros to handle IP6 addresses
152  */
153 #ifdef ISC_PLATFORM_NEEDIN6ADDRANY
154 #ifdef SYS_WINNT
155 #define IN6ADDR_ANY_INIT 	{{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }}
156 #else
157 #define IN6ADDR_ANY_INIT \
158 	{{{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
159 	    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }}}
160 #endif
161 #endif
162 
163 
164 /*
165  * IPv6 address
166  */
167 #ifdef SYS_WINNT
168 #define in6_addr in_addr6
169 #else
170 
171 struct in6_addr {
172 	union {
173 		ntp_u_int8_t   __u6_addr8[16];
174 		ntp_u_int16_t  __u6_addr16[8];
175 		ntp_u_int32_t  __u6_addr32[4];
176 	} __u6_addr;			/* 128-bit IP6 address */
177 };
178 
179 #define s6_addr   __u6_addr.__u6_addr8
180 #endif
181 
182 #if defined(ISC_PLATFORM_HAVEIPV6) && defined(ISC_PLATFORM_NEEDIN6ADDRANY)
183 extern const struct in6_addr in6addr_any;
184 #endif
185 
186 #define SIN6_LEN
187 #ifndef HAVE_SOCKADDR_IN6
188 struct sockaddr_in6 {
189 #ifdef HAVE_SA_LEN_IN_STRUCT_SOCKADDR
190 	ntp_u_int8_t	sin6_len;	/* length of this struct(sa_family_t)*/
191 	ntp_u_int8_t	sin6_family;	/* AF_INET6 (sa_family_t) */
192 #else
193 	short		sin6_family;	/* AF_INET6 (sa_family_t) */
194 #endif
195 	ntp_u_int16_t	sin6_port;	/* Transport layer port # (in_port_t)*/
196 	ntp_u_int32_t	sin6_flowinfo;	/* IP6 flow information */
197 	struct in6_addr	sin6_addr;	/* IP6 address */
198 	ntp_u_int32_t	sin6_scope_id;	/* scope zone index */
199 };
200 #endif
201 
202 /*
203  * Unspecified
204  */
205 #ifndef IN6_IS_ADDR_UNSPECIFIED
206 #define IN6_IS_ADDR_UNSPECIFIED(a)	\
207 	((*(const ntp_u_int32_t *)(const void *)(&(a)->s6_addr[0]) == 0) &&	\
208 	 (*(const ntp_u_int32_t *)(const void *)(&(a)->s6_addr[4]) == 0) &&	\
209 	 (*(const ntp_u_int32_t *)(const void *)(&(a)->s6_addr[8]) == 0) &&	\
210 	 (*(const ntp_u_int32_t *)(const void *)(&(a)->s6_addr[12]) == 0))
211 #endif
212 /*
213  * Multicast
214  */
215 #ifndef IN6_IS_ADDR_MULTICAST
216 #define IN6_IS_ADDR_MULTICAST(a)	((a)->s6_addr[0] == 0xff)
217 #endif
218 /*
219  * Unicast link / site local.
220  */
221 #ifndef IN6_IS_ADDR_LINKLOCAL
222 #define IN6_IS_ADDR_LINKLOCAL(a)	(\
223 (*((u_long *)((a)->s6_addr)    ) == 0xfe) && \
224 ((*((u_long *)((a)->s6_addr) + 1) & 0xc0) == 0x80))
225 #endif
226 
227 #ifndef IN6_IS_ADDR_SITELOCAL
228 #define IN6_IS_ADDR_SITELOCAL(a)	(\
229 (*((u_long *)((a)->s6_addr)    ) == 0xfe) && \
230 ((*((u_long *)((a)->s6_addr) + 1) & 0xc0) == 0xc0))
231 #endif
232 
233 struct addrinfo {
234 	int	ai_flags;	/* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
235 	int	ai_family;	/* PF_xxx */
236 	int	ai_socktype;	/* SOCK_xxx */
237 	int	ai_protocol;	/* 0 or IPPROTO_xxx for IPv4 and IPv6 */
238 	size_t	ai_addrlen;	/* length of ai_addr */
239 	char	*ai_canonname;	/* canonical name for hostname */
240 	struct	sockaddr *ai_addr;	/* binary address */
241 	struct	addrinfo *ai_next;	/* next structure in linked list */
242 };
243 
244 /*
245  * Error return codes from getaddrinfo()
246  */
247 #define	EAI_ADDRFAMILY	 1	/* address family for hostname not supported */
248 #define	EAI_AGAIN	 2	/* temporary failure in name resolution */
249 #define	EAI_BADFLAGS	 3	/* invalid value for ai_flags */
250 #define	EAI_FAIL	 4	/* non-recoverable failure in name resolution */
251 #define	EAI_FAMILY	 5	/* ai_family not supported */
252 #define	EAI_MEMORY	 6	/* memory allocation failure */
253 #define	EAI_NODATA	 7	/* no address associated with hostname */
254 #define	EAI_NONAME	 8	/* hostname nor servname provided, or not known */
255 #define	EAI_SERVICE	 9	/* servname not supported for ai_socktype */
256 #define	EAI_SOCKTYPE	10	/* ai_socktype not supported */
257 #define	EAI_SYSTEM	11	/* system error returned in errno */
258 #define	EAI_BADHINTS	12
259 #define	EAI_PROTOCOL	13
260 #define	EAI_MAX		14
261 
262 
263 int	getaddrinfo P((const char *, const char *,
264 			 const struct addrinfo *, struct addrinfo **));
265 int	getnameinfo P((const struct sockaddr *, u_int, char *,
266 			 size_t, char *, size_t, int));
267 void	freeaddrinfo P((struct addrinfo *));
268 char	*gai_strerror P((int));
269 
270 /*
271  * Constants for getnameinfo()
272  */
273 #ifndef NI_MAXHOST
274 #define	NI_MAXHOST	1025
275 #define	NI_MAXSERV	32
276 #endif
277 
278 /*
279  * Flag values for getnameinfo()
280  */
281 #ifndef NI_NUMERICHOST
282 #define	NI_NOFQDN	0x00000001
283 #define	NI_NUMERICHOST	0x00000002
284 #define	NI_NAMEREQD	0x00000004
285 #define	NI_NUMERICSERV	0x00000008
286 #define	NI_DGRAM	0x00000010
287 #define NI_WITHSCOPEID	0x00000020
288 #endif
289 
290 #endif /* ISC_PLATFORM_HAVEIPV6 */
291 
292 #endif /* !_NTP_RFC2553_H_ */
293