msg_basic.c 9.43 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3 4 5 6 7 8 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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355
/*
 * This file is part of the Sofia-SIP package
 *
 * Copyright (C) 2005 Nokia Corporation.
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
 * * This library is free software; you can redistribute it and/or
 * 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>
 *
 * @date  Created: Fri Feb 23 19:51:55 2001 ppessi
 * 
 * $Id: msg_basic.c,v 1.1.1.1 2005/07/20 20:35:22 kaiv Exp $
 */

#include "config.h"

const char msg_basic_id[] =
"$Id: msg_basic.c,v 1.1.1.1 2005/07/20 20:35:22 kaiv Exp $";

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>
#include <stdarg.h>

#include <su_alloc.h>

#include <msg.h>
#include <bnf.h>
#include <msg_parser.h>
#include <msg_header.h>

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

/**@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.
 */

int msg_error_dup_xtra(msg_header_t const *h, int offset);
char *msg_error_dup_one(msg_header_t *dst, msg_header_t const *src,
			  char *b, int xtra);

msg_hclass_t msg_error_class[] =
MSG_HEADER_CLASS(msg_, error, "", "", er_common, append, msg_error);

int msg_error_d(su_home_t *home, msg_header_t *h, char *s, int slen)
{
  return 0;
}

int msg_error_e(char b[], int bsiz, msg_header_t const *h, int flags)
{
  /* There is no way to encode an erroneous header */
  return 0;
}

int msg_error_dup_xtra(msg_header_t const *h, int offset)
{
  return msg_default_dup_xtra(h, offset);
}

char *msg_error_dup_one(msg_header_t *dst, msg_header_t const *src,
			  char *b, int xtra)
{
  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[] =
MSG_HEADER_CLASS(msg_, unknown, "", "", un_common, append, msg_unknown);

int msg_unknown_d(su_home_t *home, msg_header_t *h, char *s, int slen)
{
  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;
}

int msg_unknown_e(char b[], int bsiz, msg_header_t const *h, int flags)
{
  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;
}

int msg_unknown_dup_xtra(msg_header_t const *h, int offset)
{
  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,
			  char *b, int xtra)
{
  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);

  assert(b <= end);

  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
 *   unsigned        pl_len;            // Length of message payload
 * } msg_payload_t;
 * @endcode
 */

msg_hclass_t msg_payload_class[1] =
MSG_HEADER_CLASS(msg_, payload, NULL, "", pl_common, append, msg_payload);

/** Create a MIME payload */
msg_payload_t *msg_payload_create(su_home_t *home, void const *data, int len)
{
  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. */
int msg_payload_d(su_home_t *home, msg_header_t *h, char *s, int slen)
{
  h->sh_payload->pl_len = slen;
  h->sh_payload->pl_data = s;

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

  return 0;
}

int msg_payload_e(char b[], int bsiz, msg_header_t const *h, int flags)
{
  int len = h->sh_payload->pl_len;

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

  return len;
}

int msg_payload_dup_xtra(msg_header_t const *h, int offset)
{
  return offset + h->sh_payload->pl_len + 1;
}

char *msg_payload_dup_one(msg_header_t *dst,
			  msg_header_t const *src,
			  char *b, 
			  int xtra)
{
  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;
}

int msg_payload_length(msg_payload_t const *pl)
{
  /* 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[] =
MSG_HEADER_CLASS(msg_, separator, NULL, "", sep_common, single, msg_default);

/** Calculate length of line ending (0, 1 or 2) */
#define CRLF_TEST(s) ((s[0]) == '\r' ? ((s[1]) == '\n') + 1 : (s[0])=='\n')

/** Parse a separator line. */
int msg_separator_d(su_home_t *home, msg_header_t *h, char *s, int slen)
{
  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. */
int msg_separator_e(char b[], int bsiz, msg_header_t const *h, int flags)
{
  int n = strlen(h->sh_separator->sep_data);

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

  return n;
}

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

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