su_localinfo.c 42.8 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 25 26 27 28 29 30
 * 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
 *
 */

/**@ingroup su_socket
 * @CFILE su_localinfo.c
 *
 * Obtain list of local addresses.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
31
 * @author Martti Mela <Martti.Mela@nokia.com>
32
 *
Pekka Pessi's avatar
Pekka Pessi committed
33 34 35 36 37 38 39 40 41
 * @date Created: Wed Oct  4 14:09:29 EET 2000 ppessi
 */

#include "config.h"

#if HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#endif

42 43 44 45
#include <sofia-sip/su.h>
#include <sofia-sip/su_localinfo.h>
#include "su_module_debug.h"

46 47 48 49 50
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stddef.h>
51 52
#include <sys/types.h>

53 54 55 56 57 58 59
#if HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#if HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif
#if HAVE_SYS_IOCTL_H
60
#include <sys/ioctl.h>
61
#endif
Pekka Pessi's avatar
Pekka Pessi committed
62
#if HAVE_NET_IF_H
63 64 65
#include <net/if.h>
#endif
#if HAVE_NET_IF_TYPES_H
Martti Mela's avatar
Martti Mela committed
66
#include <net/if_types.h>
Pekka Pessi's avatar
Pekka Pessi committed
67 68
#endif

69 70 71 72 73 74 75 76
#if HAVE_GETIFADDRS

#define USE_LOCALINFO0 1
#define localinfo0 bsd_localinfo
static int bsd_localinfo(su_localinfo_t const *, su_localinfo_t **);

#elif HAVE_IPHLPAPI_H

Pekka Pessi's avatar
Pekka Pessi committed
77 78
#include <iphlpapi.h>
#define USE_LOCALINFO0 1
79 80 81
#define localinfo0 win_localinfo
static int win_localinfo(su_localinfo_t const *, su_localinfo_t **);

Pekka Pessi's avatar
Pekka Pessi committed
82
#else
83

Pekka Pessi's avatar
Pekka Pessi committed
84 85 86 87 88
#undef USE_LOCALINFO0
static int localinfo4(su_localinfo_t const *, su_localinfo_t **);
#  if SU_HAVE_IN6
static int localinfo6(su_localinfo_t const *, su_localinfo_t **);
#  endif
89

Pekka Pessi's avatar
Pekka Pessi committed
90 91 92 93 94 95 96 97
#endif

static int li_name(su_localinfo_t const*, int, su_sockaddr_t const*, char **);
static void li_sort(su_localinfo_t *i, su_localinfo_t **rresult);
static int li_scope4(uint32_t ip4);

/** @brief Request local address information.
 *
98 99 100 101 102 103
 * Gather the network interfaces and the addresses corresponding to them,
 * check if they match to the search criteria specifed by @a hints and
 * return a list of matching local address information in the @a
 * return_localinfo. The local address information may include IPv4/IPv6
 * addresses, interface name, interface index, address scope, and domain
 * names corresponding to the local addresses.
Pekka Pessi's avatar
Pekka Pessi committed
104
 *
105 106
 * @param[in] hints specifies selection criteria
 * @param[out] return_localinfo   return list of local addresses
Pekka Pessi's avatar
Pekka Pessi committed
107 108 109 110 111 112 113 114 115 116 117
 *
 * @par Selection criteria - hints
 *
 * The selection criteria @a hints is used to select which addresses are
 * returned and what kind of information is included in the @a res list.
 *
 * @par Selection by flags - hints->li_flags
 *
 * The @a hints->li_flags contain flags, which can be combined with bit-wise
 * or.  The currently defined flags are as follows:
 *
118
 * - #LI_V4MAPPED: when returning IPv4 addresses, map them as IPv6
Pekka Pessi's avatar
Pekka Pessi committed
119 120
 *   addresses.  If this flag is specified, IPv4 addresses are returned even
 *   if @a hints->li_family is set to @c AF_INET6.
121
 * - #LI_CANONNAME: return the domain name (DNS PTR) corresponding to the
Pekka Pessi's avatar
Pekka Pessi committed
122
 *   local address in @a li_canonname.
123
 * - #LI_NAMEREQD: Do not return addresses not in DNS.
124
 * - #LI_NUMERIC: instead of domain name, return the text presentation of
Pekka Pessi's avatar
Pekka Pessi committed
125
 *   the addresss in @a li_canonname.
126 127 128
 * - #LI_DOWN: include interfaces and their addresses even if the interfaces
 *   are down. New in @VERSION_1_12_2.
 * - #LI_IFNAME: return the interface name in @a li_ifname.
Pekka Pessi's avatar
Pekka Pessi committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
 *
 * @par Selection by address family - hints->li_family
 *
 * The address family can have three values: 0, AF_INET and AF_INET6.  If
 * address family @a hints->li_family, both IPv4 and IPv6 addresses are
 * returned.
 *
 * @par Selection by interface index - hints->li_index
 *
 * If the field @a hints->li_index is non-zero, only the addresses assigned
 * to the interface with given index are returned.  The list of interface
 * indices and names can be obtained by the function @c su_if_names().
 *
 * @par Selection by interface name - hints->li_ifname
 *
 * If the field @a hints->li_ifname is not NULL, only the addresses assigned
 * to the named interface are returned.  The list of interface names can be
 * obtained by the function @c su_if_names().
 *
 * @par Selection by address scope - hints->li_scope
 *
 * If the field @a hints->li_scope is nonzero, only the addresses with
151 152 153
 * matching scope are returned. The different address scopes can be combined
 * with bitwise or. They are defined as follows
 * - #LI_SCOPE_HOST: host-local address, valid within host (::1, 127.0.0.1/8)
154 155
 * - #LI_SCOPE_LINK: link-local address, valid within link
 *   (IP6 addresses with prefix fe80::/10,
156 157 158
 *    IP4 addresses in net 169.254.0.0/16).
 * - #LI_SCOPE_SITE: site-local address, addresses valid within organization
 *   (IPv6 addresses with prefix  fec::/10,
159
 *    private IPv4 addresses in nets 10.0.0.0/8, 172.16.0.0/12,
160 161
 *    and 192.168.0.0/16 as defined in @RFC1918)
 * - #LI_SCOPE_GLOBAL: global address.
Pekka Pessi's avatar
Pekka Pessi committed
162
 *
163 164 165 166 167
 * For instance, setting @a hints->li_scope to @c LI_SCOPE_GLOBAL | @c
 * LI_SCOPE_SITE, both the @e global and @e site-local addresses are
 * returned.
 *
 * @sa @RFC1918, @RFC4291, su_sockaddr_scope()
Pekka Pessi's avatar
Pekka Pessi committed
168 169 170 171 172 173
 *
 * @par Selection by domain name - hints->li_canonname
 *
 * If this field is non-null, the domain name (DNS PTR) corresponding to
 * local IP addresses should match to the name given in this field.
 *
174 175
 * @return Zero (#ELI_NOERROR) when successful, or negative error code when
 * failed.
176
 *
177 178 179
 * @par Diagnostics
 * Use su_gli_strerror() in order to obtain a string describing the error
 * code returned by su_getlocalinfo().
Pekka Pessi's avatar
Pekka Pessi committed
180 181
 *
 */
182
int su_getlocalinfo(su_localinfo_t const *hints,
183
		    su_localinfo_t **return_localinfo)
Pekka Pessi's avatar
Pekka Pessi committed
184 185 186 187 188
{
  int error = 0, ip4 = 0, ip6 = 0;
  su_localinfo_t *result = NULL, **rr = &result;
  su_localinfo_t hh[1] = {{ 0 }};

189
  assert(return_localinfo);
Pekka Pessi's avatar
Pekka Pessi committed
190

191
  *return_localinfo = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
192 193 194 195 196 197

  if (hints) {
    /* Copy hints so that it can be modified */
    *hh = *hints;
    if (hh->li_canonname)
      hh->li_flags |= LI_CANONNAME;
198 199
    if ((hh->li_flags & LI_IFNAME) && hh->li_ifname == NULL)
      return ELI_BADHINTS;
Pekka Pessi's avatar
Pekka Pessi committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
  }

  switch (hh->li_family) {
#if SU_HAVE_IN6
  case AF_INET6:
    if (hh->li_flags & LI_V4MAPPED)
      ip6 = ip4 = 1, hh->li_family = 0;
    else
      ip6 = 1;
    break;
#endif

  case AF_INET:
    ip4 = 1;
    break;

  case 0:
217
    ip6 = ip4 = 1;
Pekka Pessi's avatar
Pekka Pessi committed
218 219 220 221 222 223
    break;

  default:
    return -1;
  }

224
#if USE_LOCALINFO0
225
  error = localinfo0(hh, rr);
Pekka Pessi's avatar
Pekka Pessi committed
226
#else
227

Pekka Pessi's avatar
Pekka Pessi committed
228 229 230
#  if SU_HAVE_IN6
  if (ip6) {
    error = localinfo6(hh, rr);
231 232
    if (error == ELI_NOADDRESS && ip4)
      error = 0;
233 234

    if (!error)
235 236 237
      /* Search end of list */
      for (; *rr; rr = &(*rr)->li_next)
	;
Pekka Pessi's avatar
Pekka Pessi committed
238 239
  }
#  endif
240
  if (ip4 && !error) {
Pekka Pessi's avatar
Pekka Pessi committed
241 242 243 244 245
    /* Append IPv4 addresses */
    error = localinfo4(hh, rr);
  }
#endif

246
  if (!result)
Pekka Pessi's avatar
Pekka Pessi committed
247 248
    error = ELI_NOADDRESS;

249 250 251 252 253
  if (!error)
    li_sort(result, return_localinfo);
  else
    su_freelocalinfo(result);

Pekka Pessi's avatar
Pekka Pessi committed
254 255 256
  return error;
}

