test_nat.c 24.2 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

#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

82
#include "test_nat.h"
83 84 85 86 87 88 89 90 91 92 93

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
  struct nat_filter *in_filters, *out_filters;

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

101 102 103 104 105 106 107 108
  /* 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;
109

110
  su_socket_t udp_socket, tcp_socket;
111 112 113 114 115 116 117 118 119 120 121
  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 */
122
  int socktype, protocol;
123
  su_socket_t in_socket, out_socket;
124 125 126 127 128
  int in_register, out_register;
  int in_closed, out_closed;
  char in_name[64], out_name[64];
};

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

138 139 140 141 142 143 144
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
145
static void flush_bindings(struct nat *nat);
146
static int invalidate_bindings(void *nat);
Pekka Pessi's avatar
Pekka Pessi committed
147

148 149 150 151 152 153 154 155
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
156 157
static int invalidate_binding(struct binding *b);

158 159 160 161 162
LIST_PROTOS(static, nat_filter, struct nat_filter);

struct nat_filter
{
  struct nat_filter *next, **prev;
163 164
  size_t (*condition)(void *arg, void *message, size_t len);
  void *arg;
165 166
};

167
/* nat entry point */
168
static int
169 170 171 172 173
test_nat_init(su_root_t *root, struct nat *nat)
{
  su_localinfo_t *li, hints[1] = {{ 0 }};
  int error;
  unsigned port = 0, port0 = 0;
174
  su_sockaddr_t su[1];
175 176 177 178
  socklen_t sulen;
  su_wait_t wait[1];

  nat->root = root;
179
  nat->udp_socket = INVALID_SOCKET, nat->tcp_socket = INVALID_SOCKET;
180

181 182 183 184 185
  tl_gets(nat->tags, 
	  TESTNATTAG_SYMMETRIC_REF(nat->symmetric),
	  TESTNATTAG_LOGGING_REF(nat->logging),
	  TAG_END());

186 187
  hints->li_scope = LI_SCOPE_HOST | LI_SCOPE_SITE | LI_SCOPE_GLOBAL;

188 189 190 191 192
  error = su_getlocalinfo(hints, &nat->localinfo);
  if (error) {
    fprintf(stderr, "test_nat: su_getlocalinfo: %s\n", su_gli_strerror(error));
    return -1;
  }
193

194 195 196 197 198 199 200 201 202 203 204 205 206
  /* 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;

207 208
  memcpy(su, li->li_addr, sulen = li->li_addrlen);
  memset(SU_ADDR(su), 0, SU_ADDRLEN(su));
209 210 211 212 213 214

  nat->private = li;

  /* Bind TCP and UDP to same port */
  for (;;) {
    nat->udp_socket = su_socket(li->li_family, SOCK_DGRAM, IPPROTO_UDP);
215
    if (nat->udp_socket == INVALID_SOCKET)
216 217 218 219 220 221 222 223
      return -1;

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

224
      fprintf(stderr, "test_nat: port %u: %s\n",
225 226
	      port, su_strerror(su_errno()));
      su_close(nat->udp_socket);
227

228
      nat->udp_socket = INVALID_SOCKET;
229 230 231 232 233 234 235 236 237 238 239 240

      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) {
241
      su_perror("nat: getsockname(udp_socket)");
242 243 244 245 246 247 248 249 250 251 252 253
      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);
254
    if (nat->tcp_socket == INVALID_SOCKET)
255
      return -1;
256

257 258
    if (bind(nat->tcp_socket, (void *)su, sulen) < 0) {
      su_close(nat->tcp_socket);
259
      nat->tcp_socket = INVALID_SOCKET;
260

261
      fprintf(stderr, "test_nat: port %u: %s\n",
262 263 264 265 266 267 268 269 270 271 272 273 274
	      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;
275
  }
276

277 278
  memcpy(nat->in_address, li->li_addr, nat->in_addrlen = li->li_addrlen);
  nat->in_address->su_port = su->su_port;
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310

  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;
  }
311

312 313 314 315 316 317
  return 0;
}

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

Pekka Pessi's avatar
Pekka Pessi committed
320 321 322 323 324
  if (nat->tcp_register)
    su_root_deregister(root, nat->tcp_register);
  if (nat->udp_register)
    su_root_deregister(root, nat->udp_register);

325
  if (nat->udp_socket != INVALID_SOCKET)
326
    su_close(nat->udp_socket);
327
  if (nat->tcp_socket != INVALID_SOCKET)
328
    su_close(nat->tcp_socket);
Pekka Pessi's avatar
Pekka Pessi committed
329

330
  su_freelocalinfo(nat->localinfo);
Pekka Pessi's avatar
Pekka Pessi committed
331

332 333 334
  free(nat->tags);
}

