1 /* $Id$ */
2 /*
3  * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4  * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5  *
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.
10  *
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.
15  *
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
19  */
20 #ifndef __PJSIP_SIP_PARSER_H__
21 #define __PJSIP_SIP_PARSER_H__
22 
23 /**
24  * @file sip_parser.h
25  * @brief SIP Message Parser
26  */
27 
28 #include <pjsip/sip_msg.h>
29 #include <pjlib-util/scanner.h>
30 #include <pj/list.h>
31 
32 PJ_BEGIN_DECL
33 
34 /**
35  * @defgroup PJSIP_PARSER Parser
36  * @ingroup PJSIP_MSG
37  * @brief Message and message elements parsing.
38  * @{
39  */
40 
41 /**
42  * Contants for limit checks
43  */
44 #define PJSIP_MIN_CONTENT_LENGTH    0
45 #define PJSIP_MAX_CONTENT_LENGTH    PJ_MAXINT32
46 #define PJSIP_MIN_PORT		    0
47 #define PJSIP_MAX_PORT		    PJ_MAXUINT16
48 #define PJSIP_MIN_TTL		    0
49 #define PJSIP_MAX_TTL		    PJ_MAXUINT8
50 #define PJSIP_MIN_STATUS_CODE	    100
51 #define PJSIP_MAX_STATUS_CODE	    999
52 #define PJSIP_MIN_Q1000		    0
53 #define PJSIP_MAX_Q1000		    PJ_MAXINT32 / 1000
54 #define PJSIP_MIN_EXPIRES	    0
55 #define PJSIP_MAX_EXPIRES	    ((pj_uint32_t)0xFFFFFFFFUL)
56 #define PJSIP_MIN_CSEQ		    0
57 #define PJSIP_MAX_CSEQ		    PJ_MAXINT32
58 #define PJSIP_MIN_RETRY_AFTER	    0
59 #define PJSIP_MAX_RETRY_AFTER	    PJ_MAXINT32
60 
61 /**
62  * URI Parsing options.
63  */
64 enum
65 {
66     /** If this option is specified, function #pjsip_parse_uri will return
67      *  the URI object as pjsip_name_addr instead of the corresponding
68      *  URI object.
69      */
70     PJSIP_PARSE_URI_AS_NAMEADDR = 1,
71 
72     /** If this option is specified, function #pjsip_parse_uri and other
73      *  internal functions that this function calls will parse URI according
74      *  to convention for parsing From/To/Contact header. For example, when
75      *  the URI is not enclosed in brackets ("<" and ">"), all parameters
76      *  are treated as header parameters (not URI parameters).
77      */
78     PJSIP_PARSE_URI_IN_FROM_TO_HDR = 2
79 };
80 
81 /**
82  * Parser syntax error exception value.
83  */
84 extern int PJSIP_SYN_ERR_EXCEPTION;
85 
86 /**
87  * Invalid value error exception value.
88  */
89 extern int PJSIP_EINVAL_ERR_EXCEPTION;
90 
91 /**
92  * This structure is used to get error reporting from parser.
93  */
94 typedef struct pjsip_parser_err_report
95 {
96     /** Standard header fields. */
97     PJ_DECL_LIST_MEMBER(struct pjsip_parser_err_report);
98     int		except_code;	/**< Error exception (e.g. PJSIP_SYN_ERR_EXCEPTION) */
99     int		line;		/**< Line number. */
100     int		col;		/**< Column number. */
101     pj_str_t	hname;		/**< Header name, if any. */
102 } pjsip_parser_err_report;
103 
104 
105 /**
106  * Parsing context, the default argument for parsing functions.
107  */
108 typedef struct pjsip_parse_ctx
109 {
110     pj_scanner      *scanner;   /**< The scanner.       */
111     pj_pool_t       *pool;      /**< The pool.          */
112     pjsip_rx_data   *rdata;     /**< Optional rdata.    */
113 } pjsip_parse_ctx;
114 
115 
116 /**
117  * Type of function to parse header. The parsing function must follow these
118  * specification:
119  *   - It must not modify the input text.
120  *   - The hname and HCOLON has been parsed prior to invoking the handler.
121  *   - It returns the header instance on success.
122  *   - For error reporting, it must throw PJSIP_SYN_ERR_EXCEPTION exception
123  *     instead of just returning NULL.
124  *     When exception is thrown, the return value is ignored.
125  *   - It must read the header separator after finished reading the header
126  *     body. The separator types are described below, and if they don't exist,
127  *     exception must be thrown. Header separator can be a:
128  *	- newline, such as when the header is part of a SIP message.
129  *	- ampersand, such as when the header is part of an URI.
130  *	- for the last header, these separator is optional since parsing
131  *        can be terminated when seeing EOF.
132  */
133 typedef pjsip_hdr* (pjsip_parse_hdr_func)(pjsip_parse_ctx *context);
134 
135 /**
136  * Type of function to parse URI scheme.
137  * Most of the specification of header parser handler (pjsip_parse_hdr_func)
138  * also applies here (except the separator part).
139  */
140 typedef void* (pjsip_parse_uri_func)(pj_scanner *scanner, pj_pool_t *pool,
141 				     pj_bool_t parse_params);
142 
143 /**
144  * Register header parser handler. The parser handler MUST follow the
145  * specification of header parser handler function. New registration
146  * overwrites previous registration with the same name.
147  *
148  * @param hname		The header name.
149  * @param hshortname	The short header name or NULL.
150  * @param fptr		The pointer to function to parser the header.
151  *
152  * @return		PJ_SUCCESS if success, or the appropriate error code.
153  */
154 PJ_DECL(pj_status_t) pjsip_register_hdr_parser( const char *hname,
155 						const char *hshortname,
156 						pjsip_parse_hdr_func *fptr);
157 
158 /**
159  * Unregister previously registered header parser handler.
160  * All the arguments MUST exactly equal to the value specified upon
161  * registration of the handler.
162  *
163  * @param hname		The header name registered.
164  * @param hshortname	The short header name registered, or NULL.
165  * @param fptr		Previously registered function to parse the header.
166  *
167  * @return		zero if unregistration was successfull.
168  */
169 PJ_DECL(pj_status_t) pjsip_unregister_hdr_parser( const char *hname,
170 						  const char *hshortname,
171 						  pjsip_parse_hdr_func *fptr);
172 
173 /**
174  * Register URI scheme parser handler.
175  *
176  * @param scheme	The URI scheme registered.
177  * @param func		The URI parser function.
178  *
179  * @return		zero on success.
180  */
181 PJ_DECL(pj_status_t) pjsip_register_uri_parser( char *scheme,
182 					        pjsip_parse_uri_func *func);
183 
184 /**
185  * Unregister URI scheme parser handler.
186  * All the arguments MUST exactly equal to the value specified upon
187  * registration of the handler.
188  *
189  * @param scheme	The URI scheme as registered previously.
190  * @param func		The function handler as registered previously.
191  *
192  * @return		zero if the registration was successfull.
193  */
194 PJ_DECL(pj_status_t) pjsip_unregister_uri_parser( const char *scheme,
195 						  pjsip_parse_uri_func *func);
196 
197 /**
198  * Parse an URI in the input and return the correct instance of URI.
199  * Note that the input string buffer MUST be NULL terminated and have
200  * length at least size+1 (size MUST NOT include the NULL terminator).
201  *
202  * @param pool		The pool to get memory allocations.
203  * @param buf		The input buffer, which MUST be NULL terminated.
204  * @param size		The length of the string (not counting NULL terminator).
205  * @param options	If no options are given (value is zero), the object
206  *			returned is dependent on the syntax of the URI,
207  *			eg. basic SIP URL, TEL URL, or name address.
208  *			If option PJSIP_PARSE_URI_AS_NAMEADDR is given,
209  *			then the returned object is always name address object,
210  *			with the relevant URI object contained in the name
211  *			address object.
212  * @return		The URI or NULL when failed. No exception is thrown by
213  *			this function (or any public parser functions).
214  */
215 PJ_DECL(pjsip_uri*) pjsip_parse_uri( pj_pool_t *pool,
216 				     char *buf, pj_size_t size,
217 				     unsigned options);
218 
219 /**
220  * Parse SIP status line.
221  * Note that the input string buffer MUST be NULL terminated and have
222  * length at least size+1 (size MUST NOT include the NULL terminator).
223  *
224  * @param buf		Text buffer to parse, which MUST be NULL terminated.
225  * @param size		The size of the buffer, excluding the NULL character.
226  * @param status_line	Structure to receive the parsed elements.
227  *
228  * @return		PJ_SUCCESS if a status line is parsed successfully.
229  */
230 PJ_DECL(pj_status_t) pjsip_parse_status_line(char *buf, pj_size_t size,
231 					     pjsip_status_line *status_line);
232 
233 
234 /**
235  * Parse a packet buffer and build a full SIP message from the packet. This
236  * function parses all parts of the message, including request/status line,
237  * all headers, and the message body. The message body however is only
238  * treated as a text block, ie. the function will not try to parse the content
239  * of the body.
240  *
241  * Note that the input string buffer MUST be NULL terminated and have
242  * length at least size+1 (size MUST NOT include the NULL terminator).
243  *
244  * @param pool		The pool to allocate memory.
245  * @param buf		The input buffer, which MUST be NULL terminated.
246  * @param size		The length of the string (not counting NULL terminator).
247  * @param err_list	If this parameter is not NULL, then the parser will
248  *			put error messages during parsing in this list.
249  *
250  * @return		The message or NULL when failed. No exception is thrown
251  *			by this function (or any public parser functions).
252  */
253 PJ_DECL(pjsip_msg *) pjsip_parse_msg( pj_pool_t *pool,
254 				      char *buf, pj_size_t size,
255 				      pjsip_parser_err_report *err_list);
256 
257 
258 /**
259  * Parse a packet buffer and build a rdata. The resulting message will be
260  * stored in \c msg field in the \c rdata. This behaves pretty much like
261  * #pjsip_parse_msg(), except that it will also initialize the header fields
262  * in the \c rdata.
263  *
264  * This function is normally called by the transport layer.
265  *
266  * Note that the input string buffer MUST be NULL terminated and have
267  * length at least size+1 (size MUST NOT include the NULL terminator).
268  *
269  * @param buf		The input buffer, which MUST be NULL terminated.
270  * @param size		The length of the string (not counting NULL terminator).
271  * @param rdata         The receive data buffer to store the message and
272  *                      its elements.
273  *
274  * @return              The message inside the rdata if successfull, or NULL.
275  */
276 PJ_DECL(pjsip_msg *) pjsip_parse_rdata( char *buf, pj_size_t size,
277                                         pjsip_rx_data *rdata );
278 
279 /**
280  * Check incoming packet to see if a (probably) valid SIP message has been
281  * received.
282  * Note that the input string buffer MUST be NULL terminated and have
283  * length at least size+1 (size MUST NOT include the NULL terminator).
284  *
285  * @param buf		The input buffer, which must be NULL terminated.
286  * @param size		The length of the string (not counting NULL terminator).
287  * @param is_datagram	Put non-zero if transport is datagram oriented.
288  * @param msg_size	[out] If message is valid, this parameter will contain
289  *			the size of the SIP message (including body, if any).
290  *
291  * @return		PJ_SUCCESS if a message is found, or an error code.
292  */
293 PJ_DECL(pj_status_t) pjsip_find_msg(const char *buf,
294                                     pj_size_t size,
295 				    pj_bool_t is_datagram,
296                                     pj_size_t *msg_size);
297 
298 /**
299  * Parse the content of a header and return the header instance.
300  * This function parses the content of a header (ie. part after colon) according
301  * to the expected name, and will return the correct instance of header.
302  *
303  * Note that the input string buffer MUST be NULL terminated and have
304  * length at least size+1 (size MUST NOT include the NULL terminator).
305  *
306  * @param pool		Pool to allocate memory for the header.
307  * @param hname		Header name which is used to find the correct function
308  *			to parse the header.
309  * @param line		Header content, which must be NULL terminated.
310  * @param size		The length of the string (not counting NULL terminator,
311  *			if any).
312  * @param parsed_len	If the value is not NULL, then upon return the function
313  *			will fill the pointer with the length of the string
314  *			that has been parsed. This is usefull for two purposes,
315  *			one is when the string may contain more than one header
316  *			lines, and two when an error happen the value can
317  *			pinpoint the location of the error in the buffer.
318  *
319  * @return		The instance of the header if parsing was successful,
320  *			or otherwise a NULL pointer will be returned.
321  */
322 PJ_DECL(void*) pjsip_parse_hdr( pj_pool_t *pool, const pj_str_t *hname,
323 				char *line, pj_size_t size,
324 				int *parsed_len);
325 
326 /**
327  * Parse header line(s). Multiple headers can be parsed by this function.
328  * When there are multiple headers, the headers MUST be separated by either
329  * a newline (as in SIP message) or ampersand mark (as in URI). This separator
330  * is optional for the last header.
331  *
332  * Note that the input string buffer MUST be NULL terminated and have
333  * length at least size+1 (size MUST NOT include the NULL terminator).
334  *
335  * @param pool		The pool.
336  * @param input		The input text to parse, which must be NULL terminated.
337  * @param size		The text length (not counting NULL terminator).
338  * @param hlist		The header list to store the parsed headers.
339  *			This list must have been initialized before calling
340  *			this function.
341  * @param options	Specify 1 here to make parsing stop when error is
342  * 			encountered when parsing the header. Otherwise the
343  * 			error is silently ignored and parsing resumes to the
344  * 			next line.
345  * @return		zero if successfull, or -1 if error is encountered.
346  *			Upon error, the \a hlist argument MAY contain
347  *			successfully parsed headers.
348  */
349 PJ_DECL(pj_status_t) pjsip_parse_headers( pj_pool_t *pool, char *input,
350 				          pj_size_t size, pjsip_hdr *hlist,
351 				          unsigned options);
352 
353 
354 /**
355  * @}
356  */
357 
358 
359 #ifdef _MSC_VER
360 #   pragma warning(push)
361 #   pragma warning(disable:4510) // default constructor could not be generated
362 #   pragma warning(disable:4512) // assignment operator could not be generated
363 #   pragma warning(disable:4610) // user defined constructor required
364 #endif
365 
366 /**
367  * Parser constants. @see pjsip_parser_const()
368  */
369 typedef struct pjsip_parser_const_t
370 {
371     const pj_str_t pjsip_USER_STR;	/**< "user" string constant.    */
372     const pj_str_t pjsip_METHOD_STR;	/**< "method" string constant   */
373     const pj_str_t pjsip_TRANSPORT_STR;	/**< "transport" string const.  */
374     const pj_str_t pjsip_MADDR_STR;	/**< "maddr" string const.	*/
375     const pj_str_t pjsip_LR_STR;	/**< "lr" string const.		*/
376     const pj_str_t pjsip_SIP_STR;	/**< "sip" string constant.	*/
377     const pj_str_t pjsip_SIPS_STR;	/**< "sips" string constant.    */
378     const pj_str_t pjsip_TEL_STR;	/**< "tel" string constant.	*/
379     const pj_str_t pjsip_BRANCH_STR;	/**< "branch" string constant.  */
380     const pj_str_t pjsip_TTL_STR;	/**< "ttl" string constant.	*/
381     const pj_str_t pjsip_RECEIVED_STR;	/**< "received" string const.   */
382     const pj_str_t pjsip_Q_STR;		/**< "q" string constant.	*/
383     const pj_str_t pjsip_EXPIRES_STR;	/**< "expires" string constant. */
384     const pj_str_t pjsip_TAG_STR;	/**< "tag" string constant.	*/
385     const pj_str_t pjsip_RPORT_STR;	/**< "rport" string const.	*/
386 
387     pj_cis_t pjsip_HOST_SPEC;		/**< For scanning host part.	*/
388     pj_cis_t pjsip_DIGIT_SPEC;		/**< Decimal digits		*/
389     pj_cis_t pjsip_ALPHA_SPEC;		/**< Alpha (A-Z, a-z)		*/
390     pj_cis_t pjsip_ALNUM_SPEC;		/**< Decimal + Alpha.		*/
391     pj_cis_t pjsip_TOKEN_SPEC;		/**< Token.			*/
392     pj_cis_t pjsip_TOKEN_SPEC_ESC;	/**< Token without '%' character */
393     pj_cis_t pjsip_VIA_PARAM_SPEC;	/**< Via param is token + ":" for
394 					     IPv6.			*/
395     pj_cis_t pjsip_VIA_PARAM_SPEC_ESC;	/**< .. as above without '%'	*/
396     pj_cis_t pjsip_HEX_SPEC;  		/**< Hexadecimal digits.	*/
397     pj_cis_t pjsip_PARAM_CHAR_SPEC;	/**< For scanning pname (or pvalue
398 					     when it's  not quoted.) in URI */
399     pj_cis_t pjsip_PARAM_CHAR_SPEC_ESC;	/**< Variant without the escape ('%')
400 					     char			*/
401     pj_cis_t pjsip_HDR_CHAR_SPEC;	/**< Chars in hname/havalue in URL. */
402     pj_cis_t pjsip_HDR_CHAR_SPEC_ESC;	/**< Variant without the escape ('%')
403 					     char			*/
404     pj_cis_t pjsip_PROBE_USER_HOST_SPEC;/**< Hostname characters.	*/
405     pj_cis_t pjsip_PASSWD_SPEC;		/**< Password.			*/
406     pj_cis_t pjsip_PASSWD_SPEC_ESC;	/**< Variant without the escape ('%')
407 					     char			*/
408     pj_cis_t pjsip_USER_SPEC;		/**< User */
409     pj_cis_t pjsip_USER_SPEC_ESC;	/**< Variant without the escape ('%')
410 					     char			*/
411     pj_cis_t pjsip_USER_SPEC_LENIENT;	/**< User, with additional '#' char */
412     pj_cis_t pjsip_USER_SPEC_LENIENT_ESC;/**< pjsip_USER_SPEC_ESC with '#' */
413     pj_cis_t pjsip_NOT_NEWLINE;		/**< For eating up header, basically
414 					     any chars except newlines or
415 					     zero.			*/
416     pj_cis_t pjsip_NOT_COMMA_OR_NEWLINE;/**< Array elements.		*/
417     pj_cis_t pjsip_DISPLAY_SPEC;	/**< Used when searching for display
418 					     name.			*/
419     pj_cis_t pjsip_OTHER_URI_CONTENT;	/**< Generic URI content.	*/
420 
421 } pjsip_parser_const_t;
422 
423 #ifdef _MSC_VER
424 #   pragma warning(pop)
425 #endif
426 
427 
428 /**
429  * Get parser constants.
430  */
431 PJ_DECL(const pjsip_parser_const_t*) pjsip_parser_const(void);
432 
433 
434 /*
435  * Parser utilities.
436  */
437 enum
438 {
439     PJSIP_PARSE_REMOVE_QUOTE = 1
440 };
441 
442 /* Parse parameter in header (matching the character as token) */
443 PJ_DECL(void) pjsip_parse_param_imp(pj_scanner *scanner, pj_pool_t *pool,
444 			     	    pj_str_t *pname, pj_str_t *pvalue,
445 			     	    unsigned opt);
446 /* Parse parameter in URL (matching the character as paramchar) */
447 PJ_DECL(void) pjsip_parse_uri_param_imp(pj_scanner *scanner, pj_pool_t *pool,
448 				 	pj_str_t *pname, pj_str_t *pvalue,
449 				 	unsigned opt);
450 PJ_DECL(void) pjsip_concat_param_imp(pj_str_t *param, pj_pool_t *pool,
451 			     	     const pj_str_t *pname,
452 				     const pj_str_t *pvalue,
453 			     	     int sepchar);
454 PJ_DECL(void) pjsip_parse_end_hdr_imp ( pj_scanner *scanner );
455 
456 /* Parse generic array header */
457 PJ_DECL(void) pjsip_parse_generic_array_hdr_imp(pjsip_generic_array_hdr *hdr,
458 						pj_scanner *scanner);
459 
460 
461 PJ_END_DECL
462 
463 #endif	/* __PJSIP_SIP_PARSER_H__ */
464 
465