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