tport.c 107 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 31 32
 * 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
 *
 */

/**@CFILE tport.c Transport interface implementation.
 *
 * See tport.docs for more detailed description of tport interface.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * @author Ismo Puustinen <Ismo.H.Puustinen@nokia.com>
 * @author Tat Chan <Tat.Chan@nokia.com>
 * @author Kai Vehmanen <kai.vehmanen@nokia.com>
Martti Mela's avatar
Martti Mela committed
33
 * @author Martti Mela <Martti.Mela@nokia.com>
Pekka Pessi's avatar
Pekka Pessi committed
34 35 36 37 38 39
 *
 * @date Created: Thu Jul 20 12:54:32 2000 ppessi
 */

#include "config.h"

40 41
#include <sofia-sip/string0.h>
#include <sofia-sip/su.h>
42
#include <sofia-sip/su_errno.h>
43 44 45
#include <sofia-sip/su_alloc.h>
#include <sofia-sip/su_tagarg.h>
#include <sofia-sip/su_localinfo.h>
Pekka Pessi's avatar
Pekka Pessi committed
46 47 48

typedef struct tport_nat_s tport_nat_t;

49 50 51
#define SU_WAKEUP_ARG_T         struct tport_s
#define SU_TIMER_ARG_T          struct tport_master
#define SU_MSG_ARG_T            union tport_su_msg_arg
52

53
#include <sofia-sip/su_wait.h>
Pekka Pessi's avatar
Pekka Pessi committed
54

55 56
#include <sofia-sip/msg.h>
#include <sofia-sip/msg_addr.h>
Pekka Pessi's avatar
Pekka Pessi committed
57

58 59 60 61 62
#include <stdlib.h>
#include <time.h>
#include <assert.h>
#include <errno.h>
#include <limits.h>
Pekka Pessi's avatar
Pekka Pessi committed
63 64 65 66 67

#ifndef IPPROTO_SCTP
#define IPPROTO_SCTP (132)
#endif

68 69 70
#include "sofia-sip/tport.h"
#include "sofia-sip/su_uniqueid.h"
#include <sofia-sip/rbtree.h>
Pekka Pessi's avatar
Pekka Pessi committed
71

72 73
#include "tport_internal.h"

Pekka Pessi's avatar
Pekka Pessi committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
#if HAVE_FUNC
#elif HAVE_FUNCTION
#define __func__ __FUNCTION__
#else
char const __func__[] = "tport";
#endif

#define STACK_RECV(tp, msg, now)		       \
  (tp)->tp_master->mr_tpac->tpac_recv((tp)->tp_master->mr_stack, (tp), \
				      (msg), (tp)->tp_magic, (now))

#define STACK_ERROR(tp, errcode, dstname) \
  (tp)->tp_master->mr_tpac->tpac_error((tp)->tp_master->mr_stack, (tp), \
				       (errcode), (dstname))

#define STACK_SIGCOMP_ACCEPT(tp, msg)				  \
  (tp)->tp_master->mr_tpac->					  \
  tpac_sigcomp_accept((tp)->tp_master->mr_stack, (tp), (msg))

93 94 95
#define STACK_ADDRESS(tp)		       \
  (tp)->tp_master->mr_tpac->tpac_address((tp)->tp_master->mr_stack, (tp))

Pekka Pessi's avatar
Pekka Pessi committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
#define TP_STACK   tp_master->mr_stack

/* Define macros for rbtree implementation */
#define TP_LEFT(tp) ((tp)->tp_left)
#define TP_RIGHT(tp) ((tp)->tp_right)
#define TP_PARENT(tp) ((tp)->tp_dad)
#define TP_SET_RED(tp) ((tp)->tp_black = 0)
#define TP_SET_BLACK(tp) ((tp)->tp_black = 1)
#define TP_IS_RED(tp) ((tp) && (tp)->tp_black == 0)
#define TP_IS_BLACK(tp) (!(tp) || (tp)->tp_black == 1)
#define TP_COPY_COLOR(dst, src) ((dst)->tp_black = (src)->tp_black)
#define TP_INSERT(tp) ((void)0)
#define TP_REMOVE(tp) ((tp)->tp_left = (tp)->tp_right = (tp)->tp_dad = NULL)

static inline int tp_cmp(tport_t const *a, tport_t const *b)
{
  if (a == b)
    return 0;
  
  if (a->tp_addrlen != b->tp_addrlen)
    return a->tp_addrlen - b->tp_addrlen;

118
  return memcmp(a->tp_addr, b->tp_addr, a->tp_addrlen);
Pekka Pessi's avatar
Pekka Pessi committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 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 170 171
}

static inline int tprb_is_inserted(tport_t const *a)
{
  return a->tp_dad != 0 || a->tp_left != 0 || a->tp_right != 0;
}

RBTREE_PROTOS(static inline, tprb, tport_t);

RBTREE_BODIES(static inline, tprb, tport_t, 
	      TP_LEFT, TP_RIGHT, TP_PARENT,
	      TP_IS_RED, TP_SET_RED, TP_IS_BLACK, TP_SET_BLACK, TP_COPY_COLOR,
	      tp_cmp, TP_INSERT, TP_REMOVE);

enum {
  /** Default per-thread read queue length */
  THRP_PENDING = 8
};

struct tport_pending_s {
  //  tport_pending_t       *p_left, *p_right, *p_parent;
  void               *p_client;
  tport_pending_error_f *p_callback;
  msg_t              *p_msg;
  int                 p_reported;
};

/** Return true if transport is master. */
inline int tport_is_master(tport_t const *self)
{
  return 
    self && 
    self->tp_master->mr_master == self;
}

/** Return true if transport is primary. */
inline int tport_is_primary(tport_t const *self)
{
  return 
    self && 
    self->tp_pri->pri_primary == self;
}

/** Return true if transport is secondary. */
inline int tport_is_secondary(tport_t const *self)
{
  return 
    self && 
    self->tp_master->mr_master != self && 
    self->tp_pri->pri_primary != self;
}

/** Test if transport has been registered */
172
inline int tport_is_registered(tport_t const *self)
Pekka Pessi's avatar
Pekka Pessi committed
173 174 175 176 177 178 179
{
  return self->tp_index != 0;
}

/** Test if transport is stream. */
inline int tport_is_stream(tport_t const *self)
{
180
  return self->tp_addrinfo->ai_socktype == SOCK_STREAM;
Pekka Pessi's avatar
Pekka Pessi committed
181 182 183
}
 
/** Test if transport is dgram. */
184
inline int tport_is_dgram(tport_t const *self)
Pekka Pessi's avatar
Pekka Pessi committed
185
{
186
  return self->tp_addrinfo->ai_socktype == SOCK_DGRAM;
Pekka Pessi's avatar
Pekka Pessi committed
187 188 189 190 191
}
 
/** Test if transport is udp. */
inline int tport_is_udp(tport_t const *self)
{
192
  return self->tp_addrinfo->ai_protocol == IPPROTO_UDP;
Pekka Pessi's avatar
Pekka Pessi committed
193 194 195 196 197
}
 
/** Test if transport is tcp. */
inline int tport_is_tcp(tport_t const *self)
{
198
  return self->tp_addrinfo->ai_protocol == IPPROTO_TCP;
Pekka Pessi's avatar
Pekka Pessi committed
199 200 201 202 203 204
}
 
/** Return 1 if transport is reliable, 0 otherwise.
 *
 * (Note that this is part of external API).
 */
205
int tport_is_reliable(tport_t const *self)
Pekka Pessi's avatar
Pekka Pessi committed
206 207
{
  return self != NULL && 
208 209
    (self->tp_addrinfo->ai_socktype == SOCK_STREAM || 
     self->tp_addrinfo->ai_socktype == SOCK_SEQPACKET);
Pekka Pessi's avatar
Pekka Pessi committed
210 211
}

212 213 214
/** Return 0 if self is local, nonzero otherwise.
 *
 * The return valu is the 
215
 *
216
 * @sa TPTAG_PUBLIC(), enum #tport_via.
217
 */
218
int tport_is_public(tport_t const *self)
219
{
220
  return self->tp_pri->pri_public;
221
}
Pekka Pessi's avatar
Pekka Pessi committed
222 223

/** Return true if transport supports IPv4 */
224
int tport_has_ip4(tport_t const *self)
Pekka Pessi's avatar
Pekka Pessi committed
225 226
{
  return self && 
227 228
    (self->tp_addrinfo->ai_family == 0 || 
     self->tp_addrinfo->ai_family == AF_INET);
Pekka Pessi's avatar
Pekka Pessi committed
229 230
}

231 232

