sip_util.c 41.5 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 * 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 sip_util.c
26
 *
Pekka Pessi's avatar
Pekka Pessi committed
27 28 29 30 31 32 33 34 35
 * SIP utility functions.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>.
 *
 * @date Created: Tue Jun 13 02:57:51 2000 ppessi
 */

#include "config.h"

36 37 38
/* Avoid casting sip_t to msg_pub_t and sip_header_t to msg_header_t */
#define MSG_PUB_T       struct sip_s
#define MSG_HDR_T       union sip_header_u
Pekka Pessi's avatar
Pekka Pessi committed
39

40
#include <sofia-sip/su_alloc.h>
41
#include <sofia-sip/su_strlst.h>
42
#include <sofia-sip/string0.h>
Pekka Pessi's avatar
Pekka Pessi committed
43

44 45 46 47
#include "sofia-sip/sip_parser.h"
#include <sofia-sip/sip_header.h>
#include <sofia-sip/sip_util.h>
#include <sofia-sip/sip_status.h>
Pekka Pessi's avatar
Pekka Pessi committed
48

49 50
#include <sofia-sip/bnf.h>
#include <sofia-sip/hostdomain.h>
Pekka Pessi's avatar
Pekka Pessi committed
51

52

53
#include <stdio.h>
54 55 56 57 58 59 60
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <float.h>
#include <limits.h>
#include <ctype.h>
Pekka Pessi's avatar
Pekka Pessi committed
61

62
/**
63
 * Compare two SIP addresses ( @From or @To headers).
Pekka Pessi's avatar
Pekka Pessi committed
64 65 66 67 68 69 70
 *
 * @retval nonzero if matching.
 * @retval zero if not matching.
 */
int sip_addr_match(sip_addr_t const *a, sip_addr_t const *b)
{
  return
71
    (a->a_tag == NULL || b->a_tag == NULL ||
Pekka Pessi's avatar
Pekka Pessi committed
72 73
     strcmp(a->a_tag, b->a_tag) == 0)
    &&
74
    str0casecmp(a->a_host, b->a_host) == 0
Pekka Pessi's avatar
Pekka Pessi committed
75
    &&
76
    str0cmp(a->a_user, b->a_user) == 0
Pekka Pessi's avatar
Pekka Pessi committed
77 78 79 80 81
    &&
    str0cmp(a->a_url->url_scheme, b->a_url->url_scheme);
}


82 83 84 85
/**@ingroup sip_contact
 *
 * Create a contact header.
 *
86
 * Create a @Contact header object with the given URL and list of parameters.
87 88 89
 *
 * @param home      memory home
 * @param url       URL (string or pointer to url_t)
90
 * @param p,...     NULL-terminated list of @Contact parameters
91 92
 *
 * @return
93 94
 * A pointer to newly created @Contact header object when successful or NULL
 * upon an error.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
 *
 */
sip_contact_t * sip_contact_create(su_home_t *home,
				   url_string_t const *url,
				   char const *p, ...)
{
  su_strlst_t *l;
  su_home_t *lhome;
  sip_contact_t *m;

  if (url == NULL)
    return NULL;

  l = su_strlst_create_with(NULL, "<", NULL), lhome = su_strlst_home(l);
  if (l == NULL)
    return NULL;

  if (url_is_string(url))
    su_strlst_append(l, (char const *)url);
  else
    su_strlst_append(l, url_as_string(lhome, url->us_url));

  su_strlst_append(l, ">");

  if (p) {
    va_list ap;
    va_start(ap, p);

    for (; p; p = va_arg(ap, char const *)) {
      su_strlst_append(l, ";");
      su_strlst_append(l, p);
    }

    va_end(ap);
  }

  m = sip_contact_make(home, su_strlst_join(l, lhome, ""));

  su_strlst_destroy(l);

  return m;
}

138
/** Convert a @Via header to @Contact header.
139
 *
140
 * The @Contact URI will contain the port number if needed. If transport
141 142 143 144
 * protocol name starts with "TLS", "SIPS:" URI schema is used. Transport
 * parameter is included in the URI unless the transport protocol is UDP.
 *
 * @param home      memory home
145
 * @param v         @Via header field structure
146
 *                  (with <sent-protocol> and <sent-by> parameters)
147
 * @param user      username for @Contact URI (may be NULL)
148 149 150 151
 *
 * @retval contact header structure
 * @retval NULL upon an error
 *
152
 * @sa sip_contact_create_from_via_with_transport(),
153 154
 *     sip_contact_string_from_via()
 */
Pekka Pessi's avatar
Pekka Pessi committed
155
sip_contact_t *
156
sip_contact_create_from_via(su_home_t *home,
Pekka Pessi's avatar
Pekka Pessi committed
157 158 159
			    sip_via_t const *v,
			    char const *user)
{
160
  const char *tp;
Pekka Pessi's avatar
Pekka Pessi committed
161 162 163

  if (!v) return NULL;

164
  tp = v->v_protocol;
165

166 167
  if (tp == sip_transport_udp ||
      strcasecmp(tp, sip_transport_udp) == 0)  /* Default is UDP */
168 169 170 171 172
    tp = NULL;

  return sip_contact_create_from_via_with_transport(home, v, user, tp);
}

173
/** Convert a @Via header to @Contact header.
174
 *
175
 * The @Contact URI will contain the port number and transport parameters if
176 177 178 179
 * needed. If transport protocol name starts with "TLS", "SIPS:" URI schema
 * is used.
 *
 * @param home      memory home
180
 * @param v         @Via header field structure
181
 *                  (with <sent-by> parameter containing host and port)
182 183
 * @param user      username for @Contact URI (may be NULL)
 * @param transport transport name for @Contact URI (may be NULL)
184 185 186 187 188 189
 *
 * @retval contact header structure
 * @retval NULL upon an error
 *
 * @sa sip_contact_create_from_via(), sip_contact_string_from_via()
 */
190
sip_contact_t *
191
sip_contact_create_from_via_with_transport(su_home_t *home,
192 193 194
					   sip_via_t const *v,
					   char const *user,
					   char const *transport)
195 196 197 198 199 200 201
{
  char *s = sip_contact_string_from_via(NULL, v, user, transport);
  sip_contact_t *m = sip_contact_make(home, s);
  su_free(NULL, s);
  return m;
}

202
/** Convert a @Via header to @Contact URL string.
203
 *
204
 * The @Contact URI will contain the port number and transport parameters if
205 206 207 208 209 210 211
 * needed. If transport protocol name starts with "TLS", "SIPS:" URI schema
 * is used.
 *
 * The contact URI string returned will always have angle brackets ("<" and
 * ">") around it.
 *
 * @param home      memory home
212
 * @param v         @Via header field structure
213
 *                  (with <sent-by> parameter containing host and port)
214 215
 * @param user      username for @Contact URI (may be NULL)
 * @param transport transport name for @Contact URI (may be NULL)
216 217 218 219
 *
 * @retval string containing Contact URI with angle brackets
 * @retval NULL upon an error
 */
220 221 222 223 224
char *
sip_contact_string_from_via(su_home_t *home,
			    sip_via_t const *v,
			    char const *user,
			    char const *transport)
