summaryrefslogtreecommitdiff
path: root/libcli
diff options
context:
space:
mode:
authorVolker Lendecke <vl@samba.org>2019-01-10 16:54:41 +0100
committerVolker Lendecke <vl@samba.org>2019-01-15 07:53:22 +0100
commita8191f88ca82cc259b593d69980df76c3c30b1fa (patch)
tree5dd66e15a5b1feca0e86ef6fc92a22d24558195a /libcli
parentdbbce1a4545714cff0dad9e8c011800d0a66376e (diff)
downloadsamba-a8191f88ca82cc259b593d69980df76c3c30b1fa.tar.gz
libcli/dns: Add dns_res_rec_get_sockaddr
Pull the address from a res_rec if it's there Signed-off-by: Volker Lendecke <vl@samba.org> Reviewed-by: Jeremy Allison <jra@samba.org>
Diffstat (limited to 'libcli')
-rw-r--r--libcli/dns/dns_lookup.c37
-rw-r--r--libcli/dns/dns_lookup.h3
2 files changed, 40 insertions, 0 deletions
diff --git a/libcli/dns/dns_lookup.c b/libcli/dns/dns_lookup.c
index b99ab426e10..7402d443fba 100644
--- a/libcli/dns/dns_lookup.c
+++ b/libcli/dns/dns_lookup.c
@@ -335,3 +335,40 @@ fail:
TALLOC_FREE(ev);
return ret;
}
+
+bool dns_res_rec_get_sockaddr(const struct dns_res_rec *rec,
+ struct sockaddr_storage *addr)
+{
+ sa_family_t family;
+ const char *src;
+ void *dst;
+ int ret;
+
+ switch (rec->rr_type) {
+ case DNS_QTYPE_A:
+ family = AF_INET;
+ src = rec->rdata.ipv4_record;
+ dst = &(((struct sockaddr_in *)addr)->sin_addr);
+ break;
+#ifdef HAVE_IPV6
+ case DNS_QTYPE_AAAA:
+ family = AF_INET6;
+ src = rec->rdata.ipv6_record;
+ dst = &(((struct sockaddr_in6 *)addr)->sin6_addr);
+ break;
+#endif
+ default:
+ /* We only care about IP addresses */
+ return false;
+ }
+
+ *addr = (struct sockaddr_storage) { .ss_family = family };
+
+ ret = inet_pton(family, src, dst);
+ if (ret != 1) {
+ DBG_DEBUG("inet_pton(%s) failed\n", src);
+ return false;
+ }
+
+ return true;
+}
diff --git a/libcli/dns/dns_lookup.h b/libcli/dns/dns_lookup.h
index 157a3d252c1..0c05e0490c0 100644
--- a/libcli/dns/dns_lookup.h
+++ b/libcli/dns/dns_lookup.h
@@ -42,4 +42,7 @@ int dns_lookup(FILE *resolv_conf_fp,
TALLOC_CTX *mem_ctx,
struct dns_name_packet **reply);
+bool dns_res_rec_get_sockaddr(const struct dns_res_rec *rec,
+ struct sockaddr_storage *addr);
+
#endif