#if SU_HAVE_IN6
Pekka Pessi's avatar
Pekka Pessi committed
233
/** Return true if transport supports IPv6 */
234
int tport_has_ip6(tport_t const *self)
Pekka Pessi's avatar
Pekka Pessi committed
235 236
{
  return self && 
237 238
    (self->tp_addrinfo->ai_family == 0 || 
     self->tp_addrinfo->ai_family == AF_INET6);
Pekka Pessi's avatar
Pekka Pessi committed
239
}
240
#endif
Pekka Pessi's avatar
Pekka Pessi committed
241 242

/** Return true if transport supports TLS. */
243
int tport_has_tls(tport_t const *self)
Pekka Pessi's avatar
Pekka Pessi committed
244
{
245 246
  return self && self->tp_pri->pri_has_tls;

Pekka Pessi's avatar
Pekka Pessi committed
247 248
}

249 250 251
/** Return true if transport is being updated. */
int tport_is_updating(tport_t const *self)
{
252 253 254 255 256 257 258 259 260 261 262 263
  tport_primary_t *pri;

  if (tport_is_master(self)) {
    for (pri = self->tp_master->mr_primaries; pri; pri = pri->pri_next) 
      if (pri->pri_updating)
	return 1;
  }
  else if (tport_is_primary(self)) {
    return self->tp_pri->pri_updating;
  }

  return 0;
264 265
}

Pekka Pessi's avatar
Pekka Pessi committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
/** Test if transport has been closed */
static inline int tport_is_closed(tport_t const *self)
{
  return self->tp_closed;
}

/** Test if transport has been shut down */
static inline int tport_is_shutdown(tport_t const *self)
{
  return self->tp_closed || self->tp_send_close || self->tp_recv_close;
}

/** Test if transport is bound */
static inline int tport_is_bound(tport_t const *self)
{
  return self->tp_protoname != NULL;
}

/** MTU for transport  */
static inline unsigned tport_mtu(tport_t const *self)
{
  return self->tp_params->tpp_mtu;
}

static inline
int tport_has_sigcomp(tport_t const *self)
{
  return self->tp_name->tpn_comp != NULL;
}

static 
tport_t *tport_connect(tport_primary_t *pri, su_addrinfo_t *ai, 
		       tp_name_t const *tpn);

300
static int bind6only_check(tport_master_t *mr);
301 302 303 304 305 306 307 308 309 310 311

static
int tport_server_addrinfo(tport_master_t *mr,
			  char const *canon,
			  int family,
			  char const *host, 
			  char const *service,
			  char const *protocol,
			  char const * const transports[],
			  su_addrinfo_t **res);

Pekka Pessi's avatar
Pekka Pessi committed
312 313 314 315 316
static int tport_get_local_addrinfo(tport_master_t *mr, 
				    char const *port,
				    su_addrinfo_t const *hints,
				    su_addrinfo_t **return_ai);

317 318 319 320 321
int tport_getaddrinfo(char const *node, char const *service,
		      su_addrinfo_t const *hints,
		      su_addrinfo_t **res);

static void tport_freeaddrinfo(su_addrinfo_t *ai);
Pekka Pessi's avatar
Pekka Pessi committed
322

Pekka Pessi's avatar
Pekka Pessi committed
323 324 325 326
static
int tport_addrinfo_copy(su_addrinfo_t *dst, void *addr, socklen_t addrlen,
			su_addrinfo_t const *src);

Pekka Pessi's avatar
Pekka Pessi committed
327
static int
328 329 330 331 332 333
  tport_bind_client(tport_master_t *self, tp_name_t const *tpn,
		    char const * const transports[], enum tport_via public,
		    tagi_t *tags),
  tport_bind_server(tport_master_t *, tp_name_t const *tpn,
		    char const * const transports[],  enum tport_via public,
		    tagi_t *tags),
Pekka Pessi's avatar
Pekka Pessi committed
334

335
  tport_setname(tport_t *, char const *, su_addrinfo_t const *, char const *),
336 337 338
  tport_wakeup_pri(su_root_magic_t *m, su_wait_t *w, tport_t *self),
  tport_wakeup(su_root_magic_t *m, su_wait_t *w, tport_t *self),
  tport_base_wakeup(tport_t *self, int events),
339
  tport_connected(su_root_magic_t *m, su_wait_t *w, tport_t *self),
Pekka Pessi's avatar
Pekka Pessi committed
340 341 342 343
  tport_resolve(tport_t *self, msg_t *msg, tp_name_t const *tpn),
  tport_vsend(tport_t *self, msg_t *msg, tp_name_t const *tpn,
	      msg_iovec_t iov[], int iovused,
	      struct sigcomp_compartment *cc),
344
  tport_send_error(tport_t *, msg_t *, tp_name_t const *),
Pekka Pessi's avatar
Pekka Pessi committed
345 346 347
  tport_queue(tport_t *self, msg_t *msg),
  tport_queue_rest(tport_t *self, msg_t *msg, msg_iovec_t iov[], int iovused),
  tport_pending_error(tport_t *self, su_sockaddr_t const *dst, int error),
348
  tport_pending_errmsg(tport_t *self, msg_t *msg, int error);
Pekka Pessi's avatar
Pekka Pessi committed
349 350 351 352 353 354 355 356

tport_t *tport_by_addrinfo(tport_primary_t const *pri,
			   su_addrinfo_t const *ai,
			   tp_name_t const *tpn);

void tport_peer_address(tport_t *self, msg_t *msg);
static unsigned long tport_now(void);

357
static void tport_tick(su_root_magic_t *, su_timer_t *, tport_master_t *mr);
Pekka Pessi's avatar
Pekka Pessi committed
358 359

static void tport_parse(tport_t *self, int complete, su_time_t now);
Pekka Pessi's avatar
Pekka Pessi committed
360

361 362
static tport_primary_t *tport_alloc_primary(tport_master_t *mr,
					    tport_vtable_t const *vtable,
363
					    tp_name_t tpn[1],
364 365 366 367 368 369
					    su_addrinfo_t *ai, 
					    tagi_t const *tags,
					    char const **return_culprit);

static tport_primary_t *tport_listen(tport_master_t *mr,
				     tport_vtable_t const *vtable,
370
				     tp_name_t tpn[1],
371
				     su_addrinfo_t *ai,
Pekka Pessi's avatar
Pekka Pessi committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
				     tagi_t *tags);
static void tport_zap_primary(tport_primary_t *);

static char *localipname(int pf, char *buf, int bufsiz);
static int getprotohints(su_addrinfo_t *hints,
			 char const *proto, int flags);
static void tport_send_queue(tport_t *self);


/* Stack class used when transports are being destroyed */
static
void tport_destroy_recv(tp_stack_t *stack, tport_t *tp, 
			msg_t *msg, tp_magic_t *magic, 
			su_time_t received)
{
  msg_destroy(msg);
}

static
void tport_destroy_error(tp_stack_t *stack, tport_t *tp, 
			 int errcode, char const *remote)
{
}

static
msg_t *tport_destroy_alloc(tp_stack_t *stack, int flags, 
			   char const data[], unsigned len,
			   tport_t const *tp,
			   tp_client_t *tpc)
{
  return NULL;
}

/** Name for "any" transport. @internal */
static char const tpn_any[] = "*";

/** Create the master transport. */
tport_t *tport_tcreate(tp_stack_t *stack,
		       tp_stack_class_t const *tpac,
		       su_root_t *root, 
		       tag_type_t tag, tag_value_t value, ...)
{
  tport_master_t *mr;
  tp_name_t *tpn;
  tport_params_t *tpp;
  unsigned tick;
  ta_list ta;

  if (!stack || !tpac || !root) {
    su_seterrno(EINVAL);
    return NULL;
  }

  mr = su_home_clone(NULL, sizeof *mr);
  if (!mr)
    return NULL;

429
  SU_DEBUG_7(("%s(): %p\n", "tport_create", mr));
Pekka Pessi's avatar
Pekka Pessi committed
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445

  mr->mr_stack = stack;
  mr->mr_tpac = tpac;
  mr->mr_root = root;

  mr->mr_master->tp_master = mr;
  mr->mr_master->tp_params = tpp = mr->mr_params;

  mr->mr_master->tp_reusable = 1;
  tpp->tpp_mtu = UINT_MAX;
  tpp->tpp_thrprqsize = THRP_PENDING;
  tpp->tpp_qsize = TPORT_QUEUESIZE;
  tpp->tpp_sdwn_error = 1;
  tpp->tpp_idle = UINT_MAX;
  tpp->tpp_timeout = UINT_MAX;
  tpp->tpp_sigcomp_lifetime = UINT_MAX;
446
  tpp->tpp_stun_server = 1;
Pekka Pessi's avatar
Pekka Pessi committed
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466

  tpn = mr->mr_master->tp_name;
  tpn->tpn_proto = "*";
  tpn->tpn_host = "*";
  tpn->tpn_canon = "*";
  tpn->tpn_port = "*";

  ta_start(ta, tag, value);

  tport_set_params(mr->mr_master, ta_tags(ta));
  tport_open_log(mr, ta_args(ta));

  tick = 5000; /* For testing, usually 30000 is enough */  
  if (tpp->tpp_idle < 4 * tick)
    tick = tpp->tpp_idle / 4;
  if (tpp->tpp_timeout < 4 * tick)
    tick = tpp->tpp_timeout / 4;
  if (tick < 200)
    tick = 200;

467 468
  tport_init_stun_server(mr, ta_args(ta));

Pekka Pessi's avatar
Pekka Pessi committed
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
  mr->mr_timer = su_timer_create(su_root_task(root), tick);
  su_timer_set(mr->mr_timer, tport_tick, mr);

  ta_end(ta);

  return mr->mr_master;
}

