test_s2.c 37.5 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 38
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2008 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 s2tester.c
 * @brief 2nd test Suite for Sofia SIP User Agent Engine
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
 * @date Created: Wed Apr 30 12:48:27 EEST 2008 ppessi
 */

#include "config.h"

#undef NDEBUG

#define TP_MAGIC_T struct tp_magic_s

Pekka Pessi's avatar
Pekka Pessi committed
39
#include "test_s2.h"
40 41

#include <sofia-sip/sip_header.h>
42
#include <sofia-sip/sip_status.h>
43 44 45 46 47 48 49 50 51 52 53
#include <sofia-sip/msg_addr.h>
#include <sofia-sip/su_log.h>
#include <sofia-sip/su_tagarg.h>
#include <sofia-sip/su_alloc.h>

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <limits.h>
#include <time.h>

Pekka Pessi's avatar
Pekka Pessi committed
54
/* -- Module types ------------------------------------------------------ */
55 56 57 58 59 60 61

struct tp_magic_s
{
  sip_via_t *via;
  sip_contact_t *contact;
};

Pekka Pessi's avatar
Pekka Pessi committed
62
/* -- Module prototypes ------------------------------------------------- */
63 64

static msg_t *s2_msg(int flags);
65 66
static int s2_complete_response(msg_t *response,
				int status, char const *phrase,
67 68 69
				msg_t *request);
static char *s2_generate_tag(su_home_t *home);

Pekka Pessi's avatar
Pekka Pessi committed
70
/* -- Module globals ---------------------------------------------------- */
71

72
struct tester *s2;
73

74
static char const *_s2case = "0.0";
75 76
static unsigned s2_tag_generator = 0;

Pekka Pessi's avatar
Pekka Pessi committed
77
/* -- Globals ----------------------------------------------------------- */
78

79 80 81 82 83 84 85 86 87
unsigned s2_default_registration_duration = 3600;

char const s2_auth_digest_str[] =
  "Digest realm=\"s2test\", "
  "nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", "
  "qop=\"auth\", "
  "algorithm=\"MD5\"";

char const s2_auth_credentials[] = "Digest:\"s2test\":abc:abc";
88

Pekka Pessi's avatar
Pekka Pessi committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
char const s2_auth2_digest_str[] =
  "Digest realm=\"s2test2\", "
  "nonce=\"fb0c093dcd98b7102dd2f0e8b11d0f600b\", "
  "qop=\"auth\", "
  "algorithm=\"MD5\"";

char const s2_auth2_credentials[] = "Digest:\"s2test2\":abc:abc";

char const s2_auth3_digest_str[] =
  "Digest realm=\"s2test3\", "
  "nonce=\"e8b11d0f600bfb0c093dcd98b7102dd2f0\", "
  "qop=\"auth-int\", "
  "algorithm=\"MD5-sess\"";

char const s2_auth3_credentials[] = "Digest:\"s2test3\":abc:abc";

105 106
int s2_nua_thread = 0;

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
/* -- Delay scenarios --------------------------------------------------- */

static unsigned long time_offset;

extern void (*_su_time)(su_time_t *tv);

static void _su_time_fast_forwarder(su_time_t *tv)
{
  tv->tv_sec += time_offset;
}

void s2_fast_forward(unsigned long seconds)
{
  if (_su_time == NULL)
    _su_time = _su_time_fast_forwarder;

  time_offset += seconds;
}

126 127 128 129 130 131 132 133 134
/* -- NUA events -------------------------------------------------------- */

struct event *s2_remove_event(struct event *e)
{
  if ((*e->prev = e->next))
    e->next->prev = e->prev;

  e->prev = NULL, e->next = NULL;

135
  return e;
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
}

void s2_free_event(struct event *e)
{
  if (e) {
    if (e->prev) {
      if ((*e->prev = e->next))
	e->next->prev = e->prev;
    }
    nua_destroy_event(e->event);
    nua_handle_unref(e->nh);
    free(e);
  }
}

void s2_flush_events(void)
{
153 154
  while (s2->events) {
    s2_free_event(s2->events);
155 156 157 158 159 160
  }
}

struct event *s2_next_event(void)
{
  for (;;) {
161 162
    if (s2->events)
      return s2_remove_event(s2->events);
163

164
    su_root_step(s2->root, 100);
165
  }
166
}
167 168 169 170 171 172

struct event *s2_wait_for_event(nua_event_t event, int status)
{
  struct event *e;

  for (;;) {
173
    for (e = s2->events; e; e = e->next) {
174 175 176 177 178 179 180
      if (event != nua_i_none && event != e->data->e_event)
	continue;
      if (status && e->data->e_status != status)
	continue;
      return s2_remove_event(e);
    }

181
    su_root_step(s2->root, 100);
182
  }
183
}
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208

int s2_check_event(nua_event_t event, int status)
{
  struct event *e = s2_wait_for_event(event, status);
  s2_free_event(e);
  return e != NULL;
}

int s2_check_callstate(enum nua_callstate state)
{
  int retval = 0;
  tagi_t const *tagi;
  struct event *e;

  e = s2_wait_for_event(nua_i_state, 0);
  if (e) {
    tagi = tl_find(e->data->e_tags, nutag_callstate);
    if (tagi) {
      retval = (tag_value_t)state == tagi->t_value;
    }
  }
  s2_free_event(e);
  return retval;
}

209
static void
210 211 212 213 214 215 216 217 218 219 220
s2_nua_callback(nua_event_t event,
		int status, char const *phrase,
		nua_t *nua, nua_magic_t *_t,
		nua_handle_t *nh, nua_hmagic_t *hmagic,
		sip_t const *sip,
		tagi_t tags[])
{
  struct event *e, **prev;

  if (event == nua_i_active || event == nua_i_terminated)
    return;
221

222 223 224 225 226
  e = calloc(1, sizeof *e);
  nua_save_event(nua, e->event);
  e->nh = nua_handle_ref(nh);
  e->data = nua_event_data(e->event);

227
  for (prev = &s2->events; *prev; prev = &(*prev)->next)
228 229 230 231 232
    ;

  *prev = e, e->prev = prev;
}

233 234
/* ---------------------------------------------------------------------- */
/* SIP messages sent by nua */
235 236 237 238 239 240 241 242 243

