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.

237 lines
5.6 KiB

20 years ago
  1. /* $OpenBSD: ntpd.h,v 1.36 2004/09/16 01:13:42 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 <netinet/in.h>
  22. #include <arpa/inet.h>
  23. #include <netdb.h>
  24. #include <stdarg.h>
  25. #include "ntp.h"
  26. #define NTPD_USER "_ntp"
  27. #define CONFFILE "/etc/ntpd.conf"
  28. #define READ_BUF_SIZE 65535
  29. #define IDX2PEER_RESERVE 5
  30. #define PFD_RESERVE 10
  31. #define NTPD_OPT_VERBOSE 0x0001
  32. #define NTPD_OPT_VERBOSE2 0x0002
  33. #define INTERVAL_QUERY_NORMAL 30 /* sync to peers every n secs */
  34. #define INTERVAL_QUERY_PATHETIC 60
  35. #define INTERVAL_QUERY_AGRESSIVE 5
  36. #define TRUSTLEVEL_BADPEER 6
  37. #define TRUSTLEVEL_PATHETIC 2
  38. #define TRUSTLEVEL_AGRESSIVE 8
  39. #define QSCALE_OFF_MIN 0.05
  40. #define QSCALE_OFF_MAX 0.50
  41. #define QUERYTIME_MAX 15 /* single query might take n secs max */
  42. #define OFFSET_ARRAY_SIZE 8
  43. enum client_state {
  44. STATE_NONE,
  45. STATE_QUERY_SENT,
  46. STATE_REPLY_RECEIVED
  47. };
  48. struct listen_addr {
  49. TAILQ_ENTRY(listen_addr) entry;
  50. struct sockaddr_storage sa;
  51. int fd;
  52. };
  53. struct ntp_addr {
  54. struct ntp_addr *next;
  55. struct sockaddr_storage ss;
  56. };
  57. struct ntp_addr_wrap {
  58. char *name;
  59. u_int8_t pool;
  60. struct ntp_addr *a;
  61. };
  62. struct ntp_status {
  63. u_int8_t leap;
  64. int8_t precision;
  65. double rootdelay;
  66. double rootdispersion;
  67. u_int32_t refid;
  68. double reftime;
  69. u_int8_t poll;
  70. };
  71. struct ntp_offset {
  72. u_int8_t good;
  73. double offset;
  74. double delay;
  75. double error;
  76. time_t rcvd;
  77. struct ntp_status status;
  78. };
  79. struct ntp_peer {
  80. TAILQ_ENTRY(ntp_peer) entry;
  81. u_int32_t id;
  82. struct ntp_addr_wrap addr_head;
  83. struct ntp_addr *addr;
  84. struct ntp_query *query;
  85. enum client_state state;
  86. time_t next;
  87. time_t deadline;
  88. struct ntp_offset reply[OFFSET_ARRAY_SIZE];
  89. struct ntp_offset update;
  90. u_int8_t shift;
  91. u_int8_t trustlevel;
  92. };
  93. struct ntpd_conf {
  94. TAILQ_HEAD(listen_addrs, listen_addr) listen_addrs;
  95. TAILQ_HEAD(ntp_peers, ntp_peer) ntp_peers;
  96. u_int8_t opts;
  97. u_int8_t listen_all;
  98. struct ntp_status status;
  99. };
  100. struct buf {
  101. TAILQ_ENTRY(buf) entries;
  102. u_char *buf;
  103. ssize_t size;
  104. ssize_t wpos;
  105. ssize_t rpos;
  106. };
  107. struct msgbuf {
  108. u_int32_t queued;
  109. int fd;
  110. TAILQ_HEAD(bufs, buf) bufs;
  111. };
  112. struct buf_read {
  113. u_char buf[READ_BUF_SIZE];
  114. u_char *rptr;
  115. ssize_t wpos;
  116. };
  117. /* ipc messages */
  118. #define IMSG_HEADER_SIZE sizeof(struct imsg_hdr)
  119. #define MAX_IMSGSIZE 8192
  120. struct imsgbuf {
  121. int fd;
  122. pid_t pid;
  123. struct buf_read r;
  124. struct msgbuf w;
  125. };
  126. enum imsg_type {
  127. IMSG_NONE,
  128. IMSG_ADJTIME,
  129. IMSG_HOST_DNS
  130. };
  131. struct imsg_hdr {
  132. enum imsg_type type;
  133. u_int16_t len;
  134. u_int32_t peerid;
  135. pid_t pid;
  136. };
  137. struct imsg {
  138. struct imsg_hdr hdr;
  139. void *data;
  140. };
  141. /* prototypes */
  142. /* log.c */
  143. void log_init(int);
  144. void vlog(int, const char *, va_list);
  145. void log_warn(const char *, ...);
  146. void log_warnx(const char *, ...);
  147. void log_info(const char *, ...);
  148. void log_debug(const char *, ...);
  149. void fatal(const char *);
  150. void fatalx(const char *);
  151. const char * log_sockaddr(struct sockaddr *);
  152. /* buffer.c */
  153. struct buf *buf_open(ssize_t);
  154. int buf_add(struct buf *, void *, ssize_t);
  155. int buf_close(struct msgbuf *, struct buf *);
  156. void buf_free(struct buf *);
  157. void msgbuf_init(struct msgbuf *);
  158. void msgbuf_clear(struct msgbuf *);
  159. int msgbuf_write(struct msgbuf *);
  160. /* imsg.c */
  161. void imsg_init(struct imsgbuf *, int);
  162. int imsg_read(struct imsgbuf *);
  163. int imsg_get(struct imsgbuf *, struct imsg *);
  164. int imsg_compose(struct imsgbuf *, enum imsg_type, u_int32_t, pid_t,
  165. void *, u_int16_t);
  166. struct buf *imsg_create(struct imsgbuf *, enum imsg_type, u_int32_t, pid_t,
  167. u_int16_t);
  168. int imsg_add(struct buf *, void *, u_int16_t);
  169. int imsg_close(struct imsgbuf *, struct buf *);
  170. void imsg_free(struct imsg *);
  171. /* ntp.c */
  172. pid_t ntp_main(int[2], struct ntpd_conf *);
  173. void ntp_adjtime(void);
  174. void ntp_host_dns(char *, u_int32_t);
  175. /* parse.y */
  176. int parse_config(char *, struct ntpd_conf *);
  177. /* config.c */
  178. int host(const char *, struct ntp_addr **);
  179. int host_dns(const char *, struct ntp_addr **);
  180. struct ntp_peer *new_peer(void);
  181. /* ntp_msg.c */
  182. int ntp_getmsg(char *, ssize_t, struct ntp_msg *);
  183. int ntp_sendmsg(int, struct sockaddr *, struct ntp_msg *, ssize_t, int);
  184. /* server.c */
  185. int setup_listeners(struct servent *, struct ntpd_conf *, u_int *);
  186. int ntp_reply(int, struct sockaddr *, struct ntp_msg *, int);
  187. int server_dispatch(int, struct ntpd_conf *);
  188. /* client.c */
  189. int client_peer_init(struct ntp_peer *);
  190. int client_addr_init(struct ntp_peer *);
  191. int client_nextaddr(struct ntp_peer *);
  192. int client_query(struct ntp_peer *);
  193. int client_dispatch(struct ntp_peer *);
  194. /* util.c */
  195. double gettime(void);
  196. void d_to_tv(double, struct timeval *);
  197. double lfp_to_d(struct l_fixedpt);
  198. struct l_fixedpt d_to_lfp(double);
  199. double sfp_to_d(struct s_fixedpt);
  200. struct s_fixedpt d_to_sfp(double);