Path: blob/main/crypto/heimdal/lib/roken/getaddrinfo-test.c
39507 views
/*1* Copyright (c) 1999 - 2000 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include <config.h>3435#include "roken.h"36#include "getarg.h"3738static int flags;39static int family;40static int socktype;4142static int verbose_counter;43static int version_flag;44static int help_flag;4546static struct getargs args[] = {47{"verbose", 0, arg_counter, &verbose_counter,"verbose", NULL},48{"flags", 0, arg_integer, &flags, "flags", NULL},49{"family", 0, arg_integer, &family, "family", NULL},50{"socktype",0, arg_integer, &socktype, "socktype", NULL},51{"version", 0, arg_flag, &version_flag, "print version",NULL},52{"help", 0, arg_flag, &help_flag, NULL, NULL}53};5455static void56usage(int ret)57{58arg_printusage (args,59sizeof(args) / sizeof(args[0]),60NULL,61"[nodename servname...]");62exit (ret);63}6465static void66doit (const char *nodename, const char *servname)67{68struct addrinfo hints;69struct addrinfo *res, *r;70int ret;7172if (verbose_counter)73printf ("(%s,%s)... ", nodename ? nodename : "null", servname);7475memset (&hints, 0, sizeof(hints));76hints.ai_flags = flags;77hints.ai_family = family;78hints.ai_socktype = socktype;7980ret = getaddrinfo (nodename, servname, &hints, &res);81if (ret)82errx(1, "error: %s\n", gai_strerror(ret));8384if (verbose_counter)85printf ("\n");8687for (r = res; r != NULL; r = r->ai_next) {88char addrstr[256];8990if (inet_ntop (r->ai_family,91socket_get_address (r->ai_addr),92addrstr, sizeof(addrstr)) == NULL) {93if (verbose_counter)94printf ("\tbad address?\n");95continue;96}97if (verbose_counter) {98printf ("\tfamily = %d, socktype = %d, protocol = %d, "99"address = \"%s\", port = %d",100r->ai_family, r->ai_socktype, r->ai_protocol,101addrstr,102ntohs(socket_get_port (r->ai_addr)));103if (r->ai_canonname)104printf (", canonname = \"%s\"", r->ai_canonname);105printf ("\n");106}107}108freeaddrinfo (res);109}110111int112main(int argc, char **argv)113{114int optidx = 0;115int i;116117setprogname (argv[0]);118119if (getarg (args, sizeof(args) / sizeof(args[0]), argc, argv,120&optidx))121usage (1);122123if (help_flag)124usage (0);125126if (version_flag) {127fprintf (stderr, "%s from %s-%s\n", getprogname(), PACKAGE, VERSION);128return 0;129}130131argc -= optidx;132argv += optidx;133134if (argc % 2 != 0)135usage (1);136137for (i = 0; i < argc; i += 2) {138const char *nodename = argv[i];139140if (strcmp (nodename, "null") == 0)141nodename = NULL;142143doit (nodename, argv[i+1]);144}145return 0;146}147148149