struct message *
s2_remove_message(struct message *m)
{
  if ((*m->prev = m->next))
    m->next->prev = m->prev;

  m->prev = NULL, m->next = NULL;

244
  return m;
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
}

void
s2_free_message(struct message *m)
{
  if (m) {
    if (m->prev) {
      if ((*m->prev = m->next))
	m->next->prev = m->prev;
    }
    msg_destroy(m->msg);
    tport_unref(m->tport);
    free(m);
  }
}

void s2_flush_messages(void)
{
263 264
  while (s2->received) {
    s2_free_message(s2->received);
265 266 267 268 269 270 271 272 273
  }
}

struct message *
s2_next_response(void)
{
  struct message *m;

  for (;;) {
274
    for (m = s2->received; m; m = m->next) {
275 276 277
      if (m->sip->sip_status)
	return s2_remove_message(m);
    }
278
    su_root_step(s2->root, 100);
279 280 281 282 283 284 285 286 287
  }
}

struct message *
s2_wait_for_response(int status, sip_method_t method, char const *name)
{
  struct message *m;

  for (;;) {
288
    for (m = s2->received; m; m = m->next) {
289 290 291 292 293 294 295 296
      if (!m->sip->sip_status)
	continue;

      if (status != 0 && m->sip->sip_status->st_status != status)
	continue;

      if (method == sip_method_unknown && name == NULL)
	break;
297

298 299
      if (m->sip->sip_cseq == NULL)
	continue;
300

301 302 303 304 305 306 307 308 309 310 311
      if (m->sip->sip_cseq->cs_method != method)
	continue;
      if (name == NULL)
	break;
      if (strcmp(m->sip->sip_cseq->cs_method_name, name) == 0)
	break;
    }

    if (m)
      return s2_remove_message(m);

312
    su_root_step(s2->root, 100);
313
  }
314
}
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330

int
s2_check_response(int status, sip_method_t method, char const *name)
{
  struct message *m = s2_wait_for_response(status, method, name);
  s2_free_message(m);
  return m != NULL;
}


struct message *
s2_next_request(void)
{
  struct message *m;

  for (;;) {
331
    for (m = s2->received; m; m = m->next) {
332 333 334 335
      if (m->sip->sip_request)
	return s2_remove_message(m);
    }

336
    su_root_step(s2->root, 100);
337
  }
338

339
  return NULL;
340
}
341 342 343 344 345 346 347

struct message *
s2_wait_for_request(sip_method_t method, char const *name)
{
  struct message *m;

  for (;;) {
348
    for (m = s2->received; m; m = m->next) {
349 350 351 352 353 354 355 356 357 358
      if (m->sip->sip_request) {
	if (method == sip_method_unknown && name == NULL)
	  return s2_remove_message(m);

	if (m->sip->sip_request->rq_method == method &&
	    strcmp(m->sip->sip_request->rq_method_name, name) == 0)
	  return s2_remove_message(m);
      }
    }

359
    su_root_step(s2->root, 100);
360
  }
361

362
  return NULL;
363
}
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427

int
s2_check_request(sip_method_t method, char const *name)
{
  struct message *m = s2_wait_for_request(method, name);
  s2_free_message(m);
  return m != NULL;
}

struct message *
s2_respond_to(struct message *m, struct dialog *d,
	      int status, char const *phrase,
	      tag_type_t tag, tag_value_t value, ...)
{
  ta_list ta;
  msg_t *reply;
  sip_t *sip;
  su_home_t *home;
  tp_name_t tpn[1];
  char *rport;

  assert(m); assert(m->msg); assert(m->tport);
  assert(100 <= status && status < 700);

  ta_start(ta, tag, value);

  reply = s2_msg(0); sip = sip_object(reply); home = msg_home(reply);

  assert(reply && home && sip);

  if (sip_add_tl(reply, sip, ta_tags(ta)) < 0) {
    abort();
  }

  s2_complete_response(reply, status, phrase, m->msg);

  if (sip->sip_status && sip->sip_status->st_status > 100 &&
      sip->sip_to && !sip->sip_to->a_tag &&
      sip->sip_cseq && sip->sip_cseq->cs_method != sip_method_cancel) {
    char const *ltag = NULL;

    if (d && d->local)
      ltag = d->local->a_tag;

    if (ltag == NULL)
      ltag = s2_generate_tag(home);

    if (sip_to_tag(msg_home(reply), sip->sip_to, ltag) < 0) {
      assert(!"add To tag");
    }
  }

  if (d && !d->local) {
    d->local = sip_from_dup(d->home, sip->sip_to);
    d->remote = sip_to_dup(d->home, sip->sip_from);
    d->call_id = sip_call_id_dup(d->home, sip->sip_call_id);
    d->rseq = sip->sip_cseq->cs_seq;
    /* d->route = sip_route_dup(d->home, sip->sip_record_route); */
    d->target = sip_contact_dup(d->home, m->sip->sip_contact);
    d->contact = sip_contact_dup(d->home, sip->sip_contact);
  }

  *tpn = *tport_name(m->tport);

428
  rport = su_sprintf(home, "rport=%u",
429 430 431
		     ntohs(((su_sockaddr_t *)
			    msg_addrinfo(m->msg)->ai_addr)->su_port));

Pekka Pessi's avatar
Pekka Pessi committed
432 433 434
  if (s2->server_uses_rport &&
      sip->sip_via->v_rport &&
      sip->sip_via->v_rport[0] == '\0') {
435
    msg_header_add_param(home, sip->sip_via->v_common, rport);
436
  }
437 438 439 440 441 442 443 444 445 446 447 448

  tpn->tpn_port = rport + strlen("rport=");

  tport_tsend(m->tport, reply, tpn, TPTAG_MTU(INT_MAX), ta_tags(ta));
  msg_destroy(reply);

  ta_end(ta);

  return m;
}

