Initial commit for the DragonFly home-made ntpd client. Why? Because
[dragonfly.git] / usr.sbin / dntpd / ntp.h
1 /*
2  * $OpenBSD: src/usr.sbin/ntpd/ntp.h,v 1.11 2004/12/13 12:22:52 dtucker Exp $
3  * $DragonFly: src/usr.sbin/dntpd/ntp.h,v 1.1 2005/04/24 02:36:50 dillon Exp $
4  */
5
6 /*
7  * Copyright (c) 2004 Henning Brauer <henning@openbsd.org>
8  * Copyright (c) 2004 Alexander Guy <alexander.guy@andern.org>
9  *
10  * Permission to use, copy, modify, and distribute this software for any
11  * purpose with or without fee is hereby granted, provided that the above
12  * copyright notice and this permission notice appear in all copies.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
15  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
16  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
17  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21  */
22
23 #ifndef _NTP_H_
24 #define _NTP_H_
25
26 /* Style borrowed from NTP ref/tcpdump and updated for SNTPv4 (RFC2030). */
27
28 /*
29  * RFC Section 3
30  *
31  *    0                   1                   2                   3
32  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
33  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34  *   |                         Integer Part                          |
35  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36  *   |                         Fraction Part                         |
37  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38  *
39  *    0                   1                   2                   3
40  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
41  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
42  *   |            Integer Part       |     Fraction Part             |
43  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 */
45 struct l_fixedpt {
46         u_int32_t int_partl;
47         u_int32_t fractionl;
48 };
49
50 struct s_fixedpt {
51         u_int16_t int_parts;
52         u_int16_t fractions;
53 };
54
55 /* RFC Section 4
56  *
57  *    0                   1                   2                   3
58  *    0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
59  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
60  *   |LI | VN  | Mode|    Stratum    |      Poll     |   Precision   |
61  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
62  *   |                     Synchronizing Distance                    |
63  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
64  *   |                    Synchronizing Dispersion                   |
65  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
66  *   |                  Reference Clock Identifier                   |
67  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
68  *   |                                                               |
69  *   |                 Reference Timestamp (64 bits)                 |
70  *   |                                                               |
71  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
72  *   |                                                               |
73  *   |                 Originate Timestamp (64 bits)                 |
74  *   |                                                               |
75  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
76  *   |                                                               |
77  *   |                  Receive Timestamp (64 bits)                  |
78  *   |                                                               |
79  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
80  *   |                                                               |
81  *   |                  Transmit Timestamp (64 bits)                 |
82  *   |                                                               |
83  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
84  *   |                 Key Identifier (optional) (32)                |
85  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
86  *   |                                                               |
87  *   |                                                               |
88  *   |                 Message Digest (optional) (128)               |
89  *   |                                                               |
90  *   |                                                               |
91  *   +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
92  *
93  */
94
95 #define NTP_DIGESTSIZE          16
96 #define NTP_MSGSIZE_NOAUTH      48
97 #define NTP_MSGSIZE             (NTP_MSGSIZE_NOAUTH + 4 + NTP_DIGESTSIZE)
98
99 struct ntp_msg {
100         u_int8_t status;        /* status of local clock and leap info */
101         u_int8_t stratum;       /* Stratum level */
102         u_int8_t ppoll;         /* poll value */
103         int8_t precision;
104         struct s_fixedpt rootdelay;
105         struct s_fixedpt dispersion;
106         u_int32_t refid;
107         struct l_fixedpt reftime;
108         struct l_fixedpt orgtime;
109         struct l_fixedpt rectime;
110         struct l_fixedpt xmttime;
111         u_int32_t keyid;
112         u_int8_t digest[NTP_DIGESTSIZE];
113 };
114
115 struct ntp_query {
116         int                     fd;
117         struct ntp_msg          msg;
118         double                  xmttime;
119 };
120
121 /*
122  *      Leap Second Codes (high order two bits)
123  */
124 #define LI_NOWARNING    (0 << 6)        /* no warning */
125 #define LI_PLUSSEC      (1 << 6)        /* add a second (61 seconds) */
126 #define LI_MINUSSEC     (2 << 6)        /* minus a second (59 seconds) */
127 #define LI_ALARM        (3 << 6)        /* alarm condition */
128
129 /*
130  *      Status Masks
131  */
132 #define MODEMASK        (7 << 0)
133 #define VERSIONMASK     (7 << 3)
134 #define LIMASK          (3 << 6)
135
136 /*
137  *      Mode values
138  */
139 #define MODE_RES0       0       /* reserved */
140 #define MODE_SYM_ACT    1       /* symmetric active */
141 #define MODE_SYM_PAS    2       /* symmetric passive */
142 #define MODE_CLIENT     3       /* client */
143 #define MODE_SERVER     4       /* server */
144 #define MODE_BROADCAST  5       /* broadcast */
145 #define MODE_RES1       6       /* reserved for NTP control message */
146 #define MODE_RES2       7       /* reserved for private use */
147
148 #define JAN_1970        2208988800UL    /* 1970 - 1900 in seconds */
149
150 #define NTP_VERSION     4
151 #define NTP_MAXSTRATUM  15
152
153 #endif  /* _NTP_H_ */