Class: PacketGen::Header::DNS::Name

Inherits:
Types::Array show all
Defined in:
lib/packetgen/header/dns/name.rb

Overview

DNS Name, defined as a suite of labels. A label is of type Types::IntString.

Author:

  • Sylvain Daubert

Since:

  • 1.3.0

Constant Summary collapse

POINTER_MASK =

Mask to decode a pointer on another label

Since:

  • 1.3.0

0xc000

Constants inherited from Types::Array

Types::Array::HUMAN_SEPARATOR

Constants included from Types::LengthFrom

Types::LengthFrom::MAX_SZ_TO_READ

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#initializeName

Returns a new instance of Name.

Since:

  • 1.3.0



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

#dnsDNS

Returns:

Since:

  • 1.3.0



19
20
21
# File 'lib/packetgen/header/dns/name.rb', line 19

def dns
  @dns
end

Instance Method Details

#<<(label) ⇒ Name

Returns self.

Parameters:

Returns:



# File 'lib/packetgen/header/dns/name.rb', line 27

#clearObject

Clear name

@return [void]

Since:

  • 1.3.0



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

Parameters:

  • str (String)

Returns:

Since:

  • 1.3.0



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

#push(label) ⇒ Name

Returns self.

Parameters:

Returns:



# File 'lib/packetgen/header/dns/name.rb', line 27

#read(str) ⇒ Name

Read a sequence of label from a string

Parameters:

  • str (String)

    binary string

Returns:

Since:

  • 1.3.0



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_humanString

Get a human readable string

Returns:

  • (String)

Since:

  • 1.3.0



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_sString

Get options binary string

Returns:

  • (String)

Since:

  • 1.3.0



83
84
85
# File 'lib/packetgen/header/dns/name.rb', line 83

def to_s
  super << @pointer.to_s
end