sip_basic.c 74.8 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/string0.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:
Pekka Pessi's avatar
Pekka Pessi committed
67 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 121 122

msg_hclass_t sip_request_class[] = 
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 127 128 129 130 131 132 133 134 135 136 137
{
  sip_request_t *rq = h->sh_request;
  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 142 143 144 145 146 147 148
{
  sip_request_t const *rq = h->sh_request;

  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 152
{
  sip_request_t const *rq = h->sh_request;

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 167 168 169 170 171
{
  sip_request_t *rq = dst->sh_request;
  sip_request_t const *o = src->sh_request;
  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 204 205
 * @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

 * @note 
 * 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 227 228 229 230 231 232 233 234
  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);

  rq = sip_header_alloc(home, sip_request_class, xtra)->sh_request;

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

    rq->rq_method      = method;
    rq->rq_method_name = name;
    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:
Pekka Pessi's avatar
Pekka Pessi committed
252 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 274
 * The structure #sip_status_t contains representation of SIP 
 * @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 296 297 298

msg_hclass_t sip_status_class[] = 
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 302 303 304 305 306 307 308 309 310 311 312 313 314 315
{
  sip_status_t *st = h->sh_status;
  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 319 320 321 322 323 324 325 326 327 328 329 330 331 332
{
  int status;

  assert(sip_is_status(h));

  status = h->sh_status->st_status;

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

  return snprintf(b, bsiz, "%s %03u %s" CRLF,
		  h->sh_status->st_version,
		  status,
		  h->sh_status->st_phrase);
}

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

/** Duplicate one status header. */
char *sip_status_dup_one(sip_header_t *dst, sip_header_t const *src,
344
			 char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
345 346 347 348 349 350 351
{
  sip_status_t *st = dst->sh_status;
  sip_status_t const *o = src->sh_status;
  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
352
  MSG_STRING_DUP(b, st->st_phrase, o->st_phrase);
Pekka Pessi's avatar
Pekka Pessi committed
353

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

  return b;
}

/**@ingroup sip_status
 *
361
 * Create a @ref sip_status "status line" object.
Pekka Pessi's avatar
Pekka Pessi committed
362
 *
363
 * @param home    memory home used to allocate #sip_status_t object
Pekka Pessi's avatar
Pekka Pessi committed
364
 * @param status  status code (in range 100 - 699)
Pekka Pessi's avatar
Pekka Pessi committed
365
 * @param phrase  status phrase (may be NULL)
Pekka Pessi's avatar
Pekka Pessi committed
366 367 368 369 370 371 372
 * @param version version string (defaults to "SIP/2.0" if NULL)
 *
 * @note 
 * If you provide an non-NULL @a version string, it is not copied. The
 * string @b MUST remain constant.
 *
 * @return
373 374
 * 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
375 376 377 378 379 380 381 382
 */
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
383
  if (status < 100 || status > 699)
Pekka Pessi's avatar
Pekka Pessi committed
384 385
    return NULL;

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

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

  return st;
}

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

400
/**@SIP_HEADER sip_payload Message Body
Pekka Pessi's avatar
Pekka Pessi committed
401
 *
402 403 404 405 406 407
 * 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
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429
 */

/**@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
#define sip_payload_dup_xtra msg_payload_dup_xtra 
#define sip_payload_dup_one  msg_payload_dup_one
430
#define sip_payload_update   NULL
Pekka Pessi's avatar
Pekka Pessi committed
431 432 433 434 435 436

msg_hclass_t sip_payload_class[] = 
SIP_HEADER_CLASS(payload, NULL, "", pl_common, single, payload);

/**@ingroup sip_payload
 *
437
 * Create a @ref sip_payload "SIP payload" structure. 
Pekka Pessi's avatar
Pekka Pessi committed
438
 *
439
 * Create a new SIP payload structure. it
Pekka Pessi's avatar
Pekka Pessi committed
440 441 442 443 444 445 446 447
 * 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.
 *
 * @param home memory home 
 * @param data payload data 
 * @param len  payload length
 *
448
 * @return A pointer to newly created
Pekka Pessi's avatar
Pekka Pessi committed
449 450
 * payload structure, if successful, and NULL upon an error.
 */ 
451
sip_payload_t *sip_payload_create(su_home_t *home, void const *data, isize_t len)
Pekka Pessi's avatar
Pekka Pessi committed
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
{
  msg_hclass_t *hc = sip_payload_class;
  sip_header_t *h = sip_header_alloc(home, hc, len + 1);
  sip_payload_t *pl = h->sh_payload;

  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
481 482 483
 * #sip_t structure.
 *
 * The parsed separator line is stored in #sip_separator_t structure.
Pekka Pessi's avatar
Pekka Pessi committed
484 485 486 487 488
 */

/**@ingroup sip_separator
 * @typedef typedef struct sip_separator_s sip_separator_t;
 *
489
 * The structure #sip_separator_t contains representation of separator line
Pekka Pessi's avatar
Pekka Pessi committed
490 491
 * between message headers and body.
 *
492
 * The #sip_separator_t is defined as follows:
Pekka Pessi's avatar
Pekka Pessi committed
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510
 * @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

msg_hclass_t sip_separator_class[] = 
SIP_HEADER_CLASS(separator, NULL, "", sep_common, single, any);

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

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

  return sep;
}

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

/**@SIP_HEADER sip_unknown Unknown Headers
 *
528 529 530
 * 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.
Pekka Pessi's avatar
Pekka Pessi committed
531 532 533 534 535 536 537
 * 
 * @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.
 */

538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
/**@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 {
 *   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 
 * } sip_unknown_t;
 * @endcode
 */

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

msg_hclass_t sip_unknown_class[] = 
SIP_HEADER_CLASS(unknown, "", "", un_common, append, unknown);

561
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
562 563 564 565
{
  return msg_unknown_d(home, h, s, slen);
}

566
issize_t sip_unknown_e(char b[], isize_t bsiz, sip_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
567 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
 * @note Other headers (like duplicate @ContentLength headers) may be put
Pekka Pessi's avatar
Pekka Pessi committed
578 579 580 581 582 583 584
 * 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;
585 586 587 588 589 590 591 592 593 594
 * The structure #sip_error_t contains representation of error headers.
 *
 * The #sip_error_t is defined as follows:
 * @code
 * typedef struct msg_error_s {
 *   msg_common_t    er_common[1];  // Common fragment info 
 *   msg_error_t    *er_next;       // Link to next header 
 *   char const     *er_name;       // Name of bad header (if any)
 * } sip_error_t;
 * @endcode
Pekka Pessi's avatar
Pekka Pessi committed
595 596 597 598 599
 */

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

600
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
601 602 603 604
{
  return 0;
}

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

/* ====================================================================== */
/*
 * addr           = ("To" | "t" | "From" | "f") ":" 
 *                  ( 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
620 621
/**Parse @e name-addr.
 *
622 623
 * 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
624 625 626 627 628 629 630 631 632 633 634 635 636 637
 * 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
 * @param return_params  value-result paramater for @e parameters 
 * @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,
638
 * most probably a comma. If that character is a separator, the last parameter
Pekka Pessi's avatar
Pekka Pessi committed
639
 * may not be NUL (zero) terminated. So, after examining value of @a **ss,
640
 * the calling function @b MUST set it to NUL.
Pekka Pessi's avatar
Pekka Pessi committed
641
 *
642 643
 * @retval 0 if successful
 * @retval -1 upon an error
Pekka Pessi's avatar
Pekka Pessi committed
644
 *
645
 * @sa @From, @To, @Contact
Pekka Pessi's avatar
Pekka Pessi committed
646
 */
647 648 649 650 651 652
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
653 654 655
{
  char c, *s = *inout_s;
  char *display = NULL, *addr_spec = NULL;
656
  size_t n;
Pekka Pessi's avatar
Pekka Pessi committed
657

658 659 660
  if (*s == '\0')		/* Empty string */
    return -1;
  
Pekka Pessi's avatar
Pekka Pessi committed
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701
  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, ">");
    addr_spec = s; s += n; 
    if (*s) *s++ = '\0'; else return -1;
  } 
  else {
    if (return_display) 
      n = span_token_lws(s);
    else
      n = 0;

    if (s[n] == '<') {
      /* OK, we got a display name */
      display = s; s += n + 1; 
      /* NUL terminate display name */
      while (n > 0 && IS_LWS(display[n - 1]))
	n--;
      if (n > 0)
	display[n] = '\0';
      else 
	display = "";

      n = strcspn(s, ">");
      addr_spec = s; s += n; if (*s) *s++ = '\0'; else return -1;
    }
    else {
      /* addr-spec only */
      addr_spec = s;
      /**@sa
       * Discussion about comma, semicolon and question mark in 
       * @RFC3261 section 20.10.
       */
      if (return_params)
702
	n = strcspn(s, " \t,;?");	/* DO NOT accept ,;? in URL */
Pekka Pessi's avatar
Pekka Pessi committed
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
      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;
  
  /* Now, url may still not be NUL terminated, e.g., if 
   * it is like "Contact: url:foo,sip:bar,sip:zunk"
   */
  c = *s; *s = '\0';		/* terminate temporarily */
  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.
 *
740 741
 * Encodes @e name-addr headers, like @From, @To, @CallInfo, @ErrorInfo,
 * @Route, and @RecordRoute.
Pekka Pessi's avatar
Pekka Pessi committed
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
 *
 * @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)
 *
 * @return 
 * 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.
 */
760 761 762 763 764 765
issize_t sip_name_addr_e(char b[], isize_t bsiz, 
			 int flags, 
			 char const *display, 
			 int brackets, url_t const url[],
			 msg_param_t const params[], 
			 char const *comment)
Pekka Pessi's avatar
Pekka Pessi committed
766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
{
  int const compact = MSG_IS_COMPACT(flags);
  char const *u;
  char *b0 = b, *end = b + bsiz;

  brackets = brackets || display || 
    (url && (url->url_params || 
	     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);
    
  return b - b0;
}

801
/** Parse @To or @From headers */
802 803 804 805
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
806
{
807
  sip_addr_t *a = (sip_addr_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
808
  char const *comment = NULL;
Pekka Pessi's avatar
Pekka Pessi committed
809
  if (sip_name_addr_d(home, 
Pekka Pessi's avatar
Pekka Pessi committed
810 811 812 813 814 815 816 817
		      &s, 
		      &a->a_display, 
		      a->a_url, 
		      &a->a_params,
		      &comment) == -1 
      || *s /* XXX - something extra? */)
    return -1;

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

Pekka Pessi's avatar
Pekka Pessi committed
820 821 822
  return 0;
}

823
static int sip_addr_e(char b[], isize_t bsiz, sip_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
824
{
825
  sip_addr_t const *a = (sip_addr_t const *)h;
Pekka Pessi's avatar
Pekka Pessi committed
826

Pekka Pessi's avatar
Pekka Pessi committed
827
  return sip_name_addr_e(b, bsiz,
Pekka Pessi's avatar
Pekka Pessi committed
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846
			 flags,
			 a->a_display, 
			 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
847
isize_t sip_addr_dup_xtra(sip_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
848
{
849
  sip_addr_t const *a = (sip_addr_t const *)h;
Pekka Pessi's avatar
Pekka Pessi committed
850

851 852 853
  MSG_PARAMS_SIZE(offset, a->a_params);
  offset += MSG_STRING_SIZE(a->a_display);
  offset += url_xtra(a->a_url);
Pekka Pessi's avatar
Pekka Pessi committed
854
    
855
  return offset;
Pekka Pessi's avatar
Pekka Pessi committed
856 857 858 859 860 861
}

/**@internal
 * Duplicate one sip_addr_t object.
 */
static char *sip_addr_dup_one(sip_header_t *dst, sip_header_t const *src,
862
			      char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
863
{
864 865
  sip_addr_t *a = (sip_addr_t *)dst;
  sip_addr_t const *o = (sip_addr_t *)src;
Pekka Pessi's avatar
Pekka Pessi committed
866 867
  char *end = b + xtra;

Pekka Pessi's avatar
Pekka Pessi committed
868 869
  b = msg_params_dup(&a->a_params, o->a_params, b, xtra);
  MSG_STRING_DUP(b, a->a_display, o->a_display);
Pekka Pessi's avatar
Pekka Pessi committed
870 871 872 873 874 875 876
  URL_DUP(b, end, a->a_url, o->a_url);

  assert(b <= end);

  return b;
}

877 878
/** Update parameters in sip_addr_t object */
static int sip_addr_update(msg_common_t *h,
879
			   char const *name, isize_t namelen,
880 881 882 883 884 885 886 887 888 889 890 891 892 893
			   char const *value)
{
  sip_addr_t *a = (sip_addr_t *)h;

  if (name == NULL) {
    a->a_tag = NULL;
  }
  else if (namelen == strlen("tag") && !strncasecmp(name, "tag", namelen)) {
    a->a_tag = value;
  }

  return 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
894 895 896 897
/** 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)
{
898
  size_t n;
Pekka Pessi's avatar
Pekka Pessi committed
899 900 901 902 903 904 905 906 907
  sip_header_t *h;

  n = url_xtra(us->us_url);
  h = sip_header_alloc(home, sip_to_class, n);    

  if (h) {
    sip_addr_t *a = h->sh_to;
    char *s2 = sip_header_data(h);

908
    if ((size_t)url_dup(s2, n, a->a_url, us->us_url) == n)
Pekka Pessi's avatar
Pekka Pessi committed
909 910 911 912 913 914 915 916
      return a;

    su_free(home, h);
  }

  return NULL;
}

917
/** Add a tag to address structure. */
Pekka Pessi's avatar
Pekka Pessi committed
918 919 920 921
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
922
    msg_param_t value = strchr(tag, '=');
Pekka Pessi's avatar
Pekka Pessi committed
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941

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

    if (a->a_tag) {
      if (strcasecmp(a->a_tag, value) == 0)
	return 0;
      else
	return -1;
    }

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

    if (tag)
942 943
      if (msg_header_replace_param(home, a->a_common, tag) >= 0)
	return 0;
Pekka Pessi's avatar
Pekka Pessi committed
944 945 946 947 948 949 950 951 952 953
  }

  return -1;
}

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

/**@SIP_HEADER sip_call_id Call-ID Header
 *
 * The @b Call-ID header uniquely identifies a particular invitation or all
954
 * registrations of a particular client. It is defined in @RFC3261 as
Pekka Pessi's avatar
Pekka Pessi committed
955 956 957 958 959 960 961 962 963 964
 * follows:
 * 
 * @code
 *    Call-ID  =  ( "Call-ID" / "i" ) HCOLON callid
 *    callid   =  word [ "@" word ]
 *    word        =  1*(alphanum / "-" / "." / "!" / "%" / "*" /
 *                   "_" / "+" / "`" / "'" / "~" / "(" / ")" / "<" / ">" /
 *                   ":" / "\" / DQUOTE / "/" / "[" / "]" / "?" / "{" / "}" )
 * @endcode
 *
965
 * The parsed Call-ID Header is stored in #sip_call_id_t structure.
Pekka Pessi's avatar
Pekka Pessi committed
966 967 968 969 970
 */

/**@ingroup sip_call_id
 * @typedef typedef struct sip_call_id_s sip_call_id_t;
 *
971
 * The structure #sip_call_id_t contains representation of SIP @CallID
Pekka Pessi's avatar
Pekka Pessi committed
972 973 974 975 976 977 978 979
 * 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
980
 *   uint32_t       i_hash;             // Hash value (always nonzero)
Pekka Pessi's avatar
Pekka Pessi committed
981 982 983 984 985 986
 * } sip_call_id_t;
 * @endcode
 */

static msg_xtra_f sip_call_id_dup_xtra;
static msg_dup_f sip_call_id_dup_one;
987
#define sip_call_id_update NULL
Pekka Pessi's avatar
Pekka Pessi committed
988 989 990 991

msg_hclass_t sip_call_id_class[] = 
SIP_HEADER_CLASS(call_id, "Call-ID", "i", i_common, single, call_id);

992 993 994 995
issize_t sip_call_id_d(su_home_t *home,
		       sip_header_t *h,
		       char *s, 
		       isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
996 997 998
{
  sip_call_id_t *i = h->sh_call_id;
  
999
  i->i_id = s; /* XXX - why not sip_word_at_word_d(&s); */
Pekka Pessi's avatar
Pekka Pessi committed
1000 1001 1002 1003 1004 1005
  i->i_hash = msg_hash_string(s);

  return 0;
}


1006
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
1007
{
1008
  size_t n = strlen(h->sh_call_id->i_id);
Pekka Pessi's avatar
Pekka Pessi committed
1009 1010 1011 1012

  if (bsiz > n)
    strcpy(b, h->sh_call_id->i_id);

1013
  return (issize_t)n;
Pekka Pessi's avatar
Pekka Pessi committed
1014 1015
}

1016
/** Extra size of a #sip_call_id_t object. */
1017
isize_t sip_call_id_dup_xtra(sip_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
1018 1019
{
  sip_call_id_t const *i = h->sh_call_id;
Pekka Pessi's avatar
Pekka Pessi committed
1020
  return offset + MSG_STRING_SIZE(i->i_id);
Pekka Pessi's avatar
Pekka Pessi committed
1021 1022
}

1023
/**Duplicate a sip_call_id object.
Pekka Pessi's avatar
Pekka Pessi committed
1024
 *
1025
 * Duplicate (copy deeply) a single #sip_call_id_t header object.
Pekka Pessi's avatar
Pekka Pessi committed
1026 1027 1028 1029 1030 1031
 *
 * @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
 *
1032
 * @return Pointer to the new copy of #sip_call_id_t object, or @c NULL
Pekka Pessi's avatar
Pekka Pessi committed
1033 1034 1035
 * upon an error.
 */
char *sip_call_id_dup_one(sip_header_t *dst, sip_header_t const *src,
1036
			  char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
1037 1038 1039 1040 1041
{
  sip_call_id_t *i = dst->sh_call_id;
  sip_call_id_t const *o = src->sh_call_id;
  char *end = b + xtra;

Pekka Pessi's avatar
Pekka Pessi committed
1042
  MSG_STRING_DUP(b, i->i_id, o->i_id);
Pekka Pessi's avatar
Pekka Pessi committed
1043
  if (!(i->i_hash = o->i_hash))
Pekka Pessi's avatar
Pekka Pessi committed
1044
    i->i_hash = msg_hash_string(i->i_id);
1045
  assert(b <= end); (void)end;
Pekka Pessi's avatar
Pekka Pessi committed
1046 1047 1048 1049 1050 1051

  return b;
}

/**@ingroup sip_call_id
 *
1052
 * Create a @CallID header object. 
Pekka Pessi's avatar
Pekka Pessi committed
1053
 *
1054 1055 1056 1057
 * 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
1058 1059 1060 1061

 * @param home        memory home
 * @param domain      local domain name
 *
1062 1063 1064 1065
 * @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
1066 1067 1068 1069
 */
sip_call_id_t *sip_call_id_create(su_home_t *home, char const *domain)
{
  sip_call_id_t *i;
1070
  size_t xtra = su_guid_strlen + 1 + (domain ? strlen(domain) + 1 : 0);
Pekka Pessi's avatar
Pekka Pessi committed
1071 1072 1073 1074 1075 1076 1077 1078 1079 1080

  i = sip_header_alloc(home, sip_call_id_class, xtra)->sh_call_id;
  
  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
1081
    /*
1082
     * Guid looks like "NNNNNNNN-NNNN-NNNN-NNNN-XXXXXXXXXXXX"
Pekka Pessi's avatar
Pekka Pessi committed
1083
     * where NNNNNNNN-NNNN-NNNN-NNNN is timestamp and XX is MAC address
1084
     * (but we use usually random ID for MAC because we do not have
Pekka Pessi's avatar
Pekka Pessi committed
1085 1086
     *  guid generator available for all processes within node)
     */
Pekka Pessi's avatar
Pekka Pessi committed
1087 1088 1089 1090 1091 1092 1093 1094
    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
1095
    i->i_hash = msg_hash_string(i->i_id);
Pekka Pessi's avatar
Pekka Pessi committed
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
  }

  return i;

}

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

/**@SIP_HEADER sip_cseq CSeq Header 
 *
 * The CSeq header (command sequence) uniquely identifies transactions
1107
 * within a dialog.  It is defined in @RFC3261 as follows:
Pekka Pessi's avatar
Pekka Pessi committed
1108 1109 1110 1111 1112 1113 1114 1115 1116
 * 
 * @code
 *    CSeq              =  "CSeq" HCOLON 1*DIGIT LWS Method
 *    Method            =  INVITEm / ACKm / OPTIONSm / BYEm
 *                         / CANCELm / REGISTERm
 *                         / extension-method
 *    extension-method  =  token
 * @endcode
 *
1117
 * The parsed CSeq header is stored in #sip_cseq_t structure.
Pekka Pessi's avatar
Pekka Pessi committed
1118 1119 1120 1121 1122
 */

/**@ingroup sip_cseq
 * @typedef typedef struct sip_cseq_s sip_cseq_t;
 *
1123
 * The structure #sip_cseq_t contains representation of SIP @CSeq header.
Pekka Pessi's avatar
Pekka Pessi committed
1124
 *
1125
 * The #sip_cseq_t is defined as follows:
Pekka Pessi's avatar
Pekka Pessi committed
1126 1127 1128
 * @code
 * typedef struct sip_cseq_s {
 *   sip_common_t   cs_common[1];       // Common fragment info
1129
 *   sip_error_t   *cs_next;            // Link to next (dummy)
Pekka Pessi's avatar
Pekka Pessi committed
1130
 *   uint32_t       cs_seq;             // Sequence number
Pekka Pessi's avatar
Pekka Pessi committed
1131 1132 1133 1134 1135 1136 1137 1138
 *   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;
1139
#define sip_cseq_update NULL
Pekka Pessi's avatar
Pekka Pessi committed
1140 1141 1142 1143

msg_hclass_t sip_cseq_class[] = 
SIP_HEADER_CLASS(cseq, "CSeq", "", cs_common, single, cseq);

1144 1145 1146 1147
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
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
{
  sip_cseq_t *cs = h->sh_cseq;

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


1163
issize_t sip_cseq_e(char b[], isize_t bsiz, sip_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
1164 1165 1166 1167 1168 1169 1170 1171 1172
{
  assert(sip_is_cseq(h));

  return snprintf(b, bsiz, 
		  "%u %s", 
		  h->sh_cseq->cs_seq,
		  h->sh_cseq->cs_method_name);
}

1173
isize_t sip_cseq_dup_xtra(sip_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
1174 1175 1176
{
  sip_cseq_t const *cs = h->sh_cseq;
  if (!cs->cs_method)
Pekka Pessi's avatar
Pekka Pessi committed
1177
    return offset + MSG_STRING_SIZE(cs->cs_method_name);
Pekka Pessi's avatar
Pekka Pessi committed
1178 1179 1180 1181 1182
  else
    return offset;
}

char *sip_cseq_dup_one(sip_header_t *dst, sip_header_t const *src,
1183
		       char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
1184 1185 1186 1187 1188 1189
{
  sip_cseq_t *cs = dst->sh_cseq;
  sip_cseq_t const *o = src->sh_cseq;
  char *end = b + xtra;

  if (!(cs->cs_method = o->cs_method))
Pekka Pessi's avatar
Pekka Pessi committed
1190
    MSG_STRING_DUP(b, cs->cs_method_name, o->cs_method_name);
Pekka Pessi's avatar
Pekka Pessi committed
1191 1192 1193 1194
  else
    cs->cs_method_name = o->cs_method_name;
  cs->cs_seq = o->cs_seq;

1195
  assert(b <= end); (void)end;
Pekka Pessi's avatar
Pekka Pessi committed
1196 1197 1198 1199 1200 1201

  return b;
}

/**@ingroup sip_cseq 
 *
1202
 *Create a @CSeq header object. 
Pekka Pessi's avatar
Pekka Pessi committed
1203
 *
1204
 * Create a @CSeq header object with the
Pekka Pessi's avatar
Pekka Pessi committed
1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221
 * 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
1222
 * A pointer to newly created @CSeq
Pekka Pessi's avatar
Pekka Pessi committed
1223 1224 1225
 * 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
1226
			    uint32_t seq,
Pekka Pessi's avatar
Pekka Pessi committed
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
			    unsigned method,
			    char const *method_name)
{
  size_t xtra;
  sip_cseq_t *cs;

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

  if (method_name == NULL)
    return NULL;

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

  cs = sip_header_alloc(home, sip_cseq_class, xtra)->sh_cseq;
  
  if (cs) {
    cs->cs_seq = seq;
    cs->cs_method = method;
    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
1258
 * requests.  Its syntax is defined in @RFC3261 as follows:
Pekka Pessi's avatar
Pekka Pessi committed
1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
 * 
 * @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
1276
 * The @RFC2543 syntax allowed <comment>. We accept it, but don't encode it.
1277 1278
 *
 * Each parsed Contact header field is stored in #sip_contact_t structure.
Pekka Pessi's avatar
Pekka Pessi committed
1279 1280 1281 1282 1283
 */

/**@ingroup sip_contact
 * @typedef typedef struct sip_contact_s sip_contact_t;
 *
1284
 * The structure #sip_contact_t contains representation of SIP @Contact
Pekka Pessi's avatar
Pekka Pessi committed
1285 1286 1287 1288 1289 1290 1291 1292 1293
 * 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
1294
 *   msg_param_t const  *m_params;      // List of contact-params
Pekka Pessi's avatar
Pekka Pessi committed
1295 1296
 *   char const         *m_comment;     // Comment
 *
1297 1298
 *   char const         *m_q;           // Priority
 *   char const         *m_expires;     // Expiration time 
Pekka Pessi's avatar
Pekka Pessi committed
1299 1300 1301
 * } sip_contact_t;
 * @endcode
 * 
1302 1303
 * @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
1304 1305 1306 1307
 */

static msg_xtra_f sip_contact_dup_xtra;
static msg_dup_f sip_contact_dup_one;
1308
static msg_update_f sip_contact_update;
Pekka Pessi's avatar
Pekka Pessi committed
1309

1310 1311 1312 1313 1314 1315 1316
/** @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
   */
1317
  {{
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
    /* 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
1331
   }};
Pekka Pessi's avatar
Pekka Pessi committed
1332

1333 1334 1335 1336
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
1337
{
1338
  sip_contact_t *m = (sip_contact_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
1339 1340 1341

  assert(h);

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

1345 1346
  if (sip_name_addr_d(home, &s, &m->m_display, m->m_url, 
		      &m->m_params, &m->m_comment) == -1)
Pekka Pessi's avatar
Pekka Pessi committed
1347 1348
    return -1;

1349
  return msg_parse_next_field(home, h, s, slen);
Pekka Pessi's avatar
Pekka Pessi committed
1350 1351 1352
}


1353
issize_t sip_contact_e(char b[], isize_t bsiz, sip_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
1354 1355 1356
{
  sip_contact_t const *m = h->sh_contact;
  int always_lt_gt = MSG_IS_CANONIC(flags) && m->m_url->url_type != url_any;
1357

Pekka Pessi's avatar
Pekka Pessi committed
1358 1359
  assert(sip_is_contact(h));

Pekka Pessi's avatar
Pekka Pessi committed
1360
  return sip_name_addr_e(b, bsiz, flags,
Pekka Pessi's avatar
Pekka Pessi committed
1361 1362 1363 1364 1365 1366
			 m->m_display, always_lt_gt, m->m_url, 
			 m->m_params,
			 NULL /* m->m_comment */);
}


1367
isize_t sip_contact_dup_xtra(sip_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
1368 1369 1370
{
  sip_contact_t const *m = h->sh_contact;

1371 1372 1373 1374
  MSG_PARAMS_SIZE(offset, m->m_params);
  offset += MSG_STRING_SIZE(m->m_display);
  offset += url_xtra(m->m_url);
  offset += MSG_STRING_SIZE(m->m_comment);
Pekka Pessi's avatar
Pekka Pessi committed
1375

1376
  return offset;
Pekka Pessi's avatar
Pekka Pessi committed
1377 1378 1379
}

char *sip_contact_dup_one(sip_header_t *dst, sip_header_t const *src,
1380
			  char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
1381 1382 1383 1384 1385
{
  char *end = b + xtra;
  sip_contact_t *m = dst->sh_contact;
  sip_contact_t const *o = src->sh_contact;

Pekka Pessi's avatar
Pekka Pessi committed
1386 1387
  b = msg_params_dup(&m->m_params, o->m_params, b, xtra);
  MSG_STRING_DUP(b, m->m_display, o->m_display);
Pekka Pessi's avatar
Pekka Pessi committed
1388
  URL_DUP(b, end, m->m_url, o->m_url);
Pekka Pessi's avatar
Pekka Pessi committed
1389
  MSG_STRING_DUP(b, m->m_comment, o->m_comment);
Pekka Pessi's avatar
Pekka Pessi committed
1390 1391 1392 1393 1394 1395

  assert(b <= end);

  return b;
}

1396
/** Update parameter in #sip_contact_t */
1397
static int sip_contact_update(msg_common_t *h, 
1398
			      char const *name, isize_t namelen,
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
			      char const *value)
{
  sip_contact_t *m = (sip_contact_t *)h;

  if (name == NULL) {
    m->m_q = NULL;
    m->m_expires = NULL;
  }
  else if (namelen == 1 && strncasecmp(name, "q", 1) == 0) {
    /* XXX - check for invalid value? */
    m->m_q = value;
  }
  else if (namelen == strlen("expires") && 
	   !strncasecmp(name, "expires", namelen)) {
    m->m_expires = value;
  }

  return 0;
}

Pekka Pessi's avatar
Pekka Pessi committed
1419 1420
/**@ingroup sip_contact 
 *
1421
 * Add a parameter to a @Contact header object
Pekka Pessi's avatar
Pekka Pessi committed
1422
 *
1423 1424
 * Add a parameter to a @Contact
 * object. It does not copy the contents of the string @c param.
Pekka Pessi's avatar
Pekka Pessi committed
1425
 *
1426
 * @note This function @b does @b not duplicate @p param.
Pekka Pessi's avatar
Pekka Pessi committed
1427 1428
 *
 * @param home   memory home
1429
 * @param m      #sip_contact_t object
Pekka Pessi's avatar
Pekka Pessi committed
1430 1431
 * @param param  parameter string
 *
1432 1433 1434 1435
 * @return 0 when successful, and -1 upon an error.
 *
 * @deprecated Use msg_header_replace_param() directly.
 */
Pekka Pessi's avatar
Pekka Pessi committed
1436 1437 1438 1439
int sip_contact_add_param(su_home_t *home,
			  sip_contact_t *m,
			  char const *param)
{
1440
  return msg_header_replace_param(home, m->m_common, param);
Pekka Pessi's avatar
Pekka Pessi committed
1441 1442 1443 1444 1445 1446 1447
}

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

/**@SIP_HEADER sip_content_length Content-Length Header
 *
 * The Content-Length header indicates the size of the message-body in
1448
 * decimal number of octets.  Its syntax is defined in @RFC3261 as
Pekka Pessi's avatar
Pekka Pessi committed
1449 1450 1451 1452 1453 1454
 * follows:
 *
 * @code
 *    Content-Length  =  ( "Content-Length" / "l" ) HCOLON 1*DIGIT
 * @endcode
 *
1455 1456
 * The parsed Content-Length header is stored in #sip_content_length_t
 * structure.
Pekka Pessi's avatar
Pekka Pessi committed
1457 1458 1459 1460 1461 1462
 */

/**@ingroup sip_content_length
 * @typedef typedef struct sip_content_length_s sip_content_length_t;
 *
 * The structure #sip_content_length_t contains representation of SIP
1463
 * @ContentLength header.
Pekka Pessi's avatar
Pekka Pessi committed
1464 1465 1466 1467 1468
 *
 * The #sip_content_length_t is defined as follows:
 * @code
 * typedef struct sip_content_length_s {
 *   sip_common_t   l_common[1];        // Common fragment info
1469 1470
 *   sip_error_t   *l_next;             // Dummy link to next
 *   uint32_t       l_length;           // Message body length in bytes
Pekka Pessi's avatar
Pekka Pessi committed
1471 1472 1473 1474 1475 1476 1477 1478
 * } sip_content_length_t;
 * @endcode
 */

msg_hclass_t sip_content_length_class[] = 
SIP_HEADER_CLASS(content_length, "Content-Length", "l", l_common, 
		 single_critical, any);

1479 1480 1481 1482
issize_t sip_content_length_d(su_home_t *home,
			      sip_header_t *h,
			      char *s,
			      isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1483 1484 1485 1486
{
  return sip_numeric_d(home, h, s, slen);
}

1487
issize_t sip_content_length_e(char b[], isize_t bsiz, sip_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
1488 1489 1490 1491 1492 1493 1494
{
  assert(sip_is_content_length(h));
  return sip_numeric_e(b, bsiz, h, flags);
}

/**@ingroup sip_content_length 
 *
1495
 *Create a @ContentLength header object. 
Pekka Pessi's avatar
Pekka Pessi committed
1496
 *
1497
 * Create a @ContentLength
Pekka Pessi's avatar
Pekka Pessi committed
1498 1499 1500 1501 1502 1503 1504
 * header object with the value @a n.  The memory for the header is
 * allocated from the memory home @a home.
 *
 * @param home  memory home
 * @param n     payload size in bytes
 *
 * @return
1505 1506
 * A pointer to newly created @ContentLength header object when successful
 * or NULL upon an error.
Pekka Pessi's avatar
Pekka Pessi committed
1507
 */
Pekka Pessi's avatar
Pekka Pessi committed
1508
sip_content_length_t *sip_content_length_create(su_home_t *home, uint32_t n)
Pekka Pessi's avatar
Pekka Pessi committed
1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523
{
  sip_content_length_t *l = 
    sip_header_alloc(home, sip_content_length_class, 0)->sh_content_length;
  
  if (l)
    l->l_length = n;

  return l;
}

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

/**@SIP_HEADER sip_date Date Header
 *
 * The Date header field reflects the time when the request or response was
1524
 * first sent.  Its syntax is defined in @RFC3261 and @RFC2616 section 14.18 as
Pekka Pessi's avatar
Pekka Pessi committed
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541
 * follows:
 * 
 * @code
 *    Date          =  "Date" HCOLON SIP-date
 *    SIP-date      =  rfc1123-date
 *    rfc1123-date  =  wkday "," SP date1 SP time SP "GMT"
 *    date1         =  2DIGIT SP month SP 4DIGIT
 *                     ; day month year (e.g., 02 Jun 1982)
 *    time          =  2DIGIT ":" 2DIGIT ":" 2DIGIT
 *                     ; 00:00:00 - 23:59:59
 *    wkday         =  "Mon" / "Tue" / "Wed"
 *                     / "Thu" / "Fri" / "Sat" / "Sun"
 *    month         =  "Jan" / "Feb" / "Mar" / "Apr"
 *                     / "May" / "Jun" / "Jul" / "Aug"
 *                     / "Sep" / "Oct" / "Nov" / "Dec"
 * @endcode
 *
1542
 * The parsed Date header is stored in #sip_date_t structure.
Pekka Pessi's avatar
Pekka Pessi committed
1543 1544 1545 1546 1547
 */

/**@ingroup sip_date
 * @typedef typedef struct sip_date_s sip_date_t;
 *
1548
 * The structure #sip_date_t contains representation of SIP @Date header.
Pekka Pessi's avatar
Pekka Pessi committed
1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562
 *
 * The #sip_date_t is defined as follows:
 * @code
 * typedef struct sip_date_s {
 *   sip_common_t   d_common[1];        // Common fragment info
 *   sip_date_t    *d_next;             // Link to next (dummy)
 *   sip_time_t     d_time;             // Seconds since Jan 1, 1900
 * } sip_date_t;
 * @endcode
 */

msg_hclass_t sip_date_class[] = 
SIP_HEADER_CLASS(date, "Date", "", d_common, single, any);

1563
issize_t sip_date_d(su_home_t *home, sip_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1564 1565 1566 1567 1568 1569 1570 1571 1572
{
  sip_date_t *date = h->sh_date;

  if (msg_date_d((char const **)&s, &date->d_time) < 0 || *s)
    return -1;
  else
    return 0;
}

1573
issize_t sip_date_e(char b[], isize_t bsiz, sip_header_t const *h, int f)
Pekka Pessi's avatar
Pekka Pessi committed
1574 1575 1576 1577 1578 1579 1580
{
  sip_date_t const *date = h->sh_date;

  return msg_date_e(b, bsiz, date->d_time);
}

/**@ingroup sip_date
1581
 * @brief Create an @Date header object. 
Pekka Pessi's avatar
Pekka Pessi committed
1582
 *
1583
 * Create a @Date header object with
Pekka Pessi's avatar
Pekka Pessi committed
1584 1585 1586 1587 1588 1589 1590
 * the date @a date.  If @date is 0, current time (as returned by sip_now())
 * is used.
 *
 * @param home   memory home
 * @param date   date expressed as seconds since Mon, 01 Jan 1900 00:00:00
 *
 * @return
1591 1592
 * A pointer to newly created @Date header object when successful, or NULL
 * upon an error.
Pekka Pessi's avatar
Pekka Pessi committed
1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611
 */
sip_date_t *sip_date_create(su_home_t *home, sip_time_t date)
{
  sip_header_t *h = sip_header_alloc(home, sip_date_class, 0);
  
  if (h) {
    if (date == 0)
      date = sip_now();
    h->sh_date->d_time = date;
  }

  return h->sh_date;
}

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

/**@SIP_HEADER sip_expires Expires Header
 *
 * The Expires header field gives the date and time after which the message
1612
 * content expires.  Its syntax is defined in @RFC3261 as follows:
Pekka Pessi's avatar
Pekka Pessi committed
1613 1614 1615 1616 1617
 * 
 * @code
 *    Expires     =  "Expires" HCOLON delta-seconds
 * @endcode
 * 
1618
 * Note that the first SIP revision (@RFC2543) also allowed absolute time in
Pekka Pessi's avatar
Pekka Pessi committed
1619
 * Expires.
1620 1621
 *
 * The parsed Expires header is stored in #sip_expires_t structure.
Pekka Pessi's avatar
Pekka Pessi committed
1622 1623 1624 1625 1626
 */

/**@ingroup sip_expires
 * @typedef typedef struct sip_expires_s sip_expires_t;
 *
1627
 * The structure #sip_expires_t contains representation of SIP @Expires
Pekka Pessi's avatar
Pekka Pessi committed
1628 1629 1630 1631 1632 1633
 * header.
 *
 * The #sip_expires_t is defined as follows:
 * @code
 * typedef struct sip_expires_s {
 *   sip_common_t        ex_common[1];  // Common fragment info
1634
 *   sip_error_t        *ex_next;       // Link to next (dummy)
Pekka Pessi's avatar
Pekka Pessi committed
1635 1636 1637 1638 1639 1640 1641 1642 1643
 *   sip_time_t          ex_date;       // Seconds since Jan 1, 1900
 *   sip_time_t          ex_delta;      // ...or delta seconds
 * } sip_expires_t;
 * @endcode
 */

msg_hclass_t sip_expires_class[] = 
SIP_HEADER_CLASS(expires, "Expires", "", ex_common, single, any);

1644
issize_t sip_expires_d(su_home_t *home, sip_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
{
  sip_expires_t *expires = h->sh_expires;

  if (msg_date_delta_d((char const **)&s, 
		       &expires->ex_date, 
		       &expires->ex_delta) < 0 || *s)
    return -1;
  else
    return 0;
}

1656
issize_t sip_expires_e(char b[], isize_t bsiz, sip_header_t const *h, int f)
Pekka Pessi's avatar
Pekka Pessi committed
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666
{
  sip_expires_t const *expires = h->sh_expires;

  if (expires->ex_date)
    return msg_date_e(b, bsiz, expires->ex_date + expires->ex_delta);
  else
    return msg_delta_e(b, bsiz, expires->ex_delta);
}

/**@ingroup sip_expires
1667
 * @brief Create an @Expires header object. 
Pekka Pessi's avatar
Pekka Pessi committed
1668
 *
1669
 * Create an @Expires header object with the expiration time @a delta.
Pekka Pessi's avatar
Pekka Pessi committed
1670
 *
1671
 * @param home   memory home used to allocate #sip_expires_t structure
Pekka Pessi's avatar
Pekka Pessi committed
1672 1673 1674
 * @param delta  relative expiration time in seconds
 *
 * @return
1675 1676
 * A pointer to newly created @Expires header object when successful or NULL
 * upon an error.
Pekka Pessi's avatar
Pekka Pessi committed
1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692
 */
sip_expires_t *sip_expires_create(su_home_t *home, sip_time_t delta)
{
  sip_header_t *h = sip_header_alloc(home, sip_expires_class, 0);

  if (h)
    h->sh_expires->ex_delta = delta;

  return h->sh_expires;
}

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

/**@SIP_HEADER sip_from From Header
 *
 * The From header indicates the initiator of the request.  It is defined in
1693
 * @RFC3261 as follows:
Pekka Pessi's avatar
Pekka Pessi committed
1694 1695 1696 1697 1698 1699 1700 1701 1702
 * 
 * @code
 *    From        =  ( "From" / "f" ) HCOLON from-spec
 *    from-spec   =  ( name-addr / addr-spec )
 *                   *( SEMI from-param )
 *    from-param  =  tag-param / generic-param
 *    tag-param   =  "tag" EQUAL token
 * @endcode
 *
1703 1704
 *
 * The parsed From header is stored in #sip_from_t structure.
Pekka Pessi's avatar
Pekka Pessi committed
1705 1706 1707 1708 1709
 */

/**@ingroup sip_from
 * @typedef typedef struct sip_addr_s sip_from_t;
 *
1710
 * The structure #sip_from_t contains representation of @From header.
Pekka Pessi's avatar
Pekka Pessi committed
1711
 *
1712
 * The #sip_from_t is defined as follows:
Pekka Pessi's avatar
Pekka Pessi committed
1713 1714 1715
 * @code
 * typedef struct sip_addr_s {
 *   sip_common_t       a_common[1];    // Common fragment info
1716
 *   sip_error_t       *a_next;         // Link to next
Pekka Pessi's avatar
Pekka Pessi committed
1717 1718
 *   char const        *a_display;      // Display name
 *   url_t              a_url[1];       // URL
Pekka Pessi's avatar
Pekka Pessi committed
1719
 *   msg_param_t const *a_params;       // List of from-param
Pekka Pessi's avatar
Pekka Pessi committed
1720 1721 1722 1723 1724 1725 1726 1727 1728 1729
 *   char const        *a_comment;      // Comment
 *   char const        *a_tag;          // Tag parameter 
 * } sip_from_t;
 * @endcode
 *
 */

msg_hclass_t sip_from_class[] = 
SIP_HEADER_CLASS(from, "From", "f", a_params, single, addr);

1730 1731 1732 1733
issize_t sip_from_d(su_home_t *home,
		    sip_header_t *h,
		    char *s,
		    isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
1734 1735 1736 1737
{
  return sip_addr_d(home, h, s, slen);
}

1738
issize_t sip_from_e(char b[], isize_t bsiz, sip_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
1739 1740 1741 1742 1743 1744 1745
{
  assert(sip_is_from(h));
  return sip_addr_e(b, bsiz, h, flags);
}

/**@ingroup sip_from
 *
1746
 * Create a @From header object with URL.
Pekka Pessi's avatar
Pekka Pessi committed
1747
 *
1748
 * @param home      memory home used to allocate #sip_from_t structure
Pekka Pessi's avatar
Pekka Pessi committed
1749