257
/** Free local address information.
258 259 260 261 262 263 264
 *
 * Free a list of su_localinfo_t structures obtained with su_getlocalinfo()
 * or su_copylocalinfo() along with socket addresses and strings associated
 * with them.
 *
 * @sa su_getlocalinfo(), su_copylocalinfo(), #su_localinfo_t
 */
Pekka Pessi's avatar
Pekka Pessi committed
265 266 267 268 269 270 271 272 273 274 275 276
void su_freelocalinfo(su_localinfo_t *tbf)
{
  su_localinfo_t *li;

  for (li = tbf; li; li = tbf) {
    tbf = li->li_next;
    if (li->li_canonname)
      free(li->li_canonname);
    free(li);
  }
}

277
/** Describe su_localinfo errors.
Pekka Pessi's avatar
Pekka Pessi committed
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
 *
 * The function su_gli_strerror() returns a string describing the error
 * condition indicated by the code that was returned by the function
 * su_getlocalinfo().
 *
 * @param error error code returned by su_getlocalinfo()
 *
 * @return
 * A pointer to string describing the error condition.
 */
char const *su_gli_strerror(int error)
{
  switch (error) {
  case ELI_NOERROR:   return "No error";
  case ELI_NOADDRESS: return "No matching address";
  case ELI_MEMORY:    return "Memory allocation error";
  case ELI_FAMILY:    return "Unknown address family";
  case ELI_RESOLVER:  return "Error when resolving address";
  case ELI_SYSTEM:    return "System error";
297
  case ELI_BADHINTS:  return "Invalid value for hints";
Pekka Pessi's avatar
Pekka Pessi committed
298 299 300 301 302 303 304 305
  default:            return "Unknown error";
  }
}

/** Duplicate su_localinfo structure.
 */
su_localinfo_t *su_copylocalinfo(su_localinfo_t const *li0)
{
306
  size_t n;
Pekka Pessi's avatar
Pekka Pessi committed
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
  su_localinfo_t *li, *retval = NULL, **lli = &retval;

# define SLEN(s) ((s) ? strlen(s) + 1 : 0)

  for (; li0 ; li0 = li0->li_next) {
    n = sizeof(*li0) + li0->li_addrlen + SLEN(li0->li_ifname);
    if (!(li = calloc(1, n))) {
      su_freelocalinfo(retval);
      return NULL;
    }
    *lli = li;
    lli = &li->li_next;
    li->li_flags = li0->li_flags;
    li->li_family = li0->li_family;
    li->li_index = li0->li_index;
    li->li_scope = li0->li_scope;
    li->li_addrlen = li0->li_addrlen;
    li->li_addr = memcpy(li + 1, li0->li_addr, li0->li_addrlen);

    if (li0->li_canonname) {
      if (!(li->li_canonname = malloc(SLEN(li0->li_canonname)))) {
	su_freelocalinfo(retval);
	return NULL;
      }
      strcpy(li->li_canonname, li0->li_canonname);
    }

    if (li0->li_ifname)
335
      li->li_ifname = strcpy(li->li_addrlen + (char *)li->li_addr,
Pekka Pessi's avatar
Pekka Pessi committed
336 337 338 339 340 341 342 343
			     li0->li_ifname);
  }

  return retval;
}


/** Return IPv4 address scope */
344
static int
Pekka Pessi's avatar
Pekka Pessi committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
li_scope4(uint32_t ip4)
{
  ip4 = ntohl(ip4);

  if (0x7f000000 == (ip4 & 0xff000000))
    return LI_SCOPE_HOST;
  /* draft-ietf-zeroconf-ipv4-linklocal-02.txt - 169.254/16. */
  else if (0xa9fe0000 == (ip4 & 0xffff0000))
    return LI_SCOPE_LINK;
  /* RFC1918 - 10/8, 172.16/12, 192.168/16. */
  else if (0x0a000000 == (ip4 & 0xff000000) ||
	   0xac100000 == (ip4 & 0xfff00000) ||
	   0xc0a80000 == (ip4 & 0xffff0000))
    return LI_SCOPE_SITE;
  else
    return LI_SCOPE_GLOBAL;
}

Pekka Pessi's avatar
Pekka Pessi committed
363
#if SU_HAVE_IN6
364 365 366

#if HAVE_WINSOCK2_H
#define IN6_IS_ADDR_LOOPBACK SU_IN6_IS_ADDR_LOOPBACK
367
su_inline int
368 369 370 371
IN6_IS_ADDR_LOOPBACK(void const *ip6)
{
  uint8_t const *u = ip6;

372 373 374 375
  return
    u[0] == 0 && u[1] == 0 && u[2] == 0 && u[3] == 0 &&
    u[4] == 0 && u[5] == 0 && u[6] == 0 && u[7] == 0 &&
    u[8] == 0 && u[9] == 0 && u[10] == 0 && u[11] == 0 &&
376 377 378 379
    u[12] == 0 && u[13] == 0 && u[14] == 0 && u[15] == 1;
}
#endif

Pekka Pessi's avatar
Pekka Pessi committed
380
/** Return IPv6 address scope */
381
static int
Pekka Pessi's avatar
Pekka Pessi committed
382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
li_scope6(struct in6_addr const *ip6)
{
  if (IN6_IS_ADDR_V4MAPPED(ip6) || IN6_IS_ADDR_V4COMPAT(ip6)) {
    uint32_t ip4 = *(uint32_t *)(ip6->s6_addr + 12);
    return li_scope4(ip4);
  }
  else if (IN6_IS_ADDR_LOOPBACK(ip6))
    return LI_SCOPE_HOST;
  else if (IN6_IS_ADDR_LINKLOCAL(ip6))
    return LI_SCOPE_LINK;
  else if (IN6_IS_ADDR_SITELOCAL(ip6))
    return LI_SCOPE_SITE;
  else
    return LI_SCOPE_GLOBAL;
}
Pekka Pessi's avatar
Pekka Pessi committed
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
#endif

/** Return the scope of address in the sockaddr structure */
int su_sockaddr_scope(su_sockaddr_t const *su, socklen_t sulen)
{
  if (sulen >= (sizeof su->su_sin) && su->su_family == AF_INET)
    return li_scope4(su->su_sin.sin_addr.s_addr);

#if SU_HAVE_IN6
  if (sulen >= (sizeof su->su_sin6) && su->su_family == AF_INET6)
    return li_scope6(&su->su_sin6.sin6_addr);
#endif

  return 0;
}

413 414 415 416
#if HAVE_OPEN_C
extern int su_get_local_ip_addr(su_sockaddr_t *su);
#endif

Pekka Pessi's avatar
Pekka Pessi committed
417
#if USE_LOCALINFO0
Pekka Pessi's avatar
Pekka Pessi committed
418 419

