test_proxy.c 34.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 26 27 28 29 30 31 32 33 34 35 36 37
/*
 * 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_proxy.c
 * @brief Extremely simple proxy and registrar for testing nua
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Thu Nov  3 22:49:46 EET 2005
 */

#include "config.h"

#include <string.h>

struct proxy;
38 39 40 41
struct domain;
union proxy_or_domain;
struct proxy_tr;
struct client_tr;
42
struct registration_entry;
43
struct binding;
44 45

#define SU_ROOT_MAGIC_T struct proxy
46 47 48
#define NTA_LEG_MAGIC_T union proxy_or_domain
#define NTA_OUTGOING_MAGIC_T struct client_tr
#define NTA_INCOMING_MAGIC_T struct proxy_tr
49

50 51 52 53 54 55 56 57
#include <sofia-sip/su_wait.h>
#include <sofia-sip/nta.h>
#include <sofia-sip/sip_header.h>
#include <sofia-sip/sip_status.h>
#include <sofia-sip/sip_util.h>
#include <sofia-sip/auth_module.h>
#include <sofia-sip/su_tagarg.h>
#include <sofia-sip/msg_addr.h>
58
#include <sofia-sip/hostdomain.h>
59 60
#include <sofia-sip/tport.h>
#include <sofia-sip/nta_tport.h>
61 62 63

#include <stdlib.h>
#include <assert.h>
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

#define LIST_PROTOS(STORAGE, PREFIX, T)			 \
STORAGE void PREFIX ##_insert(T **list, T *node),	 \
        PREFIX ##_remove(T *node)			 

#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

89 90
#include <test_proxy.h>
#include <sofia-sip/auth_module.h>
91

92 93
struct proxy {
  su_home_t    home[1];
94
  void        *magic;
95
  su_root_t   *parent;
96
  su_clone_r   clone;
97 98
  tagi_t      *tags;

99
  su_root_t   *root;
100
 
101 102
  nta_agent_t *agent;
  url_t const *uri;
103 104
  sip_route_t *lr;
  char const *lr_str;
105
  url_t const *rr_uri;
106

107
  nta_leg_t *defleg;
108 109 110

  sip_contact_t *transport_contacts;

111 112 113 114
  struct proxy_tr *stateless;
  struct proxy_tr *transactions;

  struct domain *domains;
115 116

  struct {
117
    sip_time_t session_expires, min_se;
118 119
  } prefs;
}; 
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
struct domain {
  su_home_t home[1];
  void *magic;
  struct proxy *proxy;
  struct domain *next, **prev;

  url_t *uri;

  nta_leg_t *rleg, *uleg;

  auth_mod_t *auth;
  struct registration_entry *entries;

  struct {
    sip_time_t min_expires, expires, max_expires;
    int outbound_tcp;		/**< Use inbound TCP connection as outbound */
    int authorize;
  } prefs;  

  tagi_t *tags;
};

LIST_PROTOS(static, domain, struct domain);
static int _domain_init(void *_d);
static int  domain_init(struct domain *domain);
static void domain_destroy(struct domain *domain);

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

150
LIST_PROTOS(static, registration_entry, struct registration_entry);
151
static struct registration_entry *registration_entry_new(struct domain *,
152
							 url_t const *);
153 154 155 156 157
static void registration_entry_destroy(struct registration_entry *e);

struct registration_entry
{
  struct registration_entry *next, **prev;
158
  struct domain *domain;	/* backpointer */
159
  url_t *aor;			/* address-of-record */
160 161
  struct binding *bindings;	/* list of bindings */
  sip_contact_t *contacts;
162 163
};

164 165 166
struct binding
{
  struct binding *next, **prev;
167
  sip_contact_t *contact;	/* binding */
168
  sip_time_t registered, expires; /* When registered and when expires */
169
  sip_call_id_t *call_id;
170
  uint32_t cseq;
171
  tport_t *tport;		/**< Reference to tport */
172 173 174 175
};

static struct binding *binding_new(su_home_t *home, 
				   sip_contact_t *contact,
176
				   tport_t *tport,
177
				   sip_call_id_t const *call_id,
178 179 180 181 182 183
				   uint32_t cseq,
				   sip_time_t registered, 
				   sip_time_t expires);
static void binding_destroy(su_home_t *home, struct binding *b);
static int binding_is_active(struct binding const *b)
{
184 185 186
  return
    b->expires > sip_now() && 
    (b->tport == NULL || tport_is_clear_to_send(b->tport));
187 188
}

189 190 191
LIST_PROTOS(static, proxy_tr, struct proxy_tr);
struct proxy_tr *proxy_tr_new(struct proxy *);
static void proxy_tr_destroy(struct proxy_tr *t);
192

193
struct proxy_tr
194
{
195
  struct proxy_tr *next, **prev;
196 197

  struct proxy *proxy;		/* backpointer */
198 199 200 201 202 203

  struct domain *origin;	/* originating domain */
  struct domain *domain;	/* destination domain */

  sip_time_t now;		/* when received */

204
  nta_incoming_t *server;	/* server transaction */
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
  msg_t *msg;			/* request message */
  sip_t *sip;			/* request headers */

  sip_method_t method;		/* request method */
  int status;			/* best status */
  url_t *target;		/* request-URI */

  struct client_tr *clients;	/* client transactions */

  struct registration_entry *entry;
				/* Registration entry */

  auth_mod_t *am;		/* Authentication module */
  auth_status_t *as;		/* Authentication status */
  unsigned use_auth;		/* Authentication method (401/407) to use */

  unsigned rr:1;
};

LIST_PROTOS(static, client_tr, struct client_tr);

