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