sip_basic.c 76.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 26 27
 * 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_basic.c
 * @brief Basic SIP headers.
 *
28 29 30
 * Implementation of header classes for basic SIP headers, like request and
 * status lines, payload, @CallID, @CSeq, @Contact, @ContentLength, @Date,
 * @Expires, @From, @Route, @RecordRoute, @To, and @Via.
Pekka Pessi's avatar
Pekka Pessi committed
31 32 33 34 35 36 37 38
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>.
 *
 * @date  Created: Tue Jun 13 02:57:51 2000 ppessi
 */

#include "config.h"

39 40 41
/* 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
42

43
#include <sofia-sip/su_alloc.h>
44
#include <sofia-sip/su_string.h>
Pekka Pessi's avatar
Pekka Pessi committed
45

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

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

52
#include <sofia-sip/su_uniqueid.h>
Pekka Pessi's avatar
Pekka Pessi committed
53

54 55 56 57 58 59 60
#include <stddef.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <stdarg.h>
#include <limits.h>

Pekka Pessi's avatar
Pekka Pessi committed
61 62 63 64
/* ====================================================================== */

/**@SIP_HEADER sip_request Request Line
 *
65 66
 * The request line is first line in a SIP request message.  Its syntax defined
 * in @RFC3261 as follows:
67
 *
Pekka Pessi's avatar
Pekka Pessi committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
 * @code
 *    Request-Line   =  Method SP Request-URI SP SIP-Version CRLF
 *    Request-URI    =  SIP-URI / SIPS-URI / absoluteURI
 *    absoluteURI    =  scheme ":" ( hier-part / opaque-part )
 *    hier-part      =  ( net-path / abs-path ) [ "?" query ]
 *    net-path       =  "//" authority [ abs-path ]
 *    abs-path       =  "/" path-segments
 *    opaque-part    =  uric-no-slash *uric
 *    uric           =  reserved / unreserved / escaped
 *    uric-no-slash  =  unreserved / escaped / ";" / "?" / ":" / "@"
 *                      / "&" / "=" / "+" / "$" / ","
 *    path-segments  =  segment *( "/" segment )
 *    segment        =  *pchar *( ";" param )
 *    param          =  *pchar
 *    pchar          =  unreserved / escaped /
 *                      ":" / "@" / "&" / "=" / "+" / "$" / ","
 *    scheme         =  ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
 *    authority      =  srvr / reg-name
 *    srvr           =  [ [ userinfo "@" ] hostport ]
 *    reg-name       =  1*( unreserved / escaped / "$" / ","
 *                      / ";" / ":" / "@" / "&" / "=" / "+" )
 *    query          =  *uric
 *    SIP-Version    =  "SIP" "/" 1*DIGIT "." 1*DIGIT
 * @endcode
 *
93
 * The parsed request-line is stored in #sip_request_t structure.
Pekka Pessi's avatar
Pekka Pessi committed
94 95 96 97 98
 */

/**@ingroup sip_request
 * @typedef typedef struct sip_request_s sip_request_t;
 *
99
 * The structure #sip_request_t contains representation of SIP request line.
Pekka Pessi's avatar
Pekka Pessi committed
100
 *
101
 * The #sip_request_t is defined as follows:
Pekka Pessi's avatar
Pekka Pessi committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
 * @code
 * typedef struct sip_request_s {
 *   sip_common_t     rq_common[1];     // Common fragment info
 *   sip_unknown_t   *rq_next;          // Link to next (dummy)
 *   sip_method_t     rq_method;        // Method enum
 *   char const      *rq_method_name;   // Method name
 *   url_t            rq_url[1];        // RequestURI
 *   char const      *rq_version;       // Protocol version
 * } sip_request_t;
 * @endcode
 */

#define sip_request_insert msg_request_insert

static msg_xtra_f sip_request_dup_xtra;
static msg_dup_f sip_request_dup_one;
118
#define sip_request_update NULL
Pekka Pessi's avatar
Pekka Pessi committed
119

120
msg_hclass_t sip_request_class[] =
Pekka Pessi's avatar
Pekka Pessi committed
121 122
SIP_HEADER_CLASS(request, NULL, "", rq_common, single_critical, request);