335
struct nat *test_nat_create(su_root_t *root,
336 337 338 339 340 341 342 343 344 345 346 347 348 349
			    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);
350

351 352
    if (su_clone_start(root,
		       nat->clone,
353
		       nat,
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
		       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. */
371
int test_nat_private(struct nat *nat, void *address, socklen_t *return_addrlen)
372 373 374
{
  if (nat == NULL || address == NULL || return_addrlen == NULL)
    return su_seterrno(EFAULT);
375

376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
  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;
  }

398
  if ((size_t)addrlen > sizeof nat->out_address)
399
    return su_seterrno(EINVAL);
400

401
  for (li = nat->localinfo; li; li = li->li_next) {
402 403
    if (li != nat->private &&
	li->li_scope == LI_SCOPE_HOST &&
404 405 406
	li->li_family == su->su_family)
      break;
  }
407

408 409 410 411 412 413 414 415 416
  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
417
  su_clone_pause(nat->clone);
418 419
  memcpy(nat->out_address, address, nat->out_addrlen = addrlen);
  nat->fake = li;
Pekka Pessi's avatar
Pekka Pessi committed
420
  su_clone_resume(nat->clone);
421 422 423 424

  return 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
425 426 427 428 429 430
int test_nat_flush(struct nat *nat)
{
  if (nat == NULL)
    return su_seterrno(EFAULT);

  return su_task_execute(su_clone_task(nat->clone), 
431
			 invalidate_bindings, nat, NULL);
Pekka Pessi's avatar
Pekka Pessi committed
432 433
}

434 435
/* ====================================================================== */

436 437 438 439 440
struct binding *nat_binding_new(struct nat *nat,
				char const *protoname,
				int socktype,
				int protocol,
				int connected,
441
				su_socket_t in_socket,
442 443
				su_sockaddr_t *from,
				socklen_t fromlen)
444
{
445
  struct binding *b;
446

447 448 449
  if (nat->fake == NULL) {	/* Xyzzy */
    fprintf(stderr, "test_nat: fake address missing\n");
    su_close(in_socket);
450
    return NULL;
451
  }
452

453 454 455 456 457 458
  b = su_zalloc(nat->home, sizeof *b);
  if (b == NULL) {
    su_perror("nat_binding_new: su_zalloc");
    su_close(in_socket);
    return 0;
  }
459

Pekka Pessi's avatar
Pekka Pessi committed
460 461
  b->nat = nat;
  b->socktype = socktype;
462
  b->protocol = protocol;
463
  b->in_socket = in_socket, b->out_socket = INVALID_SOCKET;
464 465
  b->in_register = -1, b->out_register = -1;

466 467 468 469 470 471 472 473 474 475 476 477 478 479
  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;
480
  su_socket_t out_socket;
481 482 483 484 485 486 487 488 489 490 491
  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;    
  
492
  if (b->in_socket == INVALID_SOCKET) {
493 494 495
    int in_socket;

    in_socket = su_socket(from->su_family, b->socktype, b->protocol);
496
    if (in_socket == INVALID_SOCKET) {
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
      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);
516
  if (out_socket == INVALID_SOCKET) {
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
    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);
534
  su_inet_ntop(addr->su_family, SU_ADDR(addr), ipname, sizeof ipname);
535
  snprintf(b->in_name, sizeof b->in_name,
536
	   addr->su_family == AF_INET6 ? "[%s]:%u" : "%s:%u",
537
	   ipname, ntohs(addr->su_port));
538 539

  getsockname(out_socket, (void *)addr, &addrlen);
540
  su_inet_ntop(addr->su_family, SU_ADDR(addr), ipname, sizeof ipname);
541 542
  snprintf(b->out_name, sizeof b->out_name,
	   addr->su_family == AF_INET6 ? "[%s]:%u" : "%s:%u",
543
	   ipname, ntohs(addr->su_port));
544

545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
  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
567 568
  nat_binding_insert(&nat->bindings, b);

569 570 571
  if (nat->logging)
    printf("nat: new %s binding %s <=> %s\n",
	   protoname, b->in_name, b->out_name);
572 573

  return 0;
574 575 576 577 578 579 580 581 582 583 584 585
}

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
586 587 588 589 590 591 592 593 594 595 596 597 598 599
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);
  }
}

600
static int invalidate_bindings(void *arg)
Pekka Pessi's avatar
Pekka Pessi committed
601
{
602
  struct nat *nat = arg;
Pekka Pessi's avatar
Pekka Pessi committed
603 604 605 606 607
  struct binding *b;

  for (b = nat->bindings; b; b = b->next) {
    invalidate_binding(b);
  }
608
  return 0;
Pekka Pessi's avatar
Pekka Pessi committed
609 610
}

611 612 613 614 615 616 617 618 619
#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;
				       
