Method: Net::DNS::MDNS::Responder#initialize

Defined in:
lib/net/dns/mdns.rb

#initializeResponder

Returns a new instance of Responder.



427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
# File 'lib/net/dns/mdns.rb', line 427

def initialize
  @log = Logger.new(STDERR)

  @log.level = Logger::ERROR

  @mutex = Mutex.new

  @cache = Cache.new

  @queries = []

  @services = []

  @hostname = Name.create(Socket.gethostname)
  @hostname.absolute = true
  @hostaddr = Socket.getaddrinfo(@hostname.to_s, 0, Socket::AF_INET, Socket::SOCK_STREAM)[0][3]
  @hostrr   = [ @hostname, 240, IN::A.new(@hostaddr) ]
  @hostaddr = IPAddr.new(@hostaddr).hton

  debug( "start" )

  # TODO - I'm not sure about how robust this is. A better way to find the default
  # ifx would be to do:
  #   s = UDPSocket.new
  #   s.connect(any addr, any port)
  #   s.getsockname => struct sockaddr_in => ip_addr
  # But parsing a struct sockaddr_in is a PITA in ruby.

  @sock = UDPSocket.new

  # Set the close-on-exec flag, if supported.
  if Fcntl.constants.include? 'F_SETFD'
    @sock.fcntl(Fcntl::F_SETFD, 1)
  end

  # Allow 5353 to be shared.
  so_reuseport = 0x0200
  # The definition on OS X, where it is required, and where the shipped
  # ruby version (1.6) does not have Socket::SO_REUSEPORT. The definition
  # seems to be shared by at least some other BSD-derived stacks.
  if Socket.constants.include? 'SO_REUSEPORT'
    so_reuseport = Socket::SO_REUSEPORT
  end
  begin
    @sock.setsockopt(Socket::SOL_SOCKET, so_reuseport, 1)
  rescue
    warn( "set SO_REUSEPORT raised #{$!}, try SO_REUSEADDR" )
    so_reuseport = Socket::SO_REUSEADDR
    @sock.setsockopt(Socket::SOL_SOCKET, so_reuseport, 1)
  end

  # Request dest addr and ifx ids... no.

  # Bind to our port.
  @sock.bind(Socket::INADDR_ANY, Port)

  # Join the multicast group.
  #  option is a struct ip_mreq { struct in_addr, struct in_addr }
  ip_mreq =  IPAddr.new(Addr).hton + @hostaddr
  @sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_ADD_MEMBERSHIP, ip_mreq)
  @sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_MULTICAST_IF, @hostaddr)

  # Set IP TTL for outgoing packets.
  @sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_TTL, 255)
  @sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_MULTICAST_TTL, 255)

  # Apple source makes it appear that optval may need to be a "char" on
  # some systems:
  #  @sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_MULTICAST_TTL, 255 as int)
  #     - or -
  #  @sock.setsockopt(Socket::IPPROTO_IP, Socket::IP_MULTICAST_TTL, 255 as byte)

  # Start responder and cacher threads.

  @waketime = nil

  @cacher_thrd = Thread.new do
    begin
      cacher_loop
    rescue
      error( "cacher_loop exited with #{$!}" )
      $!.backtrace.each do |e| error(e) end
    end
  end

  @responder_thrd = Thread.new do
    begin
      responder_loop
    rescue
      error( "responder_loop exited with #{$!}" )
      $!.backtrace.each do |e| error(e) end
    end
  end
end