async-dns 0.10.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.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +4 -0
  3. data/.travis.yml +18 -0
  4. data/Gemfile +14 -0
  5. data/README.md +144 -0
  6. data/Rakefile +32 -0
  7. data/async-dns.gemspec +31 -0
  8. data/lib/async/dns.rb +36 -0
  9. data/lib/async/dns/chunked.rb +34 -0
  10. data/lib/async/dns/extensions/resolv.rb +136 -0
  11. data/lib/async/dns/extensions/string.rb +28 -0
  12. data/lib/async/dns/handler.rb +229 -0
  13. data/lib/async/dns/logger.rb +31 -0
  14. data/lib/async/dns/message.rb +75 -0
  15. data/lib/async/dns/replace.rb +54 -0
  16. data/lib/async/dns/resolver.rb +280 -0
  17. data/lib/async/dns/server.rb +154 -0
  18. data/lib/async/dns/system.rb +146 -0
  19. data/lib/async/dns/transaction.rb +202 -0
  20. data/lib/async/dns/transport.rb +78 -0
  21. data/lib/async/dns/version.rb +25 -0
  22. data/spec/async/dns/handler_spec.rb +58 -0
  23. data/spec/async/dns/hosts.txt +2 -0
  24. data/spec/async/dns/ipv6_spec.rb +78 -0
  25. data/spec/async/dns/message_spec.rb +56 -0
  26. data/spec/async/dns/origin_spec.rb +106 -0
  27. data/spec/async/dns/replace_spec.rb +44 -0
  28. data/spec/async/dns/resolver_performance_spec.rb +110 -0
  29. data/spec/async/dns/resolver_spec.rb +151 -0
  30. data/spec/async/dns/server/bind9/generate-local.rb +25 -0
  31. data/spec/async/dns/server/bind9/local.zone +5014 -0
  32. data/spec/async/dns/server/bind9/named.conf +14 -0
  33. data/spec/async/dns/server/bind9/named.run +0 -0
  34. data/spec/async/dns/server/million.rb +85 -0
  35. data/spec/async/dns/server_performance_spec.rb +138 -0
  36. data/spec/async/dns/slow_server_spec.rb +97 -0
  37. data/spec/async/dns/socket_spec.rb +70 -0
  38. data/spec/async/dns/system_spec.rb +57 -0
  39. data/spec/async/dns/transaction_spec.rb +140 -0
  40. data/spec/async/dns/truncation_spec.rb +61 -0
  41. data/spec/spec_helper.rb +60 -0
  42. metadata +175 -0