#elif HAVE_IFCONF
420
#if __APPLE_CC__
Pekka Pessi's avatar
Pekka Pessi committed
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
/** Build a list of local IPv4 addresses and append it to *rresult. */
static
int localinfo4(su_localinfo_t const *hints, su_localinfo_t **rresult)
{
  su_localinfo_t *tbf = NULL, **lli = &tbf;
  su_localinfo_t *li = NULL, *li_first = NULL;
  su_sockaddr_t *su;
  int error = ELI_NOADDRESS;
  char *canonname = NULL;
  su_socket_t s;

#if SU_HAVE_IN6
  int su_xtra = (hints->li_flags & LI_V4MAPPED) ? sizeof(*su) : 0;
#else
  int const su_xtra = 0;
#endif

  struct ifconf ifc;
  int numifs;
  char *buffer;
  struct ifreq *ifr, *ifr_next;

443 444 445 446
  su_sockaddr_t *sa;
  socklen_t salen = sizeof(*sa);
  int scope = 0, gni_flags = 0;

Pekka Pessi's avatar
Pekka Pessi committed
447 448
  s = su_socket(AF_INET, SOCK_DGRAM, 0);
  if (s == -1) {
449
    SU_DEBUG_1(("su_localinfo: su_socket failed: %s\n",
Pekka Pessi's avatar
Pekka Pessi committed
450 451 452 453
		su_strerror(su_errno())));
    return ELI_SYSTEM;
  }

454

455 456
  li = calloc(1, sizeof(su_localinfo_t));
  sa = calloc(1, sizeof(su_sockaddr_t));
457

458 459 460 461 462
  error = getsockname(s, (struct sockaddr *) sa, &salen);
  if (error < 0 && errno == SOCKET_ERROR) {
    SU_DEBUG_1(("%s: getsockname() failed: %s\n", __func__,
                su_strerror(su_errno())));
  }
463

464
  error = bind(s, (struct sockaddr *) sa, salen);
465

466 467 468 469 470
  if (error < 0) {
    SU_DEBUG_1(("%s: bind() failed: %s\n", __func__,
                su_strerror(su_errno())));
    goto err;
  }
471

472
  su_close(s);
473

474
  scope = li_scope4(sa->su_sin.sin_addr.s_addr);
475

476 477
  if (scope == LI_SCOPE_HOST || scope == LI_SCOPE_LINK)
    gni_flags = NI_NUMERICHOST;
478

479 480 481 482 483
  if (su_xtra) {
    /* Map IPv4 address to IPv6 address */
    memset(sa, 0, sizeof(*sa));
    sa->su_family = AF_INET6;
    ((int32_t*)&sa->su_sin6.sin6_addr)[3] = sa->su_sin.sin_addr.s_addr;
484
      ((int32_t*)&sa->su_sin6.sin6_addr)[2] = htonl(0xffff);
485
  }
486

487 488 489 490 491
  li->li_family = sa->su_family;
  li->li_scope = scope;
  li->li_index = 0;
  li->li_addrlen = su_sockaddr_size(sa);
  li->li_addr = sa;
492

493 494
  if ((error = li_name(hints, gni_flags, sa, &canonname)) < 0)
    goto err;
495

496 497 498 499 500 501 502
  if (canonname) {
    if (strchr(canonname, ':') ||
       strspn(canonname, "0123456789.") == strlen(canonname))
	  li->li_flags |= LI_NUMERIC;
  }
  else
    li->li_flags = 0;
503

504
  li->li_canonname = canonname;
505

506
  canonname = NULL;
507

508 509
  *rresult = li;
  return 0;
510

511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534
err:
  if (canonname) free(canonname);
  if (li_first) free(li_first);
  su_freelocalinfo(tbf);
  su_close(s);

  return error;
}
#else /* !__APPLE_CC__ */
/** Build a list of local IPv4 addresses and append it to *rresult. */
static
int localinfo4(su_localinfo_t const *hints, su_localinfo_t **rresult)
{
  su_localinfo_t *tbf = NULL, **lli = &tbf;
  su_localinfo_t *li = NULL, *li_first = NULL;
  su_sockaddr_t *su;
  int error = ELI_NOADDRESS;
  char *canonname = NULL;
  su_socket_t s;

#if SU_HAVE_IN6
  int su_xtra = (hints->li_flags & LI_V4MAPPED) ? sizeof(*su) : 0;
#else
  int const su_xtra = 0;
535 536
#endif

537 538 539 540 541 542 543 544 545 546 547 548
  struct ifconf ifc;
  int numifs;
  char *buffer;
  struct ifreq *ifr, *ifr_next;

#if HAVE_OPEN_C
    su_sockaddr_t *sa;
    socklen_t salen = sizeof(*sa);
#endif

  s = su_socket(AF_INET, SOCK_DGRAM, 0);
  if (s == -1) {
549
    SU_DEBUG_1(("su_localinfo: su_socket failed: %s\n",
550 551 552
		su_strerror(su_errno())));
    return ELI_SYSTEM;
  }
553

Pekka Pessi's avatar
Pekka Pessi committed
554 555 556 557
# if HAVE_IFNUM
  /* Get the list of known IP address from the kernel */
  if (ioctl(s, SIOCGIFNUM, (char *) &numifs) < 0) {
    /* can't get number of interfaces -- fall back */
558
    SU_DEBUG_1(("su_localinfo: SIOCGIFNUM failed: %s\n",
Pekka Pessi's avatar
Pekka Pessi committed
559 560 561 562 563
		su_strerror(su_errno())));
    error = ELI_SYSTEM;
    goto err;
  }

564
  SU_DEBUG_9(("su_localinfo: %d active interfaces according to SIOCGIFNUM\n",
Pekka Pessi's avatar
Pekka Pessi committed
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
	      numifs));

  if (numifs < 0)
# endif
    /* Default to 64 interfaces. Enough? */
    numifs = 64;

  if (numifs == 0)
    return 0;

  /*
   * Allocate memory for SIOCGIFCONF ioctl buffer. This memory block is also
   * used as li_first, first localinfo struct that is returned, so it can be
   * freed by freelocalinfo() without any complications.
   */
  ifc.ifc_len = numifs * sizeof (struct ifreq);
  buffer = malloc(sizeof(su_localinfo_t) + ifc.ifc_len + su_xtra);
  if (!buffer) {
    SU_DEBUG_1(("su_localinfo: memory exhausted\n"));
    error = ELI_MEMORY;
    goto err;
  }
587

Pekka Pessi's avatar
Pekka Pessi committed
588 589 590
  li_first = (su_localinfo_t *)buffer;
  memset(li_first, 0, sizeof(su_localinfo_t) + su_xtra);
  ifc.ifc_buf = buffer + sizeof(su_localinfo_t) + su_xtra;
591 592
#if HAVE_OPEN_C
  if (ioctl(s, SIOCGIFACTIVECONF, (char *)&ifc) < 0) {
593
    SU_DEBUG_1(("su_localinfo: SIOCGIFCONF failed: %s\n",
594 595 596 597 598
		su_strerror(su_errno())));
    error = ELI_SYSTEM;
    goto err;
  }
#else
Pekka Pessi's avatar
Pekka Pessi committed
599
  if (ioctl(s, SIOCGIFCONF, (char *)&ifc) < 0) {
600
    SU_DEBUG_1(("su_localinfo: SIOCGIFCONF failed: %s\n",
Pekka Pessi's avatar
Pekka Pessi committed
601 602 603 604
		su_strerror(su_errno())));
    error = ELI_SYSTEM;
    goto err;
  }
605
#endif
Pekka Pessi's avatar
Pekka Pessi committed
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630

  buffer = ifc.ifc_buf + ifc.ifc_len;

  for (ifr = ifc.ifc_req;
       (void *)ifr < (void *)buffer;
       ifr = ifr_next) {
    struct ifreq ifreq[1];
    int scope, if_index, flags = 0, gni_flags = 0;
    char *if_name;
    su_sockaddr_t su2[1];

#if SA_LEN
    if (ifr->ifr_addr.sa_len > sizeof(ifr->ifr_addr))
      ifr_next = (struct ifreq *)
	(ifr->ifr_addr.sa_len + (char *)(&ifr->ifr_addr));
    else
#else
      ifr_next = ifr + 1;
#endif

    if_name = ifr->ifr_name;

#if defined(SIOCGIFINDEX)
    ifreq[0] = *ifr;
    if (ioctl(s, SIOCGIFINDEX, ifreq) < 0) {
631
      SU_DEBUG_1(("su_localinfo: SIOCGIFINDEX failed: %s\n",
Pekka Pessi's avatar
Pekka Pessi committed
632 633 634 635 636 637 638 639 640 641 642 643 644
		  su_strerror(su_errno())));
      error = ELI_SYSTEM;
      goto err;
    }
#if HAVE_IFR_INDEX
    if_index = ifreq->ifr_index;
#elif HAVE_IFR_IFINDEX
    if_index = ifreq->ifr_ifindex;
#else
#error Unknown index field in struct ifreq
#endif

#else
645 646
#warning su_localinfo() cannot map interface name to number
    if_index = 0;
Pekka Pessi's avatar
Pekka Pessi committed
647 648 649 650
#endif

    SU_DEBUG_9(("su_localinfo: if %s with index %d\n", if_name, if_index));

651 652 653 654 655 656 657 658 659

#if HAVE_OPEN_C
    su_close(s);

    li = calloc(1, sizeof(su_localinfo_t));
    sa = calloc(1, sizeof(su_sockaddr_t));

    if (su_get_local_ip_addr(sa) < 0)
      goto err;
660

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
    li->li_family = sa->su_family;
    li->li_scope = LI_SCOPE_GLOBAL /* scope */;
    li->li_index = if_index;
    li->li_addrlen = su_sockaddr_size(sa);
    li->li_addr = sa;

    if ((error = li_name(hints, gni_flags, sa, &canonname)) < 0)
      goto err;

    if (canonname) {
      if (strchr(canonname, ':') ||
	  strspn(canonname, "0123456789.") == strlen(canonname))
	    li->li_flags |= LI_NUMERIC;
    }
    else
      li->li_flags = 0;

    li->li_canonname = canonname;

    canonname = NULL;

    *rresult = li;

    return 0;
#endif

Pekka Pessi's avatar
Pekka Pessi committed
687 688 689
#if defined(SIOCGIFFLAGS)
    ifreq[0] = *ifr;
    if (ioctl(s, SIOCGIFFLAGS, ifreq) < 0) {
690
      SU_DEBUG_1(("su_localinfo: SIOCGIFFLAGS failed: %s\n",
Pekka Pessi's avatar
Pekka Pessi committed
691 692 693 694
		  su_strerror(su_errno())));
      error = ELI_SYSTEM;
      goto err;
    }
695 696
    /* Do not include interfaces that are down unless explicitly asked */
    if ((ifreq->ifr_flags & IFF_UP) == 0 && (hints->li_flags & LI_DOWN) == 0) {
697
      SU_DEBUG_9(("su_localinfo: if %s with index %d is down\n",
698
		  if_name, if_index));
Pekka Pessi's avatar
Pekka Pessi committed
699 700
      continue;
    }
701 702
#elif defined(SIOCGIFACTIVECONF)
/* Handled above in SIOCGIFACTIVECONF vs. SIOCGIFCONF*/
Pekka Pessi's avatar
Pekka Pessi committed
703 704 705 706 707 708 709 710
#else
#error su_localinfo() cannot determine interface status
#endif

#if 0
    *ifreq = *ifr;
    ifreq->ifr_addr.sa_family = AF_INET;
    if (ioctl(s, SIOCGIFADDR, ifreq) < 0) {
711
      SU_DEBUG_1(("su_localinfo: SIOCGIFADDR failed: %s\n",
Pekka Pessi's avatar
Pekka Pessi committed
712 713 714 715 716 717 718 719
		  su_strerror(su_errno())));
      error = ELI_SYSTEM;
      goto err;
    }
    ifr->ifr_addr = ifreq->ifr_addr;
#endif

    su = (su_sockaddr_t *)&ifr->ifr_addr;
720 721 722 723

    if (SU_HAS_INADDR_ANY(su))
      continue;

Pekka Pessi's avatar
Pekka Pessi committed
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
    scope = li_scope4(su->su_sin.sin_addr.s_addr);

    if ((hints->li_scope && (hints->li_scope & scope) == 0) ||
	(hints->li_ifname && strcmp(hints->li_ifname, if_name) != 0) ||
	(hints->li_index && hints->li_index != if_index))
      continue;

#if SU_HAVE_IN6
    if (su_xtra) {
      /* Map IPv4 address to IPv6 address */
      memset(su2, 0, sizeof(*su2));
      su2->su_family = AF_INET6;
      ((int32_t*)&su2->su_sin6.sin6_addr)[2] = htonl(0xffff);
      ((int32_t*)&su2->su_sin6.sin6_addr)[3] = su->su_sin.sin_addr.s_addr;
      su = su2;
    }
#endif

    if (scope == LI_SCOPE_HOST || scope == LI_SCOPE_LINK)
      gni_flags = NI_NUMERICHOST;

    if ((error = li_name(hints, gni_flags, su, &canonname)) < 0)
      goto err;
    else if (error > 0)
      continue;
749

Pekka Pessi's avatar
Pekka Pessi committed
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795
    if (canonname)
      if (strchr(canonname, ':') ||
	  strspn(canonname, "0123456789.") == strlen(canonname))
	flags |= LI_NUMERIC;

    if (li_first)
      li = li_first;      /* Use li_first with all ifr structs to be freed */
    else if (!(li = calloc(1, (sizeof *li) + su_xtra))) {
      error = ELI_MEMORY;
      goto err;
    }
    if (!tbf) tbf = li;
    *lli = li; lli = &li->li_next;

    if (su_xtra)
      su = (su_sockaddr_t *)memcpy(li + 1, su, su_xtra);

    li->li_flags = flags;
    li->li_family = su->su_family;
    li->li_scope = scope;
    li->li_index = if_index;
    li->li_addrlen = su_sockaddr_size(su);
    li->li_addr = su;
    li->li_canonname = canonname;
    if (hints->li_flags & LI_IFNAME)
      li->li_ifname = if_name;

    canonname = NULL;
    li_first = NULL;
  }

  if (canonname) free(canonname);
  if (li_first) free(li_first);
  su_close(s);

  if (tbf) *rresult = tbf;
  return 0;

err:
  if (canonname) free(canonname);
  if (li_first) free(li_first);
  su_freelocalinfo(tbf);
  su_close(s);

  return error;
}
796
#endif /* __APPLE_CC__ */
Pekka Pessi's avatar
Pekka Pessi committed
797 798 799 800 801 802 803 804 805 806 807 808 809
#else
static
int localinfo4(su_localinfo_t const *hints, su_localinfo_t **rresult)
{
  /* Kikka #3: resolve hostname */
  char hostname[SU_MAXHOST] = "";
  char *name, *ifname;
  struct hostent *h;
  int i, flags, error, gni_flags = 0;
  su_localinfo_t *tbf = NULL;
  su_localinfo_t *li = NULL, **lli = &tbf;
  su_sockaddr_t *su;
#if SU_HAVE_IN6
810
  socklen_t su_sockaddr_size =
Pekka Pessi's avatar
Pekka Pessi committed
811 812 813
    (hints->li_flags & LI_V4MAPPED) ? sizeof(*su) : sizeof(struct sockaddr_in);
  flags = hints->li_flags & (LI_V4MAPPED|LI_CANONNAME|LI_NUMERIC|LI_IFNAME);
#else
814
  socklen_t su_sockaddr_size = sizeof(struct sockaddr_in);
Pekka Pessi's avatar
Pekka Pessi committed
815 816 817 818 819 820 821 822 823 824
  flags = hints->li_flags & (LI_CANONNAME|LI_NUMERIC|LI_IFNAME);
#endif

  error = ELI_NOERROR;

  if (hints->li_scope == 0 || (hints->li_scope & LI_SCOPE_GLOBAL)) {
    if (hints->li_canonname)
      name = hints->li_canonname;
    else if (gethostname(name = hostname, sizeof(hostname)) != 0)
      return ELI_SYSTEM;
825

Pekka Pessi's avatar
Pekka Pessi committed
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
    h = gethostbyname(name);

    if (name)
      if (strchr(name, ':') ||
	  strspn(name, "0123456789.") == strlen(name))
	flags |= LI_NUMERIC;

    for (i = 0; h && h->h_addr_list[i]; i++) {
      if ((li = calloc(1, sizeof(*li) + su_sockaddr_size)) == NULL) {
	error = ELI_MEMORY;
	goto err;
      }
      li->li_flags = flags;

      li->li_scope = li_scope4(*(uint32_t *)h->h_addr_list[i]);
      if (li->li_scope == LI_SCOPE_HOST)
	li->li_index = 1, ifname = "lo";
      else
	li->li_index = 2, ifname = "eth";

      li->li_addrlen = su_sockaddr_size;
      li->li_addr = su = (su_sockaddr_t *)(li + 1);
848
      su->su_family = li->li_family =
Pekka Pessi's avatar
Pekka Pessi committed
849 850 851 852 853 854 855 856
	li->li_flags & LI_V4MAPPED ? AF_INET6 : AF_INET;

#if SU_HAVE_IN6
      if (li->li_flags & LI_V4MAPPED) {
	((int32_t*)&su->su_sin6.sin6_addr)[2] = htonl(0xffff);
	memcpy(&((int32_t*)&su->su_sin6.sin6_addr)[3],
	       h->h_addr_list[i], h->h_length);
      }
857
      else
Pekka Pessi's avatar
Pekka Pessi committed
858 859
#endif
	memcpy(&su->su_sin.sin_addr.s_addr, h->h_addr_list[i], h->h_length);
860
      if (li->li_flags & LI_IFNAME)
Pekka Pessi's avatar
Pekka Pessi committed
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
	li->li_ifname = ifname;
      if (li->li_scope == LI_SCOPE_HOST || li->li_scope == LI_SCOPE_LINK)
	gni_flags = NI_NUMERICHOST;
      if ((error = li_name(hints, gni_flags, su, &li->li_canonname)) < 0)
	goto err;
      else if (error > 0) {
	free(li); li = NULL; continue;
      } else
	error = ELI_NOADDRESS;
      *lli = li; lli = &li->li_next; li = NULL;
    }
  }

  if (hints->li_scope == 0 || (hints->li_scope & LI_SCOPE_HOST)) {
    if ((li = calloc(1, sizeof(*li) + su_sockaddr_size)) == NULL) {
      error = ELI_MEMORY;
      goto err;
    }
879
    li->li_flags = hints->li_flags &
Pekka Pessi's avatar
Pekka Pessi committed
880 881 882 883 884 885 886 887 888 889 890 891
      (LI_V4MAPPED|LI_CANONNAME|LI_NUMERIC|LI_IFNAME);
    li->li_scope = LI_SCOPE_HOST, li->li_index = 1;
    if (li->li_flags & LI_IFNAME)
      li->li_ifname = "lo";
    li->li_addrlen = su_sockaddr_size;
    li->li_addr = su = (su_sockaddr_t *)(li + 1);
#if SU_HAVE_IN6
    if (li->li_flags & LI_V4MAPPED) {
      su->su_family = li->li_family = AF_INET6;
      ((int32_t*)&su->su_sin6.sin6_addr)[2] = htonl(0xffff);
      ((int32_t*)&su->su_sin6.sin6_addr)[3] = htonl(0x7f000001);
    }
892
    else
Pekka Pessi's avatar
Pekka Pessi committed
893 894 895 896 897 898 899
#endif
      su->su_family = li->li_family = AF_INET,
      su->su_sin.sin_addr.s_addr = htonl(0x7f000001);

    if ((error = li_name(hints, NI_NUMERICHOST, su, &li->li_canonname)) < 0) {
      goto err;
    } else if (error > 0) {
900
      free(li); li = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920
    } else {
      *lli = li; lli = &li->li_next; li = NULL;
    }
  }

  *rresult = tbf;

  return 0;

err:
  if (li) su_freelocalinfo(li);
  su_freelocalinfo(tbf);

  return error;
}

