/crypto/external/bsd/heimdal/dist/lib/roken/getaddrinfo-test.c

http://www.minix3.org/ · C · 149 lines · 92 code · 24 blank · 33 comment · 18 complexity · 94a61a652da5ed48aacb7cb2a9173376 MD5 · raw file

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