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.

259 lines
6.2 KiB

20 years ago
  1. /* $OpenBSD: ntpd.h,v 1.59 2005/07/15 03:37:15 henning Exp $ */
  2. /*
  3. * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
  14. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
  15. * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include <sys/types.h>
  18. #include <sys/uio.h>
  19. #include <sys/socket.h>
  20. #include <sys/queue.h>
  21. #include <sys/time.h>
  22. #include <netinet/in.h>
  23. #include <netinet/in_systm.h>
  24. #include <netinet/ip.h>
  25. #include <arpa/inet.h>
  26. #include <netdb.h>
  27. #include <stdarg.h>
  28. #include "ntp.h"
  29. #define NTPD_USER "_ntp"
  30. #define CONFFILE "/etc/ntpd.conf"
  31. #define READ_BUF_SIZE 4096
  32. #define NTPD_OPT_VERBOSE 0x0001
  33. #define NTPD_OPT_VERBOSE2 0x0002
  34. #define INTERVAL_QUERY_NORMAL 30 /* sync to peers every n secs */
  35. #define INTERVAL_QUERY_PATHETIC 60
  36. #define INTERVAL_QUERY_AGRESSIVE 5
  37. #define TRUSTLEVEL_BADPEER 6
  38. #define TRUSTLEVEL_PATHETIC 2
  39. #define TRUSTLEVEL_AGRESSIVE 8
  40. #define TRUSTLEVEL_MAX 10
  41. #define MAX_SERVERS_DNS 8
  42. #define QSCALE_OFF_MIN 0.05
  43. #define QSCALE_OFF_MAX 0.50
  44. #define QUERYTIME_MAX 15 /* single query might take n secs max */
  45. #define OFFSET_ARRAY_SIZE 8
  46. #define SETTIME_MIN_OFFSET 180 /* min offset for settime at start */
  47. #define SETTIME_TIMEOUT 15 /* max seconds to wait with -s */
  48. #define LOG_NEGLIGEE 128 /* negligible drift to not log (ms) */
  49. enum client_state {
  50. STATE_NONE,
  51. STATE_DNS_INPROGRESS,
  52. STATE_DNS_TEMPFAIL,
  53. STATE_DNS_DONE,
  54. STATE_QUERY_SENT,
  55. STATE_REPLY_RECEIVED
  56. };
  57. struct listen_addr {
  58. TAILQ_ENTRY(listen_addr) entry;
  59. struct sockaddr_storage sa;
  60. int fd;
  61. };
  62. struct ntp_addr {
  63. struct ntp_addr *next;
  64. struct sockaddr_storage ss;
  65. };
  66. struct ntp_addr_wrap {
  67. char *name;
  68. struct ntp_addr *a;
  69. u_int8_t pool;
  70. };
  71. struct ntp_status {
  72. double rootdelay;
  73. double rootdispersion;
  74. double reftime;
  75. u_int32_t refid;
  76. u_int32_t refid4;
  77. u_int8_t leap;
  78. int8_t precision;
  79. u_int8_t poll;
  80. u_int8_t stratum;
  81. };
  82. struct ntp_offset {
  83. struct ntp_status status;
  84. double offset;
  85. double delay;
  86. double error;
  87. time_t rcvd;
  88. u_int8_t good;
  89. };
  90. struct ntp_peer {
  91. TAILQ_ENTRY(ntp_peer) entry;
  92. struct ntp_addr_wrap addr_head;
  93. struct ntp_addr *addr;
  94. struct ntp_query *query;
  95. struct ntp_offset reply[OFFSET_ARRAY_SIZE];
  96. struct ntp_offset update;
  97. enum client_state state;
  98. time_t next;
  99. time_t deadline;
  100. u_int32_t id;
  101. u_int8_t shift;
  102. u_int8_t trustlevel;
  103. int lasterror;
  104. };
  105. struct ntpd_conf {
  106. TAILQ_HEAD(listen_addrs, listen_addr) listen_addrs;
  107. TAILQ_HEAD(ntp_peers, ntp_peer) ntp_peers;
  108. struct ntp_status status;
  109. u_int8_t listen_all;
  110. u_int8_t settime;
  111. u_int8_t debug;
  112. u_int32_t scale;
  113. };
  114. struct buf {
  115. TAILQ_ENTRY(buf) entry;
  116. u_char *buf;
  117. size_t size;
  118. size_t wpos;
  119. size_t rpos;
  120. };
  121. struct msgbuf {
  122. TAILQ_HEAD(, buf) bufs;
  123. u_int32_t queued;
  124. int fd;
  125. };
  126. struct buf_read {
  127. size_t wpos;
  128. u_char buf[READ_BUF_SIZE];
  129. u_char *rptr;
  130. };
  131. /* ipc messages */
  132. #define IMSG_HEADER_SIZE sizeof(struct imsg_hdr)
  133. #define MAX_IMSGSIZE 8192
  134. struct imsgbuf {
  135. int fd;
  136. pid_t pid;
  137. struct buf_read r;
  138. struct msgbuf w;
  139. };
  140. enum imsg_type {
  141. IMSG_NONE,
  142. IMSG_ADJTIME,
  143. IMSG_SETTIME,
  144. IMSG_HOST_DNS
  145. };
  146. struct imsg_hdr {
  147. enum imsg_type type;
  148. u_int32_t peerid;
  149. pid_t pid;
  150. u_int16_t len;
  151. };
  152. struct imsg {
  153. struct imsg_hdr hdr;
  154. void *data;
  155. };
  156. /* prototypes */
  157. /* log.c */
  158. void log_init(int);
  159. void vlog(int, const char *, va_list);
  160. void log_warn(const char *, ...);
  161. void log_warnx(const char *, ...);
  162. void log_info(const char *, ...);
  163. void log_debug(const char *, ...);
  164. void fatal(const char *);
  165. void fatalx(const char *);
  166. const char * log_sockaddr(struct sockaddr *);
  167. /* buffer.c */
  168. struct buf *buf_open(size_t);
  169. int buf_add(struct buf *, void *, size_t);
  170. int buf_close(struct msgbuf *, struct buf *);
  171. void buf_free(struct buf *);
  172. void msgbuf_init(struct msgbuf *);
  173. void msgbuf_clear(struct msgbuf *);
  174. int msgbuf_write(struct msgbuf *);
  175. /* imsg.c */
  176. void imsg_init(struct imsgbuf *, int);
  177. int imsg_read(struct imsgbuf *);
  178. int imsg_get(struct imsgbuf *, struct imsg *);
  179. int imsg_compose(struct imsgbuf *, enum imsg_type, u_int32_t, pid_t,
  180. void *, u_int16_t);
  181. struct buf *imsg_create(struct imsgbuf *, enum imsg_type, u_int32_t, pid_t,
  182. u_int16_t);
  183. int imsg_add(struct buf *, void *, u_int16_t);
  184. int imsg_close(struct imsgbuf *, struct buf *);
  185. void imsg_free(struct imsg *);
  186. /* ntp.c */
  187. pid_t ntp_main(int[2], struct ntpd_conf *);
  188. void priv_adjtime(void);
  189. void priv_settime(double);
  190. void priv_host_dns(char *, u_int32_t);
  191. /* parse.y */
  192. int parse_config(const char *, struct ntpd_conf *);
  193. /* config.c */
  194. int host(const char *, struct ntp_addr **);
  195. int host_dns(const char *, struct ntp_addr **);
  196. struct ntp_peer *new_peer(void);
  197. /* ntp_msg.c */
  198. int ntp_getmsg(char *, ssize_t, struct ntp_msg *);
  199. int ntp_sendmsg(int, struct sockaddr *, struct ntp_msg *, ssize_t, int);
  200. /* server.c */
  201. int setup_listeners(struct servent *, struct ntpd_conf *, u_int *);
  202. int ntp_reply(int, struct sockaddr *, struct ntp_msg *, int);
  203. int server_dispatch(int, struct ntpd_conf *);
  204. /* client.c */
  205. int client_peer_init(struct ntp_peer *);
  206. int client_addr_init(struct ntp_peer *);
  207. int client_nextaddr(struct ntp_peer *);
  208. int client_query(struct ntp_peer *);
  209. int client_dispatch(struct ntp_peer *, u_int8_t);
  210. void client_log_error(struct ntp_peer *, const char *, int);
  211. void update_scale(double);
  212. time_t scale_interval(time_t);
  213. time_t error_interval(void);
  214. void set_next(struct ntp_peer *, time_t);
  215. /* util.c */
  216. double gettime(void);
  217. void d_to_tv(double, struct timeval *);
  218. double lfp_to_d(struct l_fixedpt);
  219. struct l_fixedpt d_to_lfp(double);
  220. double sfp_to_d(struct s_fixedpt);
  221. struct s_fixedpt d_to_sfp(double);