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

Inherits:
BinStruct::Array
  • Object
show all
Defined in:
lib/packetgen/header/dns/name.rb

Overview

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

Author:

  • Sylvain Daubert

  • LemonTree55

Since:

  • 1.3.0

Constant Summary collapse

POINTER_MASK =

Mask to decode a pointer on another label

Since:

  • 1.3.0

0xc000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Name

Returns a new instance of Name.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

Since:

  • 1.3.0



25
26
27
28
29
30
# File 'lib/packetgen/header/dns/name.rb', line 25

def initialize(options={})
  @dns = options.delete(:dns)
  super
  @pointer = nil
  @pointer_name = nil
end

Instance Attribute Details

#dnsDNS

DNS message to which this name is attached. Used to decode compressed names

Returns:

Since:

  • 1.3.0



21
22
23
# File 'lib/packetgen/header/dns/name.rb', line 21

def dns
  @dns
end

Instance Method Details

#<<(label) ⇒ Name

Returns self.

Parameters:

  • label (BinStruct::IntString)

Returns:



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

#clearvoid

This method returns an undefined value.

Clear name

Since:

  • 1.3.0



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

Parameters:

  • str (String)

Returns:

Since:

  • 1.3.0



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

#push(label) ⇒ Name

Returns self.

Parameters:

  • label (BinStruct::IntString)

Returns:



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

#read(str) ⇒ Name

Read a sequence of label from a binary string

Parameters:

  • str (String)

    binary string

Returns:

Since:

  • 1.3.0



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_humanString

Get a human readable string

Returns:

  • (String)

Since:

  • 1.3.0



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_sString

Get options binary string

Returns:

  • (String)

Since:

  • 1.3.0



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

def to_s
  super << @pointer.to_s
end