iPXE
Data Structures | Defines | 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...

Defines

#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)
int start_ntp (struct interface *job, const char *hostname)
 Start NTP client.

Detailed Description

Network Time Protocol.

Definition in file ntp.h.


Define Documentation

#define NTP_PORT   123

NTP port.

Definition at line 17 of file ntp.h.

Referenced by ntp_deliver(), and start_ntp().

#define NTP_FL_LI_UNKNOWN   0xc0

Leap second indicator: unknown.

Definition at line 72 of file ntp.h.

Referenced by ntp_request().

#define NTP_FL_VN_1   0x20

NTP version: 1.

Definition at line 75 of file ntp.h.

Referenced by ntp_request().

#define NTP_FL_MODE_CLIENT   0x03

NTP mode: client.

Definition at line 78 of file ntp.h.

Referenced by ntp_request().

#define NTP_FL_MODE_SERVER   0x04

NTP mode: server.

Definition at line 81 of file ntp.h.

Referenced by ntp_deliver().

#define NTP_FL_MODE_MASK   0x07

NTP mode mask.

Definition at line 84 of file ntp.h.

Referenced by ntp_deliver().

#define NTP_EPOCH   2208988800UL

NTP timestamp for start of Unix epoch.

Definition at line 87 of file ntp.h.

Referenced by ntp_request().

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

Referenced by ntp_deliver(), and ntp_request().

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

Referenced by start_ntp().

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

Referenced by start_ntp().


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )
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 235 of file ntp.c.

References DBGC, ENOMEM, htons, intf_init(), intf_plug_plug(), ntp_client::job, memset(), ntp(), ntp_close(), ntp_expired(), NTP_MAX_TIMEOUT, NTP_MIN_TIMEOUT, NTP_PORT, NULL, rc, ref_init, ref_put, ntp_client::refcnt, SOCK_DGRAM, strerror(), ntp_client::timer, ntp_client::xfer, xfer_open_named_socket(), and zalloc().

Referenced by ntp().

                                                              {
        struct ntp_client *ntp;
        union {
                struct sockaddr_tcpip st;
                struct sockaddr sa;
        } server;
        int rc;

        /* Allocate and initialise structure*/
        ntp = zalloc ( sizeof ( *ntp ) );
        if ( ! ntp ) {
                rc = -ENOMEM;
                goto err_alloc;
        }
        ref_init ( &ntp->refcnt, NULL );
        intf_init ( &ntp->job, &ntp_job_desc, &ntp->refcnt );
        intf_init ( &ntp->xfer, &ntp_xfer_desc, &ntp->refcnt );
        timer_init ( &ntp->timer, ntp_expired, &ntp->refcnt );
        set_timer_limits ( &ntp->timer, NTP_MIN_TIMEOUT, NTP_MAX_TIMEOUT );

        /* Open socket */
        memset ( &server, 0, sizeof ( server ) );
        server.st.st_port = htons ( NTP_PORT );
        if ( ( rc = xfer_open_named_socket ( &ntp->xfer, SOCK_DGRAM, &server.sa,
                                             hostname, NULL ) ) != 0 ) {
                DBGC ( ntp, "NTP %p could not open socket: %s\n",
                       ntp, strerror ( rc ) );
                goto err_open;
        }

        /* Attach parent interface, mortalise self, and return */
        intf_plug_plug ( &ntp->job, job );
        ref_put ( &ntp->refcnt );
        return 0;

 err_open:
        ntp_close ( ntp, rc );
        ref_put ( &ntp->refcnt );
 err_alloc:
        return rc;
}