Class: Dnsruby::SelectThread

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/Dnsruby/select_thread.rb

Overview

:nodoc: all

Defined Under Namespace

Classes: QuerySettings, SelectWakeup

Instance Method Summary collapse

Constructor Details

#initializeSelectThread

This singleton class runs a continuous select loop which listens for responses on all of the in-use sockets. When a new query is sent, the thread is woken up, and the socket is added to the select loop (and the new timeout calculated). Note that a combination of the socket and the packet ID is sufficient to uniquely identify the query to the select thread.

But how do we find the response queue for a particular query? Hash of client_id->[query, client_queue, socket] and socket->

@todo@ should we implement some of cancel function?



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/Dnsruby/select_thread.rb', line 44

def initialize
  @@mutex = Mutex.new
  @@mutex.synchronize {
    @@in_select=false
    #        @@notifier,@@notified=IO.pipe
    @@sockets = [] # @@notified]
    @@timeouts = Hash.new
    #    @@mutex.synchronize do
    @@query_hash = Hash.new
    @@socket_hash = Hash.new
    @@observers = Hash.new
    @@tick_observers = []
    @@queued_exceptions=[]
    @@queued_responses=[]
    @@queued_validation_responses=[]
    #    end
    # Now start the select thread
    @@select_thread = Thread.new {
      do_select
    }
    #        # Start the validator thread
    #        @@validator = ValidatorThread.instance
  }
end

Instance Method Details

#add_observer(client_queue, observer) ⇒ Object



547
548
549
550
551
552
553
554
555
# File 'lib/Dnsruby/select_thread.rb', line 547

def add_observer(client_queue, observer)
  @@mutex.synchronize {
    @@observers[client_queue]=observer
    check_select_thread_synchronized # Is this really necessary? The client should start the thread by sending a query, really...        
    if (!@@tick_observers.include?observer)
      @@tick_observers.push(observer)
    end
  }
end

#add_to_select(query_settings) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/Dnsruby/select_thread.rb', line 90

def add_to_select(query_settings)
  # Add the query to sockets, and then wake the select thread up
  @@mutex.synchronize {
    check_select_thread_synchronized
    # @TODO@ This assumes that all client_query_ids are unique!
    # Would be a good idea at least to check this...
    @@query_hash[query_settings.client_query_id]=query_settings
    @@socket_hash[query_settings.socket]=[query_settings.client_query_id] # @todo@ If we use persistent sockets then we need to update this array
    @@timeouts[query_settings.client_query_id]=query_settings.endtime
    @@sockets.push(query_settings.socket)
  }
end

#check_select_thread_synchronizedObject



103
104
105
106
107
108
109
110
# File 'lib/Dnsruby/select_thread.rb', line 103

def check_select_thread_synchronized
  if (!@@select_thread.alive?)
    Dnsruby.log.debug{"Restarting select thread"}
    @@select_thread = Thread.new {
      do_select
    }
  end
end

#do_selectObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/Dnsruby/select_thread.rb', line 120

def do_select
  unused_loop_count = 0
  last_tick_time = Time.now - 10
  while true do
    if (last_tick_time < (Time.now - 0.5))
      send_tick_to_observers # ONLY NEED TO SEND THIS TWICE A SECOND - NOT EVERY SELECT!!!
      last_tick_time = Time.now
    end
    send_queued_exceptions
    send_queued_responses
    send_queued_validation_responses
    timeout = tick_time = 0.5 # We provide a timer service to various Dnsruby classes
    sockets=[]
    timeouts=[]
    has_observer = false
    @@mutex.synchronize {                
      sockets = @@sockets 
      timeouts = @@timeouts.values
      has_observer = !@@observers.empty?
    }
    if (timeouts.length > 0)
      timeouts.sort!
      timeout = timeouts[0] - Time.now
      if (timeout <= 0)
        process_timeouts
        timeout = 0
        next
      end
    end
    ready=nil
    if (has_observer && (timeout > tick_time))
      timeout = tick_time
    end
    #        next if (timeout < 0)
    begin
      ready, write, errors = IO.select(sockets, nil, nil, timeout)
    rescue SelectWakeup
      # If SelectWakeup, then just restart this loop - the select call will be made with the new data
      next
    end
    if (ready == nil)
      # proces the timeouts
      process_timeouts
      unused_loop_count+=1
    else
      process_ready(ready)
      unused_loop_count=0
      #                  process_error(errors)
    end
    @@mutex.synchronize{
      if (unused_loop_count > 10 && @@query_hash.empty? && @@observers.empty?)
        Dnsruby.log.debug{"Stopping select loop"}
        return
      end
    }
    #              }
  end
