1 /*        $NetBSD: ntp_rfc2553.h,v 1.5 2020/05/25 20:47:20 christos Exp $       */
2 
3 /*
4  * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the project nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 /*
33  * Copyright (c) 1982, 1986, 1990, 1993
34  *        The Regents of the University of California.  All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions
38  * are met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce the above copyright
42  *    notice, this list of conditions and the following disclaimer in the
43  *    documentation and/or other materials provided with the distribution.
44  * 3. All advertising materials mentioning features or use of this software
45  *    must display the following acknowledgement:
46  *        This product includes software developed by the University of
47  *        California, Berkeley and its contributors.
48  * 4. Neither the name of the University nor the names of its contributors
49  *    may be used to endorse or promote products derived from this software
50  *    without specific prior written permission.
51  *
52  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62  * SUCH DAMAGE.
63  *
64  *        @(#)in.h  8.3 (Berkeley) 1/3/94
65  */
66 
67 /*
68  * Compatability shims with the rfc2553 API to simplify ntp.
69  */
70 #ifndef NTP_RFC2553_H
71 #define NTP_RFC2553_H
72 
73 #include <netdb.h>
74 #include <isc/net.h>
75 
76 #include "ntp_types.h"
77 #include "ntp_malloc.h"
78 
79 struct addrinfo *copy_addrinfo_impl(const struct addrinfo *
80 #ifdef EREALLOC_CALLSITE      /* from ntp_malloc.h */
81                                                                          ,
82                                             const char *, int
83 #endif
84                                                    );
85 struct addrinfo *copy_addrinfo_list_impl(const struct addrinfo *
86 #ifdef EREALLOC_CALLSITE      /* from ntp_malloc.h */
87                                                                                 ,
88                                                    const char *, int
89 #endif
90                                                    );
91 #ifdef EREALLOC_CALLSITE
92 # define copy_addrinfo(l) \
93            copy_addrinfo_impl((l), __FILE__, __LINE__)
94 # define copy_addrinfo_list(l) \
95            copy_addrinfo_list_impl((l), __FILE__, __LINE__)
96 #else
97 # define copy_addrinfo(l)     copy_addrinfo_impl(l)
98 # define copy_addrinfo_list(l)          copy_addrinfo_list_impl(l)
99 #endif
100 
101 /*
102  * If various macros are not defined we need to define them
103  */
104 
105 #ifndef AF_INET6
106 # define AF_INET6   AF_MAX
107 # define PF_INET6   AF_INET6
108 #endif
109 
110 #if !defined(_SS_MAXSIZE) && !defined(_SS_ALIGNSIZE)
111 
112 # define  _SS_MAXSIZE         128
113 # define  _SS_ALIGNSIZE       (sizeof(ntp_uint64_t))
114 # ifdef ISC_PLATFORM_HAVESALEN
115 #  define _SS_PAD1SIZE        (_SS_ALIGNSIZE - sizeof(u_char) - sizeof(ntp_u_int8_t))
116 #  define _SS_PAD2SIZE        (_SS_MAXSIZE - sizeof(u_char) - sizeof(ntp_u_int8_t) - \
117                                         _SS_PAD1SIZE - _SS_ALIGNSIZE)
118 # else
119 #  define _SS_PAD1SIZE        (_SS_ALIGNSIZE - sizeof(short))
120 #  define _SS_PAD2SIZE        (_SS_MAXSIZE - sizeof(short) - \
121                                         _SS_PAD1SIZE - _SS_ALIGNSIZE)
122 # endif /* ISC_PLATFORM_HAVESALEN */
123 #endif
124 
125 #ifndef INET6_ADDRSTRLEN
126 # define  INET6_ADDRSTRLEN    46        /* max len of IPv6 addr in ascii */
127 #endif
128 
129 /*
130  * If we don't have the sockaddr_storage structure
131  * we need to define it
132  */
133 
134 #ifndef HAVE_STRUCT_SOCKADDR_STORAGE
135 struct sockaddr_storage {
136 #ifdef ISC_PLATFORM_HAVESALEN
137           ntp_u_int8_t        ss_len;             /* address length */
138           ntp_u_int8_t        ss_family;          /* address family */
139 #else
140           short               ss_family;          /* address family */
141 #endif
142           char                __ss_pad1[_SS_PAD1SIZE];
143           ntp_uint64_t        __ss_align;         /* force desired structure storage alignment */
144           char                __ss_pad2[_SS_PAD2SIZE];
145 };
146 #endif
147 
148 /*
149  * Finally if the platform doesn't support IPv6 we need some
150  * additional definitions
151  */
152 
153 /*
154  * Flag values for getaddrinfo()
155  */
156 #ifndef AI_PASSIVE
157 #define   AI_PASSIVE          0x00000001 /* get address to use bind() */
158 #define   AI_CANONNAME        0x00000002 /* fill ai_canonname */
159 #define   AI_NUMERICHOST      0x00000004 /* prevent name resolution */
160 /* valid flags for addrinfo */
161 #define AI_MASK \
162     (AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST | AI_ADDRCONFIG)
163 
164 #define   AI_ADDRCONFIG       0x00000400 /* only if any address is assigned */
165 #endif    /* !AI_PASSIVE */
166 
167 #ifndef AI_NUMERICHOST                  /* such as AIX 4.3 */
168 # define Z_AI_NUMERICHOST     0
169 #else
170 # define Z_AI_NUMERICHOST     AI_NUMERICHOST
171 #endif
172 
173 #ifndef AI_NUMERICSERV                  /* not in RFC 2553 */
174 # define Z_AI_NUMERICSERV     0
175 #else
176 # define Z_AI_NUMERICSERV     AI_NUMERICSERV
177 #endif
178 
179 #ifndef ISC_PLATFORM_HAVEIPV6
180 
181 #ifdef SYS_WINNT
182 # define in6_addr in_addr6
183 #endif
184 
185 struct addrinfo {
186           int       ai_flags; /* AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST */
187           int       ai_family;          /* PF_xxx */
188           int       ai_socktype;        /* SOCK_xxx */
189           int       ai_protocol;        /* 0 or IPPROTO_xxx for IPv4 and IPv6 */
190           size_t    ai_addrlen;         /* length of ai_addr */
191           char      *ai_canonname;      /* canonical name for hostname */
192           struct    sockaddr *ai_addr;  /* binary address */
193           struct    addrinfo *ai_next;  /* next structure in linked list */
194 };
195 
196 /*
197  * Error return codes from getaddrinfo()
198  */
199 #define   EAI_ADDRFAMILY       1        /* address family for hostname not supported */
200 #define   EAI_AGAIN  2        /* temporary failure in name resolution */
201 #define   EAI_BADFLAGS         3        /* invalid value for ai_flags */
202 #define   EAI_FAIL   4        /* non-recoverable failure in name resolution */
203 #define   EAI_FAMILY           5        /* ai_family not supported */
204 #define   EAI_MEMORY           6        /* memory allocation failure */
205 #define   EAI_NODATA           7        /* no address associated with hostname */
206 #define   EAI_NONAME           8        /* hostname nor servname provided, or not known */
207 #define   EAI_SERVICE          9        /* servname not supported for ai_socktype */
208 #define   EAI_SOCKTYPE        10        /* ai_socktype not supported */
209 #define   EAI_SYSTEM          11        /* system error returned in errno */
210 #define   EAI_BADHINTS        12
211 #define   EAI_PROTOCOL        13
212 #define   EAI_MAX             14
213 
214 
215 int       getaddrinfo (const char *, const char *,
216                                const struct addrinfo *, struct addrinfo **);
217 int       getnameinfo (const struct sockaddr *, u_int, char *,
218                                size_t, char *, size_t, int);
219 void      freeaddrinfo (struct addrinfo *);
220 char      *gai_strerror (int);
221 
222 /*
223  * Constants for getnameinfo()
224  */
225 #ifndef NI_MAXHOST
226 #define   NI_MAXHOST          1025
227 #define   NI_MAXSERV          32
228 #endif
229 
230 /*
231  * Flag values for getnameinfo()
232  */
233 #ifndef NI_NUMERICHOST
234 #define   NI_NOFQDN 0x00000001
235 #define   NI_NUMERICHOST      0x00000002
236 #define   NI_NAMEREQD         0x00000004
237 #define   NI_NUMERICSERV      0x00000008
238 #define   NI_DGRAM  0x00000010
239 #define NI_WITHSCOPEID        0x00000020
240 #endif
241 
242 #endif /* !ISC_PLATFORM_HAVEIPV6 */
243 
244 /*
245  * Set up some macros to look for IPv6 and IPv6 multicast
246  */
247 
248 #if defined(ISC_PLATFORM_HAVEIPV6) && defined(WANT_IPV6)
249 # define INCLUDE_IPV6_SUPPORT
250 # if defined(IPV6_JOIN_GROUP) && defined(IPV6_LEAVE_GROUP)
251 #  define INCLUDE_IPV6_MULTICAST_SUPPORT
252 # endif   /* IPV6 Multicast Support */
253 #endif  /* IPv6 Support */
254 
255 #endif /* !NTP_RFC2553_H */
256