Class: Scriptroute::NTP
Constant Summary
Constants inherited from IPv4
IPv4::IPPROTO_ICMP, IPv4::IPPROTO_TCP, IPv4::IPPROTO_UDP
Instance Attribute Summary collapse
- #leap_indicator ⇒ Fixnum
- #mode ⇒ Fixnum
-
#originate_timestamp ⇒ Object
Returns the value of attribute originate_timestamp.
-
#poll_interval ⇒ Object
Returns the value of attribute poll_interval.
-
#precision ⇒ Object
Returns the value of attribute precision.
-
#receive_timestamp ⇒ Object
Returns the value of attribute receive_timestamp.
-
#reference_identifier ⇒ Object
Returns the value of attribute reference_identifier.
-
#reference_timestamp ⇒ Object
Returns the value of attribute reference_timestamp.
-
#root_delay ⇒ Object
Returns the value of attribute root_delay.
-
#root_dispersion ⇒ Object
Returns the value of attribute root_dispersion.
- #stratum ⇒ Fixnum
-
#transmit_timestamp ⇒ Object
Returns the value of attribute transmit_timestamp.
- #version_number ⇒ Fixnum
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
- #float_to_two_longs(flt) ⇒ Object
- #float_to_two_shorts(flt) ⇒ Object
-
#initialize(paylen_or_str = 0) ⇒ NTP
constructor
A new instance of NTP.
-
#marshal ⇒ String
The packet in string form.
- #ntp_unmarshal(str) ⇒ Object
- #to_bits(int, bits) ⇒ Object
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_indicator ⇒ Fixnum
671 672 673 |
# File 'lib/scriptroute/packets.rb', line 671 def leap_indicator @leap_indicator end |
#mode ⇒ Fixnum
671 672 673 |
# File 'lib/scriptroute/packets.rb', line 671 def mode @mode end |
#originate_timestamp ⇒ Object
Returns the value of attribute originate_timestamp.
674 675 676 |
# File 'lib/scriptroute/packets.rb', line 674 def @originate_timestamp end |
#poll_interval ⇒ Object
Returns the value of attribute poll_interval.
672 673 674 |
# File 'lib/scriptroute/packets.rb', line 672 def poll_interval @poll_interval end |
#precision ⇒ Object
Returns the value of attribute precision.
672 673 674 |
# File 'lib/scriptroute/packets.rb', line 672 def precision @precision end |
#receive_timestamp ⇒ Object
Returns the value of attribute receive_timestamp.
675 676 677 |
# File 'lib/scriptroute/packets.rb', line 675 def @receive_timestamp end |
#reference_identifier ⇒ Object
Returns the value of attribute reference_identifier.
673 674 675 |
# File 'lib/scriptroute/packets.rb', line 673 def reference_identifier @reference_identifier end |
#reference_timestamp ⇒ Object
Returns the value of attribute reference_timestamp.
674 675 676 |
# File 'lib/scriptroute/packets.rb', line 674 def @reference_timestamp end |
#root_delay ⇒ Object
Returns the value of attribute root_delay.
672 673 674 |
# File 'lib/scriptroute/packets.rb', line 672 def root_delay @root_delay end |
#root_dispersion ⇒ Object
Returns the value of attribute root_dispersion.
673 674 675 |
# File 'lib/scriptroute/packets.rb', line 673 def root_dispersion @root_dispersion end |
#stratum ⇒ Fixnum
671 672 673 |
# File 'lib/scriptroute/packets.rb', line 671 def stratum @stratum end |
#transmit_timestamp ⇒ Object
Returns the value of attribute transmit_timestamp.
675 676 677 |
# File 'lib/scriptroute/packets.rb', line 675 def @transmit_timestamp end |
#version_number ⇒ 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 |
#marshal ⇒ String
Returns 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, ,, ,, ,, , = 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 = + 0.0 + /4294967296.0 @originate_timestamp = + /4294967296.0 @receive_timestamp = + /4294967296.0 @transmit_timestamp = + /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 |