test_nat.c 21.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
 * This library is free software; you can redistribute it and/or
 * 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 test_nat.c
26
 * @brief Simulated NAT for testing
27
 *
28 29 30 31 32 33
 * NAT thing works so that we set the outgoing proxy URI to point
 * towards its "private" address and give the real address of the proxy
 * as its "public" address. If we use different IP families here, we may
 * even manage to test real connectivity problems as proxy and endpoint
 * can not talk to each other.
 *
34 35 36 37 38 39 40 41 42 43 44 45 46
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Wed Mar  8 19:54:28 EET 2006
 */

#include "config.h"

struct nat;
struct binding;

#define SU_ROOT_MAGIC_T struct nat
#define SU_WAKEUP_ARG_T struct binding

47 48
#include <sofia-sip/su.h>
#include <sofia-sip/su_errno.h>
49 50 51 52 53 54 55
#include <sofia-sip/su_wait.h>
#include <sofia-sip/su_tagarg.h>
#include <sofia-sip/su_localinfo.h>
#include <sofia-sip/su_log.h>

#include <stdlib.h>
#include <assert.h>
56
#include <string.h>
57

58 59
#define LIST_PROTOS(STORAGE, PREFIX, T)			 \
STORAGE void PREFIX ##_insert(T **list, T *node),	 \
60
        PREFIX ##_remove(T *node)
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93

#define LIST_BODIES(STORAGE, PREFIX, T, NEXT, PREV)	  \
STORAGE void PREFIX ##_insert(T **list, T *node)   \
{							 \
  if ((node->NEXT = *list)) {				 \
    node->PREV = node->NEXT->PREV;			 \
    node->NEXT->PREV = &node->NEXT;			 \
  }							 \
  else							 \
    node->PREV = list;					 \
  *list = node;						 \
}							 \
STORAGE void PREFIX ##_remove(T *node)			 \
{							 \
  if (node->PREV)					 \
    if ((*node->PREV = node->NEXT))			 \
      node->NEXT->PREV = node->PREV;			 \
  node->PREV = NULL;					 \
}							 \
extern int LIST_DUMMY_VARIABLE

#include <test_nat.h>

struct nat {
  su_home_t    home[1];
  su_root_t   *parent;
  su_clone_r   clone;
  tagi_t      *tags;

  su_root_t   *root;

  struct binding *bindings;

94 95
  /* True if we act in symmetric way */
  int symmetric;
96 97
  /* True if we do logging */
  int logging;
98

99 100 101 102 103 104 105 106
  /* Everything sent to in_address will be forwarded to out_address */
  su_sockaddr_t in_address[1], out_address[1];
  socklen_t in_addrlen, out_addrlen;

  int family;			/* Preferred private family */

  /* ...but source address will be "fake" */
  su_localinfo_t *localinfo, *private, *fake;
107

108
  su_socket_t udp_socket, tcp_socket;
109 110 111 112 113 114 115 116 117 118 119
  int udp_register, tcp_register;

  char buffer[65536];
};

LIST_PROTOS(static, nat_binding, struct binding);

struct binding
{
  struct binding *next, **prev;
  struct nat *nat;		/* backpointer */
120
  int socktype, protocol;
121
  su_socket_t in_socket, out_socket;
122 123 124 125 126
  int in_register, out_register;
  int in_closed, out_closed;
  char in_name[64], out_name[64];
};

127 128 129 130
static struct binding *nat_binding_new(struct nat *nat,
				       char const *protoname,
				       int socktype, int protocol, 
				       int connected,
131
				       su_socket_t in_socket,
132 133
				       su_sockaddr_t *from,
				       socklen_t fromlen);
134 135
static void nat_binding_destroy(struct binding *);

136 137 138 139 140 141 142
static int binding_init(struct binding *b,
			char const *protoname,
			int connected,
			su_localinfo_t *li,
			su_sockaddr_t *from,
			socklen_t fromlen);

Pekka Pessi's avatar
Pekka Pessi committed
143
static void flush_bindings(struct nat *nat);
144
static int invalidate_bindings(void *nat);
Pekka Pessi's avatar
Pekka Pessi committed
145

146 147 148 149 150 151 152 153
static int new_udp(struct nat *, su_wait_t *wait, struct binding *dummy);
static int udp_in_to_out(struct nat *, su_wait_t *wait, struct binding *);
static int udp_out_to_in(struct nat *, su_wait_t *wait, struct binding *);