#endif

#if USE_LOCALINFO0 || !SU_HAVE_IN6

921
#elif HAVE_PROC_NET_IF_INET6
922
/** Build a list of local IPv6 addresses and append it to *return_result. */
Pekka Pessi's avatar
Pekka Pessi committed
923
static
924
int localinfo6(su_localinfo_t const *hints, su_localinfo_t **return_result)
Pekka Pessi's avatar
Pekka Pessi committed
925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949
{
  su_localinfo_t *li = NULL;
  su_sockaddr_t su[1] = {{ 0 }}, *addr;
  int error = ELI_NOADDRESS;
  char *canonname = NULL;
  char line[80];
  FILE *f;

  if ((f = fopen("/proc/net/if_inet6", "r"))) {
    for (;error;) {
      struct in6_addr in6;
      unsigned if_index, prefix_len, scope, flags;
      int addrlen, if_namelen;
      char ifname[16];

      if (!fgets(line, sizeof(line), f)) {
	if (feof(f))
	  error = ELI_NOERROR;
	break;
      }

      if (sscanf(line, "%08x%08x%08x%08x %2x %2x %2x %02x %016s\n",
		 &in6.s6_addr32[0],
		 &in6.s6_addr32[1],
		 &in6.s6_addr32[2],
950
		 &in6.s6_addr32[3],
Pekka Pessi's avatar
Pekka Pessi committed
951 952 953 954 955 956 957 958
		 &if_index, &prefix_len, &scope, &flags, ifname) != 9)
	break;

      flags = 0;

      /* Fix global scope (it is 0) */
      if (!scope) scope = LI_SCOPE_GLOBAL;

959 960 961 962 963 964 965 966 967 968
      in6.s6_addr32[0] = htonl(in6.s6_addr32[0]);
      in6.s6_addr32[1] = htonl(in6.s6_addr32[1]);
      in6.s6_addr32[2] = htonl(in6.s6_addr32[2]);
      in6.s6_addr32[3] = htonl(in6.s6_addr32[3]);

      if (IN6_IS_ADDR_V4MAPPED(&in6) || IN6_IS_ADDR_V4COMPAT(&in6)) {
	uint32_t ip4 = *(uint32_t *)(in6.s6_addr + 12);
	scope = li_scope4(ip4);
      }

Pekka Pessi's avatar
Pekka Pessi committed
969 970 971 972
      if ((hints->li_scope && (hints->li_scope & scope) == 0) ||
	  (hints->li_index && hints->li_index != if_index) ||
	  (hints->li_ifname && strcmp(hints->li_ifname, ifname) != 0))
	continue;
973

Pekka Pessi's avatar
Pekka Pessi committed
974
      su->su_family = AF_INET6;
975 976
      su->su_sin6.sin6_addr = in6;

Pekka Pessi's avatar
Pekka Pessi committed
977 978 979 980 981 982 983 984 985
      addrlen = su_sockaddr_size(su);

      if ((error = li_name(hints, 0, su, &canonname)) < 0)
	break;
      else if (error > 0)
	continue;
      else
	error = ELI_NOADDRESS;

986
      if (canonname &&
Pekka Pessi's avatar
Pekka Pessi committed
987 988 989 990 991 992 993 994 995 996 997 998 999 1000
	  (strchr(canonname, ':') ||
	   strspn(canonname, "0123456789.") == strlen(canonname)))
	flags |= LI_NUMERIC;

      if (hints->li_flags & LI_IFNAME)
	if_namelen = strlen(ifname) + 1;
      else
	if_namelen = 0;

      if ((li = calloc(1, sizeof *li + addrlen + if_namelen)) == NULL) {
	error = ELI_MEMORY;
	break;
      }
      addr = (su_sockaddr_t*)memcpy((li + 1), su, addrlen);
1001
      *return_result = li; return_result = &li->li_next;
Pekka Pessi's avatar
Pekka Pessi committed
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011

      li->li_flags = flags;
      li->li_family = AF_INET6;
      li->li_scope = scope;
      li->li_index = if_index;
      li->li_addr = addr;
      li->li_addrlen = addrlen;
      li->li_canonname = canonname;
      if (if_namelen)
	li->li_ifname = memcpy(addrlen + (char *)addr, ifname, if_namelen);
1012

Pekka Pessi's avatar
Pekka Pessi committed
1013 1014
      canonname = NULL;
    }
1015 1016

    fclose(f);
Pekka Pessi's avatar
Pekka Pessi committed
1017 1018
  }

1019
  if (canonname)
Pekka Pessi's avatar
Pekka Pessi committed
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
    free(canonname);

  return error;
}
#else
/* Use HOSTADDR6 */
static
int localinfo6(su_localinfo_t const *hints, su_localinfo_t **rresult)
{
  char *addr, *ifname;
  int flags, error;
  su_localinfo_t *li = NULL;
  su_sockaddr_t *su;
  int const su_sockaddr_size = sizeof(*su);
1034

Pekka Pessi's avatar
Pekka Pessi committed
1035 1036
  error = ELI_NOADDRESS;

1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
#if defined(__APPLE_CC__)
  {
    su_sockaddr_t *sa;
    int salen = sizeof(*sa);
    int s;

    if (hints->li_scope == 0 || (hints->li_scope & LI_SCOPE_GLOBAL)) {
      if ((addr = getenv("HOSTADDR6"))) {

	li = calloc(1, sizeof(su_localinfo_t));
	sa = calloc(1, sizeof(su_sockaddr_t));

1049
	sa->su_family = AF_INET6;
1050
	if (su_inet_pton(AF_INET6, addr, &sa->su_sin6.sin6_addr) <= 0)
1051
	  goto err;
1052

1053 1054
	s = su_socket(AF_INET6, SOCK_DGRAM, 0);
	if (s == -1) {
1055
	  SU_DEBUG_1(("su_localinfo: su_socket failed: %s\n",
1056 1057 1058
		      su_strerror(su_errno())));
	  return ELI_SYSTEM;
	}
1059

1060 1061 1062 1063 1064
	error = getsockname(s, (struct sockaddr *) sa, &salen);
	if (error < 0 && errno == SOCKET_ERROR) {
	  SU_DEBUG_1(("%s: getsockname() failed: %s\n", __func__,
		      su_strerror(su_errno())));
	}
1065

1066
	error = bind(s, (struct sockaddr *) sa, salen);
1067

1068 1069 1070 1071 1072
	if (error < 0) {
	  SU_DEBUG_1(("%s: bind() failed: %s\n", __func__,
		      su_strerror(su_errno())));
	  goto err;
	}
1073

1074
	su_close(s);
1075

1076 1077 1078 1079 1080
	li->li_family = sa->su_family;
	li->li_scope = LI_SCOPE_GLOBAL;
	li->li_index = 0;
	li->li_addrlen = su_sockaddr_size(sa);
	li->li_addr = sa;
1081

1082 1083
	if ((error = li_name(hints, NI_NUMERICHOST, sa, &li->li_canonname)) < 0)
	  goto err;
1084

1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
	li->li_flags = NI_NUMERICHOST;
      }
    }

    *rresult = li;
    return 0;
  }
#endif


Pekka Pessi's avatar
Pekka Pessi committed
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107
  if (hints->li_scope == 0 || (hints->li_scope & LI_SCOPE_GLOBAL)) {
    if ((addr = getenv("HOSTADDR6"))) {
      flags = hints->li_flags & (LI_CANONNAME|LI_NUMERIC|LI_IFNAME);

      if ((li = calloc(1, sizeof(*li) + su_sockaddr_size)) == NULL) {
	error = ELI_MEMORY;
	goto err;
      }
      li->li_flags = flags;
      li->li_scope = LI_SCOPE_GLOBAL, li->li_index = 2, ifname = "eth";
      li->li_addrlen = sizeof(*su);
      li->li_addr = su = (su_sockaddr_t *)(li + 1);
      su->su_family = li->li_family = AF_INET6;
1108
      if (su_inet_pton(AF_INET6, addr, &su->su_sin6.sin6_addr) <= 0)
Pekka Pessi's avatar
Pekka Pessi committed
1109
	goto err;
1110
      if (li->li_flags & LI_IFNAME)
Pekka Pessi's avatar
Pekka Pessi committed
1111 1112 1113 1114
	li->li_ifname = ifname;
      if ((error = li_name(hints, NI_NUMERICHOST, su, &li->li_canonname)) < 0)
	goto err;
      else if (error > 0) {
1115
	free(li); li = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
      }
    }
  }

  *rresult = li;

  return 0;

err:
  if (li) su_freelocalinfo(li);
  return error;
}
#endif

