Class: PacketGen::Header::DNS::Name
- Inherits:
-
Types::Array
- Object
- Types::Array
- PacketGen::Header::DNS::Name
- Defined in:
- lib/packetgen/header/dns/name.rb
Overview
DNS Name, defined as a suite of labels. A label is of type Types::IntString.
Constant Summary collapse
- POINTER_MASK =
Mask to decode a pointer on another label
0xc000
Constants inherited from Types::Array
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#<<(label) ⇒ Name
Self.
-
#from_human(str) ⇒ Name
Read a set of labels form a dotted string.
-
#initialize ⇒ Name
constructor
A new instance of Name.
-
#push(label) ⇒ Name
Self.
-
#read(str) ⇒ Name
Read a sequence of label from a string.
-
#to_human ⇒ String
Get a human readable string.
-
#to_s ⇒ String
Get options binary string.
Methods inherited from Types::Array
#==, #[], #clear, #clear!, #delete, #delete_at, #each, #empty?, #first, #initialize_copy, #last, set_of, set_of_klass, #size, #sz, #to_a
Methods included from Types::LengthFrom
#initialize_length_from, #read_with_length_from
Constructor Details
#initialize ⇒ Name
Returns a new instance of Name.
20 21 22 23 24 |
# File 'lib/packetgen/header/dns/name.rb', line 20 def initialize super @pointer = nil @pointer_name = nil end |
Instance Attribute Details
Instance Method Details
#from_human(str) ⇒ Name
Read a set of labels form a dotted string
36 37 38 39 40 41 42 43 44 |
# File 'lib/packetgen/header/dns/name.rb', line 36 def from_human(str) clear return self if str.nil? str.split('.').each do |label| self << Types::IntString.new(string: label) end self << Types::IntString.new end |
#read(str) ⇒ Name
Read a sequence of label from a string
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/packetgen/header/dns/name.rb', line 49 def read(str) @pointer = nil @pointer_name = nil clear return self if str.nil? PacketGen.force_binary str start = 0 loop do index = str[start, 2].unpack('n').first if pointer? index # Pointer on another label @pointer = str[start, 2] break else label = Types::IntString.new label.read(str[start..-1]) start += label.sz self << label break if label.length.zero? || str[start..-1].length.zero? end end # force resolution of compressed names name_from_pointer self end |
#to_human ⇒ String
Get a human readable string
84 85 86 87 88 89 90 |
# File 'lib/packetgen/header/dns/name.rb', line 84 def to_human ary = map(&:string) np = name_from_pointer ary << np if np str = ary.join('.') str.empty? ? '.' : str end |
#to_s ⇒ String
Get options binary string
78 79 80 |
# File 'lib/packetgen/header/dns/name.rb', line 78 def to_s super << @pointer.to_s end |