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