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 *time)

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

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

Returns

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

struct sntp_ctx
#include <sntp.h>

SNTP context

Public Members

uint32_t 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.

struct sntp_time
#include <sntp.h>

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