msg  1.13.17
msg_parser.h
Go to the documentation of this file.
1 /*
2  * This file is part of the Sofia-SIP package
3  *
4  * Copyright (C) 2005 Nokia Corporation.
5  *
6  * Contact: Pekka Pessi <pekka.pessi@nokia-email.address.hidden>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2.1 of
11  * the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21  * 02110-1301 USA
22  *
23  */
24 
25 #ifndef MSG_PARSER_H
27 #define MSG_PARSER_H
28 
40 #ifndef SU_ALLOC_H
41 #include <sofia-sip/su_alloc.h>
42 #endif
43 #ifndef MSG_HEADER_H
44 #include <sofia-sip/msg_header.h>
45 #endif
46 #ifndef BNF_H
47 #include <sofia-sip/bnf.h>
48 #endif
49 #ifndef URL_H
50 #include <sofia-sip/url.h>
51 #endif
52 
53 SOFIA_BEGIN_DECLS
54 
55 /* ---------------------------------------------------------------------------
56  * 1) Header class definitions.
57  */
58 
59 /* Do not use keywords until you fix msg_kind_foo_critical thing! */ \
60 #if HAVE_STRUCT_KEYWORDS && 0
62 #define MSG_HEADER_CLASS(pr, c, l, s, params, kind, dup, upd) \
63  {{ \
64  hc_hash: pr##c##_hash, \
65  hc_parse: pr##c##_d, \
66  hc_print: pr##c##_e, \
67  hc_dxtra: dup##_dup_xtra, \
68  hc_dup_one: dup##_dup_one, \
69  hc_update: upd##_update, \
70  hc_name: l, \
71  hc_len: sizeof(l) - 1, \
72  hc_short: s, \
73  hc_size: MSG_ALIGN(sizeof(pr##c##_t), sizeof(void*)), \
74  hc_params: offsetof(pr##c##_t, params), \
75  hc_kind: msg_kind_##kind, \
76  }}
77 #else
79 #define MSG_HEADER_CLASS(pr, c, l, s, params, kind, dup, upd) \
80  {{ \
81  pr##c##_hash, \
82  pr##c##_d, \
83  pr##c##_e, \
84  dup##_dup_xtra, \
85  dup##_dup_one, \
86  upd##_update, \
87  l, \
88  sizeof(l) - 1, \
89  s, \
90  MSG_ALIGN(sizeof(pr##c##_t), sizeof(void*)), \
91  offsetof(pr##c##_t, params), \
92  msg_kind_##kind, \
93  }}
94 #endif
95 
96 /* Mark headers critical for understanding the message */
97 #define msg_kind_single_critical msg_kind_single, 1
98 #define msg_kind_list_critical msg_kind_list, 1
99 
100 SOFIAPUBFUN issize_t msg_extract_header(msg_t *msg, msg_pub_t *mo,
101  char b[], isize_t bsiz, int eos);
103  char b[], isize_t bsiz, int eos);
105  msg_header_t **return_payload,
106  usize_t body_len,
107  char b[], isize_t bsiz, int eos);
108 
109 /* ---------------------------------------------------------------------------
110  * 2) Header processing methods for common headers.
111  */
112 
113 SOFIAPUBFUN int msg_firstline_d(char *s, char **ss2, char **ss3);
114 
115 SOFIAPUBFUN isize_t msg_default_dup_xtra(msg_header_t const *header, isize_t offset);
117  msg_header_t const *src,
118  char *b,
119  isize_t xtra);
120 
121 SOFIAPUBFUN issize_t msg_numeric_d(su_home_t *, msg_header_t *h, char *s, isize_t slen);
122 SOFIAPUBFUN issize_t msg_numeric_e(char [], isize_t, msg_header_t const *, int);
123 
124 SOFIAPUBFUN issize_t msg_list_d(su_home_t *, msg_header_t *h, char *s, isize_t slen);
125 SOFIAPUBFUN issize_t msg_list_e(char [], isize_t, msg_header_t const *, int);
126 SOFIAPUBFUN isize_t msg_list_dup_xtra(msg_header_t const *h, isize_t offset);
127 SOFIAPUBFUN char *msg_list_dup_one(msg_header_t *dst,
128  msg_header_t const *src,
129  char *b, isize_t xtra);
130 
131 SOFIAPUBFUN issize_t msg_generic_d(su_home_t *, msg_header_t *, char *, isize_t);
132 SOFIAPUBFUN issize_t msg_generic_e(char [], isize_t, msg_header_t const *, int);
133 SOFIAPUBFUN isize_t msg_generic_dup_xtra(msg_header_t const *h, isize_t offset);
135  msg_header_t const *src,
136  char *b,
137  isize_t xtra);
138 
139 SOFIAPUBFUN isize_t msg_unknown_dup_xtra(msg_header_t const *h, isize_t offset);
140 SOFIAPUBFUN char *msg_unknown_dup_one(msg_header_t *dst,
141  msg_header_t const *src,
142  char *b, isize_t xtra);
143 
144 SOFIAPUBFUN isize_t msg_error_dup_xtra(msg_header_t const *h, isize_t offset);
145 SOFIAPUBFUN char *msg_error_dup_one(msg_header_t *dst,
146  msg_header_t const *src,
147  char *b, isize_t xtra);
148 
149 SOFIAPUBFUN issize_t msg_payload_d(su_home_t *, msg_header_t *h, char *s, isize_t slen);
150 SOFIAPUBFUN issize_t msg_payload_e(char b[], isize_t bsiz, msg_header_t const *, int f);
151 SOFIAPUBFUN isize_t msg_payload_dup_xtra(msg_header_t const *h, isize_t offset);
152 SOFIAPUBFUN char *msg_payload_dup_one(msg_header_t *dst,
153  msg_header_t const *src,
154  char *b, isize_t xtra);
155 
156 SOFIAPUBFUN issize_t msg_separator_d(su_home_t *, msg_header_t *, char *, isize_t);
157 SOFIAPUBFUN issize_t msg_separator_e(char [], isize_t, msg_header_t const *, int);
158 
159 SOFIAPUBFUN issize_t msg_auth_d(su_home_t *, msg_header_t *h, char *s, isize_t slen);
160 SOFIAPUBFUN issize_t msg_auth_e(char b[], isize_t bsiz, msg_header_t const *h, int f);
161 SOFIAPUBFUN isize_t msg_auth_dup_xtra(msg_header_t const *h, isize_t offset);
163  char *b, isize_t xtra);
164 
165 /* ---------------------------------------------------------------------------
166  * 2) Macros and prototypes for building header decoding/encoding functions.
167  */
168 
169 #define MSG_HEADER_DATA(h) ((char *)(h) + (h)->sh_class->hc_size)
170 
171 #define MSG_HEADER_TEST(h) ((h) && (h)->sh_class)
172 
174 
175 SOFIAPUBFUN int msg_hostport_d(char **ss,
176  char const **return_host,
177  char const **return_port);
178 
179 SOFIAPUBFUN issize_t msg_token_d(char **ss, char const **return_token);
180 SOFIAPUBFUN issize_t msg_uint32_d(char **ss, uint32_t *return_value);
181 SOFIAPUBFUN issize_t msg_comment_d(char **ss, char const **return_comment);
182 SOFIAPUBFUN issize_t msg_quoted_d(char **ss, char **return_unquoted);
183 SOFIAPUBFUN issize_t msg_unquoted_e(char *b, isize_t bsiz, char const *s);
184 
186  char *s, isize_t slen);
187 
188 #define msg_parse_next_field_without_recursion() { \
189  msg_header_t *prev = h; \
190  msg_hclass_t *hc = prev->sh_class; \
191  char *end = s + slen; \
192  \
193  if (*s && *s != ',') \
194  return -1; \
195  \
196  if (msg_header_update_params(prev->sh_common, 0) < 0) \
197  return -1; \
198  \
199  while (*s == ',') \
200  *s = '\0', s += span_lws(s + 1) + 1; \
201  \
202  if (*s == 0) \
203  return 0; \
204  \
205  h = msg_header_alloc(home, hc, 0); \
206  if (!h) \
207  return -1; \
208  \
209  prev->sh_succ = h, h->sh_prev = &prev->sh_succ; \
210  prev->sh_next = h; \
211  slen = end - s; \
212  }
213 
214 
215 
217 #define MSG_TERM_E(p, e) ((p) < (e) ? (p)[0] = '\0' : '\0')
218 
220 #define MSG_CHAR_E(p, e, c) (++(p) < (e) ? ((p)[-1]=(c)) : (c))
221 
223 #define MSG_STRING_LEN(s, sep_size) ((s) ? (strlen(s) + sep_size) : 0)
224 
226 #define MSG_STRING_E(p, e, s) do { \
227  size_t _n = strlen(s); if (p + _n+1 < e) memcpy(p, s, _n+1); p+= _n; } while(0)
228 
230 #define MSG_STRING_DUP(p, d, s) \
231  (void)((s)?((p)=(char*)memccpy((void *)((d)=(char*)p),(s),0,INT_MAX))\
232  :((d)=NULL))
233 
234 /* Solaris has broken memccpy - it considers last argument as signed */
235 
237 #define MSG_STRING_SIZE(s) ((s) ? (strlen(s) + 1) : 0)
238 
239 SOFIAPUBFUN issize_t msg_commalist_d(su_home_t *, char **ss,
240  msg_param_t **append_list,
241  issize_t (*scanner)(char *s));
242 SOFIAPUBFUN issize_t msg_token_scan(char *start);
243 SOFIAPUBFUN issize_t msg_attribute_value_scanner(char *s);
244 
245 SOFIAPUBFUN issize_t msg_any_list_d(su_home_t *, char **ss,
246  msg_param_t **append_list,
247  issize_t (*scanner)(char *s),
248  int sep);
249 
251 #define MSG_COMMALIST_E(b, end, params, compact) do { \
252  char const * const *p_; char const * c_ = ""; \
253  for (p_ = (params); p_ && *p_; p_++, c_ = (compact ? "," : ", ")) \
254  { MSG_STRING_E(b, (end), c_); MSG_STRING_E(b, (end), *p_); } \
255 } while(0)
256 
257 /* Parameter lists */
258 
260 
262 #define MSG_PARAM_MATCH(v, s, name) \
263  (strncasecmp(s, name "=", sizeof(name)) == 0 ? (v = s + sizeof(name)) : NULL)
264 
266 #define MSG_PARAM_MATCH_P(v, s, name) \
267  ((strncasecmp((s), name "", sizeof(name) - 1) == 0 && \
268  ((s)[sizeof(name) - 1] == '=' || (s)[sizeof(name) - 1] == '\0')) ? \
269  ((v) = 1) : 0)
270 
272 #define MSG_PARAMS_NUM(n) (((n) + MSG_N_PARAMS - 1) & (size_t)(0 - MSG_N_PARAMS))
273 
275 SOFIAPUBFUN issize_t msg_avlist_d(su_home_t *, char **ss,
276  msg_param_t const **return_params);
277 
279 SOFIAPUBFUN issize_t msg_params_d(su_home_t *, char **ss,
280  msg_param_t const **return_params);
281 
283 SOFIAPUBFUN isize_t msg_params_e(char b[], isize_t bsiz, msg_param_t const pparams[]);
284 
287  msg_param_t **dst,
288  msg_param_t const *src,
289  unsigned prune,
290  int dup);
291 
293 #define MSG_PARAMS_E(b, end, params, flags) \
294  (b) += msg_params_e((b), (size_t)((b) < (end) ? (end) - (b) : 0), (params))
295 
297 #define MSG_PARAMS_SIZE(rv, params) (rv = msg_params_dup_xtra(params, rv))
298 
300 SOFIAPUBFUN char *msg_params_dup(msg_param_t const **d, msg_param_t const *s,
301  char *b, isize_t xtra);
302 
304 su_inline isize_t msg_params_count(msg_param_t const params[])
305 {
306  if (params) {
307  size_t n;
308  for (n = 0; params[n]; n++)
309  ;
310  return n;
311  }
312  else {
313  return 0;
314  }
315 }
316 
318 su_inline isize_t msg_params_dup_xtra(msg_param_t const params[], isize_t offset)
319 {
320  isize_t n = msg_params_count(params);
321  if (n) {
322  MSG_STRUCT_SIZE_ALIGN(offset);
323  offset += MSG_PARAMS_NUM(n + 1) * sizeof(msg_param_t);
324  for (n = 0; params[n]; n++)
325  offset += strlen(params[n]) + 1;
326  }
327  return offset;
328 }
329 
332 {
333  if (h)
334  return (char *)h + h->h_class->hc_size;
335  else
336  return NULL;
337 }
338 
339 SOFIA_END_DECLS
340 
341 #endif
issize_t msg_auth_d(su_home_t *, msg_header_t *h, char *s, isize_t slen)
Parse security headers.
Definition: msg_auth.c:88
msg_print_f msg_payload_e
Print a message payload.
Definition: msg_protos.h:815
Message headers.
isize_t msg_params_dup_xtra(msg_param_t const params[], isize_t offset)
Calculate memory size required by parameter list.
Definition: msg_parser.h:318
issize_t msg_separator_e(char[], isize_t, msg_header_t const *, int)
Encode a separator line.
Definition: msg_basic.c:343
issize_t msg_token_scan(char *start)
Token scanner for msg_commalist_d() accepting also empty entries.
Definition: msg_parser_util.c:593
isize_t msg_default_dup_xtra(msg_header_t const *header, isize_t offset)
Calculate extra size of a plain header.
Definition: msg_header_copy.c:325
issize_t msg_extract_payload(msg_t *msg, msg_pub_t *mo, msg_header_t **return_payload, usize_t body_len, char b[], isize_t bsiz, int eos)
Extract a message body of body_len bytes.
Definition: msg_parser.c:1313
isize_t msg_generic_dup_xtra(msg_header_t const *h, isize_t offset)
Calculate the size of strings associated with a msg_generic_t object.
Definition: msg_generic.c:94
issize_t msg_parse_next_field(su_home_t *home, msg_header_t *prev, char *s, isize_t slen)
Complete this header field and parse next header field.
Definition: msg_parser.c:1212
int msg_firstline_d(char *s, char **ss2, char **ss3)
Parse first line.
Definition: msg_parser_util.c:66
char * msg_auth_dup_one(msg_header_t *dst, msg_header_t const *src, char *b, isize_t xtra)
Duplicate one msg_auth_t object.
Definition: msg_auth.c:142
issize_t msg_params_join(su_home_t *, msg_param_t **dst, msg_param_t const *src, unsigned prune, int dup)
Join list of parameters.
Definition: msg_parser_util.c:1459
int msg_header_update_params(msg_common_t *h, int clear)
Update shortcuts to parameter values.
Definition: msg_parser_util.c:1064
#define MSG_PARAMS_NUM(n)
Calculate allocated number of items in parameter list.
Definition: msg_parser.h:272
issize_t msg_token_d(char **ss, char const **return_token)
Parse a token.
Definition: msg_parser_util.c:106
issize_t msg_extract_separator(msg_t *msg, msg_pub_t *mo, char b[], isize_t bsiz, int eos)
Extract a separator line.
Definition: msg_parser.c:1284
issize_t msg_separator_d(su_home_t *, msg_header_t *, char *, isize_t)
Parse a separator line.
Definition: msg_basic.c:328
isize_t msg_params_count(msg_param_t const params[])
Count number of parameters in the list.
Definition: msg_parser.h:304
void * msg_header_data(msg_frg_t *h)
Return pointer to extra data after header structure.
Definition: msg_parser.h:331
issize_t msg_attribute_value_scanner(char *s)
Scan an attribute (name [= value]) pair.
Definition: msg_parser_util.c:290
issize_t msg_quoted_d(char **ss, char **return_unquoted)
Parse a quoted string.
Definition: msg_parser_util.c:686
issize_t msg_unquoted_e(char *b, isize_t bsiz, char const *s)
Quote string.
Definition: msg_parser_util.c:1742
issize_t msg_payload_d(su_home_t *, msg_header_t *h, char *s, isize_t slen)
Parse payload.
Definition: msg_basic.c:236
issize_t msg_generic_e(char[], isize_t, msg_header_t const *, int)
Encode a generic header.
Definition: msg_generic.c:82
char * msg_generic_dup_one(msg_header_t *dst, msg_header_t const *src, char *b, isize_t xtra)
Duplicate one msg_generic_t object.
Definition: msg_generic.c:101
int msg_hostport_d(char **ss, char const **return_host, char const **return_port)
Parse host[":"port] pair.
Definition: msg_parser_util.c:755
issize_t msg_comment_d(char **ss, char const **return_comment)
Parse a comment.
Definition: msg_parser_util.c:654
issize_t msg_avlist_d(su_home_t *, char **ss, msg_param_t const **return_params)
Parse a semicolong-separated attribute-value list.
Definition: msg_parser_util.c:351
issize_t msg_extract_header(msg_t *msg, msg_pub_t *mo, char b[], isize_t bsiz, int eos)
Extract a header.
Definition: msg_parser.c:1004
char * msg_default_dup_one(msg_header_t *dst, msg_header_t const *src, char *b, isize_t xtra)
Duplicate a header object without external references.
Definition: msg_header_copy.c:344
issize_t msg_params_d(su_home_t *, char **ss, msg_param_t const **return_params)
Parse a semicolon-separated parameter list starting with semicolon.
Definition: msg_parser_util.c:485
issize_t msg_uint32_d(char **ss, uint32_t *return_value)
Parse a 32-bit unsigned int.
Definition: msg_parser_util.c:129
char * msg_params_dup(msg_param_t const **d, msg_param_t const *s, char *b, isize_t xtra)
Duplicate a parameter list.
issize_t msg_generic_d(su_home_t *, msg_header_t *, char *, isize_t)
Parse a generic header.
Definition: msg_generic.c:66
isize_t msg_params_e(char b[], isize_t bsiz, msg_param_t const pparams[])
Encode a list of parameters.
Definition: msg_parser_util.c:503
issize_t msg_commalist_d(su_home_t *, char **ss, msg_param_t **append_list, issize_t(*scanner)(char *s))
Parse a comma-separated list.
Definition: msg_parser_util.c:583
issize_t msg_any_list_d(su_home_t *, char **ss, msg_param_t **append_list, issize_t(*scanner)(char *s), int sep)
Parse any list.
Definition: msg_parser_util.c:189
MSG_HDR_T msg_header_t
Any protocol-specific header object.
Definition: msg_types.h:87
MSG_PUB_T msg_pub_t
Public protocol-specific message structure for accessing the message.
Definition: msg_types.h:81
Common part of the header objects (or message fragments).
Definition: msg_types.h:111
msg_hclass_t * h_class
Header class.
Definition: msg_types.h:114
Message object.
Definition: msg_internal.h:59
SU_HOME_T su_home_t
#define SOFIAPUBFUN
#define su_inline
SU_U32_T uint32_t

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