end

#get_client_id_from_answerfrom(socket, answerip, answerport) ⇒ Object



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/Dnsruby/select_thread.rb', line 391

def get_client_id_from_answerfrom(socket, answerip, answerport)
  # @TODO@ Can get rid of this, as there is only one query per socket
  client_id=nil
  # Figure out client id from answerfrom
  @@mutex.synchronize{
    ids = @@socket_hash[socket]
    ids.each do |id|
      # Does this id speak to this dest_server?
      query_settings = @@query_hash[id]
      if (answerip == query_settings.dest_server && answerport == query_settings.dest_port)
        # We have a match
        # - @TODO@ as long as we're not speaking to the same server on two ports!
        client_id = id
        break
      end
    end
  }
  return client_id
end

#get_incoming_data(socket, packet_size) ⇒ Object



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/Dnsruby/select_thread.rb', line 299

def get_incoming_data(socket, packet_size)
  answerfrom,answerip,answerport,answersize=nil
  ans,buf = nil
  begin
    if (socket.class == TCPSocket)
      # @todo@ Ruby Bug #9061 stops this working right
      # We'd like to do a socket.recvfrom, but that raises an Exception
      # on Windows for TCPSocket for Ruby 1.8.5 (and 1.8.6).
      # So, we need to do something different for TCP than UDP. *sigh*
      # @TODO@ This workaround will only work if there is exactly one socket per query
      #    - *not* ideal TCP use!
      @@mutex.synchronize{
        client_id = @@socket_hash[socket][0]
        answerfrom = @@query_hash[client_id].dest_server
        answerip = answerfrom
        answerport = @@query_hash[client_id].dest_port
      }
      buf = tcp_read(socket, 2)
      if (!buf)
        handle_recvfrom_failure(socket, "")          
        return
      end
      answersize = buf.unpack('n')[0]
      buf = tcp_read(socket,answersize)
      if (!buf)
        handle_recvfrom_failure(socket, "")          
        return
      end
    else
      if (ret = socket.recvfrom(packet_size))
        buf = ret[0]
        answerport=ret[1][1]
        answerfrom=ret[1][2]
        answerip=ret[1][3]
        answersize=(buf.length)
      else
        # recvfrom failed - why?
        Dnsruby.log.error{"Error - recvfrom failed from #{socket}"}
        handle_recvfrom_failure(socket, "")          
        return
      end        
    end
  rescue Exception => e
    Dnsruby.log.error{"Error - recvfrom failed from #{socket}, exception : #{e}"}
    handle_recvfrom_failure(socket, e)          
    return
  end
  Dnsruby.log.debug{";; answer from #{answerfrom} : #{answersize} bytes\n"}
  
  begin
    ans = Message.decode(buf)
  rescue Exception => e
#        print "DECODE ERROR\n"
    Dnsruby.log.error{"Decode error! #{e.class}, #{e}\nfor msg (length=#{buf.length}) : #{buf}"}
    # @TODO@ Should know this from the socket!
    client_id=get_client_id_from_answerfrom(socket, answerip, answerport)
    if (client_id != nil) 
      send_exception_to_client(e, socket, client_id)
    else
      Dnsruby.log.error{"Decode error from #{answerfrom} but can't determine packet id"}
    end
    return
  end
  
  if (ans!= nil)
    Dnsruby.log.debug{"#{ans}"}
    ans.answerfrom=(answerfrom)
    ans.answersize=(answersize)
    ans.answerip =(answerip)
  end
  return ans, buf
end

#handle_recvfrom_failure(socket, exception) ⇒ Object



372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
# File 'lib/Dnsruby/select_thread.rb', line 372

