localinfo.c 5.13 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

25
/**@page localinfo list local network addresses
26
 *
27
 * @section synopsis Synopsis
Pekka Pessi's avatar
Pekka Pessi committed
28 29 30
 *
 * <tt>localinfo [-imdn46gslh] [domainname]</tt>
 *
31
 * @section description Description
32
 *
Pekka Pessi's avatar
Pekka Pessi committed
33 34 35
 * The @em localinfo utility will obtain the local network addresses
 * and print them.
 *
36
 * @section options Options
Pekka Pessi's avatar
Pekka Pessi committed
37
 *
38
 * The @e localinfo utility accepts following command line options:
Pekka Pessi's avatar
Pekka Pessi committed
39 40 41 42 43 44
 * <dl>
 * <dt>-i</dt>
 * <dd>include interface name.</dd>
 * <dt>-m</dt>
 * <dd>map IPv4 addresses.</dd>
 * <dt>-d</dt>
45
 * <dd>require a reverse DNS entry.</dd>
Pekka Pessi's avatar
Pekka Pessi committed
46 47 48 49 50 51 52 53 54 55 56 57 58
 * <dt>-n</dt>
 * <dd>use numeric host names.</dd>
 * <dt>-4</dt>
 * <dd>IPv4 only.</dd>
 * <dt>-6</dt>
 * <dd>IPv6 only (but including mapped IPv4 addresses).</dd>
 * <dt>-g</dt>
 * <dd>Global addresses.</dd>
 * <dt>-s</dt>
 * <dd>Site-level addresses.</dd>
 * <dt>-l</dt>
 * <dd>Link-level addresses.</dd>
 * <dt>-h</dt>
59
 * <dd>Host-internal addresses.</dd>
Pekka Pessi's avatar
Pekka Pessi committed
60 61
 * </dl>
 *
62
 * @section examples Examples
Pekka Pessi's avatar
Pekka Pessi committed
63
 *
64
 * You want to find out local IPv6 addresses:
Pekka Pessi's avatar
Pekka Pessi committed
65 66 67 68 69 70 71 72
 * @code
 * $ localinfo -6
 * @endcode
 * You want to find out to link-local addresses
 * @code
 * $ localinfo -l -n
 * @endcode
 *
73 74 75 76 77 78 79 80
 * @section bugs Reporting Bugs
 * Report bugs to <sofia-sip-devel@lists.sourceforge.net>.
 *
 * @section author Author
 * Written by Pekka Pessi <pekka -dot pessi -at- nokia -dot- com>
 *
 * @section copyright Copyright
 * Copyright (C) 2005 Nokia Corporation.
Pekka Pessi's avatar
Pekka Pessi committed
81
 *
82 83 84
 * This program is free software; see the source for copying conditions.
 * There is @b NO warranty; not even for @b MERCHANTABILITY or <b>FITNESS
 * FOR A PARTICULAR PURPOSE</b>.
Pekka Pessi's avatar
Pekka Pessi committed
85 86
 */

87 88 89 90 91 92 93 94 95 96
#include "config.h"

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "sofia-sip/su.h"
#include "sofia-sip/su_localinfo.h"
#include "su_module_debug.h"

Pekka Pessi's avatar
Pekka Pessi committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
char const help[] =
"usage: localinfo [-imdn46gslh] [domainname]\n"
"\t-i include interface name\n"
"\t-m map IPv4 addresses\n"
"\t-d require DNS entry\n"
"\t-n use numeric host names\n"
"\t-4 IPv4 only\n"
"\t-6 IPv6 only (but including mapped IPv4 addresses)\n"
"\t-g Global addresses\n"
"\t-s Site-level addresses\n"
"\t-l Link-level addresses\n"
"\t-h Host-internal addresses\n";

int getopt(int argc, char * const argv[], char const *opstring);
extern int optind;

void usage(int returncode)
{
  fputs(help, stderr);
  exit(returncode);
}

int main(int argc, char *argv[])
{
121
  char buffer[SU_ADDRSIZE];
Pekka Pessi's avatar
Pekka Pessi committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
  su_localinfo_t hints[1] = {{ LI_CANONNAME }};
  su_localinfo_t *li, *res = NULL;
  int error;
  int ifindex = 0;

  if (argv[1] && strcmp(argv[1], "--help") == 0)
    usage(0);

  for (;;) {
    switch(getopt(argc, argv, "iImdn46gslh")) {

    case 'I': ifindex = 1; break;
    case 'i': hints->li_flags |= LI_IFNAME; ifindex = 1; break;
    case 'm': hints->li_flags |= LI_V4MAPPED; break;
    case '4': hints->li_family = AF_INET; break;
137
#if SU_HAVE_IN6
Pekka Pessi's avatar
Pekka Pessi committed
138
    case '6': hints->li_family = AF_INET6; break;
139
#endif
Pekka Pessi's avatar
Pekka Pessi committed
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    case 'd': hints->li_flags |= LI_NAMEREQD; break;
    case 'n': hints->li_flags |= LI_NUMERIC; break;
    case 'g': hints->li_scope |= LI_SCOPE_GLOBAL; break;
    case 's': hints->li_scope |= LI_SCOPE_SITE; break;
    case 'l': hints->li_scope |= LI_SCOPE_LINK; break;
    case 'h': hints->li_scope |= LI_SCOPE_HOST; break;

    case -1:
      goto main;

    default:
      usage(1);
    }
  }

 main:
  if (optind < argc)
    hints->li_canonname = argv[optind++];

  if (optind < argc)
    usage(1);

  su_init();

  if ((error = su_getlocalinfo(hints, &res)) == 0) {
    for (li = res; li; li = li->li_next) {
      if (li->li_flags & LI_NUMERIC) {
        fputs(li->li_canonname, stdout);
      }
      else {
170 171
        su_inet_ntop(li->li_family, SU_ADDR(li->li_addr),
		     buffer, sizeof(buffer));
Pekka Pessi's avatar
Pekka Pessi committed
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
        printf("%s maddr=[%s]", li->li_canonname, buffer);
      }
      if (li->li_scope & LI_SCOPE_GLOBAL)
	fputs(" scope=global", stdout);
      else if (li->li_scope & LI_SCOPE_SITE)
	fputs(" scope=site", stdout);
      else if (li->li_scope & LI_SCOPE_LINK)
	fputs(" scope=link", stdout);
      else if (li->li_scope & LI_SCOPE_HOST)
	fputs(" scope=host", stdout);
      if (ifindex) {
	if (li->li_ifname)
	  printf(" if[%d]=%s", li->li_index, li->li_ifname);
	else
	  printf(" if[%d]", li->li_index);
      }
      puts("");
    }
    su_freelocalinfo(res);
  }
  else {
    fprintf(stderr, "localinfo: %s\n", su_gli_strerror(error));
    error = 1;
  }

  su_deinit();

  return error;
}