Class: Scriptroute::NTP

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

Constant Summary

Constants inherited from IPv4

IPv4::IPPROTO_ICMP

Constants inherited from IP

IP::IPPROTO_TCP, IP::IPPROTO_UDP

Instance Attribute Summary collapse

Attributes included from UDPgeneric

#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 included from UDPgeneric

#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

Methods inherited from IP

creator, #to_bytes

Constructor Details

#initialize(paylen_or_str = 0) ⇒ NTP

Returns a new instance of NTP.



886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
# 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

Instance Attribute Details

#leap_indicatorFixnum

Returns:

  • (Fixnum)


880
881
882
# File 'lib/scriptroute/packets.rb', line 880

def leap_indicator
  @leap_indicator
end

#modeFixnum

Returns:

  • (Fixnum)


880
881
882
# File 'lib/scriptroute/packets.rb', line 880

def mode
  @mode
end

#originate_timestampObject

Returns the value of attribute originate_timestamp.



883
884
885
# File 'lib/scriptroute/packets.rb', line 883

def originate_timestamp
  @originate_timestamp
end

#poll_intervalObject

Returns the value of attribute poll_interval.



881
882
883
# File 'lib/scriptroute/packets.rb', line 881

def poll_interval
  @poll_interval
end

#precisionObject

Returns the value of attribute precision.



881
882
883
# File 'lib/scriptroute/packets.rb', line 881

def precision
  @precision
end

#receive_timestampObject

Returns the value of attribute receive_timestamp.



884
885
886
# File 'lib/scriptroute/packets.rb', line 884

def receive_timestamp
  @receive_timestamp
end

#reference_identifierObject

Returns the value of attribute reference_identifier.



882
883
884
# File 'lib/scriptroute/packets.rb', line 882

def reference_identifier
  @reference_identifier
end

#reference_timestampObject

Returns the value of attribute reference_timestamp.



883
884
885
# File 'lib/scriptroute/packets.rb', line 883

def reference_timestamp
  @reference_timestamp
end

#root_delayObject

Returns the value of attribute root_delay.



881
882
883
# File 'lib/scriptroute/packets.rb', line 881

def root_delay
  @root_delay
end

#root_dispersionObject

Returns the value of attribute root_dispersion.



882
883
884
# File 'lib/scriptroute/packets.rb', line 882

def root_dispersion
  @root_dispersion
end

#stratumFixnum

Returns:

  • (Fixnum)


880
881
882
# File 'lib/scriptroute/packets.rb', line 880

def stratum
  @stratum
end

#transmit_timestampObject

Returns the value of attribute transmit_timestamp.



884
885
886
# File 'lib/scriptroute/packets.rb', line 884

def transmit_timestamp
  @transmit_timestamp
end

#version_numberFixnum

Returns:

  • (Fixnum)


880
881
882
# File 'lib/scriptroute/packets.rb', line 880

def version_number
  @version_number
end

Instance Method Details

#float_to_two_longs(flt) ⇒ Object



949
950
951
952
# 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) ⇒ Object



944
945
946
947
# 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

#marshalString

Returns The packet in string form.

Returns:

  • (String)

    The packet in string form



965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
# 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) ⇒ Object



913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
# 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) ⇒ Object



954
955
956
957
958
959
960
961
# 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