rubydns 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. data/.gitignore +17 -0
  2. data/.travis.yml +7 -0
  3. data/Gemfile +3 -6
  4. data/README.md +48 -44
  5. data/{rakefile.rb → Rakefile} +2 -3
  6. data/bin/rd-dns-check +1 -1
  7. data/bin/rd-resolve-test +1 -1
  8. data/lib/rubydns.rb +2 -3
  9. data/lib/rubydns/chunked.rb +1 -1
  10. data/lib/rubydns/extensions/hexdump.rb +1 -1
  11. data/lib/rubydns/extensions/logger.rb +30 -0
  12. data/lib/rubydns/extensions/resolv.rb +1 -1
  13. data/lib/rubydns/extensions/string-1.8.rb +1 -1
  14. data/lib/rubydns/extensions/string-1.9.2.rb +1 -1
  15. data/lib/rubydns/extensions/string-1.9.3.rb +1 -1
  16. data/lib/rubydns/extensions/string.rb +1 -1
  17. data/lib/rubydns/handler.rb +1 -1
  18. data/lib/rubydns/message.rb +2 -1
  19. data/lib/rubydns/resolver.rb +22 -17
  20. data/lib/rubydns/server.rb +3 -5
  21. data/lib/rubydns/system.rb +1 -1
  22. data/lib/rubydns/transaction.rb +2 -2
  23. data/lib/rubydns/version.rb +2 -8
  24. data/rubydns.gemspec +33 -0
  25. data/test/examples/dropping-dns.rb +1 -1
  26. data/test/examples/fortune-dns.rb +1 -1
  27. data/test/examples/geoip-dns.rb +1 -1
  28. data/test/examples/soa-dns.rb +1 -1
  29. data/test/examples/test-dns-1.rb +1 -1
  30. data/test/examples/test-dns-2.rb +2 -5
  31. data/test/test_daemon.rb +21 -0
  32. data/test/test_passthrough.rb +21 -0
  33. data/test/test_resolver.rb +52 -0
  34. data/test/test_rules.rb +21 -0
  35. data/test/test_slow_server.rb +21 -0
  36. data/test/test_system.rb +21 -0
  37. data/test/test_truncation.rb +21 -0
  38. metadata +38 -25
  39. data/test/examples/GeoLiteCountry.dat +0 -0
  40. data/test/examples/log/DroppingDaemon.log +0 -107
  41. data/test/examples/log/FortuneDNS.log +0 -5
  42. data/test/examples/log/GeoIPDNSDaemon.log +0 -0
  43. data/test/examples/log/TestDaemon.log +0 -340
  44. data/test/examples/run/DroppingDaemon.pid +0 -1
  45. data/test/examples/run/GeoIPDNSDaemon.pid +0 -1
  46. data/test/log/BasicTestServer.log +0 -96
  47. data/test/log/SlowServer.log +0 -72
  48. data/test/log/TestPassthroughServer.log +0 -36
  49. data/test/log/TruncatedServer.log +0 -78
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.8.7"
4
+ - "1.9.2"
5
+ - "1.9.3"
6
+ - rbx-18mode
7
+ - rbx-19mode
data/Gemfile CHANGED
@@ -1,7 +1,4 @@
1
- # A sample Gemfile
2
- source "https://rubygems.org"
3
-
4
- gem "rexec", "~> 1.5.1"
5
- gem "rake"
6
- gem "eventmachine"
1
+ source 'https://rubygems.org'
7
2
 
3
+ # Specify your gem's dependencies in rubydns.gemspec
4
+ gemspec
data/README.md CHANGED
@@ -1,55 +1,52 @@
1
- RubyDNS
2
- =======
1
+ # RubyDNS
3
2
 
