iPXE
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.
#define NTP_FL_LI_UNKNOWN   0xc0
 Leap second indicator: unknown.
#define NTP_FL_VN_1   0x20
 NTP version: 1.
#define NTP_FL_MODE_CLIENT   0x03
 NTP mode: client.
#define NTP_FL_MODE_SERVER   0x04
 NTP mode: server.
#define NTP_FL_MODE_MASK   0x07
 NTP mode mask.
#define NTP_EPOCH   2208988800UL
 NTP timestamp for start of Unix epoch.
#define NTP_FRACTION_MAGIC   0x69505845UL
 NTP fraction of a second magic value.
#define NTP_MIN_TIMEOUT   ( 1 * TICKS_PER_SEC )
 NTP minimum retransmission timeout.
#define NTP_MAX_TIMEOUT   ( 10 * TICKS_PER_SEC )
 NTP maximum retransmission timeout.

Functions

 FILE_LICENCE (GPL2_OR_LATER_OR_UBDL)
 FILE_SECBOOT (PERMITTED)
int start_ntp (struct interface *job, const char *hostname)
 Start NTP client.

Detailed Description

Network Time Protocol.

Definition in file ntp.h.

Macro Definition Documentation

◆ NTP_PORT

#define NTP_PORT   123

NTP port.

Definition at line 18 of file ntp.h.

Referenced by ntp_deliver(), and start_ntp().

◆ NTP_FL_LI_UNKNOWN

#define NTP_FL_LI_UNKNOWN   0xc0

Leap second indicator: unknown.

Definition at line 73 of file ntp.h.

Referenced by ntp_request().

◆ NTP_FL_VN_1

#define NTP_FL_VN_1   0x20

NTP version: 1.

Definition at line 76 of file ntp.h.

Referenced by ntp_request().

◆ NTP_FL_MODE_CLIENT

#define NTP_FL_MODE_CLIENT   0x03

NTP mode: client.

Definition at line 79 of file ntp.h.

Referenced by ntp_request().

◆ NTP_FL_MODE_SERVER

#define NTP_FL_MODE_SERVER   0x04

NTP mode: server.

Definition at line 82 of file ntp.h.

Referenced by ntp_deliver().

◆ NTP_FL_MODE_MASK

#define NTP_FL_MODE_MASK   0x07

NTP mode mask.

Definition at line 85 of file ntp.h.

Referenced by ntp_deliver().

◆ NTP_EPOCH

#define NTP_EPOCH   2208988800UL

NTP timestamp for start of Unix epoch.

Definition at line 88 of file ntp.h.

Referenced by ntp_request().

◆ NTP_FRACTION_MAGIC

#define NTP_FRACTION_MAGIC   0x69505845UL

NTP fraction of a second magic value.

This is a policy decision.

Definition at line 94 of file ntp.h.

Referenced by ntp_deliver(), and ntp_request().

◆ NTP_MIN_TIMEOUT

#define NTP_MIN_TIMEOUT   ( 1 * TICKS_PER_SEC )

NTP minimum retransmission timeout.

This is a policy decision.

Definition at line 100 of file ntp.h.

Referenced by start_ntp().

◆ NTP_MAX_TIMEOUT

#define NTP_MAX_TIMEOUT   ( 10 * TICKS_PER_SEC )

NTP maximum retransmission timeout.

This is a policy decision.

Definition at line 106 of file ntp.h.

Referenced by start_ntp().

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL )

◆ FILE_SECBOOT()

FILE_SECBOOT ( PERMITTED )

◆ start_ntp()

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

Start NTP client.

Parameters
jobJob control interface
hostnameNTP server
Return values
rcReturn status code

Definition at line 238 of file ntp.c.

238 {
239 struct ntp_client *ntp;
240 union {
241 struct sockaddr_tcpip st;
242 struct sockaddr sa;
243 } server;
244 int rc;
245
246 /* Allocate and initialise structure*/
247 ntp = zalloc ( sizeof ( *ntp ) );
248 if ( ! ntp ) {
249 rc = -ENOMEM;
250 goto err_alloc;
251 }
252 ref_init ( &ntp->refcnt, NULL );
253 intf_init ( &ntp->job, &ntp_job_desc, &ntp->refcnt );
254 intf_init ( &ntp->xfer, &ntp_xfer_desc, &ntp->refcnt );
255 timer_init ( &ntp->timer, ntp_expired, &ntp->refcnt );
256 set_timer_limits ( &ntp->timer, NTP_MIN_TIMEOUT, NTP_MAX_TIMEOUT );
257
258 /* Open socket */
259 memset ( &server, 0, sizeof ( server ) );
260 server.st.st_port = htons ( NTP_PORT );
261 if ( ( rc = xfer_open_named_socket ( &ntp->xfer, SOCK_DGRAM, &server.sa,
262 hostname, NULL ) ) != 0 ) {
263 DBGC ( ntp, "NTP %p could not open socket: %s\n",
264 ntp, strerror ( rc ) );
265 goto err_open;
266 }
267
268 /* Attach parent interface, mortalise self, and return */
269 intf_plug_plug ( &ntp->job, job );
270 ref_put ( &ntp->refcnt );
271 return 0;
272
273 err_open:
274 ntp_close ( ntp, rc );
275 ref_put ( &ntp->refcnt );
276 err_alloc:
277 return rc;
278}
#define NULL
NULL pointer (VOID *)
Definition Base.h:322
struct arbelprm_rc_send_wqe rc
Definition arbel.h:3
#define SOCK_DGRAM
Definition socket.h:30
#define DBGC(...)
Definition compiler.h:505
#define ENOMEM
Not enough space.
Definition errno.h:535
#define htons(value)
Definition byteswap.h:136
void * memset(void *dest, int character, size_t len) __nonnull
void intf_plug_plug(struct interface *a, struct interface *b)
Plug two object interfaces together.
Definition interface.c:108
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition interface.h:204
void * zalloc(size_t size)
Allocate cleared memory.
Definition malloc.c:662
static struct interface_descriptor ntp_xfer_desc
Data transfer interface descriptor.
Definition ntp.c:198
static struct interface_descriptor ntp_job_desc
Job control interface descriptor.
Definition ntp.c:207
static void ntp_close(struct ntp_client *ntp, int rc)
Close NTP client.
Definition ntp.c:68
static void ntp_expired(struct retry_timer *timer, int fail)
Handle NTP timer expiry.
Definition ntp.c:216
#define NTP_MAX_TIMEOUT
NTP maximum retransmission timeout.
Definition ntp.h:106
#define NTP_MIN_TIMEOUT
NTP minimum retransmission timeout.
Definition ntp.h:100
#define NTP_PORT
NTP port.
Definition ntp.h:18
int ntp(const char *hostname)
Get time and date via NTP.
Definition ntpmgmt.c:46
#define ref_put(refcnt)
Drop reference to object.
Definition refcnt.h:107
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition refcnt.h:65
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:403
char * strerror(int errno)
Retrieve string representation of error number.
Definition strerror.c:79
An NTP client.
Definition ntp.c:51
struct sockaddr_tcpip st
Definition syslog.c:58
struct sockaddr sa
Definition syslog.c:57

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().