/** Destroy the master transport. */
void tport_destroy(tport_t *self)
{
  tport_master_t *mr;

  static tp_stack_class_t tport_destroy_tpac[1] = 
    {{
	sizeof tport_destroy_tpac,
	/* tpac_recv */ tport_destroy_recv,
	/* tpac_error */ tport_destroy_error,
	/* tpac_alloc */ tport_destroy_alloc,
	/* tpac_comp_accept */ NULL
      }};

  SU_DEBUG_7(("%s(%p)\n", __func__, self));

  if (self == NULL)
    return;
  
  assert(tport_is_master(self));
  if (!tport_is_master(self))
    return;

  mr = (tport_master_t *)self;
  mr->mr_tpac = tport_destroy_tpac;

  while (mr->mr_primaries)
    tport_zap_primary(mr->mr_primaries);

506 507
  tport_deinit_comp(mr);
  tport_deinit_stun_server(mr);
Pekka Pessi's avatar
Pekka Pessi committed
508 509 510 511 512 513 514 515 516 517

  if (mr->mr_dump_file)
    fclose(mr->mr_dump_file), mr->mr_dump_file = NULL;

  if (mr->mr_timer)
    su_timer_destroy(mr->mr_timer), mr->mr_timer = NULL;

  su_home_zap(mr->mr_home);
}

518

Pekka Pessi's avatar
Pekka Pessi committed
519 520
/** Allocate a primary transport */
static 
521 522
tport_primary_t *tport_alloc_primary(tport_master_t *mr,
				     tport_vtable_t const *vtable,
523
				     tp_name_t tpn[1],
524 525 526
				     su_addrinfo_t *ai,
				     tagi_t const *tags,
				     char const **return_culprit)
Pekka Pessi's avatar
Pekka Pessi committed
527 528
{
  tport_primary_t *pri, **next;
529
  tport_t *tp;
Pekka Pessi's avatar
Pekka Pessi committed
530 531 532

  for (next = &mr->mr_primaries; *next; next = &(*next)->pri_next)
    ;
533 534
  
  assert(vtable->vtp_pri_size >= sizeof *pri);
Pekka Pessi's avatar
Pekka Pessi committed
535

536
  if ((pri = su_home_clone(mr->mr_home, vtable->vtp_pri_size))) {
Pekka Pessi's avatar
Pekka Pessi committed
537
    tport_t *tp = pri->pri_primary;
538 539 540
    pri->pri_vtable = vtable;
    pri->pri_public = vtable->vtp_public;

Pekka Pessi's avatar
Pekka Pessi committed
541 542 543 544 545 546 547 548 549 550
    tp->tp_master = mr;
    tp->tp_pri = pri;
    tp->tp_socket = SOCKET_ERROR;

    tp->tp_magic = mr->mr_master->tp_magic;

    tp->tp_params = pri->pri_params;
    memcpy(tp->tp_params, mr->mr_params, sizeof (*tp->tp_params));
    tp->tp_reusable = mr->mr_master->tp_reusable;

Pekka Pessi's avatar
Pekka Pessi committed
551
    if (!pri->pri_public)
552
      tp->tp_addrinfo->ai_addr = &tp->tp_addr->su_sa;
553

Pekka Pessi's avatar
Pekka Pessi committed
554 555 556
    SU_DEBUG_5(("%s(%p): new primary tport %p\n", __func__, mr, pri));
  }

557 558 559 560 561 562 563
  *next = pri; 
  tp = pri->pri_primary;
  
  if (!tp)
    *return_culprit = "alloc";
  else if (tport_set_params(tp, TAG_NEXT(tags)) < 0)
    *return_culprit = "tport_set_params";
Pekka Pessi's avatar
Pekka Pessi committed
564 565
  else if (vtable->vtp_init_primary &&
	   vtable->vtp_init_primary(pri, tpn, ai, tags, return_culprit) < 0)
566 567 568 569 570 571 572 573
    ;
  else if (tport_setname(tp, vtable->vtp_name, ai, tpn->tpn_canon) == -1) 
    *return_culprit = "tport_setname";
  else if (tpn->tpn_ident && 
	   !(tp->tp_name->tpn_ident = su_strdup(tp->tp_home, tpn->tpn_ident)))
    *return_culprit = "alloc ident";
  else
    return pri;			/* Success */
Pekka Pessi's avatar
Pekka Pessi committed
574

575
  tport_zap_primary(pri);
Pekka Pessi's avatar
Pekka Pessi committed
576

577 578
  return NULL;
}
579

580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607

/** Destroy a primary transport and its secondary transports. @internal */
static 
void tport_zap_primary(tport_primary_t *pri)
{
  tport_primary_t **prip;

  if (pri == NULL)
    return;

  assert(tport_is_primary(pri->pri_primary));

  if (pri->pri_vtable->vtp_deinit_primary)
    pri->pri_vtable->vtp_deinit_primary(pri);

  while (pri->pri_secondary)
    tport_zap_secondary(pri->pri_secondary);

  /* We have just a single-linked list for primary transports */
  for (prip = &pri->pri_master->mr_primaries;
       *prip != pri;
       prip = &(*prip)->pri_next)
    assert(*prip);

  *prip = pri->pri_next;

  tport_zap_secondary(pri->pri_primary);
}
608

Pekka Pessi's avatar
Pekka Pessi committed
609 610 611 612 613 614
/**Create a primary transport object with socket.
 *
 * Creates a primary transport object with a server socket, and then
 * registers the socket with suitable events to the root.
 *
 * @param dad   parent (master or primary) transport object
Pekka Pessi's avatar
Pekka Pessi committed
615
 * @param ai    pointer to addrinfo structure
Pekka Pessi's avatar
Pekka Pessi committed
616 617 618 619
 * @param canon canonical name of node
 * @param protoname name of the protocol
 */
static
620 621
tport_primary_t *tport_listen(tport_master_t *mr,
			      tport_vtable_t const *vtable,
622
			      tp_name_t tpn[1],
623
			      su_addrinfo_t *ai, 
Pekka Pessi's avatar
Pekka Pessi committed
624 625 626 627 628 629 630 631
			      tagi_t *tags)
{
  tport_primary_t *pri = NULL;

  int err;
  int errlevel = 3;
  char buf[TPORT_HOSTPORTSIZE];

632 633 634 635 636
  char const *protoname = vtable->vtp_name;
  char const *culprit = "unknown";

  su_sockaddr_t *su = (void *)ai->ai_addr;

Pekka Pessi's avatar
Pekka Pessi committed
637
  /* Log an error, return error */
Pekka Pessi's avatar
Pekka Pessi committed
638 639
#define TPORT_LISTEN_ERROR(errno, what)				     \
  ((void)(err = errno,						     \
640 641 642
	  ((err == EADDRINUSE || err == EAFNOSUPPORT ||		     \
	    err == ESOCKTNOSUPPORT || err == EPROTONOSUPPORT ||	     \
	    err == ENOPROTOOPT ? 7 : 3) < SU_LOG_LEVEL ?	     \
Pekka Pessi's avatar
Pekka Pessi committed
643 644
	     su_llog(tport_log, errlevel,			     \
		     "%s(%p): %s(pf=%d %s/%s): %s\n",		     \
645
		     __func__, mr, what, ai->ai_family,		     \
Pekka Pessi's avatar
Pekka Pessi committed
646 647 648 649 650 651 652
		     protoname,					     \
		     tport_hostport(buf, sizeof(buf), su, 2),	     \
		     su_strerror(err)) : (void)0),		     \
	    tport_zap_primary(pri),		                     \
	    su_seterrno(err)),					     \
     (void *)NULL)

653
  /* Create a primary transport object for another transport. */
Pekka Pessi's avatar
Pekka Pessi committed
654
  pri = tport_alloc_primary(mr, vtable, tpn, ai, tags, &culprit);
655 656
  if (pri == NULL)
    return TPORT_LISTEN_ERROR(errno, culprit);
Pekka Pessi's avatar
Pekka Pessi committed
657

Pekka Pessi's avatar
Pekka Pessi committed
658 659 660 661 662 663 664 665 666 667 668 669 670 671
  if (pri->pri_primary->tp_socket != SOCKET_ERROR) {
    int index = 0;
    tport_t *tp = pri->pri_primary;
    su_wait_t wait[1] = { SU_WAIT_INIT };

    if (su_wait_create(wait, tp->tp_socket, tp->tp_events) == -1)
      return TPORT_LISTEN_ERROR(su_errno(), "su_wait_create");

    /* Register receiving or accepting function with events specified above */
    index = su_root_register(mr->mr_root, wait, tport_wakeup_pri, tp, 0); 
    if (index == -1) {
      su_wait_destroy(wait);
      return TPORT_LISTEN_ERROR(su_errno(), "su_root_register");
    }
Pekka Pessi's avatar
Pekka Pessi committed
672

Pekka Pessi's avatar
Pekka Pessi committed
673 674
    tp->tp_index = index;
  }
Pekka Pessi's avatar
Pekka Pessi committed
675 676

  pri->pri_primary->tp_connected = 0;
677

Pekka Pessi's avatar
Pekka Pessi committed
678 679 680 681 682 683 684
  SU_DEBUG_5(("%s(%p): %s " TPN_FORMAT "\n", 
	      __func__, pri, "listening at",
	      TPN_ARGS(pri->pri_primary->tp_name)));

  return pri;
}

