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