msg_basic.c 9.79 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
/* ====================================================================== */

/**@ingroup msg_headers
 * @defgroup msg_error Erroneous Headers
 *
 * The erroneous headers are stored in #msg_error_t structure.
64
 *
Pekka Pessi's avatar
Pekka Pessi committed
65 66 67 68 69 70 71 72 73 74 75
 * @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
{
  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.
114
 *
Pekka Pessi's avatar
Pekka Pessi committed
115 116 117 118 119 120 121 122 123 124 125 126 127
 * @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
{
  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;
142

Pekka Pessi's avatar
Pekka Pessi committed
143 144 145
  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
{
  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);
156

Pekka Pessi's avatar
Pekka Pessi committed
157 158 159
  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

  return b;
}

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

/**@ingroup msg_headers
 * @defgroup msg_payload Message Body
 *
 * The payload object contains the message body. The message body has no
187
 * structure, but it is stored in the @a pl_data buffer as a byte array.
Pekka Pessi's avatar
Pekka Pessi committed
188 189 190 191 192 193 194 195 196 197 198 199
 * 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
200
 *   msg_header_t   *pl_next;           // Next payload object
Pekka Pessi's avatar
Pekka Pessi committed
201
 *   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
{
  msg_header_t *h = msg_header_alloc(home, msg_payload_class, len + 1);

216 217
  if (h) {
    msg_payload_t *pl = (msg_payload_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
218
    char *b = msg_header_data(h->sh_common);
219

Pekka Pessi's avatar
Pekka Pessi committed
220 221 222 223 224 225 226 227 228 229 230
    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;
  }
231

Pekka Pessi's avatar
Pekka Pessi committed
232 233 234 235
  return NULL;
}

/** Parse payload. */
236
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
237
{
238 239 240 241
  msg_payload_t *pl = (msg_payload_t *)h;

  pl->pl_len = slen;
  pl->pl_data = s;
Pekka Pessi's avatar
Pekka Pessi committed
242 243 244 245 246 247 248

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

  return 0;
}

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

  if (bsiz > 0) {
255 256 257 258
    if (len < bsiz)
      memcpy(b, pl->pl_data, len), b[len] = '\0';
    else
      memcpy(b, pl->pl_data, bsiz - 1), b[bsiz - 1] = '\0';
Pekka Pessi's avatar
Pekka Pessi committed
259 260 261 262 263
  }

  return len;
}

264
isize_t msg_payload_dup_xtra(msg_header_t const *h, isize_t offset)
Pekka Pessi's avatar
Pekka Pessi committed
265
{
266 267
  msg_payload_t *pl = (msg_payload_t *)h;
  return offset + pl->pl_len + 1;
Pekka Pessi's avatar
Pekka Pessi committed
268 269 270 271
}

char *msg_payload_dup_one(msg_header_t *dst,
			  msg_header_t const *src,
272
			  char *b,
273
			  isize_t xtra)
Pekka Pessi's avatar
Pekka Pessi committed
274
{
275 276
  msg_payload_t *pl = (msg_payload_t *)dst;
  msg_payload_t const *o = (msg_payload_t const *)src;
Pekka Pessi's avatar
Pekka Pessi committed
277 278 279

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

280 281
  dst->sh_data = pl->pl_data;
  dst->sh_len = pl->pl_len;
Pekka Pessi's avatar
Pekka Pessi committed
282 283 284 285 286 287

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

  return b + pl->pl_len + 1;
}

288
usize_t msg_payload_length(msg_payload_t const *pl)
Pekka Pessi's avatar
Pekka Pessi committed
289 290 291 292 293 294 295 296
{
  /* XXX */
  return 0;
}

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

/**@ingroup msg_headers
297
 * @defgroup msg_separator Message Separator
Pekka Pessi's avatar
Pekka Pessi committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
 *
 * 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[] =
321 322
MSG_HEADER_CLASS(msg_, separator, NULL, "", sep_common, single,
		 msg_default, msg_generic);
Pekka Pessi's avatar
Pekka Pessi committed
323

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

/** Parse a separator line. */
328
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
329 330
{
  int len = CRLF_TEST(s);
331
  msg_separator_t *sep = (msg_separator_t *)h;
Pekka Pessi's avatar
Pekka Pessi committed
332 333 334

  if (len == 0 && slen > 0)
    return -1;
335

336 337
  memcpy(sep->sep_data, s, len);
  sep->sep_data[len] = '\0';
Pekka Pessi's avatar
Pekka Pessi committed
338 339 340 341 342

  return 0;
}

/** Encode a separator line. */
343
issize_t msg_separator_e(char b[], isize_t bsiz, msg_header_t const *h, int flags)
Pekka Pessi's avatar
Pekka Pessi committed
344
{
345 346
  msg_separator_t const *sep = (msg_separator_t const *)h;
  size_t n = strlen(sep->sep_data);
Pekka Pessi's avatar
Pekka Pessi committed
347 348

  if (bsiz > n)
349
    strcpy(b, sep->sep_data);
Pekka Pessi's avatar
Pekka Pessi committed
350

351
  return (issize_t)n;
Pekka Pessi's avatar
Pekka Pessi committed
352 353 354 355
}

msg_separator_t *msg_separator_create(su_home_t *home)
{
356
  msg_separator_t *sep;
Pekka Pessi's avatar
Pekka Pessi committed
357

358
  sep = (msg_separator_t *)msg_header_alloc(home, msg_separator_class, 0);
Pekka Pessi's avatar
Pekka Pessi committed
359 360 361 362 363 364 365 366 367
  if (sep)
    strcpy(sep->sep_data, CRLF);

  return sep;
}

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