@@ -0,0 +1,78 @@
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 'ipaddr'
23
+
24
+ require_relative 'message'
25
+
26
+ module Async::DNS
27
+ def self.address_family(host)
28
+ return IPAddr.new(host).family
29
+ end
30
+
31
+ # A helper class for processing incoming network data.
32
+ class BinaryStringIO < StringIO
33
+ def initialize
34
+ super
35
+
36
+ set_encoding("BINARY")
37
+ end
38
+ end
39
+
40
+ module StreamTransport
41
+ def self.read_chunk(socket)
42
+ # The data buffer:
43
+ buffer = BinaryStringIO.new
44
+
45
+ # First we need to read in the length of the packet
46
+ while buffer.size < 2
47
+ buffer.write socket.read(1)
48
+ end
49
+
50
+ # Read in the length, the first two bytes:
51
+ length = buffer.string.byteslice(0, 2).unpack('n')[0]
52
+
53
+ # Read data until we have the amount specified:
54
+ while (buffer.size - 2) < length
55
+ required = (2 + length) - buffer.size
56
+
57
+ # Read precisely the required amount:
58
+ buffer.write socket.read(required)
59
+ end
60
+
61
+ return buffer.string.byteslice(2, length)
62
+ end
63
+
64
+ def self.write_message(socket, message)
65
+ write_chunk(socket, message.encode)
66
+ end
67
+
68
+ def self.write_chunk(socket, output_data)
69
+ size_data = [output_data.bytesize].pack('n')
70
+
71
+ # TODO: Validate/check for data written correctly
72
+ count = socket.write(size_data)
73
+ count = socket.write(output_data)
74
+
75
+ return output_data.bytesize
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,25 @@
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 Async
22
+ module DNS
23
+ VERSION = '0.10.0'
24
+ end
25
+ end
@@ -0,0 +1,58 @@
1
+ # Copyright, 2017, 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 'async/dns'
22
+ require 'async/dns/system'
23
+
24
+ describe Async::DNS::TCPServerHandler do
25
+ let(:server) {Async::DNS::Server.new}
26
+ let(:host) {'127.0.0.1'}
27
+ let(:port) {6665}
28
+
29
+ subject {described_class.new(server, host, port)}
30
+
31
+ it "can rebind port" do
32
+ 2.times do
33
+ socket = subject.send(:make_socket)
34
+ expect(socket).to_not be_closed
35
+
36
+ socket.close
37
+ expect(socket).to be_closed
38
+ end
39
+ end
40
+ end
41
+
42
+ describe Async::DNS::UDPServerHandler do
43
+ let(:server) {Async::DNS::Server.new}
44
+ let(:host) {'127.0.0.1'}
45
+ let(:port) {6665}
46
+
47
+ subject {described_class.new(server, host, port)}
48
+
49
+ it "can rebind port" do
50
+ 2.times do
51
+ socket = subject.send(:make_socket)
52
+ expect(socket).to_not be_closed
53
+
54
+ socket.close
55
+ expect(socket).to be_closed
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,2 @@
1
+ # A testing host:
2
+ 1.2.3.4 testing apples
@@ -0,0 +1,78 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Copyright, 2014, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'async/dns'
24
+ require 'async/dns/system'
25
+
26
+ module Async::DNS::IPv6Spec
27
+ IN = Resolv::DNS::Resource::IN
28
+
29
+ class TestServer < Async::DNS::Server
30
+ def process(name, resource_class, transaction)
31
+ @resolver ||= Async::DNS::Resolver.new([[:udp, "8.8.8.8", 53], [:tcp, "8.8.8.8", 53]])
32
+
33
+ transaction.passthrough!(@resolver)
34
+ end
35
+ end
36
+
37
+ describe Async::DNS::TCPSocketHandler do
38
+ let(:server_interfaces) {[[:tcp, '::', 2004]]}
39
+ let(:server) {TestServer.new(listen: server_interfaces)}
40
+
41
+ include_context "reactor"
42
+
43
+ it "should connect to the server using TCP via IPv6" do
44
+ task = server.run
45
+
46
+ resolver = Async::DNS::Resolver.new([[:tcp, '::1', 2004]])
47
+
48
+ response = resolver.query('google.com')
49
+
50
+ expect(response.class).to be == Async::DNS::Message
51
+ expect(response.rcode).to be == 0
52
+ expect(response.answer).to_not be_empty
53
+
54
+ task.stop
55
+ end
56
+ end
57
+
58
+ describe Async::DNS::UDPSocketHandler do
59
+ let(:server_interfaces) {[[:udp, '::', 2006]]}
60
+ let(:server) {TestServer.new(listen: server_interfaces)}
61
+
62
+ include_context "reactor"
63
+
64
+ it "should connect to the server using UDP via IPv6" do
65
+ task = server.run
66
+
67
+ resolver = Async::DNS::Resolver.new([[:udp, '::1', 2006]])
68
+
69
+ response = resolver.query('google.com')
70
+
71
+ expect(response.class).to be == Async::DNS::Message
72
+ expect(response.rcode).to be == 0
73
+ expect(response.answer).to_not be_empty
74
+
75
+ task.stop
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,56 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'async/dns'
24
+ require 'base64'
25
+
26
+ module Async::DNS::MessageSpec
27
+ describe Async::DNS::Message do
28
+ it "should be decoded correctly" do
29
+ data = Base64.decode64(<<-EOF)
30
+ HQCBgAABAAgAAAABA3d3dwV5YWhvbwNjb20AAAEAAcAMAAUAAQAAASwADwZm
31
+ ZC1mcDMDd2cxAWLAEMArAAUAAQAAASwACQZkcy1mcDPAMsBGAAUAAQAAADwA
32
+ FQ5kcy1hbnktZnAzLWxmYgN3YTHANsBbAAUAAQAAASwAEg9kcy1hbnktZnAz
33
+ LXJlYWzAasB8AAEAAQAAADwABGKK/B7AfAABAAEAAAA8AARii7SVwHwAAQAB
34
+ AAAAPAAEYou3GMB8AAEAAQAAADwABGKK/W0AACkQAAAAAAAAAA==
35
+ EOF
36
+
37
+ message = Async::DNS::decode_message(data)
38
+ expect(message.class).to be == Async::DNS::Message
39
+ expect(message.id).to be == 0x1d00
40
+
41
+ expect(message.question.count).to be == 1
42
+ expect(message.answer.count).to be == 8
43
+ expect(message.authority.count).to be == 0
44
+ expect(message.additional.count).to be == 1
45
+ end
46
+
47
+ it "should fail to decode due to bad AAAA length" do
48
+ data = Base64.decode64(<<-EOF)
49
+ 6p6BgAABAAEAAAABCGJhaWNhaWNuA2NvbQAAHAABwAwAHAABAAABHgAEMhd7
50
+ dwAAKRAAAAAAAAAA
51
+ EOF
52
+
53
+ expect{Async::DNS::decode_message(data)}.to raise_error(Async::DNS::DecodeError)
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,106 @@
1
+ #!/usr/bin/env rspec
2
+
3
+ # Copyright, 2014, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'async/dns'
24
+
25
+ module Async::DNS::OriginSpec
26
+ describe Async::DNS::Resolver do
27
+ it "should generate fully qualified domain name with specified origin" do
28
+ resolver = Async::DNS::Resolver.new([], origin: "foo.bar.")
29
+
30
+ fully_qualified_name = resolver.fully_qualified_name("baz")
31
+
32
+ expect(fully_qualified_name).to be_absolute
33
+ expect(fully_qualified_name.to_s).to be == "baz.foo.bar."
34
+ end
35
+ end
36
+
37
+ describe Resolv::DNS::Name do
38
+ let(:name) {Resolv::DNS::Name.create("foo.bar")}
39
+
40
+ it "should be relative" do
41
+ expect(name).to_not be_absolute
42
+ expect(name.to_s).to be == "foo.bar"
43
+ end
44
+
45
+ it "should add the specified origin" do
46
+ fully_qualified_name = name.with_origin("org")
47
+
48
+ expect(fully_qualified_name.to_a.size).to be 3
49
+ expect(fully_qualified_name).to be_absolute
50
+ expect(fully_qualified_name.to_s).to be == "foo.bar.org."
51
+ end
52
+
53
+ it "should handle nil origin as absolute" do
54
+ fully_qualified_name = name.with_origin(nil)
55
+
56
+ expect(fully_qualified_name.to_a.size).to be 2
57
+ expect(fully_qualified_name).to be_absolute
58
+ expect(fully_qualified_name.to_s).to be == "foo.bar."
59
+ end
60
+
61
+ it "should handle empty origin as absolute" do
62
+ fully_qualified_name = name.with_origin('')
63
+
64
+ expect(fully_qualified_name.to_a.size).to be 2
65
+ expect(fully_qualified_name).to be_absolute
66
+ expect(fully_qualified_name.to_s).to be == "foo.bar."
67
+ end
68
+ end
69
+
70
+ describe Resolv::DNS::Name do
71
+ let(:name) {Resolv::DNS::Name.create("foo.bar.")}
72
+
73
+ it "should be absolute" do
74
+ expect(name).to be_absolute
75
+ expect(name.to_s).to be == "foo.bar."
76
+ end
77
+
78
+ it "should remove the specified origin" do
79
+ relative_name = name.without_origin("bar")
80
+
81
+ expect(relative_name.to_a.size).to be 1
82
+ expect(relative_name).to_not be_absolute
83
+ expect(relative_name.to_s).to be == "foo"
84
+ end
85
+
86
+ it "should not remove nil origin but become relative" do
87
+ relative_name = name.without_origin(nil)
88
+
89
+ expect(relative_name.to_a.size).to be 2
90
+ expect(relative_name).to_not be_absolute
91
+ expect(relative_name.to_s).to be == "foo.bar"
92
+ end
93
+
94
+ it "should not remove empty string origin but become relative" do
95
+ relative_name = name.without_origin('')
96
+
97
+ expect(relative_name.to_a.size).to be 2
98
+ expect(relative_name).to_not be_absolute
99
+ expect(relative_name.to_s).to be == "foo.bar"
100
+ end
101
+
102
+ it "should not raise an exception when origin isn't valid" do
103
+ expect{name.without_origin('bob')}.to raise_exception(Resolv::DNS::OriginError)
104
+ end
105
+ end
106
+ end
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env rspec
2
+
3
+ # Copyright, 2015, by Samuel G. D. Williams. <http://www.codeotaku.com>
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'async/dns'
24
+ require 'async/dns/replace'
25
+
26
+ module Async::DNS::ReplaceSpec
27
+ describe Async::DNS::Replace do
28
+ include_context "reactor"
29
+
30
+ let(:default_resolver) {Async::DNS::Resolver.new([[:udp, "8.8.8.8", 53], [:tcp, "8.8.8.8", 53]])}
31
+
32
+ after(:all) do
33
+ Async::DNS::Replace.resolver = nil
34
+ end
35
+
36
+ it "should replace TCPSocket hostname lookup" do
37
+ Async::DNS::Replace.resolver = default_resolver
38
+
39
+ expect(default_resolver).to receive(:addresses_for).with('www.google.com').and_call_original
40
+
41
+ TCPSocket.new('www.google.com', 80)
42
+ end
43
+ end
44
+ end