def handle_recvfrom_failure(socket, exception)
  #  No way to notify the client about this error, unless there was only one connection on the socket
  # Not a problem, as there only will ever be one connection on the socket (Kaminsky attack mitigation)
  ids_for_socket = []
  @@mutex.synchronize{
    ids_for_socket = @@socket_hash[socket]
  }
  if (ids_for_socket.length == 1)
    answerfrom=nil
    @@mutex.synchronize{
      query_settings = @@query_hash[ids_for_socket[0]]
      answerfrom=query_settings.dest_server
    }
    send_exception_to_client(OtherResolvError.new("recvfrom failed from #{answerfrom}; #{exception}"), socket, ids_for_socket[0])
  else
    Dnsruby.log.fatal{"Recvfrom failed from #{socket}, no way to tell query id"}
  end
end

#notify_queue_observers(client_queue, client_query_id) ⇒ Object



574
575
576
577
578
579
580
581
582
583
# File 'lib/Dnsruby/select_thread.rb', line 574

def notify_queue_observers(client_queue, client_query_id)
  # If any observers are known for this query queue then notify them
  observer=nil
  @@mutex.synchronize {
    observer = @@observers[client_queue]
  }
  if (observer)
    observer.handle_queue_event(client_queue, client_query_id)
  end      
end

#process_error(errors) ⇒ Object



179
180
181
182
# File 'lib/Dnsruby/select_thread.rb', line 179

def process_error(errors)
  Dnsruby.log.debug{"Error! #{errors.inspect}"}
  # @todo@ Process errors [can we do this in single socket environment?]
end

#process_ready(ready) ⇒ Object

@@query_hash=query_settings

@@socket_hash[query_settings.socket]=[query_settings.client_query_id] # @todo@ If we use persistent sockets then we need to update this array


186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/Dnsruby/select_thread.rb', line 186

def process_ready(ready)
  ready.each do |socket|
    query_settings = nil
    @@mutex.synchronize{
      # Can do this if we have a query per socket, but not otherwise...
      c_q_id = @@socket_hash[socket][0] # @todo@ If we use persistent sockets then this won't work
      query_settings = @@query_hash[c_q_id]
    }
    next if !query_settings
    udp_packet_size = query_settings.udp_packet_size
    msg, bytes = get_incoming_data(socket, udp_packet_size)
    if (msg!=nil)
      # Check that the IP we received from was the IP we sent to!
      answerip = msg.answerip.downcase
      answerfrom = msg.answerfrom.downcase
      dest_server = query_settings.dest_server
      if (dest_server && (dest_server != '0.0.0.0') &&
          (answerip != query_settings.dest_server.downcase) &&
             (answerfrom != query_settings.dest_server.downcase))
        Dnsruby.log.warn("Unsolicited response received from #{answerip} instead of #{query_settings.dest_server}")
      else 
        send_response_to_client(msg, bytes, socket)
      end
    end
    ready.delete(socket)
  end
end

#process_timeoutsObject



273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/Dnsruby/select_thread.rb', line 273

def process_timeouts
  time_now = Time.now
  timeouts={}
  @@mutex.synchronize {
    timeouts = @@timeouts
  }
  timeouts.each do |client_id, timeout|
    if (timeout < time_now)
      send_exception_to_client(ResolvTimeout.new("Query timed out"), nil, client_id)
    end
  end
end

#push_exception_to_select(client_id, client_queue, err, msg) ⇒ Object



423
424
425
426
427
428
429
430
431
432
433
434
# File 'lib/Dnsruby/select_thread.rb', line 423

def push_exception_to_select(client_id, client_queue, err, msg)
  @@mutex.synchronize{
    @@queued_exceptions.push([client_id, client_queue, err, msg])
  }
  # Make sure select loop is running!
  if (@@select_thread && @@select_thread.alive?)
  else
    @@select_thread = Thread.new {
      do_select
    }      
  end
end

#push_response_to_select(client_id, client_queue, msg, query, res) ⇒ Object



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/Dnsruby/select_thread.rb', line 436

def push_response_to_select(client_id, client_queue, msg, query, res)
  # This needs to queue the response TO THE SELECT THREAD, which then needs
  # to send it out from its normal loop.
  Dnsruby.log.debug{"Pushing response to client queue direct from resolver or validator"}
  @@mutex.synchronize{
    @@queued_responses.push([client_id, client_queue, msg, nil, query, res])
  }
  # Make sure select loop is running!
  if (@@select_thread && @@select_thread.alive?)
  else
    @@select_thread = Thread.new {
      do_select
    }
  end
end

#push_to_client(client_id, client_queue, msg, err, query, res) ⇒ Object



528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
# File 'lib/Dnsruby/select_thread.rb', line 528

