sofia-sip/msg_parser.h

Go to the documentation of this file.
00001 /*
00002  * This file is part of the Sofia-SIP package
00003  *
00004  * Copyright (C) 2005 Nokia Corporation.
00005  *
00006  * Contact: Pekka Pessi <pekka.pessi@nokia-email.address.hidden>
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public License
00010  * as published by the Free Software Foundation; either version 2.1 of
00011  * the License, or (at your option) any later version.
00012  *
00013  * This library is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00021  * 02110-1301 USA
00022  *
00023  */
00024 
00025 #ifndef MSG_PARSER_H
00026 
00027 #define MSG_PARSER_H 
00028 
00040 #ifndef SU_ALLOC_H
00041 #include <sofia-sip/su_alloc.h>
00042 #endif
00043 #ifndef MSG_HEADER_H
00044 #include <sofia-sip/msg_header.h>
00045 #endif
00046 #ifndef BNF_H
00047 #include <sofia-sip/bnf.h>
00048 #endif
00049 #ifndef URL_H
00050 #include <sofia-sip/url.h>
00051 #endif
00052 
00053 SOFIA_BEGIN_DECLS
00054 
00055 /* ---------------------------------------------------------------------------
00056  * 1) Header class definitions.
00057  */
00058 
00059 #if HAVE_STRUCT_KEYWORDS
00060 
00061 #define MSG_HEADER_CLASS(pr, c, l, s, params, kind, dup, upd)   \
00062   {{                                                            \
00063     hc_hash:    pr##c##_hash,                                   \
00064     hc_parse:   pr##c##_d,                                      \
00065     hc_print:   pr##c##_e,                                      \
00066     hc_dxtra:   dup##_dup_xtra,                                 \
00067     hc_dup_one: dup##_dup_one,                                  \
00068     hc_update:  upd##_update,                                   \
00069     hc_name:    l,                                              \
00070     hc_len:     sizeof(l) - 1,                                  \
00071     hc_short:   s,                                              \
00072     hc_size:    MSG_ALIGN(sizeof(pr##c##_t), sizeof(void*)),    \
00073     hc_params:  offsetof(pr##c##_t, params),                    \
00074     hc_kind:    msg_kind_##kind,                                \
00075   }}
00076 #else
00077 #define MSG_HEADER_CLASS(pr, c, l, s, params, kind, dup, upd)   \
00078   {{ \
00079      pr##c##_hash, \
00080      pr##c##_d, \
00081      pr##c##_e, \
00082      dup##_dup_xtra, \
00083      dup##_dup_one, \
00084      upd##_update, \
00085      l, \
00086      sizeof(l) - 1, \
00087      s, \
00088      MSG_ALIGN(sizeof(pr##c##_t), sizeof(void*)), \
00089      offsetof(pr##c##_t, params), \
00090      msg_kind_##kind, \
00091   }}
00092 #endif
00093 
00094 /* Mark headers critical for understanding the message */
00095 #define msg_kind_single_critical msg_kind_single, 1
00096 #define msg_kind_list_critical   msg_kind_list, 1
00097 
00098 SOFIAPUBFUN issize_t msg_extract_header(msg_t *msg, msg_pub_t *mo,
00099                                    char b[], isize_t bsiz, int eos);
00100 SOFIAPUBFUN issize_t msg_extract_separator(msg_t *msg, msg_pub_t *mo,
00101                                            char b[], isize_t bsiz, int eos);
00102 SOFIAPUBFUN issize_t msg_extract_payload(msg_t *msg, msg_pub_t *mo, 
00103                                          msg_header_t **return_payload, 
00104                                          usize_t body_len,
00105                                          char b[], isize_t bsiz, int eos);
00106 
00107 /* ---------------------------------------------------------------------------
00108  * 2) Header processing methods for common headers.
00109  */
00110 
00111 SOFIAPUBFUN int msg_firstline_d(char *s, char **ss2, char **ss3);
00112 
00113 SOFIAPUBFUN isize_t msg_default_dup_xtra(msg_header_t const *header, isize_t offset);
00114 SOFIAPUBFUN char *msg_default_dup_one(msg_header_t *dst, 
00115                                       msg_header_t const *src,
00116                                       char *b, 
00117                                       isize_t xtra);
00118 
00119 SOFIAPUBFUN issize_t msg_numeric_d(su_home_t *, msg_header_t *h, char *s, isize_t slen);
00120 SOFIAPUBFUN issize_t msg_numeric_e(char [], isize_t, msg_header_t const *, int);
00121 
00122 SOFIAPUBFUN issize_t msg_list_d(su_home_t *, msg_header_t *h, char *s, isize_t slen);
00123 SOFIAPUBFUN issize_t msg_list_e(char [], isize_t, msg_header_t const *, int);
00124 SOFIAPUBFUN isize_t msg_list_dup_xtra(msg_header_t const *h, isize_t offset);
00125 SOFIAPUBFUN char *msg_list_dup_one(msg_header_t *dst,
00126                                    msg_header_t const *src,
00127                                    char *b, isize_t xtra);
00128 
00129 SOFIAPUBFUN issize_t msg_generic_d(su_home_t *, msg_header_t *, char *, isize_t);
00130 SOFIAPUBFUN issize_t msg_generic_e(char [], isize_t, msg_header_t const *, int);
00131 SOFIAPUBFUN isize_t msg_generic_dup_xtra(msg_header_t const *h, isize_t offset);
00132 SOFIAPUBFUN char *msg_generic_dup_one(msg_header_t *dst,
00133                                       msg_header_t const *src,
00134                                       char *b, 
00135                                       isize_t xtra);
00136 
00137 SOFIAPUBFUN isize_t msg_unknown_dup_xtra(msg_header_t const *h, isize_t offset);
00138 SOFIAPUBFUN char *msg_unknown_dup_one(msg_header_t *dst,
00139                                       msg_header_t const *src,
00140                                       char *b, isize_t xtra);
00141 
00142 SOFIAPUBFUN isize_t msg_error_dup_xtra(msg_header_t const *h, isize_t offset);
00143 SOFIAPUBFUN char *msg_error_dup_one(msg_header_t *dst,
00144                                     msg_header_t const *src,
00145                                     char *b, isize_t xtra);
00146 
00147 SOFIAPUBFUN issize_t msg_payload_d(su_home_t *, msg_header_t *h, char *s, isize_t slen);
00148 SOFIAPUBFUN issize_t msg_payload_e(char b[], isize_t bsiz, msg_header_t const *, int f);
00149 SOFIAPUBFUN isize_t msg_payload_dup_xtra(msg_header_t const *h, isize_t offset);
00150 SOFIAPUBFUN char *msg_payload_dup_one(msg_header_t *dst,
00151                                       msg_header_t const *src,
00152                                       char *b, isize_t xtra);
00153 
00154 SOFIAPUBFUN issize_t msg_separator_d(su_home_t *, msg_header_t *, char *, isize_t);
00155 SOFIAPUBFUN issize_t msg_separator_e(char [], isize_t, msg_header_t const *, int);
00156 
00157 SOFIAPUBFUN issize_t msg_auth_d(su_home_t *, msg_header_t *h, char *s, isize_t slen);
00158 SOFIAPUBFUN issize_t msg_auth_e(char b[], isize_t bsiz, msg_header_t const *h, int f);
00159 SOFIAPUBFUN isize_t msg_auth_dup_xtra(msg_header_t const *h, isize_t offset);
00160 SOFIAPUBFUN char *msg_auth_dup_one(msg_header_t *dst, msg_header_t const *src, 
00161                                    char *b, isize_t xtra);
00162 
00163 /* ---------------------------------------------------------------------------
00164  * 2) Macros and prototypes for building header decoding/encoding functions.
00165  */
00166 
00167 #define MSG_HEADER_DATA(h) ((char *)(h) + (h)->sh_class->hc_size)
00168 
00169 #define MSG_HEADER_TEST(h) ((h) && (h)->sh_class)
00170 
00171 su_inline void *msg_header_data(msg_frg_t *h);
00172 
00173 SOFIAPUBFUN int msg_hostport_d(char **ss,
00174                                char const **return_host,
00175                                char const **return_port);
00176 
00177 SOFIAPUBFUN issize_t msg_token_d(char **ss, char const **return_token);
00178 SOFIAPUBFUN issize_t msg_uint32_d(char **ss, uint32_t *return_value);
00179 SOFIAPUBFUN issize_t msg_comment_d(char **ss, char const **return_comment);
00180 SOFIAPUBFUN issize_t msg_quoted_d(char **ss, char **return_unquoted);
00181 SOFIAPUBFUN issize_t msg_unquoted_e(char *b, isize_t bsiz, char const *s);
00182 
00183 SOFIAPUBFUN issize_t msg_parse_next_field(su_home_t *home, msg_header_t *prev,
00184                                           char *s, isize_t slen);
00185 
00187 #define MSG_TERM_E(p, e) ((p) < (e) ? (p)[0] = '\0' : '\0')
00188 
00190 #define MSG_CHAR_E(p, e, c) (++(p) < (e) ? ((p)[-1]=(c)) : (c)) 
00191 
00193 #define MSG_STRING_LEN(s, sep_size) ((s) ? (strlen(s) + sep_size) : 0)
00194 
00196 #define MSG_STRING_E(p, e, s) do { \
00197   size_t _n = strlen(s); if (p + _n+1 < e) memcpy(p, s, _n+1); p+= _n; } while(0)
00198 
00200 #define MSG_STRING_DUP(p, d, s) \
00201   (void)((s)?((p)=(char*)memccpy((void *)((d)=(char*)p),(s),0,SIZE_MAX))\
00202             :((d)=NULL))
00203 
00205 #define MSG_STRING_SIZE(s) ((s) ? (strlen(s) + 1) : 0)
00206 
00207 SOFIAPUBFUN issize_t msg_commalist_d(su_home_t *, char **ss,
00208                                      msg_param_t **append_list,
00209                                      issize_t (*scanner)(char *s));
00210 SOFIAPUBFUN issize_t msg_token_scan(char *start);
00211 SOFIAPUBFUN issize_t msg_attribute_value_scanner(char *s);
00212 
00213 SOFIAPUBFUN issize_t msg_any_list_d(su_home_t *, char **ss, 
00214                                     msg_param_t **append_list,
00215                                     issize_t (*scanner)(char *s),
00216                                     int sep);
00217 
00219 #define MSG_COMMALIST_E(b, end, params, compact) do { \
00220   char const * const *p_; char const * c_ = ""; \
00221   for (p_ = (params); p_ && *p_; p_++, c_ = (compact ? "," : ", ")) \
00222     { MSG_STRING_E(b, (end), c_); MSG_STRING_E(b, (end), *p_); } \
00223 } while(0)
00224 
00225 /* Parameter lists */
00226 
00227 SOFIAPUBFUN int msg_header_update_params(msg_common_t *h, int clear);
00228 
00230 #define MSG_PARAM_MATCH(v, s, name) \
00231   (strncasecmp(s, name "=", sizeof(name)) == 0 ? (v = s + sizeof(name)) : NULL)
00232 
00234 #define MSG_PARAM_MATCH_P(v, s, name) \
00235   ((strncasecmp((s), name "", sizeof(name) - 1) == 0 &&                 \
00236     ((s)[sizeof(name) - 1] == '=' || (s)[sizeof(name) - 1] == '\0')) ? \
00237    ((v) = 1) : 0)
00238 
00240 #define MSG_PARAMS_NUM(n) (((n) + MSG_N_PARAMS - 1) & (size_t)(0 - MSG_N_PARAMS))
00241 
00243 SOFIAPUBFUN issize_t msg_avlist_d(su_home_t *, char **ss,
00244                                   msg_param_t const **return_params);
00245 
00247 SOFIAPUBFUN issize_t msg_params_d(su_home_t *, char **ss,
00248                                   msg_param_t const **return_params);
00249 
00251 SOFIAPUBFUN isize_t msg_params_e(char b[], isize_t bsiz, msg_param_t const pparams[]);
00252 
00254 SOFIAPUBFUN issize_t msg_params_join(su_home_t *,
00255                                      msg_param_t **dst,
00256                                      msg_param_t const *src,
00257                                      unsigned prune,
00258                                      int dup);
00259 
00261 #define MSG_PARAMS_E(b, end, params, flags) \
00262   (b) += msg_params_e((b), (size_t)((b) < (end) ? (end) - (b) : 0), (params))
00263 
00265 #define MSG_PARAMS_SIZE(rv, params) (rv = msg_params_dup_xtra(params, rv))
00266 
00268 SOFIAPUBFUN char *msg_params_dup(msg_param_t const **d, msg_param_t const *s, 
00269                                  char *b, isize_t xtra);
00270 
00272 su_inline isize_t msg_params_count(msg_param_t const params[])
00273 {
00274   if (params) {
00275     size_t n;
00276     for (n = 0; params[n]; n++)
00277       ;
00278     return n;
00279   }
00280   else {
00281     return 0;
00282   }
00283 }
00284 
00286 su_inline isize_t msg_params_dup_xtra(msg_param_t const params[], isize_t offset)
00287 {
00288   isize_t n = msg_params_count(params);
00289   if (n) {
00290     MSG_STRUCT_SIZE_ALIGN(offset);
00291     offset += MSG_PARAMS_NUM(n + 1) * sizeof(msg_param_t);
00292     for (n = 0; params[n]; n++)
00293       offset += strlen(params[n]) + 1;
00294   }
00295   return offset;
00296 }
00297 
00299 su_inline void *msg_header_data(msg_frg_t *h)
00300 {
00301   if (h)
00302     return (char *)h + h->h_class->hc_size;
00303   else
00304     return NULL;
00305 }
00306 
00307 SOFIA_END_DECLS
00308 
00309 #endif 

Sofia-SIP 1.12.4 - Copyright (C) 2006 Nokia Corporation. All rights reserved. Licensed under the terms of the GNU Lesser General Public License.