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