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