Class: PacketGen::Header::DNS::Name
- Inherits:
-
BinStruct::Array
- Object
- BinStruct::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 BinStruct::IntString
.
Constant Summary collapse
- POINTER_MASK =
Mask to decode a pointer on another label
0xc000
Instance Attribute Summary collapse
-
#dns ⇒ DNS
DNS message to which this name is attached.
Instance Method Summary collapse
-
#<<(label) ⇒ Name
Self.
-
#clear ⇒ void
Clear name.
-
#from_human(str) ⇒ Name
Read a set of labels form a dotted string.
-
#initialize(options = {}) ⇒ Name
constructor
A new instance of Name.
-
#push(label) ⇒ Name
Self.
-
#read(str) ⇒ Name
Read a sequence of label from a binary string.
-
#to_human ⇒ String
Get a human readable string.
-
#to_s ⇒ String
Get options binary string.
Constructor Details
#initialize(options = {}) ⇒ Name
Returns a new instance of Name.
25 26 27 28 29 30 |
# File 'lib/packetgen/header/dns/name.rb', line 25 def initialize(={}) @dns = .delete(:dns) super @pointer = nil @pointer_name = nil end |
Instance Attribute Details
#dns ⇒ DNS
DNS message to which this name is attached. Used to decode compressed names
21 22 23 |
# File 'lib/packetgen/header/dns/name.rb', line 21 def dns @dns end |
Instance Method Details
#clear ⇒ void
This method returns an undefined value.
Clear name
54 55 56 57 58 |
# File 'lib/packetgen/header/dns/name.rb', line 54 def clear super @pointer = nil @pointer_name = nil end |
#from_human(str) ⇒ Name
Read a set of labels form a dotted string
42 43 44 45 46 47 48 49 50 |
# File 'lib/packetgen/header/dns/name.rb', line 42 def from_human(str) clear return self if str.nil? str.split('.').each do |label| self << BinStruct::IntString.new(value: label) end self << BinStruct::IntString.new end |
#read(str) ⇒ Name
Read a sequence of label from a binary string
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/packetgen/header/dns/name.rb', line 63 def read(str) clear return self if str.nil? strb = str.to_s.b start = 0 loop do index = strb[start, 2].unpack1('n') if pointer?(index) # Pointer on another label @pointer = strb[start, 2] break else label = add_label_from(strb[start..]) start += label.sz break if label.empty? || strb[start..].empty? end end # force resolution of compressed names name_from_pointer self end |
#to_human ⇒ String
Get a human readable string
94 95 96 97 98 99 100 |
# File 'lib/packetgen/header/dns/name.rb', line 94 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
88 89 90 |
# File 'lib/packetgen/header/dns/name.rb', line 88 def to_s super << @pointer.to_s end |