<p>George Joseph has uploaded this change for <strong>review</strong>.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/12766">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;">dns_core: Create new API ast_dns_resolve_ipv6_and_ipv4<br><br>The new function takes in a pointer to an ast_sockaddr structure,<br>a hostname and an optional port and then dispatches parallel<br>"AAAA" and "A" record queries. If an "AAAA" record is returned,<br>it's parsed into the ast_sockaddr structure along with the port<br>if it was supplied. If no "AAAA" record was returned, the<br>first "A" record returned (if any) is parsed instead.<br><br>This is a synchronous call. If you need asynchronous lookups,<br>use ast_dns_query_set_resolve_async and roll your own.<br><br>Change-Id: I194b0b0e73da94b35cc35263a868ffac3a8d0a95<br>---<br>M include/asterisk/dns_core.h<br>M main/dns_core.c<br>2 files changed, 86 insertions(+), 0 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;">git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/66/12766/1</pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/include/asterisk/dns_core.h b/include/asterisk/dns_core.h</span><br><span>index fe67e34..d74b012 100644</span><br><span>--- a/include/asterisk/dns_core.h</span><br><span>+++ b/include/asterisk/dns_core.h</span><br><span>@@ -28,6 +28,8 @@</span><br><span> extern "C" {</span><br><span> #endif</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/netsock2.h"</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /*! \brief Opaque structure for an active DNS query */</span><br><span> struct ast_dns_query_active;</span><br><span> </span><br><span>@@ -269,6 +271,26 @@</span><br><span> */</span><br><span> int ast_dns_resolve(const char *name, int rr_type, int rr_class, struct ast_dns_result **result);</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Synchronously resolves host to an AAAA or A record</span><br><span style="color: hsl(120, 100%, 40%);">+ * \since 16.6.0</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param address A pointer to an ast_sockaddr structure to receive the IPv6 or IPv4 address</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param host The hostname to resolve</span><br><span style="color: hsl(120, 100%, 40%);">+ * \param port (optional) A port to parse into the final ast_sockaddr structure</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval 0 success - query was completed and result is available</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval -1 failure</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \note This function makes parallel queries for both AAAA and A records for the host.</span><br><span style="color: hsl(120, 100%, 40%);">+ * The first returned AAAA record (if any) is used and if not found, the first A record</span><br><span style="color: hsl(120, 100%, 40%);">+ * is used.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \warning This function is synchronous and will block until records are returned or an error</span><br><span style="color: hsl(120, 100%, 40%);">+ * occurrs.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+int ast_dns_resolve_ipv6_and_ipv4(struct ast_sockaddr *address, const char *host, const char *port);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> #if defined(__cplusplus) || defined(c_plusplus)</span><br><span> }</span><br><span> #endif</span><br><span>diff --git a/main/dns_core.c b/main/dns_core.c</span><br><span>index 6f37a5d..e6ab4c7 100644</span><br><span>--- a/main/dns_core.c</span><br><span>+++ b/main/dns_core.c</span><br><span>@@ -40,6 +40,7 @@</span><br><span> #include "asterisk/dns_recurring.h"</span><br><span> #include "asterisk/dns_resolver.h"</span><br><span> #include "asterisk/dns_internal.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/netsock2.h"</span><br><span> </span><br><span> #include <netinet/in.h></span><br><span> #include <arpa/nameser.h></span><br><span>@@ -365,6 +366,69 @@</span><br><span> return *result ? 0 : -1;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+int ast_dns_resolve_ipv6_and_ipv4(struct ast_sockaddr *address, const char *host, const char *port)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ RAII_VAR(struct ast_dns_query_set *, queries, ast_dns_query_set_create(), ao2_cleanup);</span><br><span style="color: hsl(120, 100%, 40%);">+ int i;</span><br><span style="color: hsl(120, 100%, 40%);">+ int rc;</span><br><span style="color: hsl(120, 100%, 40%);">+ char temp_addr[AST_SOCKADDR_BUFLEN];</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!queries) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Couldn't allocate DNS query structure\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ rc = ast_dns_query_set_add(queries, host, ns_t_aaaa, ns_c_in);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (rc != 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Couldn't add 'AAAA' DNS query for '%s'\n", host);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ rc = ast_dns_query_set_add(queries, host, ns_t_a, ns_c_in);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (rc != 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Couldn't add 'A' DNS query for '%s'\n", host);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ rc = ast_query_set_resolve(queries);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (rc != 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Query set resolve failure '%s'\n", host);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ for (i = 0; i < ast_dns_query_set_num_queries(queries); ++i) {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_dns_query *query = ast_dns_query_set_get(queries, i);</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_dns_result *result = ast_dns_query_get_result(query);</span><br><span style="color: hsl(120, 100%, 40%);">+ const struct ast_dns_record *record;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ for (record = ast_dns_result_get_records(result); record; record = ast_dns_record_get_next(record)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ size_t data_size = ast_dns_record_get_data_size(record);</span><br><span style="color: hsl(120, 100%, 40%);">+ const unsigned char *data = (unsigned char *)ast_dns_record_get_data(record);</span><br><span style="color: hsl(120, 100%, 40%);">+ int rr_type = ast_dns_record_get_rr_type(record);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (rr_type == ns_t_aaaa && data_size == 16) {</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(temp_addr, sizeof(temp_addr), "[%02hhx%02hhx:%02hhx%02hhx:%02hhx%02hhx:%02hhx%02hhx:%02hhx%02hhx:%02hhx%02hhx:%02hhx%02hhx:%02hhx%02hhx]%s%s",</span><br><span style="color: hsl(120, 100%, 40%);">+ data[0], data[1], data[2], data[3], data[4], data[5], data[6], data[7],</span><br><span style="color: hsl(120, 100%, 40%);">+ data[8], data[9], data[10], data[11], data[12], data[13], data[14], data[15],</span><br><span style="color: hsl(120, 100%, 40%);">+ !ast_strlen_zero(port) ? ":" : "", S_OR(port, ""));</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (rr_type == ns_t_a && data_size == 4) {</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(temp_addr, sizeof(temp_addr), "%hhu.%hhu.%hhu.%hhu%s%s",</span><br><span style="color: hsl(120, 100%, 40%);">+ data[0], data[1], data[2], data[3],</span><br><span style="color: hsl(120, 100%, 40%);">+ !ast_strlen_zero(port) ? ":" : "", S_OR(port, ""));</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Unrecognized rr_type '%u' or data_size '%zu' from DNS query for host '%s'\n",</span><br><span style="color: hsl(120, 100%, 40%);">+ rr_type, data_size, host);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!ast_sockaddr_parse(address, temp_addr, 0)) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_log(LOG_ERROR, "Couldn't parse results from DNS query for host '%s'\n", host);</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> int ast_dns_resolver_set_data(struct ast_dns_query *query, void *data)</span><br><span> {</span><br><span> if (query->resolver_data) {</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/12766">change 12766</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/12766"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 16 </div>
<div style="display:none"> Gerrit-Change-Id: I194b0b0e73da94b35cc35263a868ffac3a8d0a95 </div>
<div style="display:none"> Gerrit-Change-Number: 12766 </div>
<div style="display:none"> Gerrit-PatchSet: 1 </div>
<div style="display:none"> Gerrit-Owner: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: newchange </div>