/** Add headers from the request to the response message. */
449 450 451
static int
s2_complete_response(msg_t *response,
		     int status, char const *phrase,
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
		     msg_t *request)
{
  su_home_t *home = msg_home(response);
  sip_t *response_sip = sip_object(response);
  sip_t const *request_sip = sip_object(request);

  int incomplete = 0;

  if (!response_sip || !request_sip || !request_sip->sip_request)
    return -1;

  if (!response_sip->sip_status)
    response_sip->sip_status = sip_status_create(home, status, phrase, NULL);
  if (!response_sip->sip_via)
    response_sip->sip_via = sip_via_dup(home, request_sip->sip_via);
  if (!response_sip->sip_from)
    response_sip->sip_from = sip_from_dup(home, request_sip->sip_from);
  if (!response_sip->sip_to)
    response_sip->sip_to = sip_to_dup(home, request_sip->sip_to);
  if (!response_sip->sip_call_id)
472
    response_sip->sip_call_id =
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498
      sip_call_id_dup(home, request_sip->sip_call_id);
  if (!response_sip->sip_cseq)
    response_sip->sip_cseq = sip_cseq_dup(home, request_sip->sip_cseq);

  if (!response_sip->sip_record_route && request_sip->sip_record_route)
    sip_add_dup(response, response_sip, (void*)request_sip->sip_record_route);

  incomplete = sip_complete_message(response) < 0;

  msg_serialize(response, (msg_pub_t *)response_sip);

  if (incomplete ||
      !response_sip->sip_status ||
      !response_sip->sip_via ||
      !response_sip->sip_from ||
      !response_sip->sip_to ||
      !response_sip->sip_call_id ||
      !response_sip->sip_cseq ||
      !response_sip->sip_content_length ||
      !response_sip->sip_separator ||
      (request_sip->sip_record_route && !response_sip->sip_record_route))
    return -1;

  return 0;
}

499
/* Send request (updating dialog).
500 501 502
 *
 * Return zero upon success, nonzero upon failure.
 */
503
int
504 505 506 507 508 509 510 511 512 513
s2_request_to(struct dialog *d,
	      sip_method_t method, char const *name,
	      tport_t *tport,
	      tag_type_t tag, tag_value_t value, ...)
{
  ta_list ta;
  tagi_t const *tags;

  msg_t *msg = s2_msg(0);
  sip_t *sip = sip_object(msg);
514
  url_t const *target = NULL;
515 516 517 518 519
  sip_cseq_t cseq[1];
  sip_via_t via[1]; char const *v_params[8];
  sip_content_length_t l[1];
  tp_name_t tpn[1];
  tp_magic_t *magic;
520
  int user_via = 0;
521 522 523 524 525 526 527

  ta_start(ta, tag, value);
  tags = ta_args(ta);

  if (sip_add_tagis(msg, sip, &tags) < 0)
    goto error;

528 529 530 531 532 533 534 535
  if (sip->sip_request)
    target = sip->sip_request->rq_url;
  else if (d->target)
    target = d->target->m_url;
  else if (s2->registration->contact)
    target = s2->registration->contact->m_url;
  else
    target = NULL;
536

537 538
  if (target == NULL)
    goto error;
539

540 541 542 543
  if (!sip->sip_request) {
    sip_request_t *rq;
    rq = sip_request_create(msg_home(msg), method, name,
			    (url_string_t *)target, NULL);
544 545 546 547 548
    sip_header_insert(msg, sip, (sip_header_t *)rq);
  }

  if (!d->local && sip->sip_from)
    d->local = sip_from_dup(d->home, sip->sip_from);
549
  if (!d->contact && sip->sip_contact)
550 551 552 553 554 555 556 557 558 559 560
    d->contact = sip_contact_dup(d->home, sip->sip_contact);
  if (!d->remote && sip->sip_to)
    d->remote = sip_to_dup(d->home, sip->sip_to);
  if (!d->target && sip->sip_request)
    d->target = sip_contact_create(d->home,
				   (url_string_t *)sip->sip_request->rq_url,
				   NULL);
  if (!d->call_id && sip->sip_call_id)
    d->call_id = sip_call_id_dup(d->home, sip->sip_call_id);
  if (!d->lseq && sip->sip_cseq)
    d->lseq = sip->sip_cseq->cs_seq;
561

562
  if (!d->local)
563
    d->local = sip_from_dup(d->home, s2->local);
564
  if (!d->contact)
565
    d->contact = sip_contact_dup(d->home, s2->contact);
566
  if (!d->remote)
567
    d->remote = sip_to_dup(d->home, s2->registration->aor);
568 569 570 571
  if (!d->call_id)
    d->call_id = sip_call_id_create(d->home, NULL);
  assert(d->local && d->contact);
  assert(d->remote && d->target);
572
  assert(d->call_id);
573

574 575
  if (tport == NULL)
    tport = d->tport;
576 577

  if (tport == NULL)
578
    tport = s2->registration->tport;
579 580

  if (tport == NULL && d->target->m_url->url_type == url_sips)
581
    tport = s2->tls.tport;
582 583

  if (tport == NULL)
584
    tport = s2->udp.tport;
585
  else if (tport == NULL)
586
    tport = s2->tcp.tport;
587
  else if (tport == NULL)
588
    tport = s2->tls.tport;
589 590 591 592

  assert(tport);

  *tpn = *tport_name(tport);
593 594 595 596

  if (tport_is_primary(tport)) {
    tpn->tpn_host = target->url_host;
    tpn->tpn_port = url_port(target);
597
    if (!tpn->tpn_port || !tpn->tpn_port[0])
598
      tpn->tpn_port = url_port_default(target->url_type);
599
  }
600 601 602

  magic = tport_magic(tport);
  assert(magic != NULL);
603 604 605 606

  sip_cseq_init(cseq);
  cseq->cs_method = method;
  cseq->cs_method_name = name;
607

608 609 610 611 612 613 614
  if (d->invite && (method == sip_method_ack || method == sip_method_cancel)) {
    cseq->cs_seq = sip_object(d->invite)->sip_cseq->cs_seq;
  }
  else {
    cseq->cs_seq = ++d->lseq;
  }

615 616 617 618
  if (sip->sip_via) {
    user_via = 1;
  }
  else if (d->invite && method == sip_method_cancel) {
619 620 621 622 623
    *via = *sip_object(d->invite)->sip_via;
  }
  else {
    *via = *magic->via;
    via->v_params = v_params;
624
    v_params[0] = su_sprintf(msg_home(msg), "branch=z9hG4bK%lx", ++s2->tid);
625 626
    v_params[1] = NULL;
  }
627 628 629 630 631

  sip_content_length_init(l);
  if (sip->sip_payload)
    l->l_length = sip->sip_payload->pl_len;

632 633 634 635 636 637 638 639 640 641 642 643 644 645
  if (d->local->a_tag == NULL) {
    char const *ltag = s2_generate_tag(d->home);

    if (sip_from_tag(d->home, d->local, ltag) < 0) {
      assert(!"add To tag");
    }

    if (sip->sip_from && sip->sip_from->a_tag == NULL) {
      if (sip_from_tag(msg_home(msg), sip->sip_from, ltag) < 0) {
	assert(!"add To tag");
      }
    }
  }

646
  sip_add_tl(msg, sip,
647 648 649 650 651
	     TAG_IF(!sip->sip_from, SIPTAG_FROM(d->local)),
	     TAG_IF(!sip->sip_contact, SIPTAG_CONTACT(d->contact)),
	     TAG_IF(!sip->sip_to, SIPTAG_TO(d->remote)),
	     TAG_IF(!sip->sip_call_id, SIPTAG_CALL_ID(d->call_id)),
	     TAG_IF(!sip->sip_cseq, SIPTAG_CSEQ(cseq)),
652
	     TAG_IF(!user_via, SIPTAG_VIA(via)),
653 654 655 656 657 658
	     TAG_IF(!sip->sip_content_length, SIPTAG_CONTENT_LENGTH(l)),
	     TAG_IF(!sip->sip_separator, SIPTAG_SEPARATOR_STR("\r\n")),
	     TAG_END());

  msg_serialize(msg, NULL);

659 660 661 662 663
  if (method == sip_method_invite) {
    msg_destroy(d->invite);
    d->invite = msg_ref_create(msg);
  }

664 665
  tport = tport_tsend(tport, msg, tpn, ta_tags(ta));
  ta_end(ta);
666 667 668 669 670 671

  if (d->tport != tport) {
    tport_unref(d->tport);
    d->tport = tport_ref(tport);
  }

672
  return tport ? 0 : -1;
673

674 675 676 677 678
 error:
  ta_end(ta);
  return -1;
}