def push_to_client(client_id, client_queue, msg, err, query, res)
  # @TODO@ Really need to let the client know that we have received a valid response!
  # Can do that by calling notify_observers here, but with an identifier which
  # defines the response to be a "Response received - validating. Please stop sending"
  # type of response.
  client_queue.push([client_id, Resolver::EventType::RECEIVED, msg, err])
  notify_queue_observers(client_queue, client_id)

  if (!err || (err.instance_of?(NXDomain)))
  #
  # This method now needs to push the response to the validator,
  # which will then take responsibility for delivering it to the client.
  # The validator will need access to the queue observers -
  validator = ValidatorThread.new(client_id, client_queue, msg, err, query ,self, res)
  validator.run
  #      @@validator.add_to_queue([client_id, client_queue, msg, err, query, self, res])
  end
end

#push_validation_response_to_select(client_id, client_queue, msg, err, query, res) ⇒ Object



452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'lib/Dnsruby/select_thread.rb', line 452

def push_validation_response_to_select(client_id, client_queue, msg, err, query, res)
  # This needs to queue the response TO THE SELECT THREAD, which then needs
  # to send it out from its normal loop.
  Dnsruby.log.debug{"Pushing response to client queue direct from resolver or validator"}
  @@mutex.synchronize{
    @@queued_validation_responses.push([client_id, client_queue, msg, err, query, res])
  }
  # Make sure select loop is running!
  if (@@select_thread && @@select_thread.alive?)
  else
    @@select_thread = Thread.new {
      do_select
    }
  end
end

#remove_id(id) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
# File 'lib/Dnsruby/select_thread.rb', line 260

def remove_id(id)
  socket=nil
  @@mutex.synchronize{
    socket = @@query_hash[id].socket
    @@timeouts.delete(id)
    @@query_hash.delete(id)  
    @@socket_hash.delete(socket)        
    @@sockets.delete(socket) # @TODO@ Not if persistent!
  }
  Dnsruby.log.debug{"Closing socket #{socket}"}
  socket.close # @TODO@ Not if persistent!
end

#remove_observer(client_queue, observer) ⇒ Object



557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/Dnsruby/select_thread.rb', line 557

def remove_observer(client_queue, observer)
  @@mutex.synchronize {
    if (@@observers[client_queue]==observer)
      #          @@observers.delete(observer)
      @@observers.delete(client_queue)
    else
      if (@@observers[client_queue] == nil)
      end
      Dnsruby.log.error{"remove_observer called with wrong observer for queue"}
      raise ArgumentError.new("remove_observer called with wrong observer for queue")
    end
    if (!@@observers.values.include?observer)
      @@tick_observers.delete(observer)
    end
  }
end

#select_thread_alive?Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
# File 'lib/Dnsruby/select_thread.rb', line 112

def select_thread_alive?
  ret=true
  @@mutex.synchronize{
    ret = @@select_thread.alive?
  }
  return ret
end

#send_exception_to_client(err, socket, client_id, msg = nil) ⇒ Object



411
412
413
414
415
416
417
418
419
420
421
# File 'lib/Dnsruby/select_thread.rb', line 411

def send_exception_to_client(err, socket, client_id, msg=nil)
  # find the client response queue
  client_queue = nil
  @@mutex.synchronize {
    client_queue = @@query_hash[client_id].client_queue
  }
  remove_id(client_id)
  #      push_to_client(client_id, client_queue, msg, err)
  client_queue.push([client_id, Resolver::EventType::ERROR, msg, err])
  notify_queue_observers(client_queue, client_id)
end

#send_queued_exceptionsObject



468
469
470
471
472
473
474
475
476
477
478
479
480
481
# File 'lib/Dnsruby/select_thread.rb', line 468

def send_queued_exceptions
  exceptions = []
  @@mutex.synchronize{
    exceptions = @@queued_exceptions
    @@queued_exceptions = []
  }
  
  exceptions.each do |item|
    client_id, client_queue, err, msg = item
    #        push_to_client(client_id, client_queue, msg, err)
    client_queue.push([client_id, Resolver::EventType::ERROR, msg, err])
    notify_queue_observers(client_queue, client_id)
  end
end

#send_queued_responsesObject



483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'lib/Dnsruby/select_thread.rb', line 483

