Skip to content

Instantly share code, notes, and snippets.

@AdamMajer
Last active February 3, 2020 09:33
Show Gist options
  • Save AdamMajer/fa1808b6c4751638f30e1bd7085bc64b to your computer and use it in GitHub Desktop.
Save AdamMajer/fa1808b6c4751638f30e1bd7085bc64b to your computer and use it in GitHub Desktop.
#define _DEFAULT_SOURCE
#include <ares.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <stdio.h>
const unsigned char buf[] =
"\x2A\x42\x81\x80\x00\x01\x00\x02\x00\x00\x00\x00\x07\x65\x78\x61"
"\x6D\x70\x6C\x65\x03\x6F\x72\x67\x00\x00\xFF\x00\x01\x07\x65\x78"
"\x61\x6D\x70\x6C\x65\x03\x6F\x72\x67\x00\x00\x01\x00\x01\xC6\xAE"
"\xA1\x55\x00\x04\x01\x02\x03\x04\x07\x65\x78\x61\x6D\x70\x6C\x65"
"\x03\x6F\x72\x67\x00\x00\x1C\x00\x01\xC6\xAE\xA1\x55\x00\x10\x00"
"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x42";
const size_t len = sizeof(buf);
int main()
{
struct hostent *h;
struct ares_addrttl ttls[10];
struct ares_addr6ttl ttls6[10];
int naddr=10;
printf("ares A parse: %d\n", ares_parse_a_reply(buf, len, &h, ttls, &naddr));
printf(" addr ttls #%d\n", naddr);
printf( " host: %s -> %d\n", h->h_name, h->h_length);
for (char **ptr=h->h_addr_list; *ptr!=NULL; ptr++)
printf(" + 1 addr\n");
for (int i=0 ;i<naddr; ++i) {
printf(" ttls %d: %u %s\n", i, ttls[i].ttl, inet_ntoa(ttls[i].ipaddr));
}
naddr=10;
printf("ares AAAA parse: %d\n", ares_parse_aaaa_reply(buf, len, &h, ttls6, &naddr));
printf( " host: %s -> %d\n", h->h_name, h->h_length);
for (char **ptr=h->h_addr_list; *ptr!=NULL; ptr++)
printf(" + 1 addr\n");
for (int i=0 ;i<naddr; ++i) {
printf(" ttls %d: %u ", i, ttls6[i].ttl);
for (int j=0; j<15; ++j)
printf("%02x:", (unsigned)ttls6[i].ip6addr._S6_un._S6_u8[j]);
printf("%02x\n", (unsigned)ttls6[i].ip6addr._S6_un._S6_u8[15]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment