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