struct client_tr
{
  struct client_tr *next, **prev;
  struct proxy_tr *t;

  int status;			/* response status */
  sip_request_t *rq;		/* request line */
  msg_t *msg;			/* request message */
  sip_t *sip;			/* request headers */
  nta_outgoing_t *client;	/* transaction */
236 237
};

238 239
LIST_BODIES(static, client_tr, struct client_tr, next, prev);

240 241
static sip_contact_t *create_transport_contacts(struct proxy *p);

242 243 244
union proxy_or_domain { struct proxy proxy[1]; struct domain domain[1]; };

static int proxy_request(union proxy_or_domain *proxy,
245
			 nta_leg_t *leg,
246
			 nta_incoming_t *irq,
247 248
			 sip_t const *sip);

249
static int domain_request(union proxy_or_domain *domain,
250 251 252 253
			  nta_leg_t *leg,
			  nta_incoming_t *irq,
			  sip_t const *sip);

254 255
static int proxy_response(struct client_tr *client,
			   nta_outgoing_t *orq,
256 257
			   sip_t const *sip);

258 259
static int close_tports(void *proxy);

260 261
static auth_challenger_t registrar_challenger[1];
static auth_challenger_t proxy_challenger[1];
262

263 264 265 266
/* Proxy entry point */
static int 
test_proxy_init(su_root_t *root, struct proxy *proxy)
{
267 268
  struct proxy_tr *t;
  struct client_tr *c;
269

270 271 272 273 274 275 276
  auth_challenger_t _proxy_challenger[1] = 
  {{ 
      SIP_407_PROXY_AUTH_REQUIRED,
      sip_proxy_authenticate_class,
      sip_proxy_authentication_info_class
    }};

277 278
  auth_challenger_t _registrar_challenger[1] = 
  {{ 
279
      SIP_401_UNAUTHORIZED,
280 281 282
      sip_www_authenticate_class,
      sip_authentication_info_class
    }};
283 284

  *proxy_challenger = *_proxy_challenger;
285 286
  *registrar_challenger = *_registrar_challenger;

287 288 289
  proxy->root = root;

  proxy->agent = nta_agent_create(root,
290
				  URL_STRING_MAKE("sip:0.0.0.0:*"),
291 292
				  NULL, NULL,
				  NTATAG_UA(0),
293
				  NTATAG_CANCEL_487(0),
294 295
				  NTATAG_SERVER_RPORT(1),
				  NTATAG_CLIENT_RPORT(1),
296
				  TAG_NEXT(proxy->tags));
297

298 299 300
  proxy->transport_contacts = create_transport_contacts(proxy);

  proxy->defleg = nta_leg_tcreate(proxy->agent,
301
				  proxy_request,
302
				  (union proxy_or_domain *)proxy,
303 304 305
				  NTATAG_NO_DIALOG(1),
				  TAG_END());

306 307 308
  proxy->prefs.session_expires = 180;
  proxy->prefs.min_se = 90;

309
  if (!proxy->defleg) 
310
    return -1;
311 312
  /* if (!proxy->example_net || !proxy->example_org || !proxy->example_com)
     return -1; */
313

314 315 316
  /* Create stateless client */
  t = su_zalloc(proxy->home, sizeof *t);
  c = su_zalloc(proxy->home, sizeof *c); 
317

318
  if (!t || !c)
319 320 321 322
    return -1;

  proxy->stateless = t;
  t->proxy = proxy;
323
  c->t = t, client_tr_insert(&t->clients, c);
324
  t->server = nta_incoming_default(proxy->agent);
325
  c->client = nta_outgoing_default(proxy->agent, proxy_response, c);
326

327
  if (!c->client || !t->server)
328 329
    return -1;

330
  proxy->uri = nta_agent_contact(proxy->agent)->m_url;
331 332 333 334 335 336
  proxy->lr_str = su_sprintf(proxy->home, "<" URL_PRINT_FORMAT ";lr>", URL_PRINT_ARGS(proxy->uri));
  proxy->lr = sip_route_make(proxy->home, proxy->lr_str);

  if (!proxy->lr)
    return -1;
  				  
337 338 339 340 341 342
  return 0;
}

static void
test_proxy_deinit(su_root_t *root, struct proxy *proxy)
{
343
  struct proxy_tr *t;
344 345

  if ((t = proxy->stateless)) {
346 347
    proxy->stateless = NULL;
    proxy_tr_destroy(t);
348 349
  }

350 351
  while (proxy->domains)
    domain_destroy(proxy->domains);
352

353 354
  nta_agent_destroy(proxy->agent);

355
  free(proxy->tags);
356 357
}

358
/* Create test proxy object */
359
struct proxy *test_proxy_create(su_root_t *root,
360
				tag_type_t tag, tag_value_t value, ...)
361 362 363 364
{
  struct proxy *p = su_home_new(sizeof *p);

  if (p) {
365 366
    ta_list ta;

367 368
    p->magic = test_proxy_create;

369 370
    p->parent = root;

371 372 373 374
    ta_start(ta, tag, value);
    p->tags = tl_llist(ta_tags(ta));
    ta_end(ta);
    
375 376
    if (su_clone_start(root,
		       p->clone,
377
		       p,
378 379 380 381 382 383 384
		       test_proxy_init,
		       test_proxy_deinit) == -1)
      su_home_unref(p->home), p = NULL;
  }

  return p;
}
385 386

/* Destroy the proxy object */
387 388 389
void test_proxy_destroy(struct proxy *p)
{
  if (p) {
390
    su_clone_wait(p->parent, p->clone);
391 392 393 394
    su_home_unref(p->home);
  }
}

