xref: /netbsd-src/external/bsd/ntp/dist/include/ntp_stdlib.h (revision b757af438b42b93f8c6571f026d8b8ef3eaf5fc9)
1 /*	$NetBSD: ntp_stdlib.h,v 1.5 2012/02/04 16:31:02 christos Exp $	*/
2 
3 /*
4  * ntp_stdlib.h - Prototypes for NTP lib.
5  */
6 #ifndef NTP_STDLIB_H
7 #define NTP_STDLIB_H
8 
9 #include <sys/types.h>
10 #ifdef HAVE_SYS_SOCKET_H
11 #include <sys/socket.h>
12 #endif
13 
14 #include "l_stdlib.h"
15 #include "ntp_rfc2553.h"
16 #include "ntp_types.h"
17 #include "ntp_malloc.h"
18 #include "ntp_string.h"
19 #include "ntp_net.h"
20 #include "ntp_syslog.h"
21 
22 
23 /*
24  * Handle gcc __attribute__ if available.
25  */
26 #ifndef __attribute__
27 /* This feature is available in gcc versions 2.5 and later.  */
28 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 5) || (defined(__STRICT_ANSI__))
29 #  define __attribute__(Spec) /* empty */
30 # endif
31 /* The __-protected variants of `format' and `printf' attributes
32    are accepted by gcc versions 2.6.4 (effectively 2.7) and later.  */
33 # if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 7)
34 #  define __format__ format
35 #  define __printf__ printf
36 # endif
37 #endif
38 
39 extern	int	mprintf(const char *, ...)
40 			__attribute__((__format__(__printf__, 1, 2)));
41 extern	int	mfprintf(FILE *, const char *, ...)
42 			__attribute__((__format__(__printf__, 2, 3)));
43 extern	int	mvfprintf(FILE *, const char *, va_list)
44 			__attribute__((__format__(__printf__, 2, 0)));
45 extern	int	mvsnprintf(char *, size_t, const char *, va_list)
46 			__attribute__((__format__(__printf__, 3, 0)));
47 extern	int	msnprintf(char *, size_t, const char *, ...)
48 			__attribute__((__format__(__printf__, 3, 4)));
49 extern	void	msyslog(int, const char *, ...)
50 				__attribute__((__format__(__printf__, 2, 3)));
51 
52 /*
53  * When building without OpenSSL, use a few macros of theirs to
54  * minimize source differences in NTP.
55  */
56 #ifndef OPENSSL
57 #define NID_md5	4	/* from openssl/objects.h */
58 /* from openssl/evp.h */
59 #define EVP_MAX_MD_SIZE	64	/* longest known is SHA512 */
60 #endif
61 
62 /* authkeys.c */
63 extern	void	auth_delkeys	(void);
64 extern	int	auth_havekey	(keyid_t);
65 extern	int	authdecrypt	(keyid_t, u_int32 *, int, int);
66 extern	int	authencrypt	(keyid_t, u_int32 *, int);
67 extern	int	authhavekey	(keyid_t);
68 extern	int	authistrusted	(keyid_t);
69 extern	int	authreadkeys	(const char *);
70 extern	void	authtrust	(keyid_t, u_long);
71 extern	int	authusekey	(keyid_t, int, const u_char *);
72 
73 extern	u_long	calyearstart	(u_long);
74 extern	const char *clockname	(int);
75 extern	int	clocktime	(int, int, int, int, int, u_long, u_long *, u_int32 *);
76 extern	int	ntp_getopt	(int, char **, const char *);
77 extern	void	init_auth	(void);
78 extern	void	init_lib	(void);
79 extern	struct savekey *auth_findkey (keyid_t);
80 extern	int	auth_moremem	(void);
81 extern	int	ymd2yd		(int, int, int);
82 
83 /* a_md5encrypt.c */
84 extern	int	MD5authdecrypt	(int, u_char *, u_int32 *, int, int);
85 extern	int	MD5authencrypt	(int, u_char *, u_int32 *, int);
86 extern	void	MD5auth_setkey	(keyid_t, int, const u_char *, size_t);
87 extern	u_int32	addr2refid	(sockaddr_u *);
88 
89 /* emalloc.c */
90 #ifndef EREALLOC_CALLSITE	/* ntp_malloc.h defines */
91 extern	void *	ereallocz	(void *, size_t, size_t, int);
92 #define	erealloczsite(p, n, o, z, f, l) ereallocz(p, n, o, (z))
93 #define	emalloc(n)		ereallocz(NULL, n, 0, FALSE)
94 #define	emalloc_zero(c)		ereallocz(NULL, (c), 0, TRUE)
95 #define	erealloc(p, c)		ereallocz(p, (c), 0, FALSE)
96 #define erealloc_zero(p, n, o)	ereallocz(p, n, (o), TRUE)
97 extern	char *	estrdup_impl	(const char *);
98 #define	estrdup(s)		estrdup_impl(s)
99 #else
100 extern	void *	ereallocz	(void *, size_t, size_t, int,
101 				 const char *, int);
102 #define erealloczsite		ereallocz
103 #define	emalloc(c)		ereallocz(NULL, (c), 0, FALSE, \
104 					  __FILE__, __LINE__)
105 #define	emalloc_zero(c)		ereallocz(NULL, (c), 0, TRUE, \
106 					  __FILE__, __LINE__)
107 #define	erealloc(p, c)		ereallocz(p, (c), 0, FALSE, \
108 					  __FILE__, __LINE__)
109 #define	erealloc_zero(p, n, o)	ereallocz(p, n, (o), TRUE, \
110 					  __FILE__, __LINE__)
111 extern	char *	estrdup_impl	(const char *, const char *, int);
112 #define	estrdup(s) estrdup_impl((s), __FILE__, __LINE__)
113 #endif
114 
115 
116 extern	int	atoint		(const char *, long *);
117 extern	int	atouint		(const char *, u_long *);
118 extern	int	hextoint	(const char *, u_long *);
119 extern	const char *	humanlogtime	(void);
120 extern	char *	inttoa		(long);
121 extern	char *	mfptoa		(u_long, u_long, short);
122 extern	char *	mfptoms		(u_long, u_long, short);
123 extern	const char * modetoa	(size_t);
124 extern  const char * eventstr	(int);
125 extern  const char * ceventstr	(int);
126 extern	char *	statustoa	(int, int);
127 extern  const char * sysstatstr (int);
128 extern  const char * peerstatstr (int);
129 extern  const char * clockstatstr (int);
130 extern	sockaddr_u * netof	(sockaddr_u *);
131 extern	char *	numtoa		(u_int32);
132 extern	char *	numtohost	(u_int32);
133 extern	char *	socktoa		(const sockaddr_u *);
134 extern	char *	socktohost	(const sockaddr_u *);
135 extern	int	octtoint	(const char *, u_long *);
136 extern	u_long	ranp2		(int);
137 extern	char *	refnumtoa	(sockaddr_u *);
138 extern	int	tsftomsu	(u_long, int);
139 extern	char *	uinttoa		(u_long);
140 
141 extern	int	decodenetnum	(const char *, sockaddr_u *);
142 
143 extern	const char * FindConfig	(const char *);
144 
145 extern	void	signal_no_reset (int, RETSIGTYPE (*func)(int));
146 
147 extern	void	getauthkeys 	(const char *);
148 extern	void	auth_agekeys	(void);
149 extern	void	rereadkeys	(void);
150 
151 /*
152  * Variable declarations for libntp.
153  */
154 
155 /*
156  * Defined by any program.
157  */
158 extern volatile int debug;		/* debugging flag */
159 
160 /* authkeys.c */
161 extern u_long	authkeynotfound;	/* keys not found */
162 extern u_long	authkeylookups;		/* calls to lookup keys */
163 extern u_long	authnumkeys;		/* number of active keys */
164 extern u_long	authkeyexpired;		/* key lifetime expirations */
165 extern u_long	authkeyuncached;	/* cache misses */
166 extern u_long	authencryptions;	/* calls to encrypt */
167 extern u_long	authdecryptions;	/* calls to decrypt */
168 
169 extern int	authnumfreekeys;
170 
171 /*
172  * The key cache. We cache the last key we looked at here.
173  */
174 extern keyid_t	cache_keyid;		/* key identifier */
175 extern u_char *	cache_key;		/* key pointer */
176 extern int	cache_type;		/* key type */
177 extern u_int	cache_keylen;		/* key length */
178 
179 /* getopt.c */
180 extern char *	ntp_optarg;		/* global argument pointer */
181 extern int	ntp_optind;		/* global argv index */
182 
183 /* lib_strbuf.c */
184 extern int	ipv4_works;
185 extern int	ipv6_works;
186 
187 /* machines.c */
188 typedef void (*pset_tod_using)(const char *);
189 extern pset_tod_using	set_tod_using;
190 
191 /* ssl_init.c */
192 #ifdef OPENSSL
193 extern	void	ssl_init		(void);
194 extern	void	ssl_check_version	(void);
195 extern	int	ssl_init_done;
196 #define	INIT_SSL()				\
197 	do {					\
198 		if (!ssl_init_done)		\
199 			ssl_init();		\
200 	} while (0)
201 #else	/* !OPENSSL follows */
202 #define	INIT_SSL()		do {} while (0)
203 #endif
204 extern	int	keytype_from_text	(const char *,	size_t *);
205 extern	const char *keytype_name	(int);
206 extern	char *	getpass_keytype		(int);
207 
208 
209 /* lib/isc/win32/strerror.c
210  *
211  * To minimize Windows-specific changes to the rest of the NTP code,
212  * particularly reference clocks, we hijack calls to strerror() to deal
213  * with our mixture of error codes from the  C runtime (open, write)
214  * and Windows (sockets, serial ports).  This is an ugly hack because
215  * both use the lowest values differently, but particularly for ntpd,
216  * it's not a problem.
217  */
218 #ifdef NTP_REDEFINE_STRERROR
219 #define	strerror(e)	ntp_strerror(e)
220 extern char *	ntp_strerror	(int e);
221 #endif
222 
223 /* systime.c */
224 extern double	sys_tick;		/* adjtime() resolution */
225 
226 /* version.c */
227 extern const char *Version;		/* version declaration */
228 
229 #endif	/* NTP_STDLIB_H */
230