Class: Scriptroute::NTP

Inherits:
UDP show all
Defined in:
lib/scriptroute/packets.rb

Constant Summary

Constants inherited from IPv4

IPv4::IPPROTO_ICMP, IPv4::IPPROTO_TCP, IPv4::IPPROTO_UDP

Instance Attribute Summary collapse

Attributes inherited from UDP

#uh_dport, #uh_sport, #uh_sum, #uh_ulen

Attributes inherited from IPv4

#ip_dst, #ip_hl, #ip_id, #ip_len, #ip_off, #ip_options, #ip_p, #ip_src, #ip_sum, #ip_tos, #ip_ttl, #ip_v

Instance Method Summary collapse

Methods inherited from UDP

#ip_payload_len, #to_s, #udp_unmarshal

Methods inherited from IPv4

#add_option, #calculate_header_len, #calculate_packet_len, creator, #ip_payload_len, #ipv4_unmarshal, #to_s

Constructor Details

#initialize(paylen_or_str = 0) ⇒ NTP

Returns a new instance of NTP.



677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
# File 'lib/scriptroute/packets.rb', line 677

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

Instance Attribute Details

#leap_indicatorFixnum

Returns:

  • (Fixnum)


671
672
673
# File 'lib/scriptroute/packets.rb', line 671

def leap_indicator
  @leap_indicator
end

#modeFixnum

Returns:

  • (Fixnum)


671
672
673
# File 'lib/scriptroute/packets.rb', line 671

def mode
  @mode
end

#originate_timestampObject

Returns the value of attribute originate_timestamp.



674
675
676
# File 'lib/scriptroute/packets.rb', line 674

def originate_timestamp
  @originate_timestamp
end

#poll_intervalObject

Returns the value of attribute poll_interval.



672
673
674
# File 'lib/scriptroute/packets.rb', line 672

def poll_interval
  @poll_interval
end

#precisionObject

Returns the value of attribute precision.



672
673
674
# File 'lib/scriptroute/packets.rb', line 672

def precision
  @precision
end

#receive_timestampObject

Returns the value of attribute receive_timestamp.



675
676
677
# File 'lib/scriptroute/packets.rb', line 675

def receive_timestamp
  @receive_timestamp
end

#reference_identifierObject

Returns the value of attribute reference_identifier.



673
674
675
# File 'lib/scriptroute/packets.rb', line 673

def reference_identifier
  @reference_identifier
end

#reference_timestampObject

Returns the value of attribute reference_timestamp.



674
675
676
# File 'lib/scriptroute/packets.rb', line 674

def reference_timestamp
  @reference_timestamp
end

#root_delayObject

Returns the value of attribute root_delay.



672
673
674
# File 'lib/scriptroute/packets.rb', line 672

def root_delay
  @root_delay
end

#root_dispersionObject

Returns the value of attribute root_dispersion.



673
674
675
# File 'lib/scriptroute/packets.rb', line 673

def root_dispersion
  @root_dispersion
end

#stratumFixnum

Returns:

  • (Fixnum)


671
672
673
# File 'lib/scriptroute/packets.rb', line 671

def stratum
  @stratum
end

#transmit_timestampObject

Returns the value of attribute transmit_timestamp.



675
676
677
# File 'lib/scriptroute/packets.rb', line 675

def transmit_timestamp
  @transmit_timestamp
end

#version_numberFixnum

Returns:

  • (Fixnum)


671
672
673
# File 'lib/scriptroute/packets.rb', line 671

def version_number
  @version_number
end

Instance Method Details

#float_to_two_longs(flt) ⇒ Object



740
741
742
743
# File 'lib/scriptroute/packets.rb', line 740

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) ⇒ Object



735
736
737
738
# File 'lib/scriptroute/packets.rb', line 735

def float_to_two_shorts(flt)
  flt == nil and raise "need a float"
  [ flt.to_i, ((flt - flt.to_i) * 65536).to_i ]
end

#marshalString

Returns The packet in string form.

Returns:

  • (String)

    The packet in string form



756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
# File 'lib/scriptroute/packets.rb', line 756

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) ⇒ Object



704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
# File 'lib/scriptroute/packets.rb', line 704

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) ⇒ Object



745
746
747
748
749
750
751
752
# File 'lib/scriptroute/packets.rb', line 745

def to_bits(int, bits)
  ret = ""
  (1..bits).each do |b|
    ret += (int % 2).to_s
    int =  int / 2
  end
  ret
end