sip_parser.h 20 KB

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