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