class Scriptroute::NTP

Attributes

leap_indicator[RW]

@return [Fixnum]

mode[RW]

@return [Fixnum]

originate_timestamp[RW]
poll_interval[RW]
precision[RW]
receive_timestamp[RW]
reference_identifier[RW]
reference_timestamp[RW]
root_delay[RW]
root_dispersion[RW]
stratum[RW]

@return [Fixnum]

transmit_timestamp[RW]
version_number[RW]

@return [Fixnum]

Public Class Methods

new(paylen_or_str = 0) click to toggle source
Calls superclass method Scriptroute::UDPgeneric::new
# File lib/scriptroute/packets.rb, line 886
def initialize(paylen_or_str = 0)
  if(paylen_or_str.is_a?(Fixnum)) then
    if( paylen_or_str < 0) then raise "payload length must be >= 0" end
    
    @leap_indicator = 3 # alarm condition / clock not synchronized
    @version_number = 4 # unclear if it should be.
    @mode = 3 # client
    @stratum = 0 # unspecified
    @poll_interval = 4 # 16s
    @precision = -6 # emulate ntpdate, though -20 is more likely
    @root_delay = 1.0 # packed funny.
    @root_dispersion = 1.0 # packed funny.
    @reference_identifier = '0.0.0.0'
    @reference_timestamp = 0.0
    @originate_timestamp = 0.0
    @receive_timestamp = 0.0
    @transmit_timestamp = 0.0
    
    super( paylen_or_str + 48 )
    
    @uh_dport = 123
    
  else
    ntp_unmarshal(paylen_or_str)
  end
end

Public Instance Methods

float_to_two_longs(flt) click to toggle source
# File lib/scriptroute/packets.rb, line 949
def float_to_two_longs(flt)
  flt == nil and raise "need a float"
  [ flt.to_i, ((flt - flt.to_i) * 4294967296).to_i ]
end
float_to_two_shorts(flt) click to toggle source
# File lib/scriptroute/packets.rb, line 944
def float_to_two_shorts(flt)
  flt == nil and raise "need a float"
  [ flt.to_i, ((flt - flt.to_i) * 65536).to_i ]
end
marshal() click to toggle source

@return [String] The packet in string form

Calls superclass method Scriptroute::UDPgeneric#marshal
# File lib/scriptroute/packets.rb, line 965
def marshal
  if ($VERBOSE) then
    puts "marshaling with IP payload length %d" % ip_payload_len 
  end
  super + [ @leap_indicator * 64 + @version_number * 8 + @mode, @stratum, @poll_interval, @precision, 
    float_to_two_shorts(@root_delay),
    float_to_two_shorts(@root_dispersion),
    @reference_identifier.to_i, 
    float_to_two_longs(@reference_timestamp),
    float_to_two_longs(@originate_timestamp),
    float_to_two_longs(@receive_timestamp),
    float_to_two_longs(@transmit_timestamp) ].flatten.pack("cccc" +
    "nn" +
    "nn" +
    "N" +
    "NN" +
    "NN" +
    "NN" +
    "NN") + "\0" * ( @uh_ulen - 8 - 48 )
  
end
ntp_unmarshal(str) click to toggle source
# File lib/scriptroute/packets.rb, line 913
def ntp_unmarshal(str)

  ntp_lvm, @stratum, @poll_interval, @precision,
    root_delay1,root_delay2,
    root_dispersion1,root_dispersion2,
    r1,r2,r3,r4,
    reference_timestamp1,reference_timestamp2,
    originate_timestamp1,originate_timestamp2,
    receive_timestamp1,receive_timestamp2,
    transmit_timestamp1, transmit_timestamp2 = str.unpack( "cccc" +
    "nn" +
    "nn" +
    "cccc" +
    "NN" +
    "NN" +
    "NN" +
    "NN");

  @leap_indicator = (ntp_lvm >> 6) & 0xfc
  @version_number = (ntp_lvm & 0x38) >> 3
  @mode = ntp_lvm & 0x07
  @root_delay = root_delay1+root_delay2/65536.0
  @root_dispersion = root_dispersion1+root_dispersion2/65536.0
  @reference_identifier = "%d.%d.%d.%d"% [r1,r2,r3,r4].map{ |i| (i<0)?i+256:i }
  @reference_timestamp = reference_timestamp1 + 0.0 + reference_timestamp2/4294967296.0
  @originate_timestamp = originate_timestamp1 + originate_timestamp2/4294967296.0
  @receive_timestamp = receive_timestamp1 + receive_timestamp2/4294967296.0
  @transmit_timestamp = transmit_timestamp1 + transmit_timestamp2/4294967296.0
  
end
to_bits(int, bits) click to toggle source
# File lib/scriptroute/packets.rb, line 954
def to_bits(int, bits)
  ret = ""
  (1..bits).each do |b|
    ret += (int % 2).to_s
    int =  int / 2
  end
  ret
end