def send_queued_responses
  responses = []
  @@mutex.synchronize{
    responses = @@queued_responses
    @@queued_responses = []
  }

  responses.each do |item|
    client_id, client_queue, msg, err, query, res = item
    #        push_to_client(client_id, client_queue, msg, err)
    client_queue.push([client_id, Resolver::EventType::RECEIVED, msg, err])
    notify_queue_observers(client_queue, client_id)
    # @TODO@ Do we need to validate this? The response has come from the cache -
    # validate it only if it has not been validated already
    # So, if we need to validate it, send it to the validation thread
    # Otherwise, send VALIDATED to the requester.
    # Should we really just be checking (level != SECURE) ?
    if (((msg.security_level == Message::SecurityLevel::UNCHECKED) ||
          (msg.security_level == Message::SecurityLevel::INDETERMINATE)) &&
        (ValidatorThread.requires_validation?(query, msg, err, res)))
      validator = ValidatorThread.new(client_id, client_queue, msg, err, query ,self, res)
      validator.run
    else
      PacketSender.cache(query, msg) # The validator won't cache it, so we'd better do it now
      client_queue.push([client_id, Resolver::EventType::VALIDATED, msg, err])
      notify_queue_observers(client_queue, client_id)
    end
  end
end

#send_queued_validation_responsesObject



513
514
515
516
517
518
519
520
521
522
523
524
525
526
# File 'lib/Dnsruby/select_thread.rb', line 513

def send_queued_validation_responses
  responses = []
  @@mutex.synchronize{
    responses = @@queued_validation_responses
    @@queued_validation_responses = []
  }

  responses.each do |item|
    client_id, client_queue, msg, err, query, res = item
    #        push_to_client(client_id, client_queue, msg, err)
    client_queue.push([client_id, Resolver::EventType::VALIDATED, msg, err])
    notify_queue_observers(client_queue, client_id)
  end
end

#send_response_to_client(msg, bytes, socket) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/Dnsruby/select_thread.rb', line 214

def send_response_to_client(msg, bytes, socket)
  # Figure out which client_ids we were expecting on this socket, then see if any header ids match up
  # @TODO@ Can get rid of this, as we only have one query per socket.
  client_ids=[]
  @@mutex.synchronize{
    client_ids = @@socket_hash[socket]
  }
  # get the queries associated with them
  client_ids.each do |id|
    query_header_id=nil
    @@mutex.synchronize{
      query_header_id = @@query_hash[id].query.header.id
    }
    if (query_header_id == msg.header.id)
      # process the response
      client_queue = nil
      res = nil
      query=nil
      @@mutex.synchronize{
        client_queue = @@query_hash[id].client_queue
        res = @@query_hash[id].single_resolver
        query = @@query_hash[id].query
      }
      tcp = (socket.class == TCPSocket)
      # At this point, we should check if the response is OK
      if (ret = res.check_response(msg, bytes, query, client_queue, id, tcp))
        remove_id(id)
        exception = msg.get_exception
        if (ret.instance_of?TsigError)
          exception = ret
        end
        Dnsruby.log.debug{"Pushing response to client queue"}
        push_to_client(id, client_queue, msg, exception, query, res)
        #            client_queue.push([id, msg, exception])
        #            notify_queue_observers(client_queue, id)
      else
        # Sending query again - don't return response
      end
      return
    end
  end
  # If not, then we have an error
  Dnsruby.log.error{"Stray packet - " + msg.inspect + "\n from " + socket.inspect}
  print("Stray packet - " + msg.question()[0].qname.to_s + " from " + msg.answerip.to_s + ", #{client_ids.length} client_ids\n")
end

#send_tick_to_observersObject



585
586
587
588
589
590
591
592
593
594
# File 'lib/Dnsruby/select_thread.rb', line 585

def send_tick_to_observers
  # If any observers are known then send them a tick
  tick_observers=nil
  @@mutex.synchronize {
    tick_observers = @@tick_observers
  }
  tick_observers.each do |observer|
    observer.tick
  end
end

#tcp_read(socket, len) ⇒ Object



286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/Dnsruby/select_thread.rb', line 286

def tcp_read(socket, len)
  buf=""
  while (buf.length < len) do
    input = socket.recv(len-buf.length) 
    if (input=="")
      TheLog.info("Bad response from server - no bytes read - ignoring")
      return false
    end
    buf += input
  end
  return buf
end