4
- * Released under the MIT license.
5
- * Copyright (C) 2009, 2011 [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams/).
6
- * [![Build Status](https://secure.travis-ci.org/ioquatix/rubydns.png)](http://travis-ci.org/ioquatix/rubydns)
3
+ RubyDNS is a high-performance DNS server which can be easily integrated into other projects or used as a stand-alone daemon (via RExec). By default it uses rule-based pattern matching. Results can be hard-coded, computed, fetched from a remote DNS server or fetched from a local cache, depending on requirements.
7
4
 
8
- RubyDNS is a simple programmatic DSL (domain specific language) for configuring and running a DNS server. RubyDNS provides a daemon that runs a DNS server which can process DNS requests depending on specific policy. Rule selection is based on pattern matching, and results can be hard-coded, computed, fetched from a remote DNS server, fetched from a local cache, etc.
5
+ In addition, RubyDNS includes a high-performance asynchronous DNS resolver built on top of EventMachine. This module can be used by itself in client applications without using the full RubyDNS server stack.
9
6
 
10
- RubyDNS provides a full daemon server using RExec. You can either use the built in daemon, customize it to your needs, or specify a full daemon implementation.
7
+ For examples and documentation please see the main [project page][1].
11
8
 
12
- RubyDNS is not designed to be high-performance and uses a thread-per-request model. This is designed to make it as easy as possible to achieve concurrent performance. This is also due to the fact that many other APIs work best this way (unfortunately).
9
+ [1]: http://www.oriontransfer.co.nz/gems/rubydns
13
10
 
14
- For examples please see the main [project page][1].
11
+ [![Build Status](https://secure.travis-ci.org/ioquatix/rubydns.png)](http://travis-ci.org/ioquatix/rubydns)
15
12
 
16
- [1]: http://www.oriontransfer.co.nz/gems/rubydns
13
+ ## Installation
17
14
 
18
- Basic Example
19
- -------------
15
+ Add this line to your application's Gemfile:
20
16
 
21
- This is copied from `test/example1.rb`. It has been simplified slightly.
17
+ gem 'rubydns'
22
18
 
23
- require 'rubygems'
24
- require 'rubydns'
19
+ And then execute:
25
20
 
26
- $R = Resolv::DNS.new
21
+ $ bundle
27
22
 
28
- RubyDNS::run_server do
29
- Name = Resolv::DNS::Name
30
- IN = Resolv::DNS::Resource::IN
31
-
32
- # For this exact address record, return an IP address
33
- match("dev.mydomain.org", IN::A) do |transaction|
34
- transaction.respond!("10.0.0.80")
35
- end
23
+ Or install it yourself as:
36
24
 
37
- match(/^test([0-9]+).mydomain.org$/, IN::A) do |match_data, transaction|
38
- offset = match_data[1].to_i
25
+ $ gem install rubydns
39
26
 
40
- if offset > 0 && offset < 10
41
- logger.info "Responding with address #{"10.0.0." + (90 + offset).to_s}..."
42
- transaction.respond!("10.0.0." + (90 + offset).to_s)
43
- else
44
- logger.info "Address out of range: #{offset}!"
45
- false
27
+ ## Usage
28
+
29
+ This is copied from `test/examples/test-dns-2.rb`. It has been simplified slightly.
30
+
31
+ require 'rubydns'
32
+
33
+ Name = Resolv::DNS::Name
34
+ IN = Resolv::DNS::Resource::IN
35
+
36
+ # Use upstream DNS for name resolution.
37
+ UPSTREAM = RubyDNS::Resolver.new([[:udp, "8.8.8.8", 53], [:tcp, "8.8.8.8", 53]])
38
+
39
+ def self.run
40
+ # Start the RubyDNS server
41
+ RubyDNS::run_server(:listen => INTERFACES) do
42
+ match("test.mydomain.org", IN::A) do |transaction|
43
+ transaction.respond!("10.0.0.80")
46
44
  end
47
- end
48
45
 
49
- # Default DNS handler
50
- otherwise do |transaction|
51
- logger.info "Passing DNS request upstream..."
52
- transaction.passthrough!($R)
46
+ # Default DNS handler
47
+ otherwise do |transaction|
48
+ transaction.passthrough!(UPSTREAM)
49
+ end
53
50
  end
54
51
  end
55
52
 
@@ -59,8 +56,7 @@ After starting this server you can test it using dig:
59
56
  dig @localhost dev.mydomain.org
60
57
  dig @localhost google.com
61
58
 
62
- Compatibility
63
- -------------
59
+ ## Compatibility
64
60
 
65
61
  ### Migrating from RubyDNS 0.3.x to 0.4.x ###
66
62
 
@@ -130,16 +126,24 @@ Once you call this, the transaction won't complete until you call either `transa
130
126
 
131
127
  You can see a complete example in `test/test_slow_server.rb`.
132
128
 
133
- Todo
134
- ----
129
+ ## Contributing
130
+
131
+ 1. Fork it
132
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
133
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
134
+ 4. Push to the branch (`git push origin my-new-feature`)
135
+ 5. Create new Pull Request
136
+
137
+ ### Desired Features
135
138
 
136
139
  * Support for more features of DNS such as zone transfer.
137
140
  * Support reverse records more easily.
138
141
 
139
- License
140
- -------
142
+ ## License
143
+
144
+ Released under the MIT license.
141
145
 
142
- Copyright (c) 2010, 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
146
+ Copyright, 2009, 2012, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
143
147
 
144
148
  Permission is hereby granted, free of charge, to any person obtaining a copy
145
149
  of this software and associated documentation files (the "Software"), to deal
@@ -1,5 +1,5 @@
1
-
2
- require 'rake/testtask'
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
3
 
4
4
  Rake::TestTask.new do |t|
5
5
  t.libs << 'test'
@@ -7,4 +7,3 @@ end
7
7
 
8
8
  desc "Run tests"
9
9
  task :default => :test
10
-
data/bin/rd-dns-check CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
- # Copyright (c) 2009, 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
2
+ # Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
3
3
  #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining a copy
5
5
  # of this software and associated documentation files (the "Software"), to deal
data/bin/rd-resolve-test CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
- # Copyright (c) 2009, 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
2
+ # Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
3
3
  #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining a copy
5
5
  # of this software and associated documentation files (the "Software"), to deal
data/lib/rubydns.rb CHANGED
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2009, 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
1
+ # Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the "Software"), to deal
@@ -66,7 +66,7 @@ module RubyDNS
66
66
  #
67
67
  def self.run_server (options = {}, &block)
68
68
  server = RubyDNS::Server.new(&block)
69
- server.logger.info "Starting RubyDNS server (v#{RubyDNS::VERSION::STRING})..."
69
+ server.logger.info "Starting RubyDNS server (v#{RubyDNS::VERSION})..."
70
70
 
71
71
  options[:listen] ||= [[:udp, "0.0.0.0", 53], [:tcp, "0.0.0.0", 53]]
72
72
 
@@ -89,4 +89,3 @@ module RubyDNS
89
89
  server.fire(:stop)
90
90
  end
91
91
  end
92
-
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2009, 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
1
+ # Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the "Software"), to deal
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2009, 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
1
+ # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the "Software"), to deal
@@ -0,0 +1,30 @@
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,4 +1,4 @@
1
- # Copyright (c) 2009, 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
1
+ # Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the "Software"), to deal
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2009, 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
1
+ # Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the "Software"), to deal
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2009, 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
1
+ # Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the "Software"), to deal
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2009, 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
1
+ # Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the "Software"), to deal
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2009, 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
1
+ # Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the "Software"), to deal
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2009, 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
1
+ # Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the "Software"), to deal
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2009, 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
1
+ # Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the "Software"), to deal
@@ -35,6 +35,7 @@ module RubyDNS
35
35
  data.force_encoding("BINARY")
36
36
  end
37
37
 
38
+ # This may throw Resolv::DNS::DecodeError.
38
39
  Message.decode(data)
39
40
  end
40
41
  end
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2012 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
1
+ # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the "Software"), to deal
@@ -111,12 +111,22 @@ module RubyDNS
111
111
  end
112
112
 
113
113
  def process_response!(response)
114
- if response.tc != 0
114
+ if Exception === response
115
+ @logger.warn "[#{@message.id}] Failure while processing response #{exception}!" if @logger
116
+ RubyDNS.log_exception(@logger, response) if @logger
117
+
118
+ try_next_server!
119
+ elsif response.tc != 0
115
120
  @logger.warn "[#{@message.id}] Received truncated response!" if @logger
116
- # We hardcode this behaviour for now.
121
+
122
+ try_next_server!
123
+ elsif response.id != @message.id
124
+ @logger.warn "[#{@message.id}] Received response with incorrect message id: #{response.id}" if @request.logger
125
+
117
126
  try_next_server!
118
127
  else
119
128
  @logger.warn "[#{@message.id}] Received valid response #{response.inspect}" if @logger
129
+
120
130
  succeed response
121
131
  end
122
132
  end
@@ -175,13 +185,11 @@ module RubyDNS
175
185
  def receive_data(data)
176
186
  # Receiving response from remote DNS server...
177
187
  message = RubyDNS::decode_message(data)
178
-
188
+
179
189
  # The message id must match, and it can't be truncated:
180
- if message.id == @request.message.id
181
- @request.process_response!(message)
182
- else
183
- @request.logger.warn "[#{@request.message.id}] Received response with incorrect message id: #{message.id}" if @request.logger
184
- end
190
+ @request.process_response!(message)
191
+ rescue Resolv::DNS::DecodeError => error
192
+ @request.process_response!(error)
185
193
  end
186
194
  end
187
195
 
@@ -214,19 +222,16 @@ module RubyDNS
214
222
  end
215
223
  end
216
224
 
217
- if @buffer.size == (@length + 2)
225
+ # If we have received more data than expected, should this be an error?
226
+ if @buffer.size >= (@length + 2)
218
227
  data = @buffer.string.byteslice(2, @length)
219
228
 
220
229
  message = RubyDNS::decode_message(data)
221
230
 
222
- if message.id == @request.message.id
223
- @request.process_response!(message)
224
- else
225
- @request.logger.warn "[#{@request.message.id}] Received response with incorrect message id: #{message.id}" if @request.logger
226
- end
227
- elsif @buffer.size > (@length + 2)
228
- @request.try_next_server!
231
+ @request.process_response!(message)
229
232
  end
233
+ rescue Resolv::DNS::DecodeError => error
234
+ @request.process_response!(error)
230
235
  end
231
236
  end
232
237
  end
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2009, 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
1
+ # Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the "Software"), to deal
@@ -19,6 +19,7 @@
19
19
  # THE SOFTWARE.
20
20
 
21
21
  require 'rubydns/transaction'
22
+ require 'rubydns/extensions/logger'
22
23
 
23
24
  module RubyDNS
24
25
 
@@ -213,10 +214,7 @@ module RubyDNS
213
214
  transaction.errback do |response|
214
215
  if Exception === response
215
216
  @logger.error "Exception thrown while processing #{transaction}!"
216
- @logger.error "#{response.class}: #{response.message}"
217
- if response.backtrace
218
- Array(response.backtrace).each { |at| @logger.error at }
219
- end
217
+ RubyDNS.log_exception(@logger, response)
220
218
  else
221
219
  @logger.error "Failure while processing #{transaction}!"
222
220
  @logger.error "#{response.inspect}"
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2009, 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
1
+ # Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the "Software"), to deal
@@ -1,4 +1,4 @@
1
- # Copyright (c) 2009, 2011 Samuel G. D. Williams. <http://www.oriontransfer.co.nz>
1
+ # Copyright, 2009, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
2
  #
3
3
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
4
  # of this software and associated documentation files (the "Software"), to deal
@@ -124,7 +124,7 @@ module RubyDNS
124
124
  case response
125
125
  when RubyDNS::Message
126
126
  yield response
127
- when RubyDNS::ResolverFailure
127
+ when RubyDNS::ResolutionFailure
128
128
  failure!(:ServFail)
129
129
  else
130
130
  # This shouldn't ever happen, but if it does for some reason we shouldn't hang.