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

Macros

#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...
 
#define NTP_MIN_TIMEOUT   ( 1 * TICKS_PER_SEC )
 NTP minimum retransmission timeout. More...
 
#define NTP_MAX_TIMEOUT   ( 10 * TICKS_PER_SEC )
 NTP maximum retransmission timeout. More...
 

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 
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

◆ NTP_PORT

#define NTP_PORT   123

NTP port.

Definition at line 17 of file ntp.h.

◆ NTP_FL_LI_UNKNOWN

#define NTP_FL_LI_UNKNOWN   0xc0

Leap second indicator: unknown.

Definition at line 72 of file ntp.h.

◆ NTP_FL_VN_1

#define NTP_FL_VN_1   0x20

NTP version: 1.

Definition at line 75 of file ntp.h.

◆ NTP_FL_MODE_CLIENT

#define NTP_FL_MODE_CLIENT   0x03

NTP mode: client.

Definition at line 78 of file ntp.h.

◆ NTP_FL_MODE_SERVER

#define NTP_FL_MODE_SERVER   0x04

NTP mode: server.

Definition at line 81 of file ntp.h.

◆ NTP_FL_MODE_MASK

#define NTP_FL_MODE_MASK   0x07

NTP mode mask.

Definition at line 84 of file ntp.h.

◆ NTP_EPOCH

#define NTP_EPOCH   2208988800UL

NTP timestamp for start of Unix epoch.

Definition at line 87 of file ntp.h.

◆ NTP_FRACTION_MAGIC

#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_MIN_TIMEOUT

#define NTP_MIN_TIMEOUT   ( 1 * TICKS_PER_SEC )

NTP minimum retransmission timeout.

This is a policy decision.

Definition at line 99 of file ntp.h.

◆ NTP_MAX_TIMEOUT

#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

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ start_ntp()

int start_ntp ( struct interface job,
const char *  hostname 
)

Start NTP client.

Parameters
jobJob control interface
hostnameNTP server
Return values
rcReturn status code

Definition at line 237 of file ntp.c.

237  {
238  struct ntp_client *ntp;
239  union {
240  struct sockaddr_tcpip st;
241  struct sockaddr sa;
242  } server;
243  int rc;
244 
245  /* Allocate and initialise structure*/
246  ntp = zalloc ( sizeof ( *ntp ) );
247  if ( ! ntp ) {
248  rc = -ENOMEM;
249  goto err_alloc;
250  }
251  ref_init ( &ntp->refcnt, NULL );
252  intf_init ( &ntp->job, &ntp_job_desc, &ntp->refcnt );
253  intf_init ( &ntp->xfer, &ntp_xfer_desc, &ntp->refcnt );
254  timer_init ( &ntp->timer, ntp_expired, &ntp->refcnt );
255  set_timer_limits ( &ntp->timer, NTP_MIN_TIMEOUT, NTP_MAX_TIMEOUT );
256 
257  /* Open socket */
258  memset ( &server, 0, sizeof ( server ) );
259  server.st.st_port = htons ( NTP_PORT );
260  if ( ( rc = xfer_open_named_socket ( &ntp->xfer, SOCK_DGRAM, &server.sa,
261  hostname, NULL ) ) != 0 ) {
262  DBGC ( ntp, "NTP %p could not open socket: %s\n",
263  ntp, strerror ( rc ) );
264  goto err_open;
265  }
266 
267  /* Attach parent interface, mortalise self, and return */
268  intf_plug_plug ( &ntp->job, job );
269  ref_put ( &ntp->refcnt );
270  return 0;
271 
272  err_open:
273  ntp_close ( ntp, rc );
274  ref_put ( &ntp->refcnt );
275  err_alloc:
276  return rc;
277 }
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:50
#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:197
static struct interface_descriptor ntp_job_desc
Job control interface descriptor.
Definition: ntp.c:206
#define NTP_MIN_TIMEOUT
NTP minimum retransmission timeout.
Definition: ntp.h:99
static void ntp_close(struct ntp_client *ntp, int rc)
Close NTP client.
Definition: ntp.c:67
#define NTP_MAX_TIMEOUT
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:215
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition: interface.h:203
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
#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().