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.

Return
0 if ok, <0 if error.
Parameters
  • ctx: Address of sntp context.
  • addr: IP address of NTP/SNTP server.
  • addr_len: IP address length of NTP/SNTP server.

int sntp_request(struct sntp_ctx *ctx, u32_t timeout, u64_t *epoch_time)

SNTP query with seconds precision (deprecated)

Return
0 if ok, <0 if error (-ETIMEDOUT if timeout).
Parameters
  • ctx: Address of sntp context.
  • timeout: Timeout of waiting for sntp response (in milliseconds).
  • epoch_time: Seconds since 1 January 1970 (output).

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

Perform SNTP query.

Return
0 if ok, <0 if error (-ETIMEDOUT if timeout).
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).

void sntp_close(struct sntp_ctx *ctx)

Release SNTP context.

Parameters
  • ctx: Address of sntp context.

int sntp_simple(const char *server, u32_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().

Return
0 if ok, <0 if error (-ETIMEDOUT if timeout).
Parameters
  • server: Address of server in format addr[:port]
  • timeout: Query timeout
  • time: Timestamp including integer and fractional seconds since 1 Jan 1970 (output).

struct sntp_ctx
#include <sntp.h>

SNTP context

struct sntp_time
#include <sntp.h>

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