]> git.ipfire.org Git - thirdparty/chrony.git/blob - ntp.h
ntp: fix log message for replaced source
[thirdparty/chrony.git] / ntp.h
1 /*
2 chronyd/chronyc - Programs for keeping computer clocks accurate.
3
4 **********************************************************************
5 * Copyright (C) Richard P. Curnow 1997-2003
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of version 2 of the GNU General Public License as
9 * published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 **********************************************************************
21
22 =======================================================================
23
24 Header file containing common NTP bits and pieces
25 */
26
27 #ifndef GOT_NTP_H
28 #define GOT_NTP_H
29
30 #include "sysincl.h"
31
32 #include "hash.h"
33
34 typedef struct {
35 uint32_t hi;
36 uint32_t lo;
37 } NTP_int64;
38
39 typedef uint32_t NTP_int32;
40
41 /* The UDP port number used by NTP */
42 #define NTP_PORT 123
43
44 /* The NTP protocol version that we support */
45 #define NTP_VERSION 4
46
47 /* Maximum stratum number (infinity) */
48 #define NTP_MAX_STRATUM 16
49
50 /* Invalid stratum number */
51 #define NTP_INVALID_STRATUM 0
52
53 /* The minimum and maximum supported length of MAC */
54 #define NTP_MIN_MAC_LENGTH (4 + 16)
55 #define NTP_MAX_MAC_LENGTH (4 + MAX_HASH_LENGTH)
56
57 /* The minimum valid length of an extension field */
58 #define NTP_MIN_EF_LENGTH 16
59
60 /* The maximum assumed length of all extension fields in an NTP packet,
61 including a MAC (RFC 5905 doesn't specify a limit on length or number of
62 extension fields in one packet) */
63 #define NTP_MAX_EXTENSIONS_LENGTH (1024 + NTP_MAX_MAC_LENGTH)
64
65 /* The maximum length of MAC in NTPv4 packets which allows deterministic
66 parsing of extension fields (RFC 7822) */
67 #define NTP_MAX_V4_MAC_LENGTH (4 + 20)
68
69 /* Type definition for leap bits */
70 typedef enum {
71 LEAP_Normal = 0,
72 LEAP_InsertSecond = 1,
73 LEAP_DeleteSecond = 2,
74 LEAP_Unsynchronised = 3
75 } NTP_Leap;
76
77 typedef enum {
78 MODE_UNDEFINED = 0,
79 MODE_ACTIVE = 1,
80 MODE_PASSIVE = 2,
81 MODE_CLIENT = 3,
82 MODE_SERVER = 4,
83 MODE_BROADCAST = 5
84 } NTP_Mode;
85
86 typedef struct {
87 uint8_t lvm;
88 uint8_t stratum;
89 int8_t poll;
90 int8_t precision;
91 NTP_int32 root_delay;
92 NTP_int32 root_dispersion;
93 NTP_int32 reference_id;
94 NTP_int64 reference_ts;
95 NTP_int64 originate_ts;
96 NTP_int64 receive_ts;
97 NTP_int64 transmit_ts;
98
99 uint8_t extensions[NTP_MAX_EXTENSIONS_LENGTH];
100 } NTP_Packet;
101
102 #define NTP_HEADER_LENGTH (int)offsetof(NTP_Packet, extensions)
103
104 /* Macros to work with the lvm field */
105 #define NTP_LVM_TO_LEAP(lvm) (((lvm) >> 6) & 0x3)
106 #define NTP_LVM_TO_VERSION(lvm) (((lvm) >> 3) & 0x7)
107 #define NTP_LVM_TO_MODE(lvm) ((lvm) & 0x7)
108 #define NTP_LVM(leap, version, mode) \
109 ((((leap) << 6) & 0xc0) | (((version) << 3) & 0x38) | ((mode) & 0x07))
110
111 /* Special NTP reference IDs */
112 #define NTP_REFID_UNSYNC 0x0UL
113 #define NTP_REFID_LOCAL 0x7F7F0101UL /* 127.127.1.1 */
114 #define NTP_REFID_SMOOTH 0x7F7F01FFUL /* 127.127.1.255 */
115
116 /* Enumeration for authentication modes of NTP packets */
117 typedef enum {
118 NTP_AUTH_NONE = 0, /* No authentication */
119 NTP_AUTH_SYMMETRIC, /* MAC using symmetric key (RFC 1305, RFC 5905) */
120 NTP_AUTH_MSSNTP, /* MS-SNTP authenticator field */
121 NTP_AUTH_MSSNTP_EXT, /* MS-SNTP extended authenticator field */
122 NTP_AUTH_NTS, /* Network Time Security (RFC ????) */
123 } NTP_AuthMode;
124
125 /* Structure describing an NTP packet */
126 typedef struct {
127 int length;
128 int version;
129 NTP_Mode mode;
130
131 int ext_fields;
132
133 struct {
134 NTP_AuthMode mode;
135 struct {
136 int start;
137 int length;
138 uint32_t key_id;
139 } mac;
140 } auth;
141 } NTP_PacketInfo;
142
143 /* Structure used to save NTP measurements. time is the local time at which
144 the sample is to be considered to have been made and offset is the offset at
145 the time (positive indicates that the local clock is slow relative to the
146 source). root_delay/root_dispersion include peer_delay/peer_dispersion. */
147 typedef struct {
148 struct timespec time;
149 double offset;
150 double peer_delay;
151 double peer_dispersion;
152 double root_delay;
153 double root_dispersion;
154 int stratum;
155 } NTP_Sample;
156
157 #endif /* GOT_NTP_H */