static int new_tcp(struct nat *, su_wait_t *wait, struct binding *dummy);
static int tcp_in_to_out(struct nat *, su_wait_t *wait, struct binding *);
static int tcp_out_to_in(struct nat *, su_wait_t *wait, struct binding *);

Pekka Pessi's avatar
Pekka Pessi committed
154 155
static int invalidate_binding(struct binding *b);

156
/* nat entry point */
157
static int
158 159 160 161 162
test_nat_init(su_root_t *root, struct nat *nat)
{
  su_localinfo_t *li, hints[1] = {{ 0 }};
  int error;
  unsigned port = 0, port0 = 0;
163
  su_sockaddr_t su[1];
164 165 166 167
  socklen_t sulen;
  su_wait_t wait[1];

  nat->root = root;
168
  nat->udp_socket = INVALID_SOCKET, nat->tcp_socket = INVALID_SOCKET;
169

170 171 172 173 174
  tl_gets(nat->tags, 
	  TESTNATTAG_SYMMETRIC_REF(nat->symmetric),
	  TESTNATTAG_LOGGING_REF(nat->logging),
	  TAG_END());

175 176
  hints->li_scope = LI_SCOPE_HOST | LI_SCOPE_SITE | LI_SCOPE_GLOBAL;

177 178 179 180 181
  error = su_getlocalinfo(hints, &nat->localinfo);
  if (error) {
    fprintf(stderr, "test_nat: su_getlocalinfo: %s\n", su_gli_strerror(error));
    return -1;
  }
182

183 184 185 186 187 188 189 190 191 192 193 194 195
  /* We must have two different IP addresses. */
  if (!nat->localinfo || !nat->localinfo->li_next) {
    fprintf(stderr, "test_nat: only one IP address available\n");
    return -1;
  }

  for (li = nat->localinfo; li; li = li->li_next) {
    if (nat->family == 0 || nat->family == li->li_family)
      break;
  }
  if (li == NULL)
    li = nat->localinfo;

196 197
  memcpy(su, li->li_addr, sulen = li->li_addrlen);
  memset(SU_ADDR(su), 0, SU_ADDRLEN(su));
198 199 200 201 202 203

  nat->private = li;

  /* Bind TCP and UDP to same port */
  for (;;) {
    nat->udp_socket = su_socket(li->li_family, SOCK_DGRAM, IPPROTO_UDP);
204
    if (nat->udp_socket == INVALID_SOCKET)
205 206 207 208 209 210 211 212
      return -1;

    if (bind(nat->udp_socket, (void *)su, sulen) < 0) {
      if (port0 == 0) {
	su_perror("nat: bind(udp_socket)");
	return -1;
      }

213
      fprintf(stderr, "test_nat: port %u: %s\n",
214 215
	      port, su_strerror(su_errno()));
      su_close(nat->udp_socket);
216

217
      nat->udp_socket = INVALID_SOCKET;
218 219 220 221 222 223 224 225 226 227 228 229

      if (++port > 65535)
	port = 1024;
      if (port == port0) {
	fprintf(stderr, "test_nat: could not find free port pairt\n");
	return -1;
      }

      continue;
    }

    if (getsockname(nat->udp_socket, (void *)su, &sulen) < 0) {
230
      su_perror("nat: getsockname(udp_socket)");
231 232 233 234 235 236 237 238 239 240 241 242
      return -1;
    }

    if (port0 == 0) {
      port = port0 = ntohs(su->su_port);
      if (port0 == 0) {
	fprintf(stderr, "test_nat: bind did not return port\n");
	return -1;
      }
    }

    nat->tcp_socket = su_socket(li->li_family, SOCK_STREAM, IPPROTO_TCP);
243
    if (nat->tcp_socket == INVALID_SOCKET)
244
      return -1;
245

246 247
    if (bind(nat->tcp_socket, (void *)su, sulen) < 0) {
      su_close(nat->tcp_socket);
248
      nat->tcp_socket = INVALID_SOCKET;
249

250
      fprintf(stderr, "test_nat: port %u: %s\n",
251 252 253 254 255 256 257 258 259 260 261 262 263
	      port, su_strerror(su_errno()));

      if (++port > 65535)
	port = 1024;
      if (port == port0) {
	fprintf(stderr, "test_nat: could not find free port pair\n");
	return -1;
      }

      continue;
    }

    break;
264
  }
265

266 267
  memcpy(nat->in_address, li->li_addr, nat->in_addrlen = li->li_addrlen);
  nat->in_address->su_port = su->su_port;
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

  if (su_setreuseaddr(nat->udp_socket, 1) < 0) {
    su_perror("nat: su_setreuseaddr(udp_socket)");
    return -1;
  }

  if (listen(nat->tcp_socket, 5) < 0) {
    su_perror("nat: listen(tcp_socket)");
    return -1;
  }

  if (su_wait_create(wait, nat->udp_socket, SU_WAIT_IN) < 0) {
    su_perror("nat: su_wait_create");
    return -1;
  }

  nat->udp_register = su_root_register(root, wait, new_udp, NULL, 0);
  if (nat->udp_register < 0) {
    su_perror("nat: su_root_register");
    return -1;
  }

  if (su_wait_create(wait, nat->tcp_socket, SU_WAIT_IN) < 0) {
    su_perror("nat: su_wait_create");
    return -1;
  }

  nat->tcp_register = su_root_register(root, wait, new_tcp, NULL, 0);
  if (nat->tcp_register < 0) {
    su_perror("nat: su_root_register");
    return -1;
  }
300

301 302 303 304 305 306
  return 0;
}

