msg_basic.c 9.63 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 28 29 30 31 32 33 34 35
 * 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
 *
 */

/**@ingroup msg_headers
 * @CFILE msg_basic.c
 * @brief Basic header handling.
 *
 * This file contains implementation of basic headers, that is, generic
 * headers like Subject or Organization containing non-structured text only,
 * numeric headers like Content-Length or Max-Forwards containing only an
 * 32-bit unsigned integer, or token list headers like Supported or Allow.
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 *
Pekka Pessi's avatar
Pekka Pessi committed
36
 * @date Created: Fri Feb 23 19:51:55 2001 ppessi
Pekka Pessi's avatar
Pekka Pessi committed
37 38 39 40
 */

#include "config.h"

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

43 44 45 46
#include <sofia-sip/msg.h>
#include <sofia-sip/bnf.h>
#include <sofia-sip/msg_parser.h>
#include <sofia-sip/msg_header.h>
Pekka Pessi's avatar
Pekka Pessi committed
47

48 49 50 51 52 53 54 55
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <stdarg.h>
#include <limits.h>

56 57
#define msg_generic_update NULL

Pekka Pessi's avatar
Pekka Pessi committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
/* ====================================================================== */

/**@ingroup msg_headers
 * @defgroup msg_error Erroneous Headers
 *
 * The erroneous headers are stored in #msg_error_t structure.
 * 
 * @note Parser may put other headers (like duplicate Content-Length
 * headers) into the list of erroneous headers. If the list of erroneous
 * headers is processed, the header type must be validated first by calling
 * msg_is_error() (or by other relevant tests).
 */

/**@ingroup msg_error
 * @typedef typedef struct msg_error_s msg_error_t;
 * Type for erroneous headers.
 */

76
isize_t msg_error_dup_xtra(msg_header_t const *h, isize_t offset);
Pekka Pessi's avatar
Pekka Pessi committed
77
char *msg_error_dup_one(msg_header_t *dst, msg_header_t const *src,
78
			  char *b, isize_t xtra);
Pekka Pessi's avatar
Pekka Pessi committed
79 80

msg_hclass_t msg_error_class[] =
81 82
MSG_HEADER_CLASS(msg_, error, "", "", er_common, append,
                 msg_error, msg_generic);
Pekka Pessi's avatar
Pekka Pessi committed
83

84
issize_t msg_error_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
85 86 87 88
{
  return 0;
}