395 396 397 398 399 400
/* Return the proxy URI */
url_t const *test_proxy_uri(struct proxy const *p)
{
  return p ? p->uri : NULL;
}

401 402 403 404 405 406 407 408 409 410 411 412 413
/* Return the proxy route URI */
char const *test_proxy_route_uri(struct proxy const *p,
				 sip_route_t const **return_route)
{
  if (p == NULL)
    return NULL;

  if (return_route)
    *return_route = p->lr;

  return p->lr_str;
}

414 415 416 417
void test_proxy_domain_set_expiration(struct domain *d,
				      sip_time_t min_expires, 
				      sip_time_t expires, 
				      sip_time_t max_expires)
418
{
419 420 421 422
  if (d) {
    d->prefs.min_expires = min_expires;
    d->prefs.expires = expires;
    d->prefs.max_expires = max_expires;
423 424 425
  }
}

426 427 428 429
void test_proxy_domain_get_expiration(struct domain *d,
				      sip_time_t *return_min_expires,
				      sip_time_t *return_expires,
				      sip_time_t *return_max_expires)
430
{
431 432 433 434
  if (d) {
    if (return_min_expires) *return_min_expires = d->prefs.min_expires;
    if (return_expires) *return_expires = d->prefs.expires;
    if (return_max_expires) *return_max_expires = d->prefs.max_expires;
435 436 437
  }
}

438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
void test_proxy_set_session_timer(struct proxy *p,
				  sip_time_t session_expires, 
				  sip_time_t min_se)
{
  if (p) {
    p->prefs.session_expires = session_expires;
    p->prefs.min_se = min_se;
  }
}

void test_proxy_get_session_timer(struct proxy *p,
				  sip_time_t *return_session_expires,
				  sip_time_t *return_min_se)
{
  if (p) {
    if (return_session_expires)
      *return_session_expires = p->prefs.session_expires;
    if (return_min_se) *return_min_se = p->prefs.min_se;
  }
}

459 460
void test_proxy_domain_set_outbound(struct domain *d,
				    int use_outbound)
461
{
462 463
  if (d) {
    d->prefs.outbound_tcp = use_outbound;
464 465 466
  }
}

467 468
void test_proxy_domain_get_outbound(struct domain *d,
				    int *return_use_outbound)
469
{
470
  if (d) {
471
    if (return_use_outbound)
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488
      *return_use_outbound = d->prefs.outbound_tcp;
  }
}

void test_proxy_domain_set_authorize(struct domain *d, int authorize)
{
  if (d) {
    d->prefs.authorize = authorize;
  }
}

void test_proxy_domain_get_authorize(struct domain *d,
				     int *return_authorize)
{
  if (d) {
    if (return_authorize)
      *return_authorize = d->prefs.authorize;
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
  }
}

int test_proxy_close_tports(struct proxy *p)
{
  if (p) {
    int retval = -EPROTO;

    su_task_execute(su_clone_task(p->clone), close_tports, p, &retval);

    if (retval < 0)
      return errno = -retval, -1;
    else
      return 0;
  }
  return errno = EFAULT, -1;
}

507 508
/* ---------------------------------------------------------------------- */

509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 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 608
struct domain *test_proxy_add_domain(struct proxy *p,
				     url_t const *uri,
				     tag_type_t tag, tag_value_t value, ...)
{
  struct domain *d;

  if (p == NULL || uri == NULL)
    return NULL;

  d = su_home_clone(p->home, sizeof *d);

  if (d) {
    ta_list ta;
    int init = 0;

    ta_start(ta, tag, value);

    d->magic = domain_init;

    d->proxy = p;
    d->uri = url_hdup(d->home, uri);
    d->tags = tl_adup(d->home, ta_args(ta));

    d->prefs.min_expires = 300;
    d->prefs.expires = 3600;
    d->prefs.max_expires = 36000;
    d->prefs.outbound_tcp = 0;
    d->prefs.authorize = 0;

    if (d->uri && d->tags && 
	!su_task_execute(su_clone_task(p->clone), _domain_init, d, &init)) {
      if (init == 0)
	/* OK */;
      else
	d = NULL;
    }
    else 
      su_home_unref(d->home);
  }

  return d;
}

static int _domain_init(void *_d)
{
  return domain_init(_d);
}

static int domain_init(struct domain *d)
{
  struct proxy *p = d->proxy;
  url_t uri[1];

  *uri = *d->uri;

  d->auth = auth_mod_create(p->root, TAG_NEXT(d->tags));

  /* Leg for URIs without userpart */
  d->rleg = nta_leg_tcreate(d->proxy->agent,
			    domain_request,
			    (union proxy_or_domain *)d,
			    NTATAG_NO_DIALOG(1),
			    URLTAG_URL(uri),
			    TAG_END());

  /* Leg for URIs with wildcard userpart */
  uri->url_user = "%";
  d->uleg = nta_leg_tcreate(d->proxy->agent,
			    domain_request,
			    (union proxy_or_domain *)d,
			    NTATAG_NO_DIALOG(1),
			    URLTAG_URL(uri),
			    TAG_END());

  if (d->auth && d->rleg && d->uleg) {
    domain_insert(&p->domains, d);
    return 0;
  }

  domain_destroy(d);

  return -1;
}

static void domain_destroy(struct domain *d)
{
  while (d->entries)
    registration_entry_destroy(d->entries);

  nta_leg_destroy(d->rleg), d->rleg = NULL;
  nta_leg_destroy(d->uleg), d->uleg = NULL;
  auth_mod_destroy(d->auth), d->auth = NULL;

  domain_remove(d);

  su_home_unref(d->home);
}

/* ---------------------------------------------------------------------- */

