Simple Network Time Protocol Library

Overview

The SNTP library implements IETF RFC4330 (Simple Network Time Protocol v4).

SNTP provides a way to synchronize clocks in computer networks.

API Reference

group sntp

Simple Network Time Protocol API.

Functions

int sntp_init(struct sntp_ctx *ctx, struct sockaddr *addr, socklen_t addr_len)

Initialize SNTP context.

Parameters:
  • ctx – Address of sntp context.

  • addr – IP address of NTP/SNTP server.

  • addr_len – IP address length of NTP/SNTP server.

Returns:

0 if ok, <0 if error.

int sntp_query(struct sntp_ctx *ctx, uint32_t timeout, struct sntp_time *time)

Perform SNTP query.

Parameters:
  • ctx – Address of sntp context.

  • timeout – Timeout of waiting for sntp response (in milliseconds).

  • time – Timestamp including integer and fractional seconds since 1 Jan 1970 (output).

Returns:

0 if ok, <0 if error (-ETIMEDOUT if timeout).

void sntp_close(struct sntp_ctx *ctx)

Release SNTP context.

Parameters:
  • ctx – Address of sntp context.

int sntp_simple(const char *server, uint32_t timeout, struct sntp_time *ts)

Convenience function to query SNTP in one-shot fashion.

Convenience wrapper which calls getaddrinfo(), sntp_init(), sntp_query(), and sntp_close().

Parameters:
  • server – Address of server in format addr[:port]

  • timeout – Query timeout

  • ts – Timestamp including integer and fractional seconds since 1 Jan 1970 (output).

Returns:

0 if ok, <0 if error (-ETIMEDOUT if timeout).

int sntp_simple_addr(struct sockaddr *addr, socklen_t addr_len, uint32_t timeout, struct sntp_time *ts)

Convenience function to query SNTP in one-shot fashion using a pre-initialized address struct.

Convenience wrapper which calls sntp_init(), sntp_query() and sntp_close().

Parameters:
  • addr – IP address of NTP/SNTP server.

  • addr_len – IP address length of NTP/SNTP server.

  • timeout – Query timeout

  • ts – Timestamp including integer and fractional seconds since 1 Jan 1970 (output).

Returns:

0 if ok, <0 if error (-ETIMEDOUT if timeout).

struct sntp_time
#include <sntp.h>

Time as returned by SNTP API, fractional seconds since 1 Jan 1970.

Public Members

uint64_t seconds

Second value.

uint32_t fraction

Fractional seconds value.

struct sntp_ctx
#include <sntp.h>

SNTP context.

Public Members

struct sntp_time expected_orig_ts

Timestamp when the request was sent from client to server.

This is used to check if the originated timestamp in the server reply matches the one in client request.