225 226
{
  const char *host, *port, *maddr, *comp;
227 228 229
  char const *scheme = "sip:";
  int one = 1;
  char _transport[16];
230 231 232

  if (!v) return NULL;

Pekka Pessi's avatar
Pekka Pessi committed
233
  host = v->v_host;
234 235 236
  if (v->v_received)
    host = v->v_received;
  port = sip_via_port(v, &one);
Pekka Pessi's avatar
Pekka Pessi committed
237
  maddr = v->v_maddr;
238
  comp = v->v_comp;
Pekka Pessi's avatar
Pekka Pessi committed
239

240
  if (host == NULL)
Pekka Pessi's avatar
Pekka Pessi committed
241 242
    return NULL;

243 244
  if (sip_transport_has_tls(v->v_protocol) ||
      sip_transport_has_tls(transport)) {
245
    scheme = "sips:";
246
    if (port && strcmp(port, SIPS_DEFAULT_SERV) == 0)
Pekka Pessi's avatar
Pekka Pessi committed
247
      port = NULL;
248 249 250
    if (port || host_is_ip_address(host))
      transport = NULL;
  }
251 252
  else if (port && strcmp(port, SIP_DEFAULT_SERV) == 0 &&
	   (host_is_ip_address(host) || host_has_domain_invalid(host))) {
253
    port = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
254 255
  }

256
  if (transport && strncasecmp(transport, "SIP/2.0/", 8) == 0)
257 258 259 260 261
    transport += 8;

  /* Make transport parameter lowercase */
  if (transport && strlen(transport) < (sizeof _transport)) {
    char *s = strcpy(_transport, transport);
262
    short c;
Pekka Pessi's avatar
Pekka Pessi committed
263

264 265 266
    for (s = _transport; (c = *s) && c != ';'; s++)
      if (isupper(c))
	*s = tolower(c);
267 268

    transport = _transport;
269 270
  }

271 272 273 274 275 276 277 278 279 280 281
  return su_strcat_all(home,
		       "<",
		       scheme,
		       user ? user : "", user ? "@" : "",
		       host,
		       SIP_STRLOG(":", port),
		       SIP_STRLOG(";transport=", transport),
		       SIP_STRLOG(";maddr=", maddr),
		       SIP_STRLOG(";comp=", comp),
		       ">",
		       NULL);
Pekka Pessi's avatar
Pekka Pessi committed
282 283
}

284 285 286 287 288 289 290 291 292
/** Check if tranport name refers to TLS */
int sip_transport_has_tls(char const *transport_name)
{
  if (!transport_name)
    return 0;

  if (transport_name == sip_transport_tls)
    return 1;

293 294 295 296
  /* transport name starts with TLS or SIP/2.0/TLS */
  return
    strncasecmp(transport_name, "TLS", 3) == 0 ||
    strncasecmp(transport_name, sip_transport_tls, 11) == 0;
297
}
298

Pekka Pessi's avatar
Pekka Pessi committed
299
/**Perform sanity check on a SIP message
300 301
 *
 * Check that the SIP message has all the mandatory fields.
Pekka Pessi's avatar
Pekka Pessi committed
302 303 304
 *
 * @param sip SIP message to be checked
 *
305
 * @return
306 307
 * When the SIP message fulfills the minimum requirements, return zero,
 * otherwise a negative status code.
Pekka Pessi's avatar
Pekka Pessi committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
 */
int
sip_sanity_check(sip_t const *sip)
{
  if (!sip ||
      !((sip->sip_request != NULL) ^ (sip->sip_status != NULL)) ||
      !sip->sip_to ||
      !sip->sip_from ||
      !sip->sip_call_id ||
      !sip->sip_cseq ||
      !sip->sip_via ||
      (sip->sip_flags & MSG_FLG_TRUNC))
    return -1;  /* Bad request */

  if (sip->sip_request) {
    url_t const *ruri = sip->sip_request->rq_url;

    switch (ruri->url_type) {
    case url_invalid:
      return -1;

    case url_sip: case url_sips: case url_im: case url_pres:
      if (!ruri->url_host || strlen(ruri->url_host) == 0)
	return -1;
      break;

334
    case url_tel:
Pekka Pessi's avatar
Pekka Pessi committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
      if (!ruri->url_user || strlen(ruri->url_user) == 0)
	return -1;
      break;
    }

    if (sip->sip_request->rq_method != sip->sip_cseq->cs_method)
      return -1;
    if (sip->sip_request->rq_method == sip_method_unknown &&
	str0casecmp(sip->sip_request->rq_method_name,
		    sip->sip_cseq->cs_method_name))
      return -1;
  }

  return 0;
}

351 352 353 354 355 356
/** Decode a string containg header field.
 *
 * The header object is initialized with the contents of the string. The
 * string is modified when parsing. The home is used to allocate extra
 * memory required when parsing, e.g., for parameter list or when there
 * string contains multiple header fields.
357
 *
358 359 360 361 362 363 364
 * @deprecated
 * Use msg_header_make() or header-specific make functions, e.g.,
 * sip_via_make().
 *
 * @retval 0 when successful
 * @retval -1 upon an error.
 */