Pekka Pessi's avatar
Pekka Pessi committed
685 686 687
int tport_bind_socket(int socket,
		      su_addrinfo_t *ai,
		      char const **return_culprit)
688
{
689
  su_sockaddr_t *su = (su_sockaddr_t *)ai->ai_addr;
690

Pekka Pessi's avatar
Pekka Pessi committed
691
  if (bind(socket, ai->ai_addr, ai->ai_addrlen) == -1) {
692
    return *return_culprit = "bind", -1;
Pekka Pessi's avatar
Pekka Pessi committed
693
  }
694

Pekka Pessi's avatar
Pekka Pessi committed
695
  if (getsockname(socket, ai->ai_addr, &ai->ai_addrlen) == SOCKET_ERROR) {
696
    return *return_culprit = "getsockname", -1;
Pekka Pessi's avatar
Pekka Pessi committed
697
  }
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712

#if defined (__linux__) && defined (SU_HAVE_IN6)
  if (ai->ai_family == AF_INET6) {
    if (!SU_SOCKADDR_INADDR_ANY(su) &&
	(IN6_IS_ADDR_V4MAPPED(&su->su_sin6.sin6_addr) ||
	 IN6_IS_ADDR_V4COMPAT(&su->su_sin6.sin6_addr))) {
      su_sockaddr_t su0[1];
      
      memcpy(su0, su, sizeof su0);
      
      memset(su, 0, ai->ai_addrlen = sizeof su->su_sin);
      su->su_family = ai->ai_family = AF_INET;
      su->su_port = su0->su_port;
      
#ifndef IN6_V4MAPPED_TO_INADDR
713
#define IN6_V4MAPPED_TO_INADDR(in6, in4) \
714 715 716 717 718 719 720
      memcpy((in4), 12 + (uint8_t *)(in6), sizeof(struct in_addr))
#endif
      IN6_V4MAPPED_TO_INADDR(&su0->su_sin6.sin6_addr, &su->su_sin.sin_addr);
    }
  }
#endif

721
  return 0;
722 723
}

724 725 726 727 728 729 730 731 732 733 734

/** Indicate stack that a transport has been updated */
void tport_has_been_updated(tport_t *self)
{
  self->tp_pri->pri_updating = 0;

  if (self->tp_master->mr_tpac->tpac_address)
    self->tp_master->mr_tpac->tpac_address(self->tp_master->mr_stack, self);
}


735 736
static
int tport_set_events(tport_t *self, int set, int clear)
Pekka Pessi's avatar
Pekka Pessi committed
737
{
738
  int events;
Pekka Pessi's avatar
Pekka Pessi committed
739

740 741
  if (self == NULL)
    return -1;
Pekka Pessi's avatar
Pekka Pessi committed
742

743 744
  events = (self->tp_events | set) & ~clear;
  self->tp_events = events;
Pekka Pessi's avatar
Pekka Pessi committed
745

746 747
  if (self->tp_pri->pri_vtable->vtp_set_events)
    return self->tp_pri->pri_vtable->vtp_set_events(self);
Pekka Pessi's avatar
Pekka Pessi committed
748

749 750 751 752 753 754 755 756 757 758 759
  SU_DEBUG_7(("tport_set_events(%p): events%s%s%s\n", self,
	      (events & SU_WAIT_IN) ? " IN" : "",
	      (events & SU_WAIT_OUT) ? " OUT" : "",
	      SU_WAIT_CONNECT != SU_WAIT_OUT &&
	      (events & SU_WAIT_CONNECT) ? " CONNECT" : ""));
  
  return 
    su_root_eventmask(self->tp_master->mr_root, 
		      self->tp_index, 
		      self->tp_socket, 
		      self->tp_events = events);
Pekka Pessi's avatar
Pekka Pessi committed
760 761 762 763 764 765 766 767 768 769 770 771 772 773
}

/**Allocate a secondary transport. @internal
 *
 * The function tport_alloc_secondary() creates a secondary transport
 * object. The new transport initally shares parameters structure with the
 * original transport.
 *
 * @param dad primary transport
 *
 * @return
 * The function tport_alloc_seconary() returns a pointer to the newly
 * created transport, or NULL upon an error.
 */
774
tport_t *tport_alloc_secondary(tport_primary_t *pri, int socket, int accepted)
Pekka Pessi's avatar
Pekka Pessi committed
775 776 777 778
{
  tport_master_t *mr = pri->pri_master;
  tport_t *self;

Pekka Pessi's avatar
Pekka Pessi committed
779
  self = su_home_clone(mr->mr_home, pri->pri_vtable->vtp_secondary_size);
Pekka Pessi's avatar
Pekka Pessi committed
780 781 782 783 784 785 786 787

  if (self) {
    SU_DEBUG_7(("%s(%p): new secondary tport %p\n", __func__, pri, self));

    self->tp_refs = -1;			/* Freshly allocated  */
    self->tp_master = mr;
    self->tp_pri = pri;
    self->tp_params = pri->pri_params;
788
    self->tp_accepted = accepted != 0;
Pekka Pessi's avatar
Pekka Pessi committed
789 790 791
    self->tp_reusable = pri->pri_primary->tp_reusable;

    self->tp_magic = pri->pri_primary->tp_magic;
792 793

    self->tp_addrinfo->ai_addr = (void *)self->tp_addr;
794 795 796 797 798 799 800 801 802 803

    self->tp_socket = socket;
    
    if (pri->pri_vtable->vtp_init_secondary &&
	pri->pri_vtable->vtp_init_secondary(self, socket, accepted) < 0) {
      if (pri->pri_vtable->vtp_deinit_secondary)
	pri->pri_vtable->vtp_deinit_secondary(self);
      su_home_zap(self->tp_home);
      return NULL;
    }
Pekka Pessi's avatar
Pekka Pessi committed
804 805 806 807 808
  }

  return self;
}

809

Pekka Pessi's avatar
Pekka Pessi committed
810 811 812 813 814 815 816 817
/** Create a connected transport object with socket.
 *
 * The function tport_connect() creates a secondary transport with a
 * connected socket. It registers the socket with suitable events to the
 * root.
 *
 * @param pri   primary transport object
 * @param ai    pointer to addrinfo structure
818
 * @param tpn   canonical name of node
Pekka Pessi's avatar
Pekka Pessi committed
819 820 821 822 823
 */
static
tport_t *tport_connect(tport_primary_t *pri, 
		       su_addrinfo_t *ai,
		       tp_name_t const *tpn)
824 825 826 827
{
  if (pri->pri_vtable->vtp_connect)
    return pri->pri_vtable->vtp_connect(pri, ai, tpn);
  else
Pekka Pessi's avatar
Pekka Pessi committed
828
    return tport_base_connect(pri, ai, ai, tpn);
829 830 831 832 833 834 835 836 837
}

/**Create a connected transport object with socket.
 *
 * The function tport_connect() creates a secondary transport with a
 * connected socket. It registers the socket with suitable events to the
 * root.
 *
 * @param pri   primary transport object
838 839
 * @param ai    pointer to addrinfo structure describing socket
 * @param real_ai  pointer to addrinfo structure describing real target
840 841 842 843
 * @param tpn   canonical name of node
 */
tport_t *tport_base_connect(tport_primary_t *pri, 
			    su_addrinfo_t *ai,
844
			    su_addrinfo_t *real_ai,
845
			    tp_name_t const *tpn)
