reqresp_parser.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 2010, Digium, Inc.
  5. *
  6. * See http://www.asterisk.org for more information about
  7. * the Asterisk project. Please do not directly contact
  8. * any of the maintainers of this project for assistance;
  9. * the project provides a web site, mailing lists and IRC
  10. * channels for your use.
  11. *
  12. * This program is free software, distributed under the terms of
  13. * the GNU General Public License Version 2. See the LICENSE file
  14. * at the top of the source tree.
  15. */
  16. /*!
  17. * \file
  18. * \brief sip request response parser header file
  19. */
  20. #ifndef _SIP_REQRESP_H
  21. #define _SIP_REQRESP_H
  22. /*! \brief uri parameters */
  23. struct uriparams {
  24. char *transport;
  25. char *user;
  26. char *method;
  27. char *ttl;
  28. char *maddr;
  29. int lr;
  30. };
  31. struct contact {
  32. AST_LIST_ENTRY(contact) list;
  33. char *name;
  34. char *user;
  35. char *pass;
  36. char *hostport;
  37. struct uriparams params;
  38. char *headers;
  39. char *expires;
  40. char *q;
  41. };
  42. AST_LIST_HEAD_NOLOCK(contactliststruct, contact);
  43. /*!
  44. * \brief parses a URI in its components.
  45. *
  46. * \note
  47. * - Multiple scheme's can be specified ',' delimited. ex: "sip:,sips:"
  48. * - If a component is not requested, do not split around it. This means
  49. * that if we don't have domain, we cannot split name:pass.
  50. * - It is safe to call with ret_name, pass, hostport pointing all to
  51. * the same place.
  52. * - If no secret parameter is provided, ret_name will return with both
  53. * parts, user:secret.
  54. * - If the URI contains a port number, hostport will return with both
  55. * parts, host:port.
  56. * - This function overwrites the URI string.
  57. *
  58. * \retval 0 on success
  59. * \retval -1 on error.
  60. *
  61. * \verbatim
  62. * general form we are expecting is sip:user:password;user-parameters@host:port;uri-parameters?headers
  63. * \endverbatim
  64. */
  65. int parse_uri(char *uri, const char *scheme, char **ret_name, char **pass,
  66. char **hostport, char **transport);
  67. /*!
  68. * \brief parses a URI in to all of its components and any trailing residue
  69. *
  70. * \retval 0 on success
  71. * \retval -1 on error.
  72. *
  73. */
  74. int parse_uri_full(char *uri, const char *scheme, char **user, char **pass,
  75. char **hostport, struct uriparams *params, char **headers,
  76. char **residue);
  77. /*!
  78. * \brief Get caller id name from SIP headers, copy into output buffer
  79. *
  80. * \retval input string pointer placed after display-name field if possible
  81. */
  82. const char *get_calleridname(const char *input, char *output, size_t outputsize);
  83. /*!
  84. * \brief Get name and number from sip header
  85. *
  86. * \note name and number point to malloced memory on return and must be
  87. * freed. If name or number is not found, they will be returned as NULL.
  88. *
  89. * \retval 0 success
  90. * \retval -1 failure
  91. */
  92. int get_name_and_number(const char *hdr, char **name, char **number);
  93. /*! \brief Pick out text in brackets from character string
  94. * \return pointer to terminated stripped string
  95. * \param tmp input string that will be modified
  96. *
  97. * Examples:
  98. * \verbatim
  99. * "foo" <bar> valid input, returns bar
  100. * foo returns the whole string
  101. * < "foo ... > returns the string between brackets
  102. * < "foo... bogus (missing closing bracket), returns the whole string
  103. * \endverbatim
  104. */
  105. char *get_in_brackets(char *tmp);
  106. /*! \brief Get text in brackets on a const without copy
  107. *
  108. * \param src String to search
  109. * \param[out] start Set to first character inside left bracket.
  110. * \param[out] length Set to lenght of string inside brackets
  111. * \retval 0 success
  112. * \retval -1 failure
  113. * \retval 1 no brackets so got all
  114. */
  115. int get_in_brackets_const(const char *src,const char **start,int *length);
  116. /*! \brief Get text in brackets and any trailing residue
  117. *
  118. * \retval 0 success
  119. * \retval -1 failure
  120. * \retval 1 no brackets so got all
  121. */
  122. int get_in_brackets_full(char *tmp, char **out, char **residue);
  123. /*! \brief Parse the ABNF structure
  124. * name-andor-addr = name-addr / addr-spec
  125. * into its components and return any trailing message-header parameters
  126. *
  127. * \retval 0 success
  128. * \retval -1 failure
  129. */
  130. int parse_name_andor_addr(char *uri, const char *scheme, char **name,
  131. char **user, char **pass, char **domain,
  132. struct uriparams *params, char **headers,
  133. char **remander);
  134. /*! \brief Parse all contact header contacts
  135. * \retval 0 success
  136. * \retval -1 failure
  137. * \retval 1 all contacts (star)
  138. */
  139. int get_comma(char *parse, char **out);
  140. int parse_contact_header(char *contactheader, struct contactliststruct *contactlist);
  141. /*!
  142. * \brief register request parsing tests
  143. */
  144. void sip_request_parser_register_tests(void);
  145. /*!
  146. * \brief unregister request parsing tests
  147. */
  148. void sip_request_parser_unregister_tests(void);
  149. /*!
  150. * \brief Parse supported header in incoming packet
  151. *
  152. * \details This function parses through the options parameters and
  153. * builds a bit field representing all the SIP options in that field. When an
  154. * item is found that is not supported, it is copied to the unsupported
  155. * out buffer.
  156. *
  157. * \param option list
  158. * \param unsupported out buffer (optional)
  159. * \param unsupported out buffer length (optional)
  160. *
  161. * \note Because this function can be called multiple times, it will append
  162. * whatever options are specified in \c options to \c unsupported. Callers
  163. * of this function should make sure the unsupported buffer is clear before
  164. * calling this function.
  165. */
  166. unsigned int parse_sip_options(const char *options, char *unsupported, size_t unsupported_len);
  167. /*!
  168. * \brief Parse required header in incoming packet or response
  169. * returns bitmap
  170. *
  171. * \param option list
  172. */
  173. unsigned int parse_required_sip_options(const char *options);
  174. /*!
  175. * \brief Compare two URIs as described in RFC 3261 Section 19.1.4
  176. *
  177. * \param input1 First URI
  178. * \param input2 Second URI
  179. * \retval 0 URIs match
  180. * \retval nonzero URIs do not match or one or both is malformed
  181. */
  182. int sip_uri_cmp(const char *input1, const char *input2);
  183. /*!
  184. * \brief initialize request and response parser data
  185. *
  186. * \retval 0 Success
  187. * \retval -1 Failure
  188. */
  189. int sip_reqresp_parser_init(void);
  190. /*!
  191. * \brief Free resources used by request and response parser
  192. */
  193. void sip_reqresp_parser_exit(void);
  194. /*!
  195. * \brief Parse a Via header
  196. *
  197. * This function parses the Via header and processes it according to section
  198. * 18.2 of RFC 3261 and RFC 3581. Since we don't have a transport layer, we
  199. * only care about the maddr and ttl parms. The received and rport params are
  200. * not parsed.
  201. *
  202. * \note This function fails to parse some odd combinations of SWS in parameter
  203. * lists.
  204. *
  205. * \code
  206. * VIA syntax. RFC 3261 section 25.1
  207. * Via = ( "Via" / "v" ) HCOLON via-parm *(COMMA via-parm)
  208. * via-parm = sent-protocol LWS sent-by *( SEMI via-params )
  209. * via-params = via-ttl / via-maddr
  210. * / via-received / via-branch
  211. * / via-extension
  212. * via-ttl = "ttl" EQUAL ttl
  213. * via-maddr = "maddr" EQUAL host
  214. * via-received = "received" EQUAL (IPv4address / IPv6address)
  215. * via-branch = "branch" EQUAL token
  216. * via-extension = generic-param
  217. * sent-protocol = protocol-name SLASH protocol-version
  218. * SLASH transport
  219. * protocol-name = "SIP" / token
  220. * protocol-version = token
  221. * transport = "UDP" / "TCP" / "TLS" / "SCTP"
  222. * / other-transport
  223. * sent-by = host [ COLON port ]
  224. * ttl = 1*3DIGIT ; 0 to 255
  225. * \endcode
  226. */
  227. struct sip_via *parse_via(const char *header);
  228. /*
  229. * \brief Free parsed Via data.
  230. */
  231. void free_via(struct sip_via *v);
  232. #endif