etc/services - sync with NetBSD-8
[minix.git] / crypto / external / bsd / heimdal / dist / lib / roken / getaddrinfo-test.c
blob09d9a9d841792df928ffc57f5080036ef55a3763
1 /* $NetBSD: getaddrinfo-test.c,v 1.1.1.1 2011/04/13 18:15:41 elric Exp $ */
3 /*
4 * Copyright (c) 1999 - 2000 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
36 #include <config.h>
38 #include <krb5/roken.h>
39 #include <krb5/getarg.h>
41 static int flags;
42 static int family;
43 static int socktype;
45 static int verbose_counter;
46 static int version_flag;
47 static int help_flag;
49 static 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}
58 static void
59 usage(int ret)
61 arg_printusage (args,
62 sizeof(args) / sizeof(args[0]),
63 NULL,
64 "[nodename servname...]");
65 exit (ret);
68 static void
69 doit (const char *nodename, const char *servname)
71 struct addrinfo hints;
72 struct addrinfo *res, *r;
73 int ret;
75 if (verbose_counter)
76 printf ("(%s,%s)... ", nodename ? nodename : "null", servname);
78 memset (&hints, 0, sizeof(hints));
79 hints.ai_flags = flags;
80 hints.ai_family = family;
81 hints.ai_socktype = socktype;
83 ret = getaddrinfo (nodename, servname, &hints, &res);
84 if (ret)
85 errx(1, "error: %s\n", gai_strerror(ret));
87 if (verbose_counter)
88 printf ("\n");
90 for (r = res; r != NULL; r = r->ai_next) {
91 char addrstr[256];
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;
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");
111 freeaddrinfo (res);
115 main(int argc, char **argv)
117 int optidx = 0;
118 int i;
120 setprogname (argv[0]);
122 if (getarg (args, sizeof(args) / sizeof(args[0]), argc, argv,
123 &optidx))
124 usage (1);
126 if (help_flag)
127 usage (0);
129 if (version_flag) {
130 fprintf (stderr, "%s from %s-%s\n", getprogname(), PACKAGE, VERSION);
131 return 0;
134 argc -= optidx;
135 argv += optidx;
137 if (argc % 2 != 0)
138 usage (1);
140 for (i = 0; i < argc; i += 2) {
141 const char *nodename = argv[i];
143 if (strcmp (nodename, "null") == 0)
144 nodename = NULL;
146 doit (nodename, argv[i+1]);
148 return 0;