Class: EventMachine::DNSBL::Server

Inherits:
Connection
  • Object
show all
Defined in:
lib/eventmachine/dnsbl/server.rb

Instance Method Summary collapse

Constructor Details

#initialize(zones = MemoryZone.new) ⇒ Server

Returns a new instance of Server.



7
8
9
# File 'lib/eventmachine/dnsbl/server.rb', line 7

def initialize(zones = MemoryZone.new)
	@zones = zones
end

Instance Method Details

#add_record(qname, answer, ttl = 300) ⇒ Object



42
43
44
45
46
# File 'lib/eventmachine/dnsbl/server.rb', line 42

def add_record(qname, answer, ttl=300)
	rr = ResourceRecord.new(qname, ttl, answer)
	@rr[qname] = [] unless @rr[qname]
	@rr[qname] << rr
end

#del_all_records(qname) ⇒ Object



53
54
55
# File 'lib/eventmachine/dnsbl/server.rb', line 53

def del_all_records(qname)
	@rr.delete(qname)
end

#del_record(qname, answer) ⇒ Object



48
49
50
51
# File 'lib/eventmachine/dnsbl/server.rb', line 48

def del_record(qname, answer)
	return nil unless @rr[qname]
	@rr[qname].delete_if {|rr| rr.answer == answer}
end

#get_records(qname, qtype) ⇒ Object



57
58
59
60
# File 'lib/eventmachine/dnsbl/server.rb', line 57

def get_records(qname, qtype)
	return nil unless @rr[qname]
	@rr[qname].find_all {|rr| rr.answer.class == qtype}
end

#receive_data(data) ⇒ Object



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
# File 'lib/eventmachine/dnsbl/server.rb', line 11

def receive_data(data)
	dns = Resolv::DNS::Message.decode(data)
	dns.qr = 1
	dns.aa = 1
	return if dns.opcode != Resolv::DNS::OpCode::Query
	unless dns.question.length == 1 and dns.question[0][1] == Resolv::DNS::Resource::IN::A
		dns.rcode = Resolv::DNS::RCode::FormErr
		res = dns.encode
		send_data res
		return
	end
	query, qtype = dns.question[0]
	rs = @zones.get_records(query.to_s, qtype)
	unless rs
		dns.rcode = Resolv::DNS::RCode::NXDomain
		res = dns.encode
		send_data res
		return
	end
	# add the records them to the set of answers
	rs.each do |rr|
		dns.add_answer(query,rr.ttl,rr.answer)
	end
	# Make sure that we are authoritative for the response! Otherwise, return REFUSED
	dns.rcode = Resolv::DNS::RCode::NoError # Or Refused
	res = dns.encode
	#port, ip = Socket.unpack_sockaddr_in(get_peername)
	#puts "DEBUG: #{ip}:#{port} #{res.length}"
	send_data res
end