addrinfo.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2005,2007 Nokia Corporation.
  5. *
  6. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. /**@page addrinfo Resolve network services
  25. *
  26. * @section synopsis Synopsis
  27. *
  28. * <tt>addrinfo [-pcn46] service-name host</tt>
  29. *
  30. * @section description Description
  31. *
  32. * The @em addrinfo utility will use su_getaddrinfo() to resolve the network
  33. * services and print resolved names. See sect 6.1 of RFC3493 and the getaddrinfo(3)
  34. * manual page of POSIX 1003.1g, for more information.
  35. *
  36. * @section options Options
  37. *
  38. * The @e addrinfo utility accepts following ccommand line options:
  39. * <dl>
  40. * <dt>-p</dt>
  41. * <dd>use passive open.</dd>
  42. * <dt>-c</dt>
  43. * <dd>get canonic name.</dd>
  44. * <dt>-n</dt>
  45. * <dd>use numeric host names.</dd>
  46. * <dt>-4</dt>
  47. * <dd>IPv4 only.</dd>
  48. * <dt>-6</dt>
  49. * <dd>IPv6 only (but including mapped IPv4 addresses).</dd>
  50. * </dl>
  51. *
  52. * @section bugs Reporting Bugs
  53. * Report bugs to <sofia-sip-devel@lists.sourceforge.net>.
  54. *
  55. * @section author Author
  56. * Written by Pekka Pessi <pekka -dot pessi -at- nokia -dot- com>
  57. *
  58. * @section copyright Copyright
  59. * Copyright (C) 2005,2007 Nokia Corporation.
  60. *
  61. * This program is free software; see the source for copying conditions.
  62. * There is @b NO warranty; not even for @b MERCHANTABILITY or <b>FITNESS
  63. * FOR A PARTICULAR PURPOSE</b>.
  64. */
  65. #include "config.h"
  66. #include <stdio.h>
  67. #include <string.h>
  68. #include <stdlib.h>
  69. #include "sofia-sip/su.h"
  70. char const help[] =
  71. "usage: addrinfo [-pnc46] <servicename> <domainname>\n"
  72. "\t-p query for passive open\n"
  73. "\t-n use numeric host names\n"
  74. "\t-c ask for canonic names\n"
  75. "\t-4 IPv4 only\n"
  76. "\t-6 IPv6 only (but including mapped IPv4 addresses)\n"
  77. ;
  78. int getopt(int argc, char * const argv[], char const *opstring);
  79. extern int optind;
  80. static void usage(void)
  81. {
  82. fputs(help, stderr);
  83. exit(1);
  84. }
  85. int main(int argc, char *argv[])
  86. {
  87. #if SU_HAVE_IN6
  88. char buffer[INET6_ADDRSTRLEN];
  89. #else
  90. char buffer[20];
  91. #endif
  92. su_addrinfo_t hints[1] = {{ 0 }};
  93. su_addrinfo_t *ai, *res = NULL;
  94. char const *host, *service;
  95. int error;
  96. for (;;) {
  97. switch(getopt(argc, argv, "ndp4c")) {
  98. case '4': hints->ai_family = AF_INET; break;
  99. #if SU_HAVE_IN6
  100. case '6': hints->ai_family = AF_INET6; break;
  101. #endif
  102. case 'p': hints->ai_flags |= AI_PASSIVE; break;
  103. case 'n': hints->ai_flags |= AI_NUMERICHOST; break;
  104. case 'c': hints->ai_flags |= AI_CANONNAME; break;
  105. case -1:
  106. goto main;
  107. default:
  108. usage();
  109. }
  110. }
  111. main:
  112. if (optind + 1 >= argc)
  113. usage();
  114. service = argv[optind++];
  115. host = argv[optind++];
  116. su_init();
  117. if ((error = su_getaddrinfo(host, service, hints, &res)) == 0) {
  118. for (ai = res; ai; ai = ai->ai_next) {
  119. su_sockaddr_t const *su = (su_sockaddr_t const *)ai->ai_addr;
  120. unsigned port;
  121. #if SU_HAVE_IN6
  122. if (su->su_family != AF_INET6 && su->su_family != AF_INET)
  123. continue;
  124. #else
  125. if (su->su_family != AF_INET)
  126. continue;
  127. #endif
  128. port = ntohs(su->su_port);
  129. su_inet_ntop(ai->ai_family, SU_ADDR(su), buffer, sizeof(buffer));
  130. printf("%d@[%s]:%u", ai->ai_protocol, buffer, port);
  131. if (ai->ai_flags & AI_CANONNAME)
  132. printf(" canon=%s", ai->ai_canonname);
  133. puts("");
  134. }
  135. su_freeaddrinfo(res);
  136. }
  137. else {
  138. fprintf(stderr, "addrinfo: %s\n", su_gai_strerror(error));
  139. error = 1;
  140. }
  141. su_deinit();
  142. return error;
  143. }