Class: Nesser::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/nesser/transaction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(s:, request:, host:, port:) ⇒ Transaction

Returns a new instance of Transaction.



37
38
39
40
41
42
43
44
45
# File 'lib/nesser/transaction.rb', line 37

def initialize(s:, request:, host:, port:)
  @s = s
  @request = request
  @host = host
  @port = port
  @sent = false

  @response = request.answer()
end

Instance Attribute Details

#requestObject (readonly)

Returns the value of attribute request.



29
30
31
# File 'lib/nesser/transaction.rb', line 29

def request
  @request
end

#responseObject

Returns the value of attribute response.



30
31
32
# File 'lib/nesser/transaction.rb', line 30

def response
  @response
end

#sentObject (readonly)

Returns the value of attribute sent.



29
30
31
# File 'lib/nesser/transaction.rb', line 29

def sent
  @sent
end

Instance Method Details

#answer!(answers = []) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/nesser/transaction.rb', line 70

def answer!(answers=[])
  answers.each do |answer|
    @response.add_answer(answer)
  end

  reply!()
end

#error!(rcode) ⇒ Object



86
87
88
89
90
91
# File 'lib/nesser/transaction.rb', line 86

def error!(rcode)
  not_sent!()

  @response.rcode = rcode
  reply!()
end

#open?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/nesser/transaction.rb', line 59

def open?()
  return !@sent
end

#passthrough!(host: '8.8.8.8', port: 53) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/nesser/transaction.rb', line 102

def passthrough!(host:'8.8.8.8', port:53)
  not_sent!()

  # Get a local handle to the socket
  s = @s

  Thread.new() do
    begin
      response = Nesser.query(
        s: s,
        hostname: @request.questions[0].name,
        server: host,
        port: port,
        type: @request.questions[0].type,
        cls: @request.questions[0].cls,
      )

      if response.rcode != RCODE_SUCCESS
        error!(response.rcode)
      else
        answer!(response.answers)
      end
    rescue StandardError
      error!(RCODE_SERVER_FAILURE)
    end
  end
end

#reply!Object



139
140
141
142
143
144
# File 'lib/nesser/transaction.rb', line 139

def reply!()
  not_sent!()

  @s.send(@response.to_bytes(), 0, @host, @port)
  @sent = true
end

#to_sObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/nesser/transaction.rb', line 147

def to_s()
  result = []

  result << '== Nesser (DNS) Transaction =='
  result << '-- Request --'
  result << @request.to_s()
  if !sent()
    result << '-- Response [not sent yet] --'
  else
    result << '-- Response [sent] --'
  end
  result << @response.to_s()
  result << ''

  return result.join("\n")
end