Module: Net::NTP

Defined in:
lib/net/ntp/ntp.rb,
lib/net/ntp/version.rb

Defined Under Namespace

Classes: Response

Constant Summary collapse

TIMEOUT =

:nodoc:

60
NTP_ADJ =

:nodoc:

2208988800
NTP_FIELDS =
[ :byte1, :stratum, :poll, :precision, :delay, :delay_fb,
:disp, :disp_fb, :ident, :ref_time, :ref_time_fb, :org_time,
:org_time_fb, :recv_time, :recv_time_fb, :trans_time,
:trans_time_fb ]
MODE =
{
  0 => 'reserved',
  1 => 'symmetric active',
  2 => 'symmetric passive',
  3 => 'client',
  4 => 'server',
  5 => 'broadcast',
  6 => 'reserved for NTP control message',
  7 => 'reserved for private use'
}
STRATUM =
{
  0 => 'unspecified or unavailable',
  1 => 'primary reference (e.g., radio clock)'
}
REFERENCE_CLOCK_IDENTIFIER =
{
  'LOCL' => 'uncalibrated local clock used as a primary reference for a subnet without external means of synchronization',
  'PPS'  => 'atomic clock or other pulse-per-second source individually calibrated to national standards',
  'ACTS' => 'NIST dialup modem service',
  'USNO' => 'USNO modem service',
  'PTB'  => 'PTB (Germany) modem service',
  'TDF'  => 'Allouis (France) Radio 164 kHz',
  'DCF'  => 'Mainflingen (Germany) Radio 77.5 kHz',
  'MSF'  => 'Rugby (UK) Radio 60 kHz',
  'WWV'  => 'Ft. Collins (US) Radio 2.5, 5, 10, 15, 20 MHz',
  'WWVB' => 'Boulder (US) Radio 60 kHz',
  'WWVH' => 'Kaui Hawaii (US) Radio 2.5, 5, 10, 15 MHz',
  'CHU'  => 'Ottawa (Canada) Radio 3330, 7335, 14670 kHz',
  'LORC' => 'LORAN-C radionavigation system',
  'OMEG' => 'OMEGA radionavigation system',
  'GPS'  => 'Global Positioning Service',
  'GOES' => 'Geostationary Orbit Environment Satellite'
}
LEAP_INDICATOR =
{
  0 => 'no warning',
  1 => 'last minute has 61 seconds',
  2 => 'last minute has 59 seconds)',
  3 => 'alarm condition (clock not synchronized)'
}
VERSION =
'2.1.3'

Class Method Summary collapse

Class Method Details

.get(host = "pool.ntp.org", port = "ntp", timeout = TIMEOUT) ⇒ Object

Sends an NTP datagram to the specified NTP server and returns a hash based upon RFC1305 and RFC2030.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/net/ntp/ntp.rb', line 66

def self.get(host="pool.ntp.org", port="ntp", timeout=TIMEOUT)
  sock = UDPSocket.new
  sock.connect(host, port)

  client_localtime      = Time.now.to_f
  client_adj_localtime  = client_localtime + NTP_ADJ
  client_frac_localtime = frac2bin(client_adj_localtime)

  ntp_msg = (['00011011']+Array.new(12, 0)+[client_localtime, client_frac_localtime.to_s]).pack("B8 C3 N10 B32")

  sock.print ntp_msg
  sock.flush

  read, write, error = IO.select [sock], nil, nil, timeout
  if read.nil?
    # For backwards compatibility we throw a Timeout error, even
    # though the timeout is being controlled by select()
    raise Timeout::Error
  else
    client_time_receive = Time.now.to_f
    data, _ = sock.recvfrom(960)
    Response.new(data, client_time_receive)
  end
end