679 680 681 682
/** Save information from response.
 *
 * Send ACK for error messages to INVITE.
 */
683 684
int s2_update_dialog(struct dialog *d, struct message *m)
{
685 686 687 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
  int status = 0;

  if (m->sip->sip_status)
    status = m->sip->sip_status->st_status;

  if (100 < status && status < 300) {
    d->remote = sip_to_dup(d->home, m->sip->sip_to);
    if (m->sip->sip_contact)
      d->contact = sip_contact_dup(d->home, m->sip->sip_contact);
  }

  if (300 <= status && m->sip->sip_cseq &&
      m->sip->sip_cseq->cs_method == sip_method_invite &&
      d->invite) {
    msg_t *ack = s2_msg(0);
    sip_t *sip = sip_object(ack);
    sip_t *invite = sip_object(d->invite);
    sip_request_t rq[1];
    sip_cseq_t cseq[1];
    tp_name_t tpn[1];

    *rq = *invite->sip_request;
    rq->rq_method = sip_method_ack, rq->rq_method_name = "ACK";
    *cseq = *invite->sip_cseq;
    cseq->cs_method = sip_method_ack, cseq->cs_method_name = "ACK";

    sip_add_tl(ack, sip,
	       SIPTAG_REQUEST(rq),
	       SIPTAG_VIA(invite->sip_via),
	       SIPTAG_FROM(invite->sip_from),
	       SIPTAG_TO(invite->sip_to),
	       SIPTAG_CALL_ID(invite->sip_call_id),
	       SIPTAG_CSEQ(cseq),
	       SIPTAG_CONTENT_LENGTH_STR("0"),
	       SIPTAG_SEPARATOR_STR("\r\n"),
	       TAG_END());

    *tpn = *tport_name(d->tport);
    if (!tport_is_secondary(d->tport) ||
	!tport_is_clear_to_send(d->tport)) {
      tpn->tpn_host = rq->rq_url->url_host;
      tpn->tpn_port = rq->rq_url->url_port;
    }

    msg_serialize(ack, NULL);
    tport_tsend(d->tport, ack, tpn, TAG_END());
  }

733 734 735 736 737 738 739 740 741 742 743 744 745
  return 0;
}

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

int
s2_save_register(struct message *rm)
{
  sip_contact_t *contact, *m, **m_prev;
  sip_expires_t const *ex;
  sip_date_t const *date;
  sip_time_t now = rm->when.tv_sec, expires;

746 747 748
  msg_header_free_all(s2->home, (msg_header_t *)s2->registration->aor);
  msg_header_free_all(s2->home, (msg_header_t *)s2->registration->contact);
  tport_unref(s2->registration->tport);
749

Pekka Pessi's avatar
Pekka Pessi committed
750 751 752
  s2->registration->aor = NULL;
  s2->registration->contact = NULL;
  s2->registration->tport = NULL;
753 754 755 756 757 758 759 760 761 762

  if (rm == NULL)
    return 0;

  assert(rm && rm->sip && rm->sip->sip_request);
  assert(rm->sip->sip_request->rq_method == sip_method_register);

  ex = rm->sip->sip_expires;
  date = rm->sip->sip_date;

763
  contact = sip_contact_dup(s2->home, rm->sip->sip_contact);
764 765 766 767 768

  for (m_prev = &contact; *m_prev;) {
    m = *m_prev;

    expires = sip_contact_expires(m, ex, date,
769
				  s2_default_registration_duration,
770 771
				  now);
    if (expires) {
772 773
      char *p = su_sprintf(s2->home, "expires=%lu", (unsigned long)expires);
      msg_header_add_param(s2->home, m->m_common, p);
774 775 776 777 778
      m_prev = &m->m_next;
    }
    else {
      *m_prev = m->m_next;
      m->m_next = NULL;
779
      msg_header_free(s2->home, (msg_header_t *)m);
780 781 782 783 784 785
    }
  }

  if (contact == NULL)
    return 0;

786 787 788
  s2->registration->aor = sip_to_dup(s2->home, rm->sip->sip_to);
  s2->registration->contact = contact;
  s2->registration->tport = tport_ref(rm->tport);
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804

  return 0;
}

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

static char *
s2_generate_tag(su_home_t *home)
{
  s2_tag_generator += 1;

  return su_sprintf(home, "tag=N2-%s/%u", _s2case, s2_tag_generator);
}

void s2_case(char const *number,
	     char const *title,
805
	     char const *description)
806 807 808 809 810 811
{
  _s2case = number;
}

