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