Author: mmichelson Date: Mon Apr 6 15:24:31 2015 New Revision: 434152 URL: http://svnview.digium.com/svn/asterisk?view=rev&rev=434152 Log: Place record location code within the DNS core.
This eliminates some more duplication. Modified: team/group/dns/include/asterisk/dns_internal.h team/group/dns/main/dns_core.c team/group/dns/main/dns_naptr.c team/group/dns/main/dns_srv.c Modified: team/group/dns/include/asterisk/dns_internal.h URL: http://svnview.digium.com/svn/asterisk/team/group/dns/include/asterisk/dns_internal.h?view=diff&rev=434152&r1=434151&r2=434152 ============================================================================== --- team/group/dns/include/asterisk/dns_internal.h (original) +++ team/group/dns/include/asterisk/dns_internal.h Mon Apr 6 15:24:31 2015 @@ -204,3 +204,18 @@ */ void dns_srv_sort(struct ast_dns_result *result); +/*! + * \brief Find the location of a DNS record within the entire DNS answer + * + * The DNS record that has been returned by the resolver may be a copy of the record that was + * found in the complete DNS response. If so, then some DNS record types (specifically those that + * parse domains) will need to locate the DNS record within the complete DNS response. This is so + * that if the domain contains pointers to other sections of the DNS response, then the referenced + * domains may be located. + * + * \param record The DNS record returned by a resolver implementation + * \param record_size The size of the DNS record in bytes + * \param response The complete DNS answer + * \param response_size The size of the complete DNS response + */ +char *dns_find_record(const char *record, size_t record_size, const char *response, size_t response_size); Modified: team/group/dns/main/dns_core.c URL: http://svnview.digium.com/svn/asterisk/team/group/dns/main/dns_core.c?view=diff&rev=434152&r1=434151&r2=434152 ============================================================================== --- team/group/dns/main/dns_core.c (original) +++ team/group/dns/main/dns_core.c Mon Apr 6 15:24:31 2015 @@ -610,3 +610,24 @@ ast_verb(2, "Unregistered DNS resolver '%s'\n", resolver->name); } + +char *dns_find_record(const char *record, size_t record_size, const char *response, size_t response_size) +{ + size_t remaining_size = response_size; + const char *search_base = response; + char *record_offset; + + while (1) { + record_offset = memchr(search_base, record[0], remaining_size); + + ast_assert(record_offset != NULL); + ast_assert(search_base + remaining_size - record_offset >= record_size); + + if (!memcmp(record_offset, record, record_size)) { + return record_offset; + } + + remaining_size -= record_offset - search_base; + search_base = record_offset + 1; + } +} Modified: team/group/dns/main/dns_naptr.c URL: http://svnview.digium.com/svn/asterisk/team/group/dns/main/dns_naptr.c?view=diff&rev=434152&r1=434151&r2=434152 ============================================================================== --- team/group/dns/main/dns_naptr.c (original) +++ team/group/dns/main/dns_naptr.c Mon Apr 6 15:24:31 2015 @@ -392,54 +392,10 @@ char *regexp; char replacement[256] = ""; int replacement_size; - char *naptr_offset; - char *naptr_search_base = (char *)query->result->answer; - size_t remaining_size = query->result->answer_size; - char *end_of_record; + const char *end_of_record; enum flags_result flags_res; - /* - * This is bordering on the hackiest thing I've ever written. - * Part of parsing a NAPTR record is to parse a potential replacement - * domain name. Decoding this domain name requires the use of the - * dn_expand() function. This function requires that the domain you - * pass in be a pointer to within the full DNS answer. Unfortunately, - * libunbound gives its RRs back as copies of data from the DNS answer - * instead of pointers to within the DNS answer. This means that in order - * to be able to parse the domain name correctly, I need to find the - * current NAPTR record inside the DNS answer and operate on it. This - * loop is designed to find the current NAPTR record within the full - * DNS answer and set the "ptr" variable to the beginning of the - * NAPTR RDATA - */ - while (1) { - naptr_offset = memchr(naptr_search_base, data[0], remaining_size); - - /* Since the NAPTR record we have been given came from the DNS answer, - * we should never run into a situation where we can't find ourself - * in the answer - */ - ast_assert(naptr_offset != NULL); - ast_assert(naptr_search_base + remaining_size - naptr_offset >= size); - - /* ... but just to be on the safe side, let's be sure we can break - * out if the assertion doesn't hold - */ - if (!naptr_offset || naptr_search_base + remaining_size - naptr_offset < size) { - ast_log(LOG_ERROR, "Failed to locate NAPTR record within DNS result\n"); - return NULL; - } - - if (!memcmp(naptr_offset, data, size)) { - /* BAM! FOUND IT! */ - ptr = naptr_offset; - break; - } - /* Data didn't match us, so keep looking */ - remaining_size -= naptr_offset - naptr_search_base; - naptr_search_base = naptr_offset + 1; - } - + ptr = dns_find_record(data, size, query->result->answer, query->result->answer_size); ast_assert(ptr != NULL); end_of_record = ptr + size; @@ -472,6 +428,7 @@ if (PAST_END_OF_RECORD) { return NULL; } + flags = ptr; ptr += flags_size; if (PAST_END_OF_RECORD) { Modified: team/group/dns/main/dns_srv.c URL: http://svnview.digium.com/svn/asterisk/team/group/dns/main/dns_srv.c?view=diff&rev=434152&r1=434151&r2=434152 ============================================================================== --- team/group/dns/main/dns_srv.c (original) +++ team/group/dns/main/dns_srv.c Mon Apr 6 15:24:31 2015 @@ -47,29 +47,12 @@ uint16_t weight; uint16_t port; const char *ptr; - char *srv_offset; - char *srv_search_base = (char *)query->result->answer; - size_t remaining_size = query->result->answer_size; const char *end_of_record; struct ast_dns_srv_record *srv; int host_size; char host[NI_MAXHOST] = ""; - while (1) { - srv_offset = memchr(srv_search_base, data[0], remaining_size); - - ast_assert(srv_offset != NULL); - ast_assert(srv_search_base + remaining_size - srv_offset >= size); - - if (!memcmp(srv_offset, data, size)) { - ptr = srv_offset; - break; - } - - remaining_size -= srv_offset - srv_search_base; - srv_search_base = srv_offset + 1; - } - + ptr = dns_find_record(data, size, query->result->answer, query->result->answer_size); ast_assert(ptr != NULL); end_of_record = ptr + size; -- _____________________________________________________________________ -- Bandwidth and Colocation Provided by http://www.api-digital.com -- svn-commits mailing list To UNSUBSCRIBE or update options visit: http://lists.digium.com/mailman/listinfo/svn-commits