addrinfo.c 4.05 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3
/*
 * This file is part of the Sofia-SIP package
 *
4
 * Copyright (C) 2005,2007 Nokia Corporation.
Pekka Pessi's avatar
Pekka Pessi committed
5 6 7
 *
 * 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 addrinfo Resolve network services
26
 *
27
 * @section synopsis Synopsis
Pekka Pessi's avatar
Pekka Pessi committed
28
 *
29
 * <tt>addrinfo [-pcn46] service-name host</tt>
Pekka Pessi's avatar
Pekka Pessi committed
30
 *
31
 * @section description Description
32
 *
Pekka Pessi's avatar
Pekka Pessi committed
33
 * The @em addrinfo utility will use su_getaddrinfo() to resolve the network
34 35
 * services and print resolved names. See sect 6.1 of RFC3493 and the getaddrinfo(3)
 * manual page of POSIX 1003.1g, for more information.
Pekka Pessi's avatar
Pekka Pessi committed
36
 *
37
 * @section options Options
Pekka Pessi's avatar
Pekka Pessi committed
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
 *
 * The @e addrinfo utility accepts following ccommand line options:
 * <dl>
 * <dt>-p</dt>
 * <dd>use passive open.</dd>
 * <dt>-c</dt>
 * <dd>get canonic name.</dd>
 * <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>
 * </dl>
 *
53 54 55 56 57 58 59
 * @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
60
 * Copyright (C) 2005,2007 Nokia Corporation.
Pekka Pessi's avatar
Pekka Pessi committed
61
 *
62 63 64
 * 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
65 66
 */

67 68 69 70 71 72 73 74
#include "config.h"

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

#include "sofia-sip/su.h"

Pekka Pessi's avatar
Pekka Pessi committed
75
char const help[] =
76
"usage: addrinfo [-pnc46] <servicename> <domainname>\n"
Pekka Pessi's avatar
Pekka Pessi committed
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
"\t-p query for passive open\n"
"\t-n use numeric host names\n"
"\t-c ask for canonic names\n"
"\t-4 IPv4 only\n"
"\t-6 IPv6 only (but including mapped IPv4 addresses)\n"
;

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

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

int main(int argc, char *argv[])
{
95
#if SU_HAVE_IN6
Pekka Pessi's avatar
Pekka Pessi committed
96
  char buffer[INET6_ADDRSTRLEN];
97 98 99
#else
  char buffer[20];
#endif
Pekka Pessi's avatar
Pekka Pessi committed
100 101 102 103 104 105 106 107
  su_addrinfo_t hints[1] = {{ 0 }};
  su_addrinfo_t *ai, *res = NULL;
  char const *host, *service;
  int error;

  for (;;) {
    switch(getopt(argc, argv, "ndp4c")) {
    case '4': hints->ai_family = AF_INET; break;
108
#if SU_HAVE_IN6
Pekka Pessi's avatar
Pekka Pessi committed
109
    case '6': hints->ai_family = AF_INET6; break;
110
#endif
Pekka Pessi's avatar
Pekka Pessi committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
    case 'p': hints->ai_flags |= AI_PASSIVE; break;
    case 'n': hints->ai_flags |= AI_NUMERICHOST; break;
    case 'c': hints->ai_flags |= AI_CANONNAME; break;

    case -1:
      goto main;

    default:
      usage();
    }
  }

 main:
  if (optind + 1 >= argc)
    usage();

  service = argv[optind++];
  host = argv[optind++];

  su_init();

  if ((error = su_getaddrinfo(host, service, hints, &res)) == 0) {
    for (ai = res; ai; ai = ai->ai_next) {
      su_sockaddr_t const *su = (su_sockaddr_t const *)ai->ai_addr;
      unsigned port;

137
#if SU_HAVE_IN6
Pekka Pessi's avatar
Pekka Pessi committed
138 139
      if (su->su_family != AF_INET6 && su->su_family != AF_INET)
	continue;
140 141 142 143
#else
      if (su->su_family != AF_INET)
	continue;
#endif
Pekka Pessi's avatar
Pekka Pessi committed
144 145

      port = ntohs(su->su_port);
146
      su_inet_ntop(ai->ai_family, SU_ADDR(su), buffer, sizeof(buffer));
Pekka Pessi's avatar
Pekka Pessi committed
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
      printf("%d@[%s]:%u", ai->ai_protocol, buffer, port);

      if (ai->ai_flags & AI_CANONNAME)
        printf(" canon=%s", ai->ai_canonname);

      puts("");
    }
    su_freeaddrinfo(res);
  }
  else {
    fprintf(stderr, "addrinfo: %s\n", su_gai_strerror(error));
    error = 1;
  }

  su_deinit();

  return error;
}