1/*	$NetBSD: getaddrinfo-test.c,v 1.1.1.1 2011/04/13 18:15:41 elric Exp $	*/
2
3/*
4 * Copyright (c) 1999 - 2000 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 *
19 * 3. Neither the name of the Institute nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#include <config.h>
37
38#include <krb5/roken.h>
39#include <krb5/getarg.h>
40
41static int flags;
42static int family;
43static int socktype;
44
45static int verbose_counter;
46static int version_flag;
47static int help_flag;
48
49static struct getargs args[] = {
50    {"verbose",	0,	arg_counter,	&verbose_counter,"verbose",	NULL},
51    {"flags",	0,	arg_integer,	&flags,		"flags",	NULL},
52    {"family",	0,	arg_integer,	&family,	"family",	NULL},
53    {"socktype",0,	arg_integer,	&socktype,	"socktype",	NULL},
54    {"version",	0,	arg_flag,	&version_flag,	"print version",NULL},
55    {"help",	0,	arg_flag,	&help_flag,	NULL,		NULL}
56};
57
58static void
59usage(int ret)
60{
61    arg_printusage (args,
62		    sizeof(args) / sizeof(args[0]),
63		    NULL,
64		    "[nodename servname...]");
65    exit (ret);
66}
67
68static void
69doit (const char *nodename, const char *servname)
70{
71    struct addrinfo hints;
72    struct addrinfo *res, *r;
73    int ret;
74
75    if (verbose_counter)
76	printf ("(%s,%s)... ", nodename ? nodename : "null", servname);
77
78    memset (&hints, 0, sizeof(hints));
79    hints.ai_flags    = flags;
80    hints.ai_family   = family;
81    hints.ai_socktype = socktype;
82
83    ret = getaddrinfo (nodename, servname, &hints, &res);
84    if (ret)
85	errx(1, "error: %s\n", gai_strerror(ret));
86
87    if (verbose_counter)
88	printf ("\n");
89
90    for (r = res; r != NULL; r = r->ai_next) {
91	char addrstr[256];
92
93	if (inet_ntop (r->ai_family,
94		       socket_get_address (r->ai_addr),
95		       addrstr, sizeof(addrstr)) == NULL) {
96	    if (verbose_counter)
97		printf ("\tbad address?\n");
98	    continue;
99	}
100	if (verbose_counter) {
101	    printf ("\tfamily = %d, socktype = %d, protocol = %d, "
102		    "address = \"%s\", port = %d",
103		    r->ai_family, r->ai_socktype, r->ai_protocol,
104		    addrstr,
105		    ntohs(socket_get_port (r->ai_addr)));
106	    if (r->ai_canonname)
107		printf (", canonname = \"%s\"", r->ai_canonname);
108	    printf ("\n");
109	}
110    }
111    freeaddrinfo (res);
112}
113
114int
115main(int argc, char **argv)
116{
117    int optidx = 0;
118    int i;
119
120    setprogname (argv[0]);
121
122    if (getarg (args, sizeof(args) / sizeof(args[0]), argc, argv,
123		&optidx))
124	usage (1);
125
126    if (help_flag)
127	usage (0);
128
129    if (version_flag) {
130	fprintf (stderr, "%s from %s-%s\n", getprogname(), PACKAGE, VERSION);
131	return 0;
132    }
133
134    argc -= optidx;
135    argv += optidx;
136
137    if (argc % 2 != 0)
138	usage (1);
139
140    for (i = 0; i < argc; i += 2) {
141	const char *nodename = argv[i];
142
143	if (strcmp (nodename, "null") == 0)
144	    nodename = NULL;
145
146	doit (nodename, argv[i+1]);
147    }
148    return 0;
149}
150