1
/* $Id: sip_parser.h 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#ifndef __PJSIP_SIP_PARSER_H__
21
#define __PJSIP_SIP_PARSER_H__
25
* @brief SIP Message Parser
28
#include <pjsip/sip_msg.h>
29
#include <pjlib-util/scanner.h>
35
* @defgroup PJSIP_PARSER Parser
37
* @brief Message and message elements parsing.
42
* URI Parsing options.
46
/** If this option is specified, function #pjsip_parse_uri will return
47
* the URI object as pjsip_name_addr instead of the corresponding
50
PJSIP_PARSE_URI_AS_NAMEADDR = 1,
52
/** If this option is specified, function #pjsip_parse_uri and other
53
* internal functions that this function calls will parse URI according
54
* to convention for parsing From/To/Contact header. For example, when
55
* the URI is not enclosed in brackets ("<" and ">"), all parameters
56
* are treated as header parameters (not URI parameters).
58
PJSIP_PARSE_URI_IN_FROM_TO_HDR = 2
62
* Parser syntax error exception value.
64
extern int PJSIP_SYN_ERR_EXCEPTION;
67
* This structure is used to get error reporting from parser.
69
typedef struct pjsip_parser_err_report
71
/** Standard header fields. */
72
PJ_DECL_LIST_MEMBER(struct pjsip_parser_err_report);
73
int except_code; /**< Error exception (e.g. PJSIP_SYN_ERR_EXCEPTION) */
74
int line; /**< Line number. */
75
int col; /**< Column number. */
76
pj_str_t hname; /**< Header name, if any. */
77
} pjsip_parser_err_report;
81
* Parsing context, the default argument for parsing functions.
83
typedef struct pjsip_parse_ctx
85
pj_scanner *scanner; /**< The scanner. */
86
pj_pool_t *pool; /**< The pool. */
87
pjsip_rx_data *rdata; /**< Optional rdata. */
92
* Type of function to parse header. The parsing function must follow these
94
* - It must not modify the input text.
95
* - The hname and HCOLON has been parsed prior to invoking the handler.
96
* - It returns the header instance on success.
97
* - For error reporting, it must throw PJSIP_SYN_ERR_EXCEPTION exception
98
* instead of just returning NULL.
99
* When exception is thrown, the return value is ignored.
100
* - It must read the header separator after finished reading the header
101
* body. The separator types are described below, and if they don't exist,
102
* exception must be thrown. Header separator can be a:
103
* - newline, such as when the header is part of a SIP message.
104
* - ampersand, such as when the header is part of an URI.
105
* - for the last header, these separator is optional since parsing
106
* can be terminated when seeing EOF.
108
typedef pjsip_hdr* (pjsip_parse_hdr_func)(pjsip_parse_ctx *context);
111
* Type of function to parse URI scheme.
112
* Most of the specification of header parser handler (pjsip_parse_hdr_func)
113
* also applies here (except the separator part).
115
typedef void* (pjsip_parse_uri_func)(pj_scanner *scanner, pj_pool_t *pool,
116
pj_bool_t parse_params);
119
* Register header parser handler. The parser handler MUST follow the
120
* specification of header parser handler function. New registration
121
* overwrites previous registration with the same name.
123
* @param hname The header name.
124
* @param hshortname The short header name or NULL.
125
* @param fptr The pointer to function to parser the header.
127
* @return PJ_SUCCESS if success, or the appropriate error code.
129
PJ_DECL(pj_status_t) pjsip_register_hdr_parser( const char *hname,
130
const char *hshortname,
131
pjsip_parse_hdr_func *fptr);
134
* Unregister previously registered header parser handler.
135
* All the arguments MUST exactly equal to the value specified upon
136
* registration of the handler.
138
* @param hname The header name registered.
139
* @param hshortname The short header name registered, or NULL.
140
* @param fptr Previously registered function to parse the header.
142
* @return zero if unregistration was successfull.
144
PJ_DECL(pj_status_t) pjsip_unregister_hdr_parser( const char *hname,
145
const char *hshortname,
146
pjsip_parse_hdr_func *fptr);
149
* Register URI scheme parser handler.
151
* @param scheme The URI scheme registered.
152
* @param func The URI parser function.
154
* @return zero on success.
156
PJ_DECL(pj_status_t) pjsip_register_uri_parser( char *scheme,
157
pjsip_parse_uri_func *func);
160
* Unregister URI scheme parser handler.
161
* All the arguments MUST exactly equal to the value specified upon
162
* registration of the handler.
164
* @param scheme The URI scheme as registered previously.
165
* @param func The function handler as registered previously.
167
* @return zero if the registration was successfull.
169
PJ_DECL(pj_status_t) pjsip_unregister_uri_parser( const char *scheme,
170
pjsip_parse_uri_func *func);
173
* Parse an URI in the input and return the correct instance of URI.
175
* @param pool The pool to get memory allocations.
176
* @param buf The input buffer, which MUST be NULL terminated.
177
* @param size The length of the string (not counting NULL terminator).
178
* @param options If no options are given (value is zero), the object
179
* returned is dependent on the syntax of the URI,
180
* eg. basic SIP URL, TEL URL, or name address.
181
* If option PJSIP_PARSE_URI_AS_NAMEADDR is given,
182
* then the returned object is always name address object,
183
* with the relevant URI object contained in the name
185
* @return The URI or NULL when failed. No exception is thrown by
186
* this function (or any public parser functions).
188
PJ_DECL(pjsip_uri*) pjsip_parse_uri( pj_pool_t *pool,
189
char *buf, pj_size_t size,
193
* Parse SIP status line.
195
* @param buf Text buffer to parse, which MUST be NULL terminated.
196
* @param size The size of the buffer, excluding the NULL character.
197
* @param status_line Structure to receive the parsed elements.
199
* @return PJ_SUCCESS if a status line is parsed successfully.
201
PJ_DECL(pj_status_t) pjsip_parse_status_line(char *buf, pj_size_t size,
202
pjsip_status_line *status_line);
206
* Parse a packet buffer and build a full SIP message from the packet. This
207
* function parses all parts of the message, including request/status line,
208
* all headers, and the message body. The message body however is only
209
* treated as a text block, ie. the function will not try to parse the content
212
* @param pool The pool to allocate memory.
213
* @param buf The input buffer, which MUST be NULL terminated.
214
* @param size The length of the string (not counting NULL terminator).
215
* @param err_list If this parameter is not NULL, then the parser will
216
* put error messages during parsing in this list.
218
* @return The message or NULL when failed. No exception is thrown
219
* by this function (or any public parser functions).
221
PJ_DECL(pjsip_msg *) pjsip_parse_msg( pj_pool_t *pool,
222
char *buf, pj_size_t size,
223
pjsip_parser_err_report *err_list);
227
* Parse a packet buffer and build a rdata. The resulting message will be
228
* stored in \c msg field in the \c rdata. This behaves pretty much like
229
* #pjsip_parse_msg(), except that it will also initialize the header fields
232
* This function is normally called by the transport layer.
234
* @param buf The input buffer, which MUST be NULL terminated.
235
* @param size The length of the string (not counting NULL terminator).
236
* @param rdata The receive data buffer to store the message and
239
* @return The message inside the rdata if successfull, or NULL.
241
PJ_DECL(pjsip_msg *) pjsip_parse_rdata( char *buf, pj_size_t size,
242
pjsip_rx_data *rdata );
245
* Check incoming packet to see if a (probably) valid SIP message has been
248
* @param buf The input buffer, which must be NULL terminated.
249
* @param size The buffer size.
250
* @param is_datagram Put non-zero if transport is datagram oriented.
251
* @param msg_size [out] If message is valid, this parameter will contain
252
* the size of the SIP message (including body, if any).
254
* @return PJ_SUCCESS if a message is found, or an error code.
256
PJ_DECL(pj_status_t) pjsip_find_msg(const char *buf,
258
pj_bool_t is_datagram,
259
pj_size_t *msg_size);
262
* Parse the content of a header and return the header instance.
263
* This function parses the content of a header (ie. part after colon) according
264
* to the expected name, and will return the correct instance of header.
266
* @param pool Pool to allocate memory for the header.
267
* @param hname Header name which is used to find the correct function
268
* to parse the header.
269
* @param line Header content, which must be NULL terminated.
270
* @param size The length of the string (not counting NULL terminator,
272
* @param parsed_len If the value is not NULL, then upon return the function
273
* will fill the pointer with the length of the string
274
* that has been parsed. This is usefull for two purposes,
275
* one is when the string may contain more than one header
276
* lines, and two when an error happen the value can
277
* pinpoint the location of the error in the buffer.
279
* @return The instance of the header if parsing was successful,
280
* or otherwise a NULL pointer will be returned.
282
PJ_DECL(void*) pjsip_parse_hdr( pj_pool_t *pool, const pj_str_t *hname,
283
char *line, pj_size_t size,
287
* Parse header line(s). Multiple headers can be parsed by this function.
288
* When there are multiple headers, the headers MUST be separated by either
289
* a newline (as in SIP message) or ampersand mark (as in URI). This separator
290
* is optional for the last header.
292
* @param pool The pool.
293
* @param input The input text to parse, which must be NULL terminated.
294
* @param size The text length.
295
* @param hlist The header list to store the parsed headers.
296
* This list must have been initialized before calling
298
* @param options Specify 1 here to make parsing stop when error is
299
* encountered when parsing the header. Otherwise the
300
* error is silently ignored and parsing resumes to the
302
* @return zero if successfull, or -1 if error is encountered.
303
* Upon error, the \a hlist argument MAY contain
304
* successfully parsed headers.
306
PJ_DECL(pj_status_t) pjsip_parse_headers( pj_pool_t *pool, char *input,
307
pj_size_t size, pjsip_hdr *hlist,
317
# pragma warning(push)
318
# pragma warning(disable:4510) // default constructor could not be generated
319
# pragma warning(disable:4512) // assignment operator could not be generated
320
# pragma warning(disable:4610) // user defined constructor required
324
* Parser constants. @see pjsip_parser_const()
326
typedef struct pjsip_parser_const_t
328
const pj_str_t pjsip_USER_STR; /**< "user" string constant. */
329
const pj_str_t pjsip_METHOD_STR; /**< "method" string constant */
330
const pj_str_t pjsip_TRANSPORT_STR; /**< "transport" string const. */
331
const pj_str_t pjsip_MADDR_STR; /**< "maddr" string const. */
332
const pj_str_t pjsip_LR_STR; /**< "lr" string const. */
333
const pj_str_t pjsip_SIP_STR; /**< "sip" string constant. */
334
const pj_str_t pjsip_SIPS_STR; /**< "sips" string constant. */
335
const pj_str_t pjsip_TEL_STR; /**< "tel" string constant. */
336
const pj_str_t pjsip_BRANCH_STR; /**< "branch" string constant. */
337
const pj_str_t pjsip_TTL_STR; /**< "ttl" string constant. */
338
const pj_str_t pjsip_RECEIVED_STR; /**< "received" string const. */
339
const pj_str_t pjsip_Q_STR; /**< "q" string constant. */
340
const pj_str_t pjsip_EXPIRES_STR; /**< "expires" string constant. */
341
const pj_str_t pjsip_TAG_STR; /**< "tag" string constant. */
342
const pj_str_t pjsip_RPORT_STR; /**< "rport" string const. */
344
pj_cis_t pjsip_HOST_SPEC; /**< For scanning host part. */
345
pj_cis_t pjsip_DIGIT_SPEC; /**< Decimal digits */
346
pj_cis_t pjsip_ALPHA_SPEC; /**< Alpha (A-Z, a-z) */
347
pj_cis_t pjsip_ALNUM_SPEC; /**< Decimal + Alpha. */
348
pj_cis_t pjsip_TOKEN_SPEC; /**< Token. */
349
pj_cis_t pjsip_TOKEN_SPEC_ESC; /**< Token without '%' character */
350
pj_cis_t pjsip_VIA_PARAM_SPEC; /**< Via param is token + ":" for
352
pj_cis_t pjsip_VIA_PARAM_SPEC_ESC; /**< .. as above without '%' */
353
pj_cis_t pjsip_HEX_SPEC; /**< Hexadecimal digits. */
354
pj_cis_t pjsip_PARAM_CHAR_SPEC; /**< For scanning pname (or pvalue
355
when it's not quoted.) in URI */
356
pj_cis_t pjsip_PARAM_CHAR_SPEC_ESC; /**< Variant without the escape ('%')
358
pj_cis_t pjsip_HDR_CHAR_SPEC; /**< Chars in hname/havalue in URL. */
359
pj_cis_t pjsip_HDR_CHAR_SPEC_ESC; /**< Variant without the escape ('%')
361
pj_cis_t pjsip_PROBE_USER_HOST_SPEC;/**< Hostname characters. */
362
pj_cis_t pjsip_PASSWD_SPEC; /**< Password. */
363
pj_cis_t pjsip_PASSWD_SPEC_ESC; /**< Variant without the escape ('%')
365
pj_cis_t pjsip_USER_SPEC; /**< User */
366
pj_cis_t pjsip_USER_SPEC_ESC; /**< Variant without the escape ('%')
368
pj_cis_t pjsip_USER_SPEC_LENIENT; /**< User, with additional '#' char */
369
pj_cis_t pjsip_USER_SPEC_LENIENT_ESC;/**< pjsip_USER_SPEC_ESC with '#' */
370
pj_cis_t pjsip_NOT_NEWLINE; /**< For eating up header, basically
371
any chars except newlines or
373
pj_cis_t pjsip_NOT_COMMA_OR_NEWLINE;/**< Array elements. */
374
pj_cis_t pjsip_DISPLAY_SPEC; /**< Used when searching for display
376
pj_cis_t pjsip_OTHER_URI_CONTENT; /**< Generic URI content. */
378
} pjsip_parser_const_t;
381
# pragma warning(pop)
386
* Get parser constants.
388
PJ_DECL(const pjsip_parser_const_t*) pjsip_parser_const(void);
396
PJSIP_PARSE_REMOVE_QUOTE = 1
399
/* Parse parameter in header (matching the character as token) */
400
PJ_DECL(void) pjsip_parse_param_imp(pj_scanner *scanner, pj_pool_t *pool,
401
pj_str_t *pname, pj_str_t *pvalue,
403
/* Parse parameter in URL (matching the character as paramchar) */
404
PJ_DECL(void) pjsip_parse_uri_param_imp(pj_scanner *scanner, pj_pool_t *pool,
405
pj_str_t *pname, pj_str_t *pvalue,
407
PJ_DECL(void) pjsip_concat_param_imp(pj_str_t *param, pj_pool_t *pool,
408
const pj_str_t *pname,
409
const pj_str_t *pvalue,
411
PJ_DECL(void) pjsip_parse_end_hdr_imp ( pj_scanner *scanner );
415
#endif /* __PJSIP_SIP_PARSER_H__ */