/* ---------------------------------------------------------------------- */
/* tport interface */
812
static void
813
s2_stack_recv(struct tester *s2,
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
	      tport_t *tp,
	      msg_t *msg,
	      tp_magic_t *magic,
	      su_time_t now)
{
  struct message *next = calloc(1, sizeof *next), **prev;

  next->msg = msg;
  next->sip = sip_object(msg);
  next->when = now;
  next->tport = tport_ref(tp);

#if 0
  if (next->sip->sip_request)
    printf("nua sent: %s\n", next->sip->sip_request->rq_method_name);
  else
    printf("nua sent: SIP/2.0 %u %s\n",
	   next->sip->sip_status->st_status,
	   next->sip->sip_status->st_phrase);
#endif

835
  for (prev = &s2->received; *prev; prev = &(*prev)->next)
836 837 838 839 840 841
    ;

  next->prev = prev, *prev = next;
}

static void
842
s2_stack_error(struct tester *s2,
843 844 845 846
	       tport_t *tp,
	       int errcode,
	       char const *remote)
{
847
  fprintf(stderr, "%s(%p): error %d (%s) from %s\n",
848
	  "nua_tester_error",
849
	  (void *)tp, errcode, su_strerror(errcode),
850 851 852 853
	  remote ? remote : "<unknown destination>");
}

static msg_t *
854
s2_stack_alloc(struct tester *s2, int flags,
855
	       char const data[], usize_t size,
856
	       tport_t const *tport,
857 858
	       tp_client_t *tpc)
{
859
  return msg_create(s2->mclass, flags | s2->flags);
860 861 862 863 864
}

static msg_t *
s2_msg(int flags)
{
865
  return msg_create(s2->mclass, flags | s2->flags);
866 867 868 869 870 871 872 873 874 875 876
}

tp_stack_class_t const s2_stack[1] =
  {{
      /* tpac_size */ (sizeof s2_stack),
      /* tpac_recv */  s2_stack_recv,
      /* tpac_error */ s2_stack_error,
      /* tpac_alloc */ s2_stack_alloc,
  }};

/** Basic setup for test cases */
877
void s2_setup_base(char const *hostname)
878
{
879
  assert(s2 == NULL);
880 881 882

  su_init();

883
  s2 = su_home_new(sizeof *s2);
884

885
  assert(s2 != NULL);
886

887
  s2->root = su_root_create(s2);
888

889
  assert(s2->root != NULL);
890

891
  s2->local = sip_from_format(s2->home, "Bob <sip:bob@%s>",
892
			     hostname ? hostname : "example.net");
893

894 895 896
  if (hostname == NULL)
    hostname = "127.0.0.1";

897 898
  s2->hostname = hostname;
  s2->tid = (unsigned long)time(NULL) * 510633671UL;
899 900 901 902 903 904 905 906 907 908 909 910
}

SOFIAPUBVAR su_log_t nua_log[];
SOFIAPUBVAR su_log_t soa_log[];
SOFIAPUBVAR su_log_t nea_log[];
SOFIAPUBVAR su_log_t nta_log[];
SOFIAPUBVAR su_log_t tport_log[];
SOFIAPUBVAR su_log_t su_log_default[];

void
s2_setup_logs(int level)
{
911
  assert(s2);
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931

  su_log_soft_set_level(nua_log, level);
  su_log_soft_set_level(soa_log, level);
  su_log_soft_set_level(su_log_default, level);
  su_log_soft_set_level(nea_log, level);
  su_log_soft_set_level(nta_log, level);
  su_log_soft_set_level(tport_log, level);
}

static char const * default_protocols[] = { "udp", "tcp", NULL };

void
s2_setup_tport(char const * const *protocols,
	       tag_type_t tag, tag_value_t value, ...)
{
  ta_list ta;
  tp_name_t tpn[1];
  int bound;
  tport_t *tp;

932
  assert(s2 != NULL);
933 934 935

  ta_start(ta, tag, value);

936
  if (s2->master == NULL) {
937 938 939
    s2->master = tport_tcreate(s2, s2_stack, s2->root,
			       TPTAG_LOG(getenv("S2_TPORT_LOG") != NULL),
			       ta_tags(ta));
940

941 942
    if (s2->master == NULL) {
      assert(s2->master);
943
    }
944 945
    s2->mclass = sip_default_mclass();
    s2->flags = 0;
946 947 948 949
  }

  memset(tpn, 0, (sizeof tpn));
  tpn->tpn_proto = "*";
950
  tpn->tpn_host = s2->hostname;
951 952 953 954
  tpn->tpn_port = "*";

  if (protocols == NULL)
    protocols = default_protocols;
955 956

  bound = tport_tbind(s2->master, tpn, protocols,
957 958 959 960
		      TPTAG_SERVER(1),
		      ta_tags(ta));
  assert(bound != -1);

961
  tp = tport_primaries(s2->master);
962

963
  if (protocols == default_protocols && s2->contact == NULL) {
964
    *tpn = *tport_name(tp);
965
    s2->contact = sip_contact_format(s2->home, "<sip:%s:%s>",
966 967 968 969 970 971 972 973 974 975 976 977 978 979
				    tpn->tpn_host,
				    tpn->tpn_port);
  }

  for (;tp; tp = tport_next(tp)) {
    sip_via_t *v;
    sip_contact_t *m;
    tp_magic_t *magic;

    if (tport_magic(tp))
      continue;

    *tpn = *tport_name(tp);

980
    v = sip_via_format(s2->home, "SIP/2.0/%s %s:%s",
981
		       tpn->tpn_proto,
982
		       tpn->tpn_host,
983 984 985
		       tpn->tpn_port);
    assert(v != NULL);
    if (strncasecmp(tpn->tpn_proto, "tls", 3)) {
986
      m = sip_contact_format(s2->home, "<sip:%s:%s;transport=%s>",
987 988 989
			     tpn->tpn_host,
			     tpn->tpn_port,
			     tpn->tpn_proto);
990
      if (s2->udp.contact == NULL && strcasecmp(tpn->tpn_proto, "udp") == 0) {
991
	s2->udp.tport = tport_ref(tp);
992
	s2->udp.contact = m;
993
      }
994
      if (s2->tcp.contact == NULL && strcasecmp(tpn->tpn_proto, "tcp") == 0) {
995
	s2->tcp.tport = tport_ref(tp);
996
	s2->tcp.contact = m;
997 998 999
      }
    }
    else if (strcasecmp(tpn->tpn_proto, "tls")) {
1000
      m = sip_contact_format(s2->home, "<sips:%s:%s;transport=%s>",
1001 1002 1003 1004 1005
			     tpn->tpn_host,
			     tpn->tpn_port,
			     tpn->tpn_proto);
    }
    else {
1006
      m = sip_contact_format(s2->home, "<sips:%s:%s>",
1007 1008
			     tpn->tpn_host,
			     tpn->tpn_port);
1009
      if (s2->tls.contact == NULL) {
1010
	s2->tls.tport = tport_ref(tp);
1011
	s2->tls.contact = m;
1012 1013 1014 1015
      }
    }
    assert(m != NULL);

1016
    magic = su_zalloc(s2->home, (sizeof *magic));
1017 1018
    magic->via = v, magic->contact = m;

1019 1020
    if (s2->contact == NULL)
      s2->contact = m;
1021 1022 1023 1024 1025

    tport_set_magic(tp, magic);
  }
}

