2014-04-11 02:15:06 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* netutils/ntpclient/ntpclient.c
|
|
|
|
*
|
2016-06-09 16:18:49 +02:00
|
|
|
* Copyright (C) 2014, 2016 Gregory Nutt. All rights reserved.
|
2014-04-11 02:15:06 +02:00
|
|
|
* Author: Gregory Nutt <gnutt@nuttx.org>
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 3. Neither the name NuttX nor the names of its contributors may be
|
|
|
|
* used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 THE
|
|
|
|
* COPYRIGHT OWNER 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.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <sys/socket.h>
|
2015-02-15 22:18:35 +01:00
|
|
|
#include <sys/time.h>
|
2014-04-11 02:15:06 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <debug.h>
|
|
|
|
|
2014-07-05 15:06:51 +02:00
|
|
|
#include <netinet/in.h>
|
|
|
|
|
2016-07-11 01:47:07 +02:00
|
|
|
#ifdef CONFIG_LIBC_NETDB
|
|
|
|
# include <netdb.h>
|
|
|
|
# include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
|
2016-07-11 18:11:18 +02:00
|
|
|
#include "netutils/ntpclient.h"
|
2014-04-11 02:15:06 +02:00
|
|
|
|
|
|
|
#include "ntpv3.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
2016-06-09 16:18:49 +02:00
|
|
|
|
2016-07-11 01:47:07 +02:00
|
|
|
/* Configuration ************************************************************/
|
|
|
|
|
|
|
|
#if defined(CONFIG_LIBC_NETDB) && !defined(CONFIG_NETUTILS_NTPCLIENT_SERVER)
|
|
|
|
# error CONFIG_NETUTILS_NTPCLIENT_SERVER my be provided
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(CONFIG_LIBC_NETDB) && !defined(CONFIG_NETUTILS_NTPCLIENT_SERVERIP)
|
|
|
|
# error CONFIG_NETUTILS_NTPCLIENT_SERVERIP my be provided
|
|
|
|
#endif
|
|
|
|
|
2014-04-11 02:15:06 +02:00
|
|
|
/* NTP Time is seconds since 1900. Convert to Unix time which is seconds
|
|
|
|
* since 1970
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define NTP2UNIX_TRANLSLATION 2208988800u
|
|
|
|
#define NTP_VERSION 3
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
2016-06-09 16:18:49 +02:00
|
|
|
|
2014-04-11 02:15:06 +02:00
|
|
|
/* This enumeration describes the state of the NTP daemon */
|
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
enum ntpc_daemon_e
|
2014-04-11 02:15:06 +02:00
|
|
|
{
|
|
|
|
NTP_NOT_RUNNING = 0,
|
|
|
|
NTP_STARTED,
|
|
|
|
NTP_RUNNING,
|
|
|
|
NTP_STOP_REQUESTED,
|
|
|
|
NTP_STOPPED
|
|
|
|
};
|
|
|
|
|
2015-05-06 15:54:22 +02:00
|
|
|
/* This type describes the state of the NTP client daemon. Only one
|
2014-04-11 02:15:06 +02:00
|
|
|
* instance of the NTP daemon is permitted in this implementation.
|
|
|
|
*/
|
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
struct ntpc_daemon_s
|
2014-04-11 02:15:06 +02:00
|
|
|
{
|
2014-04-11 16:27:15 +02:00
|
|
|
volatile uint8_t state; /* See enum ntpc_daemon_e */
|
2014-04-11 02:15:06 +02:00
|
|
|
sem_t interlock; /* Used to synchronize start and stop events */
|
|
|
|
pid_t pid; /* Task ID of the NTP daemon */
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Data
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/* This type describes the state of the NTP client daemon. Only once
|
|
|
|
* instance of the NTP daemon is permitted in this implementation. This
|
|
|
|
* limitation is due only to this global data structure.
|
|
|
|
*/
|
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
static struct ntpc_daemon_s g_ntpc_daemon;
|
2014-04-11 02:15:06 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Functions
|
|
|
|
****************************************************************************/
|
2016-06-09 16:18:49 +02:00
|
|
|
|
2014-04-11 02:15:06 +02:00
|
|
|
/****************************************************************************
|
2014-04-11 16:27:15 +02:00
|
|
|
* Name: ntpc_getuint32
|
2014-04-11 02:15:06 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Return the big-endian, 4-byte value in network (big-endian) order.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
static inline uint32_t ntpc_getuint32(FAR uint8_t *ptr)
|
2014-04-11 02:15:06 +02:00
|
|
|
{
|
|
|
|
/* Network order is big-endian; host order is irrelevant */
|
|
|
|
|
2014-04-12 16:03:03 +02:00
|
|
|
return (uint32_t)ptr[3] | /* MS byte appears first in data stream */
|
|
|
|
((uint32_t)ptr[2] << 8) |
|
|
|
|
((uint32_t)ptr[1] << 16) |
|
|
|
|
((uint32_t)ptr[0] << 24);
|
2014-04-11 02:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-04-11 16:27:15 +02:00
|
|
|
* Name: ntpc_settime
|
2014-04-11 02:15:06 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Given the NTP time in seconds, set the system time
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
static void ntpc_settime(FAR uint8_t *timestamp)
|
2014-04-11 02:15:06 +02:00
|
|
|
{
|
|
|
|
struct timespec tp;
|
|
|
|
time_t seconds;
|
|
|
|
uint32_t frac;
|
|
|
|
uint32_t nsec;
|
|
|
|
#ifdef CONFIG_HAVE_LONG_LONG
|
|
|
|
uint64_t tmp;
|
|
|
|
#else
|
|
|
|
uint32_t a16;
|
|
|
|
uint32_t b0;
|
|
|
|
uint32_t t32;
|
|
|
|
uint32_t t16;
|
|
|
|
uint32_t t0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* NTP timestamps are represented as a 64-bit fixed-point number, in
|
|
|
|
* seconds relative to 0000 UT on 1 January 1900. The integer part is
|
|
|
|
* in the first 32 bits and the fraction part in the last 32 bits, as
|
|
|
|
* shown in the following diagram.
|
|
|
|
*
|
|
|
|
* 0 1 2 3
|
|
|
|
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
* | Integer Part |
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
* | Fraction Part |
|
|
|
|
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
|
|
|
*/
|
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
seconds = ntpc_getuint32(timestamp);
|
2014-04-11 02:15:06 +02:00
|
|
|
|
|
|
|
/* Translate seconds to account for the difference in the origin time */
|
|
|
|
|
|
|
|
if (seconds > NTP2UNIX_TRANLSLATION)
|
|
|
|
{
|
|
|
|
seconds -= NTP2UNIX_TRANLSLATION;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Conversion of the fractional part to nanoseconds:
|
|
|
|
*
|
|
|
|
* NSec = (f * 1,000,000,000) / 4,294,967,296
|
|
|
|
* = (f * (5**9 * 2**9) / (2**32)
|
|
|
|
* = (f * 5**9) / (2**23)
|
|
|
|
* = (f * 1,953,125) / 8,388,608
|
|
|
|
*/
|
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
frac = ntpc_getuint32(timestamp + 4);
|
2014-04-11 02:15:06 +02:00
|
|
|
#ifdef CONFIG_HAVE_LONG_LONG
|
|
|
|
/* if we have 64-bit long long values, then the computation is easy */
|
|
|
|
|
|
|
|
tmp = ((uint64_t)frac * 1953125) >> 23;
|
|
|
|
nsec = (uint32_t)tmp;
|
|
|
|
|
|
|
|
#else
|
|
|
|
/* If we don't have 64 bit integer types, then the calculation is a little
|
|
|
|
* more complex:
|
|
|
|
*
|
|
|
|
* Let f = a << 16 + b
|
|
|
|
* 1,953,125 = 0x1d << 16 + 0xcd65
|
|
|
|
* NSec << 23 = ((a << 16) + b) * ((0x1d << 16) + 0xcd65)
|
|
|
|
* = (a << 16) * 0x1d << 16) +
|
|
|
|
* (a << 16) * 0xcd65 +
|
|
|
|
* b * 0x1d << 16) +
|
|
|
|
* b * 0xcd65;
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Break the fractional part up into two values */
|
|
|
|
|
|
|
|
a16 = frac >> 16;
|
|
|
|
b0 = frac & 0xffff;
|
|
|
|
|
|
|
|
/* Get the b32 and b0 terms
|
|
|
|
*
|
|
|
|
* t32 = (a << 16) * 0x1d << 16)
|
|
|
|
* t0 = b * 0xcd65
|
|
|
|
*/
|
|
|
|
|
|
|
|
t32 = 0x001d * a16;
|
2014-04-11 16:27:15 +02:00
|
|
|
t0 = 0xcd65 * b0;
|
2014-04-11 02:15:06 +02:00
|
|
|
|
|
|
|
/* Get the first b16 term
|
|
|
|
*
|
|
|
|
* (a << 16) * 0xcd65
|
|
|
|
*/
|
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
t16 = 0xcd65 * a16;
|
2014-04-11 02:15:06 +02:00
|
|
|
|
|
|
|
/* Add the upper 16-bits to the b32 accumulator */
|
|
|
|
|
|
|
|
t32 += (t16 >> 16);
|
|
|
|
|
|
|
|
/* Add the lower 16-bits to the b0 accumulator, handling carry to the b32
|
|
|
|
* accumulator
|
|
|
|
*/
|
|
|
|
|
|
|
|
t16 <<= 16;
|
|
|
|
if (t0 > (0xffffffff - t16))
|
|
|
|
{
|
|
|
|
t32++;
|
|
|
|
}
|
2014-04-11 17:24:59 +02:00
|
|
|
|
|
|
|
t0 += t16;
|
2014-04-11 02:15:06 +02:00
|
|
|
|
|
|
|
/* Get the second b16 term
|
|
|
|
*
|
2014-04-11 17:24:59 +02:00
|
|
|
* b * (0x1d << 16)
|
2014-04-11 02:15:06 +02:00
|
|
|
*/
|
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
t16 = 0x001d * b0;
|
2014-04-11 02:15:06 +02:00
|
|
|
|
|
|
|
/* Add the upper 16-bits to the b32 accumulator */
|
|
|
|
|
|
|
|
t32 += (t16 >> 16);
|
|
|
|
|
|
|
|
/* Add the lower 16-bits to the b0 accumulator, handling carry to the b32
|
|
|
|
* accumulator
|
|
|
|
*/
|
|
|
|
|
|
|
|
t16 <<= 16;
|
|
|
|
if (t0 > (0xffffffff - t16))
|
|
|
|
{
|
|
|
|
t32++;
|
|
|
|
}
|
2014-04-11 17:24:59 +02:00
|
|
|
|
|
|
|
t0 += t16;
|
2014-04-11 02:15:06 +02:00
|
|
|
|
|
|
|
/* t32 and t0 represent the 64 bit product. Now shift right by 23 bits to
|
|
|
|
* accomplish the divide by by 2**23.
|
|
|
|
*/
|
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
nsec = (t32 << (32 - 23)) + (t0 >> 23);
|
2014-04-11 02:15:06 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Set the system time */
|
|
|
|
|
|
|
|
tp.tv_sec = seconds;
|
|
|
|
tp.tv_nsec = nsec;
|
|
|
|
clock_settime(CLOCK_REALTIME, &tp);
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
sinfo("Set time to %lu seconds: %d\n", (unsigned long)tp.tv_sec, ret);
|
2014-04-11 02:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-04-11 16:27:15 +02:00
|
|
|
* Name: ntpc_daemon
|
2014-04-11 02:15:06 +02:00
|
|
|
*
|
|
|
|
* Description:
|
2017-05-11 21:40:03 +02:00
|
|
|
* This the NTP client daemon. This is a *very* minimal
|
2014-04-11 02:15:06 +02:00
|
|
|
* implementation! An NTP request is and the system clock is set when the
|
|
|
|
* response is received
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
static int ntpc_daemon(int argc, char **argv)
|
2014-04-11 02:15:06 +02:00
|
|
|
{
|
|
|
|
struct sockaddr_in server;
|
|
|
|
struct ntp_datagram_s xmit;
|
|
|
|
struct ntp_datagram_s recv;
|
|
|
|
struct timeval tv;
|
2016-07-11 01:47:07 +02:00
|
|
|
|
|
|
|
#ifdef CONFIG_LIBC_NETDB
|
|
|
|
struct hostent *he;
|
|
|
|
struct in_addr **addr_list;
|
|
|
|
#endif
|
|
|
|
|
2014-04-11 02:15:06 +02:00
|
|
|
socklen_t socklen;
|
|
|
|
ssize_t nbytes;
|
|
|
|
int exitcode = EXIT_SUCCESS;
|
2016-06-09 16:18:49 +02:00
|
|
|
int retry = 0;
|
2014-04-11 02:15:06 +02:00
|
|
|
int sd;
|
2016-06-09 16:18:49 +02:00
|
|
|
int ret;
|
2014-04-11 02:15:06 +02:00
|
|
|
|
|
|
|
/* Indicate that we have started */
|
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
g_ntpc_daemon.state = NTP_RUNNING;
|
|
|
|
sem_post(&g_ntpc_daemon.interlock);
|
2014-04-11 02:15:06 +02:00
|
|
|
|
|
|
|
/* Create a datagram socket */
|
|
|
|
|
|
|
|
sd = socket(AF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (sd < 0)
|
|
|
|
{
|
2016-06-11 23:51:27 +02:00
|
|
|
nerr("ERROR: socket failed: %d\n", errno);
|
2014-04-11 02:15:06 +02:00
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
g_ntpc_daemon.state = NTP_STOPPED;
|
|
|
|
sem_post(&g_ntpc_daemon.interlock);
|
2014-04-11 02:15:06 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup a receive timeout on the socket */
|
|
|
|
|
|
|
|
tv.tv_sec = 5;
|
|
|
|
tv.tv_usec = 0;
|
|
|
|
|
|
|
|
ret = setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(struct timeval));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:51:27 +02:00
|
|
|
nerr("ERROR: setsockopt failed: %d\n", errno);
|
2014-04-11 02:15:06 +02:00
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
g_ntpc_daemon.state = NTP_STOPPED;
|
|
|
|
sem_post(&g_ntpc_daemon.interlock);
|
2014-04-11 02:15:06 +02:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Setup or sockaddr_in struct with information about the server we are
|
2017-05-11 21:40:03 +02:00
|
|
|
* going to ask the time from.
|
2014-04-11 02:15:06 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
memset(&server, 0, sizeof(struct sockaddr_in));
|
|
|
|
server.sin_family = AF_INET;
|
|
|
|
server.sin_port = htons(CONFIG_NETUTILS_NTPCLIENT_PORTNO);
|
2016-07-11 01:47:07 +02:00
|
|
|
|
|
|
|
#ifndef CONFIG_LIBC_NETDB
|
2014-04-11 02:15:06 +02:00
|
|
|
server.sin_addr.s_addr = htonl(CONFIG_NETUTILS_NTPCLIENT_SERVERIP);
|
2016-07-11 01:47:07 +02:00
|
|
|
#else
|
|
|
|
he = gethostbyname(CONFIG_NETUTILS_NTPCLIENT_SERVER);
|
|
|
|
if (he != NULL && he->h_addrtype == AF_INET)
|
|
|
|
{
|
|
|
|
addr_list = (struct in_addr **)he->h_addr_list;
|
|
|
|
server.sin_addr.s_addr = addr_list[0]->s_addr;
|
|
|
|
ninfo("INFO: '%s' resolved to: %s\n",
|
|
|
|
CONFIG_NETUTILS_NTPCLIENT_SERVER,
|
|
|
|
inet_ntoa(server.sin_addr));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nerr("ERROR: Failed to resolve '%s'\n", CONFIG_NETUTILS_NTPCLIENT_SERVER);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
#endif
|
2014-04-11 02:15:06 +02:00
|
|
|
|
|
|
|
/* Here we do the communication with the NTP server. This is a very simple
|
2014-04-11 17:02:50 +02:00
|
|
|
* client architecture. A request is sent and then a NTP packet is received
|
|
|
|
* and used to set the current time.
|
|
|
|
*
|
2014-04-11 17:24:59 +02:00
|
|
|
* NOTE that the scheduler is locked whenever this loop runs. That
|
2014-04-11 17:02:50 +02:00
|
|
|
* assures both: (1) that there are no asynchronous stop requests and
|
|
|
|
* (2) that we are not suspended while in critical moments when we about
|
|
|
|
* to set the new time. This sounds harsh, but this function is suspended
|
2014-04-13 21:18:06 +02:00
|
|
|
* most of the time either: (1) sending a datagram, (2) receiving a datagram,
|
2014-04-11 17:02:50 +02:00
|
|
|
* or (3) waiting for the next poll cycle.
|
2014-04-13 21:18:06 +02:00
|
|
|
*
|
|
|
|
* TODO: The first datagram that is sent is usually lost. That is because
|
|
|
|
* the MAC address of the NTP server is not in the ARP table. This is
|
|
|
|
* particularly bad here because the request will not be sent again until
|
|
|
|
* the long delay expires leaving the system with bad time for a long time
|
|
|
|
* initially. Solutions:
|
|
|
|
*
|
|
|
|
* 1. Fix send logic so that it assures that the ARP request has been
|
|
|
|
* sent and the entry is in the ARP table before sending the packet
|
|
|
|
* (best).
|
|
|
|
* 2. Add some ad hoc logic here so that there is no delay until at least
|
|
|
|
* one good time is received.
|
2014-04-11 02:15:06 +02:00
|
|
|
*/
|
|
|
|
|
2014-04-11 17:02:50 +02:00
|
|
|
sched_lock();
|
2014-04-12 16:03:03 +02:00
|
|
|
while (g_ntpc_daemon.state != NTP_STOP_REQUESTED)
|
2014-04-11 02:15:06 +02:00
|
|
|
{
|
2014-04-11 17:24:59 +02:00
|
|
|
/* Format the transmit datagram */
|
|
|
|
|
2014-04-11 02:15:06 +02:00
|
|
|
memset(&xmit, 0, sizeof(xmit));
|
|
|
|
xmit.lvm = MKLVM(0, 3, NTP_VERSION);
|
|
|
|
|
2016-06-11 19:55:38 +02:00
|
|
|
sinfo("Sending a NTP packet\n");
|
2014-04-11 02:15:06 +02:00
|
|
|
|
|
|
|
ret = sendto(sd, &xmit, sizeof(struct ntp_datagram_s),
|
|
|
|
0, (FAR struct sockaddr *)&server,
|
|
|
|
sizeof(struct sockaddr_in));
|
2014-04-11 17:02:50 +02:00
|
|
|
|
2014-04-11 02:15:06 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2014-04-11 17:24:59 +02:00
|
|
|
/* Check if we received a signal. That is not an error but
|
|
|
|
* other error events will terminate the client.
|
|
|
|
*/
|
|
|
|
|
2014-04-11 17:02:50 +02:00
|
|
|
int errval = errno;
|
|
|
|
if (errval != EINTR)
|
2014-04-11 16:27:15 +02:00
|
|
|
{
|
2016-06-11 23:51:27 +02:00
|
|
|
nerr("ERROR: sendto() failed: %d\n", errval);
|
2014-04-11 16:27:15 +02:00
|
|
|
exitcode = EXIT_FAILURE;
|
2014-04-11 17:02:50 +02:00
|
|
|
break;
|
2014-04-11 16:27:15 +02:00
|
|
|
}
|
2014-04-11 17:02:50 +02:00
|
|
|
|
|
|
|
/* Go back to the top of the loop if we were interrupted
|
|
|
|
* by a signal. The signal might mean that we were
|
|
|
|
* requested to stop(?)
|
|
|
|
*/
|
|
|
|
|
|
|
|
continue;
|
2014-04-11 02:15:06 +02:00
|
|
|
}
|
|
|
|
|
2014-04-11 17:24:59 +02:00
|
|
|
/* Attempt to receive a packet (with a timeout that was set up via
|
|
|
|
* setsockopt() above)
|
|
|
|
*/
|
2014-04-11 02:15:06 +02:00
|
|
|
|
|
|
|
socklen = sizeof(struct sockaddr_in);
|
|
|
|
nbytes = recvfrom(sd, (void *)&recv, sizeof(struct ntp_datagram_s),
|
|
|
|
0, (FAR struct sockaddr *)&server, &socklen);
|
2014-04-11 17:02:50 +02:00
|
|
|
|
2014-04-11 17:24:59 +02:00
|
|
|
/* Check if the received message was long enough to be a valid NTP
|
|
|
|
* datagram.
|
|
|
|
*/
|
|
|
|
|
2014-04-12 16:03:03 +02:00
|
|
|
if (nbytes >= (ssize_t)NTP_DATAGRAM_MINSIZE)
|
2014-04-11 02:15:06 +02:00
|
|
|
{
|
2016-06-11 19:55:38 +02:00
|
|
|
sinfo("Setting time\n");
|
2014-04-11 16:27:15 +02:00
|
|
|
ntpc_settime(recv.recvtimestamp);
|
2016-07-11 01:47:07 +02:00
|
|
|
retry = 0;
|
2014-04-11 02:15:06 +02:00
|
|
|
}
|
|
|
|
|
2014-04-11 17:24:59 +02:00
|
|
|
/* Check for errors. Note that properly received, short datagrams
|
|
|
|
* are simply ignored.
|
|
|
|
*/
|
|
|
|
|
|
|
|
else if (nbytes < 0)
|
|
|
|
{
|
|
|
|
/* Check if we received a signal. That is not an error but
|
|
|
|
* other error events will terminate the client.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int errval = errno;
|
|
|
|
if (errval != EINTR)
|
|
|
|
{
|
2016-06-09 16:18:49 +02:00
|
|
|
/* Allow up to three retries */
|
|
|
|
|
|
|
|
if (++retry < 3)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Then declare the failure */
|
|
|
|
|
2016-06-11 23:51:27 +02:00
|
|
|
nerr("ERROR: recvfrom() failed: %d\n", errval);
|
2014-04-11 17:24:59 +02:00
|
|
|
exitcode = EXIT_FAILURE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
/* A full implementation of an NTP client would require much more. I
|
|
|
|
* think we can skip most of that here.
|
2014-04-11 02:15:06 +02:00
|
|
|
*/
|
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
if (g_ntpc_daemon.state == NTP_RUNNING)
|
|
|
|
{
|
2016-06-11 19:55:38 +02:00
|
|
|
sinfo("Waiting for %d seconds\n",
|
2014-04-11 16:27:15 +02:00
|
|
|
CONFIG_NETUTILS_NTPCLIENT_POLLDELAYSEC);
|
|
|
|
|
|
|
|
(void)sleep(CONFIG_NETUTILS_NTPCLIENT_POLLDELAYSEC);
|
|
|
|
}
|
2014-04-11 02:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The NTP client is terminating */
|
|
|
|
|
2014-04-11 17:02:50 +02:00
|
|
|
sched_unlock();
|
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
g_ntpc_daemon.state = NTP_STOPPED;
|
|
|
|
sem_post(&g_ntpc_daemon.interlock);
|
2014-04-11 02:15:06 +02:00
|
|
|
return exitcode;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
/****************************************************************************
|
2014-04-11 16:27:15 +02:00
|
|
|
* Name: ntpc_start
|
2014-04-11 02:15:06 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Start the NTP daemon
|
|
|
|
*
|
2014-04-11 16:27:15 +02:00
|
|
|
* Returned Value:
|
|
|
|
* On success, the non-negative task ID of the NTPC daemon is returned;
|
|
|
|
* On failure, a negated errno value is returned.
|
|
|
|
*
|
2014-04-11 02:15:06 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
int ntpc_start(void)
|
2014-04-11 02:15:06 +02:00
|
|
|
{
|
|
|
|
/* Is the NTP in a non-running state? */
|
|
|
|
|
|
|
|
sched_lock();
|
2014-04-11 16:27:15 +02:00
|
|
|
if (g_ntpc_daemon.state == NTP_NOT_RUNNING ||
|
|
|
|
g_ntpc_daemon.state == NTP_STOPPED)
|
2014-04-11 02:15:06 +02:00
|
|
|
{
|
|
|
|
/* Is this the first time that the NTP daemon has been started? */
|
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
if (g_ntpc_daemon.state == NTP_NOT_RUNNING)
|
2014-04-11 02:15:06 +02:00
|
|
|
{
|
|
|
|
/* Yes... then we will need to initialize the state structure */
|
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
sem_init(&g_ntpc_daemon.interlock, 0, 0);
|
2014-04-11 02:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Start the NTP daemon */
|
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
g_ntpc_daemon.state = NTP_STARTED;
|
|
|
|
g_ntpc_daemon.pid =
|
2014-09-01 21:21:15 +02:00
|
|
|
task_create("NTP daemon", CONFIG_NETUTILS_NTPCLIENT_SERVERPRIO,
|
2014-04-11 16:27:15 +02:00
|
|
|
CONFIG_NETUTILS_NTPCLIENT_STACKSIZE, ntpc_daemon,
|
2014-04-11 02:15:06 +02:00
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* Handle failures to start the NTP daemon */
|
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
if (g_ntpc_daemon.pid < 0)
|
2014-04-11 02:15:06 +02:00
|
|
|
{
|
|
|
|
int errval = errno;
|
|
|
|
DEBUGASSERT(errval > 0);
|
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
g_ntpc_daemon.state = NTP_STOPPED;
|
2016-06-11 23:51:27 +02:00
|
|
|
nerr("ERROR: Failed to start the NTP daemon\n", errval);
|
2015-05-06 15:54:22 +02:00
|
|
|
sched_unlock();
|
2014-04-11 02:15:06 +02:00
|
|
|
return -errval;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for any daemon state change */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2014-04-11 16:27:15 +02:00
|
|
|
(void)sem_wait(&g_ntpc_daemon.interlock);
|
2014-04-11 02:15:06 +02:00
|
|
|
}
|
2014-04-11 16:27:15 +02:00
|
|
|
while (g_ntpc_daemon.state == NTP_STARTED);
|
2014-04-11 02:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sched_unlock();
|
2014-04-11 16:27:15 +02:00
|
|
|
return g_ntpc_daemon.pid;
|
2014-04-11 02:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
2014-04-11 16:27:15 +02:00
|
|
|
* Name: ntpc_stop
|
2014-04-11 02:15:06 +02:00
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Stop the NTP daemon
|
|
|
|
*
|
2014-04-11 16:27:15 +02:00
|
|
|
* Returned Value:
|
|
|
|
* Zero on success; a negated errno value on failure. The current
|
|
|
|
* implementation only returns success.
|
|
|
|
*
|
2014-04-11 02:15:06 +02:00
|
|
|
****************************************************************************/
|
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
#ifndef CONFIG_DISABLE_SIGNALS
|
|
|
|
int ntpc_stop(void)
|
2014-04-11 02:15:06 +02:00
|
|
|
{
|
2014-04-11 16:27:15 +02:00
|
|
|
int ret;
|
|
|
|
|
2014-04-11 02:15:06 +02:00
|
|
|
/* Is the NTP in a running state? */
|
|
|
|
|
|
|
|
sched_lock();
|
2014-04-11 16:27:15 +02:00
|
|
|
if (g_ntpc_daemon.state == NTP_STARTED ||
|
|
|
|
g_ntpc_daemon.state == NTP_RUNNING)
|
2014-04-11 02:15:06 +02:00
|
|
|
{
|
|
|
|
/* Yes.. request that the daemon stop. */
|
|
|
|
|
2014-04-11 16:27:15 +02:00
|
|
|
g_ntpc_daemon.state = NTP_STOP_REQUESTED;
|
2014-04-11 02:15:06 +02:00
|
|
|
|
|
|
|
/* Wait for any daemon state change */
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
2014-04-11 16:27:15 +02:00
|
|
|
/* Signal the NTP client */
|
|
|
|
|
|
|
|
ret = kill(g_ntpc_daemon.pid,
|
|
|
|
CONFIG_NETUTILS_NTPCLIENT_SIGWAKEUP);
|
|
|
|
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-06-11 23:51:27 +02:00
|
|
|
nerr("ERROR: kill pid %d failed: %d\n",
|
2014-04-11 16:27:15 +02:00
|
|
|
g_ntpc_daemon.pid, errno);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for the NTP client to respond to the stop request */
|
|
|
|
|
|
|
|
(void)sem_wait(&g_ntpc_daemon.interlock);
|
2014-04-11 02:15:06 +02:00
|
|
|
}
|
2014-04-11 16:27:15 +02:00
|
|
|
while (g_ntpc_daemon.state == NTP_STOP_REQUESTED);
|
2014-04-11 02:15:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sched_unlock();
|
|
|
|
return OK;
|
|
|
|
}
|
2014-04-11 16:27:15 +02:00
|
|
|
#endif
|