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