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



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

#dnsDNS

Returns:

Since:

  • 1.3.0



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

def dns
  @dns
end

Instance Method Details

#<<(label) ⇒ Name

Returns self.

Parameters:

Returns:



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

#clearObject

Clear name

@return [void]

Since:

  • 1.3.0



48
49
50
51
52
# File 'lib/packetgen/header/dns/name.rb', line 48

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



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

#push(label) ⇒ Name

Returns self.

Parameters:

Returns:



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

#read(str) ⇒ Name

Read a sequence of label from a string

Parameters:

  • str (String)

    binary string

Returns:

Since:

  • 1.3.0



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/packetgen/header/dns/name.rb', line 57

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



88
89
90
91
92
93
94
# File 'lib/packetgen/header/dns/name.rb', line 88

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



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

def to_s
  super << @pointer.to_s
end