Pekka Pessi's avatar
Pekka Pessi committed
846 847 848 849 850 851 852
{
  tport_master_t *mr = pri->pri_master;
  tport_t *self = NULL;

  su_socket_t s = SOCKET_ERROR;
  int index = 0, err;
  su_wait_t wait[1] = { SU_WAIT_INIT };
853
  su_wakeup_f wakeup = tport_wakeup;
854
  int events = SU_WAIT_IN | SU_WAIT_ERR;
Pekka Pessi's avatar
Pekka Pessi committed
855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898

  int errlevel = 3;
  char buf[TPORT_HOSTPORTSIZE];

  if (ai == NULL || ai->ai_addrlen > sizeof (self->tp_addr))
    return NULL;
  
  /* Log an error, return error */
#define TPORT_CONNECT_ERROR(errno, what)  \
  return							     \
    ((void)(err = errno, s != SOCKET_ERROR ? su_close(s) : 0,	     \
	    su_wait_destroy(wait),				     \
	    (SU_LOG_LEVEL >= errlevel ?				     \
	     su_llog(tport_log, errlevel,			     \
		     "%s(%p): %s(pf=%d %s/%s): %s\n",		     \
		     __func__, pri, #what, ai->ai_family,	     \
		     tpn->tpn_proto,				     \
		       tport_hostport(buf, sizeof(buf),		     \
				      (void *)ai->ai_addr, 2),	     \
		     su_strerror(err)) : (void)0),		     \
	    tport_zap_secondary(self),				     \
	    su_seterrno(err)),					     \
     (void *)NULL)

  s = su_socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);

  if (pri->pri_primary->tp_socket != SOCKET_ERROR) {
    su_sockaddr_t susa;
    socklen_t susalen = sizeof(susa);
    int pri_s = pri->pri_primary->tp_socket;

    if (getsockname(pri_s, &susa.su_sa, &susalen) < 0) {
      SU_DEBUG_3(("tport_connect: getsockname(): %s\n", 
		  su_strerror(su_errno())));
    }
    else {
      susa.su_port = 0;
      if (bind(s, &susa.su_sa, susalen) < 0) {
	SU_DEBUG_3(("tport_connect: bind(local-ip): %s\n", 
		    su_strerror(su_errno())));
      }
    }
  }

899 900 901 902 903
#if SU_HAVE_IN6
  if (s == SOCKET_ERROR) {
    if (ai->ai_family == AF_INET6 && su_errno() == EAFNOSUPPORT)
      errlevel = 7;
    TPORT_CONNECT_ERROR(su_errno(), socket);
Pekka Pessi's avatar
Pekka Pessi committed
904
  }
905
#endif
Pekka Pessi's avatar
Pekka Pessi committed
906 907 908

  if (connect(s, ai->ai_addr, ai->ai_addrlen) == SOCKET_ERROR) {
    err = su_errno();
909
    if (err != EINPROGRESS && err != EAGAIN && err != EWOULDBLOCK)
Pekka Pessi's avatar
Pekka Pessi committed
910
      TPORT_CONNECT_ERROR(err, connect);
911 912
    events = SU_WAIT_CONNECT | SU_WAIT_ERR;
    wakeup = tport_connected;
Pekka Pessi's avatar
Pekka Pessi committed
913 914
  }

915
  if (su_wait_create(wait, s, events) == -1)
Pekka Pessi's avatar
Pekka Pessi committed
916 917
    TPORT_CONNECT_ERROR(su_errno(), su_wait_create);

918 919 920
  if ((self = tport_alloc_secondary(pri, s, 0)) == NULL)
    TPORT_CONNECT_ERROR(errno, tport_alloc_secondary);

Pekka Pessi's avatar
Pekka Pessi committed
921
  /* Register receiving function with events specified above */
922
  if ((index = su_root_register(mr->mr_root, wait, wakeup, self, 0)) == -1)
Pekka Pessi's avatar
Pekka Pessi committed
923 924
    TPORT_CONNECT_ERROR(su_errno(), su_root_register);

925
  /* Set sockname for the tport */
926
  if (tport_setname(self, tpn->tpn_proto, real_ai, tpn->tpn_canon) == -1) 
Pekka Pessi's avatar
Pekka Pessi committed
927 928 929 930
    TPORT_CONNECT_ERROR(su_errno(), tport_setname);

  self->tp_socket   = s;
  self->tp_index    = index;
931
  self->tp_events   = events;
Pekka Pessi's avatar
Pekka Pessi committed
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
  self->tp_conn_orient = 1;

  SU_DEBUG_5(("%s(%p): %s " TPN_FORMAT "\n", 
	      __func__, self, "connecting to",
	      TPN_ARGS(self->tp_name)));

  tprb_append(&pri->pri_secondary, self);

  return self;
}

/** Destroy a secondary transport. @internal */
void tport_zap_secondary(tport_t *self)
{
  tport_master_t *mr;

  if (self == NULL)
    return;

  /* Remove from rbtree */
  tprb_remove(&self->tp_pri->pri_secondary, self);

954 955 956
  /* Do not deinit primary as secondary! */
  if (tport_is_secondary(self) &&
      self->tp_pri->pri_vtable->vtp_deinit_secondary)
957 958
    self->tp_pri->pri_vtable->vtp_deinit_secondary(self);

Pekka Pessi's avatar
Pekka Pessi committed
959 960 961 962 963 964 965 966
  if (self->tp_msg) {
    msg_destroy(self->tp_msg), self->tp_msg = NULL;
    SU_DEBUG_3(("%s(%p): zapped partially received message\n", 
		__func__, self));
  }

  if (self->tp_queue && self->tp_queue[self->tp_qhead]) {
    size_t n = 0, i, N = self->tp_params->tpp_qsize;
Pekka Pessi's avatar
Pekka Pessi committed
967
    for (i = self->tp_qhead; self->tp_queue[i]; i = (i + 1) % N) {
Pekka Pessi's avatar
Pekka Pessi committed
968 969 970 971 972 973 974 975 976 977 978 979 980
      msg_destroy(self->tp_queue[i]), self->tp_queue[i] = NULL;
      n++;
    }
    SU_DEBUG_3(("%s(%p): zapped %lu queued messages\n", 
		__func__, self, (LU)n));
  }

  if (self->tp_pused) {
    SU_DEBUG_3(("%s(%p): zapped with pending requests\n", __func__, self));
  }

  mr = self->tp_master;

981 982
  tport_stun_server_remove_socket(self);

Pekka Pessi's avatar
Pekka Pessi committed
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029
  if (self->tp_index)
    su_root_deregister(mr->mr_root, self->tp_index);
  self->tp_index = 0;
  if (self->tp_socket != -1)
    su_close(self->tp_socket);
  self->tp_socket = -1;

  su_home_zap(self->tp_home);
}

/** Create a new transport reference. */
tport_t *tport_incref(tport_t *tp)
{
  if (tp) {
    if (tp->tp_refs >= 0)
      tp->tp_refs++;
    else if (tp->tp_refs == -1)
      tp->tp_refs = 1;
  }
  return tp;
}

/** Destroy a transport reference. */
void tport_decref(tport_t **ttp)
{
  assert(ttp);

  if (*ttp) {
    if ((*ttp)->tp_refs > 0)
      if (--(*ttp)->tp_refs == 0 && (*ttp)->tp_params->tpp_idle == 0)
	if (!tport_is_closed(*ttp))
	  tport_close(*ttp);
    *ttp = NULL;
  }
}

/** Get transport parameters.
 *
 * @param self          pointer to a transport object
 * @param tag,value,... list of tags
 */
int tport_get_params(tport_t const *self,
		     tag_type_t tag, tag_value_t value, ...)
{
  ta_list ta;
  int n;
  tport_params_t const *tpp;
1030
  tport_primary_t const *pri = self->tp_pri;
Pekka Pessi's avatar
Pekka Pessi committed
1031 1032

  if (self == NULL)
1033
    return su_seterrno(EINVAL);
Pekka Pessi's avatar
Pekka Pessi committed
1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048

  tpp = self->tp_params;
  ta_start(ta, tag, value);

  n = tl_tgets(ta_args(ta),
	       TPTAG_MTU(tpp->tpp_mtu),
	       TPTAG_REUSE(self->tp_reusable),
	       TPTAG_CONNECT(tpp->tpp_conn_orient),
	       TPTAG_QUEUESIZE(tpp->tpp_qsize),
	       TPTAG_IDLE(tpp->tpp_idle),
	       TPTAG_TIMEOUT(tpp->tpp_timeout),
	       TPTAG_SDWN_ERROR(tpp->tpp_sdwn_error),
	       TPTAG_DEBUG_DROP(tpp->tpp_drop),
	       TPTAG_THRPSIZE(tpp->tpp_thrpsize),
	       TPTAG_THRPRQSIZE(tpp->tpp_thrprqsize),
1049
	       TAG_IF(pri, TPTAG_PUBLIC(pri ? pri->pri_public : 0)),
Pekka Pessi's avatar
Pekka Pessi committed
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
	       TAG_END());

  ta_end(ta);

  return n;
}

/** Set transport parameters.
 *
 * @param self          pointer to a transport object
 * @param tag,value,... list of tags
 */
