File: ntp_io.c

package info (click to toggle)
chrony 1.14-7
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,480 kB
  • ctags: 2,091
  • sloc: ansic: 15,266; sh: 194; makefile: 113
file content (276 lines) | stat: -rw-r--r-- 7,412 bytes parent folder | download
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
  $Header: /cvs/src/chrony/ntp_io.c,v 1.16 2000/06/12 21:22:49 richard Exp $

  =======================================================================

  chronyd/chronyc - Programs for keeping computer clocks accurate.

  Copyright (C) 1997-1999 Richard P. Curnow
  All rights reserved.

  For conditions of use, refer to the file LICENCE.

  =======================================================================

  This file deals with the IO aspects of reading and writing NTP packets
  */

#include "sysincl.h"

#include "ntp_io.h"
#include "ntp_core.h"
#include "ntp_sources.h"
#include "sched.h"
#include "local.h"
#include "logging.h"
#include "conf.h"

#include <fcntl.h>

/* The file descriptor for the socket */
static int sock_fd;

/* Flag indicating that we have been initialised */
static int initialised=0;

/* ================================================== */

/* Forward prototypes */
static void read_from_socket(void *anything);

/* ================================================== */

static void
do_size_checks(void)
{
  /* Assertions to check the sizes of certain data types
     and the positions of certain record fields */

  /* Check that certain invariants are true */
  assert(sizeof(NTP_int32) == 4);
  assert(sizeof(NTP_int64) == 8);
  assert(sizeof(unsigned long) == 4);

  /* Check offsets of all fields in the NTP packet format */
  assert(offsetof(NTP_Packet, lvm)             ==  0);
  assert(offsetof(NTP_Packet, stratum)         ==  1);
  assert(offsetof(NTP_Packet, poll)            ==  2);
  assert(offsetof(NTP_Packet, precision)       ==  3);
  assert(offsetof(NTP_Packet, root_delay)      ==  4);
  assert(offsetof(NTP_Packet, root_dispersion) ==  8);
  assert(offsetof(NTP_Packet, reference_id)    == 12);
  assert(offsetof(NTP_Packet, reference_ts)    == 16);
  assert(offsetof(NTP_Packet, originate_ts)    == 24);
  assert(offsetof(NTP_Packet, receive_ts)      == 32);
  assert(offsetof(NTP_Packet, transmit_ts)     == 40);

}

/* ================================================== */

void
NIO_Initialise(void)
{
  struct sockaddr_in my_addr;
  unsigned short port_number;
  unsigned long bind_address;
  int on_off = 1;
  
  assert(!initialised);
  initialised = 1;

  do_size_checks();

  port_number = CNF_GetNTPPort();

  /* Open Internet domain UDP socket for NTP message transmissions */

#if 0
  sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
#else
  sock_fd = socket(AF_INET, SOCK_DGRAM, 0);
#endif 
  if (sock_fd < 0) {
    LOG_FATAL(LOGF_NtpIO, "Could not open socket : %s\n", strerror(errno));
  }

  /* Make the socket capable of re-using an old address */
  if (setsockopt(sock_fd, SOL_SOCKET, SO_REUSEADDR, &on_off, sizeof(on_off)) < 0) {
    LOG(LOGS_ERR, LOGF_NtpIO, "Could not set socket options");
    /* Don't quit - we might survive anyway */
  }

  /* Bind the port */
  my_addr.sin_family = AF_INET;
  my_addr.sin_port = htons(port_number);

  CNF_GetBindAddress(&bind_address);

  if (bind_address != 0UL) {
    my_addr.sin_addr.s_addr = htonl(bind_address);
  } else {
    my_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  }

#if 0
  LOG(LOGS_INFO, LOGF_NtpIO, "Initialising, socket fd=%d\n", sock_fd);
#endif

  if (bind(sock_fd, (struct sockaddr *) &my_addr, sizeof(my_addr)) < 0) {
    LOG_FATAL(LOGF_NtpIO, "Could not bind socket : %s\n", strerror(errno));
  }

  /* Register handler for read events on the socket */
  SCH_AddInputFileHandler(sock_fd, read_from_socket, NULL);

#if 0
  if (fcntl(sock_fd, F_SETFL, O_NONBLOCK | O_NDELAY) < 0) {
    LOG(LOGS_ERR, LOGF_NtpIO, "Could not make socket non-blocking");
  }

  if (ioctl(sock_fd, I_SETSIG, S_INPUT) < 0) {
    LOG(LOGS_ERR, LOGF_NtpIO, "Could not enable signal");
  }
#endif

  return;
}