620
  su_inet_ntop(from->su_family, SU_ADDR(from), ipname, sizeof ipname);
621 622 623 624 625 626 627 628 629 630 631 632
  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, 
633
			INVALID_SOCKET, from, fromlen);
634 635 636 637 638

  return b;
}
#endif

639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654
/* ====================================================================== */

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);

655 656
  n = su_recvfrom(nat->udp_socket, nat->buffer, sizeof nat->buffer, 0,
		  from, &fromlen);
657 658 659 660 661
  if (n < 0) {
    su_perror("new_udp: recvfrom");
    return 0;
  }

662
  b = nat_binding_new(nat, "UDP", SOCK_DGRAM, IPPROTO_UDP, nat->symmetric, 
663
		      INVALID_SOCKET, from, fromlen);
664
  if (b == NULL)
665 666
    return 0;

667
  if (nat->symmetric)
668
    m = su_send(b->out_socket, nat->buffer, n, 0);
669
  else
670 671
    m = su_sendto(b->out_socket, nat->buffer, n, 0, 
		  nat->out_address, nat->out_addrlen);
672

673 674 675
  if (nat->logging)
    printf("nat: udp out %d/%d %s => %s\n",
	   (int)m, (int)n, b->in_name, b->out_name);
676 677 678 679 680 681 682 683

  return 0;
}

static int udp_in_to_out(struct nat *nat, su_wait_t *wait, struct binding *b)
{
  int events;
  ssize_t n, m;
684
  size_t len, filtered;
685
  struct nat_filter *f;
686 687 688

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

689
  n = su_recv(b->in_socket, nat->buffer, sizeof nat->buffer, 0);
690
  if (n == -1) {
691 692 693 694
    su_perror("udp_in_to_out: recv");
    return 0;
  }

695 696
  len = (size_t)n;

697
  for (f = nat->out_filters; f; f = f->next) {
698
    filtered = f->condition(f->arg, nat->buffer, len);
699
    if (filtered != len) {
700
      if (nat->logging)
701 702 703 704 705
	printf("nat: udp filtered "MOD_ZU" from %s => "MOD_ZU" to %s\n",
	       len, b->in_name, filtered, b->out_name);
      if (filtered == 0)
	return 0;
      len = filtered;
706 707 708
    }
  }

709
  if (nat->symmetric)
710
    m = su_send(b->out_socket, nat->buffer, len, 0);
711
  else
712
    m = su_sendto(b->out_socket, nat->buffer, len, 0,
713
		  nat->out_address, nat->out_addrlen);
714

715 716 717
  if (nat->logging)
    printf("nat: udp out %d/%d %s => %s\n",
	   (int)m, (int)n, b->in_name, b->out_name);
718 719 720 721 722 723 724 725

  return 0;
}

static int udp_out_to_in(struct nat *nat, su_wait_t *wait, struct binding *b)
{
  int events;
  ssize_t n, m;
726
  size_t len, filtered;
727
  struct nat_filter *f;
728 729 730

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

731
  n = su_recv(b->out_socket, nat->buffer, sizeof nat->buffer, 0);
732 733 734 735 736
  if (n < 0) {
    su_perror("udp_out_to_out: recv");
    return 0;
  }

737 738
  len = (size_t)n;

739
  for (f = nat->in_filters; f; f = f->next) {
740
    filtered = f->condition(f->arg, nat->buffer, len);
741
    if (filtered != len) {
742
      if (nat->logging)
743 744 745 746 747
	printf("nat: udp filtered "MOD_ZU" from %s => "MOD_ZU" to %s\n",
	       len, b->out_name, filtered, b->in_name);
      if (filtered == 0)
	return 0;
      len = filtered;
748 749 750
    }
  }

751
  m = su_send(b->in_socket, nat->buffer, n, 0);
752

753 754 755
  if (nat->logging)
    printf("nat: udp in %d/%d %s => %s\n",
	   (int)m, (int)n, b->out_name, b->in_name);
756 757 758 759 760 761 762 763 764

  return 0;
}

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

static int new_tcp(struct nat *nat, su_wait_t *wait, struct binding *dummy)
{
  int events;
765
  su_socket_t in_socket;
766 767 768 769 770 771
  su_sockaddr_t from[1];
  socklen_t fromlen = (sizeof from);
  struct binding *b;

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

772
  in_socket = accept(nat->tcp_socket, (void *)from, &fromlen);
773
  if (in_socket == INVALID_SOCKET) {
774
    su_perror("new_tcp: accept");
775 776 777
    return 0;
  }

778 779 780
  b = nat_binding_new(nat, "TCP", SOCK_STREAM, IPPROTO_TCP, 1,
		      in_socket, from, fromlen);
  if (b == NULL)
781 782 783 784 785 786 787 788 789 790 791 792
    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);