static void
test_nat_deinit(su_root_t *root, struct nat *nat)
{
Pekka Pessi's avatar
Pekka Pessi committed
307
  flush_bindings(nat);
308

Pekka Pessi's avatar
Pekka Pessi committed
309 310 311 312 313
  if (nat->tcp_register)
    su_root_deregister(root, nat->tcp_register);
  if (nat->udp_register)
    su_root_deregister(root, nat->udp_register);

314
  if (nat->udp_socket != INVALID_SOCKET)
315
    su_close(nat->udp_socket);
316
  if (nat->tcp_socket != INVALID_SOCKET)
317
    su_close(nat->tcp_socket);
Pekka Pessi's avatar
Pekka Pessi committed
318

319
  su_freelocalinfo(nat->localinfo);
Pekka Pessi's avatar
Pekka Pessi committed
320

321 322 323
  free(nat->tags);
}

324
struct nat *test_nat_create(su_root_t *root,
325 326 327 328 329 330 331 332 333 334 335 336 337 338
			    int family,
			    tag_type_t tag, tag_value_t value, ...)
{
  struct nat *nat = su_home_new(sizeof *nat);

  if (nat) {
    ta_list ta;

    nat->parent = root;
    nat->family = family;

    ta_start(ta, tag, value);
    nat->tags = tl_llist(ta_tags(ta));
    ta_end(ta);
339

340 341
    if (su_clone_start(root,
		       nat->clone,
342
		       nat,
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
		       test_nat_init,
		       test_nat_deinit) == -1)
      su_home_unref(nat->home), nat = NULL;
  }

  return nat;
}

void test_nat_destroy(struct nat *nat)
{
  if (nat) {
    su_clone_wait(nat->parent, nat->clone);
    su_home_unref(nat->home);
  }
}

/** Get "private" address. */
360
int test_nat_private(struct nat *nat, void *address, socklen_t *return_addrlen)
361 362 363
{
  if (nat == NULL || address == NULL || return_addrlen == NULL)
    return su_seterrno(EFAULT);
364

365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
  if (*return_addrlen < nat->in_addrlen)
    return su_seterrno(EINVAL);

  memcpy(address, nat->in_address, *return_addrlen = nat->in_addrlen);

  return 0;
}

