1b528cefcSMark Murray /* 25e9cd1aeSAssar Westerlund * 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 #ifdef HAVE_CONFIG_H 35b528cefcSMark Murray #include <config.h> 36c19800e8SDoug Rabson RCSID("$Id: getaddrinfo-test.c 15930 2005-08-12 13:42:17Z lha $"); 37b528cefcSMark Murray #endif 38b528cefcSMark Murray 39b528cefcSMark Murray #include "roken.h" 40b528cefcSMark Murray #include "getarg.h" 41b528cefcSMark Murray 42b528cefcSMark Murray static int flags; 43b528cefcSMark Murray static int family; 44b528cefcSMark Murray static int socktype; 45b528cefcSMark Murray 46b528cefcSMark Murray static int version_flag; 47b528cefcSMark Murray static int help_flag; 48b528cefcSMark Murray 49b528cefcSMark Murray static struct getargs args[] = { 50b528cefcSMark Murray {"flags", 0, arg_integer, &flags, "flags", NULL}, 51b528cefcSMark Murray {"family", 0, arg_integer, &family, "family", NULL}, 52b528cefcSMark Murray {"socktype",0, arg_integer, &socktype, "socktype", NULL}, 53b528cefcSMark Murray {"version", 0, arg_flag, &version_flag, "print version",NULL}, 54b528cefcSMark Murray {"help", 0, arg_flag, &help_flag, NULL, NULL} 55b528cefcSMark Murray }; 56b528cefcSMark Murray 57b528cefcSMark Murray static void 58b528cefcSMark Murray usage(int ret) 59b528cefcSMark Murray { 60b528cefcSMark Murray arg_printusage (args, 61b528cefcSMark Murray sizeof(args) / sizeof(args[0]), 62b528cefcSMark Murray NULL, 63b528cefcSMark Murray "[nodename servname...]"); 64b528cefcSMark Murray exit (ret); 65b528cefcSMark Murray } 66b528cefcSMark Murray 67b528cefcSMark Murray static void 68b528cefcSMark Murray doit (const char *nodename, const char *servname) 69b528cefcSMark Murray { 70b528cefcSMark Murray struct addrinfo hints; 71b528cefcSMark Murray struct addrinfo *res, *r; 72b528cefcSMark Murray int ret; 73b528cefcSMark Murray 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); 82b528cefcSMark Murray if (ret) { 83b528cefcSMark Murray printf ("error: %s\n", gai_strerror(ret)); 84b528cefcSMark Murray return; 85b528cefcSMark Murray } 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) { 94b528cefcSMark Murray printf ("\tbad address?\n"); 95b528cefcSMark Murray continue; 96b528cefcSMark Murray } 97c19800e8SDoug Rabson printf ("\tfamily = %d, socktype = %d, protocol = %d, " 98b528cefcSMark Murray "address = \"%s\", port = %d", 99b528cefcSMark Murray r->ai_family, r->ai_socktype, r->ai_protocol, 100b528cefcSMark Murray addrstr, 101b528cefcSMark Murray ntohs(socket_get_port (r->ai_addr))); 102b528cefcSMark Murray if (r->ai_canonname) 103b528cefcSMark Murray printf (", canonname = \"%s\"", r->ai_canonname); 104b528cefcSMark Murray printf ("\n"); 105b528cefcSMark Murray } 106b528cefcSMark Murray freeaddrinfo (res); 107b528cefcSMark Murray } 108b528cefcSMark Murray 109b528cefcSMark Murray int 110b528cefcSMark Murray main(int argc, char **argv) 111b528cefcSMark Murray { 112c19800e8SDoug Rabson int optidx = 0; 113b528cefcSMark Murray int i; 114b528cefcSMark Murray 115adb0ddaeSAssar Westerlund setprogname (argv[0]); 116b528cefcSMark Murray 117b528cefcSMark Murray if (getarg (args, sizeof(args) / sizeof(args[0]), argc, argv, 118c19800e8SDoug Rabson &optidx)) 119b528cefcSMark Murray usage (1); 120b528cefcSMark Murray 121b528cefcSMark Murray if (help_flag) 122b528cefcSMark Murray usage (0); 123b528cefcSMark Murray 124b528cefcSMark Murray if (version_flag) { 125adb0ddaeSAssar Westerlund fprintf (stderr, "%s from %s-%s)\n", getprogname(), PACKAGE, VERSION); 126b528cefcSMark Murray return 0; 127b528cefcSMark Murray } 128b528cefcSMark Murray 129c19800e8SDoug Rabson argc -= optidx; 130c19800e8SDoug Rabson argv += optidx; 131b528cefcSMark Murray 132b528cefcSMark Murray if (argc % 2 != 0) 133b528cefcSMark Murray usage (1); 134b528cefcSMark Murray 135b528cefcSMark Murray for (i = 0; i < argc; i += 2) { 136b528cefcSMark Murray const char *nodename = argv[i]; 137b528cefcSMark Murray 138b528cefcSMark Murray if (strcmp (nodename, "null") == 0) 139b528cefcSMark Murray nodename = NULL; 140b528cefcSMark Murray 141b528cefcSMark Murray doit (nodename, argv[i+1]); 142b528cefcSMark Murray } 143b528cefcSMark Murray return 0; 144b528cefcSMark Murray } 145