1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392
/* ---------------------------------------------------------------------- */
/* S2 DNS server */

#include <sofia-resolv/sres_record.h>

extern uint16_t _sres_default_port;

static int s2_dns_query(struct tester *s2,
			su_wait_t *w,
			su_wakeup_arg_t *arg);

void s2_setup_dns(void)
{
  int n;
  su_socket_t socket;
  su_wait_t *wait;
  su_sockaddr_t su[1];
  socklen_t sulen = sizeof su->su_sin;

  assert(s2->nua == NULL); assert(s2->root != NULL);

  memset(su, 0, sulen);
  su->su_len = sulen;
  su->su_family = AF_INET;

  /* su->su_port = htons(1053); */

  socket = su_socket(su->su_family, SOCK_DGRAM, 0);

  n = bind(socket, &su->su_sa, sulen); assert(n == 0);
  n = getsockname(socket, &su->su_sa, &sulen); assert(n == 0);

  _sres_default_port = ntohs(su->su_port);

  wait = s2->dns.wait;
  n = su_wait_create(wait, socket, SU_WAIT_IN); assert(n == 0);
  s2->dns.reg = su_root_register(s2->root, wait, s2_dns_query, NULL, 0);
  assert(s2->dns.reg > 0);
  s2->dns.socket = socket;
}

static
struct s2_dns_response {
  struct s2_dns_response *next;
  uint16_t qlen, dlen;
  struct m_header {
    /* Header defined in RFC 1035 section 4.1.1 (page 26) */
    uint16_t mh_id;		/* Query ID */
    uint16_t mh_flags;		/* Flags */
    uint16_t mh_qdcount;	/* Question record count */
    uint16_t mh_ancount;	/* Answer record count */
    uint16_t mh_nscount;	/* Authority records count */
    uint16_t mh_arcount;	/* Additional records count */
  } header[1];
  uint8_t data[1500];
} *zonedata;

enum {
  FLAGS_QR = (1 << 15),
  FLAGS_QUERY = (0 << 11),
  FLAGS_IQUERY = (1 << 11),
  FLAGS_STATUS = (2 << 11),
  FLAGS_OPCODE = (15 << 11),	/* mask */
  FLAGS_AA = (1 << 10),		/*  */
  FLAGS_TC = (1 << 9),
  FLAGS_RD = (1 << 8),
  FLAGS_RA = (1 << 7),

  FLAGS_RCODE = (15 << 0),	/* mask of return code */

  FLAGS_OK = 0,			/* No error condition. */
  FLAGS_FORMAT_ERR = 1,		/* Server could not interpret query. */
  FLAGS_SERVER_ERR = 2,		/* Server error. */
  FLAGS_NAME_ERR = 3,		/* No domain name. */
  FLAGS_UNIMPL_ERR = 4,		/* Not implemented. */
  FLAGS_AUTH_ERR = 5,		/* Refused */
};

uint32_t s2_dns_ttl = 3600;

static int
s2_dns_query(struct tester *s2,
	     su_wait_t *w,
	     su_wakeup_arg_t *arg)
{
  union {
    struct m_header header[1];
    uint8_t buffer[1500];
  } request;
  ssize_t len;

  su_socket_t socket;
  su_sockaddr_t su[1];
  socklen_t sulen = sizeof su;
  uint16_t flags;
  struct s2_dns_response *r;
  size_t const hlen = sizeof r->header;

  (void)arg;

  socket = s2->dns.socket;

  len = su_recvfrom(socket, request.buffer, sizeof request.buffer, 0,
		    &su->su_sa, &sulen);

  flags = ntohs(request.header->mh_flags);

  if (len < (ssize_t)hlen)
    return 0;
  if ((flags & FLAGS_QR) == FLAGS_QR)
    return 0;
  if ((flags & FLAGS_RCODE) != FLAGS_OK)
    return 0;

  if ((flags & FLAGS_OPCODE) != FLAGS_QUERY
      || ntohs(request.header->mh_qdcount) != 1) {
    flags |= FLAGS_QR | FLAGS_UNIMPL_ERR;
    request.header->mh_flags = htons(flags);
    su_sendto(socket, request.buffer, len, 0, &su->su_sa, sulen);
    return 0;
  }

  for (r = zonedata; r; r = r->next) {
    if (memcmp(r->data, request.buffer + hlen, r->qlen) == 0)
      break;
  }

  if (r) {
    flags |= FLAGS_QR | FLAGS_AA | FLAGS_OK;
    request.header->mh_flags = htons(flags);
    request.header->mh_ancount = htons(r->header->mh_ancount);
    request.header->mh_nscount = htons(r->header->mh_nscount);
    request.header->mh_arcount = htons(r->header->mh_arcount);
    memcpy(request.buffer + hlen + r->qlen,
	   r->data + r->qlen,
	   r->dlen - r->qlen);
    len = hlen + r->dlen;
  }
  else {
    flags |= FLAGS_QR | FLAGS_AA | FLAGS_NAME_ERR;
  }

  request.header->mh_flags = htons(flags);
  su_sendto(socket, request.buffer, len, 0, &su->su_sa, sulen);
  return 0;
}

