]> CyberLeo.Net >> Repos - FreeBSD/releng/9.2.git/blob - contrib/bind9/lib/export/samples/sample-gai.c
- Copy stable/9 to releng/9.2 as part of the 9.2-RELEASE cycle.
[FreeBSD/releng/9.2.git] / contrib / bind9 / lib / export / samples / sample-gai.c
1 /*
2  * Copyright (C) 2009  Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and/or distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 /* $Id: sample-gai.c,v 1.4 2009/09/02 23:48:02 tbox Exp $ */
18
19 #include <config.h>
20
21 #include <sys/types.h>
22 #include <sys/socket.h>
23
24 #include <irs/netdb.h>
25
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29
30 static void
31 do_gai(int family, char *hostname) {
32         struct addrinfo hints, *res, *res0;
33         int error;
34         char namebuf[1024], addrbuf[1024], servbuf[1024];
35
36         memset(&hints, 0, sizeof(hints));
37         hints.ai_family = family;
38         hints.ai_socktype = SOCK_STREAM;
39         hints.ai_flags = AI_CANONNAME;
40         error = getaddrinfo(hostname, "http", &hints, &res0);
41         if (error) {
42                 fprintf(stderr, "getaddrinfo failed for %s,family=%d: %s\n",
43                         hostname, family, gai_strerror(error));
44                 return;
45         }
46
47         for (res = res0; res; res = res->ai_next) {
48                 error = getnameinfo(res->ai_addr, res->ai_addrlen,
49                                     addrbuf, sizeof(addrbuf),
50                                     NULL, 0, NI_NUMERICHOST);
51                 if (error == 0)
52                         error = getnameinfo(res->ai_addr, res->ai_addrlen,
53                                             namebuf, sizeof(namebuf),
54                                             servbuf, sizeof(servbuf), 0);
55                 if (error != 0) {
56                         fprintf(stderr, "getnameinfo failed: %s\n",
57                                 gai_strerror(error));
58                 } else {
59                         printf("%s(%s/%s)=%s:%s\n", hostname,
60                                res->ai_canonname, addrbuf, namebuf, servbuf);
61                 }
62         }
63
64         freeaddrinfo(res);
65 }
66
67 int
68 main(int argc, char *argv[]) {
69         if (argc < 2)
70                 exit(1);
71
72         do_gai(AF_INET, argv[1]);
73         do_gai(AF_INET6, argv[1]);
74         do_gai(AF_UNSPEC, argv[1]);
75
76         exit(0);
77 }