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