Skip to content

Commit

Permalink
libcli/dns: Add dns_res_rec_get_sockaddr
Browse files Browse the repository at this point in the history
Pull the address from a res_rec if it's there

Signed-off-by: Volker Lendecke <[email protected]>
Reviewed-by: Jeremy Allison <[email protected]>
  • Loading branch information
vlendec committed Jan 15, 2019
1 parent dbbce1a commit a8191f8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
37 changes: 37 additions & 0 deletions libcli/dns/dns_lookup.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,3 +335,40 @@ int dns_lookup(FILE *resolv_conf_fp,
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;
}
3 changes: 3 additions & 0 deletions libcli/dns/dns_lookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit a8191f8

Please sign in to comment.