Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

359 lines
8.9 KiB

18 years ago
  1. /* $OpenBSD: ntpd.h,v 1.109 2014/01/22 02:55:15 benno Exp $ */
  2. /*
  3. * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
  4. * Copyright (c) 2012 Mike Miller <mmiller@mgm51.com>
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  15. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  16. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <sys/types.h>
  19. #include <sys/uio.h>
  20. #include <sys/socket.h>
  21. #include <sys/queue.h>
  22. #include <sys/time.h>
  23. #include <netinet/in.h>
  24. #include <netinet/in_systm.h>
  25. #include <netinet/ip.h>
  26. #include <arpa/inet.h>
  27. #include <netdb.h>
  28. #include <pwd.h>
  29. #include <stdarg.h>
  30. #include <event.h>
  31. #include <poll.h>
  32. #include "ntp.h"
  33. #include <imsg.h>
  34. #define NTPD_USER "_ntp"
  35. #define CONFFILE "/etc/ntpd.conf"
  36. #define DRIFTFILE "/var/db/ntpd.drift"
  37. #define CTLSOCKET "/var/run/ntpd.sock"
  38. #define INTERVAL_QUERY_NORMAL 30 /* sync to peers every n secs */
  39. #define INTERVAL_QUERY_PATHETIC 60
  40. #define INTERVAL_QUERY_AGGRESSIVE 5
  41. #define TRUSTLEVEL_BADPEER 6
  42. #define TRUSTLEVEL_PATHETIC 2
  43. #define TRUSTLEVEL_AGGRESSIVE 8
  44. #define TRUSTLEVEL_MAX 10
  45. #define MAX_SERVERS_DNS 8
  46. #define QSCALE_OFF_MIN 0.001
  47. #define QSCALE_OFF_MAX 0.050
  48. #define QUERYTIME_MAX 15 /* single query might take n secs max */
  49. #define OFFSET_ARRAY_SIZE 8
  50. #define SENSOR_OFFSETS 6
  51. #define SETTIME_TIMEOUT 15 /* max seconds to wait with -s */
  52. #define LOG_NEGLIGIBLE_ADJTIME 32 /* negligible drift to not log (ms) */
  53. #define LOG_NEGLIGIBLE_ADJFREQ 0.05 /* negligible rate to not log (ppm) */
  54. #define FREQUENCY_SAMPLES 8 /* samples for est. of permanent drift */
  55. #define MAX_FREQUENCY_ADJUST 128e-5 /* max correction per iteration */
  56. #define REPORT_INTERVAL (24*60*60) /* interval between status reports */
  57. #define MAX_SEND_ERRORS 3 /* max send errors before reconnect */
  58. #define MAX_DISPLAY_WIDTH 80 /* max chars in ctl_show report line */
  59. #define FILTER_ADJFREQ 0x01 /* set after doing adjfreq */
  60. #define SENSOR_DATA_MAXAGE (15*60)
  61. #define SENSOR_QUERY_INTERVAL 15
  62. #define SENSOR_QUERY_INTERVAL_SETTIME (SETTIME_TIMEOUT/3)
  63. #define SENSOR_SCAN_INTERVAL (5*60)
  64. #define SENSOR_DEFAULT_REFID "HARD"
  65. enum client_state {
  66. STATE_NONE,
  67. STATE_DNS_INPROGRESS,
  68. STATE_DNS_TEMPFAIL,
  69. STATE_DNS_DONE,
  70. STATE_QUERY_SENT,
  71. STATE_REPLY_RECEIVED
  72. };
  73. struct listen_addr {
  74. TAILQ_ENTRY(listen_addr) entry;
  75. struct sockaddr_storage sa;
  76. int fd;
  77. int rtable;
  78. };
  79. struct ntp_addr {
  80. struct ntp_addr *next;
  81. struct sockaddr_storage ss;
  82. int rtable;
  83. };
  84. struct ntp_addr_wrap {
  85. char *name;
  86. struct ntp_addr *a;
  87. u_int8_t pool;
  88. };
  89. struct ntp_status {
  90. double rootdelay;
  91. double rootdispersion;
  92. double reftime;
  93. u_int32_t refid;
  94. u_int32_t send_refid;
  95. u_int8_t synced;
  96. u_int8_t leap;
  97. int8_t precision;
  98. u_int8_t poll;
  99. u_int8_t stratum;
  100. };
  101. struct ntp_offset {
  102. struct ntp_status status;
  103. double offset;
  104. double delay;
  105. double error;
  106. time_t rcvd;
  107. u_int8_t good;
  108. };
  109. struct ntp_peer {
  110. TAILQ_ENTRY(ntp_peer) entry;
  111. struct ntp_addr_wrap addr_head;
  112. struct ntp_addr *addr;
  113. struct ntp_query *query;
  114. struct ntp_offset reply[OFFSET_ARRAY_SIZE];
  115. struct ntp_offset update;
  116. enum client_state state;
  117. time_t next;
  118. time_t deadline;
  119. time_t poll;
  120. u_int32_t id;
  121. u_int8_t shift;
  122. u_int8_t trustlevel;
  123. u_int8_t weight;
  124. int lasterror;
  125. int senderrors;
  126. int rtable;
  127. };
  128. struct ntp_sensor {
  129. TAILQ_ENTRY(ntp_sensor) entry;
  130. struct ntp_offset offsets[SENSOR_OFFSETS];
  131. struct ntp_offset update;
  132. time_t next;
  133. time_t last;
  134. char *device;
  135. u_int32_t refid;
  136. int sensordevid;
  137. int correction;
  138. u_int8_t stratum;
  139. u_int8_t weight;
  140. u_int8_t shift;
  141. };
  142. struct ntp_conf_sensor {
  143. TAILQ_ENTRY(ntp_conf_sensor) entry;
  144. char *device;
  145. char *refstr;
  146. int correction;
  147. u_int8_t stratum;
  148. u_int8_t weight;
  149. };
  150. struct ntp_freq {
  151. double overall_offset;
  152. double x, y;
  153. double xx, xy;
  154. int samples;
  155. u_int num;
  156. };
  157. struct ntpd_conf {
  158. TAILQ_HEAD(listen_addrs, listen_addr) listen_addrs;
  159. TAILQ_HEAD(ntp_peers, ntp_peer) ntp_peers;
  160. TAILQ_HEAD(ntp_sensors, ntp_sensor) ntp_sensors;
  161. TAILQ_HEAD(ntp_conf_sensors, ntp_conf_sensor) ntp_conf_sensors;
  162. struct ntp_status status;
  163. struct ntp_freq freq;
  164. u_int32_t scale;
  165. u_int8_t listen_all;
  166. u_int8_t settime;
  167. u_int8_t debug;
  168. u_int8_t noaction;
  169. u_int8_t filters;
  170. };
  171. struct imsgev {
  172. struct imsgbuf ibuf;
  173. void (*handler)(int, short, void *);
  174. struct event ev;
  175. void *data;
  176. short events;
  177. };
  178. struct ctl_show_status {
  179. u_int peercnt;
  180. u_int sensorcnt;
  181. u_int valid_peers;
  182. u_int valid_sensors;
  183. u_int8_t synced;
  184. u_int8_t stratum;
  185. double clock_offset;
  186. };
  187. struct ctl_show_peer {
  188. char peer_desc[MAX_DISPLAY_WIDTH];
  189. u_int8_t syncedto;
  190. u_int8_t weight;
  191. u_int8_t trustlevel;
  192. u_int8_t stratum;
  193. time_t next;
  194. time_t poll;
  195. double offset;
  196. double delay;
  197. double jitter;
  198. };
  199. struct ctl_show_sensor {
  200. char sensor_desc[MAX_DISPLAY_WIDTH];
  201. u_int8_t syncedto;
  202. u_int8_t weight;
  203. u_int8_t good;
  204. u_int8_t stratum;
  205. time_t next;
  206. time_t poll;
  207. double offset;
  208. double correction;
  209. };
  210. enum blockmodes {
  211. BM_NORMAL,
  212. BM_NONBLOCK
  213. };
  214. struct ctl_conn {
  215. TAILQ_ENTRY(ctl_conn) entry;
  216. struct imsgev iev;
  217. struct imsgbuf ibuf;
  218. };
  219. TAILQ_HEAD(ctl_conns, ctl_conn) ;
  220. enum imsg_type {
  221. IMSG_NONE,
  222. IMSG_ADJTIME,
  223. IMSG_ADJFREQ,
  224. IMSG_SETTIME,
  225. IMSG_HOST_DNS,
  226. IMSG_CTL_SHOW_STATUS,
  227. IMSG_CTL_SHOW_PEERS,
  228. IMSG_CTL_SHOW_PEERS_END,
  229. IMSG_CTL_SHOW_SENSORS,
  230. IMSG_CTL_SHOW_SENSORS_END,
  231. IMSG_CTL_SHOW_ALL,
  232. IMSG_CTL_SHOW_ALL_END
  233. };
  234. enum ctl_actions {
  235. CTL_SHOW_STATUS,
  236. CTL_SHOW_PEERS,
  237. CTL_SHOW_SENSORS,
  238. CTL_SHOW_ALL
  239. };
  240. /* prototypes */
  241. /* log.c */
  242. void log_init(int);
  243. void vlog(int, const char *, va_list);
  244. void log_warn(const char *, ...);
  245. void log_warnx(const char *, ...);
  246. void log_info(const char *, ...);
  247. void log_debug(const char *, ...);
  248. void fatal(const char *);
  249. void fatalx(const char *);
  250. const char *log_sockaddr(struct sockaddr *);
  251. /* ntp.c */
  252. pid_t ntp_main(int[2], int, struct ntpd_conf *, struct passwd *);
  253. int priv_adjtime(void);
  254. void priv_settime(double);
  255. void priv_host_dns(char *, u_int32_t);
  256. int offset_compare(const void *, const void *);
  257. void update_scale(double);
  258. time_t scale_interval(time_t);
  259. time_t error_interval(void);
  260. extern struct ntpd_conf *conf;
  261. extern struct ctl_conns ctl_conns;
  262. /* parse.y */
  263. int parse_config(const char *, struct ntpd_conf *);
  264. /* config.c */
  265. int host(const char *, struct ntp_addr **);
  266. int host_dns(const char *, struct ntp_addr **);
  267. struct ntp_peer *new_peer(void);
  268. struct ntp_conf_sensor *new_sensor(char *);
  269. /* ntp_msg.c */
  270. int ntp_getmsg(struct sockaddr *, char *, ssize_t, struct ntp_msg *);
  271. int ntp_sendmsg(int, struct sockaddr *, struct ntp_msg *);
  272. /* server.c */
  273. int setup_listeners(struct servent *, struct ntpd_conf *, u_int *);
  274. int ntp_reply(int, struct sockaddr *, struct ntp_msg *, int);
  275. int server_dispatch(int, struct ntpd_conf *);
  276. /* client.c */
  277. int client_peer_init(struct ntp_peer *);
  278. int client_addr_init(struct ntp_peer *);
  279. int client_nextaddr(struct ntp_peer *);
  280. int client_query(struct ntp_peer *);
  281. int client_dispatch(struct ntp_peer *, u_int8_t);
  282. void client_log_error(struct ntp_peer *, const char *, int);
  283. void set_next(struct ntp_peer *, time_t);
  284. /* util.c */
  285. double gettime_corrected(void);
  286. double getoffset(void);
  287. double gettime(void);
  288. time_t getmonotime(void);
  289. void d_to_tv(double, struct timeval *);
  290. double lfp_to_d(struct l_fixedpt);
  291. struct l_fixedpt d_to_lfp(double);
  292. double sfp_to_d(struct s_fixedpt);
  293. struct s_fixedpt d_to_sfp(double);
  294. char *print_rtable(int);
  295. /* sensors.c */
  296. void sensor_init(void);
  297. int sensor_scan(void);
  298. void sensor_query(struct ntp_sensor *);
  299. int sensor_hotplugfd(void);
  300. void sensor_hotplugevent(int);
  301. /* ntp_dns.c */
  302. pid_t ntp_dns(int[2], struct ntpd_conf *, struct passwd *);
  303. /* control.c */
  304. int control_init(char *);
  305. int control_listen(int);
  306. void control_shutdown(int);
  307. void control_cleanup(const char *);
  308. int control_accept(int);
  309. struct ctl_conn *control_connbyfd(int);
  310. int control_close(int);
  311. int control_dispatch_msg(struct pollfd *, u_int *);
  312. void session_socket_blockmode(int, enum blockmodes);
  313. void build_show_status(struct ctl_show_status *);
  314. void build_show_peer(struct ctl_show_peer *,
  315. struct ntp_peer *);
  316. void build_show_sensor(struct ctl_show_sensor *,
  317. struct ntp_sensor *);