]> CyberLeo.Net >> Repos - FreeBSD/stable/9.git/blob - contrib/bind9/lib/export/samples/sample-gai.c
Update BIND to 9.9.8
[FreeBSD/stable/9.git] / contrib / bind9 / lib / export / samples / sample-gai.c
1 /*
2  * Copyright (C) 2009, 2012, 2013, 2015  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 #include <isc/print.h>
31
32 static void
33 do_gai(int family, char *hostname) {
34         struct addrinfo hints, *res, *res0;
35         int error;
36         char namebuf[1024], addrbuf[1024], servbuf[1024];
37
38         memset(&hints, 0, sizeof(hints));
39         hints.ai_family = family;
40         hints.ai_socktype = SOCK_STREAM;
41         hints.ai_flags = AI_CANONNAME;
42         error = getaddrinfo(hostname, "http", &hints, &res0);
43         if (error) {
44                 fprintf(stderr, "getaddrinfo failed for %s,family=%d: %s\n",
45                         hostname, family, gai_strerror(error));
46                 return;
47         }
48
49         for (res = res0; res; res = res->ai_next) {
50                 error = getnameinfo(res->ai_addr, res->ai_addrlen,
51                                     addrbuf, sizeof(addrbuf),
52                                     NULL, 0, NI_NUMERICHOST);
53                 if (error == 0)
54                         error = getnameinfo(res->ai_addr, res->ai_addrlen,
55                                             namebuf, sizeof(namebuf),
56                                             servbuf, sizeof(servbuf), 0);
57                 if (error != 0) {
58                         fprintf(stderr, "getnameinfo failed: %s\n",
59                                 gai_strerror(error));
60                 } else {
61                         printf("%s(%s/%s)=%s:%s\n", hostname,
62                                res->ai_canonname, addrbuf, namebuf, servbuf);
63                 }
64         }
65
66         freeaddrinfo(res0);
67 }
68
69 int
70 main(int argc, char *argv[]) {
71         if (argc < 2)
72                 exit(1);
73
74         do_gai(AF_INET, argv[1]);
75         do_gai(AF_INET6, argv[1]);
76         do_gai(AF_UNSPEC, argv[1]);
77
78         return (0);
79 }