int tport_set_params(tport_t *self,
		     tag_type_t tag, tag_value_t value, ...)
{
  ta_list ta;
  int n;
  tport_params_t tpp[1], *tpp0;

1069
  int connect, sdwn_error, reusable, stun_server;
Pekka Pessi's avatar
Pekka Pessi committed
1070 1071 1072 1073

  struct sigcomp_compartment *cc = NONE;

  if (self == NULL)
1074
    return su_seterrno(EINVAL);
Pekka Pessi's avatar
Pekka Pessi committed
1075 1076 1077 1078 1079 1080

  memcpy(tpp, tpp0 = self->tp_params, sizeof *tpp);

  connect = tpp->tpp_conn_orient;
  sdwn_error = tpp->tpp_sdwn_error;
  reusable = self->tp_reusable;
1081
  stun_server = tpp->tpp_stun_server;
Pekka Pessi's avatar
Pekka Pessi committed
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097

  ta_start(ta, tag, value);

  n = tl_gets(ta_args(ta),
	      TPTAG_MTU_REF(tpp->tpp_mtu),
	      TAG_IF(!self->tp_queue, TPTAG_QUEUESIZE_REF(tpp->tpp_qsize)),
	      TPTAG_IDLE_REF(tpp->tpp_idle),
	      TPTAG_TIMEOUT_REF(tpp->tpp_timeout),
	      TPTAG_DEBUG_DROP_REF(tpp->tpp_drop),
	      TPTAG_THRPSIZE_REF(tpp->tpp_thrpsize),
	      TPTAG_THRPRQSIZE_REF(tpp->tpp_thrprqsize),
	      TPTAG_SIGCOMP_LIFETIME_REF(tpp->tpp_sigcomp_lifetime),
	      TPTAG_CONNECT_REF(connect),
	      TPTAG_SDWN_ERROR_REF(sdwn_error),
	      TPTAG_REUSE_REF(reusable),
	      TPTAG_COMPARTMENT_REF(cc),
1098
	      TPTAG_STUN_SERVER_REF(stun_server),
Pekka Pessi's avatar
Pekka Pessi committed
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
	      TAG_END());

  ta_end(ta);

  if (n == 0)
    return 0;

  if (tpp->tpp_idle > 0 && tpp->tpp_idle < 2000)
    tpp->tpp_idle = 2000;
  if (tpp->tpp_timeout < 1000)
    tpp->tpp_timeout = 1000;
  if (tpp->tpp_drop > 1000)
    tpp->tpp_drop = 1000;
  if (tpp->tpp_thrprqsize > 0)
    tpp->tpp_thrprqsize = tpp0->tpp_thrprqsize;
  if (tpp->tpp_sigcomp_lifetime != 0 && tpp->tpp_sigcomp_lifetime < 30)
    tpp->tpp_sigcomp_lifetime = 30;
  if (tpp->tpp_qsize >= 1000)
    tpp->tpp_qsize = 1000;

  /* Currently only primary UDP transport can *not* be connection oriented */ 
  tpp->tpp_conn_orient = connect 
    || !tport_is_primary(self) || !tport_is_dgram(self);
1122 1123 1124
  tpp->tpp_sdwn_error = sdwn_error;
  self->tp_reusable = reusable;
  tpp->tpp_stun_server = stun_server;
Pekka Pessi's avatar
Pekka Pessi committed
1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135

  if (tport_is_secondary(self) && 
      self->tp_params == self->tp_pri->pri_primary->tp_params) {
    tpp0 = su_zalloc(self->tp_home, sizeof *tpp0); if (!tpp0) return -1;
  }

  memcpy(tpp0, tpp, sizeof *tpp);

  return n;
}

1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
extern tport_vtable_t const tport_udp_vtable;
extern tport_vtable_t const tport_tcp_vtable;
extern tport_vtable_t const tport_tls_vtable;
extern tport_vtable_t const tport_sctp_vtable;
extern tport_vtable_t const tport_udp_client_vtable;
extern tport_vtable_t const tport_tcp_client_vtable;
extern tport_vtable_t const tport_sctp_client_vtable;
extern tport_vtable_t const tport_tls_client_vtable;
extern tport_vtable_t const tport_http_connect_vtable;
extern tport_vtable_t const tport_threadpool_vtable;
1146 1147 1148 1149 1150

#define TPORT_NUMBER_OF_TYPES 64

tport_vtable_t const *tport_vtables[TPORT_NUMBER_OF_TYPES + 1] =
{
1151
  &tport_http_connect_vtable,
1152
#if HAVE_TLS
1153 1154
  &tport_tls_client_vtable,
  &tport_tls_vtable,
1155
#endif
1156
#if HAVE_SCTP && 0		/* SCTP is broken */
1157 1158
  &tport_sctp_client_vtable,
  &tport_sctp_vtable,
1159
#endif
1160 1161 1162 1163 1164
  &tport_tcp_client_vtable,
  &tport_tcp_vtable,
  &tport_udp_client_vtable,
  &tport_udp_vtable,
  &tport_threadpool_vtable,
1165
#if HAVE_SOFIA_STUN
Pekka Pessi's avatar
Pekka Pessi committed
1166
  &tport_stun_vtable,
1167
#endif
1168 1169
};

1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185
/** Register new transport vtable */
int tport_register_type(tport_vtable_t const *vtp)
{
  int i;

  for (i = TPORT_NUMBER_OF_TYPES; i >= 0; i--) {
    if (tport_vtables[i] == NULL) {
      tport_vtables[i] = vtp;
      return 0;
    }
  }

  su_seterrno(ENOMEM);
  return -1;
}

1186 1187 1188
/**Get a vtable for given protocol */
tport_vtable_t const *tport_vtable_by_name(char const *protoname,
					   enum tport_via public) 
Pekka Pessi's avatar
Pekka Pessi committed
1189
{
1190 1191 1192 1193
  int i;

  for (i = TPORT_NUMBER_OF_TYPES; i >= 0; i--) {
    tport_vtable_t const *vtable = tport_vtables[i];
1194

1195 1196
    if (vtable == NULL)
      continue;
1197 1198 1199 1200
    if (vtable->vtp_public != public)
      continue;
    if (strcasecmp(vtable->vtp_name, protoname))
      continue;
Pekka Pessi's avatar
Pekka Pessi committed
1201 1202 1203 1204

    assert(vtable->vtp_pri_size >= sizeof (tport_primary_t));
    assert(vtable->vtp_secondary_size >= sizeof (tport_t));
    
1205 1206 1207 1208
    return vtable;
  }

  return NULL;
Pekka Pessi's avatar
Pekka Pessi committed
1209 1210
}

1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
#if 0
tport_set_f const *tport_set_methods[TPORT_NUMBER_OF_TYPES + 1] = 
  {
    tport_server_bind_set,
    tport_client_bind_set,
    tport_threadpool_set,
    tport_http_connect_set,
#if HAVE_TLS
    tport_tls_set,
#endif
    NULL
  };
1223

1224 1225 1226 1227 1228 1229
int tport_bind_set(tport_master_t *mr, 
		   tp_name_t const *tpn,
		   char const * const transports[],
		   tagi_t const *taglist,
		   tport_set_t **return_set,
		   int set_size)
1230
{
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
  int i;

  for (i = TPORT_NUMBER_OF_TYPES; i >= 0; i--) {
    tport_set_f const *perhaps = tport_vtables[i];
    int result;

    if (perhaps == NULL)
      continue;

    result = perhaps(mr, tpn, transports, taglist, return_set, set_size);
    if (result != 0)
      return result;
  }

  return 0;
1246
}
1247
#endif
1248

Pekka Pessi's avatar
Pekka Pessi committed
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
/** Bind transport objects.
 *
 * @param self        pointer to a transport object
 * @param tpn         desired transport address
 * @param transports  list of protocol names supported by stack
 * @param tag,value,... tagged argument list
 */