1130 1131 1132 1133 1134 1135

#if HAVE_GETIFADDRS

#include <ifaddrs.h>

static
1136
int bsd_localinfo(su_localinfo_t const hints[1],
1137 1138 1139 1140
		  su_localinfo_t **return_result)
{
  struct ifaddrs *ifa, *results;
  int error = 0;
1141
#if SU_HAVE_IN6
1142
  int v4_mapped = (hints->li_flags & LI_V4MAPPED) != 0;
1143
#endif
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154
  char *canonname = NULL;

  if (getifaddrs(&results) < 0) {
    if (errno == ENOMEM)
      return ELI_MEMORY;
    else
      return ELI_SYSTEM;
  }

  for (ifa = results; ifa; ifa = ifa->ifa_next) {
    su_localinfo_t *li;
1155 1156 1157 1158
    su_sockaddr_t *su;
#if SU_HAVE_IN6
    su_sockaddr_t su2[1];
#endif
1159 1160 1161 1162 1163
    socklen_t sulen;
    int scope, flags = 0, gni_flags = 0, if_index = 0;
    char const *ifname = 0;
    size_t ifnamelen = 0;

1164 1165 1166 1167
    /* no ip address from if that is down */
    if ((ifa->ifa_flags & IFF_UP) == 0 && (hints->li_flags & LI_DOWN) == 0)
      continue;

1168 1169 1170 1171 1172 1173 1174 1175
    su = (su_sockaddr_t *)ifa->ifa_addr;

    if (!su)
      continue;

    if (su->su_family == AF_INET) {
      sulen = sizeof(su->su_sin);
      scope = li_scope4(su->su_sin.sin_addr.s_addr);
1176
#if SU_HAVE_IN6
1177 1178
      if (v4_mapped)
	sulen = sizeof(su->su_sin6);
1179
#endif
1180
    }
1181
#if SU_HAVE_IN6
1182 1183 1184 1185 1186 1187
    else if (su->su_family == AF_INET6) {
      if (IN6_IS_ADDR_MULTICAST(&su->su_sin6.sin6_addr))
	continue;
      sulen = sizeof(su->su_sin6);
      scope = li_scope6(&su->su_sin6.sin6_addr);
    }
1188
#endif
1189
    else
1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
      continue;

    if (hints->li_flags & LI_IFNAME) {
      ifname = ifa->ifa_name;
      if (ifname)
	ifnamelen = strlen(ifname) + 1;
    }

    if ((hints->li_scope && (hints->li_scope & scope) == 0) ||
	(hints->li_family && hints->li_family != su->su_family) ||
	(hints->li_ifname && (!ifname || strcmp(hints->li_ifname, ifname))) ||
	(hints->li_index && hints->li_index != if_index))
      continue;
1203

1204 1205 1206
    if (scope == LI_SCOPE_HOST || scope == LI_SCOPE_LINK)
      gni_flags = NI_NUMERICHOST;

1207
#if SU_HAVE_IN6
1208 1209 1210 1211 1212 1213 1214 1215
    if (v4_mapped && su->su_family == AF_INET) {
      /* Map IPv4 address to IPv6 address */
      memset(su2, 0, sizeof(*su2));
      su2->su_family = AF_INET6;
      ((int32_t*)&su2->su_sin6.sin6_addr)[2] = htonl(0xffff);
      ((int32_t*)&su2->su_sin6.sin6_addr)[3] = su->su_sin.sin_addr.s_addr;
      su = su2;
    }
1216
#endif
1217 1218 1219 1220 1221 1222 1223 1224

    if ((error = li_name(hints, gni_flags, su, &canonname)) < 0)
      break;

    if (error > 0) {
      error = 0;
      continue;
    }
1225

1226 1227 1228 1229 1230 1231 1232 1233 1234
    if (canonname)
      if (strchr(canonname, ':') ||
	  canonname[strspn(canonname, "0123456789.")] == '\0')
	flags |= LI_NUMERIC;

    if (!(li = calloc(1, sizeof(*li) + sulen + ifnamelen))) {
      SU_DEBUG_1(("su_getlocalinfo: memory exhausted\n"));
      error = ELI_MEMORY;
      break;
1235
    }
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
    *return_result = li, return_result = &li->li_next;

    li->li_flags = flags;
    li->li_family = su->su_family;
    li->li_scope = scope;
    li->li_index = if_index;
    li->li_addrlen = sulen;
    li->li_addr = memcpy(li + 1, su, sulen);
    li->li_canonname = canonname;
    if (ifnamelen) {
      li->li_ifname = strcpy((char *)(li + 1) + sulen, ifname);
    }

    canonname = NULL;
  }

  if (canonname)
    free(canonname);

  freeifaddrs(results);

  return error;
}

