forked from bsdphk/Ntimed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathntp_peer.c
235 lines (198 loc) · 5.93 KB
/
ntp_peer.c
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*-
* 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.
*
*/
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <poll.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include "ntimed.h"
#include "udp.h"
#include "ntp.h"
struct ntp_peer *
NTP_Peer_New(const char *hostname, const void *sa, unsigned salen)
{
struct ntp_peer *np;
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
AZ(getnameinfo(sa, salen, hbuf, sizeof(hbuf), sbuf,
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV));
ALLOC_OBJ(np, NTP_PEER_MAGIC);
AN(np);
np->sa_len = salen;
np->sa = calloc(1, np->sa_len);
AN(np->sa);
memcpy(np->sa, sa, np->sa_len);
np->hostname = strdup(hostname);
AN(np->hostname);
np->ip = strdup(hbuf);
AN(np->ip);
ALLOC_OBJ(np->tx_pkt, NTP_PACKET_MAGIC);
AN(np->tx_pkt);
ALLOC_OBJ(np->rx_pkt, NTP_PACKET_MAGIC);
AN(np->rx_pkt);
NTP_Tool_Client_Req(np->tx_pkt);
return (np);
}
struct ntp_peer *
NTP_Peer_NewLookup(struct ocx *ocx, const char *hostname)
{
struct addrinfo hints, *res0;
int error;
struct ntp_peer *np;
memset(&hints, 0, sizeof hints);
hints.ai_family = PF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
error = getaddrinfo(hostname, "ntp", &hints, &res0);
if (error)
Fail(ocx, 0, "hostname '%s', port 'ntp': %s\n",
hostname, gai_strerror(error));
np = NTP_Peer_New(hostname, res0->ai_addr, res0->ai_addrlen);
freeaddrinfo(res0);
return (np);
}
void
NTP_Peer_Destroy(struct ntp_peer *np)
{
CHECK_OBJ_NOTNULL(np, NTP_PEER_MAGIC);
free(np->sa);
free(np->hostname);
free(np->ip);
free(np->tx_pkt);
free(np->rx_pkt);
FREE_OBJ(np);
}
#ifndef NOIPV6
static int addr_is_same(struct sockaddr_storage *a, struct sockaddr_storage *b)
{
/* This is bulky. Is there a better way? */
if (a->ss_family == AF_INET && b->ss_family == AF_INET) {
/* ipv4: compare port and address, ignore padding. */
if (((struct sockaddr_in*)a)->sin_port !=
((struct sockaddr_in*)b)->sin_port)
return 0;
if (((struct sockaddr_in*)a)->sin_addr.s_addr !=
((struct sockaddr_in*)b)->sin_addr.s_addr)
return 0;
return 1;
}
if (a->ss_family == AF_INET6 && b->ss_family == AF_INET6) {
/* ipv6: compare port and address, ignore flowlabel. */
if (((struct sockaddr_in6*)a)->sin6_port !=
((struct sockaddr_in6*)b)->sin6_port)
return 0;
if (!IN6_ARE_ADDR_EQUAL(
&((struct sockaddr_in6*)a)->sin6_addr,
&((struct sockaddr_in6*)b)->sin6_addr))
return 0;
return 1;
}
if (a->ss_family == AF_INET && b->ss_family == AF_INET6) {
struct sockaddr_storage *tmp;
tmp = a;
a = b;
b = tmp;
}
if (a->ss_family == AF_INET6 && b->ss_family == AF_INET) {
/* comparing IPv6 to IPv4. Check port, and for a */
/* matching IPv4 address in the IPv6 address field.*/
if (((struct sockaddr_in6*)a)->sin6_port !=
((struct sockaddr_in*)b)->sin_port)
return 0;
/* 'a' should have the form: 0:0:0:0:0:FFFF:IPv4:IPv4 */
if (!IN6_IS_ADDR_V4MAPPED(&((struct sockaddr_in6*)a)->sin6_addr))
return 0;
if (memcmp(((struct sockaddr_in6*)a)->sin6_addr.s6_addr+12,
&((struct sockaddr_in*)b)->sin_addr, 4))
return 0;
return 1;
}
return 0;
}
#endif
int
NTP_Peer_Poll(struct ocx *ocx, int fd, const struct ntp_peer *np, double tmo)
{
char buf[100];
ssize_t len;
struct sockaddr_storage rss;
socklen_t rssl;
ssize_t l;
int i, timeout_msec;
struct pollfd pfd[1];
struct timestamp t0, t1, t2;
double d;
assert(fd >= 0);
CHECK_OBJ_NOTNULL(np, NTP_PEER_MAGIC);
assert(tmo > 0.0 && tmo <= 1.0);
len = NTP_Packet_Pack(buf, sizeof buf, np->tx_pkt);
l = sendto(fd, buf, len, 0, (struct sockaddr*) np->sa, np->sa_len);
if (l != len)
Fail(ocx, l < 0 ? 1 : 0,
"Tx peer %s %s got %zd", np->hostname, np->ip, l);
(void)TB_Now(&t0);
while (1) {
(void)TB_Now(&t1);
d = TS_Diff(&t1, &t0);
timeout_msec = lround(1e3 * (tmo - d));
pfd[0].fd = fd;
pfd[0].events = POLLIN;
pfd[0].revents = 0;
if (timeout_msec <= 0)
i = 0;
else
i = poll(pfd, 1, timeout_msec);
if (i < 0)
Fail(ocx, 1, "poll(2) failed\n");
if (i == 0)
return (0);
i = UdpTimedRx(ocx, fd, &rss, &rssl, &t2, buf, sizeof buf);
if (i < 0)
Fail(ocx, 1, "Rx failed\n");
if (i != 48) {
Debug(ocx, "Rx peer %s %s got len=%d\n",
np->hostname, np->ip, i);
continue;
}
/* Ignore packets from other hosts */
#ifndef NOIPV6
if (!addr_is_same(&rss, np->sa))
continue;
#else
if (np->sa_len != rssl || memcmp(np->sa, &rss, rssl))
continue;
#endif
AN(NTP_Packet_Unpack(np->rx_pkt, buf, i));
np->rx_pkt->ts_rx = t2;
/* Ignore packets which are not replies to our packet */
if (TS_Diff(&np->tx_pkt->ntp_transmit,
&np->rx_pkt->ntp_origin) != 0.0) {
continue;
}
return (1);
}
}