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
Constants included from Types::LengthFrom
Types::LengthFrom::MAX_SZ_TO_READ
Instance Attribute Summary collapse
Instance Method Summary collapse
-
#<<(label) ⇒ Name
Self.
-
#clear ⇒ Object
Clear name @return [void].
-
#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!, #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, #sz_to_read
Methods included from Types::Fieldable
#format_inspect, #sz, #type_name
Constructor Details
#initialize ⇒ Name
Returns a new instance of Name.
21 22 23 24 25 |
# File 'lib/packetgen/header/dns/name.rb', line 21 def initialize super @pointer = nil @pointer_name = nil end |
Instance Attribute Details
Instance Method Details
#clear ⇒ Object
Clear name
@return [void]
49 50 51 52 53 |
# File 'lib/packetgen/header/dns/name.rb', line 49 def clear super @pointer = nil @pointer_name = nil end |
#from_human(str) ⇒ Name
Read a set of labels form a dotted string
37 38 39 40 41 42 43 44 45 |
# File 'lib/packetgen/header/dns/name.rb', line 37 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
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/packetgen/header/dns/name.rb', line 58 def read(str) clear return self if str.nil? PacketGen.force_binary str start = 0 loop do index = str[start, 2].unpack1('n') if pointer? index # Pointer on another label @pointer = str[start, 2] break else label = add_label_from(str[start..]) start += label.sz break if label.empty? || str[start..].empty? end end # force resolution of compressed names name_from_pointer self end |
#to_human ⇒ String
Get a human readable string
89 90 91 92 93 94 95 |
# File 'lib/packetgen/header/dns/name.rb', line 89 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
83 84 85 |
# File 'lib/packetgen/header/dns/name.rb', line 83 def to_s super << @pointer.to_s end |