#elif USE_LOCALINFO0 && HAVE_IPHLPAPI_H && SU_HAVE_IN6
Pekka Pessi's avatar
Pekka Pessi committed
1261

1262
static
Pekka Pessi's avatar
Pekka Pessi committed
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291
char const *ws2ifname(DWORD iftype)
{
  switch (iftype) {
  case IF_TYPE_ETHERNET_CSMACD:         return "eth";
  case IF_TYPE_IEEE80212:               return "eth";
  case IF_TYPE_FASTETHER:               return "eth";
  case IF_TYPE_GIGABITETHERNET:         return "eth";
  case IF_TYPE_ISO88025_TOKENRING:      return "token";
  case IF_TYPE_FDDI:                    return "fddi";
  case IF_TYPE_PPP:                     return "ppp";
  case IF_TYPE_SOFTWARE_LOOPBACK:       return "lo";
  case IF_TYPE_SLIP:                    return "sl";
  case IF_TYPE_FRAMERELAY:              return "fr";
  case IF_TYPE_ATM:                     return "atm";
  case IF_TYPE_HIPPI:                   return "hippi";
  case IF_TYPE_ISDN:                    return "isdn";
  case IF_TYPE_IEEE80211:               return "wlan";
  case IF_TYPE_ADSL:                    return "adsl";
  case IF_TYPE_RADSL:                   return "radsl";
  case IF_TYPE_SDSL:                    return "sdsl";
  case IF_TYPE_VDSL:                    return "vdsl";
  case IF_TYPE_TUNNEL:                  return "tunnel";
  case IF_TYPE_IEEE1394:                return "fw";
  case IF_TYPE_OTHER:
  default:                              return "other";
  }
}

