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