static void put_uint16(struct s2_dns_response *m, uint16_t h)
{
  uint8_t *p = m->data + m->dlen;

  assert(m->dlen + (sizeof h) < sizeof m->data);
  p[0] = h >> 8; p[1] = h;
  m->dlen += (sizeof h);
}

static void put_uint32(struct s2_dns_response *m, uint32_t w)
{
  uint8_t *p = m->data + m->dlen;

  assert(m->dlen + (sizeof w) < sizeof m->data);
  p[0] = w >> 24; p[1] = w >> 16; p[2] = w >> 8; p[3] = w;
  m->dlen += (sizeof w);
}

static void put_domain(struct s2_dns_response *m, char const *domain)
{
  char const *label;
  size_t llen;

  /* Copy domain into query label at a time */
  for (label = domain; label && label[0]; label += llen) {
    assert(!(label[0] == '.' && label[1] != '\0'));
    llen = strcspn(label, ".");
    assert(llen < 64);
    assert(m->dlen + llen + 1 < sizeof m->data);
    m->data[m->dlen++] = (uint8_t)llen;
    if (llen == 0)
      return;

    memcpy(m->data + m->dlen, label, llen);
    m->dlen += (uint16_t)llen;

    if (label[llen] == '\0')
      break;
    if (label[llen + 1])
      llen++;
  }

  assert(m->dlen < sizeof m->data);
  m->data[m->dlen++] = '\0';
}

static void put_string(struct s2_dns_response *m, char const *string)
{
  uint8_t *p = m->data + m->dlen;
  size_t len = strlen(string);

  assert(len <= 255);
  assert(m->dlen + len + 1 < sizeof m->data);

  *p++ = (uint8_t)len;
  memcpy(p, string, len);
  m->dlen += len + 1;
}

static uint16_t put_len_at(struct s2_dns_response *m)
{
  uint16_t at = m->dlen;
  assert(m->dlen + sizeof(at) < sizeof m->data);
  memset(m->data + m->dlen, 0, sizeof(at));
  m->dlen += sizeof(at);
  return at;
}

static void put_len(struct s2_dns_response *m, uint16_t start)
{
  uint8_t *p = m->data + start;
  uint16_t len = m->dlen - (start + 2);
  p[0] = len >> 8; p[1] = len;
}

static void put_data(struct s2_dns_response *m, void *data, uint16_t len)
{
  assert(m->dlen + len < sizeof m->data);
  memcpy(m->data + m->dlen, data, len);
  m->dlen += len;
}

static void put_query(struct s2_dns_response *m, char const *domain,
		      uint16_t qtype)
{
  assert(m->header->mh_qdcount == 0);
  put_domain(m, domain), put_uint16(m, qtype), put_uint16(m, sres_class_in);
  m->header->mh_qdcount++;
  m->qlen = m->dlen;
}

static void put_a_record(struct s2_dns_response *m,
			 char const *domain,
			 struct in_addr addr)
{
  uint16_t start;

  put_domain(m, domain);
  put_uint16(m, sres_type_a);
  put_uint16(m, sres_class_in);
  put_uint32(m, s2_dns_ttl);
  start = put_len_at(m);

  put_data(m, &addr, sizeof addr);
  put_len(m, start);
}

static void put_srv_record(struct s2_dns_response *m,
			   char const *domain,
			   uint16_t prio, uint16_t weight,
			   uint16_t port, char const *target)
{
  uint16_t start;
  put_domain(m, domain);
  put_uint16(m, sres_type_srv);
  put_uint16(m, sres_class_in);
  put_uint32(m, s2_dns_ttl);
  start = put_len_at(m);

  put_uint16(m, prio);
  put_uint16(m, weight);
  put_uint16(m, port);
  put_domain(m, target);
  put_len(m, start);
}

static void put_naptr_record(struct s2_dns_response *m,
			     char const *domain,
			     uint16_t order, uint16_t preference,
			     char const *flags,
			     char const *services,
			     char const *regexp,
			     char const *replace)
{
  uint16_t start;
  put_domain(m, domain);
  put_uint16(m, sres_type_naptr);
  put_uint16(m, sres_class_in);
  put_uint32(m, s2_dns_ttl);
  start = put_len_at(m);

  put_uint16(m, order);
  put_uint16(m, preference);
  put_string(m, flags);
  put_string(m, services);
  put_string(m, regexp);
  put_domain(m, replace);
  put_len(m, start);
}

static void put_srv_record_from_uri(struct s2_dns_response *m,
				    char const *base,
				    uint16_t prio, uint16_t weight,
				    url_t const *uri, char const *server)
{
  char domain[1024] = "none";
  char const *service = url_port(uri);
  uint16_t port;

  if (uri->url_type == url_sips) {
    strcpy(domain, "_sips._tcp.");
  }
  else if (uri->url_type == url_sip) {
    if (url_has_param(uri, "transport=udp")) {
      strcpy(domain, "_sip._udp.");
    }
    else if (url_has_param(uri, "transport=tcp")) {
      strcpy(domain, "_sip._tcp.");
    }
  }

  assert(strcmp(domain, "none"));

  strcat(domain, base);

  if (m->header->mh_qdcount == 0)
    put_query(m, domain, sres_type_srv);

  port = (uint16_t)strtoul(service, NULL, 10);

  put_srv_record(m, domain, prio, weight, port, server);
}

static
void s2_add_to_zone(struct s2_dns_response *_r)
{
  size_t size = offsetof(struct s2_dns_response, data[_r->dlen]);
  struct s2_dns_response *r = malloc(size); assert(r);

  memcpy(r, _r, size);
  r->next = zonedata;
  zonedata = r;
}


static void make_server(char *server, char const *prefix, char const *domain)
{
  strcpy(server, prefix);

  if (strlen(server) == 0 || server[strlen(server) - 1] != '.') {
    strcat(server, ".");
    strcat(server, domain);
  }
}