609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
static sip_contact_t *create_transport_contacts(struct proxy *p)
{
  su_home_t *home = p->home;
  sip_via_t *v;
  sip_contact_t *retval = NULL, **mm = &retval;

  if (!p->agent)
    return NULL;

  for (v = nta_agent_via(p->agent); v; v = v->v_next) {
    char const *proto = v->v_protocol;

    if (v->v_next && 
	strcasecmp(v->v_host, v->v_next->v_host) == 0 &&
	str0cmp(v->v_port, v->v_next->v_port) == 0 &&
	((proto == sip_transport_udp &&
	  v->v_next->v_protocol == sip_transport_tcp) ||
	 (proto == sip_transport_tcp &&
	  v->v_next->v_protocol == sip_transport_udp)))
      /* We have udp/tcp pair, insert URL without tport parameter */
      *mm = sip_contact_create_from_via_with_transport(home, v, NULL, NULL);
    if (*mm) mm = &(*mm)->m_next;

    *mm = sip_contact_create_from_via_with_transport(home, v, NULL, proto);

    if (*mm) mm = &(*mm)->m_next;
  }

  return retval;
}

/* ---------------------------------------------------------------------- */

642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
static int proxy_tr_with(struct proxy *proxy,
			 struct domain *domain,
			 nta_incoming_t *irq,
			 sip_t const *sip,
			 int (*process)(struct proxy_tr *));
static int proxy_transaction(struct proxy_tr *t);
static int respond_transaction(struct proxy_tr *t,
			       int status, char const *phrase,
			       tag_type_t tag, tag_value_t value,
			       ...);
static int validate_transaction(struct proxy_tr *t);
static int originating_transaction(struct proxy_tr *t);
static int challenge_transaction(struct proxy_tr *t);
static int session_timers(struct proxy_tr *t);
static int incoming_transaction(struct proxy_tr *t);
static int target_transaction(struct proxy_tr *t,
			      url_t const *target,
			      tport_t *tport);
static int process_register(struct proxy_tr *t);
static int process_options(struct proxy_tr *t);

static int proxy_ack_cancel(struct proxy_tr *t,
			    nta_incoming_t *irq,
			    sip_t const *sip);
666

667 668 669 670 671 672 673
static struct registration_entry *
registration_entry_find(struct domain const *domain, url_t const *uri);

static int proxy_request(union proxy_or_domain *pod,
			 nta_leg_t *leg,
			 nta_incoming_t *irq,
			 sip_t const *sip)
674
{
675 676 677 678 679 680 681 682 683 684 685
  assert(pod->proxy->magic = test_proxy_init);

  return proxy_tr_with(pod->proxy, NULL, irq, sip, proxy_transaction);
}

static int domain_request(union proxy_or_domain *pod,
			  nta_leg_t *leg,
			  nta_incoming_t *irq,
			  sip_t const *sip)
{
  int (*process)(struct proxy_tr *) = NULL;
686 687
  sip_method_t method = sip->sip_request->rq_method;

688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
  assert(pod->domain->magic = domain_init);

  if (leg == pod->domain->uleg)
    process = proxy_transaction;
  else if (method == sip_method_register)
    process = process_register;
  else if (method == sip_method_options) 
    process = process_options;

  if (process == NULL)
    return 501;			/* Not implemented */

  return proxy_tr_with(pod->domain->proxy, pod->domain, irq, sip, process);
}