/** Set "public" address. */
int test_nat_public(struct nat *nat, void const *address, int addrlen)
{
  su_sockaddr_t const *su = address;
  su_localinfo_t *li;

  if (nat == NULL)
    return su_seterrno(EFAULT);

  if (address == NULL) {
    nat->fake = NULL;
    return 0;
  }

387
  if ((size_t)addrlen > sizeof nat->out_address)
388
    return su_seterrno(EINVAL);
389

390
  for (li = nat->localinfo; li; li = li->li_next) {
391 392
    if (li != nat->private &&
	li->li_scope == LI_SCOPE_HOST &&
393 394 395
	li->li_family == su->su_family)
      break;
  }
396

397 398 399 400 401 402 403 404 405
  if (li == NULL)
    for (li = nat->localinfo; li; li = li->li_next) {
      if (li != nat->private && li->li_family == su->su_family)
	break;
    }

  if (li == NULL)
    return su_seterrno(EADDRNOTAVAIL);

Pekka Pessi's avatar
Pekka Pessi committed
406
  su_clone_pause(nat->clone);
407 408
  memcpy(nat->out_address, address, nat->out_addrlen = addrlen);
  nat->fake = li;
Pekka Pessi's avatar
Pekka Pessi committed
409
  su_clone_resume(nat->clone);
410 411 412 413

  return 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
414 415 416 417 418 419
int test_nat_flush(struct nat *nat)
{
  if (nat == NULL)
    return su_seterrno(EFAULT);

  return su_task_execute(su_clone_task(nat->clone), 
420
			 invalidate_bindings, nat, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
421 422
}

423 424
/* ====================================================================== */

425 426 427 428 429
struct binding *nat_binding_new(struct nat *nat,
				char const *protoname,
				int socktype,
				int protocol,
				int connected,
430
				su_socket_t in_socket,
431 432
				su_sockaddr_t *from,
				socklen_t fromlen)
433
{
434
  struct binding *b;
435

436 437 438
  if (nat->fake == NULL) {	/* Xyzzy */
    fprintf(stderr, "test_nat: fake address missing\n");
    su_close(in_socket);
439
    return NULL;
440
  }
441

442 443 444 445 446 447
  b = su_zalloc(nat->home, sizeof *b);
  if (b == NULL) {
    su_perror("nat_binding_new: su_zalloc");
    su_close(in_socket);
    return 0;
  }
448

Pekka Pessi's avatar
Pekka Pessi committed
449 450
  b->nat = nat;
  b->socktype = socktype;
451
  b->protocol = protocol;
452
  b->in_socket = in_socket, b->out_socket = INVALID_SOCKET;
453 454
  b->in_register = -1, b->out_register = -1;

455 456 457 458 459 460 461 462 463 464 465 466 467 468
  if (binding_init(b, protoname, connected, nat->fake, from, fromlen) < 0)
    nat_binding_destroy(b), b = NULL;

  return b;
}

static int binding_init(struct binding *b,
			char const *protoname,
			int connected,
			su_localinfo_t *li,
			su_sockaddr_t *from,
			socklen_t fromlen)
{
  struct nat *nat = b->nat;
469
  su_socket_t out_socket;
470 471 472 473 474 475 476 477 478 479 480
  su_sockaddr_t addr[1];
  socklen_t addrlen = (sizeof addr);
  char ipname[64];
  su_wait_t wait[1];
  su_wakeup_f in_to_out, out_to_in;

  if (b->socktype == SOCK_STREAM)
    in_to_out = tcp_in_to_out, out_to_in = tcp_out_to_in;    
  else
    in_to_out = udp_in_to_out, out_to_in = udp_out_to_in;    
  
481
  if (b->in_socket == INVALID_SOCKET) {
482 483 484
    int in_socket;

    in_socket = su_socket(from->su_family, b->socktype, b->protocol);
485
    if (in_socket == INVALID_SOCKET) {
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
      su_perror("nat_binding_new: socket");
      return -1;
    }
    b->in_socket = in_socket;
    if (su_setreuseaddr(in_socket, 1) < 0) {
      su_perror("nat_binding_new: su_setreuseaddr(in_socket)");
      return -1;
    }
    if (bind(in_socket, (void *)nat->in_address, nat->in_addrlen) < 0) {
      su_perror("nat_binding_new: bind(in_socket)");
      return -1;
    }
    if (connect(in_socket, (void *)from, fromlen) < 0) {
      su_perror("nat_binding_new: connect(in_socket)");
      return -1;
    }
  }

  out_socket = su_socket(li->li_family, b->socktype, b->protocol);
505
  if (out_socket == INVALID_SOCKET) {
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
    su_perror("nat_binding_new: socket");
    return -1;
  }
  b->out_socket = out_socket;

  if (bind(out_socket, (void *)li->li_addr, li->li_addrlen) < 0) {
    su_perror("nat_binding_new: bind(to)");
    return -1;
  }

  if (connected)
    if (connect(out_socket, (void *)nat->out_address, nat->out_addrlen) < 0) {
      su_perror("nat_binding_new: connect(to)");
      return -1;
    }

  getpeername(b->in_socket, (void *)addr, &addrlen);
  inet_ntop(addr->su_family, SU_ADDR(addr), ipname, sizeof ipname);
524
  snprintf(b->in_name, sizeof b->in_name,
525
	   addr->su_family == AF_INET6 ? "[%s]:%u" : "%s:%u",
526
	   ipname, ntohs(addr->su_port));
527 528

  getsockname(out_socket, (void *)addr, &addrlen);
529
  inet_ntop(addr->su_family, SU_ADDR(addr), ipname, sizeof ipname);
530 531
  snprintf(b->out_name, sizeof b->out_name,
	   addr->su_family == AF_INET6 ? "[%s]:%u" : "%s:%u",
532
	   ipname, ntohs(addr->su_port));
533

534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
  if (su_wait_create(wait, b->in_socket, SU_WAIT_IN) < 0) {
    su_perror("nat_binding_new: su_wait_create");
    return -1;
  }
  b->in_register = su_root_register(nat->root, wait, in_to_out, b, 0);
  if (b->in_register < 0) {
    su_perror("nat_binding_new: su_root_register");
    su_wait_destroy(wait); 
    return -1;
  }

  if (su_wait_create(wait, out_socket, SU_WAIT_IN) < 0) {
    su_perror("nat_binding_new: su_wait_create");
    return -1;
  }
  b->out_register = su_root_register(nat->root, wait, out_to_in, b, 0);
  if (b->out_register < 0) {
    su_perror("nat_binding_new: su_root_register");
    su_wait_destroy(wait);
    return -1;
  }

Pekka Pessi's avatar
Pekka Pessi committed
556 557
  nat_binding_insert(&nat->bindings, b);

558 559 560
  if (nat->logging)
    printf("nat: new %s binding %s <=> %s\n",
	   protoname, b->in_name, b->out_name);
561 562

  return 0;
563 564 565 566 567 568 569 570 571 572 573 574
}

static void nat_binding_destroy(struct binding *b)
{
  nat_binding_remove(b);
  if (b->in_register != -1)
    su_root_deregister(b->nat->root, b->in_register);
  if (b->out_register != -1)
    su_root_deregister(b->nat->root, b->out_register);
  su_close(b->in_socket), su_close(b->out_socket);
}

Pekka Pessi's avatar
Pekka Pessi committed
575 576 577 578 579 580 581 582 583 584 585 586 587 588
static void flush_bindings(struct nat *nat)
{
  struct binding *b;

  for (b = nat->bindings; b; b = b->next) {
    if (b->in_register)
      su_root_deregister(nat->root, b->in_register);
    su_close(b->in_socket);
    if (b->out_register)
      su_root_deregister(nat->root, b->out_register);
    su_close(b->out_socket);
  }
}

589
static int invalidate_bindings(void *arg)
Pekka Pessi's avatar
Pekka Pessi committed
590
{
591
  struct nat *nat = arg;
Pekka Pessi's avatar
Pekka Pessi committed
592 593 594 595 596
  struct binding *b;

  for (b = nat->bindings; b; b = b->next) {
    invalidate_binding(b);
  }
597
  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
598 599
}

600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
#if 0
static struct binding *nat_binding_find(struct nat *nat,
					su_sockaddr_t *from, 
					int fromlen)
{
  char name[64], ipname[64];
  size_t namelen;
  struct binding *b;
				       
  inet_ntop(from->su_family, SU_ADDR(from), ipname, sizeof ipname);
  snprintf(name, sizeof name,
	   from->su_family == AF_INET6 ? "[%s]:%u" : "%s:%u",
	   ipname, ntohs(from->su_port));
  namelen = strlen(name) + 1;

  for (b = nat->bindings; b; b = b->next) {
    if (memcmp(name, b->in_name, namelen) == 0)
      return b;
  }

  if (b == NULL)
    b = nat_binding_new(nat, "UDP", SOCK_DGRAM, IPPROTO_UDP, nat->symmetric, 
622
			INVALID_SOCKET, from, fromlen);
623 624 625 626 627

  return b;
}
#endif

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
/* ====================================================================== */

LIST_BODIES(static, nat_binding, struct binding, next, prev);

/* ====================================================================== */

static int new_udp(struct nat *nat, su_wait_t *wait, struct binding *dummy)
{
  int events;
  su_sockaddr_t from[1];
  socklen_t fromlen = (sizeof from);
  struct binding *b;
  ssize_t n, m;

  events = su_wait_events(wait, nat->udp_socket);

644 645
  n = su_recvfrom(nat->udp_socket, nat->buffer, sizeof nat->buffer, 0,
		  from, &fromlen);
646 647 648 649 650
  if (n < 0) {
    su_perror("new_udp: recvfrom");
    return 0;
  }

651
  b = nat_binding_new(nat, "UDP", SOCK_DGRAM, IPPROTO_UDP, nat->symmetric, 
652
		      INVALID_SOCKET, from, fromlen);
653
  if (b == NULL)
654 655
    return 0;

656
  if (nat->symmetric)
657
    m = su_send(b->out_socket, nat->buffer, n, 0);
658
  else
659 660
    m = su_sendto(b->out_socket, nat->buffer, n, 0, 
		  nat->out_address, nat->out_addrlen);
661

662 663 664
  if (nat->logging)
    printf("nat: udp out %d/%d %s => %s\n",
	   (int)m, (int)n, b->in_name, b->out_name);
665 666 667 668 669 670 671 672 673 674 675

  return 0;
}

static int udp_in_to_out(struct nat *nat, su_wait_t *wait, struct binding *b)
{
  int events;
  ssize_t n, m;

  events = su_wait_events(wait, b->in_socket);

676
  n = su_recv(b->in_socket, nat->buffer, sizeof nat->buffer, 0);
677 678 679 680 681
  if (n < 0) {
    su_perror("udp_in_to_out: recv");
    return 0;
  }

682
  if (nat->symmetric)
683
    m = su_send(b->out_socket, nat->buffer, n, 0);
684
  else
685 686
    m = su_sendto(b->out_socket, nat->buffer, n, 0,
		  nat->out_address, nat->out_addrlen);
687

688 689 690
  if (nat->logging)
    printf("nat: udp out %d/%d %s => %s\n",
	   (int)m, (int)n, b->in_name, b->out_name);
691 692 693 694 695 696 697 698 699 700 701

  return 0;
}

static int udp_out_to_in(struct nat *nat, su_wait_t *wait, struct binding *b)
{
  int events;
  ssize_t n, m;

  events = su_wait_events(wait, b->out_socket);

702
  n = su_recv(b->out_socket, nat->buffer, sizeof nat->buffer, 0);
703 704 705 706 707
  if (n < 0) {
    su_perror("udp_out_to_out: recv");
    return 0;
  }

708
  m = su_send(b->in_socket, nat->buffer, n, 0);
709

710 711 712
  if (nat->logging)
    printf("nat: udp in %d/%d %s => %s\n",
	   (int)m, (int)n, b->out_name, b->in_name);
713 714 715 716 717 718 719 720 721

  return 0;
}

/* ====================================================================== */

static int new_tcp(struct nat *nat, su_wait_t *wait, struct binding *dummy)
{
  int events;
722
  su_socket_t in_socket;
723 724 725 726 727 728
  su_sockaddr_t from[1];
  socklen_t fromlen = (sizeof from);
  struct binding *b;

  events = su_wait_events(wait, nat->tcp_socket);

729
  in_socket = accept(nat->tcp_socket, (void *)from, &fromlen);
730
  if (in_socket == INVALID_SOCKET) {
731
    su_perror("new_tcp: accept");
732 733 734
    return 0;
  }

735 736 737
  b = nat_binding_new(nat, "TCP", SOCK_STREAM, IPPROTO_TCP, 1,
		      in_socket, from, fromlen);
  if (b == NULL)
738 739 740 741 742 743 744 745 746 747 748 749
    return 0;

  return 0;
}

static int tcp_in_to_out(struct nat *nat, su_wait_t *wait, struct binding *b)
{
  int events;
  ssize_t n, m, o;

  events = su_wait_events(wait, b->in_socket);

750
  n = su_recv(b->in_socket, nat->buffer, sizeof nat->buffer, 0);
751 752 753 754 755 756
  if (n < 0) {
    su_perror("tcp_in_to_out: recv");
    return 0;
  }

  if (n == 0) {
757 758
    if (nat->logging)
      printf("nat: tcp out FIN %s => %s\n", b->in_name, b->out_name);
759 760 761 762 763 764 765
    shutdown(b->out_socket, 1);
    su_root_eventmask(nat->root, b->in_register, b->in_socket, 0);
    b->in_closed = 1;
    if (b->out_closed && b->in_closed)
      nat_binding_destroy(b);
    return 0;
  }
766

767
  for (m = 0; m < n; m += o) {
768
    o = su_send(b->out_socket, nat->buffer + m, n - m, 0);
769 770 771 772 773 774
    if (o < 0) {
      su_perror("tcp_in_to_out: send");
      break;
    }
  }

775 776 777
  if (nat->logging)
    printf("nat: tcp out %d/%d %s => %s\n",
	   (int)m, (int)n, b->in_name, b->out_name);
778 779 780 781 782 783 784 785 786 787 788

  return 0;
}

static int tcp_out_to_in(struct nat *nat, su_wait_t *wait, struct binding *b)
{
  int events;
  ssize_t n, m, o;

  events = su_wait_events(wait, b->out_socket);

789
  n = su_recv(b->out_socket, nat->buffer, sizeof nat->buffer, 0);
790 791 792 793 794 795
  if (n < 0) {
    su_perror("tcp_out_to_in: recv");
    return 0;
  }

  if (n == 0) {
796 797
    if (nat->logging)
      printf("nat: tcp out FIN %s => %s\n", b->out_name, b->in_name);
798 799 800 801 802 803 804 805 806
    shutdown(b->in_socket, 1);
    su_root_eventmask(nat->root, b->in_register, b->out_socket, 0);
    b->out_closed = 1;
    if (b->out_closed && b->in_closed)
      nat_binding_destroy(b);
    return 0;
  }

  for (m = 0; m < n; m += o) {
807
    o = su_send(b->in_socket, nat->buffer + m, n - m, 0);
808
    if (o < 0) {
809 810
      if (su_errno() != EPIPE)
	su_perror("tcp_in_to_out: send");
811 812 813 814
      break;
    }
  }

815 816 817
  if (nat->logging)
    printf("nat: tcp in %d/%d %s => %s\n",
	   (int)m, (int)n, b->out_name, b->in_name);
818 819 820

  return 0;
}
Pekka Pessi's avatar
Pekka Pessi committed
821 822 823 824 825 826

static int invalidate_binding(struct binding *b)
{
  struct nat *nat = b->nat;
  su_sockaddr_t addr[1];
  socklen_t addrlen = (sizeof addr);
827 828
  su_socket_t out;
  int out_register;
Pekka Pessi's avatar
Pekka Pessi committed
829 830 831 832
  su_wait_t wout[1];
  char name[64];

  out = su_socket(nat->fake->li_family, b->socktype, 0);
833
  if (out == INVALID_SOCKET) {
Pekka Pessi's avatar
Pekka Pessi committed
834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 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
    su_perror("new_udp: socket");
    return -1;
  }
  if (bind(out, (void *)nat->fake->li_addr, nat->fake->li_addrlen) < 0) {
    su_perror("new_udp: bind(to)");
    su_close(out);
    return -1;
  }

  if (nat->symmetric)
    if (connect(out, (void *)nat->out_address, nat->out_addrlen) < 0) {
      su_perror("new_udp: connect(to)");
      su_close(out);
      return -1;
    }

  if (su_wait_create(wout, out, SU_WAIT_IN) < 0) {
    su_perror("new_udp: su_wait_create");
    su_close(out);
    return -1;
  }

  if (b->socktype == SOCK_DGRAM)
    out_register = su_root_register(nat->root, wout, udp_out_to_in, b, 0);
  else
    out_register = su_root_register(nat->root, wout, tcp_out_to_in, b, 0);

  if (out_register < 0) {
    su_perror("new_udp: su_root_register");
    su_wait_destroy(wout);
    su_close(out);
    return -1;
  }

  su_root_deregister(nat->root, b->out_register);
  su_close(b->out_socket);

  b->out_socket = out;
  b->out_register = out_register;

  getsockname(out, (void *)addr, &addrlen);
  inet_ntop(addr->su_family, SU_ADDR(addr), name, sizeof name);
  snprintf(b->out_name, sizeof b->out_name,
	   addr->su_family == AF_INET6 ? "[%s]:%u" : "%s:%u",
	   name, ntohs(addr->su_port));

880 881
  if (nat->logging)
    printf("nat: flushed binding %s <=> %s\n", b->in_name, b->out_name);
Pekka Pessi's avatar
Pekka Pessi committed
882 883 884

  return 0;
}