Doxygen Book
ntputil.c File Reference

NTP utility functions. More...

#include <errno.h>
#include <netdb.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "ntputil.h"
Include dependency graph for ntputil.c:

Go to the source code of this file.

Classes

struct  _ntp_timestamp_t
 A custom data type to represent NTP timestamp format. More...
 
struct  _ntp_packet_t
 A custom data type to represent NTP packet header format. More...
 

Typedefs

typedef struct _ntp_timestamp_t ntp_timestamp_t
 A custom data type to represent NTP timestamp format. More...
 
typedef struct _ntp_packet_t ntp_packet_t
 A custom data type to represent NTP packet header format. More...
 

Functions

uint32_t _convert_to_host_byte_order (uint32_t in)
 Wrapper function of ntohl. More...
 
int64_t ntputil_get_epoch (uint32_t hnums, char **hnames, uint16_t *ports)
 Get NTP timestamps from the given or public NTP servers. More...
 

Variables

const uint64_t NTPUTIL_TIMESTAMP_DELTA = 2208988800ULL
 
const double NTPUTIL_MAX_FRAC_DOUBLE = 4294967295.0L
 
const int64_t NTPUTIL_SEC_TO_USEC_MULTIPLIER = 1000000
 
const char NTPUTIL_DEFAULT_HNAME [] = "pool.ntp.org"
 
const uint16_t NTPUTIL_DEFAULT_PORT = 123
 

Detailed Description

NTP utility functions.

Copyright (C) 2021 Wook Song wook1.nosp@m.6.so.nosp@m.ng@sa.nosp@m.msun.nosp@m.g.com

Date
16 Jul 2021
See also
https://github.com/nnstreamer/nnstreamer
Author
Wook Song wook1.nosp@m.6.so.nosp@m.ng@sa.nosp@m.msun.nosp@m.g.com
Bug:
No known bugs except for NYI items
Todo:
Need to support caching and polling timer mechanism

Definition in file ntputil.c.

Typedef Documentation

◆ ntp_packet_t

typedef struct _ntp_packet_t ntp_packet_t

A custom data type to represent NTP packet header format.

NTP Packet Header Format (https://www.ietf.org/rfc/rfc5905.txt p.18) 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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |LI | VN |Mode | Stratum | Poll | Precision | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Root Delay | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Root Dispersion | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Reference ID | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |

  • Reference Timestamp (64) + | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
  • Origin Timestamp (64) + | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
  • Receive Timestamp (64) + | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
  • Transmit Timestamp (64) + | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | . . . Extension Field 1 (variable) . . . | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | . . . Extension Field 2 (variable) . . . | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Key Identifier | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | | | dgst (128) | | | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

◆ ntp_timestamp_t

A custom data type to represent NTP timestamp format.

NTP Timestamp Format (https://www.ietf.org/rfc/rfc5905.txt p.12) 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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Seconds | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | Fraction | +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+

Function Documentation

◆ _convert_to_host_byte_order()

uint32_t _convert_to_host_byte_order ( uint32_t  in)

Wrapper function of ntohl.

Converting network byte order to host byte order.

Definition at line 125 of file ntputil.c.

Here is the caller graph for this function:

◆ ntputil_get_epoch()

int64_t ntputil_get_epoch ( uint32_t  hnums,
char **  hnames,
uint16_t *  ports 
)

Get NTP timestamps from the given or public NTP servers.

Parameters
[in]hnumsA number of hostname and port pairs. If 0 is given, the NTP server pool will be used.
[in]hnamesA list of hostname
[in]portsA list of port
Returns
an Unix epoch time as microseconds on success, negative values on error
Note
ntp_timestamp_t recv_ts in ntp_packet_t means the timestamp as the packet left the NTP server. 'sec' corresponds to the seconds passed since 1900 and 'frac' is needed to convert seconds to smaller units of a second such as microsceonds. Note that the bit/byte order of those data should be converted to the host's endianness.
NTP uses an epoch of January 1, 1900 while the Unix epoch is the number of seconds that have elapsed since January 1, 1970. For this reason, we subtract 70 years worth of seconds from the seconds since 1900

Definition at line 140 of file ntputil.c.

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ NTPUTIL_DEFAULT_HNAME

const char NTPUTIL_DEFAULT_HNAME[] = "pool.ntp.org"

Definition at line 118 of file ntputil.c.

◆ NTPUTIL_DEFAULT_PORT

const uint16_t NTPUTIL_DEFAULT_PORT = 123

Definition at line 119 of file ntputil.c.

◆ NTPUTIL_MAX_FRAC_DOUBLE

const double NTPUTIL_MAX_FRAC_DOUBLE = 4294967295.0L

Definition at line 116 of file ntputil.c.

◆ NTPUTIL_SEC_TO_USEC_MULTIPLIER

const int64_t NTPUTIL_SEC_TO_USEC_MULTIPLIER = 1000000

Definition at line 117 of file ntputil.c.

◆ NTPUTIL_TIMESTAMP_DELTA

const uint64_t NTPUTIL_TIMESTAMP_DELTA = 2208988800ULL

Definition at line 115 of file ntputil.c.