int tport_tbind(tport_t *self,
		tp_name_t const *tpn,
		char const * const transports[],
		tag_type_t tag, tag_value_t value, ...)
{
  ta_list ta;
1262
  int server = 1, retval, public = 0;
Pekka Pessi's avatar
Pekka Pessi committed
1263 1264
  tp_name_t mytpn[1];
  tport_master_t *mr;
Pekka Pessi's avatar
Pekka Pessi committed
1265 1266
  char const *http_connect = NULL;
  
Pekka Pessi's avatar
Pekka Pessi committed
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
  if (self == NULL || tport_is_secondary(self) ||
      tpn == NULL || transports == NULL) {
    su_seterrno(EINVAL);
    return -1;
  }

  *mytpn = *tpn;

  if (mytpn->tpn_ident == NULL)
    mytpn->tpn_ident = self->tp_ident;

  ta_start(ta, tag, value);

  tl_gets(ta_args(ta),
	  TPTAG_SERVER_REF(server),
1282
	  TPTAG_PUBLIC_REF(public),
Pekka Pessi's avatar
Pekka Pessi committed
1283
	  TPTAG_IDENT_REF(mytpn->tpn_ident),
Pekka Pessi's avatar
Pekka Pessi committed
1284
	  TPTAG_HTTP_CONNECT_REF(http_connect),
Pekka Pessi's avatar
Pekka Pessi committed
1285 1286 1287
	  TAG_END());

  mr = self->tp_master; assert(mr);
Pekka Pessi's avatar
Pekka Pessi committed
1288

1289 1290 1291 1292 1293
#if HAVE_SOFIA_STUN
  if (public == 0 && stun_is_requested(ta_tags(ta)))
    public = tport_type_stun;
#endif

Pekka Pessi's avatar
Pekka Pessi committed
1294 1295 1296 1297
  if (http_connect && public == 0)
    public = tport_type_connect;
    
  if (server && public == 0)
1298
    retval = tport_bind_server(mr, mytpn, transports, public, ta_args(ta));
Pekka Pessi's avatar
Pekka Pessi committed
1299
  else
1300
    retval = tport_bind_client(mr, mytpn, transports, public, ta_args(ta));
Pekka Pessi's avatar
Pekka Pessi committed
1301 1302 1303 1304 1305 1306

  ta_end(ta);

  return retval;
}

1307

Pekka Pessi's avatar
Pekka Pessi committed
1308 1309 1310 1311 1312 1313
/** Bind primary transport objects used by a client-only application.
 * @internal
 */
int tport_bind_client(tport_master_t *mr,
                      tp_name_t const *tpn,
                      char const * const transports[],
1314
		      enum tport_via public,
Pekka Pessi's avatar
Pekka Pessi committed
1315 1316 1317 1318
		      tagi_t *tags)
{
  int i;
  tport_primary_t *pri = NULL, **tbf;
1319 1320 1321 1322
  tp_name_t tpn0[1] = {{ "*", "*", "*", "*", NULL, NULL }};
  char const *why = "unknown";

  tport_vtable_t const *vtable;
Pekka Pessi's avatar
Pekka Pessi committed
1323

Pekka Pessi's avatar
Pekka Pessi committed
1324 1325 1326
  if (public == tport_type_local)
    public = tport_type_client;

Pekka Pessi's avatar
Pekka Pessi committed
1327 1328
  SU_DEBUG_5(("%s(%p) to " TPN_FORMAT "\n", __func__, mr, TPN_ARGS(tpn)));

1329 1330
  memset(tpn0, 0, sizeof(tpn0));

Pekka Pessi's avatar
Pekka Pessi committed
1331 1332 1333 1334
  for (tbf = &mr->mr_primaries; *tbf; tbf = &(*tbf)->pri_next)
    ;

  for (i = 0; transports[i]; i++) {
1335
    su_addrinfo_t hints[1];
Pekka Pessi's avatar
Pekka Pessi committed
1336 1337 1338 1339 1340 1341
    char const *proto = transports[i];
    
    if (strcmp(proto, tpn->tpn_proto) != 0 && 
        strcmp(tpn->tpn_proto, tpn_any) != 0)
      continue;

1342 1343 1344 1345
    vtable = tport_vtable_by_name(proto, public);
    if (!vtable)
      continue;

Pekka Pessi's avatar
Pekka Pessi committed
1346 1347 1348 1349
    /* Resolve protocol, skip unknown transport protocols */
    if (getprotohints(hints, proto, AI_PASSIVE) < 0)
      continue;

1350 1351 1352
    tpn0->tpn_proto = proto;
    tpn0->tpn_comp = tpn->tpn_comp;
    tpn0->tpn_ident = tpn->tpn_ident;
Pekka Pessi's avatar
Pekka Pessi committed
1353

1354
    hints->ai_canonname = "*";
Pekka Pessi's avatar
Pekka Pessi committed
1355

Pekka Pessi's avatar
Pekka Pessi committed
1356
    if (!(pri = tport_alloc_primary(mr, vtable, tpn0, hints, tags, &why)))
1357
      break;
1358

1359
    pri->pri_public = tport_type_client; /* XXX */
Pekka Pessi's avatar
Pekka Pessi committed
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
  }

  if (!pri)
    tport_zap_primary(*tbf);

  return pri ? 0 : -1;
}

/** Bind primary transport objects used by a server application. */
int tport_bind_server(tport_master_t *mr,
1370
                      tp_name_t const *tpn,
Pekka Pessi's avatar
Pekka Pessi committed
1371
                      char const * const transports[],
1372
		      enum tport_via public,
Pekka Pessi's avatar
Pekka Pessi committed
1373 1374 1375
		      tagi_t *tags)
{
  char hostname[256];
1376
  char const *canon = NULL, *host, *service;
1377 1378
  int error = 0, not_supported, family = 0;
  tport_primary_t *pri = NULL, **tbf;
1379 1380
  su_addrinfo_t *ai, *res = NULL;
  unsigned port, port0, port1, old;
1381
  unsigned short step = 0;
Pekka Pessi's avatar
Pekka Pessi committed
1382

1383
  bind6only_check(mr);
Pekka Pessi's avatar
Pekka Pessi committed
1384 1385 1386 1387 1388 1389 1390

  SU_DEBUG_5(("%s(%p) to " TPN_FORMAT "\n", __func__, mr, TPN_ARGS(tpn)));

  if (tpn->tpn_host == NULL || strcmp(tpn->tpn_host, tpn_any) == 0) {
    /* Use a local IP address */
    host = NULL;
  }
Martti Mela's avatar
Martti Mela committed
1391
#ifdef SU_HAVE_IN6
Pekka Pessi's avatar
Pekka Pessi committed
1392 1393 1394 1395 1396
  else if (tpn->tpn_host && tpn->tpn_host[0] == '[') {
    /* Remove [] around IPv6 addresses. */
    host = strcpy(hostname, tpn->tpn_host + 1);
    hostname[strlen(hostname) - 1] = '\0';
  }
Martti Mela's avatar
Martti Mela committed
1397
#endif
Pekka Pessi's avatar
Pekka Pessi committed
1398
  else
1399
    host = tpn->tpn_host;
Pekka Pessi's avatar
Pekka Pessi committed
1400 1401 1402

  if (tpn->tpn_port != NULL && strlen(tpn->tpn_port) > 0 &&
      strcmp(tpn->tpn_port, tpn_any) != 0)
1403
    service = tpn->tpn_port;
Pekka Pessi's avatar
Pekka Pessi committed
1404
  else 
1405
    service = "";
Pekka Pessi's avatar
Pekka Pessi committed
1406 1407 1408

  if (host && (strcmp(host, "0.0.0.0") == 0 || strcmp(host, "0") == 0))
    host = NULL, family = AF_INET;
1409
#if SU_HAVE_IN6
Pekka Pessi's avatar
Pekka Pessi committed
1410 1411
  else if (host && strcmp(host, "::") == 0)
    host = NULL, family = AF_INET6;
1412
#endif
Pekka Pessi's avatar
Pekka Pessi committed
1413 1414 1415 1416 1417

  if (tpn->tpn_canon && strcmp(tpn->tpn_canon, tpn_any) &&
      (host || tpn->tpn_canon != tpn->tpn_host))
    canon = tpn->tpn_canon;

1418 1419 1420 1421
  if (tport_server_addrinfo(mr, canon, family, 
			    host, service, tpn->tpn_proto,
			    transports, &res) < 0)
    return -1;
Pekka Pessi's avatar
Pekka Pessi committed
1422

1423 1424 1425 1426 1427
  for (tbf = &mr->mr_primaries; *tbf; tbf = &(*tbf)->pri_next)
    ;

  port = port0 = port1 = ntohs(((su_sockaddr_t *)res->ai_addr)->su_port);
  error = ENOENT, not_supported = 1;
Pekka Pessi's avatar
Pekka Pessi committed
1428 1429

  /* 
1430 1431
   * Loop until we can bind all the transports requested 
   * by the transport user to the same port. 
Pekka Pessi's avatar
Pekka Pessi committed
1432
   */
1433 1434
  for (;;) {
    for (ai = res; ai; ai = ai->ai_next) {
1435
      tp_name_t tpname[1];
1436 1437 1438 1439 1440 1441 1442 1443
      su_addrinfo_t ainfo[1];
      su_sockaddr_t su[1];
      tport_vtable_t const *vtable;

      vtable = tport_vtable_by_name(ai->ai_canonname, public);
      if (!vtable)
	continue;

Pekka Pessi's avatar
Pekka Pessi committed
1444
      tport_addrinfo_copy(ainfo, su, sizeof su, ai);
1445 1446 1447
      ainfo->ai_canonname = (char *)canon;
      su->su_port = htons(port);

1448 1449 1450 1451
      memcpy(tpname, tpn, sizeof tpname);
      tpname->tpn_canon = canon;
      tpname->tpn_host = host;

1452 1453 1454
      SU_DEBUG_9(("%s(%p): calling tport_listen for %s\n", 
		  __func__, mr, ai->ai_canonname));

1455
      pri = tport_listen(mr, vtable, tpname, ainfo, tags);
1456 1457 1458 1459 1460 1461 1462 1463 1464 1465
      if (!pri) {
	switch (error = su_errno()) {
	case EADDRNOTAVAIL:	/* Not our address */
	case ENOPROTOOPT:	/* Protocol not supported */
	case ESOCKTNOSUPPORT:	/* Socket type not supported */
	  continue;
	default:
	  break;
	}
	break;
Pekka Pessi's avatar
Pekka Pessi committed
1466 1467
      }

1468
      not_supported = 0;
Pekka Pessi's avatar
Pekka Pessi committed
1469

1470
      if (port0 == 0 && port == 0) {
Pekka Pessi's avatar
Pekka Pessi committed
1471 1472
	port = port1 = ntohs(su->su_port);
	assert(public != tport_type_server || port != 0);
1473 1474
      }
    }
Pekka Pessi's avatar
Pekka Pessi committed
1475

1476 1477
    if (ai == NULL)
      break;
Martti Mela's avatar
Martti Mela committed
1478

1479 1480
    while (*tbf)
      tport_zap_primary(*tbf);
Pekka Pessi's avatar
Pekka Pessi committed
1481

1482 1483
    if (error != EADDRINUSE || port0 != 0 || port == 0)
      break;
Pekka Pessi's avatar
Pekka Pessi committed
1484

1485 1486 1487
    while (step == 0) {
      /* step should be relative prime to 65536 - 1024 */
      /* 65536 - 1024 = 7 * 3 * 3 * 1024 */
1488
      step = su_randint(1, 65535 - 1024 - 1) | 1;
1489 1490 1491 1492 1493 1494
      if (step % 3 == 0)
	step = (step + 2) % (65536 - 1024);
      if (step % 7 == 0)
	step = (step + 2) % (65536 - 1024);
    }
    old = port; port += step; if (port >= 65536) port -= (65536 - 1024);
Pekka Pessi's avatar
Pekka Pessi committed
1495

1496 1497
    if (port == port1)		/* All ports in use! */
      break;
Pekka Pessi's avatar
Pekka Pessi committed
1498

1499 1500
    SU_DEBUG_3(("%s(%p): cannot bind all transports to port %u, trying %u\n", 
		__func__, mr, old, port));
Pekka Pessi's avatar
Pekka Pessi committed
1501 1502
  }

1503
  tport_freeaddrinfo(res);
1504

1505
  if (res && not_supported)
Pekka Pessi's avatar
Pekka Pessi committed
1506 1507
    error = EPROTONOSUPPORT;

1508
  if (!*tbf) {
Pekka Pessi's avatar
Pekka Pessi committed
1509 1510 1511 1512 1513
    su_seterrno(error);
    return -1;
  }

  return 0;
1514
}
1515

