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 int msg_extract_header(msg_t *msg, msg_pub_t *mo,
00099                                    char b[], int bsiz, int eos);
00100 SOFIAPUBFUN int msg_extract_separator(msg_t *msg, msg_pub_t *mo,
00101                                       char b[], int bsiz, int eos);
00102 SOFIAPUBFUN int msg_extract_payload(msg_t *msg, msg_pub_t *mo, 
00103                                     msg_header_t **return_payload, 
00104                                     unsigned body_len,
00105                                     char b[], int 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 int msg_default_dup_xtra(msg_header_t const *header, int offset);
00114 SOFIAPUBFUN char *msg_default_dup_one(msg_header_t *dst, 
00115                                       msg_header_t const *src,
00116                                       char *b, 
00117                                       int xtra);
00118 
00119 SOFIAPUBFUN int msg_numeric_d(su_home_t *, msg_header_t *h, char *s, int slen);
00120 SOFIAPUBFUN int msg_numeric_e(char [], int, msg_header_t const *, int);
00121 
00122 SOFIAPUBFUN int msg_list_d(su_home_t *, msg_header_t *h, char *s, int slen);
00123 SOFIAPUBFUN int msg_list_e(char [], int, msg_header_t const *, int);
00124 SOFIAPUBFUN int msg_list_dup_xtra(msg_header_t const *h, int offset);
00125 SOFIAPUBFUN char *msg_list_dup_one(msg_header_t *dst,
00126                                    msg_header_t const *src,
00127                                    char *b, int xtra);
00128 
00129 SOFIAPUBFUN int msg_generic_d(su_home_t *, msg_header_t *, char *, int n);
00130 SOFIAPUBFUN int msg_generic_e(char [], int, msg_header_t const *, int);
00131 SOFIAPUBFUN int msg_generic_dup_xtra(msg_header_t const *h, int offset);
00132 SOFIAPUBFUN char *msg_generic_dup_one(msg_header_t *dst,
00133                                       msg_header_t const *src,
00134                                       char *b, 
00135                                       int xtra);
00136 
00137 SOFIAPUBFUN int msg_unknown_dup_xtra(msg_header_t const *h, int offset);
00138 SOFIAPUBFUN char *msg_unknown_dup_one(msg_header_t *dst,
00139                                       msg_header_t const *src,
00140                                       char *b, int xtra);
00141 
00142 SOFIAPUBFUN int msg_error_dup_xtra(msg_header_t const *h, int offset);
00143 SOFIAPUBFUN char *msg_error_dup_one(msg_header_t *dst,
00144                                     msg_header_t const *src,
00145                                     char *b, int xtra);
00146 
00147 SOFIAPUBFUN int msg_payload_d(su_home_t *, msg_header_t *h, char *s, int slen);
00148 SOFIAPUBFUN int msg_payload_e(char b[], int bsiz, msg_header_t const *, int f);
00149 SOFIAPUBFUN int msg_payload_dup_xtra(msg_header_t const *h, int offset);
00150 SOFIAPUBFUN char *msg_payload_dup_one(msg_header_t *dst,
00151                                       msg_header_t const *src,
00152                                       char *b, int xtra);
00153 
00154 SOFIAPUBFUN int msg_separator_d(su_home_t *, msg_header_t *, char *, int);
00155 SOFIAPUBFUN int msg_separator_e(char [], int, msg_header_t const *, int);
00156 
00157 SOFIAPUBFUN int msg_auth_d(su_home_t *, msg_header_t *h, char *s, int slen);
00158 SOFIAPUBFUN int msg_auth_e(char b[], int bsiz, msg_header_t const *h, int f);
00159 SOFIAPUBFUN int msg_auth_dup_xtra(msg_header_t const *h, int offset);
00160 SOFIAPUBFUN char *msg_auth_dup_one(msg_header_t *dst, msg_header_t const *src, 
00161                                    char *b, int 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 int msg_token_d(char **ss, char const **return_token);
00178 SOFIAPUBFUN int msg_uint32_d(char **ss, uint32_t *return_value);
00179 SOFIAPUBFUN int msg_comment_d(char **ss, char const **return_comment);
00180 SOFIAPUBFUN int msg_quoted_d(char **ss, char **return_unquoted);
00181 SOFIAPUBFUN int msg_unquoted_e(char *b, int bsiz, char const *s);
00182 
00184 #define MSG_TERM_E(p, e) ((p) < (e) ? (p)[0] = '\0' : '\0')
00185 
00187 #define MSG_CHAR_E(p, e, c) (++(p) < (e) ? ((p)[-1]=(c)) : (c)) 
00188 
00190 #define MSG_STRING_LEN(s, sep_size) ((s) ? (strlen(s) + sep_size) : 0)
00191 
00193 #define MSG_STRING_E(p, e, s) do { \
00194   int _n = strlen(s); if (p + _n+1 < e) memcpy(p, s, _n+1); p+= _n; } while(0)
00195 
00197 #define MSG_STRING_DUP(p, d, s) \
00198   (void)((s)?((p)=(char*)memccpy((void *)((d)=(char*)p),(s),0,0x7fffffff))\
00199             :((d)=NULL))
00200 
00202 #define MSG_STRING_SIZE(s) ((s) ? (strlen(s) + 1) : 0)
00203 
00204 SOFIAPUBFUN int msg_commalist_d(su_home_t *, char **ss,
00205                                 msg_param_t **return_list,
00206                                 int (*scanner)(char *s));
00207 SOFIAPUBFUN int msg_token_scan(char *start);
00208 SOFIAPUBFUN int msg_attribute_value_scanner(char *s);
00209 
00210 SOFIAPUBFUN int msg_any_list_d(su_home_t *, char **ss, 
00211                                msg_param_t **retval,
00212                                int (*scanner)(char *s), int sep);
00213 
00215 #define MSG_COMMALIST_E(b, end, params, compact) do { \
00216   char const * const *p_; char const * c_ = ""; \
00217   for (p_ = (params); p_ && *p_; p_++, c_ = (compact ? "," : ", ")) \
00218     { MSG_STRING_E(b, (end), c_); MSG_STRING_E(b, (end), *p_); } \
00219 } while(0)
00220 
00221 /* Parameter lists */
00222 
00223 SOFIAPUBFUN int msg_header_update_params(msg_common_t *h, int clear);
00224 
00226 #define MSG_PARAM_MATCH(v, s, name) \
00227   (strncasecmp(s, name "=", sizeof(name)) == 0 ? (v = s + sizeof(name)) : NULL)
00228 
00230 #define MSG_PARAM_MATCH_P(v, s, name) \
00231   ((strncasecmp((s), name "", sizeof(name) - 1) == 0 &&                 \
00232     ((s)[sizeof(name) - 1] == '=' || (s)[sizeof(name) - 1] == '\0')) ? \
00233    ((v) = 1) : 0)
00234 
00236 #define MSG_PARAMS_NUM(n) ((((n) + MSG_N_PARAMS - 1 )&-MSG_N_PARAMS))
00237 
00239 SOFIAPUBFUN int msg_avlist_d(su_home_t *, char **ss,
00240                              msg_param_t const **return_params);
00241 
00243 SOFIAPUBFUN int msg_params_d(su_home_t *, char **ss,
00244                              msg_param_t const **return_params);
00245 
00247 SOFIAPUBFUN int msg_params_e(char b[], int bsiz, msg_param_t const pparams[]);
00248 
00250 SOFIAPUBFUN int msg_params_join(su_home_t *,
00251                                 msg_param_t **dst,
00252                                 msg_param_t const *src,
00253                                 unsigned prune,
00254                                 int dup);
00255 
00257 #define MSG_PARAMS_E(b, end, params, flags) \
00258   b += msg_params_e(b, b < end ? end - b : 0, params)
00259 
00261 #define MSG_PARAMS_SIZE(rv, params) (rv = msg_params_dup_xtra(params, rv))
00262 
00264 SOFIAPUBFUN char *msg_params_dup(msg_param_t const **d, msg_param_t const *s, 
00265                                  char *b, int xtra);
00266 
00268 su_inline int msg_params_count(msg_param_t const params[])
00269 {
00270   if (params) {
00271     int n;
00272     for (n = 0; params[n]; n++)
00273       ;
00274     return n;
00275   }
00276   else {
00277     return 0;
00278   }
00279 }
00280 
00282 su_inline int msg_params_dup_xtra(msg_param_t const params[], int offset)
00283 {
00284   int n = msg_params_count(params);
00285   if (n) {
00286     MSG_STRUCT_SIZE_ALIGN(offset);
00287     offset += MSG_PARAMS_NUM(n + 1) * sizeof(msg_param_t);
00288     for (n = 0; params[n]; n++)
00289       offset += strlen(params[n]) + 1;
00290   }
00291   return offset;
00292 }
00293 
00295 su_inline void *msg_header_data(msg_frg_t *h)
00296 {
00297   if (h)
00298     return (char *)h + h->h_class->hc_size;
00299   else
00300     return NULL;
00301 }
00302 
00303 SOFIA_END_DECLS
00304 
00305 #endif 

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