Class: MDNS
- Inherits:
-
Object
- Object
- MDNS
- Defined in:
- lib/mdns.rb,
lib/mdns/version.rb
Defined Under Namespace
Classes: Record
Constant Summary collapse
- MULTICAST_IP =
'224.0.0.251'
- MDNS_PORT =
5353
- VERSION =
"0.2.1"
Class Method Summary collapse
- .add_record(host, ttl, ip) ⇒ Object
- .hosts ⇒ Object
- .records ⇒ Object
- .reset ⇒ Object
- .respond(record, query = nil, ip = MULTICAST_IP, port = MDNS_PORT) ⇒ Object
- .start ⇒ Object
- .stop ⇒ Object
Class Method Details
.add_record(host, ttl, ip) ⇒ Object
70 71 72 73 |
# File 'lib/mdns.rb', line 70 def add_record(host, ttl, ip) records[host] = Record.new(host, ttl, ip) respond(records[host]) end |
.hosts ⇒ Object
79 80 81 |
# File 'lib/mdns.rb', line 79 def hosts records.keys end |
.records ⇒ Object
75 76 77 |
# File 'lib/mdns.rb', line 75 def records @records ||= {} end |
.reset ⇒ Object
83 84 85 86 |
# File 'lib/mdns.rb', line 83 def reset @records = nil stop end |
.respond(record, query = nil, ip = MULTICAST_IP, port = MDNS_PORT) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/mdns.rb', line 53 def respond(record, query = nil, ip = MULTICAST_IP, port = MDNS_PORT) return if !@socket || @socket.closed? # I have no idea what I'm doing response = Resolv::DNS::Message.new(query ? query.id : 0) response.qr = 1 response.opcode = 0 response.aa = 1 response.rd = 0 response.ra = 0 response.rcode = 0 if query response.add_question(*query.question.first) end response.add_answer(record.host, record.ttl, Resolv::DNS::Resource::IN::A.new(record.ip)) @socket.send(response.encode, 0, ip, port) end |
.start ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/mdns.rb', line 12 def start @socket = UDPSocket.new @socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true) @socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEPORT, true) if Socket.const_defined?("SO_REUSEPORT") ip_mreq = IPAddr.new(MULTICAST_IP).hton + IPAddr.new('0.0.0.0').hton @socket.setsockopt(Socket::IPPROTO_IP, Socket::IP_ADD_MEMBERSHIP, ip_mreq) @socket.setsockopt(Socket::IPPROTO_IP, Socket::IP_TTL, 255) @socket.setsockopt(Socket::IPPROTO_IP, Socket::IP_MULTICAST_TTL, 255) @socket.bind(Socket::INADDR_ANY, MDNS_PORT) Thread.abort_on_exception = true @thr = Thread.new do loop do data, address = @socket.recvfrom(1024) packet = begin Resolv::DNS::Message.decode(data) rescue Resolv::DNS::DecodeError nil # Invalid DNS data rescue nil # Sometimes it errors out due to non DNS data end next if packet.nil? if packet.qr == 0 hosts = packet.question.map(&:first).map(&:to_s) matches = self.hosts & hosts matches.each do |host| respond(records[host], packet, address[3], address[1]) end end end end # Broadcast records records.values.each do |record| respond(record) end at_exit do stop end end |
.stop ⇒ Object
88 89 90 91 92 93 94 95 96 |
# File 'lib/mdns.rb', line 88 def stop if @thr @thr.kill @thr = nil end if @socket && !@socket.closed? @socket.close end end |