/* ================================================== */

void
NIO_Finalise(void)
{
  if (sock_fd >= 0) {
    SCH_RemoveInputFileHandler(sock_fd);
    close(sock_fd);
  }
  sock_fd = -1;
  initialised = 0;
  return;
}

/* ================================================== */


/* ================================================== */

static void
read_from_socket(void *anything)
{
  /* This should only be called when there is something
     to read, otherwise it will block. */

  int status;
  ReceiveBuffer message;
  int message_length;
  struct sockaddr_in where_from;
  int from_length;
  unsigned int flags = 0;
  struct timeval now;
  NTP_Remote_Address remote_addr;
  double local_clock_err;

  assert(initialised);

  from_length = sizeof(where_from);
  message_length = sizeof(message);

  LCL_ReadCookedTime(&now, &local_clock_err);
  status = recvfrom(sock_fd, &message, message_length, flags,
                    (struct sockaddr *)&where_from, &from_length);

  /* Don't bother checking if read failed or why if it did.  More
     likely than not, it will be connection refused, resulting from a
     previous sendto() directing a datagram at a port that is not
     listening (which appears to generate an ICMP response, and on
     some architectures e.g. Linux this is translated into an error
     reponse on a subsequent recvfrom). */

  if (status > 0) {
    remote_addr.ip_addr = ntohl(where_from.sin_addr.s_addr);
    remote_addr.port = ntohs(where_from.sin_port);

    if (status == NTP_NORMAL_PACKET_SIZE) {

      NSR_ProcessReceive((NTP_Packet *) &message.ntp_pkt, &now, &remote_addr);

    } else if (status == sizeof(NTP_Packet)) {

      NSR_ProcessAuthenticatedReceive((NTP_Packet *) &message.ntp_pkt, &now, &remote_addr);

    } else {

      /* Just ignore the packet if it's not of a recognized length */

    }
  }
  
  return;
}

/* ================================================== */
/* Send an unauthenticated packet to a given address */

void
NIO_SendNormalPacket(NTP_Packet *packet, NTP_Remote_Address *remote_addr)
{
  struct sockaddr_in remote;

  assert(initialised);

  remote.sin_family = AF_INET;
  remote.sin_port = htons(remote_addr->port);
  remote.sin_addr.s_addr = htonl(remote_addr->ip_addr);

  if (sendto(sock_fd, (void *) packet, NTP_NORMAL_PACKET_SIZE, 0,
             (struct sockaddr *) &remote, sizeof(remote)) < 0) {
    LOG(LOGS_WARN, LOGF_NtpIO, "Could not send : %s\n", strerror(errno));
  }

  return;
}

/* ================================================== */
/* Send an authenticated packet to a given address */

void
NIO_SendAuthenticatedPacket(NTP_Packet *packet, NTP_Remote_Address *remote_addr)
{
  struct sockaddr_in remote;

  assert(initialised);

  remote.sin_family = AF_INET;
  remote.sin_port = htons(remote_addr->port);
  remote.sin_addr.s_addr = htonl(remote_addr->ip_addr);

  if (sendto(sock_fd, (void *) packet, sizeof(NTP_Packet), 0,
             (struct sockaddr *) &remote, sizeof(remote)) < 0) {
    LOG(LOGS_WARN, LOGF_NtpIO, "Could not send : %s\n", strerror(errno));
  }

  return;
}

/* ================================================== */

/* We ought to use getservbyname, but I can't really see this changing */
#define ECHO_PORT 7

void
NIO_SendEcho(NTP_Remote_Address *remote_addr)
{
  unsigned long magic_message = 0xbe7ab1e7UL;
  struct sockaddr_in addr;

  addr.sin_family = AF_INET;
  addr.sin_port = htons(ECHO_PORT);
  addr.sin_addr.s_addr = htonl(remote_addr->ip_addr);

  /* Just ignore error status on send - this is not a big deal anyway */
  sendto(sock_fd, (void *) &magic_message, sizeof(unsigned long), 0,
         (struct sockaddr *) &addr, sizeof(addr));


}