793
  n = su_recv(b->in_socket, nat->buffer, sizeof nat->buffer, 0);
794 795 796 797 798 799
  if (n < 0) {
    su_perror("tcp_in_to_out: recv");
    return 0;
  }

  if (n == 0) {
800 801
    if (nat->logging)
      printf("nat: tcp out FIN %s => %s\n", b->in_name, b->out_name);
802 803 804 805 806 807 808
    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;
  }
809

810
  for (m = 0; m < n; m += o) {
811
    o = su_send(b->out_socket, nat->buffer + m, n - m, 0);
812 813 814 815 816 817
    if (o < 0) {
      su_perror("tcp_in_to_out: send");
      break;
    }
  }

818 819 820
  if (nat->logging)
    printf("nat: tcp out %d/%d %s => %s\n",
	   (int)m, (int)n, b->in_name, b->out_name);
821 822 823 824 825 826 827 828 829 830 831

  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);

832
  n = su_recv(b->out_socket, nat->buffer, sizeof nat->buffer, 0);
833 834 835 836 837 838
  if (n < 0) {
    su_perror("tcp_out_to_in: recv");
    return 0;
  }

  if (n == 0) {
839 840
    if (nat->logging)
      printf("nat: tcp out FIN %s => %s\n", b->out_name, b->in_name);
841 842 843 844 845 846 847 848 849
    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) {
850
    o = su_send(b->in_socket, nat->buffer + m, n - m, 0);
851
    if (o < 0) {
852 853
      if (su_errno() != EPIPE)
	su_perror("tcp_in_to_out: send");
854 855 856 857
      break;
    }
  }

858 859 860
  if (nat->logging)
    printf("nat: tcp in %d/%d %s => %s\n",
	   (int)m, (int)n, b->out_name, b->in_name);
861 862 863

  return 0;
}
Pekka Pessi's avatar
Pekka Pessi committed
864 865 866 867 868 869

static int invalidate_binding(struct binding *b)
{
  struct nat *nat = b->nat;
  su_sockaddr_t addr[1];
  socklen_t addrlen = (sizeof addr);
870 871
  su_socket_t out;
  int out_register;
Pekka Pessi's avatar
Pekka Pessi committed
872 873 874 875
  su_wait_t wout[1];
  char name[64];

  out = su_socket(nat->fake->li_family, b->socktype, 0);
876
  if (out == INVALID_SOCKET) {
Pekka Pessi's avatar
Pekka Pessi committed
877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917
    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);
918
  su_inet_ntop(addr->su_family, SU_ADDR(addr), name, sizeof name);
Pekka Pessi's avatar
Pekka Pessi committed
919 920 921 922
  snprintf(b->out_name, sizeof b->out_name,
	   addr->su_family == AF_INET6 ? "[%s]:%u" : "%s:%u",
	   name, ntohs(addr->su_port));

923 924
  if (nat->logging)
    printf("nat: flushed binding %s <=> %s\n", b->in_name, b->out_name);
Pekka Pessi's avatar
Pekka Pessi committed
925 926 927

  return 0;
}
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954

LIST_BODIES(static, nat_filter, struct nat_filter, next, prev);

struct args {
  struct nat *nat;
  struct nat_filter *f;
  int outbound;
};

int execute_nat_filter_insert(void *_args)
{
  struct args *a = (struct args *)_args;
  if (a->outbound)
    nat_filter_insert(&a->nat->out_filters, a->f);
  else
    nat_filter_insert(&a->nat->in_filters, a->f);
  return 0;
}

int execute_nat_filter_remove(void *_args)
{
  struct args *a = (struct args *)_args;
  nat_filter_remove(a->f);
  return 0;
}

struct nat_filter *test_nat_add_filter(struct nat *nat,
955 956
				       size_t (*condition)(void *arg,
							   void *message,
957
							   size_t len),
958
				       void *arg,
959 960 961 962 963 964 965 966 967 968 969 970 971
				       int outbound)
{
  struct args a[1];

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

  a->nat = nat;
  a->f = su_zalloc(nat->home, sizeof *a->f);
  a->outbound = outbound;

  if (a->f) {
    a->f->condition = condition;
972
    a->f->arg = arg;
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999
    if (su_task_execute(su_clone_task(nat->clone),
			execute_nat_filter_insert, a, NULL) < 0)
      su_free(nat->home, a->f), a->f = NULL;
  }

  return a->f;
}


int test_nat_remove_filter(struct nat *nat,
			   struct nat_filter *filter)
{
  struct args a[1];

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

  a->nat = nat;
  a->f = filter;
  
  if (su_task_execute(su_clone_task(nat->clone),
		      execute_nat_filter_remove, a, NULL) < 0)
    return -1;

  su_free(nat->home, filter);
  return 0;
}