xref: /freebsd/contrib/ntp/include/ntp_stdlib.h (revision f0574f5c)
1c0b746e5SOllivier Robert /*
2c0b746e5SOllivier Robert  * ntp_stdlib.h - Prototypes for NTP lib.
3c0b746e5SOllivier Robert  */
42b15cb3dSCy Schubert #ifndef NTP_STDLIB_H
52b15cb3dSCy Schubert #define NTP_STDLIB_H
6c0b746e5SOllivier Robert 
72b15cb3dSCy Schubert #include <sys/types.h>
82b15cb3dSCy Schubert #ifdef HAVE_SYS_SOCKET_H
92b15cb3dSCy Schubert #include <sys/socket.h>
102b15cb3dSCy Schubert #endif
112b15cb3dSCy Schubert 
122b15cb3dSCy Schubert #include "declcond.h"	/* ntpd uses ntpd/declcond.h, others include/ */
13c0b746e5SOllivier Robert #include "l_stdlib.h"
142b15cb3dSCy Schubert #include "ntp_net.h"
152b15cb3dSCy Schubert #include "ntp_debug.h"
162b15cb3dSCy Schubert #include "ntp_malloc.h"
172b15cb3dSCy Schubert #include "ntp_string.h"
182b15cb3dSCy Schubert #include "ntp_syslog.h"
1968ba7e87SXin LI #include "ntp_keyacc.h"
202b15cb3dSCy Schubert 
212b15cb3dSCy Schubert #ifdef __GNUC__
222b15cb3dSCy Schubert #define NTP_PRINTF(fmt, args) __attribute__((__format__(__printf__, fmt, args)))
232b15cb3dSCy Schubert #else
242b15cb3dSCy Schubert #define NTP_PRINTF(fmt, args)
252b15cb3dSCy Schubert #endif
262b15cb3dSCy Schubert 
272b15cb3dSCy Schubert extern	int	mprintf(const char *, ...) NTP_PRINTF(1, 2);
282b15cb3dSCy Schubert extern	int	mfprintf(FILE *, const char *, ...) NTP_PRINTF(2, 3);
292b15cb3dSCy Schubert extern	int	mvfprintf(FILE *, const char *, va_list) NTP_PRINTF(2, 0);
302b15cb3dSCy Schubert extern	int	mvsnprintf(char *, size_t, const char *, va_list)
312b15cb3dSCy Schubert 			NTP_PRINTF(3, 0);
322b15cb3dSCy Schubert extern	int	msnprintf(char *, size_t, const char *, ...)
332b15cb3dSCy Schubert 			NTP_PRINTF(3, 4);
342b15cb3dSCy Schubert extern	void	msyslog(int, const char *, ...) NTP_PRINTF(2, 3);
359034852cSGleb Smirnoff extern	void	mvsyslog(int, const char *, va_list) NTP_PRINTF(2, 0);
362b15cb3dSCy Schubert extern	void	init_logging	(const char *, u_int32, int);
372b15cb3dSCy Schubert extern	int	change_logfile	(const char *, int);
382b15cb3dSCy Schubert extern	void	setup_logfile	(const char *);
392b15cb3dSCy Schubert #ifndef errno_to_str
402b15cb3dSCy Schubert extern	void	errno_to_str(int, char *, size_t);
412b15cb3dSCy Schubert #endif
42c0b746e5SOllivier Robert 
43c0b746e5SOllivier Robert /*
442b15cb3dSCy Schubert  * When building without OpenSSL, use a few macros of theirs to
452b15cb3dSCy Schubert  * minimize source differences in NTP.
46c0b746e5SOllivier Robert  */
472b15cb3dSCy Schubert #ifndef OPENSSL
482b15cb3dSCy Schubert #define NID_md5	4	/* from openssl/objects.h */
492b15cb3dSCy Schubert /* from openssl/evp.h */
502b15cb3dSCy Schubert #define EVP_MAX_MD_SIZE	64	/* longest known is SHA512 */
51c0b746e5SOllivier Robert #endif
52c0b746e5SOllivier Robert 
532b15cb3dSCy Schubert #define SAVE_ERRNO(stmt)				\
542b15cb3dSCy Schubert 	{						\
552b15cb3dSCy Schubert 		int preserved_errno;			\
562b15cb3dSCy Schubert 							\
572b15cb3dSCy Schubert 		preserved_errno = socket_errno();	\
582b15cb3dSCy Schubert 		{					\
592b15cb3dSCy Schubert 			stmt				\
602b15cb3dSCy Schubert 		}					\
612b15cb3dSCy Schubert 		errno = preserved_errno;		\
622b15cb3dSCy Schubert 	}
632b15cb3dSCy Schubert 
642b15cb3dSCy Schubert typedef void (*ctrl_c_fn)(void);
652b15cb3dSCy Schubert 
662b15cb3dSCy Schubert /* authkeys.c */
672b15cb3dSCy Schubert extern	void	auth_delkeys	(void);
682b15cb3dSCy Schubert extern	int	auth_havekey	(keyid_t);
693311ff84SXin LI extern	int	authdecrypt	(keyid_t, u_int32 *, size_t, size_t);
703311ff84SXin LI extern	size_t	authencrypt	(keyid_t, u_int32 *, size_t);
712b15cb3dSCy Schubert extern	int	authhavekey	(keyid_t);
722b15cb3dSCy Schubert extern	int	authistrusted	(keyid_t);
7368ba7e87SXin LI extern	int	authistrustedip	(keyid_t, sockaddr_u *);
742b15cb3dSCy Schubert extern	int	authreadkeys	(const char *);
752b15cb3dSCy Schubert extern	void	authtrust	(keyid_t, u_long);
762b15cb3dSCy Schubert extern	int	authusekey	(keyid_t, int, const u_char *);
772b15cb3dSCy Schubert 
782b15cb3dSCy Schubert /*
792b15cb3dSCy Schubert  * Based on the NTP timestamp, calculate the NTP timestamp of
802b15cb3dSCy Schubert  * the corresponding calendar unit. Use the pivot time to unfold
812b15cb3dSCy Schubert  * the NTP timestamp properly, or the current system time if the
822b15cb3dSCy Schubert  * pivot pointer is NULL.
832b15cb3dSCy Schubert  */
842b15cb3dSCy Schubert extern	u_int32	calyearstart	(u_int32 ntptime, const time_t *pivot);
852b15cb3dSCy Schubert extern	u_int32	calmonthstart	(u_int32 ntptime, const time_t *pivot);
862b15cb3dSCy Schubert extern	u_int32	calweekstart	(u_int32 ntptime, const time_t *pivot);
872b15cb3dSCy Schubert extern	u_int32	caldaystart	(u_int32 ntptime, const time_t *pivot);
882b15cb3dSCy Schubert 
892b15cb3dSCy Schubert extern	const char *clockname	(int);
902b15cb3dSCy Schubert extern	int	clocktime	(int, int, int, int, int, u_int32, u_long *, u_int32 *);
912b15cb3dSCy Schubert extern	int	ntp_getopt	(int, char **, const char *);
922b15cb3dSCy Schubert extern	void	init_auth	(void);
932b15cb3dSCy Schubert extern	void	init_lib	(void);
942b15cb3dSCy Schubert extern	struct savekey *auth_findkey (keyid_t);
952b15cb3dSCy Schubert extern	void	auth_moremem	(int);
962b15cb3dSCy Schubert extern	void	auth_prealloc_symkeys(int);
972b15cb3dSCy Schubert extern	int	ymd2yd		(int, int, int);
982b15cb3dSCy Schubert 
992b15cb3dSCy Schubert /* a_md5encrypt.c */
1003311ff84SXin LI extern	int	MD5authdecrypt	(int, const u_char *, u_int32 *, size_t, size_t);
1013311ff84SXin LI extern	size_t	MD5authencrypt	(int, const u_char *, u_int32 *, size_t);
10268ba7e87SXin LI extern	void	MD5auth_setkey	(keyid_t, int, const u_char *, size_t, KeyAccT *c);
1032b15cb3dSCy Schubert extern	u_int32	addr2refid	(sockaddr_u *);
1042b15cb3dSCy Schubert 
1052b15cb3dSCy Schubert /* emalloc.c */
1062b15cb3dSCy Schubert #ifndef EREALLOC_CALLSITE	/* ntp_malloc.h defines */
1072b15cb3dSCy Schubert extern	void *	ereallocz	(void *, size_t, size_t, int);
108f0574f5cSXin LI extern	void *	oreallocarrayxz	(void *optr, size_t nmemb, size_t size, size_t extra);
109276da39aSCy Schubert #define	erealloczsite(p, n, o, z, f, l) ereallocz((p), (n), (o), (z))
110276da39aSCy Schubert #define	emalloc(n)		ereallocz(NULL, (n), 0, FALSE)
1112b15cb3dSCy Schubert #define	emalloc_zero(c)		ereallocz(NULL, (c), 0, TRUE)
112276da39aSCy Schubert #define	erealloc(p, c)		ereallocz((p), (c), 0, FALSE)
113276da39aSCy Schubert #define erealloc_zero(p, n, o)	ereallocz((p), (n), (o), TRUE)
114f0574f5cSXin LI #define ereallocarray(p, n, s)	oreallocarrayxz((p), (n), (s), 0)
115f0574f5cSXin LI #define eallocarray(n, s)	oreallocarrayxz(NULL, (n), (s), 0)
116f0574f5cSXin LI #define ereallocarrayxz(p, n, s, x)	oreallocarrayxz((p), (n), (s), (x))
117f0574f5cSXin LI #define eallocarrayxz(n, s, x)	oreallocarrayxz(NULL, (n), (s), (x))
1182b15cb3dSCy Schubert extern	char *	estrdup_impl(const char *);
1192b15cb3dSCy Schubert #define	estrdup(s)		estrdup_impl(s)
120c0b746e5SOllivier Robert #else
1212b15cb3dSCy Schubert extern	void *	ereallocz	(void *, size_t, size_t, int,
1222b15cb3dSCy Schubert 				 const char *, int);
123f0574f5cSXin LI extern	void *	oreallocarrayxz	(void *optr, size_t nmemb, size_t size,
124f0574f5cSXin LI 				 size_t extra, const char *, int);
1252b15cb3dSCy Schubert #define erealloczsite		ereallocz
1262b15cb3dSCy Schubert #define	emalloc(c)		ereallocz(NULL, (c), 0, FALSE, \
1272b15cb3dSCy Schubert 					  __FILE__, __LINE__)
1282b15cb3dSCy Schubert #define	emalloc_zero(c)		ereallocz(NULL, (c), 0, TRUE, \
1292b15cb3dSCy Schubert 					  __FILE__, __LINE__)
130276da39aSCy Schubert #define	erealloc(p, c)		ereallocz((p), (c), 0, FALSE, \
1312b15cb3dSCy Schubert 					  __FILE__, __LINE__)
132276da39aSCy Schubert #define	erealloc_zero(p, n, o)	ereallocz((p), (n), (o), TRUE, \
133276da39aSCy Schubert 					  __FILE__, __LINE__)
134f0574f5cSXin LI #define ereallocarray(p, n, s)	oreallocarrayxz((p), (n), (s), 0, \
135276da39aSCy Schubert 					  __FILE__, __LINE__)
136f0574f5cSXin LI #define eallocarray(n, s)	oreallocarrayxz(NULL, (n), (s), 0, \
137f0574f5cSXin LI 					  __FILE__, __LINE__)
138f0574f5cSXin LI #define ereallocarrayxz(p, n, s, x)	oreallocarrayxz((p), (n), (s), (x), \
139f0574f5cSXin LI 					  __FILE__, __LINE__)
140f0574f5cSXin LI #define eallocarrayxz(n, s, x)	oreallocarrayxz(NULL, (n), (s), (x), \
1412b15cb3dSCy Schubert 					  __FILE__, __LINE__)
1422b15cb3dSCy Schubert extern	char *	estrdup_impl(const char *, const char *, int);
1432b15cb3dSCy Schubert #define	estrdup(s) estrdup_impl((s), __FILE__, __LINE__)
144c0b746e5SOllivier Robert #endif
145c0b746e5SOllivier Robert 
146c0b746e5SOllivier Robert 
1472b15cb3dSCy Schubert extern	int	atoint		(const char *, long *);
1482b15cb3dSCy Schubert extern	int	atouint		(const char *, u_long *);
1492b15cb3dSCy Schubert extern	int	hextoint	(const char *, u_long *);
1502b15cb3dSCy Schubert extern	const char *	humanlogtime	(void);
1512b15cb3dSCy Schubert extern	const char *	humantime	(time_t);
15268ba7e87SXin LI extern int	is_ip_address	(const char *, u_short, sockaddr_u *);
1532b15cb3dSCy Schubert extern	char *	mfptoa		(u_int32, u_int32, short);
1542b15cb3dSCy Schubert extern	char *	mfptoms		(u_int32, u_int32, short);
1552b15cb3dSCy Schubert extern	const char * modetoa	(size_t);
1562b15cb3dSCy Schubert extern	const char * eventstr	(int);
1572b15cb3dSCy Schubert extern	const char * ceventstr	(int);
1582b15cb3dSCy Schubert extern	const char * res_match_flags(u_short);
1592b15cb3dSCy Schubert extern	const char * res_access_flags(u_short);
1602b15cb3dSCy Schubert #ifdef KERNEL_PLL
1612b15cb3dSCy Schubert extern	const char * k_st_flags	(u_int32);
162c0b746e5SOllivier Robert #endif
1632b15cb3dSCy Schubert extern	char *	statustoa	(int, int);
1642b15cb3dSCy Schubert extern	sockaddr_u * netof	(sockaddr_u *);
1652b15cb3dSCy Schubert extern	char *	numtoa		(u_int32);
1662b15cb3dSCy Schubert extern	char *	numtohost	(u_int32);
1672b15cb3dSCy Schubert extern	const char * socktoa	(const sockaddr_u *);
1682b15cb3dSCy Schubert extern	const char * sockporttoa(const sockaddr_u *);
1692b15cb3dSCy Schubert extern	u_short	sock_hash	(const sockaddr_u *);
1702b15cb3dSCy Schubert extern	int	sockaddr_masktoprefixlen(const sockaddr_u *);
1712b15cb3dSCy Schubert extern	const char * socktohost	(const sockaddr_u *);
1722b15cb3dSCy Schubert extern	int	octtoint	(const char *, u_long *);
1732b15cb3dSCy Schubert extern	u_long	ranp2		(int);
1742b15cb3dSCy Schubert extern	const char *refnumtoa	(sockaddr_u *);
1752b15cb3dSCy Schubert extern	const char *refid_str	(u_int32, int);
176c0b746e5SOllivier Robert 
1772b15cb3dSCy Schubert extern	int	decodenetnum	(const char *, sockaddr_u *);
178c0b746e5SOllivier Robert 
1792b15cb3dSCy Schubert extern	const char * FindConfig	(const char *);
180c0b746e5SOllivier Robert 
1812b15cb3dSCy Schubert extern	void	signal_no_reset (int, RETSIGTYPE (*func)(int));
1822b15cb3dSCy Schubert extern	void	set_ctrl_c_hook (ctrl_c_fn);
183c0b746e5SOllivier Robert 
1842b15cb3dSCy Schubert extern	void	getauthkeys 	(const char *);
1852b15cb3dSCy Schubert extern	void	auth_agekeys	(void);
1862b15cb3dSCy Schubert extern	void	rereadkeys	(void);
187c0b746e5SOllivier Robert 
188c0b746e5SOllivier Robert /*
189c0b746e5SOllivier Robert  * Variable declarations for libntp.
190c0b746e5SOllivier Robert  */
191c0b746e5SOllivier Robert 
192c0b746e5SOllivier Robert /* authkeys.c */
193c0b746e5SOllivier Robert extern u_long	authkeynotfound;	/* keys not found */
194c0b746e5SOllivier Robert extern u_long	authkeylookups;		/* calls to lookup keys */
195c0b746e5SOllivier Robert extern u_long	authnumkeys;		/* number of active keys */
196c0b746e5SOllivier Robert extern u_long	authkeyexpired;		/* key lifetime expirations */
197c0b746e5SOllivier Robert extern u_long	authkeyuncached;	/* cache misses */
198c0b746e5SOllivier Robert extern u_long	authencryptions;	/* calls to encrypt */
199c0b746e5SOllivier Robert extern u_long	authdecryptions;	/* calls to decrypt */
200c0b746e5SOllivier Robert 
201c0b746e5SOllivier Robert extern int	authnumfreekeys;
202c0b746e5SOllivier Robert 
203c0b746e5SOllivier Robert /*
204c0b746e5SOllivier Robert  * The key cache. We cache the last key we looked at here.
205c0b746e5SOllivier Robert  */
206224ba2bdSOllivier Robert extern keyid_t	cache_keyid;		/* key identifier */
2072b15cb3dSCy Schubert extern int	cache_type;		/* key type */
2082b15cb3dSCy Schubert extern u_char *	cache_secret;		/* secret */
2094990d495SXin LI extern size_t	cache_secretsize;	/* secret octets */
2102b15cb3dSCy Schubert extern u_short	cache_flags;		/* KEY_ bit flags */
211c0b746e5SOllivier Robert 
212c0b746e5SOllivier Robert /* getopt.c */
213c0b746e5SOllivier Robert extern char *	ntp_optarg;		/* global argument pointer */
214c0b746e5SOllivier Robert extern int	ntp_optind;		/* global argv index */
215c0b746e5SOllivier Robert 
2162b15cb3dSCy Schubert /* lib_strbuf.c */
2172b15cb3dSCy Schubert extern int	ipv4_works;
2182b15cb3dSCy Schubert extern int	ipv6_works;
219c0b746e5SOllivier Robert 
2202b15cb3dSCy Schubert /* machines.c */
2212b15cb3dSCy Schubert typedef void (*pset_tod_using)(const char *);
2222b15cb3dSCy Schubert extern pset_tod_using	set_tod_using;
2232b15cb3dSCy Schubert 
2242b15cb3dSCy Schubert /* ssl_init.c */
2252b15cb3dSCy Schubert #ifdef OPENSSL
2262b15cb3dSCy Schubert extern	void	ssl_init		(void);
2272b15cb3dSCy Schubert extern	void	ssl_check_version	(void);
2282b15cb3dSCy Schubert extern	int	ssl_init_done;
2292b15cb3dSCy Schubert #define	INIT_SSL()				\
2302b15cb3dSCy Schubert 	do {					\
2312b15cb3dSCy Schubert 		if (!ssl_init_done)		\
2322b15cb3dSCy Schubert 			ssl_init();		\
2332b15cb3dSCy Schubert 	} while (0)
2342b15cb3dSCy Schubert #else	/* !OPENSSL follows */
2352b15cb3dSCy Schubert #define	INIT_SSL()		do {} while (0)
2362b15cb3dSCy Schubert #endif
2372b15cb3dSCy Schubert extern	int	keytype_from_text	(const char *,	size_t *);
2382b15cb3dSCy Schubert extern	const char *keytype_name	(int);
2392b15cb3dSCy Schubert extern	char *	getpass_keytype		(int);
2402b15cb3dSCy Schubert 
2412b15cb3dSCy Schubert /* strl-obsd.c */
2422b15cb3dSCy Schubert #ifndef HAVE_STRLCPY		/* + */
2432b15cb3dSCy Schubert /*
2442b15cb3dSCy Schubert  * Copy src to string dst of size siz.  At most siz-1 characters
2452b15cb3dSCy Schubert  * will be copied.  Always NUL terminates (unless siz == 0).
2462b15cb3dSCy Schubert  * Returns strlen(src); if retval >= siz, truncation occurred.
2472b15cb3dSCy Schubert  */
2482b15cb3dSCy Schubert extern	size_t	strlcpy(char *dst, const char *src, size_t siz);
2492b15cb3dSCy Schubert #endif
2502b15cb3dSCy Schubert #ifndef HAVE_STRLCAT		/* + */
2512b15cb3dSCy Schubert /*
2522b15cb3dSCy Schubert  * Appends src to string dst of size siz (unlike strncat, siz is the
2532b15cb3dSCy Schubert  * full size of dst, not space left).  At most siz-1 characters
2542b15cb3dSCy Schubert  * will be copied.  Always NUL terminates (unless siz <= strlen(dst)).
2552b15cb3dSCy Schubert  * Returns strlen(src) + MIN(siz, strlen(initial dst)).
2562b15cb3dSCy Schubert  * If retval >= siz, truncation occurred.
2572b15cb3dSCy Schubert  */
2582b15cb3dSCy Schubert extern	size_t	strlcat(char *dst, const char *src, size_t siz);
2592b15cb3dSCy Schubert #endif
2602b15cb3dSCy Schubert 
2612b15cb3dSCy Schubert 
2622b15cb3dSCy Schubert 
2632b15cb3dSCy Schubert /* lib/isc/win32/strerror.c
2642b15cb3dSCy Schubert  *
2652b15cb3dSCy Schubert  * To minimize Windows-specific changes to the rest of the NTP code,
2662b15cb3dSCy Schubert  * particularly reference clocks, we hijack calls to strerror() to deal
2672b15cb3dSCy Schubert  * with our mixture of error codes from the  C runtime (open, write)
2682b15cb3dSCy Schubert  * and Windows (sockets, serial ports).  This is an ugly hack because
2692b15cb3dSCy Schubert  * both use the lowest values differently, but particularly for ntpd,
2702b15cb3dSCy Schubert  * it's not a problem.
2712b15cb3dSCy Schubert  */
2722b15cb3dSCy Schubert #ifdef NTP_REDEFINE_STRERROR
2732b15cb3dSCy Schubert #define	strerror(e)	ntp_strerror(e)
2742b15cb3dSCy Schubert extern char *	ntp_strerror	(int e);
275c0b746e5SOllivier Robert #endif
276c0b746e5SOllivier Robert 
277c0b746e5SOllivier Robert /* systime.c */
2782b15cb3dSCy Schubert extern double	sys_tick;		/* tick size or time to read */
2792b15cb3dSCy Schubert extern double	measured_tick;		/* non-overridable sys_tick */
2802b15cb3dSCy Schubert extern double	sys_fuzz;		/* min clock read latency */
2812b15cb3dSCy Schubert extern int	trunc_os_clock;		/* sys_tick > measured_tick */
282c0b746e5SOllivier Robert 
283c0b746e5SOllivier Robert /* version.c */
284c0b746e5SOllivier Robert extern const char *Version;		/* version declaration */
2852b15cb3dSCy Schubert 
2862b15cb3dSCy Schubert #endif	/* NTP_STDLIB_H */
287