Module: RubyDNS

Defined in:
lib/rubydns/chunked.rb,
lib/rubydns.rb,
lib/rubydns/server.rb,
lib/rubydns/system.rb,
lib/rubydns/handler.rb,
lib/rubydns/message.rb,
lib/rubydns/version.rb,
lib/rubydns/resolver.rb,
lib/rubydns/transaction.rb,
lib/rubydns/extensions/string-1.8.rb,
lib/rubydns/extensions/string-1.9.2.rb,
lib/rubydns/extensions/string-1.9.3.rb

Overview

Copyright © 2009, 2011 Samuel G. D. Williams. <www.oriontransfer.co.nz>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Defined Under Namespace

Modules: System, TCPHandler, UDPHandler, VERSION Classes: BinaryStringIO, InvalidProtocolError, LengthError, ResolutionFailure, Resolver, Server, Transaction

Constant Summary collapse

UDP_TRUNCATION_SIZE =
512
Message =

The DNS message container.

Resolv::DNS::Message

Class Method Summary collapse

Class Method Details

.chunked(string, chunk_size = 255) ⇒ Object

Produces an array of arrays of binary data with each sub-array a maximum of chunk_size bytes.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rubydns/chunked.rb', line 23

def self.chunked(string, chunk_size = 255)
	chunks = []
	
	offset = 0
	while offset < string.bytesize
		chunks << string.byteslice(offset, chunk_size)
		offset += chunk_size
	end
	
	return chunks
end

.decode_message(data) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/rubydns/message.rb', line 31

def self.decode_message(data)
	if data.respond_to? :force_encoding
		data.force_encoding("BINARY")
	end
	
	Message.decode(data)
end

.run_server(options = {}, &block) ⇒ Object

Run a server with the given rules. A number of options can be supplied:

:interfaces

A set of sockets or addresses as defined below.

One important feature of DNS is the port it runs on. The options[:listen] allows you to specify a set of network interfaces and ports to run the server on. This must be a list of [protocol, interface address, port].

INTERFACES = [[:udp, "0.0.0.0", 5300]]
RubyDNS::run_server(:listen => INTERFACES) do
  ...
end

You can specify already connected sockets if need be:

socket = UDPSocket.new; socket.bind("0.0.0.0", 53)
Process::Sys.setuid(server_uid)
INTERFACES = [socket]

The default interface is [[:udp, "0.0.0.0", 53]]. The server typically needs to run as root for this to work, since port 53 is privileged.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/rubydns.rb', line 69

def self.run_server (options = {}, &block)
	server = RubyDNS::Server.new(&block)
	server.logger.info "Starting RubyDNS server (v#{RubyDNS::VERSION::STRING})..."
	
	options[:listen] ||= [[:udp, "0.0.0.0", 53], [:tcp, "0.0.0.0", 53]]
	
	EventMachine.run do
		server.fire(:setup)
		
		# Setup server sockets
		options[:listen].each do |spec|
			server.logger.info "Listening on #{spec.join(':')}"
			if spec[0] == :udp
				EventMachine.open_datagram_socket(spec[1], spec[2], UDPHandler, server)
			elsif spec[0] == :tcp
				EventMachine.start_server(spec[1], spec[2], TCPHandler, server)
			end
		end
		
		server.fire(:start)
	end
	
	server.fire(:stop)
end