static
1292
int win_localinfo(su_localinfo_t const hints[1], su_localinfo_t **rresult)
Pekka Pessi's avatar
Pekka Pessi committed
1293 1294
{
  /* This is Windows XP code, for both IPv6 and IPv4. */
1295
  ULONG iaa_size = 2048;
Pekka Pessi's avatar
Pekka Pessi committed
1296 1297 1298 1299 1300 1301 1302 1303
  IP_ADAPTER_ADDRESSES *iaa0, *iaa;
  int error, loopback_seen = 0;
  int v4_mapped = (hints->li_flags & LI_V4MAPPED) != 0;
  char *canonname = NULL;
  su_localinfo_t *li, **next;
  int flags = GAA_FLAG_SKIP_MULTICAST;
  *rresult = NULL; next = rresult;

1304
  iaa0 = malloc((size_t)iaa_size);
Pekka Pessi's avatar
Pekka Pessi committed
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
  if (!iaa0) {
    SU_DEBUG_1(("su_localinfo: memory exhausted\n"));
    error = ELI_MEMORY;
    goto err;
  }
  error = GetAdaptersAddresses(hints->li_family, flags, NULL, iaa0, &iaa_size);
  if (error == ERROR_BUFFER_OVERFLOW) {
    if ((iaa0 = realloc(iaa0, iaa_size)))
      error = GetAdaptersAddresses(hints->li_family, flags, NULL, iaa0, &iaa_size);
  }
  if (error) {
1316 1317 1318 1319 1320 1321 1322 1323
    char const *empty = "";
    LPTSTR msg = empty;

    if (error == ERROR_NO_DATA) {
      error = ELI_NOADDRESS;
      goto err;
    }

1324 1325
    if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
	               FORMAT_MESSAGE_FROM_SYSTEM |
1326 1327 1328 1329 1330 1331
	               FORMAT_MESSAGE_IGNORE_INSERTS,
	  	       NULL,
	  	       error,
		       MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		       msg, 0, NULL))
      msg = empty;
1332

1333 1334
    SU_DEBUG_1(("su_localinfo: GetAdaptersAddresses: %s (%d)\n", msg, error));
    if (msg != empty) LocalFree((LPVOID)msg);
Pekka Pessi's avatar
Pekka Pessi committed
1335 1336 1337
    error = ELI_SYSTEM;
    goto err;
  }
1338

Pekka Pessi's avatar
Pekka Pessi committed
1339 1340
  for (iaa = iaa0; iaa; iaa = iaa->Next) {
    IP_ADAPTER_UNICAST_ADDRESS *ua;
1341
    IP_ADAPTER_UNICAST_ADDRESS lua[1];
Pekka Pessi's avatar
Pekka Pessi committed
1342
    int if_index = iaa->IfIndex;
1343
    size_t ifnamelen = 0;
Pekka Pessi's avatar
Pekka Pessi committed
1344 1345 1346 1347 1348 1349 1350 1351 1352
    char ifname[16];

    for (ua = iaa->FirstUnicastAddress; ;ua = ua->Next) {
      su_sockaddr_t *su;
      socklen_t sulen;
      su_sockaddr_t su2[1];
      int scope, flags = 0, gni_flags = 0;

      if (ua == NULL) {
1353
	/* There is no loopback interface in windows */
Pekka Pessi's avatar
Pekka Pessi committed
1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
	if (!loopback_seen && iaa->Next == NULL) {
	  struct sockaddr_in loopback_sin = { AF_INET, 0, {{ 127, 0, 0, 1 }}};

	  lua->Address.lpSockaddr = (struct sockaddr *)&loopback_sin;
	  lua->Address.iSockaddrLength = sizeof(loopback_sin);
	  lua->Next = NULL;

	  iaa->IfType = IF_TYPE_SOFTWARE_LOOPBACK;
	  if_index = 1;

	  ua = lua;
	}
	else
	  break;
      }

      su = (su_sockaddr_t *)ua->Address.lpSockaddr;
      sulen = ua->Address.iSockaddrLength;

      if (su->su_family == AF_INET) {
	scope = li_scope4(su->su_sin.sin_addr.s_addr);
	if (v4_mapped)
	  sulen = sizeof(su->su_sin6);
	if (scope == LI_SCOPE_HOST)
	  loopback_seen = 1;
      }
      else if (su->su_family == AF_INET6) {
	if (IN6_IS_ADDR_MULTICAST(&su->su_sin6.sin6_addr))
	  continue;
	scope = li_scope6(&su->su_sin6.sin6_addr);
      }
1385
      else
Pekka Pessi's avatar
Pekka Pessi committed
1386 1387 1388
	continue;

      if (hints->li_flags & LI_IFNAME) {
1389
	snprintf(ifname, sizeof(ifname), "%s%u",
Pekka Pessi's avatar
Pekka Pessi committed
1390 1391 1392 1393 1394 1395
		 ws2ifname(iaa->IfType), if_index);
	ifnamelen = strlen(ifname) + 1;
      }

      if ((hints->li_scope && (hints->li_scope & scope) == 0) ||
	  (hints->li_family && hints->li_family != su->su_family) ||
Pekka Pessi's avatar
Pekka Pessi committed
1396
	  /* (hints->li_ifname && strcmp(hints->li_ifname, ifname) != 0) || */
Pekka Pessi's avatar
Pekka Pessi committed
1397 1398
	  (hints->li_index && hints->li_index != if_index))
	continue;
1399

Pekka Pessi's avatar
Pekka Pessi committed
1400 1401 1402 1403 1404 1405 1406
      if (scope == LI_SCOPE_HOST || scope == LI_SCOPE_LINK)
	gni_flags = NI_NUMERICHOST;

      if (v4_mapped && su->su_family == AF_INET) {
	/* Map IPv4 address to IPv6 address */
	memset(su2, 0, sizeof(*su2));
	su2->su_family = AF_INET6;
Pekka Pessi's avatar
Pekka Pessi committed
1407 1408
	((int32_t*)&su2->su_sin6.sin6_addr)[2] = htonl(0xffff);
	((int32_t*)&su2->su_sin6.sin6_addr)[3] = su->su_sin.sin_addr.s_addr;
Pekka Pessi's avatar
Pekka Pessi committed
1409 1410 1411 1412 1413 1414 1415
	su = su2;
      }

      if ((error = li_name(hints, gni_flags, su, &canonname)) < 0)
	goto err;
      else if (error > 0)
	continue;
1416

Pekka Pessi's avatar
Pekka Pessi committed
1417 1418 1419 1420 1421 1422
      if (canonname)
	if (strchr(canonname, ':') ||
	    strspn(canonname, "0123456789.") == strlen(canonname))
	  flags |= LI_NUMERIC;

      if (!(li = calloc(1, sizeof(*li) + sulen + ifnamelen))) {
1423
	SU_DEBUG_1(("su_getlocalinfo: memory exhausted\n"));
Pekka Pessi's avatar
Pekka Pessi committed
1424
	error = ELI_MEMORY; goto err;
1425
      }
Pekka Pessi's avatar
Pekka Pessi committed
1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
      *next = li, next = &li->li_next;

      li->li_flags = flags;
      li->li_family = su->su_family;
      li->li_scope = scope;
      li->li_index = if_index;
      li->li_addrlen = sulen;
      li->li_addr = memcpy(li + 1, su, sulen);
      li->li_canonname = canonname;
      if (ifnamelen) {
	li->li_ifname = strcpy((char *)(li + 1) + sulen, ifname);
	/* WideCharToMultiByte(CP_ACP, 0,
			    ifname, -1, (char *)(li + 1) + sulen, ifnamelen,
			    NULL, NULL); */
      }

      canonname = NULL;
    }
  }

  if (iaa0) free(iaa0);
  return 0;

err:
  if (iaa0) free(iaa0);
  if (canonname) free(canonname);
  su_freelocalinfo(*rresult), *rresult = NULL;
  return error;
}

