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