-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathucast.rb
More file actions
107 lines (86 loc) · 2.35 KB
/
Copy pathucast.rb
File metadata and controls
107 lines (86 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/local/bin/ruby18 -w
# Author: Sam Roberts <sroberts@uniserve.com>
# Licence: this file is placed in the public domain
$:.unshift(File.dirname($0))
require 'getoptlong'
require 'net/dns/resolvx'
require 'net/dns'
class Resolv
class DNS
class Resource
module IN
class SRV
def inspect
"#{target}:#{port} weight=#{weight} priority=#{priority}"
end
end
class TXT
def inspect
strings.inspect
end
end
class PTR
def inspect
name.to_s
end
end
class A
def inspect
address.to_s
end
end
class HINFO
def inspect
"os=#{os.inspect}\ncpu=#{cpu.inspect}"
end
end
end
end
end
end
include Net::DNS
$stderr.sync = true
$stdout.sync = true
Addr = '224.0.0.251'
Port = 5353
$id = 1
def ask(name, type)
$id += 1
qmsg = Message.new($id)
qmsg.add_question(name, type)
@sock.send(qmsg.encode, 0, Addr,Port)
end
@sock = UDPSocket.new
ask(Name.create(ARGV.first || '_http._tcp.local'), IN::PTR)
loop do
reply, from = @sock.recvfrom(9000)
puts "++ from #{from.inspect}"
msg = Resolv::DNS::Message.decode(reply)
qr = msg.qr==0 ? 'Q' : 'R'
qrstr = msg.qr==0 ? 'Query' : 'Resp'
opcode = { 0=>'QUERY', 1=>'IQUERY', 2=>'STATUS'}[msg.opcode]
puts "#{qrstr}: id #{msg.id} qr #{qr} opcode #{opcode} aa #{msg.aa} tc #{msg.tc} rd #{msg.rd} ra #{msg.ra} rcode #{msg.rcode}"
msg.question.each do |name, type, unicast|
puts "qu #{Net::DNS.rrname type} #{name.to_s.inspect} unicast=#{unicast}"
end
msg.answer.each do |name, ttl, data, cacheflush|
puts "an #{Net::DNS.rrname data} #{name.to_s.inspect} ttl=#{ttl} cacheflush=#{cacheflush}"
puts " #{data.inspect}"
case data
when IN::PTR
ask(data.name, IN::SRV)
ask(data.name, IN::TXT)
when IN::SRV
ask(data.target, IN::A)
end
end
msg.authority.each do |name, ttl, data, cacheflush|
puts "au #{Net::DNS.rrname data} #{name.to_s.inspect} ttl=#{ttl} cacheflush=#{cacheflush.inspect}"
puts " #{data.inspect}"
end
msg.additional.each do |name, ttl, data, cacheflush|
puts "ad #{Net::DNS.rrname data} #{name.to_s.inspect} ttl=#{ttl} cacheflush=#{cacheflush.inspect}"
puts " #{data.inspect}"
end
puts
end