5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <netinet/ip.h>
36 #include <arpa/inet.h>
45 } __attribute__ ((packed));
50 } __attribute__ ((packed));
53 uint8_t flags; /* Mode, version and leap indicator */
54 uint8_t stratum; /* Stratum details */
55 int8_t poll; /* Maximum interval in log2 seconds */
56 int8_t precision; /* Clock precision in log2 seconds */
57 struct ntp_short rootdelay; /* Root delay */
58 struct ntp_short rootdisp; /* Root dispersion */
59 uint32_t refid; /* Reference ID */
60 struct ntp_time reftime; /* Reference timestamp */
61 struct ntp_time orgtime; /* Origin timestamp */
62 struct ntp_time rectime; /* Receive timestamp */
63 struct ntp_time xmttime; /* Transmit timestamp */
64 } __attribute__ ((packed));
66 #define OFFSET_1900_1970 2208988800UL /* 1970 - 1900 in seconds */
68 #define STEPTIME_MIN_OFFSET 0.128
70 #define LOGTOD(a) ((a) < 0 ? 1. / (1L << -(a)) : 1L << (int)(a))
72 #define NTP_FLAG_LI_SHIFT 6
73 #define NTP_FLAG_LI_MASK 0x3
74 #define NTP_FLAG_LI_NOWARNING 0x0
75 #define NTP_FLAG_LI_ADDSECOND 0x1
76 #define NTP_FLAG_LI_DELSECOND 0x2
77 #define NTP_FLAG_LI_NOTINSYNC 0x3
79 #define NTP_FLAG_VN_SHIFT 3
80 #define NTP_FLAG_VN_MASK 0x7
82 #define NTP_FLAG_MD_SHIFT 0
83 #define NTP_FLAG_MD_MASK 0x7
84 #define NTP_FLAG_MD_UNSPEC 0
85 #define NTP_FLAG_MD_ACTIVE 1
86 #define NTP_FLAG_MD_PASSIVE 2
87 #define NTP_FLAG_MD_CLIENT 3
88 #define NTP_FLAG_MD_SERVER 4
89 #define NTP_FLAG_MD_BROADCAST 5
90 #define NTP_FLAG_MD_CONTROL 6
91 #define NTP_FLAG_MD_PRIVATE 7
93 #define NTP_FLAGS_ENCODE(li, vn, md) ((uint8_t)( \
94 (((li) & NTP_FLAG_LI_MASK) << NTP_FLAG_LI_SHIFT) | \
95 (((vn) & NTP_FLAG_VN_MASK) << NTP_FLAG_VN_SHIFT) | \
96 (((md) & NTP_FLAG_MD_MASK) << NTP_FLAG_MD_SHIFT)))
98 #define NTP_FLAGS_LI_DECODE(flags) ((uint8_t)(((flags) >> NTP_FLAG_LI_SHIFT) & NTP_FLAG_LI_MASK))
99 #define NTP_FLAGS_VN_DECODE(flags) ((uint8_t)(((flags) >> NTP_FLAG_VN_SHIFT) & NTP_FLAG_VN_MASK))
100 #define NTP_FLAGS_MD_DECODE(flags) ((uint8_t)(((flags) >> NTP_FLAG_MD_SHIFT) & NTP_FLAG_MD_MASK))
102 static guint channel_watch = 0;
103 static struct timeval transmit_timeval;
104 static int transmit_fd = 0;
106 static char *timeserver = NULL;
107 static gint poll_id = 0;
108 static gint timeout_id = 0;
110 static void send_packet(int fd, const char *server)
113 struct sockaddr_in addr;
116 memset(&msg, 0, sizeof(msg));
117 msg.flags = NTP_FLAGS_ENCODE(NTP_FLAG_LI_NOTINSYNC, 4, NTP_FLAG_MD_CLIENT);
119 msg.poll = 10; // max
121 memset(&addr, 0, sizeof(addr));
122 addr.sin_family = AF_INET;
123 addr.sin_port = htons(123);
124 addr.sin_addr.s_addr = inet_addr(server);
126 gettimeofday(&transmit_timeval, NULL);
128 msg.xmttime.seconds = htonl(transmit_timeval.tv_sec + OFFSET_1900_1970);
129 msg.xmttime.fraction = htonl(transmit_timeval.tv_usec * 1000);
131 len = sendto(fd, &msg, sizeof(msg), MSG_DONTWAIT,
132 &addr, sizeof(addr));
134 connman_error("Time request for server %s failed (%d/%s)",
135 server, errno, strerror(errno));
137 if (errno == ENETUNREACH)
138 __connman_timeserver_sync_next();
143 if (len != sizeof(msg)) {
144 connman_error("Broken time request for server %s", server);
149 static gboolean next_server(gpointer user_data)
151 if (timeserver != NULL) {
156 __connman_timeserver_sync_next();
161 static gboolean next_poll(gpointer user_data)
163 if (timeserver == NULL || transmit_fd == 0)
166 send_packet(transmit_fd, timeserver);
171 static void decode_msg(void *base, size_t len, struct timeval *tv)
173 struct ntp_msg *msg = base;
174 double org, rec, xmt, dst;
175 double delay, offset;
176 static guint transmit_delay;
178 if (len < sizeof(*msg)) {
179 connman_error("Invalid response from time server");
184 connman_error("Invalid packet timestamp from time server");
188 DBG("flags : 0x%02x", msg->flags);
189 DBG("stratum : %u", msg->stratum);
190 DBG("poll : %f seconds (%d)",
191 LOGTOD(msg->poll), msg->poll);
192 DBG("precision : %f seconds (%d)",
193 LOGTOD(msg->precision), msg->precision);
194 DBG("root delay : %u seconds (fraction %u)",
195 msg->rootdelay.seconds, msg->rootdelay.fraction);
196 DBG("root disp. : %u seconds (fraction %u)",
197 msg->rootdisp.seconds, msg->rootdisp.fraction);
198 DBG("reference : 0x%04x", msg->refid);
200 transmit_delay = LOGTOD(msg->poll);
202 if (msg->flags != 0x24)
205 org = transmit_timeval.tv_sec +
206 (1.0e-6 * transmit_timeval.tv_usec) + OFFSET_1900_1970;
207 rec = ntohl(msg->rectime.seconds) +
208 ((double) ntohl(msg->rectime.fraction) / UINT_MAX);
209 xmt = ntohl(msg->xmttime.seconds) +
210 ((double) ntohl(msg->xmttime.fraction) / UINT_MAX);
211 dst = tv->tv_sec + (1.0e-6 * tv->tv_usec) + OFFSET_1900_1970;
213 DBG("org=%f rec=%f xmt=%f dst=%f", org, rec, xmt, dst);
215 offset = ((rec - org) + (xmt - dst)) / 2;
216 delay = (dst - org) - (xmt - rec);
218 DBG("offset=%f delay=%f", offset, delay);
220 /* Remove the timeout, as timeserver has responded */
222 g_source_remove(timeout_id);
225 * Now poll the server every transmit_delay seconds
226 * for time correction.
229 g_source_remove(poll_id);
231 DBG("Timeserver %s, next sync in %d seconds", timeserver, transmit_delay);
233 poll_id = g_timeout_add_seconds(transmit_delay, next_poll, NULL);
235 connman_info("ntp: time slew %+.6f s", offset);
237 if (offset < STEPTIME_MIN_OFFSET && offset > -STEPTIME_MIN_OFFSET) {
240 adj.tv_sec = (long) offset;
241 adj.tv_usec = (offset - adj.tv_sec) * 1000000;
243 DBG("adjusting time");
245 if (adjtime(&adj, &adj) < 0) {
246 connman_error("Failed to adjust time");
250 DBG("%lu seconds, %lu msecs", adj.tv_sec, adj.tv_usec);
255 gettimeofday(&cur, NULL);
256 dtime = offset + cur.tv_sec + 1.0e-6 * cur.tv_usec;
257 cur.tv_sec = (long) dtime;
258 cur.tv_usec = (dtime - cur.tv_sec) * 1000000;
262 if (settimeofday(&cur, NULL) < 0) {
263 connman_error("Failed to set time");
267 DBG("%lu seconds, %lu msecs", cur.tv_sec, cur.tv_usec);
271 static gboolean received_data(GIOChannel *channel, GIOCondition condition,
274 unsigned char buf[128];
277 struct cmsghdr *cmsg;
283 if (condition & (G_IO_HUP | G_IO_ERR | G_IO_NVAL)) {
284 connman_error("Problem with timer server channel");
289 fd = g_io_channel_unix_get_fd(channel);
292 iov.iov_len = sizeof(buf);
294 memset(&msg, 0, sizeof(msg));
297 msg.msg_control = aux;
298 msg.msg_controllen = sizeof(aux);
300 len = recvmsg(fd, &msg, MSG_DONTWAIT);
306 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
307 if (cmsg->cmsg_level != SOL_SOCKET)
310 switch (cmsg->cmsg_type) {
312 tv = (struct timeval *) CMSG_DATA(cmsg);
317 decode_msg(iov.iov_base, iov.iov_len, tv);
322 static void start_ntp(char *server)
325 struct sockaddr_in addr;
326 int tos = IPTOS_LOWDELAY, timestamp = 1;
331 DBG("server %s", server);
333 if (channel_watch > 0)
336 transmit_fd = socket(PF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
337 if (transmit_fd < 0) {
338 connman_error("Failed to open time server socket");
342 memset(&addr, 0, sizeof(addr));
343 addr.sin_family = AF_INET;
345 if (bind(transmit_fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
346 connman_error("Failed to bind time server socket");
351 if (setsockopt(transmit_fd, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)) < 0) {
352 connman_error("Failed to set type of service option");
357 if (setsockopt(transmit_fd, SOL_SOCKET, SO_TIMESTAMP, ×tamp,
358 sizeof(timestamp)) < 0) {
359 connman_error("Failed to enable timestamp support");
364 channel = g_io_channel_unix_new(transmit_fd);
365 if (channel == NULL) {
370 g_io_channel_set_encoding(channel, NULL, NULL);
371 g_io_channel_set_buffered(channel, FALSE);
373 g_io_channel_set_close_on_unref(channel, TRUE);
375 channel_watch = g_io_add_watch_full(channel, G_PRIORITY_DEFAULT,
376 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
377 received_data, NULL, NULL);
379 g_io_channel_unref(channel);
382 send_packet(transmit_fd, server);
385 int __connman_ntp_start(char *server)
392 if (timeserver != NULL)
395 timeserver = g_strdup(server);
397 start_ntp(timeserver);
400 * Add a fallback timeout , preferably short, 5 sec here,
401 * to fallback on the next server.
404 timeout_id = g_timeout_add_seconds(5, next_server, NULL);
409 void __connman_ntp_stop()
414 g_source_remove(poll_id);
417 g_source_remove(timeout_id);
419 if (channel_watch > 0) {
420 g_source_remove(channel_watch);
425 if (timeserver != NULL) {