#elif HAVE_SIO_ADDRESS_LIST_QUERY
static
int localinfo0(su_localinfo_t const *hints, su_localinfo_t **rresult)
{
  /* This is Windows IPv4 code */
  short family = AF_INET;
  su_socket_t s;
  union {
    SOCKET_ADDRESS_LIST sal[1];
#if HAVE_INTERFACE_INFO_EX
1466
    INTERFACE_INFO_EX   ii[1];
Pekka Pessi's avatar
Pekka Pessi committed
1467 1468 1469 1470 1471
#else
    INTERFACE_INFO      ii[1];
#endif
    char buffer[2048];
  } b = {{ 1 }};
1472
  DWORD salen = sizeof(b);
Pekka Pessi's avatar
Pekka Pessi committed
1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
  int i, error = -1;
#if SU_HAVE_IN6
  int v4_mapped = (hints->li_flags & LI_V4MAPPED) != 0;
#endif
  su_localinfo_t *li, *head = NULL, **next = &head;
  char *canonname = NULL, *if_name = NULL;

  *rresult = NULL;

  s = su_socket(family, SOCK_DGRAM, 0);
1483
  if (s == INVALID_SOCKET) {
1484
    SU_DEBUG_1(("su_getlocalinfo: %s: %s\n", "su_socket",
Pekka Pessi's avatar
Pekka Pessi committed
1485 1486 1487 1488 1489 1490 1491
		            su_strerror(su_errno())));
    return -1;
  }

  /* get the list of known IP address (NT5 and up) */
  if (WSAIoctl(s, SIO_ADDRESS_LIST_QUERY, NULL, 0,
               &b, sizeof(b), &salen, NULL, NULL) == SOCKET_ERROR) {
1492
    SU_DEBUG_1(("su_getlocalinfo: %s: %s\n", "SIO_ADDRESS_LIST_QUERY",
Pekka Pessi's avatar
Pekka Pessi committed
1493 1494 1495 1496
		su_strerror(su_errno())));
    error = -1; goto err;
  }
  if (b.sal->iAddressCount < 1) {
1497
    SU_DEBUG_1(("su_getlocalinfo: no local addresses\n"));
Pekka Pessi's avatar
Pekka Pessi committed
1498 1499
    error = -1; goto err;
  }
1500

Pekka Pessi's avatar
Pekka Pessi committed
1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
  for (i = 0; i < b.sal->iAddressCount; i++) {
    su_sockaddr_t *su = (su_sockaddr_t *)b.sal->Address[i].lpSockaddr;
#if SU_HAVE_IN6
    socklen_t sulen = v4_mapped ? sizeof(*su) : b.sal->Address[i].iSockaddrLength;
    su_sockaddr_t su2[1];
#else
    socklen_t sulen = b.sal->Address[i].iSockaddrLength;
#endif
    int scope, flags = 0, gni_flags = 0;

    scope = li_scope4(su->su_sin.sin_addr.s_addr);

    if (hints->li_scope && (hints->li_scope & scope) == 0)
      continue;
1515

Pekka Pessi's avatar
Pekka Pessi committed
1516 1517 1518 1519
    if (scope == LI_SCOPE_HOST || scope == LI_SCOPE_LINK)
      gni_flags = NI_NUMERICHOST;

    if (!(li = calloc(1, sizeof(*li) + sulen))) {
1520
      SU_DEBUG_1(("su_getlocalinfo: memory exhausted\n"));
Pekka Pessi's avatar
Pekka Pessi committed
1521 1522 1523 1524 1525 1526 1527 1528 1529
      error = -1; goto err;
    }
    *next = li, next = &li->li_next;

#if SU_HAVE_IN6
    if (v4_mapped) {
      /* Map IPv4 address to IPv6 address */
      memset(su2, 0, sizeof(*su2));
      su2->su_family = AF_INET6;
Pekka Pessi's avatar
Pekka Pessi committed
1530 1531
      ((int32_t*)&su2->su_sin6.sin6_addr)[2] = htonl(0xffff);
      ((int32_t*)&su2->su_sin6.sin6_addr)[3] = su->su_sin.sin_addr.s_addr;
Pekka Pessi's avatar
Pekka Pessi committed
1532 1533 1534 1535 1536 1537 1538 1539
      su = su2;
    }
#endif

    if ((error = li_name(hints, gni_flags, su, &canonname)) < 0)
      goto err;
    else if (error > 0)
      continue;
1540

Pekka Pessi's avatar
Pekka Pessi committed
1541 1542 1543 1544
    if (canonname)
      if (strchr(canonname, ':') ||
	  strspn(canonname, "0123456789.") == strlen(canonname))
	flags |= LI_NUMERIC;
1545

Pekka Pessi's avatar
Pekka Pessi committed
1546 1547 1548 1549 1550 1551 1552 1553 1554
    li->li_flags = flags;
    li->li_family = su->su_family;
    li->li_scope = scope;
    li->li_index = i;
    li->li_addrlen = su_sockaddr_size(su);
    li->li_addr = su;
    li->li_canonname = canonname, canonname = NULL;
    if (hints->li_flags & LI_IFNAME)
      li->li_ifname = if_name;
1555
    li->li_addr = (su_sockaddr_t *)(li + 1);
Pekka Pessi's avatar
Pekka Pessi committed
1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573
    li->li_addrlen = sulen;
    memcpy(li->li_addr, su, sulen);
  }

  *rresult = head;
  su_close(s);

  return 0;

err:
  if (canonname) free(canonname);
  su_freelocalinfo(head);
  su_close(s);

  return error;
}
#endif

1574 1575
static
int li_name(su_localinfo_t const *hints,
Pekka Pessi's avatar
Pekka Pessi committed
1576
	    int gni_flags,
1577
	    su_sockaddr_t const *su,
Pekka Pessi's avatar
Pekka Pessi committed
1578 1579 1580 1581 1582 1583 1584 1585 1586 1587
	    char **ccanonname)
{
  char name[SU_MAXHOST];
  int error;
  int flags = hints->li_flags;

  *ccanonname = NULL;

  if ((flags & LI_CANONNAME) || hints->li_canonname) {
    if ((flags & LI_NAMEREQD) == LI_NAMEREQD)
1588
      gni_flags |= NI_NAMEREQD;
Pekka Pessi's avatar
Pekka Pessi committed
1589 1590 1591
    if (flags & LI_NUMERIC)
      gni_flags |= NI_NUMERICHOST;

1592
    error = su_getnameinfo(su, su_sockaddr_size(su),
1593 1594
			   name, sizeof(name), NULL, 0,
			   gni_flags);
Pekka Pessi's avatar
Pekka Pessi committed
1595 1596 1597
    if (error) {
      if ((flags & LI_NAMEREQD) == LI_NAMEREQD)
	return 1;
1598
      SU_DEBUG_7(("li_name: getnameinfo() failed\n"));
Michael Jerris's avatar
Michael Jerris committed
1599
      if (!su_inet_ntop(su->su_family, SU_ADDR(su), name, sizeof name))
1600
	return ELI_RESOLVER;
Pekka Pessi's avatar
Pekka Pessi committed
1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619
    }

    if (hints->li_canonname && strcasecmp(name, hints->li_canonname))
      return 1;

    if (!(flags & LI_CANONNAME))
      return 0;

    if (!(*ccanonname = strdup(name)))
      return ELI_MEMORY;
  }
  return 0;
}

static
void li_sort(su_localinfo_t *i, su_localinfo_t **rresult)
{
  su_localinfo_t *li, **lli;

1620 1621 1622 1623 1624 1625
#define LI_MAPPED(li) \
  ((li)->li_family == AF_INET6 &&					\
   (IN6_IS_ADDR_V4MAPPED(&(li)->li_addr->su_sin6.sin6_addr) ||		\
    IN6_IS_ADDR_V4COMPAT(&(li)->li_addr->su_sin6.sin6_addr)))

  /* Sort addresses according to scope (and mappedness) */
Pekka Pessi's avatar
Pekka Pessi committed
1626 1627
  for (li = i; li; li = i) {
    i = li->li_next;
1628
    for (lli = rresult; *lli; lli = &(*lli)->li_next) {
Pekka Pessi's avatar
Pekka Pessi committed
1629 1630
      if ((*lli)->li_scope < li->li_scope)
	break;
1631
#if SU_HAVE_IN6
1632 1633
      if (LI_MAPPED(*lli) > LI_MAPPED(li))
	break;
1634
#endif
1635
    }
Pekka Pessi's avatar
Pekka Pessi committed
1636 1637 1638 1639 1640 1641 1642 1643
    li->li_next = *lli;
    *lli = li;
  }
}

/**Get local IP address.
 *
 * @deprecated
1644
 * Use su_getlocalinfo() instead.
Pekka Pessi's avatar
Pekka Pessi committed
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
 */
int su_getlocalip(su_sockaddr_t *sa)
{
  su_localinfo_t *li = NULL, hints[1] = {{ 0 }};

  hints->li_family = sa->su_sa.sa_family ? sa->su_sa.sa_family : AF_INET;

  if (su_getlocalinfo(hints, &li) == 0) {
    memcpy(sa, li->li_addr, li->li_addrlen);
    su_freelocalinfo(li);
    return 0;
  }
1657
  else
Pekka Pessi's avatar
Pekka Pessi committed
1658 1659
    return -1;
}