forked from bsdphk/Ntimed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ntp.h
143 lines (117 loc) · 4.37 KB
/
ntp.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*-
* Copyright (c) 2014 Poul-Henning Kamp
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* NTP protocol stuff
* ==================
*
*/
struct ntp_peer;
#ifdef NTP_H_INCLUDED
#error "ntp.h included multiple times"
#endif
#define NTP_H_INCLUDED
enum ntp_mode {
#define NTP_MODE(n, l, u) NTP_MODE_##u = n,
#include "ntp_tbl.h"
#undef NTP_MODE
};
enum ntp_leap {
#define NTP_LEAP(n, l, u) NTP_LEAP_##u = n,
#include "ntp_tbl.h"
#undef NTP_LEAP
};
enum ntp_state {
#define NTP_STATE(n, l, u, d) NTP_STATE_##u = n,
#include "ntp_tbl.h"
#undef NTP_STATE
};
/* ntp_packet.c -- [De]Serialisation **********************************/
struct ntp_packet {
unsigned magic;
#define NTP_PACKET_MAGIC 0x78b7f0be
enum ntp_leap ntp_leap;
uint8_t ntp_version;
enum ntp_mode ntp_mode;
uint8_t ntp_stratum;
uint8_t ntp_poll;
int8_t ntp_precision;
struct timestamp ntp_delay;
struct timestamp ntp_dispersion;
uint8_t ntp_refid[4];
struct timestamp ntp_reference;
struct timestamp ntp_origin;
struct timestamp ntp_receive;
struct timestamp ntp_transmit;
struct timestamp ts_rx;
};
struct ntp_packet *NTP_Packet_Unpack(struct ntp_packet *dst, void *ptr,
ssize_t len);
size_t NTP_Packet_Pack(void *ptr, ssize_t len, struct ntp_packet *);
/* ntp_tools.c -- Handy tools *****************************************/
void NTP_Tool_Client_Req(struct ntp_packet *);
void NTP_Tool_Format(char *p, ssize_t len, const struct ntp_packet *pkt);
int NTP_Tool_Scan(struct ntp_packet *pkt, const char *buf);
/* ntp_filter.c -- NTP sanity checking ********************************/
typedef void ntp_filter_f(struct ocx *, const struct ntp_peer *);
void NF_New(struct ntp_peer *);
void NF_Init(void);
/* ntp_peer.c -- State management *************************************/
struct ntp_peer {
unsigned magic;
#define NTP_PEER_MAGIC 0xbf0740a0
char *hostname;
char *ip;
struct sockaddr *sa;
unsigned sa_len;
struct ntp_packet *tx_pkt;
struct ntp_packet *rx_pkt;
ntp_filter_f *filter_func;
void *filter_priv;
struct combiner *combiner;
// For ntp_peerset.c
TAILQ_ENTRY(ntp_peer) list;
struct ntp_group *group;
enum ntp_state state;
const struct ntp_peer *other;
};
struct ntp_peer *NTP_Peer_New(const char *name, const void *, unsigned);
struct ntp_peer *NTP_Peer_NewLookup(struct ocx *ocx, const char *name);
void NTP_Peer_Destroy(struct ntp_peer *np);
int NTP_Peer_Poll(struct ocx *, const struct udp_socket *,
const struct ntp_peer *, double tmo);
/* ntp_peerset.c -- Peer set management ****************************/
struct ntp_peerset *NTP_PeerSet_New(struct ocx *);
void NTP_PeerSet_AddSim(struct ocx *, struct ntp_peerset *,
const char *hostname, const char *ip);
int NTP_PeerSet_Add(struct ocx *, struct ntp_peerset *, const char *hostname);
void NTP_PeerSet_Poll(struct ocx *, struct ntp_peerset *, struct udp_socket *,
struct todolist *);
struct ntp_peer *NTP_PeerSet_Iter0(const struct ntp_peerset *);
struct ntp_peer *NTP_PeerSet_IterN(const struct ntp_peerset *,
const struct ntp_peer *);
#define NTP_PeerSet_Foreach(var, nps) \
for(var = NTP_PeerSet_Iter0(nps); \
var != NULL; \
var = NTP_PeerSet_IterN(nps, var))