xref: /netbsd-src/external/bsd/ntp/dist/include/ntp_stdlib.h (revision 53b02e147d4ed531c0d2a5ca9b3e8026ba3e99b5)
1 /*	$NetBSD: ntp_stdlib.h,v 1.17 2020/05/29 20:15:37 christos Exp $	*/
2 
3 /*
4  * ntp_stdlib.h - Prototypes for NTP lib.
5  */
6 #ifndef NTP_STDLIB_H
7 #define NTP_STDLIB_H
8 
9 #include <sys/types.h>
10 #ifdef HAVE_SYS_SOCKET_H
11 #include <sys/socket.h>
12 #endif
13 
14 #include "declcond.h"	/* ntpd uses ntpd/declcond.h, others include/ */
15 #include "l_stdlib.h"
16 #include "ntp_net.h"
17 #include "ntp_debug.h"
18 #include "ntp_malloc.h"
19 #include "ntp_string.h"
20 #include "ntp_syslog.h"
21 #include "ntp_keyacc.h"
22 
23 #ifdef __GNUC__
24 #define NTP_PRINTF(fmt, args) __attribute__((__format__(__printf__, fmt, args)))
25 #define NTP_SYSLOG(fmt, args) __attribute__((__format__(__syslog__, fmt, args)))
26 #define NTP_FORMAT_ARG(args) __attribute__((__format_arg__(args)))
27 #else
28 #define NTP_PRINTF(fmt, args)
29 #define NTP_SYSLOG(fmt, args)
30 #define NTP_FORMAT_ARG(args)
31 #endif
32 
33 extern	int	mprintf(const char *, ...) NTP_SYSLOG(1, 2);
34 extern	int	mfprintf(FILE *, const char *, ...) NTP_SYSLOG(2, 3);
35 extern	int	mvfprintf(FILE *, const char *, va_list) NTP_SYSLOG(2, 0);
36 extern	int	mvsnprintf(char *, size_t, const char *, va_list)
37 			NTP_SYSLOG(3, 0);
38 extern	int	msnprintf(char *, size_t, const char *, ...)
39 			NTP_SYSLOG(3, 4);
40 extern	void	msyslog(int, const char *, ...) NTP_SYSLOG(2, 3);
41 extern	void	mvsyslog(int, const char *, va_list) NTP_SYSLOG(2, 0);
42 extern	void	init_logging	(const char *, u_int32, int);
43 extern	int	change_logfile	(const char *, int);
44 extern	void	setup_logfile	(const char *);
45 #ifndef errno_to_str
46 extern	void	errno_to_str(int, char *, size_t);
47 #endif
48 
49 extern	int	xvsbprintf(char**, char* const, char const*, va_list) NTP_PRINTF(3, 0);
50 extern	int	xsbprintf(char**, char* const, char const*, ...) NTP_PRINTF(3, 4);
51 
52 /*
53  * When building without OpenSSL, use a few macros of theirs to
54  * minimize source differences in NTP.
55  */
56 #ifndef OPENSSL
57 #define NID_md5	4	/* from openssl/objects.h */
58 /* from openssl/evp.h */
59 #define EVP_MAX_MD_SIZE	64	/* longest known is SHA512 */
60 #endif
61 
62 #define SAVE_ERRNO(stmt)				\
63 	{						\
64 		int preserved_errno;			\
65 							\
66 		preserved_errno = socket_errno();	\
67 		{					\
68 			stmt				\
69 		}					\
70 		errno = preserved_errno;		\
71 	}
72 
73 typedef void (*ctrl_c_fn)(void);
74 
75 /* authkeys.c */
76 extern	void	auth_delkeys	(void);
77 extern	int	auth_havekey	(keyid_t);
78 extern	int	authdecrypt	(keyid_t, u_int32 *, size_t, size_t);
79 extern	size_t	authencrypt	(keyid_t, u_int32 *, size_t);
80 extern	int	authhavekey	(keyid_t);
81 extern	int	authistrusted	(keyid_t);
82 extern	int	authistrustedip	(keyid_t, sockaddr_u *);
83 extern	int	authreadkeys	(const char *);
84 extern	void	authtrust	(keyid_t, u_long);
85 extern	int	authusekey	(keyid_t, int, const u_char *);
86 
87 /*
88  * Based on the NTP timestamp, calculate the NTP timestamp of
89  * the corresponding calendar unit. Use the pivot time to unfold
90  * the NTP timestamp properly, or the current system time if the
91  * pivot pointer is NULL.
92  */
93 extern	u_int32	calyearstart	(u_int32 ntptime, const time_t *pivot);
94 extern	u_int32	calmonthstart	(u_int32 ntptime, const time_t *pivot);
95 extern	u_int32	calweekstart	(u_int32 ntptime, const time_t *pivot);
96 extern	u_int32	caldaystart	(u_int32 ntptime, const time_t *pivot);
97 
98 extern	const char *clockname	(int);
99 extern	int	clocktime	(int, int, int, int, int, u_int32, u_long *, u_int32 *);
100 extern	int	ntp_getopt	(int, char **, const char *);
101 extern	void	init_auth	(void);
102 extern	void	init_lib	(void);
103 extern	struct savekey *auth_findkey (keyid_t);
104 extern	void	auth_moremem	(int);
105 extern	void	auth_prealloc_symkeys(int);
106 extern	int	ymd2yd		(int, int, int);
107 
108 /* a_md5encrypt.c */
109 extern	int	MD5authdecrypt	(int, const u_char *, size_t, u_int32 *, size_t, size_t);
110 extern	size_t	MD5authencrypt	(int, const u_char *, size_t, u_int32 *, size_t);
111 extern	void	MD5auth_setkey	(keyid_t, int, const u_char *, size_t, KeyAccT *c);
112 extern	u_int32	addr2refid	(sockaddr_u *);
113 
114 /* emalloc.c */
115 #ifndef EREALLOC_CALLSITE	/* ntp_malloc.h defines */
116 extern	void *	ereallocz	(void *, size_t, size_t, int);
117 extern	void *	oreallocarrayxz	(void *optr, size_t nmemb, size_t size, size_t extra);
118 #define	erealloczsite(p, n, o, z, f, l) ereallocz((p), (n), (o), (z))
119 #define	emalloc(n)		ereallocz(NULL, (n), 0, FALSE)
120 #define	emalloc_zero(c)		ereallocz(NULL, (c), 0, TRUE)
121 #define	erealloc(p, c)		ereallocz((p), (c), 0, FALSE)
122 #define erealloc_zero(p, n, o)	ereallocz((p), (n), (o), TRUE)
123 #define ereallocarray(p, n, s)	oreallocarrayxz((p), (n), (s), 0)
124 #define eallocarray(n, s)	oreallocarrayxz(NULL, (n), (s), 0)
125 #define ereallocarrayxz(p, n, s, x)	oreallocarrayxz((p), (n), (s), (x))
126 #define eallocarrayxz(n, s, x)	oreallocarrayxz(NULL, (n), (s), (x))
127 extern	char *	estrdup_impl(const char *);
128 #define	estrdup(s)		estrdup_impl(s)
129 #else
130 extern	void *	ereallocz	(void *, size_t, size_t, int,
131 				 const char *, int);
132 extern	void *	oreallocarrayxz	(void *optr, size_t nmemb, size_t size,
133 				 size_t extra, const char *, int);
134 #define erealloczsite		ereallocz
135 #define	emalloc(c)		ereallocz(NULL, (c), 0, FALSE, \
136 					  __FILE__, __LINE__)
137 #define	emalloc_zero(c)		ereallocz(NULL, (c), 0, TRUE, \
138 					  __FILE__, __LINE__)
139 #define	erealloc(p, c)		ereallocz((p), (c), 0, FALSE, \
140 					  __FILE__, __LINE__)
141 #define	erealloc_zero(p, n, o)	ereallocz((p), (n), (o), TRUE, \
142 					  __FILE__, __LINE__)
143 #define ereallocarray(p, n, s)	oreallocarrayxz((p), (n), (s), 0, \
144 					  __FILE__, __LINE__)
145 #define eallocarray(n, s)	oreallocarrayxz(NULL, (n), (s), 0, \
146 					  __FILE__, __LINE__)
147 #define ereallocarrayxz(p, n, s, x)	oreallocarrayxz((p), (n), (s), (x), \
148 					  __FILE__, __LINE__)
149 #define eallocarrayxz(n, s, x)	oreallocarrayxz(NULL, (n), (s), (x), \
150 					  __FILE__, __LINE__)
151 extern	char *	estrdup_impl(const char *, const char *, int);
152 #define	estrdup(s) estrdup_impl((s), __FILE__, __LINE__)
153 #endif
154 
155 
156 extern	int	atoint		(const char *, long *);
157 extern	int	atouint		(const char *, u_long *);
158 extern	int	hextoint	(const char *, u_long *);
159 extern	const char *	humanlogtime	(void);
160 extern	const char *	humantime	(time_t);
161 extern int	is_ip_address	(const char *, u_short, sockaddr_u *);
162 extern	char *	mfptoa		(u_int32, u_int32, short);
163 extern	char *	mfptoms		(u_int32, u_int32, short);
164 extern	const char * modetoa	(size_t);
165 extern	const char * eventstr	(int);
166 extern	const char * ceventstr	(int);
167 extern	const char * res_match_flags(u_short);
168 extern	const char * res_access_flags(u_int32);
169 #ifdef KERNEL_PLL
170 extern	const char * k_st_flags	(u_int32);
171 #endif
172 extern	char *	statustoa	(int, int);
173 extern	sockaddr_u * netof	(sockaddr_u *);
174 extern	char *	numtoa		(u_int32);
175 extern	char *	numtohost	(u_int32);
176 extern	const char * socktoa	(const sockaddr_u *);
177 extern	const char * sockporttoa(const sockaddr_u *);
178 extern	u_short	sock_hash	(const sockaddr_u *);
179 extern	int	sockaddr_masktoprefixlen(const sockaddr_u *);
180 extern	const char * socktohost	(const sockaddr_u *);
181 extern	int	octtoint	(const char *, u_long *);
182 extern	u_long	ranp2		(int);
183 extern	const char *refnumtoa	(sockaddr_u *);
184 extern	const char *refid_str	(u_int32, int);
185 
186 extern	int	decodenetnum	(const char *, sockaddr_u *);
187 
188 extern	const char * FindConfig	(const char *);
189 
190 extern	void	signal_no_reset (int, RETSIGTYPE (*func)(int));
191 extern	void	set_ctrl_c_hook (ctrl_c_fn);
192 
193 extern	void	getauthkeys 	(const char *);
194 extern	void	auth_agekeys	(void);
195 extern	void	rereadkeys	(void);
196 
197 /*
198  * Variable declarations for libntp.
199  */
200 
201 /* authkeys.c */
202 extern u_long	authkeynotfound;	/* keys not found */
203 extern u_long	authkeylookups;		/* calls to lookup keys */
204 extern u_long	authnumkeys;		/* number of active keys */
205 extern u_long	authkeyexpired;		/* key lifetime expirations */
206 extern u_long	authkeyuncached;	/* cache misses */
207 extern u_long	authencryptions;	/* calls to encrypt */
208 extern u_long	authdecryptions;	/* calls to decrypt */
209 
210 extern int	authnumfreekeys;
211 
212 /*
213  * The key cache. We cache the last key we looked at here.
214  */
215 extern keyid_t	cache_keyid;		/* key identifier */
216 extern int	cache_type;		/* key type */
217 extern u_char *	cache_secret;		/* secret */
218 extern size_t	cache_secretsize;	/* secret octets */
219 extern u_short	cache_flags;		/* KEY_ bit flags */
220 
221 /* getopt.c */
222 extern char *	ntp_optarg;		/* global argument pointer */
223 extern int	ntp_optind;		/* global argv index */
224 
225 /* lib_strbuf.c */
226 extern int	ipv4_works;
227 extern int	ipv6_works;
228 
229 /* machines.c */
230 typedef void (*pset_tod_using)(const char *);
231 extern pset_tod_using	set_tod_using;
232 
233 /* ssl_init.c */
234 #ifdef OPENSSL
235 extern	void	ssl_init		(void);
236 extern	void	ssl_check_version	(void);
237 extern	int	ssl_init_done;
238 #define	INIT_SSL()				\
239 	do {					\
240 		if (!ssl_init_done)		\
241 			ssl_init();		\
242 	} while (0)
243 #else	/* !OPENSSL follows */
244 #define	INIT_SSL()		do {} while (0)
245 #endif
246 extern	int	keytype_from_text	(const char *,	size_t *);
247 extern	const char *keytype_name	(int);
248 extern	char *	getpass_keytype		(int);
249 
250 /* strl-obsd.c */
251 #ifndef HAVE_STRLCPY		/* + */
252 /*
253  * Copy src to string dst of size siz.  At most siz-1 characters
254  * will be copied.  Always NUL terminates (unless siz == 0).
255  * Returns strlen(src); if retval >= siz, truncation occurred.
256  */
257 extern	size_t	strlcpy(char *dst, const char *src, size_t siz);
258 #endif
259 #ifndef HAVE_STRLCAT		/* + */
260 /*
261  * Appends src to string dst of size siz (unlike strncat, siz is the
262  * full size of dst, not space left).  At most siz-1 characters
263  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
264  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
265  * If retval >= siz, truncation occurred.
266  */
267 extern	size_t	strlcat(char *dst, const char *src, size_t siz);
268 #endif
269 
270 
271 
272 /* lib/isc/win32/strerror.c
273  *
274  * To minimize Windows-specific changes to the rest of the NTP code,
275  * particularly reference clocks, we hijack calls to strerror() to deal
276  * with our mixture of error codes from the  C runtime (open, write)
277  * and Windows (sockets, serial ports).  This is an ugly hack because
278  * both use the lowest values differently, but particularly for ntpd,
279  * it's not a problem.
280  */
281 #ifdef NTP_REDEFINE_STRERROR
282 #define	strerror(e)	ntp_strerror(e)
283 extern char *	ntp_strerror	(int e);
284 #endif
285 
286 /* systime.c */
287 extern double	sys_tick;		/* tick size or time to read */
288 extern double	measured_tick;		/* non-overridable sys_tick */
289 extern double	sys_fuzz;		/* min clock read latency */
290 extern int	trunc_os_clock;		/* sys_tick > measured_tick */
291 
292 /* version.c */
293 extern const char *Version;		/* version declaration */
294 
295 #endif	/* NTP_STDLIB_H */
296