rubydns 0.6.0 → 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +6 -14
- data/.gitignore +23 -14
- data/.rspec +4 -0
- data/.simplecov +15 -0
- data/.travis.yml +9 -5
- data/.yardopts +1 -0
- data/Gemfile +6 -2
- data/README.md +82 -92
- data/Rakefile +2 -5
- data/bin/rubydns-check +374 -0
- data/examples/Gemfile +7 -0
- data/examples/README.md +137 -0
- data/examples/basic-dns.rb +24 -0
- data/examples/cname.rb +25 -0
- data/{test/examples/dropping-dns.rb → examples/flakey-dns.rb} +28 -31
- data/{test/examples → examples}/fortune-dns.rb +42 -46
- data/examples/geoip-dns.rb +115 -0
- data/examples/simple.rb +25 -0
- data/{test/examples → examples}/soa-dns.rb +27 -27
- data/{test/examples → examples}/test-dns-1.rb +26 -20
- data/{test/examples → examples}/test-dns-2.rb +17 -19
- data/examples/wikipedia-dns.rb +107 -0
- data/lib/rubydns/rule_based_server.rb +180 -0
- data/lib/rubydns/version.rb +1 -1
- data/lib/rubydns.rb +13 -63
- data/rubydns.gemspec +29 -23
- data/spec/rubydns/daemon_spec.rb +114 -0
- data/{test/test_system.rb → spec/rubydns/injected_supervisor_spec.rb} +32 -25
- data/spec/rubydns/passthrough_spec.rb +85 -0
- data/spec/rubydns/rules_spec.rb +74 -0
- data/spec/spec_helper.rb +30 -0
- metadata +101 -78
- data/bin/rd-dns-check +0 -374
- data/bin/rd-resolve-test +0 -160
- data/lib/rubydns/chunked.rb +0 -34
- data/lib/rubydns/extensions/hexdump.rb +0 -38
- data/lib/rubydns/extensions/logger.rb +0 -30
- data/lib/rubydns/extensions/resolv.rb +0 -53
- data/lib/rubydns/extensions/string-1.8.rb +0 -35
- data/lib/rubydns/extensions/string-1.9.2.rb +0 -29
- data/lib/rubydns/extensions/string-1.9.3.rb +0 -31
- data/lib/rubydns/extensions/string.rb +0 -27
- data/lib/rubydns/handler.rb +0 -140
- data/lib/rubydns/message.rb +0 -41
- data/lib/rubydns/resolver.rb +0 -239
- data/lib/rubydns/server.rb +0 -241
- data/lib/rubydns/system.rb +0 -146
- data/lib/rubydns/transaction.rb +0 -250
- data/test/examples/geoip-dns.rb +0 -86
- data/test/helper.rb +0 -9
- data/test/test_daemon.rb +0 -100
- data/test/test_domains.txt +0 -185
- data/test/test_passthrough.rb +0 -80
- data/test/test_resolver.rb +0 -105
- data/test/test_rules.rb +0 -74
- data/test/test_slow_server.rb +0 -98
- data/test/test_truncation.rb +0 -78
- /data/{test → spec/rubydns}/hosts.txt +0 -0
data/bin/rd-resolve-test
DELETED
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env ruby
|
|
2
|
-
# Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
|
3
|
-
#
|
|
4
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
-
# of this software and associated documentation files (the "Software"), to deal
|
|
6
|
-
# in the Software without restriction, including without limitation the rights
|
|
7
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
-
# copies of the Software, and to permit persons to whom the Software is
|
|
9
|
-
# furnished to do so, subject to the following conditions:
|
|
10
|
-
#
|
|
11
|
-
# The above copyright notice and this permission notice shall be included in
|
|
12
|
-
# all copies or substantial portions of the Software.
|
|
13
|
-
#
|
|
14
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20
|
-
# THE SOFTWARE.
|
|
21
|
-
|
|
22
|
-
require 'rubydns/version'
|
|
23
|
-
|
|
24
|
-
require 'resolv'
|
|
25
|
-
require 'optparse'
|
|
26
|
-
|
|
27
|
-
OPTIONS = {
|
|
28
|
-
:Domains => [],
|
|
29
|
-
:Nameservers => [],
|
|
30
|
-
:Timeout => 0.5,
|
|
31
|
-
|
|
32
|
-
:Threads => 10,
|
|
33
|
-
:Requests => 20
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
ARGV.options do |o|
|
|
37
|
-
script_name = File.basename($0)
|
|
38
|
-
|
|
39
|
-
o.banner = "Usage: #{script_name} [options] nameserver [nameserver]"
|
|
40
|
-
|
|
41
|
-
o.on("-d [path]", String, "Specify a file that contains a list of domains") do |path|
|
|
42
|
-
OPTIONS[:Domains] += File.readlines(path).collect { |name| name.strip.downcase }
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
o.on("-t [timeout]", Float, "Queries that take longer than this will be printed") do |timeout|
|
|
46
|
-
OPTIONS[:Timeout] = timeout.to_f
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
o.on("--threads [count]", Integer, "Number of threads to resolve names concurrently") do |count|
|
|
50
|
-
OPTIONS[:Threads] = count.to_i
|
|
51
|
-
end
|
|
52
|
-
|
|
53
|
-
o.on("--requests [count]", Integer, "Number of requests to perform per thread") do |count|
|
|
54
|
-
OPTIONS[:Requests] = count.to_i
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
o.on_tail("--copy", "Display copyright information") do
|
|
58
|
-
puts "#{script_name} v#{RubyDNS::VERSION::STRING}. Copyright (c) 2009, 2011 Samuel Williams. Released under the MIT license."
|
|
59
|
-
puts "See http://www.oriontransfer.co.nz/ for more information."
|
|
60
|
-
|
|
61
|
-
exit
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
o.on_tail("-h", "--help", "Show this help message.") { puts o; exit }
|
|
65
|
-
end.parse!
|
|
66
|
-
|
|
67
|
-
OPTIONS[:Nameservers] = ARGV
|
|
68
|
-
|
|
69
|
-
if OPTIONS[:Nameservers].size > 0
|
|
70
|
-
$R = Resolv::DNS.new(:nameserver => ARGV)
|
|
71
|
-
else
|
|
72
|
-
$R = Resolv::DNS.new
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
$TG = ThreadGroup.new
|
|
76
|
-
$M = Mutex.new
|
|
77
|
-
$STATUS = {}
|
|
78
|
-
$TOTAL = [0.0, 0]
|
|
79
|
-
|
|
80
|
-
if OPTIONS[:Domains].size == 0
|
|
81
|
-
OPTIONS[:Domains] += ["www.google.com", "www.amazon.com", "www.apple.com", "www.microsoft.com"]
|
|
82
|
-
OPTIONS[:Domains] += ["www.rubygems.org", "www.ruby-lang.org", "www.slashdot.org", "www.lucidsystems.org"]
|
|
83
|
-
OPTIONS[:Domains] += ["www.facebook.com", "www.twitter.com", "www.myspace.com", "www.youtube.com"]
|
|
84
|
-
OPTIONS[:Domains] += ["www.oriontransfer.co.nz", "www.digg.com"]
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
def random_domain
|
|
88
|
-
d = OPTIONS[:Domains]
|
|
89
|
-
|
|
90
|
-
d[rand(d.size - 1)]
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
puts "Starting test with #{OPTIONS[:Domains].size} domains..."
|
|
94
|
-
puts "Using nameservers: " + OPTIONS[:Nameservers].join(", ")
|
|
95
|
-
puts "Only long running queries will be printed..."
|
|
96
|
-
|
|
97
|
-
def resolve_domain
|
|
98
|
-
s = Time.now
|
|
99
|
-
result = nil
|
|
100
|
-
name = random_domain
|
|
101
|
-
|
|
102
|
-
begin
|
|
103
|
-
result = [$R.getaddress(name)]
|
|
104
|
-
rescue Resolv::ResolvError
|
|
105
|
-
$M.synchronize do
|
|
106
|
-
puts "Name #{name} failed to resolve!"
|
|
107
|
-
$STATUS[name] ||= []
|
|
108
|
-
$STATUS[name] << :failure
|
|
109
|
-
|
|
110
|
-
if $STATUS[name].include?(:success)
|
|
111
|
-
puts "Name #{name} has had previous successes!"
|
|
112
|
-
end
|
|
113
|
-
end
|
|
114
|
-
|
|
115
|
-
return
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
result.unshift(name)
|
|
119
|
-
result.unshift(Time.now - s)
|
|
120
|
-
|
|
121
|
-
$M.synchronize do
|
|
122
|
-
$TOTAL[0] += result[0]
|
|
123
|
-
$TOTAL[1] += 1
|
|
124
|
-
|
|
125
|
-
if result[0] > OPTIONS[:Timeout]
|
|
126
|
-
puts "\t\t%0.2fs: %s => %s" % result
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
$STATUS[name] ||= []
|
|
130
|
-
$STATUS[name] << :success
|
|
131
|
-
|
|
132
|
-
if $STATUS[name].include?(:failure)
|
|
133
|
-
puts "Name #{name} has had previous failures!"
|
|
134
|
-
end
|
|
135
|
-
end
|
|
136
|
-
end
|
|
137
|
-
|
|
138
|
-
puts "Starting threads..."
|
|
139
|
-
Thread.abort_on_exception = true
|
|
140
|
-
|
|
141
|
-
OPTIONS[:Threads].times do
|
|
142
|
-
th = Thread.new do
|
|
143
|
-
OPTIONS[:Requests].times do
|
|
144
|
-
resolve_domain
|
|
145
|
-
end
|
|
146
|
-
end
|
|
147
|
-
|
|
148
|
-
$TG.add th
|
|
149
|
-
end
|
|
150
|
-
|
|
151
|
-
$TG.list.each { |thr| thr.join }
|
|
152
|
-
|
|
153
|
-
$STATUS.each do |name, results|
|
|
154
|
-
if results.include?(:failure)
|
|
155
|
-
puts "Name #{name} failed at least once!"
|
|
156
|
-
end
|
|
157
|
-
end
|
|
158
|
-
|
|
159
|
-
puts
|
|
160
|
-
puts "Requests: #{$TOTAL[1]} Average time: #{$TOTAL[0] / $TOTAL[1]}"
|
data/lib/rubydns/chunked.rb
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
# Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
|
2
|
-
#
|
|
3
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
-
# of this software and associated documentation files (the "Software"), to deal
|
|
5
|
-
# in the Software without restriction, including without limitation the rights
|
|
6
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
-
# copies of the Software, and to permit persons to whom the Software is
|
|
8
|
-
# furnished to do so, subject to the following conditions:
|
|
9
|
-
#
|
|
10
|
-
# The above copyright notice and this permission notice shall be included in
|
|
11
|
-
# all copies or substantial portions of the Software.
|
|
12
|
-
#
|
|
13
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
-
# THE SOFTWARE.
|
|
20
|
-
|
|
21
|
-
module RubyDNS
|
|
22
|
-
# Produces an array of arrays of binary data with each sub-array a maximum of chunk_size bytes.
|
|
23
|
-
def self.chunked(string, chunk_size = 255)
|
|
24
|
-
chunks = []
|
|
25
|
-
|
|
26
|
-
offset = 0
|
|
27
|
-
while offset < string.bytesize
|
|
28
|
-
chunks << string.byteslice(offset, chunk_size)
|
|
29
|
-
offset += chunk_size
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
return chunks
|
|
33
|
-
end
|
|
34
|
-
end
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
# Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
|
2
|
-
#
|
|
3
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
-
# of this software and associated documentation files (the "Software"), to deal
|
|
5
|
-
# in the Software without restriction, including without limitation the rights
|
|
6
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
-
# copies of the Software, and to permit persons to whom the Software is
|
|
8
|
-
# furnished to do so, subject to the following conditions:
|
|
9
|
-
#
|
|
10
|
-
# The above copyright notice and this permission notice shall be included in
|
|
11
|
-
# all copies or substantial portions of the Software.
|
|
12
|
-
#
|
|
13
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
-
# THE SOFTWARE.
|
|
20
|
-
|
|
21
|
-
require 'stringio'
|
|
22
|
-
|
|
23
|
-
class String
|
|
24
|
-
def hexdump
|
|
25
|
-
i = 1
|
|
26
|
-
out = StringIO.new
|
|
27
|
-
|
|
28
|
-
out.puts "Size: #{self.bytesize}"
|
|
29
|
-
while (self.length > 16*(i-1))
|
|
30
|
-
a = self.slice(16*(i-1)..(16*i)-1)
|
|
31
|
-
out.printf("%06x: %4.4x %4.4x %4.4x %4.4x %4.4x %4.4x %4.4x %4.4x ", (i-1)*16, *a.unpack("n16"))
|
|
32
|
-
out.printf("|%s|\n", a.tr("^\040-\176","."))
|
|
33
|
-
i += 1
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
return out.string
|
|
37
|
-
end
|
|
38
|
-
end
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
# Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
|
2
|
-
#
|
|
3
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
-
# of this software and associated documentation files (the "Software"), to deal
|
|
5
|
-
# in the Software without restriction, including without limitation the rights
|
|
6
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
-
# copies of the Software, and to permit persons to whom the Software is
|
|
8
|
-
# furnished to do so, subject to the following conditions:
|
|
9
|
-
#
|
|
10
|
-
# The above copyright notice and this permission notice shall be included in
|
|
11
|
-
# all copies or substantial portions of the Software.
|
|
12
|
-
#
|
|
13
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
-
# THE SOFTWARE.
|
|
20
|
-
|
|
21
|
-
module RubyDNS
|
|
22
|
-
# Logs an exception nicely.
|
|
23
|
-
def self.log_exception(logger, exception)
|
|
24
|
-
logger.error "#{exception.class}: #{exception.message}"
|
|
25
|
-
if exception.backtrace
|
|
26
|
-
Array(exception.backtrace).each { |at| logger.error at }
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
end
|
|
30
|
-
end
|
|
@@ -1,53 +0,0 @@
|
|
|
1
|
-
# Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
|
2
|
-
#
|
|
3
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
-
# of this software and associated documentation files (the "Software"), to deal
|
|
5
|
-
# in the Software without restriction, including without limitation the rights
|
|
6
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
-
# copies of the Software, and to permit persons to whom the Software is
|
|
8
|
-
# furnished to do so, subject to the following conditions:
|
|
9
|
-
#
|
|
10
|
-
# The above copyright notice and this permission notice shall be included in
|
|
11
|
-
# all copies or substantial portions of the Software.
|
|
12
|
-
#
|
|
13
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
-
# THE SOFTWARE.
|
|
20
|
-
|
|
21
|
-
require 'resolv'
|
|
22
|
-
|
|
23
|
-
class Resolv
|
|
24
|
-
class DNS
|
|
25
|
-
class Message
|
|
26
|
-
# Merge the given message with this message. A number of heuristics are
|
|
27
|
-
# applied in order to ensure that the result makes sense. For example,
|
|
28
|
-
# If the current message is not recursive but is being merged with a
|
|
29
|
-
# message that was recursive, this bit is maintained. If either message
|
|
30
|
-
# is authoritive, then the result is also authoritive.
|
|
31
|
-
#
|
|
32
|
-
# Modifies the current message in place.
|
|
33
|
-
def merge! (other)
|
|
34
|
-
# Authoritive Answer
|
|
35
|
-
@aa = @aa && other.aa
|
|
36
|
-
|
|
37
|
-
@question += other.question
|
|
38
|
-
@answer += other.answer
|
|
39
|
-
@authority += other.authority
|
|
40
|
-
@additional += other.additional
|
|
41
|
-
|
|
42
|
-
# Recursion Available
|
|
43
|
-
@ra = @ra || other.ra
|
|
44
|
-
|
|
45
|
-
# Result Code (Error Code)
|
|
46
|
-
@rcode = other.rcode unless other.rcode == 0
|
|
47
|
-
|
|
48
|
-
# Recursion Desired
|
|
49
|
-
@rd = @rd || other.rd
|
|
50
|
-
end
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
end
|
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
# Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
|
2
|
-
#
|
|
3
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
-
# of this software and associated documentation files (the "Software"), to deal
|
|
5
|
-
# in the Software without restriction, including without limitation the rights
|
|
6
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
-
# copies of the Software, and to permit persons to whom the Software is
|
|
8
|
-
# furnished to do so, subject to the following conditions:
|
|
9
|
-
#
|
|
10
|
-
# The above copyright notice and this permission notice shall be included in
|
|
11
|
-
# all copies or substantial portions of the Software.
|
|
12
|
-
#
|
|
13
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
-
# THE SOFTWARE.
|
|
20
|
-
|
|
21
|
-
require 'stringio'
|
|
22
|
-
|
|
23
|
-
class String
|
|
24
|
-
def bytesize
|
|
25
|
-
size
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
def byteslice(*args)
|
|
29
|
-
self[*args]
|
|
30
|
-
end
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
module RubyDNS
|
|
34
|
-
BinaryStringIO = StringIO
|
|
35
|
-
end
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
# Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
|
2
|
-
#
|
|
3
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
-
# of this software and associated documentation files (the "Software"), to deal
|
|
5
|
-
# in the Software without restriction, including without limitation the rights
|
|
6
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
-
# copies of the Software, and to permit persons to whom the Software is
|
|
8
|
-
# furnished to do so, subject to the following conditions:
|
|
9
|
-
#
|
|
10
|
-
# The above copyright notice and this permission notice shall be included in
|
|
11
|
-
# all copies or substantial portions of the Software.
|
|
12
|
-
#
|
|
13
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
-
# THE SOFTWARE.
|
|
20
|
-
|
|
21
|
-
require 'stringio'
|
|
22
|
-
require 'rubydns/extensions/string-1.9.3'
|
|
23
|
-
|
|
24
|
-
class String
|
|
25
|
-
def byteslice(*args)
|
|
26
|
-
self.dup.force_encoding("BINARY").slice(*args)
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
# Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
|
2
|
-
#
|
|
3
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
-
# of this software and associated documentation files (the "Software"), to deal
|
|
5
|
-
# in the Software without restriction, including without limitation the rights
|
|
6
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
-
# copies of the Software, and to permit persons to whom the Software is
|
|
8
|
-
# furnished to do so, subject to the following conditions:
|
|
9
|
-
#
|
|
10
|
-
# The above copyright notice and this permission notice shall be included in
|
|
11
|
-
# all copies or substantial portions of the Software.
|
|
12
|
-
#
|
|
13
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
-
# THE SOFTWARE.
|
|
20
|
-
|
|
21
|
-
require 'stringio'
|
|
22
|
-
|
|
23
|
-
module RubyDNS
|
|
24
|
-
class BinaryStringIO < StringIO
|
|
25
|
-
def initialize
|
|
26
|
-
super
|
|
27
|
-
|
|
28
|
-
set_encoding("BINARY")
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
end
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
# Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
|
2
|
-
#
|
|
3
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
-
# of this software and associated documentation files (the "Software"), to deal
|
|
5
|
-
# in the Software without restriction, including without limitation the rights
|
|
6
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
-
# copies of the Software, and to permit persons to whom the Software is
|
|
8
|
-
# furnished to do so, subject to the following conditions:
|
|
9
|
-
#
|
|
10
|
-
# The above copyright notice and this permission notice shall be included in
|
|
11
|
-
# all copies or substantial portions of the Software.
|
|
12
|
-
#
|
|
13
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
-
# THE SOFTWARE.
|
|
20
|
-
|
|
21
|
-
require 'rubydns/chunked'
|
|
22
|
-
|
|
23
|
-
class String
|
|
24
|
-
def chunked(chunk_size = 255)
|
|
25
|
-
RubyDNS::chunked(self)
|
|
26
|
-
end
|
|
27
|
-
end
|
data/lib/rubydns/handler.rb
DELETED
|
@@ -1,140 +0,0 @@
|
|
|
1
|
-
# Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
|
2
|
-
#
|
|
3
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
-
# of this software and associated documentation files (the "Software"), to deal
|
|
5
|
-
# in the Software without restriction, including without limitation the rights
|
|
6
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
-
# copies of the Software, and to permit persons to whom the Software is
|
|
8
|
-
# furnished to do so, subject to the following conditions:
|
|
9
|
-
#
|
|
10
|
-
# The above copyright notice and this permission notice shall be included in
|
|
11
|
-
# all copies or substantial portions of the Software.
|
|
12
|
-
#
|
|
13
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
-
# THE SOFTWARE.
|
|
20
|
-
|
|
21
|
-
require 'rubydns/message'
|
|
22
|
-
|
|
23
|
-
module RubyDNS
|
|
24
|
-
|
|
25
|
-
def self.get_peer_details(connection)
|
|
26
|
-
Socket.unpack_sockaddr_in(connection.get_peername)[1]
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
module UDPHandler
|
|
30
|
-
def initialize(server)
|
|
31
|
-
@server = server
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def self.process(server, data, options = {}, &block)
|
|
35
|
-
server.logger.debug "Receiving incoming query (#{data.bytesize} bytes)..."
|
|
36
|
-
query = nil
|
|
37
|
-
|
|
38
|
-
begin
|
|
39
|
-
query = RubyDNS::decode_message(data)
|
|
40
|
-
|
|
41
|
-
return server.process_query(query, options, &block)
|
|
42
|
-
rescue
|
|
43
|
-
server.logger.error "Error processing request!"
|
|
44
|
-
server.logger.error "#{$!.class}: #{$!.message}"
|
|
45
|
-
|
|
46
|
-
$!.backtrace.each { |at| server.logger.error at }
|
|
47
|
-
|
|
48
|
-
# Encoding may fail, so we need to handle this particular case:
|
|
49
|
-
server_failure = Resolv::DNS::Message::new(query ? query.id : 0)
|
|
50
|
-
server_failure.qr = 1
|
|
51
|
-
server_failure.opcode = query ? query.opcode : 0
|
|
52
|
-
server_failure.aa = 1
|
|
53
|
-
server_failure.rd = 0
|
|
54
|
-
server_failure.ra = 0
|
|
55
|
-
|
|
56
|
-
server_failure.rcode = Resolv::DNS::RCode::ServFail
|
|
57
|
-
|
|
58
|
-
# We can't do anything at this point...
|
|
59
|
-
yield server_failure
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
def receive_data(data)
|
|
64
|
-
options = {:peer => RubyDNS::get_peer_details(self)}
|
|
65
|
-
|
|
66
|
-
UDPHandler.process(@server, data, options) do |answer|
|
|
67
|
-
data = answer.encode
|
|
68
|
-
|
|
69
|
-
@server.logger.debug "Writing response to client (#{data.bytesize} bytes) via UDP..."
|
|
70
|
-
|
|
71
|
-
if data.bytesize > UDP_TRUNCATION_SIZE
|
|
72
|
-
@server.logger.warn "Response via UDP was larger than #{UDP_TRUNCATION_SIZE}!"
|
|
73
|
-
|
|
74
|
-
# Reencode data with truncation flag marked as true:
|
|
75
|
-
truncation_error = Resolv::DNS::Message.new(answer.id)
|
|
76
|
-
truncation_error.tc = 1
|
|
77
|
-
|
|
78
|
-
data = truncation_error.encode
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
self.send_data(data)
|
|
82
|
-
end
|
|
83
|
-
end
|
|
84
|
-
end
|
|
85
|
-
|
|
86
|
-
class LengthError < StandardError
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
module TCPHandler
|
|
90
|
-
def initialize(server)
|
|
91
|
-
@server = server
|
|
92
|
-
|
|
93
|
-
@buffer = BinaryStringIO.new
|
|
94
|
-
|
|
95
|
-
@length = nil
|
|
96
|
-
@processed = 0
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
def receive_data(data)
|
|
100
|
-
# We buffer data until we've received the entire packet:
|
|
101
|
-
@buffer.write(data)
|
|
102
|
-
|
|
103
|
-
# Message includes a 16-bit length field.. we need to see if we have received it yet:
|
|
104
|
-
if @length == nil
|
|
105
|
-
if (@buffer.size - @processed) < 2
|
|
106
|
-
raise LengthError.new("Malformed message smaller than two bytes received")
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
# Grab the length field:
|
|
110
|
-
@length = @buffer.string.byteslice(@processed, 2).unpack('n')[0]
|
|
111
|
-
@processed += 2
|
|
112
|
-
end
|
|
113
|
-
|
|
114
|
-
if (@buffer.size - @processed) >= @length
|
|
115
|
-
data = @buffer.string.byteslice(@processed, @length)
|
|
116
|
-
|
|
117
|
-
options = {:peer => RubyDNS::get_peer_details(self)}
|
|
118
|
-
|
|
119
|
-
UDPHandler.process(@server, data, options) do |answer|
|
|
120
|
-
data = answer.encode
|
|
121
|
-
|
|
122
|
-
@server.logger.debug "Writing response to client (#{data.bytesize} bytes) via TCP..."
|
|
123
|
-
|
|
124
|
-
self.send_data([data.bytesize].pack('n'))
|
|
125
|
-
self.send_data(data)
|
|
126
|
-
end
|
|
127
|
-
|
|
128
|
-
@processed += @length
|
|
129
|
-
@length = nil
|
|
130
|
-
end
|
|
131
|
-
end
|
|
132
|
-
|
|
133
|
-
def unbind
|
|
134
|
-
if @processed != @buffer.size
|
|
135
|
-
raise LengthError.new("Unprocessed data remaining (#{@buffer.size - @processed} bytes unprocessed)")
|
|
136
|
-
end
|
|
137
|
-
end
|
|
138
|
-
end
|
|
139
|
-
|
|
140
|
-
end
|
data/lib/rubydns/message.rb
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
# Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
|
2
|
-
#
|
|
3
|
-
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4
|
-
# of this software and associated documentation files (the "Software"), to deal
|
|
5
|
-
# in the Software without restriction, including without limitation the rights
|
|
6
|
-
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7
|
-
# copies of the Software, and to permit persons to whom the Software is
|
|
8
|
-
# furnished to do so, subject to the following conditions:
|
|
9
|
-
#
|
|
10
|
-
# The above copyright notice and this permission notice shall be included in
|
|
11
|
-
# all copies or substantial portions of the Software.
|
|
12
|
-
#
|
|
13
|
-
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14
|
-
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15
|
-
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
|
-
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17
|
-
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
18
|
-
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
19
|
-
# THE SOFTWARE.
|
|
20
|
-
|
|
21
|
-
require 'eventmachine'
|
|
22
|
-
require 'stringio'
|
|
23
|
-
require 'resolv'
|
|
24
|
-
|
|
25
|
-
require 'rubydns/extensions/resolv'
|
|
26
|
-
|
|
27
|
-
module RubyDNS
|
|
28
|
-
UDP_TRUNCATION_SIZE = 512
|
|
29
|
-
|
|
30
|
-
# The DNS message container.
|
|
31
|
-
Message = Resolv::DNS::Message
|
|
32
|
-
|
|
33
|
-
def self.decode_message(data)
|
|
34
|
-
if data.respond_to? :force_encoding
|
|
35
|
-
data.force_encoding("BINARY")
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
# This may throw Resolv::DNS::DecodeError.
|
|
39
|
-
Message.decode(data)
|
|
40
|
-
end
|
|
41
|
-
end
|