/** Set up DNS domain */
void s2_dns_domain(char const *domain, int use_naptr,
		   /* char *prefix, int priority, url_t const *uri, */
		   ...)
{
  struct s2_dns_response m[1];

  char server[1024], target[1024];

  va_list va0, va;
  char const *prefix; int priority; url_t const *uri;
  struct in_addr localhost;

  assert(s2->dns.reg != 0);

1393
  su_inet_pton(AF_INET, "127.0.0.1", &localhost);
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490

  va_start(va0, use_naptr);

  if (use_naptr) {
    memset(m, 0, sizeof m);
    put_query(m, domain, sres_type_naptr);

    va_copy(va, va0);

    for (;(prefix = va_arg(va, char *));) {
      char *services = NULL;

      priority = va_arg(va, int);
      uri = va_arg(va, url_t *); assert(uri);

      if (uri->url_type == url_sips) {
	services = "SIPS+D2T";
	strcpy(target, "_sips._tcp.");
      }
      else if (uri->url_type == url_sip) {
	if (url_has_param(uri, "transport=udp")) {
	  services = "SIP+D2U";
	  strcpy(target, "_sip._udp.");
	}
	else if (url_has_param(uri, "transport=tcp")) {
	  services = "SIP+D2T";
	  strcpy(target, "_sip._tcp.");
	}
      }

      strcat(target, domain);
      assert(services);
      put_naptr_record(m, domain, 1, priority, "s", services, "", target);
      m->header->mh_ancount++;
    }

    va_end(va);
    va_copy(va, va0);

    for (;(prefix = va_arg(va, char *));) {
      priority = va_arg(va, int);
      uri = va_arg(va, url_t *); assert(uri);

      make_server(server, prefix, domain);

      put_srv_record_from_uri(m, domain, priority, 10, uri, server);
      m->header->mh_arcount++;

      put_a_record(m, server, localhost);
      m->header->mh_arcount++;
    }
    va_end(va);

    s2_add_to_zone(m);
  }

  /* Add SRV records */
  va_copy(va, va0);
  for (;(prefix = va_arg(va, char *));) {
    priority = va_arg(va, int);
    uri = va_arg(va, url_t *); assert(uri);

    make_server(server, prefix, domain);

    memset(m, 0, sizeof m);
    put_srv_record_from_uri(m, domain, priority, 10, uri, server);
    m->header->mh_ancount++;

    strcpy(server, prefix); strcat(server, domain);

    put_a_record(m, server, localhost);
    m->header->mh_arcount++;

    s2_add_to_zone(m);
  }
  va_end(va);

  /* Add A records */
  va_copy(va, va0);
  for (;(prefix = va_arg(va, char *));) {
    (void)va_arg(va, int);
    (void)va_arg(va, url_t *);

    memset(m, 0, sizeof m);
    make_server(server, prefix, domain);

    put_query(m, server, sres_type_a);
    put_a_record(m, server, localhost);
    m->header->mh_ancount++;

    s2_add_to_zone(m);
  }
  va_end(va);

  va_end(va0);
}

1491
void
1492 1493 1494 1495 1496 1497 1498 1499
s2_teardown(void)
{
  s2 = NULL;
  su_deinit();
}

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

1500 1501
#include <sofia-sip/sresolv.h>

1502
nua_t *s2_nua_setup(tag_type_t tag, tag_value_t value, ...)
1503 1504 1505
{
  ta_list ta;

1506
  s2_setup_base(NULL);
1507
  s2_setup_dns();
1508 1509

  s2_setup_logs(0);
1510
  s2_setup_tport(NULL, TAG_END());
1511
  assert(s2->contact);
1512

1513 1514 1515
  /* enable/disable multithreading */
  su_root_threading(s2->root, s2_nua_thread);

1516 1517 1518 1519 1520
  s2_dns_domain("example.org", 1,
		"s2", 1, s2->udp.contact->m_url,
		"s2", 1, s2->tcp.contact->m_url,
		NULL);

1521
  ta_start(ta, tag, value);
1522
  s2->nua =
1523 1524 1525 1526
    nua_create(s2->root,
	       s2_nua_callback,
	       s2,
	       SIPTAG_FROM_STR("Alice <sip:alice@example.org>"),
1527 1528 1529 1530 1531 1532 1533 1534
	       /* NUTAG_PROXY((url_string_t *)s2->contact->m_url), */
	       /* Use internal DNS server */
	       NUTAG_PROXY("sip:example.org"),
#if HAVE_WIN32
	       SRESTAG_RESOLV_CONF("NUL"),
#else
	       SRESTAG_RESOLV_CONF("/dev/null"),
#endif
1535 1536
	       ta_tags(ta));
  ta_end(ta);
1537

1538 1539
  return s2->nua;
}
1540

1541 1542 1543 1544 1545 1546
void s2_nua_teardown(void)
{
  nua_destroy(s2->nua);
  s2->nua = NULL;
  s2_teardown();
}
1547

1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584
/* ====================================================================== */

/** Register NUA user.
 *
 * <pre>
 *  A                  B
 *  |-----REGISTER---->|
 *  |<-----200 OK------|
 *  |                  |
 * </pre>
 */
void s2_register_setup(void)
{
  nua_handle_t *nh;
  struct message *m;

  assert(s2 && s2->nua);
  assert(!s2->registration->nh);

  nh = nua_handle(s2->nua, NULL, TAG_END());

  nua_register(nh, TAG_END());

  m = s2_wait_for_request(SIP_METHOD_REGISTER);
  assert(m);
  s2_save_register(m);

  s2_respond_to(m, NULL,
		SIP_200_OK,
		SIPTAG_CONTACT(s2->registration->contact),
		TAG_END());
  s2_free_message(m);

  assert(s2->registration->contact != NULL);
  s2_check_event(nua_r_register, 200);

  s2->registration->nh = nh;
1585 1586
}

1587 1588 1589 1590 1591 1592 1593 1594 1595 1596
/** Un-register NUA user.
 *
 * <pre>
 *  A                  B
 *  |-----REGISTER---->|
 *  |<-----200 OK------|
 *  |                  |
 * </pre>
 */
void s2_register_teardown(void)
1597
{
1598 1599 1600 1601 1602
  if (s2 && s2->registration->nh) {
    nua_handle_t *nh = s2->registration->nh;
    struct message *m;

    nua_unregister(nh, TAG_END());
1603

1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618
    m = s2_wait_for_request(SIP_METHOD_REGISTER); assert(m);
    s2_save_register(m);
    s2_respond_to(m, NULL,
		  SIP_200_OK,
		  SIPTAG_CONTACT(s2->registration->contact),
		  TAG_END());
    assert(s2->registration->contact == NULL);

    s2_free_message(m);

    s2_check_event(nua_r_unregister, 200);

    nua_handle_destroy(nh);
    s2->registration->nh = NULL;
  }
1619
}
1620