89
issize_t msg_error_e(char b[], isize_t bsiz, msg_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
90 91 92 93 94
{
  /* There is no way to encode an erroneous header */
  return 0;
}

95
isize_t msg_error_dup_xtra(msg_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
96 97 98 99 100
{
  return msg_default_dup_xtra(h, offset);
}

char *msg_error_dup_one(msg_header_t *dst, msg_header_t const *src,
101
			  char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
{
  return msg_default_dup_one(dst, src, b, xtra);
}

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

/**@ingroup msg_headers
 * @defgroup msg_unknown Unknown Headers
 *
 * The unknown headers are handled with #msg_unknown_t structure. The whole
 * unknown header including its name is included in the header value string
 * @a g_value.
 * 
 * @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 not regocnized or parser, but they
 * are treated as unknown and put unparsed into the list of unknown headers.
 */

/**@ingroup msg_unknown
 * @typedef typedef struct msg_unknown_s msg_unknown_t;
 *
 * Type for unknown headers.
 */

msg_hclass_t msg_unknown_class[] =
128 129
MSG_HEADER_CLASS(msg_, unknown, "", "", un_common, append,
                 msg_unknown, msg_generic);
Pekka Pessi's avatar
Pekka Pessi committed
130

131
issize_t msg_unknown_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145
{
  msg_unknown_t *un = (msg_unknown_t *)h;

  if (msg_token_d(&s, &un->un_name) < 0 ||
      *s != ':')
    return -1;

  *s++ = '\0';
  skip_lws(&s);
  un->un_value = s;
					   
  return 0;
}

146
issize_t msg_unknown_e(char b[], isize_t bsiz, msg_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
147 148 149 150 151 152 153 154 155 156 157 158 159
{
  char *b0 = b, *end = b + bsiz;
  msg_unknown_t *un = (msg_unknown_t *)h;
  int const compact = MSG_IS_COMPACT(flags);

  MSG_STRING_E(b, end, un->un_name);
  MSG_CHAR_E(b, end, ':');
  if (!compact) MSG_CHAR_E(b, end, ' ');
  MSG_STRING_E(b, end, un->un_value);
  
  return b - b0;
}

160
isize_t msg_unknown_dup_xtra(msg_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
161 162 163 164 165 166
{
  msg_unknown_t const *un = (msg_unknown_t *)h;
  return offset + MSG_STRING_SIZE(un->un_name) + MSG_STRING_SIZE(un->un_value);
}

char *msg_unknown_dup_one(msg_header_t *dst, msg_header_t const *src,
167
			  char *b, isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
168 169 170 171 172 173 174 175
{
  msg_unknown_t *un = (msg_unknown_t *)dst;
  msg_unknown_t const *o = (msg_unknown_t *)src;
  char *end = b + xtra;

  MSG_STRING_DUP(b, un->un_name, o->un_name);
  MSG_STRING_DUP(b, un->un_value, o->un_value);

176
  assert(b <= end); (void)end;
Pekka Pessi's avatar
Pekka Pessi committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

  return b;
}

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

/**@ingroup msg_headers
 * @defgroup msg_payload Message Body
 *
 * The payload object contains the message body. The message body has no
 * structure, but it is stored in the @a pl_data buffer as a byte array. 
 * Multiple payload objects may be linked to a list.
 */

/**@ingroup msg_payload
 * @typedef typedef struct msg_payload_s msg_payload_t;
 *
 * The structure msg_payload_t contains representation of MIME message payload.
 *
 * The msg_payload_t is defined as follows:
 * @code
 * typedef struct msg_payload_s {
 *   msg_common_t    pl_common[1];      // Common fragment info
 *   msg_header_t   *pl_next;           // Next payload object 
 *   char           *pl_data;           // Data - may contain zero bytes
202
 *   usize_t         pl_len;            // Length of message payload
Pekka Pessi's avatar
Pekka Pessi committed
203 204 205 206 207
 * } msg_payload_t;
 * @endcode
 */

msg_hclass_t msg_payload_class[1] =
208 209
MSG_HEADER_CLASS(msg_, payload, NULL, "", pl_common, append,
		 msg_payload, msg_generic);
Pekka Pessi's avatar
Pekka Pessi committed
210 211

/** Create a MIME payload */
212
msg_payload_t *msg_payload_create(su_home_t *home, void const *data, usize_t len)
Pekka Pessi's avatar
Pekka Pessi committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
{
  msg_header_t *h = msg_header_alloc(home, msg_payload_class, len + 1);
  msg_payload_t *pl = h->sh_payload;

  if (pl) {
    char *b = msg_header_data(h->sh_common);
    if (data)
      memcpy(b, data, len);
    else
      memset(b, 0, len);
    b[len] = 0;

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

    return pl;
  }
  return NULL;
}

/** Parse payload. */
234
issize_t msg_payload_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
235 236 237 238 239 240 241 242 243 244
{
  h->sh_payload->pl_len = slen;
  h->sh_payload->pl_data = s;

  h->sh_len = slen;
  h->sh_data = s;

  return 0;
}

245
issize_t msg_payload_e(char b[], isize_t bsiz, msg_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
246
{
247
  size_t len = h->sh_payload->pl_len;
Pekka Pessi's avatar
Pekka Pessi committed
248 249 250 251 252 253 254 255 256

  if (bsiz > 0) {
    memcpy(b, h->sh_payload->pl_data, bsiz > len ? len : bsiz);
    b[bsiz > len ? len : bsiz - 1] = '\0';
  }

  return len;
}

257
isize_t msg_payload_dup_xtra(msg_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
258 259 260 261 262 263 264
{
  return offset + h->sh_payload->pl_len + 1;
}

char *msg_payload_dup_one(msg_header_t *dst,
			  msg_header_t const *src,
			  char *b, 
265
			  isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279
{
  msg_payload_t *pl = dst->sh_payload;
  msg_payload_t const *o = src->sh_payload;

  memcpy(pl->pl_data = b, o->pl_data, pl->pl_len = o->pl_len);

  pl->pl_common->h_data = pl->pl_data;
  pl->pl_common->h_len = pl->pl_len;

  pl->pl_data[pl->pl_len] = 0;	/* NUL terminate just in case */

  return b + pl->pl_len + 1;
}

280
usize_t msg_payload_length(msg_payload_t const *pl)
Pekka Pessi's avatar
Pekka Pessi committed
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
{
  /* XXX */
  return 0;
}

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

/**@ingroup msg_headers
 * @defgroup msg_separator Message Separator 
 *
 * An empty line separates headers from the message body. In order to avoid
 * modifying messages with integrity protection, the separator line has its
 * own header structure which is included in the msg_t structure.
 */

/**@ingroup msg_separator
 * @typedef typedef struct msg_separator_s msg_separator_t;
 *
 * The structure msg_separator_t contains representation of separator line
 * between message headers and body.
 *
 * The msg_separator_t is defined as follows:
 * @code
 * typedef struct msg_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
 * } msg_separator_t;
 * @endcode
 */

msg_hclass_t msg_separator_class[] =
313 314
MSG_HEADER_CLASS(msg_, separator, NULL, "", sep_common, single,
		 msg_default, msg_generic);
Pekka Pessi's avatar
Pekka Pessi committed
315

316
/** Calculate length of line ending (0, 1 or 2). @internal */
Pekka Pessi's avatar
Pekka Pessi committed
317 318 319
#define CRLF_TEST(s) ((s[0]) == '\r' ? ((s[1]) == '\n') + 1 : (s[0])=='\n')

/** Parse a separator line. */
320
issize_t msg_separator_d(su_home_t *home, msg_header_t *h, char *s, isize_t slen)
Pekka Pessi's avatar
Pekka Pessi committed
321 322 323 324 325 326 327 328 329 330 331 332 333
{
  int len = CRLF_TEST(s);

  if (len == 0 && slen > 0)
    return -1;
  
  memcpy(h->sh_separator->sep_data, s, len);
  h->sh_separator->sep_data[len] = '\0';

  return 0;
}

/** Encode a separator line. */
334
issize_t msg_separator_e(char b[], isize_t bsiz, msg_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
335
{
336
  size_t n = strlen(h->sh_separator->sep_data);
Pekka Pessi's avatar
Pekka Pessi committed
337 338 339 340

  if (bsiz > n)
    strcpy(b, h->sh_separator->sep_data);

341
  return (issize_t)n;
Pekka Pessi's avatar
Pekka Pessi committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
}

msg_separator_t *msg_separator_create(su_home_t *home)
{
  msg_separator_t *sep = 
    msg_header_alloc(home, msg_separator_class, 0)->sh_separator;

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

  return sep;
}

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