lib/rubydns/server.rb in rubydns-0.3.4 vs lib/rubydns/server.rb in rubydns-0.4.0

- old
+ new

@@ -24,15 +24,14 @@ # This class provides the core of the DSL. It contains a list of rules which # are used to match against incoming DNS questions. These rules are used to # generate responses which are either DNS resource records or failures. class Server - # Instantiate a server with a block # # server = Server.new do - # match(/server.mydomain.com/, :A) do |transaction| + # match(/server.mydomain.com/, IN::A) do |transaction| # transaction.respond!("1.2.3.4") # end # end # def initialize(&block) @@ -53,26 +52,14 @@ # a String or a Regex instance. Optionally, a second argument can be # provided which is either a String, Symbol or Array of resource record # types which the rule matches against. # # match("www.google.com") - # match("gmail.com", :MX) - # match(/g?mail.(com|org|net)/, [:MX, :A]) + # match("gmail.com", IN::MX) + # match(/g?mail.(com|org|net)/, [IN::MX, IN::A]) # - def match (*pattern, &block) - # Normalize pattern - case pattern[1] - when nil - # Do nothing - when String - pattern[1] = pattern[1].upcase - when Symbol - pattern[1] = pattern[1].to_s.upcase - when Array - pattern[1] = pattern[1].collect { |v| v.to_s.upcase } - end - + def match(*pattern, &block) @rules << [pattern, Proc.new(&block)] end # Register a named event which may be invoked later using #fire # on(:start) do |server| @@ -106,26 +93,26 @@ # Give a name and a record type, try to match a rule and use it for # processing the given arguments. # # If a rule returns false, it is considered that the rule failed and # futher matching is carried out. - def process(name, record_type, *args) - @logger.debug "Searching for #{name} #{record_type}" + def process(name, resource_class, *args) + @logger.debug "Searching for #{name} #{resource_class.name}" @rules.each do |rule| @logger.debug "Checking rule #{rule[0].inspect}..." pattern = rule[0] - # Match failed against record_type? + # Match failed against resource_class? case pattern[1] - when String - next unless pattern[1] == record_type - @logger.debug "Resource type #{record_type} matched" + when Class + next unless pattern[1] == resource_class + @logger.debug "Resource class #{resource_class.name} matched" when Array - next unless pattern[1].include?(record_type) - @logger.debug "Resource type #{record_type} matched #{pattern[1].inspect}" + next unless pattern[1].include?(resource_class) + @logger.debug "Resource class #{resource_class} matched #{pattern[1].inspect}" end # Match succeeded against name? case pattern[0] when Regexp @@ -165,19 +152,17 @@ end if @otherwise @otherwise.call(*args) else - @logger.warn "Failed to handle #{name} #{record_type}!" + @logger.warn "Failed to handle #{name} #{resource_class.name}!" end end # Process an incoming DNS message. Returns a serialized message to be # sent back to the client. - def receive_data(data, &block) - query = Resolv::DNS::Message::decode(data) - + def process_query(query, &block) # Setup answer answer = Resolv::DNS::Message::new(query.id) answer.qr = 1 # 0 = Query, 1 = Response answer.opcode = query.opcode # Type of Query; copy from query answer.aa = 1 # Is this an authoritative response: 0 = No, 1 = Yes @@ -200,10 +185,10 @@ end if block_given? yield answer else - answer.encode + return answer end end end end