static int proxy_tr_with(struct proxy *proxy,
			 struct domain *domain,
			 nta_incoming_t *irq,
			 sip_t const *sip,
			 int (*process)(struct proxy_tr *))
{
  struct proxy_tr *t = NULL;
  int status = 500;

  assert(proxy->magic = test_proxy_init);

  t = proxy_tr_new(proxy);
  if (t) {
    t->proxy = proxy, t->domain = domain, t->server = irq;
    t->msg = nta_incoming_getrequest(irq);
    t->sip = sip_object(t->msg);

    t->method = sip->sip_request->rq_method;
    t->target = sip->sip_request->rq_url;
    t->now = nta_incoming_received(irq, NULL);

    if (t->method != sip_method_ack && t->method != sip_method_cancel)
      nta_incoming_bind(irq, proxy_ack_cancel, t);

    if (process(t) < 200)
      return 0;

    proxy_tr_destroy(t);
  }
  else {
    nta_incoming_treply(irq, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
  }

  return status;
}

/** Forward request */
static int proxy_transaction(struct proxy_tr *t)
{
  if (originating_transaction(t))
    return t->status;

  if (validate_transaction(t))
    return t->status;

  if (session_timers(t))
    return t->status;

  if (t->domain)
    return incoming_transaction(t);

  return target_transaction(t, t->target, NULL);
}
  
static int respond_transaction(struct proxy_tr *t,
			       int status, char const *phrase,
			       tag_type_t tag, tag_value_t value,
			       ...)
{
  ta_list ta;
  void *info = NULL, *response = NULL;

  ta_start(ta, tag, value);

  if (t->as)
    info = t->as->as_info, response = t->as->as_response;
  
  if (nta_incoming_treply(t->server, t->status = status, phrase, 
			  SIPTAG_HEADER(info),
			  SIPTAG_HEADER(response),
			  ta_tags(ta)) < 0)
    t->status = status = 500;

  ta_end(ta);
  
  return status;
}

static int originating_transaction(struct proxy_tr *t)
{
  struct domain *o;
  char const *host;

  host = t->sip->sip_from->a_url->url_host;
  if (!host)
    return 0;

  for (o = t->proxy->domains; o; o = o->next)
    if (strcasecmp(host, o->uri->url_host) == 0)
      break;
793

794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
  t->origin = o;

  if (o && o->auth && o->prefs.authorize) {
    t->am = o->auth;
    t->use_auth = 407;
  }

  return 0;
}

static int validate_transaction(struct proxy_tr *t)
{
  sip_max_forwards_t *mf;

  mf = t->sip->sip_max_forwards;
809 810

  if (mf && mf->mf_count <= 1) {
811 812 813 814
    if (t->method == sip_method_options)
      return process_options(t);

    return respond_transaction(t, SIP_483_TOO_MANY_HOPS, TAG_END());
815 816
  }

817 818
  /* Remove our routes */
  while (t->sip->sip_route && 
819 820 821
	 url_has_param(t->sip->sip_route->r_url, "lr") && 
	 (url_cmp(t->proxy->lr->r_url, t->sip->sip_route->r_url) == 0 ||
	  url_cmp(t->proxy->rr_uri, t->sip->sip_route->r_url) == 0)) {
822 823 824
    sip_route_remove(t->msg, t->sip);
    /* add record-route also to the forwarded request  */
    t->rr = 1;			
825
  }
826

827 828 829 830 831 832 833 834 835 836 837 838 839 840 841
  if (t->use_auth)
    return challenge_transaction(t);
  
  return 0;
}

static int session_timers(struct proxy_tr *t)
{
  sip_t *sip = t->sip;
  sip_session_expires_t *x = NULL, x0[1];
  sip_min_se_t *min_se = NULL, min_se0[1];
  char const *require = NULL;

  if (t->method == sip_method_invite) {
    if (t->proxy->prefs.min_se) {
842
      if (!sip->sip_min_se || 
843
	  sip->sip_min_se->min_delta < t->proxy->prefs.min_se) {
844
	min_se = sip_min_se_init(min_se0);
845
	min_se->min_delta = t->proxy->prefs.min_se;
846 847 848
      }

      if (sip->sip_session_expires
849
	  && sip->sip_session_expires->x_delta < t->proxy->prefs.min_se
850 851 852
	  && sip_has_supported(sip->sip_supported, "timer")) {
	if (min_se == NULL)
	  min_se = sip->sip_min_se; assert(min_se);
853 854 855
	return respond_transaction(t, SIP_422_SESSION_TIMER_TOO_SMALL,
				   SIPTAG_MIN_SE(min_se),
				   TAG_END());
856
      }
857 858
    }

859
    if (t->proxy->prefs.session_expires) {
860
      if (!sip->sip_session_expires ||
861
	  sip->sip_session_expires->x_delta > t->proxy->prefs.session_expires) {
862
	x = sip_session_expires_init(x0);
863
	x->x_delta = t->proxy->prefs.session_expires;
864 865 866
	if (!sip_has_supported(sip->sip_supported, "timer"))
	  require = "timer";
      }
867
    }
868

869 870 871 872 873 874
    if (x || min_se || require)
      sip_add_tl(t->msg, t->sip,
		 SIPTAG_REQUIRE_STR(require),
		 SIPTAG_MIN_SE(min_se),
		 SIPTAG_SESSION_EXPIRES(x),
		 TAG_END());
875 876
  }

877 878
  return 0;
}
879

880 881 882 883
static int incoming_transaction(struct proxy_tr *t)
{
  struct registration_entry *e;
  struct binding *b;
884

885 886 887 888
#if 0
  if (sip->sip_request->rq_method == sip_method_register) 
    return process_register(proxy, irq, sip);
#endif
889

890 891 892
  t->entry = e = registration_entry_find(t->domain, t->target);
  if (e == NULL)
    return respond_transaction(t, SIP_404_NOT_FOUND, TAG_END());
893

894 895 896
  for (b = e->bindings; b; b = b->next) {
    if (binding_is_active(b)) 
      target_transaction(t, b->contact->m_url, b->tport);
897

898 899
    if (t->clients)		/* XXX - enable forking */
      break;
900
  }
901 902 903 904 905 906 907 908 909 910 911 912 913 914

  if (t->clients != NULL)
    return 0;

  return respond_transaction(t, SIP_480_TEMPORARILY_UNAVAILABLE, TAG_END());
}

static int target_transaction(struct proxy_tr *t,
			      url_t const *target,
			      tport_t *tport)
{
  struct client_tr *c = su_zalloc(t->proxy->home, sizeof *c);

  if (c == NULL)
915
    return 500;
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935

  c->t = t;
  c->msg = msg_copy(t->msg);
  c->sip = sip_object(c->msg);

  if (c->msg)
    c->rq = sip_request_create(msg_home(c->msg),
			       c->sip->sip_request->rq_method,
			       c->sip->sip_request->rq_method_name,
			       (url_string_t *)target,
			       NULL);

  msg_header_insert(c->msg, (msg_pub_t *)c->sip, (msg_header_t *)c->rq);

  if (t->rr && 0) {
    sip_record_route_t rr[1];

    *sip_record_route_init(rr)->r_url = *t->proxy->rr_uri;

    msg_header_add_dup(c->msg, (msg_pub_t *)c->sip, (msg_header_t *)rr);    
936 937
  }

938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
  if (c->rq)
    /* Forward request */
    c->client = nta_outgoing_mcreate(t->proxy->agent,
				     proxy_response, c,
				     NULL,
				     msg_ref_create(c->msg),
				     NTATAG_TPORT(tport),
				     TAG_END());

  if (c->client)
    return client_tr_insert(&t->clients, c), 0;

  msg_destroy(c->msg);
  su_free(t->proxy->home, c);

  return 500;
954 955
}

956
static int challenge_transaction(struct proxy_tr *t)
957 958
{
  auth_status_t *as;
959 960 961
  sip_t *sip = t->sip;

  assert(t->am);
962

963
  t->as = as = auth_status_new(t->proxy->home);
964
  if (!as)
965
    return respond_transaction(t, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
966 967

  as->as_method = sip->sip_request->rq_method_name;
968
  as->as_source = msg_addrinfo(t->msg);
969 970 971 972 973 974 975 976

  as->as_user_uri = sip->sip_from->a_url;
  as->as_display = sip->sip_from->a_display;

  if (sip->sip_payload)
    as->as_body = sip->sip_payload->pl_data,
      as->as_bodylen = sip->sip_payload->pl_len;

977 978 979 980 981 982
  if (t->use_auth == 401)
    auth_mod_check_client(t->am, as, sip->sip_authorization,
			  registrar_challenger);
  else
    auth_mod_check_client(t->am, as, sip->sip_proxy_authorization,
			  proxy_challenger);
983

984 985
  if (as->as_status)
    return respond_transaction(t, as->as_status, as->as_phrase, TAG_END());
986

987 988
  if (as->as_match)
    msg_header_remove(t->msg, (msg_pub_t *)sip, as->as_match);
989

990
  return 0;
991 992
}		      

993
int proxy_ack_cancel(struct proxy_tr *t,
994
		     nta_incoming_t *irq,
995 996
		     sip_t const *sip)
{
997 998 999 1000 1001
  struct client_tr *c;
  int status;

  if (sip == NULL) {		/* timeout */
    proxy_tr_destroy(t);	
1002 1003 1004
    return 0;
  }

1005
  if (sip->sip_request->rq_method != sip_method_cancel)
1006
    return 500;
1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018

  status = 200;

  for (c = t->clients; c; c = c->next) {
    if (c->client && c->status < 200)
      /*
       * We don't care about response to CANCEL (or ACK)
       * so we give NULL as callback pointer (and nta immediately 
       * destroys transaction object or marks it disposable)
       */
      if (nta_outgoing_tcancel(c->client, NULL, NULL, TAG_END()) == NULL)
	status = 500;
1019
  }
1020 1021

  return status;
1022 1023
}

1024
int proxy_response(struct client_tr *c,
1025 1026 1027 1028 1029
		   nta_outgoing_t *client,
		   sip_t const *sip)
{
  int final;

1030 1031
  assert(c->t);

1032 1033 1034 1035
  if (sip) {
    msg_t *response = nta_outgoing_getresponse(client);
    final = sip->sip_status->st_status >= 200;
    sip_via_remove(response, sip_object(response));
1036
    nta_incoming_mreply(c->t->server, response);
1037 1038 1039
  }
  else {
    final = 1;
1040
    respond_transaction(c->t, SIP_408_REQUEST_TIMEOUT, TAG_END());
1041 1042 1043
  }

  if (final)
1044
    proxy_tr_destroy(c->t);
1045 1046 1047 1048

  return 0;
}

1049 1050
struct proxy_tr *
proxy_tr_new(struct proxy *proxy)
1051
{
1052
  struct proxy_tr *t;
1053 1054 1055 1056

  t = su_zalloc(proxy->home, sizeof *t);
  if (t) {
    t->proxy = proxy;
1057
    proxy_tr_insert(&proxy->transactions, t);
1058 1059 1060 1061 1062
  }
  return t;
}

static
1063
void proxy_tr_destroy(struct proxy_tr *t)
1064
{
1065 1066
  struct client_tr *c;

1067 1068
  if (t == t->proxy->stateless)
    return;
1069

1070
  proxy_tr_remove(t);
1071

1072 1073 1074 1075 1076 1077 1078 1079 1080
  if (t->as)
    su_home_unref(t->as->as_home), t->as = NULL;
  
  while (t->clients) {
    client_tr_remove(c = t->clients);
    nta_outgoing_destroy(c->client), c->client = NULL;
    msg_destroy(c->msg), c->msg = NULL;
    su_free(t->proxy->home, c);
  }
1081

1082
  nta_incoming_destroy(t->server);
1083

1084 1085
  su_free(t->proxy->home, t);
}
1086

1087
LIST_BODIES(static, proxy_tr, struct proxy_tr, next, prev);
1088

1089
/* ---------------------------------------------------------------------- */
1090

1091
static int process_options(struct proxy_tr *t)
1092
{
1093 1094 1095
  return respond_transaction(t, SIP_200_OK,
			     SIPTAG_CONTACT(t->proxy->transport_contacts),
			     TAG_END());
1096 1097 1098 1099
}

/* ---------------------------------------------------------------------- */

1100 1101 1102 1103
static int check_received_contact(struct proxy_tr *t);
static int validate_contacts(struct proxy_tr *t);
static int check_out_of_order(struct proxy_tr *t);
static int update_bindings(struct proxy_tr *t);
1104

1105
int process_register(struct proxy_tr *t)
1106
{
1107 1108 1109 1110 1111 1112 1113 1114 1115
  /* This is before authentication because we want to be bug-compatible */
  if (check_received_contact(t))
    return t->status;

  if (t->domain->auth) {
    t->am = t->domain->auth, t->use_auth = 401;
    if (challenge_transaction(t))
      return t->status;
  }
1116

1117 1118
  if (validate_contacts(t))
    return t->status;
1119

1120
  t->entry = registration_entry_find(t->domain, t->sip->sip_to->a_url);
1121

1122 1123 1124 1125
  if (check_out_of_order(t))
    return t->status;
  
  return update_bindings(t);
1126 1127
}

1128
static int check_received_contact(struct proxy_tr *t)
1129
{
1130
  sip_t *sip = t->sip;
1131 1132 1133 1134 1135 1136
  sip_contact_t *m = sip->sip_contact;
  sip_via_t *v = sip->sip_via;

  if (m && v && v->v_received && m->m_url->url_host
      && strcasecmp(v->v_received, m->m_url->url_host) 
      && host_is_ip_address(m->m_url->url_host))
1137
    return respond_transaction(t, 406, "Unacceptable Contact", TAG_END());
1138

1139
  return 0;
1140 1141
}

1142 1143
/* Validate expiration times */
static int validate_contacts(struct proxy_tr *t)
1144
{
1145 1146 1147
  sip_contact_t const *m = t->sip->sip_contact;
  sip_expires_t const *ex = t->sip->sip_expires;
  sip_date_t const *date = t->sip->sip_date;
1148 1149
  sip_time_t expires;

1150 1151 1152 1153 1154 1155 1156 1157
  if (m && m->m_url->url_type == url_any) {
    if (!ex || ex->ex_delta || ex->ex_time || m->m_next)
      return respond_transaction(t, SIP_400_BAD_REQUEST, TAG_END());
    return 0;
  }

  for (; m; m = m->m_next) {
    expires = sip_contact_expires(m, ex, date, t->domain->prefs.expires, t->now);
1158
    
1159 1160 1161 1162 1163 1164 1165 1166
    if (expires > 0 && expires < t->domain->prefs.min_expires) {
      sip_min_expires_t me[1];

      sip_min_expires_init(me)->me_delta = t->domain->prefs.min_expires;

      return respond_transaction(t, SIP_423_INTERVAL_TOO_BRIEF,
				 SIPTAG_MIN_EXPIRES(me),
				 TAG_END());
1167 1168
    }
  }
1169 1170 1171 1172

  return 0;
}

1173
/** Check for out-of-order register request */
1174
static int check_out_of_order(struct proxy_tr *t)
1175 1176
{
  struct binding const *b;
1177 1178
  sip_call_id_t const *id = t->sip->sip_call_id;
  uint32_t cseq = t->sip->sip_cseq->cs_seq;
1179
  sip_contact_t *m;
1180

1181
  if (t->entry == NULL || !t->sip->sip_contact)
1182 1183
    return 0;

1184 1185
  /* RFC 3261 subsection 10.3 step 6 and step 7 (p. 66): */
  /* Check for reordered register requests */
1186
  for (b = t->entry->bindings; b; b = b->next) {
1187
    if (binding_is_active(b) &&
1188 1189 1190
	strcmp(id->i_id, b->call_id->i_id) == 0 &&
	cseq <= b->cseq) {
      for (m = t->sip->sip_contact; m; m = m->m_next) {
1191 1192
	if (m->m_url->url_type == url_any ||
	    url_cmp_all(m->m_url, b->contact->m_url) == 0)
1193 1194
	  return respond_transaction(t, SIP_500_INTERNAL_SERVER_ERROR,
				     TAG_END());
1195
      }
1196 1197 1198 1199
    }
  }

  return 0;
1200 1201 1202
}

static struct registration_entry *
1203
registration_entry_find(struct domain const *d, url_t const *uri)
1204 1205 1206 1207
{
  struct registration_entry *e;

  /* Our routing table */
1208
  for (e = d->entries; e; e = e->next) {
1209 1210 1211
    if (url_cmp(uri, e->aor) == 0)
      return e;
  }
1212

1213 1214 1215 1216
  return NULL;
}

static struct registration_entry *
1217
registration_entry_new(struct domain *d, url_t const *aor)
1218 1219 1220
{
  struct registration_entry *e;

1221 1222 1223 1224
  if (d == NULL)
    return NULL;

  e = su_zalloc(d->home, sizeof *e); 
1225 1226 1227
  if (!e) 
    return NULL;

1228 1229
  e->domain = d;
  e->aor = url_hdup(d->home, aor);
1230
  if (!e->aor) {
1231
    su_free(d->home, e);
1232 1233 1234
    return NULL;
  }

1235
  registration_entry_insert(&d->entries, e);
1236 1237 1238 1239 1240 1241 1242 1243 1244

  return e;
}

static void
registration_entry_destroy(struct registration_entry *e)
{
  if (e) {
    registration_entry_remove(e);
1245
    su_free(e->domain->home, e->aor);
1246
    while (e->bindings)
1247 1248 1249
      binding_destroy(e->domain->home, e->bindings);
    msg_header_free(e->domain->home, (void *)e->contacts);
    su_free(e->domain->home, e);
1250 1251 1252
  }
}

1253 1254 1255 1256 1257
sip_contact_t *entry_contacts(struct registration_entry *entry)
{
  return entry ? entry->contacts : NULL;
}

1258
LIST_BODIES(static, registration_entry, struct registration_entry, next, prev);
1259 1260 1261 1262 1263 1264 1265

/* ---------------------------------------------------------------------- */
/* Bindings */

static
struct binding *binding_new(su_home_t *home, 
			    sip_contact_t *contact,
1266
			    tport_t *tport,
1267
			    sip_call_id_t const *call_id,
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
			    uint32_t cseq,
			    sip_time_t registered, 
			    sip_time_t expires)
{
  struct binding *b;
  
  b = su_zalloc(home, sizeof *b);

  if (b) {
    sip_contact_t m[1];
    *m = *contact; m->m_next = NULL;

    b->contact = sip_contact_dup(home, m);
1281
    b->tport = tport_ref(tport);
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305
    b->call_id = sip_call_id_dup(home, call_id);
    b->cseq = cseq;
    b->registered = registered;
    b->expires = expires;

    if (!b->contact || !b->call_id)
      binding_destroy(home, b), b = NULL;

    if (b)
      msg_header_remove_param(b->contact->m_common, "expires");
  }
  
  return b;
}

static
void binding_destroy(su_home_t *home, struct binding *b)
{
  if (b->prev) {
    if ((*b->prev = b->next))
      b->next->prev = b->prev;
  }
  msg_header_free(home, (void *)b->contact);
  msg_header_free(home, (void *)b->call_id);
1306
  tport_unref(b->tport);
1307 1308 1309
  su_free(home, b);
}

1310
static int update_bindings(struct proxy_tr *t)
1311
{
1312
  struct domain *d = t->domain;
1313 1314
  struct binding *b, *old, *next, *last, *bindings = NULL, **bb = &bindings;
  sip_contact_t *m;
1315 1316 1317 1318
  sip_call_id_t const *id = t->sip->sip_call_id;
  uint32_t cseq = t->sip->sip_cseq->cs_seq;
  sip_expires_t *ex = t->sip->sip_expires;
  sip_date_t *date = t->sip->sip_date;
1319
  sip_time_t expires;
1320
  tport_t *tport = NULL;
1321 1322 1323 1324 1325 1326 1327 1328
  sip_contact_t *contacts = NULL, **mm = &contacts; 
  void *tbf;

  if (t->sip->sip_contact == NULL) {
    if (t->entry)
      contacts = t->entry->contacts;
    goto ok200;
  }
1329

1330 1331 1332 1333
  if (t->entry == NULL)
    t->entry = registration_entry_new(d, t->sip->sip_to->a_url);
  if (t->entry == NULL)
    return respond_transaction(t, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1334

1335 1336 1337
  if (d->prefs.outbound_tcp && 
      str0casecmp(t->sip->sip_via->v_protocol, sip_transport_tcp) == 0)
    tport = nta_incoming_transport(t->proxy->agent, t->server, NULL);
1338

1339
  /* Create new bindings */
1340
  for (m = t->sip->sip_contact; m; m = m->m_next) {
1341 1342 1343
    if (m->m_url->url_type == url_any)
      break;
    
1344
    expires = sip_contact_expires(m, ex, date, d->prefs.expires, t->now);
1345

1346 1347
    if (expires > d->prefs.max_expires)
      expires = d->prefs.max_expires;
1348 1349 1350

    msg_header_remove_param(m->m_common, "expires");

1351
    b = binding_new(d->home, m, tport, id, cseq, t->now, t->now + expires);
1352 1353 1354 1355 1356 1357
    if (!b)
      break;

    *bb = b, b->prev = bb, bb = &b->next;
  }

1358 1359
  tport_unref(tport);

1360 1361 1362 1363
  last = NULL;

  if (m == NULL) {
    /* Merge new bindings with old ones */
1364
    for (old = t->entry->bindings; old; old = next) {
1365 1366 1367
      next = old->next;

      for (b = bindings; b != last; b = b->next) {
1368
	if (url_cmp_all(old->contact->m_url, b->contact->m_url) != 0) 
1369 1370 1371 1372 1373
	  continue;

	if (strcmp(old->call_id->i_id, b->call_id->i_id) == 0) {
	  b->registered = old->registered;
	}
1374
	binding_destroy(d->home, old);
1375 1376 1377 1378
	break;
      }
    }

1379
    for (bb = &t->entry->bindings; *bb; bb = &(*bb)->next)
1380 1381 1382 1383 1384 1385 1386
      ;

    if ((*bb = bindings))
      bindings->prev = bb;
  }
  else if (m->m_url->url_type == url_any) {
    /* Unregister all */
1387 1388
    for (b = t->entry->bindings; b; b = b->next) {
      b->expires = t->now;
1389 1390 1391 1392
    }
  }
  else {
    /* Infernal error */
1393

1394 1395
    for (old = bindings; old; old = next) {
      next = old->next;
1396
      binding_destroy(d->home, old);
1397
    }
1398

1399
    return respond_transaction(t, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1400 1401
  }

1402
  for (b = t->entry->bindings; b; b = b->next) {
1403
    char const *expires;
1404 1405

    if (b->expires <= t->now)
1406
      continue;
1407 1408

    *mm = sip_contact_copy(d->home, b->contact);
Pekka Pessi's avatar
Pekka Pessi committed
1409
    if (*mm) {
1410 1411 1412
      expires = su_sprintf(d->home, "expires=%u", 
			   (unsigned)(b->expires - t->now));
      msg_header_add_param(d->home, (*mm)->m_common, expires);
Pekka Pessi's avatar
Pekka Pessi committed
1413 1414
      mm = &(*mm)->m_next;
    }
1415 1416
  }

1417 1418 1419 1420 1421 1422 1423 1424
  tbf = t->entry->contacts;
  t->entry->contacts = contacts;
  msg_header_free(d->home, tbf);

 ok200:
  return respond_transaction(t, SIP_200_OK,
			     SIPTAG_CONTACT(contacts),
			     TAG_END());
1425
}
1426 1427 1428 1429 1430 1431

/* ---------------------------------------------------------------------- */

static int close_tports(void *_proxy)
{
  struct proxy *p = _proxy;
1432
  struct domain *d;
1433 1434 1435 1436
  struct registration_entry *e;
  struct binding *b;
  
  /* Close all outbound transports */
1437 1438 1439 1440 1441 1442 1443 1444
  for (d = p->domains; d; d = d->next) {
    for (e = d->entries; e; e = e->next) {
      for (b = e->bindings; b; b = b->next) {
	if (b->tport) {
	  tport_shutdown(b->tport, 1);
	  tport_unref(b->tport);
	  b->tport = NULL;
	}
1445 1446 1447 1448 1449 1450
      }
    }
  }
  
  return 0;
}