1516

1517 1518
/** Check if we can bind to IPv6 separately from IPv4 bind */
static
1519
int bind6only_check(tport_master_t *mr)
1520 1521
{
  int retval = 0;
1522
#if SU_HAVE_IN6
1523 1524 1525 1526
  su_sockaddr_t su[1], su4[1];
  socklen_t sulen, su4len;
  int s6, s4;

1527 1528 1529
  if (mr->mr_boundserver)
    return 0;

1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550
  s4 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
  s6 = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
  
  memset(su, 0, sizeof *su);
  su->su_len = sulen = (sizeof su->su_sin6);
  su->su_family = AF_INET6;
  
  memset(su4, 0, sizeof *su4);
  su4->su_len = su4len = (sizeof su->su_sin);
  su4->su_family = AF_INET;
  
  if (bind(s6, &su->su_sa, sulen) < 0)
    ;
  else if (getsockname(s6, &su->su_sa, &sulen) < 0)
    ;
  else if ((su4->su_port = su->su_port) != 0 && 
	   bind(s4, &su4->su_sa, su4len) == 0)
    retval = 1;

  su_close(s6), su_close(s4);

1551 1552 1553 1554
  mr->mr_bindv6only = retval;
  mr->mr_boundserver = 1;
#endif

1555
  return retval;
Pekka Pessi's avatar
Pekka Pessi committed
1556 1557
}

1558 1559
/* Number of supported transports */
#define TPORT_N (8)
Pekka Pessi's avatar
Pekka Pessi committed
1560

1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617
/** Return list of addrinfo structures matching to 
 * canon/host/service/protocol
 */
static
int tport_server_addrinfo(tport_master_t *mr,
			  char const *canon,
			  int family,
			  char const *host, 
			  char const *service,
			  char const *protocol,
			  char const * const transports[],
			  su_addrinfo_t **return_addrinfo)
{
  int i, N;
  su_addrinfo_t hints[TPORT_N + 1];

  *return_addrinfo = NULL;

  /* 
   * Resolve all the transports requested by the protocol
   */
  for (i = 0, N = 0; transports[i] && N < TPORT_N; i++) {
    su_addrinfo_t *ai = &hints[N];

    if (strcasecmp(transports[i], protocol) != 0 && 
	strcmp(protocol, tpn_any) != 0)
      continue;

    /* Resolve protocol, skip unknown transport protocols. */
    if (getprotohints(ai, transports[i], AI_PASSIVE) < 0)
      continue;

    ai->ai_family = family;
    ai->ai_next = &hints[++N];
  }

  if (N == 0)
    return su_seterrno(ENOENT);
  if (transports[i] /* Too many protocols */) 
    return su_seterrno(ENOMEM);

  hints[N - 1].ai_next = NULL;

  if (host) {
    int error = tport_getaddrinfo(host, service, hints, return_addrinfo);
    if (error || !*return_addrinfo) {
      SU_DEBUG_3(("%s(%p): su_getaddrinfo(%s, %s) for %s: %s\n", 
		  __func__, mr, host ? host : "\"\"", service, protocol,
		  su_gai_strerror(error)));
      return su_seterrno(error != EAI_MEMORY ? ENOENT : ENOMEM);
    }
    return 0;
  }

  return tport_get_local_addrinfo(mr, service, hints, return_addrinfo);
}

Pekka Pessi's avatar
Pekka Pessi committed
1618 1619 1620 1621 1622 1623 1624 1625
/** Convert localinfo into addrinfo */
static
int
tport_get_local_addrinfo(tport_master_t *mr, 
			 char const *port,
			 su_addrinfo_t const *hints,
			 su_addrinfo_t **return_ai)
{
1626 1627 1628 1629
  int error, family;
  su_localinfo_t lihints[1] = {{ 0 }};
  su_localinfo_t *li, *li_result;
  su_addrinfo_t const *h;
Pekka Pessi's avatar
Pekka Pessi committed
1630 1631 1632 1633 1634
  su_addrinfo_t *ai, **prev;
  su_sockaddr_t *su;
  unsigned long lport = 0;
  char *rest;

1635 1636
  prev = return_ai, *prev = NULL;

Pekka Pessi's avatar
Pekka Pessi committed
1637 1638 1639 1640
  if (port) {
    lport = strtoul(port, &rest, 10);
    if (lport >= 65536) {
      su_seterrno(EINVAL);
1641
      return -1;
Pekka Pessi's avatar
Pekka Pessi committed
1642 1643 1644
    }
  }

1645
  family = hints->ai_family;
Pekka Pessi's avatar
Pekka Pessi committed
1646

1647 1648 1649
  for (h = hints->ai_next; h && family; h = h->ai_next)
    if (h->ai_family != family)
      family = 0;
Pekka Pessi's avatar
Pekka Pessi committed
1650

1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
  lihints->li_flags = 0;
  lihints->li_family = family;
  lihints->li_scope = LI_SCOPE_GLOBAL | LI_SCOPE_SITE | LI_SCOPE_HOST;

  error = su_getlocalinfo(lihints, &li_result);
  if (error) {
#if SU_HAVE_IN6
    SU_DEBUG_3(("%s(%p): su_getlocalinfo() for %s address: %s\n", 
		__func__, mr, 
		family == AF_INET6 ? "ip6" 
		: family == AF_INET ? "ip4" : "ip",
		su_gli_strerror(error)));
#else
    SU_DEBUG_3(("%s(%p): su_getlocalinfo() for %s address: %s\n", 
		__func__, mr, 
		family == AF_INET ? "ip4" : "ip",
		su_gli_strerror(error)));
#endif
    su_seterrno(ENOENT);
    return -1;
  }

  for (li = li_result; li; li = li->li_next) {
    for (h = hints; h; h = h->ai_next) {
      if (h->ai_family && h->ai_family != li->li_family)
	continue;

      ai = calloc(1, sizeof *ai + li->li_addrlen);
      if (ai == NULL)
	break;
Pekka Pessi's avatar
Pekka Pessi committed
1681
    
1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704