After another look at the current changes, switch directly to the in-tree
[dragonfly.git] / contrib / ntpd / ntpd.h
1 /*      $OpenBSD: src/usr.sbin/ntpd/ntpd.h,v 1.41 2004/10/22 21:17:37 henning Exp $ */
2
3 /*
4  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
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
19 #include <sys/types.h>
20 #include <sys/uio.h>
21 #include <sys/socket.h>
22 #include <sys/queue.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 <stdarg.h>
29
30 #include "ntp.h"
31
32 #define NTPD_USER       "_ntp"
33 #define CONFFILE        "/etc/ntpd.conf"
34
35 #define READ_BUF_SIZE           65535
36
37 #define NTPD_OPT_VERBOSE        0x0001
38 #define NTPD_OPT_VERBOSE2       0x0002
39
40 #define INTERVAL_QUERY_NORMAL           30      /* sync to peers every n secs */
41 #define INTERVAL_QUERY_PATHETIC         60
42 #define INTERVAL_QUERY_AGRESSIVE        5
43
44 #define TRUSTLEVEL_BADPEER              6
45 #define TRUSTLEVEL_PATHETIC             2
46 #define TRUSTLEVEL_AGRESSIVE            8
47
48 #define QSCALE_OFF_MIN                  0.05
49 #define QSCALE_OFF_MAX                  0.50
50
51 #define QUERYTIME_MAX           15      /* single query might take n secs max */
52 #define OFFSET_ARRAY_SIZE       8
53 #define SETTIME_MIN_OFFSET      180     /* min offset for settime at start */
54
55 enum client_state {
56         STATE_NONE,
57         STATE_QUERY_SENT,
58         STATE_REPLY_RECEIVED
59 };
60
61 struct listen_addr {
62         TAILQ_ENTRY(listen_addr)         entry;
63         struct sockaddr_storage          sa;
64         int                              fd;
65 };
66
67 struct ntp_addr {
68         struct ntp_addr         *next;
69         struct sockaddr_storage  ss;
70 };
71
72 struct ntp_addr_wrap {
73         char                    *name;
74         u_int8_t                 pool;
75         struct ntp_addr         *a;
76 };
77
78 struct ntp_status {
79         u_int8_t        leap;
80         int8_t          precision;
81         double          rootdelay;
82         double          rootdispersion;
83         u_int32_t       refid;
84         double          reftime;
85         u_int8_t        poll;
86         u_int8_t        stratum;
87 };
88
89 struct ntp_offset {
90         u_int8_t                good;
91         double                  offset;
92         double                  delay;
93         double                  error;
94         time_t                  rcvd;
95         struct ntp_status       status;
96 };
97
98 struct ntp_peer {
99         TAILQ_ENTRY(ntp_peer)            entry;
100         u_int32_t                        id;
101         struct ntp_addr_wrap             addr_head;
102         struct ntp_addr                 *addr;
103         struct ntp_query                *query;
104         enum client_state                state;
105         time_t                           next;
106         time_t                           deadline;
107         struct ntp_offset                reply[OFFSET_ARRAY_SIZE];
108         struct ntp_offset                update;
109         u_int8_t                         shift;
110         u_int8_t                         trustlevel;
111 };
112
113 struct ntpd_conf {
114         TAILQ_HEAD(listen_addrs, listen_addr)   listen_addrs;
115         TAILQ_HEAD(ntp_peers, ntp_peer)         ntp_peers;
116         u_int8_t                                listen_all;
117         u_int8_t                                settime;
118         u_int8_t                                debug;
119         struct ntp_status                       status;
120 };
121
122 struct buf {
123         TAILQ_ENTRY(buf)         entries;
124         u_char                  *buf;
125         ssize_t                  size;
126         ssize_t                  wpos;
127         ssize_t                  rpos;
128 };
129
130 struct msgbuf {
131         u_int32_t                queued;
132         int                      fd;
133         TAILQ_HEAD(bufs, buf)    bufs;
134 };
135
136 struct buf_read {
137         u_char                   buf[READ_BUF_SIZE];
138         u_char                  *rptr;
139         ssize_t                  wpos;
140 };
141
142 /* ipc messages */
143
144 #define IMSG_HEADER_SIZE        sizeof(struct imsg_hdr)
145 #define MAX_IMSGSIZE            8192
146
147 struct imsgbuf {
148         int                     fd;
149         pid_t                   pid;
150         struct buf_read         r;
151         struct msgbuf           w;
152 };
153
154 enum imsg_type {
155         IMSG_NONE,
156         IMSG_ADJTIME,
157         IMSG_SETTIME,
158         IMSG_HOST_DNS
159 };
160
161 struct imsg_hdr {
162         enum imsg_type  type;
163         u_int16_t       len;
164         u_int32_t       peerid;
165         pid_t           pid;
166 };
167
168 struct imsg {
169         struct imsg_hdr  hdr;
170         void            *data;
171 };
172
173 /* prototypes */
174 /* log.c */
175 void             log_init(int);
176 void             vlog(int, const char *, va_list);
177 void             log_warn(const char *, ...);
178 void             log_warnx(const char *, ...);
179 void             log_info(const char *, ...);
180 void             log_debug(const char *, ...);
181 void             fatal(const char *);
182 void             fatalx(const char *);
183 const char *     log_sockaddr(struct sockaddr *);
184
185 /* buffer.c */
186 struct buf      *buf_open(ssize_t);
187 int              buf_add(struct buf *, void *, ssize_t);
188 int              buf_close(struct msgbuf *, struct buf *);
189 void             buf_free(struct buf *);
190 void             msgbuf_init(struct msgbuf *);
191 void             msgbuf_clear(struct msgbuf *);
192 int              msgbuf_write(struct msgbuf *);
193
194 /* imsg.c */
195 void     imsg_init(struct imsgbuf *, int);
196 int      imsg_read(struct imsgbuf *);
197 int      imsg_get(struct imsgbuf *, struct imsg *);
198 int      imsg_compose(struct imsgbuf *, enum imsg_type, u_int32_t, pid_t,
199             void *, u_int16_t);
200 struct buf      *imsg_create(struct imsgbuf *, enum imsg_type, u_int32_t, pid_t,
201                     u_int16_t);
202 int      imsg_add(struct buf *, void *, u_int16_t);
203 int      imsg_close(struct imsgbuf *, struct buf *);
204 void     imsg_free(struct imsg *);
205
206 /* ntp.c */
207 pid_t    ntp_main(int[2], struct ntpd_conf *);
208 void     ntp_adjtime(void);
209 void     ntp_settime(double);
210 void     ntp_host_dns(char *, u_int32_t);
211
212 /* parse.y */
213 int      parse_config(char *, struct ntpd_conf *);
214
215 /* config.c */
216 int              host(const char *, struct ntp_addr **);
217 int              host_dns(const char *, struct ntp_addr **);
218 struct ntp_peer *new_peer(void);
219
220 /* ntp_msg.c */
221 int     ntp_getmsg(char *, ssize_t, struct ntp_msg *);
222 int     ntp_sendmsg(int, struct sockaddr *, struct ntp_msg *, ssize_t, int);
223
224 /* server.c */
225 int     setup_listeners(struct servent *, struct ntpd_conf *, u_int *);
226 int     ntp_reply(int, struct sockaddr *, struct ntp_msg *, int);
227 int     server_dispatch(int, struct ntpd_conf *);
228
229 /* client.c */
230 int     client_peer_init(struct ntp_peer *);
231 int     client_addr_init(struct ntp_peer *);
232 int     client_nextaddr(struct ntp_peer *);
233 int     client_query(struct ntp_peer *);
234 int     client_dispatch(struct ntp_peer *, u_int8_t);
235
236 /* util.c */
237 double                  gettime(void);
238 void                    d_to_tv(double, struct timeval *);
239 double                  lfp_to_d(struct l_fixedpt);
240 struct l_fixedpt        d_to_lfp(double);
241 double                  sfp_to_d(struct s_fixedpt);
242 struct s_fixedpt        d_to_sfp(double);