123
/**Parse @ref sip_request "request line" from a a SIP message. */
124
issize_t sip_request_d(su_home_t *home, sip_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
125
{
126
  sip_request_t *rq = (sip_request_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
127 128 129 130 131 132 133 134 135 136 137
  char *uri, *version;

  if (msg_firstline_d(s, &uri, &version) < 0 || !uri || !version ||
      (rq->rq_method = sip_method_d(&s, &rq->rq_method_name)) < 0 || *s ||
      url_d(rq->rq_url, uri) < 0 ||
      sip_version_d(&version, &rq->rq_version) < 0 || *version)
    return -1;

  return 0;
}

138
/**Encode @ref sip_request "request line" of a a SIP message. */
139
issize_t sip_request_e(char b[], isize_t bsiz, sip_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
140
{
141
  sip_request_t const *rq = (sip_request_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
142 143 144 145 146 147 148

  return snprintf(b, bsiz, "%s " URL_FORMAT_STRING " %s" CRLF,
		  rq->rq_method_name,
		  URL_PRINT_ARGS(rq->rq_url),
		  rq->rq_version);
}

149
isize_t sip_request_dup_xtra(sip_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
150
{
151
  sip_request_t const *rq = (sip_request_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
152

153
  offset += url_xtra(rq->rq_url);
Pekka Pessi's avatar
Pekka Pessi committed
154
  if (!rq->rq_method)
155 156
    offset += MSG_STRING_SIZE(rq->rq_method_name);
  offset += sip_version_xtra(rq->rq_version);
Pekka Pessi's avatar
Pekka Pessi committed
157

158
  return offset;
Pekka Pessi's avatar
Pekka Pessi committed
159 160 161 162
}

/** Duplicate one request header. */
char *sip_request_dup_one(sip_header_t *dst, sip_header_t const *src,
163
			  char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
164
{
165 166
  sip_request_t *rq = (sip_request_t *)dst;
  sip_request_t const *o = (sip_request_t *)src;
Pekka Pessi's avatar
Pekka Pessi committed
167 168 169 170 171
  char *end = b + xtra;

  URL_DUP(b, end, rq->rq_url, o->rq_url);

  if (!(rq->rq_method = o->rq_method))
Pekka Pessi's avatar
Pekka Pessi committed
172
    MSG_STRING_DUP(b, rq->rq_method_name, o->rq_method_name);
Pekka Pessi's avatar
Pekka Pessi committed
173 174 175 176 177 178 179 180 181 182 183
  else
    rq->rq_method_name = o->rq_method_name;
  sip_version_dup(&b, &rq->rq_version, o->rq_version);

  assert(b <= end);

  return b;
}

/**@ingroup sip_request
 *
184
 * Create a @ref sip_request "request line" object.
Pekka Pessi's avatar
Pekka Pessi committed
185
 *
186
 * Create a request line object with
Pekka Pessi's avatar
Pekka Pessi committed
187 188 189 190
 * method enum @a method, method name @a name, request URI @a uri, and
 * protocol version @a version.  The memory for the header object is
 * allocated from the memory home @a home.
 *
191
 * @param home     memory home used to allocate #sip_request_t object
Pekka Pessi's avatar
Pekka Pessi committed
192 193 194 195 196 197 198 199 200 201 202 203
 * @param method   method enum
 * @param name     method name (required if method is not well-known)
 * @param uri      request URI
 * @param version  version string (defaults to "SIP/2.0" if NULL)
 *
 * @par Example
 * The following code fragment creates an OPTIONS request object:
 * @code
 *   sip_request_t *rq;
 *   rq = sip_request_create(home, SIP_METHOD_OPTIONS, requestURI, NULL);
 * @endcode

204
 * @note
Pekka Pessi's avatar
Pekka Pessi committed
205
 * If you provide an non-NULL @a version string, it is not copied. The
206
 * version string @b MUST remain constant.
Pekka Pessi's avatar
Pekka Pessi committed
207 208 209 210 211 212
 */
sip_request_t *sip_request_create(su_home_t *home,
				  sip_method_t method, char const *name,
				  url_string_t const *uri,
				  char const *version)
{
213
  size_t xtra;
Pekka Pessi's avatar
Pekka Pessi committed
214 215 216 217 218 219 220 221 222 223 224 225 226
  sip_request_t *rq;

  if (method)
    name = sip_method_name(method, name);

  if (!name)
    return NULL;

  if (!method)
    method = sip_method_code(name);

  xtra = url_xtra(uri->us_url) + (method ? 0 : strlen(name) + 1);

227
  rq = (sip_request_t *)sip_header_alloc(home, sip_request_class, xtra);
Pekka Pessi's avatar
Pekka Pessi committed
228 229 230 231 232 233

  if (rq) {
    char *b = (char *)(rq + 1), *end = b + xtra;

    rq->rq_method      = method;
    rq->rq_method_name = name;
234
    if (!method)
Pekka Pessi's avatar
Pekka Pessi committed
235
      MSG_STRING_DUP(b, rq->rq_method_name, name);
Pekka Pessi's avatar
Pekka Pessi committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

    URL_DUP(b, end, rq->rq_url, uri->us_url);

    rq->rq_version = version ? version : SIP_VERSION_CURRENT;
    assert(b == end);
  }

  return rq;
}

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

/**@SIP_HEADER sip_status Status Line
 *
 * The status line is first line in a response message.  It is defined in
251
 * @RFC3261 as follows:
252
 *
Pekka Pessi's avatar
Pekka Pessi committed
253 254 255 256 257 258 259 260 261 262 263 264 265 266
 * @code
 *    Status-Line     =  SIP-Version SP Status-Code SP Reason-Phrase CRLF
 *    Status-Code     =  Informational
 *                   /   Redirection
 *                   /   Success
 *                   /   Client-Error
 *                   /   Server-Error
 *                   /   Global-Failure
 *                   /   extension-code
 *    extension-code  =  3DIGIT
 *    Reason-Phrase   =  *(reserved / unreserved / escaped
 *                       / UTF8-NONASCII / UTF8-CONT / SP / HTAB)
 * @endcode
 *
267
 * The parsed status line is stored in #sip_status_t structure.
Pekka Pessi's avatar
Pekka Pessi committed
268 269 270 271 272
 */

/**@ingroup sip_status
 * @typedef typedef struct sip_status_s sip_status_t;
 *
273
 * The structure #sip_status_t contains representation of SIP
274
 * @ref sip_status "status line".
Pekka Pessi's avatar
Pekka Pessi committed
275
 *
276
 * The #sip_status_t is defined as follows:
Pekka Pessi's avatar
Pekka Pessi committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
 * @code
 * typedef struct sip_status_s {
 *   sip_common_t   st_common[1];       // Common fragment info
 *   sip_unknown_t *st_next;            // Link to next (dummy)
 *   char const    *st_version;         // Protocol version
 *   int            st_status;          // Status code
 *   char const    *st_phrase;          // Status phrase
 * } sip_status_t;
 * @endcode
 */


static msg_xtra_f sip_status_dup_xtra;
static msg_dup_f sip_status_dup_one;

#define sip_status_insert msg_status_insert
293
#define sip_status_update NULL
Pekka Pessi's avatar
Pekka Pessi committed
294

295
msg_hclass_t sip_status_class[] =
Pekka Pessi's avatar
Pekka Pessi committed
296 297 298
SIP_HEADER_CLASS(status, NULL, "", st_common, single_critical, status);

/** Parse status line */
299
issize_t sip_status_d(su_home_t *home, sip_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
300
{
301
  sip_status_t *st = (sip_status_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315
  char *status, *phrase;
  unsigned long code;

  if (msg_firstline_d(s, &status, &phrase) < 0 ||
      sip_version_d(&s, &st->st_version) < 0 || *s ||
      (code = strtoul(status, &status, 10)) >= INT_MAX || *status)
    return -1;

  st->st_status = code;
  st->st_phrase = phrase;

  return 0;
}

316
issize_t sip_status_e(char b[], isize_t bsiz, sip_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
317
{
318
  sip_status_t const *st = (sip_status_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
319 320 321 322
  int status;

  assert(sip_is_status(h));

323
  status = st->st_status;
Pekka Pessi's avatar
Pekka Pessi committed
324 325 326 327 328

  if (status > 999 || status < 100)
    status = 0;

  return snprintf(b, bsiz, "%s %03u %s" CRLF,
329
		  st->st_version,
Pekka Pessi's avatar
Pekka Pessi committed
330
		  status,
331
		  st->st_phrase);
Pekka Pessi's avatar
Pekka Pessi committed
332 333
}

334
/** Extra size of a #sip_status_t object. */
335
isize_t sip_status_dup_xtra(sip_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
336
{
337
  sip_status_t const *st = (sip_status_t *)h;
338 339 340
  offset += sip_version_xtra(st->st_version);
  offset += MSG_STRING_SIZE(st->st_phrase);
  return offset;
Pekka Pessi's avatar
Pekka Pessi committed
341 342 343 344
}

/** Duplicate one status header. */
char *sip_status_dup_one(sip_header_t *dst, sip_header_t const *src,
345
			 char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
346
{
347 348
  sip_status_t *st = (sip_status_t *)dst;
  sip_status_t const *o = (sip_status_t *)src;
Pekka Pessi's avatar
Pekka Pessi committed
349 350 351 352
  char *end = b + xtra;

  sip_version_dup(&b, &st->st_version, o->st_version);
  st->st_status = o->st_status;
Pekka Pessi's avatar
Pekka Pessi committed
353
  MSG_STRING_DUP(b, st->st_phrase, o->st_phrase);
Pekka Pessi's avatar
Pekka Pessi committed
354

355
  assert(b <= end); (void)end;
Pekka Pessi's avatar
Pekka Pessi committed
356 357 358 359 360 361

  return b;
}

/**@ingroup sip_status
 *
362
 * Create a @ref sip_status "status line" object.
Pekka Pessi's avatar
Pekka Pessi committed
363
 *
364
 * @param home    memory home used to allocate #sip_status_t object
Pekka Pessi's avatar
Pekka Pessi committed
365
 * @param status  status code (in range 100 - 699)
Pekka Pessi's avatar
Pekka Pessi committed
366
 * @param phrase  status phrase (may be NULL)
Pekka Pessi's avatar
Pekka Pessi committed
367 368
 * @param version version string (defaults to "SIP/2.0" if NULL)
 *
369
 * @note
Pekka Pessi's avatar
Pekka Pessi committed
370 371 372 373
 * If you provide an non-NULL @a version string, it is not copied. The
 * string @b MUST remain constant.
 *
 * @return
374 375
 * A pointer to newly created @ref sip_status "status line"
 * structure when successful, or NULL upon an error.
Pekka Pessi's avatar
Pekka Pessi committed
376 377 378 379 380 381 382 383
 */
sip_status_t *sip_status_create(su_home_t *home,
				unsigned status,
				char const *phrase,
				char const *version)
{
  sip_status_t *st;

Pekka Pessi's avatar
Pekka Pessi committed
384
  if (status < 100 || status > 699)
Pekka Pessi's avatar
Pekka Pessi committed
385 386
    return NULL;

Pekka Pessi's avatar
Pekka Pessi committed
387 388 389
  if (phrase == NULL && (phrase = sip_status_phrase(status)) == NULL)
    phrase = "";

390
  if ((st = (sip_status_t *)sip_header_alloc(home, sip_status_class, 0))) {
Pekka Pessi's avatar
Pekka Pessi committed
391 392 393 394 395 396 397 398 399 400
    st->st_status = status;
    st->st_phrase = phrase;
    st->st_version = version ? version : SIP_VERSION_CURRENT;
  }

  return st;
}

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

401
/**@SIP_HEADER sip_payload Message Body
Pekka Pessi's avatar
Pekka Pessi committed
402
 *
403 404 405 406 407 408
 * The payload structure contains the optional message body. The message
 * body stored in the #sip_payload_t structure has no internal structure,
 * but it is accessed as a byte array. Use @ref sdp_parser "SDP parser" to
 * parse SDP content, for instance.
 *
 * The message body is stored in a #sip_payload_t structure.
Pekka Pessi's avatar
Pekka Pessi committed
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
 */

/**@ingroup sip_payload
 * @typedef typedef struct sip_payload_s sip_payload_t;
 *
 * The structure #sip_payload_t contains representation of SIP message payload.
 *
 * The #sip_payload_t is defined as follows:
 * @code
 * typedef struct sip_payload_s {
 *   msg_common_t    pl_common[1];      // Common fragment info
 *   msg_header_t   *pl_next;           // Next payload (if multipart message)
 *   char           *pl_data;           // Data - may contain NUL
 *   unsigned        pl_len;            // Length of message payload
 * } sip_payload_t;
 * @endcode
 */

#define sip_payload_d 	     msg_payload_d
#define sip_payload_e 	     msg_payload_e
429
#define sip_payload_dup_xtra msg_payload_dup_xtra
Pekka Pessi's avatar
Pekka Pessi committed
430
#define sip_payload_dup_one  msg_payload_dup_one
431
#define sip_payload_update   NULL
Pekka Pessi's avatar
Pekka Pessi committed
432

433
msg_hclass_t sip_payload_class[] =
Pekka Pessi's avatar
Pekka Pessi committed
434 435 436 437
SIP_HEADER_CLASS(payload, NULL, "", pl_common, single, payload);

/**@ingroup sip_payload
 *
438
 * Create a @ref sip_payload "SIP payload" structure.
Pekka Pessi's avatar
Pekka Pessi committed
439
 *
440
 * Create a new SIP payload structure. it
Pekka Pessi's avatar
Pekka Pessi committed
441 442 443 444
 * copies the given data to the the payload data, and NUL terminates it (it
 * allocates one extra byte for NUL).  If a NULL pointer is given as @a data,
 * sip_payload_create() allocates and zeroes a data buffer of @a len bytes.
 *
445 446
 * @param home memory home
 * @param data payload data
Pekka Pessi's avatar
Pekka Pessi committed
447 448
 * @param len  payload length
 *
449
 * @return A pointer to newly created
Pekka Pessi's avatar
Pekka Pessi committed
450
 * payload structure, if successful, and NULL upon an error.
451
 */
452
sip_payload_t *sip_payload_create(su_home_t *home, void const *data, isize_t len)
Pekka Pessi's avatar
Pekka Pessi committed
453 454 455
{
  msg_hclass_t *hc = sip_payload_class;
  sip_header_t *h = sip_header_alloc(home, hc, len + 1);
456
  sip_payload_t *pl = (sip_payload_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

  if (pl) {
    char *b = sip_header_data(h);
    if (data) {
      memcpy(b, data, len);
      b[len] = 0;
    }
    else {
      memset(b, 0, len + 1);
    }

    h->sh_data = pl->pl_data = b;
    h->sh_len = pl->pl_len = len;
  }

  return pl;
}

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

/**@SIP_HEADER sip_separator Separator Line
 *
 * An empty line separates message headers from the message body (payload).
 * In order to avoid modifying messages with integrity protection, the
 * separator line has its own header structure which is included in the
482 483 484
 * #sip_t structure.
 *
 * The parsed separator line is stored in #sip_separator_t structure.
Pekka Pessi's avatar
Pekka Pessi committed
485 486 487 488 489
 */

/**@ingroup sip_separator
 * @typedef typedef struct sip_separator_s sip_separator_t;
 *
490
 * The structure #sip_separator_t contains representation of separator line
Pekka Pessi's avatar
Pekka Pessi committed
491 492
 * between message headers and body.
 *
493
 * The #sip_separator_t is defined as follows:
Pekka Pessi's avatar
Pekka Pessi committed
494 495 496 497 498 499 500 501 502 503 504 505 506
 * @code
 * typedef struct sip_separator_s {
 *   msg_common_t    sep_common[1];     // Common fragment info
 *   msg_header_t   *sep_next;          // Pointer to next header
 *   char            sep_data[4];       // NUL-terminated separator
 * } sip_separator_t;
 * @endcode
 */

#define sip_separator_d msg_separator_d
#define sip_separator_e msg_separator_e
#define sip_separator_insert msg_separator_insert

507
msg_hclass_t sip_separator_class[] =
Pekka Pessi's avatar
Pekka Pessi committed
508 509 510
SIP_HEADER_CLASS(separator, NULL, "", sep_common, single, any);

/**@ingroup sip_separator
511
 *
512
 * Create a @ref sip_separator "SIP separator line" structure.
Pekka Pessi's avatar
Pekka Pessi committed
513 514 515
 */
sip_separator_t *sip_separator_create(su_home_t *home)
{
516 517
  sip_separator_t *sep = (sip_separator_t *)
    sip_header_alloc(home, sip_separator_class, 0);
Pekka Pessi's avatar
Pekka Pessi committed
518 519 520 521 522 523 524 525 526 527 528

  if (sep)
    strcpy(sep->sep_data, CRLF);

  return sep;
}

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

/**@SIP_HEADER sip_unknown Unknown Headers
 *
529 530 531
 * The unknown headers are handled with #sip_unknown_t structure. The
 * unknown header name is stored in @a un_name field and the header field
 * following the colon is stored in @a un_value field.
532
 *
Pekka Pessi's avatar
Pekka Pessi committed
533 534 535 536 537 538
 * @note It is possible to speed up parsing process by creating a parser
 * which does understand only a minimum number of headers. If such a parser
 * is used, some well-known headers are regarded as unknown and put into
 * list of unknown headers.
 */

539 540 541 542 543 544 545 546
/**@ingroup sip_unknown
 * @typedef typedef struct sip_unknown_s sip_unknown_t;
 *
 * The structure #sip_unknown_t contains representation of unknown headers.
 *
 * The #sip_unknown_t is defined as follows:
 * @code
 * typedef struct msg_unknown_s {
547 548 549 550
 *   msg_common_t    un_common[1];  // Common fragment info
 *   msg_unknown_t  *un_next;       // Link to next unknown header
 *   char const     *un_name;       // Header name
 *   char const     *un_value;      // Header field value
551 552 553 554
 * } sip_unknown_t;
 * @endcode
 */

555
#define sip_unknown_dup_xtra msg_unknown_dup_xtra
Pekka Pessi's avatar
Pekka Pessi committed
556
#define sip_unknown_dup_one  msg_unknown_dup_one
557
#define sip_unknown_update NULL
Pekka Pessi's avatar
Pekka Pessi committed
558

559
msg_hclass_t sip_unknown_class[] =
Pekka Pessi's avatar
Pekka Pessi committed
560 561
SIP_HEADER_CLASS(unknown, "", "", un_common, append, unknown);

562
issize_t sip_unknown_d(su_home_t *home, sip_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
563 564 565 566
{
  return msg_unknown_d(home, h, s, slen);
}

567
issize_t sip_unknown_e(char b[], isize_t bsiz, sip_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
568 569 570 571 572 573 574 575 576
{
  return msg_unknown_e(b, bsiz, h, flags);
}

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

/**@SIP_HEADER sip_error Erroneous Headers
 *
 * The erroneous headers are stored in #sip_error_t structure.
577
 *
578
 * @note Other headers (like duplicate @ContentLength headers) may be put
Pekka Pessi's avatar
Pekka Pessi committed
579 580 581 582 583 584 585
 * into the list of erroneous headers (@c sip->sip_error). If the list of
 * erroneous headers is processed, the header type must be validated first
 * by calling sip_is_error() (or by other relevant tests).
 */

/**@ingroup sip_error
 * @typedef typedef msg_error_t sip_error_t;
586 587 588 589 590
 * The structure #sip_error_t contains representation of error headers.
 *
 * The #sip_error_t is defined as follows:
 * @code
 * typedef struct msg_error_s {
591 592
 *   msg_common_t    er_common[1];  // Common fragment info
 *   msg_error_t    *er_next;       // Link to next header
593 594 595
 *   char const     *er_name;       // Name of bad header (if any)
 * } sip_error_t;
 * @endcode
Pekka Pessi's avatar
Pekka Pessi committed
596 597 598 599 600
 */

msg_hclass_t sip_error_class[] =
SIP_HEADER_CLASS(error, NULL, "", er_common, append, any);

601
issize_t sip_error_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
602 603 604 605
{
  return 0;
}

606
issize_t sip_error_e(char b[], isize_t bsiz, msg_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
607 608 609 610 611 612 613
{
  /* There is no way to encode an erroneous header */
  return 0;
}

/* ====================================================================== */
/*
614
 * addr           = ("To" | "t" | "From" | "f") ":"
Pekka Pessi's avatar
Pekka Pessi committed
615 616 617 618 619 620
 *                  ( name-addr | addr-spec ) *( ";" addr-params )
 * name-addr      = [ display-name ] "<" addr-spec ">"
 * addr-spec      = SIP-URL | URI
 * display-name   = *token | quoted-string
 */

Pekka Pessi's avatar
Pekka Pessi committed
621 622
/**Parse @e name-addr.
 *
623 624
 * Parses <i>( name-addr | addr-spec )</i> construct on @Contact, @From,
 * @To, and other compatible headers. It splits the argument string in
Pekka Pessi's avatar
Pekka Pessi committed
625 626 627 628 629 630 631 632 633
 * four parts:
 *
 * @par
 * @e [display-name] @e addr-spec @e [parameters] @e [comment] @e [ss]
 *
 * @param home           pointer to memory home
 * @param inout_s        pointer to pointer to string to be parsed
 * @param return_display value-result parameter for @e display-name
 * @param return_url     value-result parameter for @e addr-spec
634
 * @param return_params  value-result paramater for @e parameters
Pekka Pessi's avatar
Pekka Pessi committed
635 636 637 638
 * @param return_comment value-result parameter for @e comment
 *
 * @note After succesful call to the function @c sip_name_addr_d(), *ss
 * contains pointer to the first character not beloging to @e name-addr,
639
 * most probably a comma. If that character is a separator, the last parameter
Pekka Pessi's avatar
Pekka Pessi committed
640
 * may not be NUL (zero) terminated. So, after examining value of @a **ss,
641
 * the calling function @b MUST set it to NUL.
Pekka Pessi's avatar
Pekka Pessi committed
642
 *
643 644
 * @retval 0 if successful
 * @retval -1 upon an error
Pekka Pessi's avatar
Pekka Pessi committed
645
 *
646
 * @sa @From, @To, @Contact
Pekka Pessi's avatar
Pekka Pessi committed
647
 */
648 649 650 651 652 653
issize_t sip_name_addr_d(su_home_t *home,
			 char **inout_s,
			 char const **return_display,
			 url_t *return_url,
			 msg_param_t const **return_params,
			 char const **return_comment)
Pekka Pessi's avatar
Pekka Pessi committed
654 655 656
{
  char c, *s = *inout_s;
  char *display = NULL, *addr_spec = NULL;
657
  size_t n;
Pekka Pessi's avatar
Pekka Pessi committed
658

659 660
  if (*s == '\0')		/* Empty string */
    return -1;
661

Pekka Pessi's avatar
Pekka Pessi committed
662 663 664 665 666 667 668 669 670 671
  if (return_display && *s == '"') {
    /* Quoted string */
    if (msg_quoted_d(&s, &display) == -1)
      return -1;

    /* Now, we should have a '<' in s[0] */
    if (s[0] != '<')
      return -1;
    s++[0] = '\0';		/* NUL terminate quoted string... */
    n = strcspn(s, ">");
672
    addr_spec = s; s += n;
Pekka Pessi's avatar
Pekka Pessi committed
673
    if (*s) *s++ = '\0'; else return -1;
674
  }
Pekka Pessi's avatar
Pekka Pessi committed
675
  else {
676
    if (return_display)
Pekka Pessi's avatar
Pekka Pessi committed
677 678 679 680 681 682
      n = span_token_lws(s);
    else
      n = 0;

    if (s[n] == '<') {
      /* OK, we got a display name */
683
      display = s; s += n + 1;
Pekka Pessi's avatar
Pekka Pessi committed
684 685 686 687 688
      /* NUL terminate display name */
      while (n > 0 && IS_LWS(display[n - 1]))
	n--;
      if (n > 0)
	display[n] = '\0';
689
      else
Pekka Pessi's avatar
Pekka Pessi committed
690 691 692 693 694 695 696 697 698
	display = "";

      n = strcspn(s, ">");
      addr_spec = s; s += n; if (*s) *s++ = '\0'; else return -1;
    }
    else {
      /* addr-spec only */
      addr_spec = s;
      /**@sa
699
       * Discussion about comma, semicolon and question mark in
Pekka Pessi's avatar
Pekka Pessi committed
700 701 702
       * @RFC3261 section 20.10.
       */
      if (return_params)
703
	n = strcspn(s, " \t,;?");	/* DO NOT accept ,;? in URL */
Pekka Pessi's avatar
Pekka Pessi committed
704 705 706 707 708 709 710 711 712 713 714 715 716
      else
	/* P-Asserted-Identity and friends */
	n = strcspn(s, " ,"); /* DO NOT accept , in URL */
      s += n;
      if (IS_LWS(*s))
	*s++ = '\0';
    }
  }

  skip_lws(&s);

  if (return_display)
    *return_display = display;
717 718

  /* Now, url may still not be NUL terminated, e.g., if
Pekka Pessi's avatar
Pekka Pessi committed
719 720 721
   * it is like "Contact: url:foo,sip:bar,sip:zunk"
   */
  c = *s; *s = '\0';		/* terminate temporarily */
722 723 724
  /* Do not accept an empty URL */
  if (addr_spec[0] == '\0')
    return -1;
Pekka Pessi's avatar
Pekka Pessi committed
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743
  if (url_d(return_url, addr_spec) == -1)
    return -1;
  *s = c;			/* return terminator */

  *inout_s = s;

  if (c == ';' && return_params)
    if (msg_params_d(home, inout_s, return_params) == -1)
      return -1;

  if (**inout_s == '(' && return_comment)
    if (msg_comment_d(inout_s, return_comment) == -1)
      return -1;

  return 0;
}

/**Encode @e name-addr and parameter list.
 *
744 745
 * Encodes @e name-addr headers, like @From, @To, @CallInfo, @ErrorInfo,
 * @Route, and @RecordRoute.
Pekka Pessi's avatar
Pekka Pessi committed
746 747 748 749 750 751 752 753 754 755
 *
 * @param b        buffer to store the encoding result
 * @param bsiz     size of the buffer @a b
 * @param flags    encoding flags
 * @param display  display name encoded before the @a url (may be NULL)
 * @param brackets if true, use always brackets around @a url
 * @param url      pointer to URL structure
 * @param params   pointer to parameter list (may be NULL)
 * @param comment  comment string encoded after others (may be NULL)
 *
756
 * @return
Pekka Pessi's avatar
Pekka Pessi committed
757 758 759 760 761 762 763
 * Returns number of characters in encoding, excluding the
 * final NUL.
 *
 * @note
 * The encoding result may be incomplete if the buffer size is not large
 * enough to store the whole encoding result.
 */
764 765 766
issize_t sip_name_addr_e(char b[], isize_t bsiz,
			 int flags,
			 char const *display,
767
			 int brackets, url_t const url[],
768
			 msg_param_t const params[],
769
			 char const *comment)
Pekka Pessi's avatar
Pekka Pessi committed
770 771 772 773 774
{
  int const compact = MSG_IS_COMPACT(flags);
  char const *u;
  char *b0 = b, *end = b + bsiz;

775 776
  brackets = brackets || display ||
    (url && (url->url_params ||
Pekka Pessi's avatar
Pekka Pessi committed
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
	     url->url_headers ||
	     ((u = url->url_user) && u[strcspn(u, ";,?")]) ||
	     ((u = url->url_password) && u[strcspn(u, ",")])));

  if (display && display[0]) {
    MSG_STRING_E(b, end, display);
    if (!compact) MSG_CHAR_E(b, end, ' ');
  }
  if (url) {
    if (brackets) MSG_CHAR_E(b, end, '<');
    URL_E(b, end, url);
    if (brackets) MSG_CHAR_E(b, end, '>');
  }

  MSG_PARAMS_E(b, end, params, flags);

  if (comment) {
    if (!compact) MSG_CHAR_E(b, end, ' ');
    MSG_CHAR_E(b, end, '(');
    MSG_STRING_E(b, end, comment);
    MSG_CHAR_E(b, end, ')');
  }

  MSG_TERM_E(b, end);
801

Pekka Pessi's avatar
Pekka Pessi committed
802 803 804
  return b - b0;
}

805 806 807 808 809 810 811 812 813
/** Calculate the extra size needed to duplicate a name-addr-params construct.
 *
 * @param display  display name (may be NULL)
 * @param addr     pointer to URL structure
 * @param params   pointer to parameter list (may be NULL)
 * @param offset   base offset
 *
 * @retval Size of duplicated name-addr-params construct, including base offset.
 *
Pekka Pessi's avatar
Pekka Pessi committed
814
 * @NEW_1_12_7.
815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
 */
isize_t sip_name_addr_xtra(char const *display, url_t const *addr,
			   msg_param_t const params[],
			   isize_t offset)
{
  SIP_PARAMS_SIZE(offset, params);
  offset += SIP_STRING_SIZE(display);
  offset += url_xtra(addr);
  return offset;
}

/**Duplicate a name-addr-params construct.
 *
 * @param d_display value-result parameter for copied @e name (may be NULL)
 * @param display   display name (may be NULL)
 * @param d_addr    value-result parameter for copied @e address
 * @param addr      pointer to URL address structure
 * @param d_params value-result parameter for copied parameters (may be NULL)
 * @param params   pointer to parameter list (may be NULL)
 * @param b        pointer to memory pool
 * @param xtra     size of the memory pool
 *
 * @retval End of the memory area used.
 *
Pekka Pessi's avatar
Pekka Pessi committed
839
 * @NEW_1_12_7.
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
 */
char *sip_name_addr_dup(char const **d_display, char const *display,
			url_t *d_addr, url_t const *addr,
			msg_param_t const **d_params, msg_param_t const params[],
			char *b, isize_t xtra)
{
  char *end = b + xtra;

  if (d_params)
    b = msg_params_dup(d_params, params, b, xtra);

  URL_DUP(b, end, d_addr, addr);

  if (d_display)
    MSG_STRING_DUP(b, *d_display, display);

  assert(b <= end);

  return b;
}


862
/** Parse @To or @From headers */
863 864 865 866
static issize_t sip_addr_d(su_home_t *home,
			   sip_header_t *h,
			   char *s,
			   isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
867
{
868
  sip_addr_t *a = (sip_addr_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
869
  char const *comment = NULL;
870 871 872 873
  if (sip_name_addr_d(home,
		      &s,
		      &a->a_display,
		      a->a_url,
Pekka Pessi's avatar
Pekka Pessi committed
874
		      &a->a_params,
875
		      &comment) == -1
Pekka Pessi's avatar
Pekka Pessi committed
876 877 878
      || *s /* XXX - something extra? */)
    return -1;

Pekka Pessi's avatar
Pekka Pessi committed
879
  a->a_tag = msg_params_find(a->a_params, "tag=");
880

Pekka Pessi's avatar
Pekka Pessi committed
881 882 883
  return 0;
}

884
static int sip_addr_e(char b[], isize_t bsiz, sip_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
885
{
886
  sip_addr_t const *a = (sip_addr_t const *)h;
Pekka Pessi's avatar
Pekka Pessi committed
887

Pekka Pessi's avatar
Pekka Pessi committed
888
  return sip_name_addr_e(b, bsiz,
Pekka Pessi's avatar
Pekka Pessi committed
889
			 flags,
890
			 a->a_display,
Pekka Pessi's avatar
Pekka Pessi committed
891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907
			 MSG_IS_CANONIC(flags), a->a_url,
			 a->a_params,
			 NULL);
}

/**
 * Extra dup size of a sip_addr_t object.
 *
 * This function calculates extra size required when duplicating a
 * sip_addr_t object.
 *
 * @param a pointer to a sip_addr_t object
 *
 * @return
 *   Size of strings related to sip_addr_t object.
 */
static
908
isize_t sip_addr_dup_xtra(sip_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
909
{
910
  sip_addr_t const *a = (sip_addr_t const *)h;
Pekka Pessi's avatar
Pekka Pessi committed
911

912
  return sip_name_addr_xtra(a->a_display,
913 914 915
			    a->a_url,
			    a->a_params,
			    offset);
Pekka Pessi's avatar
Pekka Pessi committed
916 917 918 919 920 921
}

/**@internal
 * Duplicate one sip_addr_t object.
 */
static char *sip_addr_dup_one(sip_header_t *dst, sip_header_t const *src,
922
			      char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
923
{
924 925
  sip_addr_t *a = (sip_addr_t *)dst;
  sip_addr_t const *o = (sip_addr_t *)src;
Pekka Pessi's avatar
Pekka Pessi committed
926

927 928 929 930
  return sip_name_addr_dup(&a->a_display, o->a_display,
			   a->a_url, o->a_url,
			   &a->a_params, o->a_params,
			   b, xtra);
Pekka Pessi's avatar
Pekka Pessi committed
931 932
}

933 934
/** Update parameters in sip_addr_t object */
static int sip_addr_update(msg_common_t *h,
935
			   char const *name, isize_t namelen,
936 937 938 939 940 941 942
			   char const *value)
{
  sip_addr_t *a = (sip_addr_t *)h;

  if (name == NULL) {
    a->a_tag = NULL;
  }
943
  else if (namelen == strlen("tag") && su_casenmatch(name, "tag", namelen)) {
944 945 946 947 948 949
    a->a_tag = value;
  }

  return 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
950 951 952 953
/** Create an address header object from URL */
static sip_addr_t *
sip_addr_make_url(su_home_t *home, msg_hclass_t *hc, url_string_t const *us)
{
954
  size_t n;
Pekka Pessi's avatar
Pekka Pessi committed
955 956 957
  sip_header_t *h;

  n = url_xtra(us->us_url);
958
  h = sip_header_alloc(home, hc, n);
Pekka Pessi's avatar
Pekka Pessi committed
959 960

  if (h) {
961
    sip_addr_t *a = (sip_to_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
962 963
    char *s2 = sip_header_data(h);

964
    if ((size_t)url_dup(s2, n, a->a_url, us->us_url) == n)
Pekka Pessi's avatar
Pekka Pessi committed
965 966 967 968 969 970 971 972
      return a;

    su_free(home, h);
  }

  return NULL;
}

973
/** Add a tag to address structure. */
Pekka Pessi's avatar
Pekka Pessi committed
974 975 976 977
static
int sip_addr_tag(su_home_t *home, sip_addr_t *a, char const *tag)
{
  if (a && tag) {
Pekka Pessi's avatar
Pekka Pessi committed
978
    msg_param_t value = strchr(tag, '=');
Pekka Pessi's avatar
Pekka Pessi committed
979 980 981 982 983 984 985

    if (value)
      value = strchr(value, '=') + 1;
    else
      value = tag;

    if (a->a_tag) {
986
      if (su_casematch(a->a_tag, value))
Pekka Pessi's avatar
Pekka Pessi committed
987 988 989 990 991 992 993 994 995 996 997
	return 0;
      else
	return -1;
    }

    if (tag == value)
      tag = su_sprintf(home, "tag=%s", tag);
    else
      tag = su_strdup(home, tag);

    if (tag)
998 999
      if (msg_header_replace_param(home, a->a_common, tag) >= 0)
	return 0;
Pekka Pessi's avatar
Pekka Pessi committed
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
  }

  return -1;
}

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

/**@SIP_HEADER sip_call_id Call-ID Header
 *
 * The @b Call-ID header uniquely identifies a particular invitation or all
1010
 * registrations of a particular client. It is defined in @RFC3261 as
Pekka Pessi's avatar
Pekka Pessi committed
1011
 * follows:
1012
 *
Pekka Pessi's avatar
Pekka Pessi committed
1013 1014 1015 1016 1017 1018 1019 1020
 * @code
 *    Call-ID  =  ( "Call-ID" / "i" ) HCOLON callid
 *    callid   =  word [ "@" word ]
 *    word        =  1*(alphanum / "-" / "." / "!" / "%" / "*" /
 *                   "_" / "+" / "`" / "'" / "~" / "(" / ")" / "<" / ">" /
 *                   ":" / "\" / DQUOTE / "/" / "[" / "]" / "?" / "{" / "}" )
 * @endcode
 *
1021
 * The parsed Call-ID Header is stored in #sip_call_id_t structure.
Pekka Pessi's avatar
Pekka Pessi committed
1022 1023 1024 1025 1026
 */

/**@ingroup sip_call_id
 * @typedef typedef struct sip_call_id_s sip_call_id_t;
 *
1027
 * The structure #sip_call_id_t contains representation of SIP @CallID
Pekka Pessi's avatar
Pekka Pessi committed
1028 1029 1030 1031 1032 1033 1034 1035
 * header.
 *
 * The #sip_call_id_t is defined as follows:
 * @code
 * typedef struct sip_call_id_s {
 *   sip_common_t   i_common[1];        // Common fragment info
 *   sip_call_id_t *i_next;             // Link to next (dummy)
 *   char const    *i_id;               // ID value
Pekka Pessi's avatar
Pekka Pessi committed
1036
 *   uint32_t       i_hash;             // Hash value (always nonzero)
Pekka Pessi's avatar
Pekka Pessi committed
1037 1038 1039 1040 1041 1042
 * } sip_call_id_t;
 * @endcode
 */

static msg_xtra_f sip_call_id_dup_xtra;
static msg_dup_f sip_call_id_dup_one;
1043
#define sip_call_id_update NULL
Pekka Pessi's avatar
Pekka Pessi committed
1044

1045
msg_hclass_t sip_call_id_class[] =
Pekka Pessi's avatar
Pekka Pessi committed
1046 1047
SIP_HEADER_CLASS(call_id, "Call-ID", "i", i_common, single, call_id);

1048 1049
issize_t sip_call_id_d(su_home_t *home,
		       sip_header_t *h,
1050
		       char *s,
1051
		       isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1052
{
1053
  sip_call_id_t *i = (sip_call_id_t *)h;
1054

1055
  i->i_id = s; /* XXX - why not sip_word_at_word_d(&s); */
Pekka Pessi's avatar
Pekka Pessi committed
1056 1057 1058 1059 1060 1061
  i->i_hash = msg_hash_string(s);

  return 0;
}


1062
issize_t sip_call_id_e(char b[], isize_t bsiz, sip_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
1063
{
1064 1065
  sip_call_id_t const *i = (sip_call_id_t *)h;
  size_t n = strlen(i->i_id);
Pekka Pessi's avatar
Pekka Pessi committed
1066 1067

  if (bsiz > n)
1068
    strcpy(b, i->i_id);
Pekka Pessi's avatar
Pekka Pessi committed
1069

1070
  return (issize_t)n;
Pekka Pessi's avatar
Pekka Pessi committed
1071 1072
}

1073
/** Extra size of a #sip_call_id_t object. */
1074
isize_t sip_call_id_dup_xtra(sip_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
1075
{
1076
  sip_call_id_t const *i = (sip_call_id_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1077
  return offset + MSG_STRING_SIZE(i->i_id);
Pekka Pessi's avatar
Pekka Pessi committed
1078 1079
}

1080
/**Duplicate a sip_call_id object.
Pekka Pessi's avatar
Pekka Pessi committed
1081
 *
1082
 * Duplicate (copy deeply) a single #sip_call_id_t header object.
Pekka Pessi's avatar
Pekka Pessi committed
1083 1084 1085 1086 1087 1088
 *
 * @param dst   pointer to newly allocated header object
 * @param src   pointer to a header object to be duplicated
 * @param b     memory buffer used to copy external references
 * @param xtra  number bytes in buffer @a b
 *
1089
 * @return Pointer to the new copy of #sip_call_id_t object, or @c NULL
Pekka Pessi's avatar
Pekka Pessi committed
1090 1091 1092
 * upon an error.
 */
char *sip_call_id_dup_one(sip_header_t *dst, sip_header_t const *src,
1093
			  char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
1094
{
1095 1096
  sip_call_id_t *i = (sip_call_id_t *)dst;
  sip_call_id_t const *o = (sip_call_id_t *)src;
Pekka Pessi's avatar
Pekka Pessi committed
1097 1098
  char *end = b + xtra;

Pekka Pessi's avatar
Pekka Pessi committed
1099
  MSG_STRING_DUP(b, i->i_id, o->i_id);
Pekka Pessi's avatar
Pekka Pessi committed
1100
  if (!(i->i_hash = o->i_hash))
Pekka Pessi's avatar
Pekka Pessi committed
1101
    i->i_hash = msg_hash_string(i->i_id);
1102
  assert(b <= end); (void)end;
Pekka Pessi's avatar
Pekka Pessi committed
1103 1104 1105 1106 1107 1108

  return b;
}

/**@ingroup sip_call_id
 *
1109
 * Create a @CallID header object.
Pekka Pessi's avatar
Pekka Pessi committed
1110
 *
1111 1112 1113 1114
 * Create a Call-ID header object with a new unique value. It uses
 * su_guid_generate() function to generate the value. If the local host name
 * @a domain is specified, it is prepended to the generated value instead of
 * local MAC address.
Pekka Pessi's avatar
Pekka Pessi committed
1115 1116 1117 1118

 * @param home        memory home
 * @param domain      local domain name
 *
1119 1120 1121 1122
 * @return A pointer to newly created @CallID header object when
 * successful or NULL upon an error.
 *
 * @sa su_guid_generate(), su_guid_sprintf()
Pekka Pessi's avatar
Pekka Pessi committed
1123 1124 1125 1126
 */
sip_call_id_t *sip_call_id_create(su_home_t *home, char const *domain)
{
  sip_call_id_t *i;
1127
  size_t xtra = su_guid_strlen + 1 + (domain ? strlen(domain) + 1 : 0);
Pekka Pessi's avatar
Pekka Pessi committed
1128

1129
  i = (sip_call_id_t *)sip_header_alloc(home, sip_call_id_class, xtra);
1130

Pekka Pessi's avatar
Pekka Pessi committed
1131 1132 1133 1134 1135 1136 1137
  if (i) {
    char *b;
    su_guid_t guid[1];

    i->i_id = b = (char *)(i + 1);

    su_guid_generate(guid);
Pekka Pessi's avatar
Pekka Pessi committed
1138
    /*
1139
     * Guid looks like "NNNNNNNN-NNNN-NNNN-NNNN-XXXXXXXXXXXX"
Pekka Pessi's avatar
Pekka Pessi committed
1140
     * where NNNNNNNN-NNNN-NNNN-NNNN is timestamp and XX is MAC address
1141
     * (but we use usually random ID for MAC because we do not have
Pekka Pessi's avatar
Pekka Pessi committed
1142 1143
     *  guid generator available for all processes within node)
     */
Pekka Pessi's avatar
Pekka Pessi committed
1144 1145 1146 1147 1148 1149 1150 1151
    su_guid_sprintf(b, su_guid_strlen + 1, guid);

    /* If we have a domain name don't include MAC address at the end of guid */
    if (domain) {
      b[8 + 5 + 5 + 5] = '@';
      strcpy(b + 8 + 5 + 5 + 5 + 1, domain);
    }

Pekka Pessi's avatar
Pekka Pessi committed
1152
    i->i_hash = msg_hash_string(i->i_id);
Pekka Pessi's avatar
Pekka Pessi committed
1153 1154 1155 1156 1157 1158 1159 1160
  }

  return i;

}

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

1161
/**@SIP_HEADER sip_cseq CSeq Header
Pekka Pessi's avatar
Pekka Pessi committed
1162 1163
 *
 * The CSeq header (command sequence) uniquely identifies transactions
1164
 * within a dialog.  It is defined in @RFC3261 as follows:
1165
 *
Pekka Pessi's avatar
Pekka Pessi committed
1166 1167 1168 1169 1170 1171 1172 1173
 * @code
 *    CSeq              =  "CSeq" HCOLON 1*DIGIT LWS Method
 *    Method            =  INVITEm / ACKm / OPTIONSm / BYEm
 *                         / CANCELm / REGISTERm
 *                         / extension-method
 *    extension-method  =  token
 * @endcode
 *
1174
 * The parsed CSeq header is stored in #sip_cseq_t structure.
Pekka Pessi's avatar
Pekka Pessi committed
1175 1176 1177 1178 1179
 */

/**@ingroup sip_cseq
 * @typedef typedef struct sip_cseq_s sip_cseq_t;
 *
1180
 * The structure #sip_cseq_t contains representation of SIP @CSeq header.
Pekka Pessi's avatar
Pekka Pessi committed
1181
 *
1182
 * The #sip_cseq_t is defined as follows:
Pekka Pessi's avatar
Pekka Pessi committed
1183 1184 1185
 * @code
 * typedef struct sip_cseq_s {
 *   sip_common_t   cs_common[1];       // Common fragment info
1186
 *   sip_error_t   *cs_next;            // Link to next (dummy)
Pekka Pessi's avatar
Pekka Pessi committed
1187
 *   uint32_t       cs_seq;             // Sequence number
Pekka Pessi's avatar
Pekka Pessi committed
1188 1189 1190 1191 1192 1193 1194 1195
 *   sip_method_t   cs_method;          // Method enum
 *   char const    *cs_method_name;     // Method name
 * } sip_cseq_t;
 * @endcode
 */

static msg_xtra_f sip_cseq_dup_xtra;
static msg_dup_f sip_cseq_dup_one;
1196
#define sip_cseq_update NULL
Pekka Pessi's avatar
Pekka Pessi committed
1197

1198
msg_hclass_t sip_cseq_class[] =
Pekka Pessi's avatar
Pekka Pessi committed
1199 1200
SIP_HEADER_CLASS(cseq, "CSeq", "", cs_common, single, cseq);

1201 1202 1203 1204
issize_t sip_cseq_d(su_home_t *home,
		    sip_header_t *h,
		    char *s,
		    isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1205
{
1206
  sip_cseq_t *cs = (sip_cseq_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219

  if (msg_uint32_d(&s, &cs->cs_seq) < 0)
    return -1;

  if (*s) {
    if ((cs->cs_method = sip_method_d(&s, &cs->cs_method_name)) >= 0)
      return 0;
  }

  return -1;
}


1220
issize_t sip_cseq_e(char b[], isize_t bsiz, sip_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
1221
{
1222 1223
  sip_cseq_t const *cs = (sip_cseq_t *)h;

Pekka Pessi's avatar
Pekka Pessi committed
1224 1225
  assert(sip_is_cseq(h));

1226
  return snprintf(b, bsiz, "%u %s", cs->cs_seq, cs->cs_method_name);
Pekka Pessi's avatar
Pekka Pessi committed
1227 1228
}

1229
isize_t sip_cseq_dup_xtra(sip_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
1230
{
1231
  sip_cseq_t const *cs = (sip_cseq_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1232
  if (!cs->cs_method)
Pekka Pessi's avatar
Pekka Pessi committed
1233
    return offset + MSG_STRING_SIZE(cs->cs_method_name);
Pekka Pessi's avatar
Pekka Pessi committed
1234 1235 1236 1237 1238
  else
    return offset;
}

char *sip_cseq_dup_one(sip_header_t *dst, sip_header_t const *src,
1239
		       char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
1240
{
1241 1242
  sip_cseq_t *cs = (sip_cseq_t *)dst;
  sip_cseq_t const *o = (sip_cseq_t *)src;
Pekka Pessi's avatar
Pekka Pessi committed
1243 1244 1245
  char *end = b + xtra;

  if (!(cs->cs_method = o->cs_method))
Pekka Pessi's avatar
Pekka Pessi committed
1246
    MSG_STRING_DUP(b, cs->cs_method_name, o->cs_method_name);
Pekka Pessi's avatar
Pekka Pessi committed
1247 1248 1249 1250
  else
    cs->cs_method_name = o->cs_method_name;
  cs->cs_seq = o->cs_seq;

1251
  assert(b <= end); (void)end;
Pekka Pessi's avatar
Pekka Pessi committed
1252 1253 1254 1255

  return b;
}

1256
/**@ingroup sip_cseq
Pekka Pessi's avatar
Pekka Pessi committed
1257
 *
1258
 *Create a @CSeq header object.
Pekka Pessi's avatar
Pekka Pessi committed
1259
 *
1260
 * Create a @CSeq header object with the
Pekka Pessi's avatar
Pekka Pessi committed
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277
 * sequence number @a seq, method enum @a method and method name @a
 * method_name.  The memory for the header object is allocated from the
 * memory home @a home.
 *
 * @param home        memory home
 * @param seq         sequence number
 * @param method      method enum
 * @param method_name method name (required if method is not well-known)
 *
 * @par Example
 * The following code fragment creates a cseq object for OPTIONS request:
 * @code
 *   sip_cseq_t *cseq;
 *   cseq = sip_cseq_create(home, agent->seq++, SIP_METHOD_OPTIONS);
 * @endcode
 *
 * @return
1278
 * A pointer to newly created @CSeq
Pekka Pessi's avatar
Pekka Pessi committed
1279 1280 1281
 * header object when successful or NULL upon an error.
 */
sip_cseq_t *sip_cseq_create(su_home_t *home,
Pekka Pessi's avatar
Pekka Pessi committed
1282
			    uint32_t seq,
Pekka Pessi's avatar
Pekka Pessi committed
1283 1284 1285 1286 1287 1288 1289
			    unsigned method,
			    char const *method_name)
{
  size_t xtra;
  sip_cseq_t *cs;

  if (method)
Kai Samposalo's avatar
Kai Samposalo committed
1290
    method_name = sip_method_name((sip_method_t)method, method_name);
Pekka Pessi's avatar
Pekka Pessi committed
1291 1292 1293 1294 1295 1296

  if (method_name == NULL)
    return NULL;

  xtra = (method ? 0 : (strlen(method_name) + 1));

1297
  cs = (sip_cseq_t *)sip_header_alloc(home, sip_cseq_class, xtra);
1298

Pekka Pessi's avatar
Pekka Pessi committed
1299 1300
  if (cs) {
    cs->cs_seq = seq;
Kai Samposalo's avatar
Kai Samposalo committed
1301
    cs->cs_method = (sip_method_t)method;
Pekka Pessi's avatar
Pekka Pessi committed
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313
    if (!method)
      method_name = strcpy((char *)(cs + 1), method_name);
    cs->cs_method_name = method_name;
  }
  return cs;
}


/* ====================================================================== */
/**@SIP_HEADER sip_contact Contact Header
 *
 * The Contact header contain a list of URLs used to redirect future
1314
 * requests.  Its syntax is defined in @RFC3261 as follows:
1315
 *
Pekka Pessi's avatar
Pekka Pessi committed
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
 * @code
 *    Contact            =  ("Contact" / "m" ) HCOLON
 *                          ( STAR / (contact-param *(COMMA contact-param)))
 *    contact-param      =  (name-addr / addr-spec) *(SEMI contact-params)
 *    name-addr          =  [ display-name ] LAQUOT addr-spec RAQUOT
 *    addr-spec          =  SIP-URI / SIPS-URI / absoluteURI
 *    display-name       =  *(token LWS)/ quoted-string
 *    contact-params     =  c-p-q / c-p-expires
 *                          / contact-extension
 *    c-p-q              =  "q" EQUAL qvalue
 *    c-p-expires        =  "expires" EQUAL delta-seconds
 *    contact-extension  =  generic-param
 *    delta-seconds      =  1*DIGIT
 * @endcode
 *
 * @note
1332
 * The @RFC2543 syntax allowed <comment>. We accept it, but don't encode it.
1333 1334
 *
 * Each parsed Contact header field is stored in #sip_contact_t structure.
Pekka Pessi's avatar
Pekka Pessi committed
1335 1336 1337 1338 1339
 */

/**@ingroup sip_contact
 * @typedef typedef struct sip_contact_s sip_contact_t;
 *
1340
 * The structure #sip_contact_t contains representation of SIP @Contact
Pekka Pessi's avatar
Pekka Pessi committed
1341 1342 1343 1344 1345 1346 1347 1348 1349
 * header.
 *
 * The #sip_contact_t is defined as follows:
 * @code
 * typedef struct sip_contact_s {
 *   sip_common_t        m_common[1];   // Common fragment info
 *   sip_contact_t      *m_next;        // Link to next
 *   char const         *m_display;     // Display name
 *   url_t               m_url[1];      // SIP URL
Pekka Pessi's avatar
Pekka Pessi committed
1350
 *   msg_param_t const  *m_params;      // List of contact-params
Pekka Pessi's avatar
Pekka Pessi committed
1351 1352
 *   char const         *m_comment;     // Comment
 *
1353
 *   char const         *m_q;           // Priority
1354
 *   char const         *m_expires;     // Expiration time
Pekka Pessi's avatar
Pekka Pessi committed
1355 1356
 * } sip_contact_t;
 * @endcode
1357
 *
1358 1359
 * @note The <comment> field @ref sip_contact_s::m_comment "m_comment" is
 * deprecated: it is parsed but not included in encoding.
Pekka Pessi's avatar
Pekka Pessi committed
1360 1361 1362 1363
 */

static msg_xtra_f sip_contact_dup_xtra;
static msg_dup_f sip_contact_dup_one;
1364
static msg_update_f sip_contact_update;
Pekka Pessi's avatar
Pekka Pessi committed
1365

1366 1367 1368 1369 1370 1371 1372
/** @showinitializer */
msg_hclass_t sip_contact_class[] =
  /*
   * Cut through the fog of macros
   * SIP_HEADER_CLASS(contact, "Contact", "m", m_params, append, contact);
   * and show here how the msg_hclass_t is initialized
   */
1373
  {{
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386
    /* hc_hash: */     sip_contact_hash,
    /* hc_parse: */    sip_contact_d,
    /* hc_print: */    sip_contact_e,
    /* hc_dxtra: */    sip_contact_dup_xtra,
    /* hc_dup_one: */  sip_contact_dup_one,
    /* hc_update: */   sip_contact_update,
    /* hc_name: */     "Contact",
    /* hc_len: */      sizeof("Contact") - 1,
    /* hc_short: */    "m",
    /* hc_size: */     MSG_ALIGN(sizeof(sip_contact_t), sizeof(void*)),
    /* hc_params: */   offsetof(sip_contact_t, m_params),
    /* hc_kind:	*/     msg_kind_append,
    /* hc_critical: */ 0
1387
   }};
Pekka Pessi's avatar
Pekka Pessi committed
1388

1389 1390 1391 1392
issize_t sip_contact_d(su_home_t *home,
		       sip_header_t *h,
		       char *s,
		       isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1393
{
1394
  sip_contact_t *m = (sip_contact_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1395 1396 1397

  assert(h);

1398 1399
  while (*s == ',')   /* Ignore empty entries (comma-whitespace) */
    *s = '\0', s += span_lws(s + 1) + 1;
Pekka Pessi's avatar
Pekka Pessi committed
1400

1401
  if (sip_name_addr_d(home, &s, &m->m_display, m->m_url,
1402
		      &m->m_params, &m->m_comment) == -1)
Pekka Pessi's avatar
Pekka Pessi committed
1403 1404
    return -1;

1405
  return msg_parse_next_field(home, h, s, slen);
Pekka Pessi's avatar
Pekka Pessi committed
1406 1407 1408
}


1409
issize_t sip_contact_e(char b[], isize_t bsiz, sip_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
1410
{
1411
  sip_contact_t const *m = (sip_contact_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1412
  int always_lt_gt = MSG_IS_CANONIC(flags) && m->m_url->url_type != url_any;
1413

Pekka Pessi's avatar
Pekka Pessi committed
1414 1415
  assert(sip_is_contact(h));

Pekka Pessi's avatar
Pekka Pessi committed
1416
  return sip_name_addr_e(b, bsiz, flags,
1417
			 m->m_display, always_lt_gt, m->m_url,
Pekka Pessi's avatar
Pekka Pessi committed
1418 1419 1420 1421 1422
			 m->m_params,
			 NULL /* m->m_comment */);
}


1423
isize_t sip_contact_dup_xtra(sip_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
1424
{
1425
  sip_contact_t const *m = (sip_contact_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1426

1427 1428 1429 1430 1431
  return sip_name_addr_xtra(m->m_display,
			    m->m_url,
			    m->m_params,
			    offset)
    + MSG_STRING_SIZE(m->m_comment);
Pekka Pessi's avatar
Pekka Pessi committed
1432 1433 1434
}

char *sip_contact_dup_one(sip_header_t *dst, sip_header_t const *src,
1435
			  char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
1436
{
1437 1438
  sip_contact_t *m = (sip_contact_t *)dst;
  sip_contact_t const *o = (sip_contact_t *)src;
Pekka Pessi's avatar
Pekka Pessi committed
1439

1440 1441 1442 1443
  b = sip_name_addr_dup(&m->m_display, o->m_display,
			m->m_url, o->m_url,
			&m->m_params, o->m_params,
			b, xtra);
Pekka Pessi's avatar
Pekka Pessi committed
1444
  MSG_STRING_DUP(b, m->m_comment, o->m_comment);
Pekka Pessi's avatar
Pekka Pessi committed
1445 1446 1447
  return b;
}

1448
/** Update parameter in #sip_contact_t */
1449
static int sip_contact_update(msg_common_t *h,
1450
			      char const *name, isize_t namelen,
1451 1452 1453 1454 1455 1456 1457 1458
			      char const *value)
{
  sip_contact_t *m = (sip_contact_t *)h;

  if (name == NULL) {
    m->m_q = NULL;
    m->m_expires = NULL;
  }
1459
  else if (namelen == 1 && su_casenmatch(name, "q", 1)) {
1460 1461 1462
    /* XXX - check for invalid value? */
    m->m_q = value;
  }
1463
  else if (namelen == strlen("expires") &&
1464
	   su_casenmatch(name, "expires", namelen)) {
1465 1466 1467 1468 1469 1470
    m->m_expires = value;
  }

  return 0;
}

1471
/**@ingroup sip_contact
Pekka Pessi's avatar
Pekka Pessi committed
1472
 *