localinfo.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2005 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 localinfo list local network addresses
  25. *
  26. * @section synopsis Synopsis
  27. *
  28. * <tt>localinfo [-imdn46gslh] [domainname]</tt>
  29. *
  30. * @section description Description
  31. *
  32. * The @em localinfo utility will obtain the local network addresses
  33. * and print them.
  34. *
  35. * @section options Options
  36. *
  37. * The @e localinfo utility accepts following command line options:
  38. * <dl>
  39. * <dt>-i</dt>
  40. * <dd>include interface name.</dd>
  41. * <dt>-m</dt>
  42. * <dd>map IPv4 addresses.</dd>
  43. * <dt>-d</dt>
  44. * <dd>require a reverse DNS entry.</dd>
  45. * <dt>-n</dt>
  46. * <dd>use numeric host names.</dd>
  47. * <dt>-4</dt>
  48. * <dd>IPv4 only.</dd>
  49. * <dt>-6</dt>
  50. * <dd>IPv6 only (but including mapped IPv4 addresses).</dd>
  51. * <dt>-g</dt>
  52. * <dd>Global addresses.</dd>
  53. * <dt>-s</dt>
  54. * <dd>Site-level addresses.</dd>
  55. * <dt>-l</dt>
  56. * <dd>Link-level addresses.</dd>
  57. * <dt>-h</dt>
  58. * <dd>Host-internal addresses.</dd>
  59. * </dl>
  60. *
  61. * @section examples Examples
  62. *
  63. * You want to find out local IPv6 addresses:
  64. * @code
  65. * $ localinfo -6
  66. * @endcode
  67. * You want to find out to link-local addresses
  68. * @code
  69. * $ localinfo -l -n
  70. * @endcode
  71. *
  72. * @section bugs Reporting Bugs
  73. * Report bugs to <sofia-sip-devel@lists.sourceforge.net>.
  74. *
  75. * @section author Author
  76. * Written by Pekka Pessi <pekka -dot pessi -at- nokia -dot- com>
  77. *
  78. * @section copyright Copyright
  79. * Copyright (C) 2005 Nokia Corporation.
  80. *
  81. * This program is free software; see the source for copying conditions.
  82. * There is @b NO warranty; not even for @b MERCHANTABILITY or <b>FITNESS
  83. * FOR A PARTICULAR PURPOSE</b>.
  84. */
  85. #include "config.h"
  86. #include <stdio.h>
  87. #include <string.h>
  88. #include <stdlib.h>
  89. #include "sofia-sip/su.h"
  90. #include "sofia-sip/su_localinfo.h"
  91. #include "su_module_debug.h"
  92. char const help[] =
  93. "usage: localinfo [-imdn46gslh] [domainname]\n"
  94. "\t-i include interface name\n"
  95. "\t-m map IPv4 addresses\n"
  96. "\t-d require DNS entry\n"
  97. "\t-n use numeric host names\n"
  98. "\t-4 IPv4 only\n"
  99. "\t-6 IPv6 only (but including mapped IPv4 addresses)\n"
  100. "\t-g Global addresses\n"
  101. "\t-s Site-level addresses\n"
  102. "\t-l Link-level addresses\n"
  103. "\t-h Host-internal addresses\n";
  104. int getopt(int argc, char * const argv[], char const *opstring);
  105. extern int optind;
  106. static void usage(int returncode)
  107. {
  108. fputs(help, stderr);
  109. exit(returncode);
  110. }
  111. int main(int argc, char *argv[])
  112. {
  113. char buffer[SU_ADDRSIZE];
  114. su_localinfo_t hints[1] = {{ LI_CANONNAME }};
  115. su_localinfo_t *li, *res = NULL;
  116. int error;
  117. int ifindex = 0;
  118. if (argv[1] && strcmp(argv[1], "--help") == 0)
  119. usage(0);
  120. for (;;) {
  121. switch(getopt(argc, argv, "iImdn46gslh")) {
  122. case 'I': ifindex = 1; break;
  123. case 'i': hints->li_flags |= LI_IFNAME; ifindex = 1; break;
  124. case 'm': hints->li_flags |= LI_V4MAPPED; break;
  125. case '4': hints->li_family = AF_INET; break;
  126. #if SU_HAVE_IN6
  127. case '6': hints->li_family = AF_INET6; break;
  128. #endif
  129. case 'd': hints->li_flags |= LI_NAMEREQD; break;
  130. case 'n': hints->li_flags |= LI_NUMERIC; break;
  131. case 'g': hints->li_scope |= LI_SCOPE_GLOBAL; break;
  132. case 's': hints->li_scope |= LI_SCOPE_SITE; break;
  133. case 'l': hints->li_scope |= LI_SCOPE_LINK; break;
  134. case 'h': hints->li_scope |= LI_SCOPE_HOST; break;
  135. case -1:
  136. goto main;
  137. default:
  138. usage(1);
  139. }
  140. }
  141. main:
  142. if (optind < argc)
  143. hints->li_canonname = argv[optind++];
  144. if (optind < argc)
  145. usage(1);
  146. su_init();
  147. if ((error = su_getlocalinfo(hints, &res)) == 0) {
  148. for (li = res; li; li = li->li_next) {
  149. if (li->li_flags & LI_NUMERIC) {
  150. fputs(li->li_canonname, stdout);
  151. }
  152. else {
  153. su_inet_ntop(li->li_family, SU_ADDR(li->li_addr),
  154. buffer, sizeof(buffer));
  155. printf("%s maddr=[%s]", li->li_canonname, buffer);
  156. }
  157. if (li->li_scope & LI_SCOPE_GLOBAL)
  158. fputs(" scope=global", stdout);
  159. else if (li->li_scope & LI_SCOPE_SITE)
  160. fputs(" scope=site", stdout);
  161. else if (li->li_scope & LI_SCOPE_LINK)
  162. fputs(" scope=link", stdout);
  163. else if (li->li_scope & LI_SCOPE_HOST)
  164. fputs(" scope=host", stdout);
  165. if (ifindex) {
  166. if (li->li_ifname)
  167. printf(" if[%d]=%s", li->li_index, li->li_ifname);
  168. else
  169. printf(" if[%d]", li->li_index);
  170. }
  171. puts("");
  172. }
  173. su_freelocalinfo(res);
  174. }
  175. else {
  176. fprintf(stderr, "localinfo: %s\n", su_gli_strerror(error));
  177. error = 1;
  178. }
  179. su_deinit();
  180. return error;
  181. }