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" 192b15cb3dSCy Schubert 202b15cb3dSCy Schubert #ifdef __GNUC__ 212b15cb3dSCy Schubert #define NTP_PRINTF(fmt, args) __attribute__((__format__(__printf__, fmt, args))) 222b15cb3dSCy Schubert #else 232b15cb3dSCy Schubert #define NTP_PRINTF(fmt, args) 242b15cb3dSCy Schubert #endif 252b15cb3dSCy Schubert 262b15cb3dSCy Schubert extern int mprintf(const char *, ...) NTP_PRINTF(1, 2); 272b15cb3dSCy Schubert extern int mfprintf(FILE *, const char *, ...) NTP_PRINTF(2, 3); 282b15cb3dSCy Schubert extern int mvfprintf(FILE *, const char *, va_list) NTP_PRINTF(2, 0); 292b15cb3dSCy Schubert extern int mvsnprintf(char *, size_t, const char *, va_list) 302b15cb3dSCy Schubert NTP_PRINTF(3, 0); 312b15cb3dSCy Schubert extern int msnprintf(char *, size_t, const char *, ...) 322b15cb3dSCy Schubert NTP_PRINTF(3, 4); 332b15cb3dSCy Schubert extern void msyslog(int, const char *, ...) NTP_PRINTF(2, 3); 349034852cSGleb Smirnoff extern void mvsyslog(int, const char *, va_list) NTP_PRINTF(2, 0); 352b15cb3dSCy Schubert extern void init_logging (const char *, u_int32, int); 362b15cb3dSCy Schubert extern int change_logfile (const char *, int); 372b15cb3dSCy Schubert extern void setup_logfile (const char *); 382b15cb3dSCy Schubert #ifndef errno_to_str 392b15cb3dSCy Schubert extern void errno_to_str(int, char *, size_t); 402b15cb3dSCy Schubert #endif 41c0b746e5SOllivier Robert 42c0b746e5SOllivier Robert /* 432b15cb3dSCy Schubert * When building without OpenSSL, use a few macros of theirs to 442b15cb3dSCy Schubert * minimize source differences in NTP. 45c0b746e5SOllivier Robert */ 462b15cb3dSCy Schubert #ifndef OPENSSL 472b15cb3dSCy Schubert #define NID_md5 4 /* from openssl/objects.h */ 482b15cb3dSCy Schubert /* from openssl/evp.h */ 492b15cb3dSCy Schubert #define EVP_MAX_MD_SIZE 64 /* longest known is SHA512 */ 50c0b746e5SOllivier Robert #endif 51c0b746e5SOllivier Robert 522b15cb3dSCy Schubert #define SAVE_ERRNO(stmt) \ 532b15cb3dSCy Schubert { \ 542b15cb3dSCy Schubert int preserved_errno; \ 552b15cb3dSCy Schubert \ 562b15cb3dSCy Schubert preserved_errno = socket_errno(); \ 572b15cb3dSCy Schubert { \ 582b15cb3dSCy Schubert stmt \ 592b15cb3dSCy Schubert } \ 602b15cb3dSCy Schubert errno = preserved_errno; \ 612b15cb3dSCy Schubert } 622b15cb3dSCy Schubert 632b15cb3dSCy Schubert typedef void (*ctrl_c_fn)(void); 642b15cb3dSCy Schubert 652b15cb3dSCy Schubert /* authkeys.c */ 662b15cb3dSCy Schubert extern void auth_delkeys (void); 672b15cb3dSCy Schubert extern int auth_havekey (keyid_t); 68*3311ff84SXin LI extern int authdecrypt (keyid_t, u_int32 *, size_t, size_t); 69*3311ff84SXin LI extern size_t authencrypt (keyid_t, u_int32 *, size_t); 702b15cb3dSCy Schubert extern int authhavekey (keyid_t); 712b15cb3dSCy Schubert extern int authistrusted (keyid_t); 722b15cb3dSCy Schubert extern int authreadkeys (const char *); 732b15cb3dSCy Schubert extern void authtrust (keyid_t, u_long); 742b15cb3dSCy Schubert extern int authusekey (keyid_t, int, const u_char *); 752b15cb3dSCy Schubert 762b15cb3dSCy Schubert /* 772b15cb3dSCy Schubert * Based on the NTP timestamp, calculate the NTP timestamp of 782b15cb3dSCy Schubert * the corresponding calendar unit. Use the pivot time to unfold 792b15cb3dSCy Schubert * the NTP timestamp properly, or the current system time if the 802b15cb3dSCy Schubert * pivot pointer is NULL. 812b15cb3dSCy Schubert */ 822b15cb3dSCy Schubert extern u_int32 calyearstart (u_int32 ntptime, const time_t *pivot); 832b15cb3dSCy Schubert extern u_int32 calmonthstart (u_int32 ntptime, const time_t *pivot); 842b15cb3dSCy Schubert extern u_int32 calweekstart (u_int32 ntptime, const time_t *pivot); 852b15cb3dSCy Schubert extern u_int32 caldaystart (u_int32 ntptime, const time_t *pivot); 862b15cb3dSCy Schubert 872b15cb3dSCy Schubert extern const char *clockname (int); 882b15cb3dSCy Schubert extern int clocktime (int, int, int, int, int, u_int32, u_long *, u_int32 *); 892b15cb3dSCy Schubert extern int ntp_getopt (int, char **, const char *); 902b15cb3dSCy Schubert extern void init_auth (void); 912b15cb3dSCy Schubert extern void init_lib (void); 922b15cb3dSCy Schubert extern struct savekey *auth_findkey (keyid_t); 932b15cb3dSCy Schubert extern void auth_moremem (int); 942b15cb3dSCy Schubert extern void auth_prealloc_symkeys(int); 952b15cb3dSCy Schubert extern int ymd2yd (int, int, int); 962b15cb3dSCy Schubert 972b15cb3dSCy Schubert /* a_md5encrypt.c */ 98*3311ff84SXin LI extern int MD5authdecrypt (int, const u_char *, u_int32 *, size_t, size_t); 99*3311ff84SXin LI extern size_t MD5authencrypt (int, const u_char *, u_int32 *, size_t); 1002b15cb3dSCy Schubert extern void MD5auth_setkey (keyid_t, int, const u_char *, size_t); 1012b15cb3dSCy Schubert extern u_int32 addr2refid (sockaddr_u *); 1022b15cb3dSCy Schubert 1032b15cb3dSCy Schubert /* emalloc.c */ 1042b15cb3dSCy Schubert #ifndef EREALLOC_CALLSITE /* ntp_malloc.h defines */ 1052b15cb3dSCy Schubert extern void * ereallocz (void *, size_t, size_t, int); 106276da39aSCy Schubert extern void * oreallocarray (void *optr, size_t nmemb, size_t size); 107276da39aSCy Schubert #define erealloczsite(p, n, o, z, f, l) ereallocz((p), (n), (o), (z)) 108276da39aSCy Schubert #define emalloc(n) ereallocz(NULL, (n), 0, FALSE) 1092b15cb3dSCy Schubert #define emalloc_zero(c) ereallocz(NULL, (c), 0, TRUE) 110276da39aSCy Schubert #define erealloc(p, c) ereallocz((p), (c), 0, FALSE) 111276da39aSCy Schubert #define erealloc_zero(p, n, o) ereallocz((p), (n), (o), TRUE) 112276da39aSCy Schubert #define ereallocarray(p, n, s) oreallocarray((p), (n), (s)) 113276da39aSCy Schubert #define eallocarray(n, s) oreallocarray(NULL, (n), (s)) 1142b15cb3dSCy Schubert extern char * estrdup_impl(const char *); 1152b15cb3dSCy Schubert #define estrdup(s) estrdup_impl(s) 116c0b746e5SOllivier Robert #else 1172b15cb3dSCy Schubert extern void * ereallocz (void *, size_t, size_t, int, 1182b15cb3dSCy Schubert const char *, int); 119276da39aSCy Schubert extern void * oreallocarray (void *optr, size_t nmemb, size_t size, 120276da39aSCy Schubert const char *, int); 1212b15cb3dSCy Schubert #define erealloczsite ereallocz 1222b15cb3dSCy Schubert #define emalloc(c) ereallocz(NULL, (c), 0, FALSE, \ 1232b15cb3dSCy Schubert __FILE__, __LINE__) 1242b15cb3dSCy Schubert #define emalloc_zero(c) ereallocz(NULL, (c), 0, TRUE, \ 1252b15cb3dSCy Schubert __FILE__, __LINE__) 126276da39aSCy Schubert #define erealloc(p, c) ereallocz((p), (c), 0, FALSE, \ 1272b15cb3dSCy Schubert __FILE__, __LINE__) 128276da39aSCy Schubert #define erealloc_zero(p, n, o) ereallocz((p), (n), (o), TRUE, \ 129276da39aSCy Schubert __FILE__, __LINE__) 130276da39aSCy Schubert #define ereallocarray(p, n, s) oreallocarray((p), (n), (s), \ 131276da39aSCy Schubert __FILE__, __LINE__) 132276da39aSCy Schubert #define eallocarray(n, s) oreallocarray(NULL, (n), (s), \ 1332b15cb3dSCy Schubert __FILE__, __LINE__) 1342b15cb3dSCy Schubert extern char * estrdup_impl(const char *, const char *, int); 1352b15cb3dSCy Schubert #define estrdup(s) estrdup_impl((s), __FILE__, __LINE__) 136c0b746e5SOllivier Robert #endif 137c0b746e5SOllivier Robert 138c0b746e5SOllivier Robert 1392b15cb3dSCy Schubert extern int atoint (const char *, long *); 1402b15cb3dSCy Schubert extern int atouint (const char *, u_long *); 1412b15cb3dSCy Schubert extern int hextoint (const char *, u_long *); 1422b15cb3dSCy Schubert extern const char * humanlogtime (void); 1432b15cb3dSCy Schubert extern const char * humantime (time_t); 1442b15cb3dSCy Schubert extern char * mfptoa (u_int32, u_int32, short); 1452b15cb3dSCy Schubert extern char * mfptoms (u_int32, u_int32, short); 1462b15cb3dSCy Schubert extern const char * modetoa (size_t); 1472b15cb3dSCy Schubert extern const char * eventstr (int); 1482b15cb3dSCy Schubert extern const char * ceventstr (int); 1492b15cb3dSCy Schubert extern const char * res_match_flags(u_short); 1502b15cb3dSCy Schubert extern const char * res_access_flags(u_short); 1512b15cb3dSCy Schubert #ifdef KERNEL_PLL 1522b15cb3dSCy Schubert extern const char * k_st_flags (u_int32); 153c0b746e5SOllivier Robert #endif 1542b15cb3dSCy Schubert extern char * statustoa (int, int); 1552b15cb3dSCy Schubert extern sockaddr_u * netof (sockaddr_u *); 1562b15cb3dSCy Schubert extern char * numtoa (u_int32); 1572b15cb3dSCy Schubert extern char * numtohost (u_int32); 1582b15cb3dSCy Schubert extern const char * socktoa (const sockaddr_u *); 1592b15cb3dSCy Schubert extern const char * sockporttoa(const sockaddr_u *); 1602b15cb3dSCy Schubert extern u_short sock_hash (const sockaddr_u *); 1612b15cb3dSCy Schubert extern int sockaddr_masktoprefixlen(const sockaddr_u *); 1622b15cb3dSCy Schubert extern const char * socktohost (const sockaddr_u *); 1632b15cb3dSCy Schubert extern int octtoint (const char *, u_long *); 1642b15cb3dSCy Schubert extern u_long ranp2 (int); 1652b15cb3dSCy Schubert extern const char *refnumtoa (sockaddr_u *); 1662b15cb3dSCy Schubert extern const char *refid_str (u_int32, int); 167c0b746e5SOllivier Robert 1682b15cb3dSCy Schubert extern int decodenetnum (const char *, sockaddr_u *); 169c0b746e5SOllivier Robert 1702b15cb3dSCy Schubert extern const char * FindConfig (const char *); 171c0b746e5SOllivier Robert 1722b15cb3dSCy Schubert extern void signal_no_reset (int, RETSIGTYPE (*func)(int)); 1732b15cb3dSCy Schubert extern void set_ctrl_c_hook (ctrl_c_fn); 174c0b746e5SOllivier Robert 1752b15cb3dSCy Schubert extern void getauthkeys (const char *); 1762b15cb3dSCy Schubert extern void auth_agekeys (void); 1772b15cb3dSCy Schubert extern void rereadkeys (void); 178c0b746e5SOllivier Robert 179c0b746e5SOllivier Robert /* 180c0b746e5SOllivier Robert * Variable declarations for libntp. 181c0b746e5SOllivier Robert */ 182c0b746e5SOllivier Robert 183c0b746e5SOllivier Robert /* authkeys.c */ 184c0b746e5SOllivier Robert extern u_long authkeynotfound; /* keys not found */ 185c0b746e5SOllivier Robert extern u_long authkeylookups; /* calls to lookup keys */ 186c0b746e5SOllivier Robert extern u_long authnumkeys; /* number of active keys */ 187c0b746e5SOllivier Robert extern u_long authkeyexpired; /* key lifetime expirations */ 188c0b746e5SOllivier Robert extern u_long authkeyuncached; /* cache misses */ 189c0b746e5SOllivier Robert extern u_long authencryptions; /* calls to encrypt */ 190c0b746e5SOllivier Robert extern u_long authdecryptions; /* calls to decrypt */ 191c0b746e5SOllivier Robert 192c0b746e5SOllivier Robert extern int authnumfreekeys; 193c0b746e5SOllivier Robert 194c0b746e5SOllivier Robert /* 195c0b746e5SOllivier Robert * The key cache. We cache the last key we looked at here. 196c0b746e5SOllivier Robert */ 197224ba2bdSOllivier Robert extern keyid_t cache_keyid; /* key identifier */ 1982b15cb3dSCy Schubert extern int cache_type; /* key type */ 1992b15cb3dSCy Schubert extern u_char * cache_secret; /* secret */ 2002b15cb3dSCy Schubert extern u_short cache_secretsize; /* secret octets */ 2012b15cb3dSCy Schubert extern u_short cache_flags; /* KEY_ bit flags */ 202c0b746e5SOllivier Robert 203c0b746e5SOllivier Robert /* getopt.c */ 204c0b746e5SOllivier Robert extern char * ntp_optarg; /* global argument pointer */ 205c0b746e5SOllivier Robert extern int ntp_optind; /* global argv index */ 206c0b746e5SOllivier Robert 2072b15cb3dSCy Schubert /* lib_strbuf.c */ 2082b15cb3dSCy Schubert extern int ipv4_works; 2092b15cb3dSCy Schubert extern int ipv6_works; 210c0b746e5SOllivier Robert 2112b15cb3dSCy Schubert /* machines.c */ 2122b15cb3dSCy Schubert typedef void (*pset_tod_using)(const char *); 2132b15cb3dSCy Schubert extern pset_tod_using set_tod_using; 2142b15cb3dSCy Schubert 2152b15cb3dSCy Schubert /* ssl_init.c */ 2162b15cb3dSCy Schubert #ifdef OPENSSL 2172b15cb3dSCy Schubert extern void ssl_init (void); 2182b15cb3dSCy Schubert extern void ssl_check_version (void); 2192b15cb3dSCy Schubert extern int ssl_init_done; 2202b15cb3dSCy Schubert #define INIT_SSL() \ 2212b15cb3dSCy Schubert do { \ 2222b15cb3dSCy Schubert if (!ssl_init_done) \ 2232b15cb3dSCy Schubert ssl_init(); \ 2242b15cb3dSCy Schubert } while (0) 2252b15cb3dSCy Schubert #else /* !OPENSSL follows */ 2262b15cb3dSCy Schubert #define INIT_SSL() do {} while (0) 2272b15cb3dSCy Schubert #endif 2282b15cb3dSCy Schubert extern int keytype_from_text (const char *, size_t *); 2292b15cb3dSCy Schubert extern const char *keytype_name (int); 2302b15cb3dSCy Schubert extern char * getpass_keytype (int); 2312b15cb3dSCy Schubert 2322b15cb3dSCy Schubert /* strl-obsd.c */ 2332b15cb3dSCy Schubert #ifndef HAVE_STRLCPY /* + */ 2342b15cb3dSCy Schubert /* 2352b15cb3dSCy Schubert * Copy src to string dst of size siz. At most siz-1 characters 2362b15cb3dSCy Schubert * will be copied. Always NUL terminates (unless siz == 0). 2372b15cb3dSCy Schubert * Returns strlen(src); if retval >= siz, truncation occurred. 2382b15cb3dSCy Schubert */ 2392b15cb3dSCy Schubert extern size_t strlcpy(char *dst, const char *src, size_t siz); 2402b15cb3dSCy Schubert #endif 2412b15cb3dSCy Schubert #ifndef HAVE_STRLCAT /* + */ 2422b15cb3dSCy Schubert /* 2432b15cb3dSCy Schubert * Appends src to string dst of size siz (unlike strncat, siz is the 2442b15cb3dSCy Schubert * full size of dst, not space left). At most siz-1 characters 2452b15cb3dSCy Schubert * will be copied. Always NUL terminates (unless siz <= strlen(dst)). 2462b15cb3dSCy Schubert * Returns strlen(src) + MIN(siz, strlen(initial dst)). 2472b15cb3dSCy Schubert * If retval >= siz, truncation occurred. 2482b15cb3dSCy Schubert */ 2492b15cb3dSCy Schubert extern size_t strlcat(char *dst, const char *src, size_t siz); 2502b15cb3dSCy Schubert #endif 2512b15cb3dSCy Schubert 2522b15cb3dSCy Schubert 2532b15cb3dSCy Schubert 2542b15cb3dSCy Schubert /* lib/isc/win32/strerror.c 2552b15cb3dSCy Schubert * 2562b15cb3dSCy Schubert * To minimize Windows-specific changes to the rest of the NTP code, 2572b15cb3dSCy Schubert * particularly reference clocks, we hijack calls to strerror() to deal 2582b15cb3dSCy Schubert * with our mixture of error codes from the C runtime (open, write) 2592b15cb3dSCy Schubert * and Windows (sockets, serial ports). This is an ugly hack because 2602b15cb3dSCy Schubert * both use the lowest values differently, but particularly for ntpd, 2612b15cb3dSCy Schubert * it's not a problem. 2622b15cb3dSCy Schubert */ 2632b15cb3dSCy Schubert #ifdef NTP_REDEFINE_STRERROR 2642b15cb3dSCy Schubert #define strerror(e) ntp_strerror(e) 2652b15cb3dSCy Schubert extern char * ntp_strerror (int e); 266c0b746e5SOllivier Robert #endif 267c0b746e5SOllivier Robert 268c0b746e5SOllivier Robert /* systime.c */ 2692b15cb3dSCy Schubert extern double sys_tick; /* tick size or time to read */ 2702b15cb3dSCy Schubert extern double measured_tick; /* non-overridable sys_tick */ 2712b15cb3dSCy Schubert extern double sys_fuzz; /* min clock read latency */ 2722b15cb3dSCy Schubert extern int trunc_os_clock; /* sys_tick > measured_tick */ 273c0b746e5SOllivier Robert 274c0b746e5SOllivier Robert /* version.c */ 275c0b746e5SOllivier Robert extern const char *Version; /* version declaration */ 2762b15cb3dSCy Schubert 2772b15cb3dSCy Schubert #endif /* NTP_STDLIB_H */ 278