Class: Resolv::DNS

Inherits:
Object
  • Object
show all
Defined in:
lib/logmerge/resolv.rb

Defined Under Namespace

Modules: Label, OpCode, RCode Classes: Config, DecodeError, EncodeError, Message, Name, Query, Requester, Resource

Constant Summary collapse

Port =
53
UDPSize =
512
DNSThreadGroup =
ThreadGroup.new

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_info = nil) ⇒ DNS

Returns a new instance of DNS.



385
386
387
388
389
# File 'lib/logmerge/resolv.rb', line 385

def initialize(config_info=nil)
  @mutex = Mutex.new
  @config = Config.new(config_info)
  @initialized = nil
end

Class Method Details

.open(*args) ⇒ Object



375
376
377
378
379
380
381
382
383
# File 'lib/logmerge/resolv.rb', line 375

def self.open(*args)
  dns = new(*args)
  return dns unless block_given?
  begin
    yield dns
  ensure
    dns.close
  end
end

Instance Method Details

#closeObject



408
409
410
411
412
413
414
415
416
# File 'lib/logmerge/resolv.rb', line 408

def close
  @mutex.synchronize {
    if @initialized
      @requester.close if @requester
      @requester = nil
      @initialized = false
    end
  }
end

#each_address(name) ⇒ Object



429
430
431
# File 'lib/logmerge/resolv.rb', line 429

def each_address(name)
  each_resource(name, Resource::IN::A) {|resource| yield resource.address}
end

#each_name(address) ⇒ Object



444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/logmerge/resolv.rb', line 444

def each_name(address)
  case address
  when Name
    ptr = address
  when IPv4::Regex
    ptr = IPv4.create(address).to_name
  when IPv6::Regex
    ptr = IPv6.create(address).to_name
  else
    raise ResolvError.new("cannot interpret as address: #{address}")
  end
  each_resource(ptr, Resource::IN::PTR) {|resource| yield resource.name}
end

#each_resource(name, typeclass, &proc) ⇒ Object



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
# File 'lib/logmerge/resolv.rb', line 469

def each_resource(name, typeclass, &proc)
  lazy_initialize
  q = Queue.new
  senders = {}
  begin
    @config.resolv(name) {|candidate, tout, nameserver|
      msg = Message.new
      msg.rd = 1
      msg.add_question(candidate, typeclass)
      unless sender = senders[[candidate, nameserver]]
        sender = senders[[candidate, nameserver]] =
          @requester.sender(msg, candidate, q, nameserver)
      end
      sender.send
      reply = reply_name = nil
      timeout(tout, ResolvTimeout) { reply, reply_name = q.pop }
      case reply.rcode
      when RCode::NoError
        extract_resources(reply, reply_name, typeclass, &proc)
        return
      when RCode::NXDomain
        raise Config::NXDomain.new(reply_name.to_s)
      else
        raise Config::OtherResolvError.new(reply_name.to_s)
      end
    }
  ensure
    @requester.delete(q)
  end
end

#extract_resources(msg, name, typeclass) ⇒ Object



500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
# File 'lib/logmerge/resolv.rb', line 500

def extract_resources(msg, name, typeclass)
  if typeclass < Resource::ANY
    n0 = Name.create(name)
    msg.each_answer {|n, ttl, data|
      yield data if n0 == n
    }
  end
  yielded = false
  n0 = Name.create(name)
  msg.each_answer {|n, ttl, data|
    if n0 == n
      case data
      when typeclass
        yield data
        yielded = true
      when Resource::CNAME
        n0 = data.name
      end
    end
  }
  return if yielded
  msg.each_answer {|n, ttl, data|
    if n0 == n
      case data
      when typeclass
        yield data
      end
    end
  }
end

#getaddress(name) ⇒ Object

Raises:



418
419
420
421
# File 'lib/logmerge/resolv.rb', line 418

def getaddress(name)
  each_address(name) {|address| return address}
  raise ResolvError.new("DNS result has no information for #{name}")
end

#getaddresses(name) ⇒ Object



423
424
425
426
427
# File 'lib/logmerge/resolv.rb', line 423

def getaddresses(name)
  ret = []
  each_address(name) {|address| ret << address}
  return ret
end

#getname(address) ⇒ Object

Raises:



433
434
435
436
# File 'lib/logmerge/resolv.rb', line 433

def getname(address)
  each_name(address) {|name| return name}
  raise ResolvError.new("DNS result has no information for #{address}")
end

#getnames(address) ⇒ Object



438
439
440
441
442
# File 'lib/logmerge/resolv.rb', line 438

def getnames(address)
  ret = []
  each_name(address) {|name| ret << name}
  return ret
end

#getresource(name, typeclass) ⇒ Object

Raises:



458
459
460
461
# File 'lib/logmerge/resolv.rb', line 458

def getresource(name, typeclass)
  each_resource(name, typeclass) {|resource| return resource}
  raise ResolvError.new("DNS result has no information for #{name}")
end

#getresources(name, typeclass) ⇒ Object



463
464
465
466
467
# File 'lib/logmerge/resolv.rb', line 463

def getresources(name, typeclass)
  ret = []
  each_resource(name, typeclass) {|resource| ret << resource}
  return ret
end

#lazy_initializeObject



391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/logmerge/resolv.rb', line 391

def lazy_initialize
  @mutex.synchronize {
    unless @initialized
      @config.lazy_initialize

      if nameserver = @config.single?
        @requester = Requester::ConnectedUDP.new(nameserver)
      else
        @requester = Requester::UnconnectedUDP.new
      end

      @initialized = true
    end
  }
  self
end