]> git.ipfire.org Git - thirdparty/chrony.git/blame - ntp.h
test: use env shebang in all bash scripts
[thirdparty/chrony.git] / ntp.h
CommitLineData
88840341 1/*
88840341
RC
2 chronyd/chronyc - Programs for keeping computer clocks accurate.
3
4 **********************************************************************
6672f045 5 * Copyright (C) Richard P. Curnow 1997-2003
88840341
RC
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.,
8e23110a 18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
88840341
RC
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
6ca73bf6 30#include "sysincl.h"
88840341 31
777303f1
ML
32#include "hash.h"
33
88840341
RC
34typedef struct {
35 uint32_t hi;
36 uint32_t lo;
37} NTP_int64;
38
39typedef uint32_t NTP_int32;
40
9983185d
ML
41/* The UDP port number used by NTP */
42#define NTP_PORT 123
43
8fbfe55e
ML
44/* The NTP protocol version that we support */
45#define NTP_VERSION 4
46
e0059bcc
ML
47/* Maximum stratum number (infinity) */
48#define NTP_MAX_STRATUM 16
49
83010590
ML
50/* Invalid stratum number */
51#define NTP_INVALID_STRATUM 0
52
8fbfe55e 53/* The minimum and maximum supported length of MAC */
5e61c002
ML
54#define NTP_MIN_MAC_LENGTH (4 + 16)
55#define NTP_MAX_MAC_LENGTH (4 + MAX_HASH_LENGTH)
88840341 56
d29bef93
ML
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
89b127bf
ML
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
88840341
RC
69/* Type definition for leap bits */
70typedef enum {
71 LEAP_Normal = 0,
72 LEAP_InsertSecond = 1,
73 LEAP_DeleteSecond = 2,
74 LEAP_Unsynchronised = 3
75} NTP_Leap;
76
77typedef 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
86typedef 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;
88840341 98
d29bef93 99 uint8_t extensions[NTP_MAX_EXTENSIONS_LENGTH];
8fbfe55e 100} NTP_Packet;
88840341 101
d29bef93 102#define NTP_HEADER_LENGTH (int)offsetof(NTP_Packet, extensions)
8fbfe55e
ML
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))
88840341 110
6be54f36
ML
111/* Special NTP reference IDs */
112#define NTP_REFID_UNSYNC 0x0UL
113#define NTP_REFID_LOCAL 0x7F7F0101UL /* 127.127.1.1 */
ab1f01ba 114#define NTP_REFID_SMOOTH 0x7F7F01FFUL /* 127.127.1.255 */
6be54f36 115
588785e1
ML
116/* Enumeration for authentication modes of NTP packets */
117typedef enum {
46cac4e2 118 NTP_AUTH_NONE = 0, /* No authentication */
35b3a42e
ML
119 NTP_AUTH_SYMMETRIC, /* NTP MAC or CMAC using a symmetric key
120 (RFC 1305, RFC 5905, RFC 8573) */
46cac4e2
ML
121 NTP_AUTH_MSSNTP, /* MS-SNTP authenticator field */
122 NTP_AUTH_MSSNTP_EXT, /* MS-SNTP extended authenticator field */
35b3a42e 123 NTP_AUTH_NTS, /* Network Time Security (RFC 8915) */
588785e1
ML
124} NTP_AuthMode;
125
cb8660e7
ML
126/* Structure describing an NTP packet */
127typedef struct {
128 int length;
129 int version;
130 NTP_Mode mode;
588785e1
ML
131
132 int ext_fields;
133
134 struct {
135 NTP_AuthMode mode;
136 struct {
137 int start;
138 int length;
139 uint32_t key_id;
140 } mac;
141 } auth;
cb8660e7
ML
142} NTP_PacketInfo;
143
6bef8aa0
ML
144/* Structure used to save NTP measurements. time is the local time at which
145 the sample is to be considered to have been made and offset is the offset at
146 the time (positive indicates that the local clock is slow relative to the
147 source). root_delay/root_dispersion include peer_delay/peer_dispersion. */
148typedef struct {
149 struct timespec time;
150 double offset;
151 double peer_delay;
152 double peer_dispersion;
153 double root_delay;
154 double root_dispersion;
6bef8aa0
ML
155} NTP_Sample;
156
88840341 157#endif /* GOT_NTP_H */