Data Structures | Macros | Functions
ntp.h File Reference

Network Time Protocol. More...

#include <stdint.h>
#include <ipxe/in.h>
#include <ipxe/interface.h>

Go to the source code of this file.

Data Structures

struct  ntp_short
 An NTP short-format timestamp. More...
struct  ntp_timestamp
 An NTP timestamp. More...
union  ntp_id
 An NTP reference identifier. More...
struct  ntp_header
 An NTP header. More...


#define NTP_PORT   123
 NTP port. More...
#define NTP_FL_LI_UNKNOWN   0xc0
 Leap second indicator: unknown. More...
#define NTP_FL_VN_1   0x20
 NTP version: 1. More...
#define NTP_FL_MODE_CLIENT   0x03
 NTP mode: client. More...
#define NTP_FL_MODE_SERVER   0x04
 NTP mode: server. More...
#define NTP_FL_MODE_MASK   0x07
 NTP mode mask. More...
#define NTP_EPOCH   2208988800UL
 NTP timestamp for start of Unix epoch. More...
#define NTP_FRACTION_MAGIC   0x69505845UL
 NTP fraction of a second magic value. More...
 NTP minimum retransmission timeout. More...
#define NTP_MAX_TIMEOUT   ( 10 * TICKS_PER_SEC )
 NTP maximum retransmission timeout. More...


int start_ntp (struct interface *job, const char *hostname)
 Start NTP client. More...

Detailed Description

Network Time Protocol.

Definition in file ntp.h.

Macro Definition Documentation


#define NTP_PORT   123

NTP port.

Definition at line 17 of file ntp.h.


#define NTP_FL_LI_UNKNOWN   0xc0

Leap second indicator: unknown.

Definition at line 72 of file ntp.h.


#define NTP_FL_VN_1   0x20

NTP version: 1.

Definition at line 75 of file ntp.h.


#define NTP_FL_MODE_CLIENT   0x03

NTP mode: client.

Definition at line 78 of file ntp.h.


#define NTP_FL_MODE_SERVER   0x04

NTP mode: server.

Definition at line 81 of file ntp.h.


#define NTP_FL_MODE_MASK   0x07

NTP mode mask.

Definition at line 84 of file ntp.h.


#define NTP_EPOCH   2208988800UL

NTP timestamp for start of Unix epoch.

Definition at line 87 of file ntp.h.


#define NTP_FRACTION_MAGIC   0x69505845UL

NTP fraction of a second magic value.

This is a policy decision.

Definition at line 93 of file ntp.h.



NTP minimum retransmission timeout.

This is a policy decision.

Definition at line 99 of file ntp.h.


#define NTP_MAX_TIMEOUT   ( 10 * TICKS_PER_SEC )

NTP maximum retransmission timeout.

This is a policy decision.

Definition at line 105 of file ntp.h.

Function Documentation



◆ start_ntp()

int start_ntp ( struct interface job,
const char *  hostname 

Start NTP client.

jobJob control interface
hostnameNTP server
Return values
rcReturn status code

Definition at line 235 of file ntp.c.

235  {
236  struct ntp_client *ntp;
237  union {
238  struct sockaddr_tcpip st;
239  struct sockaddr sa;
240  } server;
241  int rc;
243  /* Allocate and initialise structure*/
244  ntp = zalloc ( sizeof ( *ntp ) );
245  if ( ! ntp ) {
246  rc = -ENOMEM;
247  goto err_alloc;
248  }
249  ref_init ( &ntp->refcnt, NULL );
250  intf_init ( &ntp->job, &ntp_job_desc, &ntp->refcnt );
251  intf_init ( &ntp->xfer, &ntp_xfer_desc, &ntp->refcnt );
252  timer_init ( &ntp->timer, ntp_expired, &ntp->refcnt );
253  set_timer_limits ( &ntp->timer, NTP_MIN_TIMEOUT, NTP_MAX_TIMEOUT );
255  /* Open socket */
256  memset ( &server, 0, sizeof ( server ) );
257  server.st.st_port = htons ( NTP_PORT );
258  if ( ( rc = xfer_open_named_socket ( &ntp->xfer, SOCK_DGRAM, &server.sa,
259  hostname, NULL ) ) != 0 ) {
260  DBGC ( ntp, "NTP %p could not open socket: %s\n",
261  ntp, strerror ( rc ) );
262  goto err_open;
263  }
265  /* Attach parent interface, mortalise self, and return */
266  intf_plug_plug ( &ntp->job, job );
267  ref_put ( &ntp->refcnt );
268  return 0;
270  err_open:
271  ntp_close ( ntp, rc );
272  ref_put ( &ntp->refcnt );
273  err_alloc:
274  return rc;
275 }
TCP/IP socket address.
Definition: tcpip.h:75
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:64
#define SOCK_DGRAM
Definition: socket.h:29
#define DBGC(...)
Definition: compiler.h:505
void intf_plug_plug(struct interface *a, struct interface *b)
Plug two object interfaces together.
Definition: interface.c:107
struct sockaddr_tcpip st
Definition: syslog.c:56
An NTP client.
Definition: ntp.c:48
#define ENOMEM
Not enough space.
Definition: errno.h:534
struct sockaddr sa
Definition: syslog.c:55
int ntp(const char *hostname)
Get time and date via NTP.
Definition: ntpmgmt.c:45
Generalized socket address structure.
Definition: socket.h:96
#define NTP_PORT
NTP port.
Definition: ntp.h:17
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
static struct interface_descriptor ntp_xfer_desc
Data transfer interface descriptor.
Definition: ntp.c:195
static struct interface_descriptor ntp_job_desc
Job control interface descriptor.
Definition: ntp.c:204
NTP minimum retransmission timeout.
Definition: ntp.h:99
static void ntp_close(struct ntp_client *ntp, int rc)
Close NTP client.
Definition: ntp.c:65
NTP maximum retransmission timeout.
Definition: ntp.h:105
static void ntp_expired(struct retry_timer *timer, int fail)
Handle NTP timer expiry.
Definition: ntp.c:213
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition: interface.h:190
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
#define htons(value)
Definition: byteswap.h:135
int xfer_open_named_socket(struct interface *xfer, int semantics, struct sockaddr *peer, const char *name, struct sockaddr *local)
Open named socket.
Definition: resolv.c:402
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:106
void * memset(void *dest, int character, size_t len) __nonnull

References DBGC, ENOMEM, htons, intf_init(), intf_plug_plug(), memset(), ntp(), ntp_close(), ntp_expired(), ntp_job_desc, NTP_MAX_TIMEOUT, NTP_MIN_TIMEOUT, NTP_PORT, ntp_xfer_desc, NULL, rc, ref_init, ref_put, sa, SOCK_DGRAM, st, strerror(), xfer_open_named_socket(), and zalloc().

Referenced by ntp().