anchor.diff:  
   
   % It's a wildcard CNAME
  -resolve_best_match_with_wildcard(Message, Qname, Qtype, ClientIP, CnameChain, MatchedRecords, Zone, []) ->
  +resolve_best_match_with_wildcard(Message, Qname, Qtype, {ClientIP, ServerIP}, CnameChain, MatchedRecords, Zone, []) ->
     %erldns_log:debug("Resolving best match with wildcard"),
     TypeMatchedRecords = case Qtype of
                            ?DNS_TYPE_ANY ->
  
positive.code:       %% Ask the custom handlers for their records.
        %erldns_log:debug("Exact match for name, but no type matches in zone, try custom handlers"),
        NewRecords = lists:flatten(lists:map(custom_lookup(Qname, Qtype, MatchedRecords), erldns_handler:get_handlers())),
        resolve_exact_match(Message, Qname, Qtype, ClientIP, CnameChain, MatchedRecords, Zone, NewRecords, AuthorityRecords);
      _ ->
        %erldns_log:debug("Found exact match for name and type matches"),
        resolve_exact_match(Message, Qname, Qtype, ClientIP, CnameChain, MatchedRecords, Zone, TypeMatches, AuthorityRecords)
    end.
  
  %% There were no matches for exact name and type, so now we are looking for NS records
  %% in the exact name matches.
  resolve_exact_match(Message, _Qname, Qtype, ClientIP, CnameChain, MatchedRecords, Zone, _ExactTypeMatches = [], AuthorityRecords) ->
    %erldns_log:debug("No exact matches for name and type"),
    ReferralRecords = lists:filter(erldns_records:match_type(?DNS_TYPE_NS), MatchedRecords), % Query matched records for NS type
    resolve_no_exact_type_match(Message, Qtype, ClientIP, CnameChain, [], Zone, MatchedRecords, ReferralRecords, AuthorityRecords);
  
  %% There were exact matches of name and type.
  resolve_exact_match(Message, Qname, Qtype, ClientIP, CnameChain, _MatchedRecords, Zone, ExactTypeMatches, AuthorityRecords) ->
    %erldns_log:debug("Found exact matches of name and type. Authority records: ~p", [AuthorityRecords]),
    resolve_exact_type_match(Message, Qname, Qtype, ClientIP, CnameChain, ExactTypeMatches, Zone, AuthorityRecords).
  
  
  
  %% There was an exact type match for an NS query, however there is no SOA record for the zone.
  resolve_exact_type_match(Message, _Qname, ?DNS_TYPE_NS, ClientIP, CnameChain, MatchedRecords, Zone, []) ->
    Answer = lists:last(MatchedRecords),
    Name = Answer#dns_rr.name,
    %erldns_log:debug("Exact match type was NS, restarting query with delegated name ~p", [Name]),
    % It isn't clear what the QTYPE should be on a delegated restart. I assume an A record.
    restart_delegated_query(Message, Name, ?DNS_TYPE_A, ClientIP, CnameChain, Zone, erldns_zone_cache:in_zone(Name));
  
  %% There was an exact type match for an NS query and an SOA record.
  resolve_exact_type_match(Message, _Qname, ?DNS_TYPE_NS, _ClientIP, _CnameChain, MatchedRecords, _Zone, _AuthorityRecords) ->
    %erldns_log:debug("Authoritative for record, returning answers"),
    Message#dns_message{aa = true, rc = ?DNS_RCODE_NOERROR, answers = Message#dns_message.answers ++ MatchedRecords};
  
  %% There was an exact type match for something other than an NS record and we are authoritative because there is an SOA record.
  resolve_exact_type_match(Message, Qname, Qtype, ClientIP, CnameChain, MatchedRecords, Zone, _AuthorityRecords) ->
    % NOTE: this is a potential bug because it assumes the last record is the one to examine.
    Answer = lists:last(MatchedRecords),
    case erldns_zone_cache:get_delegations(Answer#dns_rr.name) of
      [] ->
        resolve_exact_type_match(Message, Qname, Qtype, ClientIP, CnameChain, MatchedRecords, Zone, _AuthorityRecords, _NSRecords = []);
      NSRecords  ->
        NSRecord = lists:last(NSRecords),
        case erldns_zone_cache:get_authority(Qname) of
          {ok, [SoaRecord]} ->
            case SoaRecord#dns_rr.name =:= NSRecord#dns_rr.name of
              true ->
                Message#dns_message{aa = true, rc = ?DNS_RCODE_NOERROR, answers = Message#dns_message.answers ++ MatchedRecords};
              false ->
                resolve_exact_type_match(Message, Qname, Qtype, ClientIP, CnameChain, MatchedRecords, Zone, _AuthorityRecords, NSRecords)
            end;
          {error, authority_not_found} ->
            resolve_exact_type_match(Message, Qname, Qtype, ClientIP, CnameChain, MatchedRecords, Zone, _AuthorityRecords, NSRecords)
        end
    end.
  
  %% We are authoritative and there were no NS records here.
  resolve_exact_type_match(Message, _Qname, _Qtype, _ClientIP, _CnameChain, MatchedRecords, _Zone, _AuthorityRecords, _NSRecords = []) ->
    Message#dns_message{aa = true, rc = ?DNS_RCODE_NOERROR, answers = Message#dns_message.answers ++ MatchedRecords};
  
  %% We are authoritative and there are NS records here.
  resolve_exact_type_match(Message, _Qname, Qtype, ClientIP, CnameChain, MatchedRecords, Zone, _AuthorityRecords, NSRecords) ->
    % NOTE: there are potential bugs here because it assumes the last record is the one to examine
    Answer = lists:last(MatchedRecords),
    NSRecord = lists:last(NSRecords),
  
negative.code:       %erldns_log:debug("No type matches found, using custom handlers"),
        NewRecords = lists:map(erldns_records:replace_name(Qname), lists:flatten(lists:map(custom_lookup(Qname, Qtype, MatchedRecords), erldns_handler:get_handlers()))),
        %erldns_log:debug("Records from custom handlers: ~p", [NewRecords]),
        resolve_best_match_with_wildcard(Message, Qname, Qtype, {ClientIP, ServerIP}, CnameChain, MatchedRecords, Zone, [], NewRecords);
      _ ->
        resolve_best_match_with_wildcard(Message, Qname, Qtype, {ClientIP, ServerIP}, CnameChain, MatchedRecords, Zone, [], TypeMatches)
    end;
  
  % It is a wildcard CNAME
  resolve_best_match_with_wildcard(Message, Qname, Qtype, {ClientIP, ServerIP}, CnameChain, BestMatchRecords, Zone, CnameRecords) ->
    resolve_best_match_with_wildcard_cname(Message, Qname, Qtype, {ClientIP, ServerIP}, CnameChain, BestMatchRecords, Zone, CnameRecords).
  
  % It is not a CNAME and there were no exact type matches
  resolve_best_match_with_wildcard(Message, _Qname, _Qtype, {_ClientIP, _ServerIP}, _CnameChain, _BestMatchRecords, Zone, [], []) ->
    Message#dns_message{aa = true, authority=Zone#zone.authority};
  
  % It is not a CNAME and there were exact type matches
  resolve_best_match_with_wildcard(Message, _Qname, _Qtype, {_ClientIP, _ServerIP}, _CnameChain, _BestMatchRecords, _Zone, [], TypeMatches) ->
    Message#dns_message{aa = true, answers = Message#dns_message.answers ++ TypeMatches}.
  
  
  
  
  % It is a CNAME and the Qtype was CNAME
  resolve_best_match_with_wildcard_cname(Message, _Qname, ?DNS_TYPE_CNAME, {_ClientIP, _ServerIP}, _CnameChain, _BestMatchRecords, _Zone, CnameRecords) ->
    Message#dns_message{aa = true, answers = Message#dns_message.answers ++ CnameRecords};
  
  % It is a CNAME and the Qtype was not CNAME
  resolve_best_match_with_wildcard_cname(Message, Qname, Qtype, {ClientIP, ServerIP}, CnameChain, BestMatchRecords, Zone, CnameRecords) ->
    CnameRecord = lists:last(CnameRecords), % There should only be one CNAME. Multiple CNAMEs kill unicorns.
    resolve_best_match_with_wildcard_cname(Message, Qname, Qtype, {ClientIP, ServerIP}, CnameChain, BestMatchRecords, Zone, CnameRecords, lists:member(CnameRecord, CnameChain)).
  
  % Indicates CNAME loop
  resolve_best_match_with_wildcard_cname(Message, _Qname, _Qtype, {_ClientIP, _ServerIP}, _CnameChain, _BestMatchRecords, _Zone, _CnameRecords, true) ->
    %erldns_log:debug("CNAME loop detected (best match)"),
    Message#dns_message{aa = true, rc = ?DNS_RCODE_SERVFAIL};
  
  % We should follow the CNAME
  resolve_best_match_with_wildcard_cname(Message, _Qname, Qtype, {ClientIP, ServerIP}, CnameChain, _BestMatchRecords, Zone, CnameRecords, false) ->
    %erldns_log:debug("Follow CNAME (best match)"),
    CnameRecord = lists:last(CnameRecords),
    Name = CnameRecord#dns_rr.data#dns_rrdata_cname.dname,
    restart_query(Message#dns_message{aa = true, answers = Message#dns_message.answers ++ CnameRecords}, Name, Qtype, {ClientIP, ServerIP}, CnameChain ++ CnameRecords, Zone, erldns_zone_cache:in_zone(Name)).
  
  
  
  
  % There are referral records
  resolve_best_match_referral(Message, Qname, Qtype, {ClientIP, ServerIP}, CnameChain, BestMatchRecords, Zone, ReferralRecords) ->
    resolve_best_match_referral(Message, Qname, Qtype, {ClientIP, ServerIP}, CnameChain, BestMatchRecords, Zone, ReferralRecords, lists:filter(erldns_records:match_type(?DNS_TYPE_SOA), BestMatchRecords)). % Lookup SOA in best match records
  
  % Indicate that we are not authoritative for the name as there were no
  % SOA records in the best-match results. The name has thus been delegated
  % to another authority.
  resolve_best_match_referral(Message, _Qname, _Qtype, {_ClientIP, _ServerIP}, _CnameChain, _BestMatchRecords, _Zone, ReferralRecords, []) ->
    Message#dns_message{aa = false, authority = Message#dns_message.authority ++ ReferralRecords};
  
  % We are authoritative for the name since there was an SOA record in
  % the best match results.
  resolve_best_match_referral(Message, _Qname, _Qtype, {_ClientIP, _ServerIP}, [], _BestMatchRecords, _Zone, _ReferralRecords, Authority) ->
    Message#dns_message{aa = true, rc = ?DNS_RCODE_NXDOMAIN, authority = Authority};
  
  % We are authoritative and the Qtype is ANY so we just return the 
  % original message.
  resolve_best_match_referral(Message, _Qname, ?DNS_TYPE_ANY, {_ClientIP, _ServerIP}, _CnameChain, _BestMatchRecords, _Zone, _ReferralRecords, _Authority) ->
    Message;
  resolve_best_match_referral(Message, _Qname, _Qtype, {_ClientIP, _ServerIP}, _CnameChain, _BestMatchRecords, _Zone, _ReferralRecords, Authority) ->
    Message#dns_message{authority = Authority}.
  
  
  