1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
|
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
int i, ret;
struct gaicb * reqs[argc - 1];
char host[NI_MAXHOST];
struct addrinfo *res;
if (argc < 2)
{
fprintf(stderr, "Usage: %s HOST...\n", argv[0]);
exit(EXIT_FAILURE);
}
for (i = 0; i < argc - 1; i++)
{
reqs[i] = (gaicb*)malloc(sizeof (*reqs[0]));
if (reqs[i] == NULL)
{
perror("malloc");
exit(EXIT_FAILURE);
}
memset(reqs[i], 0, sizeof (*reqs[0]));
reqs[i]->ar_name = argv[i + 1];
}
ret = getaddrinfo_a(GAI_WAIT, reqs, argc - 1, NULL);
if (ret != 0)
{
fprintf(stderr, "getaddrinfo_a() failed: %s\n",
gai_strerror(ret));
exit(EXIT_FAILURE);
}
for (i = 0; i < argc - 1; i++)
{
printf("%s: ", reqs[i]->ar_name);
ret = gai_error(reqs[i]);
if (ret == 0)
{
res = reqs[i]->ar_result;
ret = getnameinfo(res->ai_addr, res->ai_addrlen,
host, sizeof (host),
NULL, 0, NI_NUMERICHOST);
if (ret != 0)
{
fprintf(stderr, "getnameinfo() failed: %s\n",
gai_strerror(ret));
exit(EXIT_FAILURE);
}
puts(host);
/*
* Still got a memory leak with these lines
freeaddrinfo(reqs[i]->ar_result);
free (reqs[i]);
*/
}
else
{
puts(gai_strerror(ret));
}
}
exit(EXIT_SUCCESS);
}
|