365
issize_t sip_header_field_d(su_home_t *home, sip_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
366 367
{
  if (h && s && s[slen] == '\0') {
368
    size_t n = span_lws(s);
Pekka Pessi's avatar
Pekka Pessi committed
369
    s += n; slen -= n;
370

371
    for (n = slen; n >= 1 && IS_LWS(s[n - 1]); n--)
Pekka Pessi's avatar
Pekka Pessi committed
372
      ;
373

374
    s[n] = '\0';
375

376 377
    assert(SIP_HDR_TEST(h));

Pekka Pessi's avatar
Pekka Pessi committed
378 379 380 381 382 383
    return h->sh_class->hc_parse(home, h, s, slen);
  }
  else
    return -1;
}

384 385 386 387
/** Encode a SIP header contents.
 *
 * @deprecated Use msg_header_field_e() instead.
 */
388
issize_t sip_header_field_e(char *b, isize_t bsiz, sip_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
389
{
390
  return msg_header_field_e(b, bsiz, h, flags);
Pekka Pessi's avatar
Pekka Pessi committed
391 392 393
}

/** Convert the header @a h to a string allocated from @a home. */
394
char *sip_header_as_string(su_home_t *home, sip_header_t const *h)
Pekka Pessi's avatar
Pekka Pessi committed
395
{
396
  ssize_t len;
Pekka Pessi's avatar
Pekka Pessi committed
397 398 399 400 401 402 403
  char *rv, s[128];

  if (h == NULL)
    return NULL;

  len = sip_header_field_e(s, sizeof(s), h, 0);

404
  if (len >= 0 && (size_t)len < sizeof(s))
Pekka Pessi's avatar
Pekka Pessi committed
405 406 407 408 409 410 411 412 413 414
    return su_strdup(home, s);

  if (len == -1)
    len = 2 * sizeof(s);
  else
    len += 1;

  for (rv = su_alloc(home, len);
       rv;
       rv = su_realloc(home, rv, len)) {
415
    ssize_t n = sip_header_field_e(rv, len, h, 0);
Pekka Pessi's avatar
Pekka Pessi committed
416 417 418
    if (n > -1 && n + 1 <= len)
      break;
    if (n > -1)			/* glibc >2.1 */
419
      len = n + 1;
Pekka Pessi's avatar
Pekka Pessi committed
420
    else			/* glibc 2.0 */
421
      len *= 2;
Pekka Pessi's avatar
Pekka Pessi committed
422 423 424 425 426 427
  }

  return rv;
}

/** Calculate size of a SIP header. */
428
isize_t sip_header_size(sip_header_t const *h)
Pekka Pessi's avatar
Pekka Pessi committed
429 430 431 432 433 434 435 436
{
  assert(h == NULL || h == SIP_NONE || h->sh_class);
  if (h == NULL || h == SIP_NONE)
    return 0;
  else
    return h->sh_class->hc_dxtra(h, h->sh_class->hc_size);
}

437
/** Duplicate a url or make a url out of string.
Pekka Pessi's avatar
Pekka Pessi committed
438 439 440 441 442 443 444 445
 * @deprecated Use url_hdup() instead.
 */
url_t *sip_url_dup(su_home_t *home, url_t const *o)
{
  return url_hdup(home, o);
}

/**Calculate Q value.
446
 *
447
 * Convert q-value string @a q to numeric value
Pekka Pessi's avatar
Pekka Pessi committed
448 449 450
 * in range (0..1000).  Q values are used, for instance, to describe
 * relative priorities of registered contacts.
 *
451
 * @param q q-value string <code>("1" | "." 1,3DIGIT)</code>
452
 *
453
 * @return An integer in range 0 .. 1000.
Pekka Pessi's avatar
Pekka Pessi committed
454
 */
455
unsigned sip_q_value(char const *q)
Pekka Pessi's avatar
Pekka Pessi committed
456
{
Pekka Pessi's avatar
Pekka Pessi committed
457
  unsigned value = 0;
Pekka Pessi's avatar
Pekka Pessi committed
458 459 460 461 462 463 464 465 466 467 468

  if (!q)
    return 1000;
  if (q[0] != '0' && q[0] != '.' && q[0] != '1')
    return 500; /* Garbage... */
  while (q[0] == '0')
    q++;
  if (q[0] >= '1' && q[0] <= '9')
    return 1000;
  if (q[0] == '\0')
    return 0;
469
  if (q[0] != '.')
Pekka Pessi's avatar
Pekka Pessi committed
470 471 472 473 474 475 476 477 478 479 480
    return 500;    /* Garbage... */

  if (q[1] >= '0' && q[1] <= '9') {
    value = (q[1] - '0') * 100;
    if (q[2] >= '0' && q[2] <= '9') {
      value += (q[2] - '0') * 10;
      if (q[3] >= '0' && q[3] <= '9') {
	value += (q[3] - '0');
	if (q[4] > '5' && q[4] <= '9')
	  /* Round upwards */
	  value += 1;
481
	else if (q[4] == '5')
Pekka Pessi's avatar
Pekka Pessi committed
482 483 484 485 486 487 488 489 490 491 492
	  value += value & 1; /* Round to even */
      }
    }
  }

  return value;
}


/**@ingroup sip_route
 *
493
 * Get first route header and remove it from its fragment chain.
Pekka Pessi's avatar
Pekka Pessi committed
494 495 496 497 498 499 500
 *
 */
sip_route_t *sip_route_remove(msg_t *msg, sip_t *sip)
{
  sip_route_t *r;

  if ((r = sip->sip_route))
Pekka Pessi's avatar
Pekka Pessi committed
501
    msg_header_remove(msg, (msg_pub_t *)sip, (msg_header_t *)r);
Pekka Pessi's avatar
Pekka Pessi committed
502 503 504 505 506 507

  return r;
}

/**@ingroup sip_route
 *
508
 * Get last route header and remove it from its fragment chain.
Pekka Pessi's avatar
Pekka Pessi committed
509 510 511 512 513 514
 *
 */
sip_route_t *sip_route_pop(msg_t *msg, sip_t *sip)
{
  sip_route_t *r;

515
  for (r = sip->sip_route; r; r = r->r_next)
Pekka Pessi's avatar
Pekka Pessi committed
516
    if (r->r_next == NULL) {
Pekka Pessi's avatar
Pekka Pessi committed
517
      msg_header_remove(msg, (msg_pub_t *)sip, (msg_header_t *)r);
Pekka Pessi's avatar
Pekka Pessi committed
518 519 520 521 522 523 524 525 526
      return r;
    }

  return NULL;
}


/**@ingroup sip_route
 *
527
 * Get first route header and rewrite the RequestURI.
Pekka Pessi's avatar
Pekka Pessi committed
528 529 530 531
 */
sip_route_t *sip_route_follow(msg_t *msg, sip_t *sip)
{
  if (sip->sip_route) {
532 533
    /* XXX - in case of outbound proxy, route may contain our address */

Pekka Pessi's avatar
Pekka Pessi committed
534 535 536 537 538 539 540
    sip_route_t *r = sip_route_remove(msg, sip);
    sip_request_t *rq = sip->sip_request;

    rq = sip_request_create(msg_home(msg), rq->rq_method, rq->rq_method_name,
			    (url_string_t const *)r->r_url, rq->rq_version);
    url_strip_transport(rq->rq_url);

Pekka Pessi's avatar
Pekka Pessi committed
541
    msg_header_insert(msg, (msg_pub_t *)sip, (msg_header_t *)rq);
Pekka Pessi's avatar
Pekka Pessi committed
542 543 544 545 546 547

    return r;
  }
  return NULL;
}

Pekka Pessi's avatar
Pekka Pessi committed
548
/**@ingroup sip_route
549
 *
Pekka Pessi's avatar
Pekka Pessi committed
550 551 552 553
 * Check if route header has lr param.
 *
 * "lr" param can be either URL or header parameter.
 */
554
int
Pekka Pessi's avatar
Pekka Pessi committed
555 556 557 558 559 560 561 562 563 564 565 566
sip_route_is_loose(sip_route_t const *r)
{
  if (!r)
    return 0;
  if (r->r_url->url_params)
    return url_has_param(r->r_url, "lr");
  else
    return r->r_params && msg_params_find(r->r_params, "lr") != NULL;
}

/**@ingroup sip_route
 *
567
 * Reverse a route header (@Route, @RecordRoute, @Path, @ServiceRoute).
Pekka Pessi's avatar
Pekka Pessi committed
568
 */
569
sip_route_t *sip_route_reverse_as(su_home_t *home,
570
				  msg_hclass_t *hc,
571
				  sip_route_t const *route)
Pekka Pessi's avatar
Pekka Pessi committed
572 573 574 575 576
{
  sip_route_t *reverse = NULL;
  sip_route_t r[1], *tmp;
  sip_route_init(r);

577 578
  r->r_common->h_class = hc;

Pekka Pessi's avatar
Pekka Pessi committed
579 580 581 582
  for (reverse = NULL; route; route = route->r_next) {
    *r->r_url = *route->r_url;
    /* Fix broken (Record-)Routes without <> */
    if (r->r_url->url_params == NULL
583 584
	&& r->r_params
	&& r->r_params[0]
Pekka Pessi's avatar
Pekka Pessi committed
585 586 587 588 589 590 591
	&& (r->r_params[0][0] == 'l' || r->r_params[0][0] == 'L')
	&& (r->r_params[0][1] == 'r' || r->r_params[0][1] == 'R')
	&& (r->r_params[0][2] == '=' || r->r_params[0][2] == 0))
      r->r_url->url_params = route->r_params[0],
	r->r_params = route->r_params + 1;
    else
      r->r_params = route->r_params;
592
    tmp = (sip_route_t *)msg_header_dup_as(home, hc, (msg_header_t *)r);
593
    if (!tmp)
Pekka Pessi's avatar
Pekka Pessi committed
594 595 596 597 598 599 600 601
      goto error;
    tmp->r_next = reverse;
    reverse = tmp;
  }

  return reverse;

 error:
602
  msg_header_free_all(home, (msg_header_t *)reverse);
Pekka Pessi's avatar
Pekka Pessi committed
603 604 605 606 607 608
  return NULL;
}


/**@ingroup sip_route
 *
609
 * Reverse a @Route header.
610
 *
611
 * Reverse A route header like @RecordRoute or @Path.
612 613 614 615 616 617 618 619
 */
sip_route_t *sip_route_reverse(su_home_t *home, sip_route_t const *route)
{
  return sip_route_reverse_as(home, sip_route_class, route);
}


/**@ingroup sip_route
Pekka Pessi's avatar
Pekka Pessi committed
620
 *
621
 * Fix and duplicate a route header (@Route, @RecordRoute, @Path, @ServiceRoute).
Pekka Pessi's avatar
Pekka Pessi committed
622 623
 *
 */
624
sip_route_t *sip_route_fixdup_as(su_home_t *home,
625
				 msg_hclass_t *hc,
626
				 sip_route_t const *route)
Pekka Pessi's avatar
Pekka Pessi committed
627 628 629 630 631 632 633 634 635 636
{
  sip_route_t *copy = NULL;
  sip_route_t r[1], **rr;
  sip_route_init(r);

  /* Copy the record route as route */
  for (rr = &copy; route; route = route->r_next) {
    *r->r_url = *route->r_url;
    /* Fix broken (Record-)Routes without <> */
    if (r->r_url->url_params == NULL
637 638
	&& r->r_params
	&& r->r_params[0]
Pekka Pessi's avatar
Pekka Pessi committed
639 640 641 642 643 644 645
	&& (r->r_params[0][0] == 'l' || r->r_params[0][0] == 'L')
	&& (r->r_params[0][1] == 'r' || r->r_params[0][1] == 'R')
	&& (r->r_params[0][2] == '=' || r->r_params[0][2] == 0))
      r->r_url->url_params = route->r_params[0],
	r->r_params = route->r_params + 1;
    else
      r->r_params = route->r_params;
646
    *rr = (sip_route_t *)msg_header_dup_as(home, hc, (msg_header_t *)r);
Pekka Pessi's avatar
Pekka Pessi committed
647 648 649 650 651 652 653
    if (!*rr) goto error;
    rr = &(*rr)->r_next;
  }

  return copy;

 error:
654
  msg_header_free_all(home, (msg_header_t *)copy);
Pekka Pessi's avatar
Pekka Pessi committed
655 656 657
  return NULL;
}

658 659 660

/**@ingroup sip_route
 *
661
 * Fix and duplicate a @Route header.
662
 *
663
 * Copy a route header like @RecordRoute or @Path as @Route.
664 665 666 667 668 669 670
 *
 */
sip_route_t *sip_route_fixdup(su_home_t *home, sip_route_t const *route)
{
  return sip_route_fixdup_as(home, sip_route_class, route);
}

Pekka Pessi's avatar
Pekka Pessi committed
671 672 673 674 675
static void sip_fragment_clear_chain(sip_header_t *h)
{
  void const *next;

  for (h = h; h; h = h->sh_succ) {
676
    next = (char *)h->sh_data + h->sh_len;
Pekka Pessi's avatar
Pekka Pessi committed
677 678 679 680 681 682 683 684 685 686 687 688 689 690

    sip_fragment_clear(h->sh_common);

    if (!next ||
	!h->sh_succ ||
	h->sh_next != h->sh_succ ||
	h->sh_succ->sh_data != next ||
	h->sh_succ->sh_len)
      return;
  }
}

/**@ingroup sip_route
 *
691
 * Fix @Route header.
Pekka Pessi's avatar
Pekka Pessi committed
692 693 694 695 696
 */
sip_route_t *sip_route_fix(sip_route_t *route)
{
  sip_route_t *r;
  sip_header_t *h = NULL;
697
  size_t i;
Pekka Pessi's avatar
Pekka Pessi committed
698 699 700

  for (r = route; r; r = r->r_next) {
    /* Keep track of first header structure on this header line */
701
    if (!h
702
	|| (char *)h->sh_data + h->sh_len != r->r_common->h_data
Pekka Pessi's avatar
Pekka Pessi committed
703 704 705 706
	|| r->r_common->h_len)
      h = (sip_header_t *)r;

    if (r->r_url->url_params == NULL
707 708
	&& r->r_params
	&& r->r_params[0]
Pekka Pessi's avatar
Pekka Pessi committed
709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
	&& (r->r_params[0][0] == 'l' || r->r_params[0][0] == 'L')
	&& (r->r_params[0][1] == 'r' || r->r_params[0][1] == 'R')
	&& (r->r_params[0][2] == '=' || r->r_params[0][2] == 0)) {
      r->r_url->url_params = r->r_params[0];

      for (i = 0; r->r_params[i]; i++)
	((char const **)r->r_params)[i] = r->r_params[i + 1];

      sip_fragment_clear_chain(h);
    }
  }

  return route;
}

/**@ingroup sip_via
 *
726
 * Get first via header and remove it from its fragment chain.
Pekka Pessi's avatar
Pekka Pessi committed
727 728 729 730
 */
sip_via_t *sip_via_remove(msg_t *msg, sip_t *sip)
{
  sip_via_t *v;
Pekka Pessi's avatar
Pekka Pessi committed
731 732 733

  if (sip == NULL)
    return NULL;
734

Pekka Pessi's avatar
Pekka Pessi committed
735 736 737 738 739 740 741 742
  for (v = sip->sip_via; v; v = v->v_next) {
    sip_fragment_clear(v->v_common);

    if (v->v_next != (void *)v->v_common->h_succ)
      break;
  }

  if ((v = sip->sip_via))
Pekka Pessi's avatar
Pekka Pessi committed
743
    msg_header_remove(msg, (msg_pub_t *)sip, (msg_header_t *)v);
Pekka Pessi's avatar
Pekka Pessi committed
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764

  return v;
}

/** Serialize payload.
 *
 * The sip_payload_serialize() adds missing headers to MIME multiparty payload,
 * encodes them and orders them in header chain.  It also calculates the total
 * length of the payload.
 */
unsigned long sip_payload_serialize(msg_t *msg, sip_payload_t *pl)
{
  unsigned long total;

  for (total = 0; pl; pl = (sip_payload_t *)pl->pl_next) {
    total += (unsigned)pl->pl_common->h_len;
  }

  return total;
}

765
/**
766 767
 * Remove extra parameters from an AOR URL.
 *
768
 * The extra parameters listed in the @RFC3261 table 1 include port number,
769
 * method, maddr, ttl, transport, lr and headers.
770
 *
771 772 773 774
 * @note The funtion modifies the @a url and the strings attached to it.
 *
 * @retval 0 when successful
 * @retval -1 upon an error
Pekka Pessi's avatar
Pekka Pessi committed
775 776 777 778 779 780 781 782 783
 */
int sip_aor_strip(url_t *url)
{
  if (url == NULL)
    return -1;

  url->url_port = NULL;
  url->url_headers = NULL;

784 785
  if (url->url_params)
    url_strip_transport(url);
786

787
  if (url->url_params)
788
    url->url_params =
789
      url_strip_param_string((char *)url->url_params, "lr");
Pekka Pessi's avatar
Pekka Pessi committed
790 791 792 793

  return 0;
}

794
/** Compare @SecurityVerify header with @SecurityServer header. */
Pekka Pessi's avatar
Pekka Pessi committed
795 796
int sip_security_verify_compare(sip_security_server_t const *s,
				sip_security_verify_t const *v,
Pekka Pessi's avatar
Pekka Pessi committed
797
				msg_param_t *return_d_ver)
Pekka Pessi's avatar
Pekka Pessi committed
798
{
799 800
  size_t i, j;
  int retval, digest;
Pekka Pessi's avatar
Pekka Pessi committed
801
  msg_param_t const *s_params, *v_params, empty[] = { NULL };
Pekka Pessi's avatar
Pekka Pessi committed
802 803 804 805 806 807 808 809 810 811

  if (return_d_ver)
    *return_d_ver = NULL;

  if (s == NULL)
    return 0;

  for (;;s = s->sa_next, v = v->sa_next) {
    if (s == NULL || v == NULL)
      return (s == NULL) - (v == NULL);
812

Pekka Pessi's avatar
Pekka Pessi committed
813 814 815 816 817 818 819 820 821 822
    if ((retval = str0cmp(s->sa_mec, v->sa_mec)))
      return retval;

    digest = strcasecmp(s->sa_mec, "Digest") == 0;

    s_params = s->sa_params, v_params = v->sa_params;

    if (digest && s_params == NULL && v_params != NULL)
      s_params = empty;

823
    if (s_params == NULL || v_params == NULL) {
Pekka Pessi's avatar
Pekka Pessi committed
824 825
      if ((retval = (s_params == NULL) - (v_params == NULL)))
	return retval;
826 827
      continue;
    }
Pekka Pessi's avatar
Pekka Pessi committed
828 829

    for (i = 0, j = 0;; i++, j++) {
830
      if (digest && v_params[j] &&
Pekka Pessi's avatar
Pekka Pessi committed
831 832 833 834 835 836 837
	  strncasecmp(v_params[j], "d-ver=", 6) == 0) {
	if (return_d_ver)
	  *return_d_ver = v_params[j] + strlen("d-ver=");
	j++;
      }

      retval = str0cmp(s_params[i], v_params[j]);
838 839

      if (retval || s_params[i] == NULL || v_params[j] == NULL)
Pekka Pessi's avatar
Pekka Pessi committed
840 841 842 843 844 845 846 847
	break;
    }

    if (retval)
      return retval;
  }
}

848
/** Select best mechanism from @SecurityClient header.
Pekka Pessi's avatar
Pekka Pessi committed
849
 *
850
 * @note We assume that @SecurityServer header in @a s is sorted by
Pekka Pessi's avatar
Pekka Pessi committed
851 852 853
 * preference.
 */
sip_security_client_t const *
854 855
sip_security_client_select(sip_security_client_t const *client,
			   sip_security_server_t const *server)
Pekka Pessi's avatar
Pekka Pessi committed
856
{
857
  sip_security_server_t const *c, *s;
Pekka Pessi's avatar
Pekka Pessi committed
858

859 860
  if (server == NULL || client == NULL)
    return NULL;
Pekka Pessi's avatar
Pekka Pessi committed
861

862 863 864 865
  for (s = server; s; s = s->sa_next) {
    for (c = client; c; c = c->sa_next) {
      if (str0cmp(s->sa_mec, c->sa_mec) == 0)
	return c;
Pekka Pessi's avatar
Pekka Pessi committed
866 867
    }
  }
868 869

  return NULL;
Pekka Pessi's avatar
Pekka Pessi committed
870 871 872 873
}

/**Checks if the response with given response code terminates dialog or
 * dialog usage.
874
 *
Pekka Pessi's avatar
Pekka Pessi committed
875 876 877
 * @return -1 if the response with given code terminates whole dialog.
 * @return 1 if the response terminates the dialog usage.
 * @return 0 if the response does not terminate dialog or dialog usage.
878
 *
879 880 881 882 883 884
 * @return
 * The @a *return_graceful_terminate_usage is set to 1, if application
 * should gracefully terminate its dialog usage. It is set to 0, if no
 * graceful terminate is required. If it is up to application policy to
 * decide whether to gracefully terminate or not, the
 * @a *return_graceful_terminate_usage is left unmodified.
Pekka Pessi's avatar
Pekka Pessi committed
885
 *
886
 * @RFC5057
Pekka Pessi's avatar
Pekka Pessi committed
887 888 889 890 891 892 893 894
 */
int sip_response_terminates_dialog(int response_code,
				   sip_method_t method,
				   int *return_graceful_terminate_usage)
{
  enum { no_effect, terminate_usage = 1, terminate_dialog = -1 };
  int dummy;

895
  if (!return_graceful_terminate_usage)
Pekka Pessi's avatar
Pekka Pessi committed
896 897 898 899 900
    return_graceful_terminate_usage = &dummy;

  if (response_code < 300)
    return *return_graceful_terminate_usage = 0;

901
  /*
Pekka Pessi's avatar
Pekka Pessi committed
902 903 904 905 906 907
      3xx responses: Redirection mid-dialog is not well understood in SIP,
      but whatever effect it has impacts the entire dialog and all of
      its usages equally.  In our example scenario, both the
      subscription and the invite usage would be redirected by this
      single response.
  */
908
  if (response_code < 400)
Pekka Pessi's avatar
Pekka Pessi committed
909 910 911 912
    return *return_graceful_terminate_usage = 0;

  if (response_code < 500) switch (response_code) {
  default:
913
  case 400: /** @par 400 and unrecognized 4xx responses
914

Pekka Pessi's avatar
Pekka Pessi committed
915 916 917 918 919 920 921 922 923
      These responses affect only the NOTIFY transaction, not the
      subscription, the dialog it resides in (beyond affecting the local
      CSeq), or any other usage of that dialog. In general, the response
      is a complaint about this transaction, not the usage or dialog the
      transaction occurs in.
    */
    *return_graceful_terminate_usage = 0;
    return 0;

924
  case 401:
925 926 927 928
  case 407: /** @par 401 Unauthorized and 407 Proxy Authentication Required

      This request, not the subscription or dialog, is being challenged. The
      usages and dialog are not terminated.
929
    */
Pekka Pessi's avatar
Pekka Pessi committed
930 931 932
    *return_graceful_terminate_usage = 0;
    return 0;

933
  case 402: /** @par 402 Payment Required
934

Pekka Pessi's avatar
Pekka Pessi committed
935 936 937 938 939 940
      This is a reserved response code. If encountered, it should be
      treated as an unrecognized 4xx.
    */
    *return_graceful_terminate_usage = 0;
    return 0;

941
  case 403: /** @par 403 Forbidden
942

Pekka Pessi's avatar
Pekka Pessi committed
943 944 945 946 947 948
      This response terminates the subscription, but has no effect on
      any other usages of the dialog. In our example scenario, the
      invite usage continues to exist. Similarly, if the 403 came in
      response to a re-INVITE, the invite usage would be terminated, but
      not the subscription.
    */
949 950
    *return_graceful_terminate_usage = 0;
    return 0;
951 952

  case 404: /** @par 404 Not Found
953

Pekka Pessi's avatar
Pekka Pessi committed
954 955
      This response destroys the dialog and all usages sharing it. The
      Request-URI that is being 404ed is the remote target set by the
956
      @Contact provided by the peer. Getting this response means
Pekka Pessi's avatar
Pekka Pessi committed
957 958 959 960
      something has gone fundamentally wrong with the dialog state.
    */
    return terminate_dialog;

961
  case 405: /** @par 405 Method Not Allowed
962

Pekka Pessi's avatar
Pekka Pessi committed
963 964 965 966 967 968 969
      In our example scenario, this response destroys the subscription,
      but not the invite usage or the dialog. It's an aberrant case for
      NOTIFYs to receive a 405 since they only come as a result to
      something that creates subscription. In general, a 405 within a
      given usage affects only that usage, but does not affect other
      usages of the dialog.
    */
970 971 972 973 974
    switch (method) {
    case sip_method_notify:
    case sip_method_subscribe:
    case sip_method_invite:
      return terminate_usage;
975 976 977
    default:
      *return_graceful_terminate_usage = 0;
      return 0;
978
    }
Pekka Pessi's avatar
Pekka Pessi committed
979

980
  case 406: /** @par 406 Not Acceptable
981

982
      These responses concern details of the message in the transaction.
Pekka Pessi's avatar
Pekka Pessi committed
983 984 985 986 987 988 989
      Subsequent requests in this same usage may succeed. Neither the
      usage nor dialog is terminated, other usages sharing this dialog
      are unaffected.
      */
    *return_graceful_terminate_usage = 0;
    return 0;

990
  case 408: /** @par 408 Request Timeout
991

Pekka Pessi's avatar
Pekka Pessi committed
992 993 994 995 996 997
      Receiving a 408 will have the same effect on
      usages and dialogs as a real transaction timeout as described in
      Section 3.2.
    */
    return terminate_usage;

998
  case 410: /** @par 410 Gone
999

Pekka Pessi's avatar
Pekka Pessi committed
1000 1001
      This response destroys the dialog and all usages sharing
      it.  The Request-URI that is being rejected is the remote target
1002
      set by the @Contact provided by the peer.  Similar to 404, getting
Pekka Pessi's avatar
Pekka Pessi committed
1003 1004 1005 1006 1007 1008 1009
      this response means something has gone fundamentally wrong with
      the dialog state, its slightly less aberrant in that the other
      endpoint recognizes that this was once a valid URI that it isn't
      willing to respond to anymore.
    */
    return terminate_dialog;

1010
  case 412: /* Conditional Request Failed: */
Pekka Pessi's avatar
Pekka Pessi committed
1011 1012 1013
  case 413: /* Request Entity Too Large: */
  case 414: /* Request-URI Too Long: */
  case 415: /* Unsupported Media Type: */
1014
    /** @par 412, 413, 414 and 415
1015

1016
      These responses concern details of the message in the transaction.
Pekka Pessi's avatar
Pekka Pessi committed
1017 1018 1019 1020 1021 1022 1023
      Subsequent requests in this same usage may succeed. Neither the usage
      nor dialog is terminated, other usages sharing this dialog are
      unaffected.
    */
    *return_graceful_terminate_usage = 0;
    return 0;

1024
  case 416: /** @par 416 Unsupported URI Scheme
1025

Pekka Pessi's avatar
Pekka Pessi committed
1026 1027
      Similar to 404 and 410, this response
      came to a request whose Request-URI was provided by the peer in a
1028
      @Contact header field.  Something has gone fundamentally wrong, and
Pekka Pessi's avatar
Pekka Pessi committed
1029 1030 1031 1032
      the dialog and all of its usages are destroyed.
    */
    return terminate_dialog;

1033 1034 1035 1036 1037
  case 417:
    /** @par 417 Uknown Resource-Priority
      The effect of this response on usages
      and dialogs is analgous to that for 420 and 488.  The usage is not
      affected.  The dialog is only affected by a change in its local
1038
      @CSeq.  No other usages of the dialog are affected.
1039 1040
    */

1041
  case 420: /* Bad Extension */
1042 1043 1044 1045
  case 421: /* Extension Required */

    /** @par 420 Bad Extension and 421 Extension Required

Pekka Pessi's avatar
Pekka Pessi committed
1046 1047
      These responses are objecting to the request, not the usage. The
      usage is not affected. The dialog is only affected by a change in
1048
      its local @CSeq. No other usages of the dialog are affected.
Pekka Pessi's avatar
Pekka Pessi committed
1049
    */
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061

  case 422: /** @par 422 Session Interval Too Small

      This response will not be returned to
      a NOTIFY in our example scenario.  This response is non-sensical
      for any mid-usage request.  If it is received, an element in the
      path of the request is violating protocol, and the recipient
      should treat this as it would an unknown 4xx response.  If the
      response came to a request that was attempting to establish a new
      usage in an existing dialog, no new usage is created and existing
      usages are unaffected.
    */
Pekka Pessi's avatar
Pekka Pessi committed
1062

1063
  case 423: /** @par 423 Interval Too Brief
1064

Pekka Pessi's avatar
Pekka Pessi committed
1065 1066 1067 1068 1069 1070
      This response won't happen in our example
      scenario, but if it came in response to a re-SUBSCRIBE, the
      subscribe usage is not destroyed (or otherwise affected).  No
      other usages of the dialog are affected.
    */

1071 1072 1073 1074
  case 428: /** @par 428 Use Identity Header

      This response objects to the request, not
      the usage.  The usage is not affected.  The dialog is only
1075
      affected by a change in its local @CSeq.  No other usages of the
1076 1077
      dialog are affected. */

1078
  case 429: /** @par 429 Provide Referrer Identity
1079

Pekka Pessi's avatar
Pekka Pessi committed
1080 1081
      This response won't be returned to a NOTIFY as in our example
      scenario, but when it is returned to a REFER, it is objecting to
1082
      the REFER request itself, not any usage the REFER occurs within.
Pekka Pessi's avatar
Pekka Pessi committed
1083 1084
      The usage is unaffected. Any other usages sharing this dialog are
      unaffected. The dialog is only affected by a change in its local
1085
      @CSeq.
Pekka Pessi's avatar
Pekka Pessi committed
1086
    */
1087 1088 1089

  case 436: case 437: case 438:
    /** @par 436 Bad Identity-Info, 437 Unsupported Certificate, 438 Invalid \
1090
     *  Identity Header
1091 1092 1093
     *
     * These responses object to the request, not the usage.
     * The usage is not affected.  The dialog is only affected by a
1094
     * change in its local @CSeq.  No other usages of the dialog are
1095 1096
     * affected.
     */
Pekka Pessi's avatar
Pekka Pessi committed
1097 1098 1099 1100
    *return_graceful_terminate_usage = 0;
    return 0;


1101 1102 1103
  case 480: /** @par 480 Temporarily Unavailable

      @RFC3261 is unclear on what this response means for mid-usage
Pekka Pessi's avatar
Pekka Pessi committed
1104 1105
      requests. Clarifications will be made to show that this response
      affects only the usage in which the request occurs. No other usages
1106
      are affected. If the response included a @RetryAfter header field,
Pekka Pessi's avatar
Pekka Pessi committed
1107 1108 1109 1110 1111 1112
      further requests in that usage should not be sent until the indicated
      time has past. Requests in other usages may still be sent at any time.
    */
    return terminate_usage;


1113
  case 481: /** @par 481 Call/Transaction Does Not Exist
1114

Pekka Pessi's avatar
Pekka Pessi committed
1115 1116 1117
      This response indicates that the peer has lost its copy of the dialog
      state. The dialog and any usages sharing it are destroyed.

1118
      The dialog
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134
      itself should not be destroyed unless this was the last usage.
      The effects of a 481 on a dialog and its usages are the most
      ambiguous of any final response.  There are implementations that
      have chosen the meaning recommended here, and others that destroy
      the entire dialog without regard to the number of outstanding
      usages.  Going forward with this clarification will allow those
      deployed implementations that assumed only the usage was destroyed
      to work with a wider number of implementations.  Those that made
      the other choice will continue to function as they do now,
      suffering at most the same extra messages needed for a peer to
      discover that that other usages have gone away that they currently
      do.  However, the necessary clarification to @RFC3261 needs to
      make it very clear that the ability to terminate usages
      independently from the overall dialog using a 481 is not
      justification for designing new applications that count on
      multiple usages in a dialog.
Pekka Pessi's avatar
Pekka Pessi committed
1135
    */
1136
    return terminate_usage;
Pekka Pessi's avatar
Pekka Pessi committed
1137 1138


1139
  case 482: /** @par 482 Loop Detected
1140

Pekka Pessi's avatar
Pekka Pessi committed
1141
      This response is aberrant mid-dialog.  It will
1142
      only occur if the @RecordRoute header field was improperly
Pekka Pessi's avatar
Pekka Pessi committed
1143 1144 1145 1146
      constructed by the proxies involved in setting up the dialog's
      initial usage, or if a mid-dialog request forks and merges (which
      should never happen).  Future requests using this dialog state
      will also fail.  The dialog and any usages sharing it are
1147
      destroyed.
Pekka Pessi's avatar
Pekka Pessi committed
1148 1149 1150 1151
    */
    return terminate_dialog;


1152
  case 483: /** @par 483 Too Many Hops
1153

Pekka Pessi's avatar
Pekka Pessi committed
1154 1155
      Similar to 482, receiving this mid-dialog is
      aberrant.  Unlike 482, recovery may be possible by increasing
1156 1157
      @MaxForwards (assuming that the requester did something strange
      like using a smaller value for @MaxForwards in mid-dialog requests
Pekka Pessi's avatar
Pekka Pessi committed
1158
      than it used for an initial request).  If the request isn't tried
1159
      with an increased @MaxForwards, then the agent should attempt to
Pekka Pessi's avatar
Pekka Pessi committed
1160
      gracefully terminate this usage and all other usages that share
1161
      its dialog.
Pekka Pessi's avatar
Pekka Pessi committed
1162 1163 1164 1165
    */
    *return_graceful_terminate_usage = 1;
    return 0;

1166
  case 484: /* Address Incomplete */
Pekka Pessi's avatar
Pekka Pessi committed
1167
  case 485: /* Ambiguous */
1168 1169
    /** @par 484 Address Incomplete and 485 Ambiguous

Pekka Pessi's avatar
Pekka Pessi committed
1170 1171
      Similar to 404 and 410, these
      responses came to a request whose Request-URI was provided by the
1172
      peer in a @Contact header field.  Something has gone fundamentally
Pekka Pessi's avatar
Pekka Pessi committed
1173 1174 1175 1176
      wrong, and the dialog and all of its usages are destroyed.
    */
    return terminate_dialog;

1177
  case 486: /** @par 486 Busy Here
1178

Pekka Pessi's avatar
Pekka Pessi committed
1179 1180 1181 1182 1183
      This response is non-sensical in our example scenario,
      or in any scenario where this response comes inside an established
      usage.  If it occurs in that context, it should be treated as an
      unknown 4xx response.  The usage, and any other usages sharing its
      dialog are unaffected.  The dialog is only affected by the change
1184
      in its local @CSeq.  If this response is to a request that is
Pekka Pessi's avatar
Pekka Pessi committed
1185 1186 1187 1188 1189 1190 1191 1192
      attempting to establish a new usage within an existing dialog
      (such as an INVITE sent within a dialog established by a
      subscription), the request fails, no new usage is created, and no
      other usages are affected.
    */
    *return_graceful_terminate_usage = 0;
    return 0;

1193
  case 487: /** @par 487 Request Terminated
1194

Pekka Pessi's avatar
Pekka Pessi committed
1195 1196 1197 1198 1199 1200 1201 1202 1203
      This response speaks to the disposition of a
      particular request (transaction).  The usage in which that request
      occurs is not affected by this response (it may be affected by
      another associated request within that usage).  No other usages
      sharing this dialog are affected.
    */
    *return_graceful_terminate_usage = 0;
    return 0;

1204
  case 488: /** @par 488 Not Acceptable Here
1205

Pekka Pessi's avatar
Pekka Pessi committed
1206 1207
      This response is objecting to the request,
      not the usage.  The usage is not affected.  The dialog is only
1208
      affected by a change in its local @CSeq.  No other usages of the
Pekka Pessi's avatar
Pekka Pessi committed
1209 1210 1211 1212 1213
      dialog are affected.
    */
    *return_graceful_terminate_usage = 0;
    return 0;

1214
  case 489: /** @par 489 Bad Event
1215

1216
      In our example scenario, @RFC3265 declares that the
Pekka Pessi's avatar
Pekka Pessi committed
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
      subscription usage in which the NOTIFY is sent is terminated.  The
      invite usage is unaffected and the dialog continues to exist.
      This response is only valid in the context of SUBSCRIBE and
      NOTIFY.  UAC behavior for receiving this response to other methods
      is not specified, but treating it as an unknown 4xx is a
      reasonable practice.
    */
    *return_graceful_terminate_usage = 0;
    return method == sip_method_notify ? terminate_usage : no_effect;

1227
  case 491: /** @par 491 Request Pending
1228

Pekka Pessi's avatar
Pekka Pessi committed
1229 1230 1231
      This response addresses in-dialog request glare.
      Its affect is scoped to the request.  The usage in which the
      request occurs is not affected.  The dialog is only affected by
1232
      the change in its local @CSeq.  No other usages sharing this dialog
Pekka Pessi's avatar
Pekka Pessi committed
1233 1234 1235 1236 1237
      are affected.
    */
    *return_graceful_terminate_usage = 0;
    return 0;

1238
  case 493: /** @par 493 Undecipherable
1239

Pekka Pessi's avatar
Pekka Pessi committed
1240 1241
      This response objects to the request, not the
      usage.  The usage is not affected.  The dialog is only affected by
1242
      a change in its local @CSeq.  No other usages of the dialog are
Pekka Pessi's avatar
Pekka Pessi committed
1243 1244 1245 1246 1247
      affected.
    */
    *return_graceful_terminate_usage = 0;
    return 0;

1248
  case 494: /** @par 494 Security Agreement Required
1249

Pekka Pessi's avatar
Pekka Pessi committed
1250 1251
      This response is objecting to the
      request, not the usage.  The usage is not affected.  The dialog is
1252
      only affected by a change in its local @CSeq.  No other usages of
Pekka Pessi's avatar
Pekka Pessi committed
1253 1254 1255 1256 1257 1258 1259 1260 1261
      the dialog are affected.
    */
    *return_graceful_terminate_usage = 0;
    return 0;
  }

  if (response_code < 600) switch (response_code) {
  case 500: /* 500 and 5xx unrecognized responses */
  default:
1262 1263
    /** @par 500 and 5xx unrecognized responses

Pekka Pessi's avatar
Pekka Pessi committed
1264
      These responses are complaints against the request (transaction),
1265
      not the usage. If the response contains a @RetryAfter header field
Pekka Pessi's avatar
Pekka Pessi committed
1266 1267 1268
      value, the server thinks the condition is temporary and the
      request can be retried after the indicated interval. This usage,
      and any other usages sharing the dialog are unaffected. If the
1269
      response does not contain a @RetryAfter header field value, the UA
Pekka Pessi's avatar
Pekka Pessi committed
1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
      may decide to retry after an interval of its choosing or attempt
      to gracefully terminate the usage. Whether or not to terminate
      other usages depends on the application. If the UA receives a 500
      (or unrecognized 5xx) in response to an attempt to gracefully
      terminate this usage, it can treat this usage as terminated. If
      this is the last usage sharing the dialog, the dialog is also
      terminated.
    */
    /* Do not change *return_graceful_terminate_usage */
    return 0;

1281
  case 501: /** @par 501 Not Implemented
1282

Pekka Pessi's avatar
Pekka Pessi committed
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
      This would be a degenerate response in our
      example scenario since the NOTIFY is being sent as part of an
      established subscribe usage.  In this case, the UA knows the
      condition is unrecoverable and should stop attempting to send
      NOTIFYs on this usage.  (It may or may not destroy the usage.  If
      it remembers the bad behavior, it can reject any refresh
      subscription).  In general, this response may or may not affect
      the usage (a 501 to an unknown method or an INFO will not end an
      invite usage).  It will never affect other usages sharing this
      usage's dialog.
    */
    /* Do not change *return_graceful_terminate_usage */
    return 0;

1297
  case 502: /** @par 502 Bad Gateway
1298

Pekka Pessi's avatar
Pekka Pessi committed
1299
      This response is aberrant mid-dialog. It will only occur if the
1300
      @RecordRoute header field was improperly constructed by the
Pekka Pessi's avatar
Pekka Pessi committed
1301 1302 1303 1304 1305 1306
      proxies involved in setting up the dialog's initial usage. Future
      requests using this dialog state will also fail. The dialog and
      any usages sharing it are destroyed.
    */
    return terminate_dialog;

1307
  case 503: /** @par 503 Service Unavailable
1308

1309
      As per @RFC3263, the logic handling locating SIP servers for
Pekka Pessi's avatar
Pekka Pessi committed
1310 1311 1312 1313 1314 1315 1316
      transactions may handle 503 requests (effectively sequentially
      forking at the endpoint based on DNS results). If this process
      does not yield a better response, a 503 may be returned to the
      transaction user. Like a 500 response, the error is a complaint
      about this transaction, not the usage. Because this response
      occurred in the context of an established usage (hence an existing
      dialog), the route-set has already been formed and any opportunity
1317 1318
      to try alternate servers (as recommended in @RFC3261) has been exhausted
      by the @RFC3263 logic. The response should be handled as described
Pekka Pessi's avatar
Pekka Pessi committed
1319 1320 1321 1322 1323
      for 500 earlier in this memo.
    */
    /* Do not change *return_graceful_terminate_usage */
    return 0;

1324
  case 504: /** @par 504 Server Time-out
1325

Pekka Pessi's avatar
Pekka Pessi committed
1326 1327 1328 1329 1330 1331 1332 1333
      It is not obvious under what circumstances this
      response would be returned to a request in an existing dialog.  If
      it occurs it should have the same affect on the dialog and its
      usages as described for unknown 5xx responses.
    */
    /* Do not change *return_graceful_terminate_usage */
    return 0;

1334
  case 505: /* Version Not Supported */
Pekka Pessi's avatar
Pekka Pessi committed
1335
  case 513: /* Message Too Large */
1336 1337
    /** @par 505 Version Not Supported and 513 Message Too Large

Pekka Pessi's avatar
Pekka Pessi committed
1338 1339
      These responses are objecting to the request, not the usage. The
      usage is not affected. The dialog is only affected by a change in
1340
      its local @CSeq. No other usages of the dialog are affected.
Pekka Pessi's avatar
Pekka Pessi committed
1341 1342 1343 1344
    */
    *return_graceful_terminate_usage = 0;
    return 0;

1345
  case 580: /** @par 580 Precondition Failure
1346

Pekka Pessi's avatar
Pekka Pessi committed
1347 1348
      This response is objecting to the request,
      not the usage.  The usage is not affected.  The dialog is only
1349
      affected by a change in its local @CSeq.  No other usages of the
Pekka Pessi's avatar
Pekka Pessi committed
1350 1351 1352 1353 1354 1355
      dialog are affected.
    */
    *return_graceful_terminate_usage = 0;
    return 0;
  }

1356
  if (response_code < 700) switch (response_code) {
Pekka Pessi's avatar
Pekka Pessi committed
1357 1358
  case 600: /* 600 and 6xx unrecognized responses */
  default:
1359 1360
    /** @par 600 and 6xx unrecognized responses

Pekka Pessi's avatar
Pekka Pessi committed
1361 1362
      Unlike 400 Bad Request, a 600 response code says something about
      the recipient user, not the request that was made. This end user
1363
      is stating an unwillingness to communicate.
Pekka Pessi's avatar
Pekka Pessi committed
1364

1365
      If the response contains a @RetryAfter header field value, the
Pekka Pessi's avatar
Pekka Pessi committed
1366 1367 1368
      user is indicating willingness to communicate later and the
      request can be retried after the indicated interval. This usage,
      and any other usages sharing the dialog are unaffected. If the
1369
      response does not contain a @RetryAfter header field value, the UA
Pekka Pessi's avatar
Pekka Pessi committed
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380
      may decide to retry after an interval of its choosing or attempt
      to gracefully terminate the usage. Whether or not to terminate
      other usages depends on the application. If the UA receives a 600
      (or unrecognized 6xx) in response to an attempt to gracefully
      terminate this usage, it can treat this usage as terminated. If
      this is the last usage sharing the dialog, the dialog is also
      terminated.
    */
    /* Do not change graceful_terminate */
    return 0;

1381
  case 603: /** @par 603 Decline
1382

Pekka Pessi's avatar
Pekka Pessi committed
1383 1384 1385 1386 1387 1388 1389 1390 1391
      This response declines the action indicated by the
      associated request.  It can be used, for example, to decline a
      hold or transfer attempt.  Receiving this response does NOT
      terminate the usage it occurs in.  Other usages sharing the dialog
      are unaffected.
    */
    *return_graceful_terminate_usage = 0;
    return 0;

1392
  case 604: /** @par 604 Does Not Exist Anywhere
1393

Pekka Pessi's avatar
Pekka Pessi committed
1394 1395
      Like 404, this response destroys the
      dialog and all usages sharing it.  The Request-URI that is being
1396
      604ed is the remote target set by the @Contact provided by the
Pekka Pessi's avatar
Pekka Pessi committed
1397 1398 1399 1400 1401
      peer.  Getting this response means something has gone
      fundamentally wrong with the dialog state.
    */
    return terminate_dialog;

1402
  case 606: /** @par 606 Not Acceptable
1403

Pekka Pessi's avatar
Pekka Pessi committed
1404 1405 1406 1407
      This response is objecting to aspects of the
      associated request, not the usage the request appears in.  The
      usage is unaffected.  Any other usages sharing the dialog are
      unaffected.  The only affect on the dialog is the change in the
1408
      local @CSeq.
Pekka Pessi's avatar
Pekka Pessi committed
1409 1410 1411 1412 1413 1414 1415 1416 1417
    */
    *return_graceful_terminate_usage = 0;
    return 0;
  }

  /* Do not change graceful_terminate */

  return 0;
}
1418