osip_uri.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
  3. Copyright (C) 2001-2020 Aymeric MOIZARD amoizard@antisip.com
  4. This library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. This library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with this library; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  15. */
  16. #ifndef _URLS_H_
  17. #define _URLS_H_
  18. #include <osipparser2/osip_const.h>
  19. #include <osipparser2/osip_list.h>
  20. /**
  21. * @file osip_uri.h
  22. * @brief oSIP url parser Routines
  23. *
  24. * This is the implementation of sip url scheme. It also partially support
  25. * any unrecognised scheme (not starting with 'sip:' or 'sips:'). Unrecognised
  26. * scheme are stored in url->string.
  27. */
  28. /**
  29. * @defgroup oSIP_URLS oSIP url parser Handling
  30. * @ingroup osip2_parser
  31. * @{
  32. */
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /**
  37. * Structure for referencing url parameters.
  38. * @var osip_uri_param_t
  39. */
  40. typedef struct osip_uri_param osip_uri_param_t;
  41. /**
  42. * Structure for referencing url parameters.
  43. * @struct osip_uri_param
  44. */
  45. struct osip_uri_param {
  46. char *gname; /**< uri parameter name */
  47. char *gvalue;
  48. /**< uri parameter value */
  49. };
  50. /**
  51. * Structure for referencing url headers.
  52. * @var osip_uri_header_t
  53. */
  54. typedef osip_uri_param_t osip_uri_header_t;
  55. /**
  56. * Allocate a url parameter element.
  57. * @param url_param The element to work on.
  58. */
  59. int osip_uri_param_init(osip_uri_param_t **url_param);
  60. /**
  61. * Free a url parameter element.
  62. * @param url_param The element to work on.
  63. */
  64. void osip_uri_param_free(osip_uri_param_t *url_param);
  65. /**
  66. * Set values of a url parameter element.
  67. * @param url_param The element to work on.
  68. * @param name The token name.
  69. * @param value The token value.
  70. */
  71. int osip_uri_param_set(osip_uri_param_t *url_param, char *name, char *value);
  72. /**
  73. * Clone a url parameter element.
  74. * @param url_param The element to work on.
  75. * @param dest The resulting new allocated element.
  76. */
  77. int osip_uri_param_clone(const osip_uri_param_t *url_param, osip_uri_param_t **dest);
  78. #ifndef DOXYGEN
  79. /*
  80. * Free a list of a url parameter element.
  81. * @param url_params The list of url parameter element to free.
  82. */
  83. void osip_uri_param_freelist(osip_list_t *url_params);
  84. #endif
  85. /**
  86. * Allocate and add a url parameter element in a list.
  87. * @param url_params The list of url parameter element to work on.
  88. * @param name The token name.
  89. * @param value The token value.
  90. */
  91. int osip_uri_param_add(osip_list_t *url_params, char *name, char *value);
  92. /**
  93. * Find in a url parameter element in a list.
  94. * @param url_params The list of url parameter element to work on.
  95. * @param name The name of the parameter element to find.
  96. * @param dest A pointer on the element found.
  97. */
  98. int osip_uri_param_get_byname(osip_list_t *url_params, char *name, osip_uri_param_t **dest);
  99. /**
  100. * Allocate a generic parameter element.
  101. * @param url_header The element to work on.
  102. */
  103. #define osip_uri_header_init(url_header) osip_uri_param_init(url_header)
  104. /**
  105. * Free a generic parameter element.
  106. * @param url_header The element to work on.
  107. */
  108. #define osip_uri_header_free(url_header) osip_uri_param_free(url_header)
  109. /**
  110. * Set values of a generic parameter element.
  111. * @param url_header The element to work on.
  112. * @param name The token name.
  113. * @param value The token value.
  114. */
  115. #define osip_uri_header_set(url_header, name, value) osip_uri_param_set(url_header, name, value)
  116. /**
  117. * Clone a generic parameter element.
  118. * @param url_header The element to work on.
  119. * @param dest The resulting new allocated element.
  120. */
  121. #define osip_uri_header_clone(url_header, dest) osip_uri_param_clone(url_header, dest)
  122. #ifndef DOXYGEN
  123. /*
  124. * Free a list of a generic parameter element.
  125. * @param LIST The list of generic parameter element to free.
  126. */
  127. #define osip_uri_header_freelist(LIST) osip_uri_param_freelist(LIST)
  128. #endif
  129. /**
  130. * Allocate and add a generic parameter element in a list.
  131. * @param url_headers The list of generic parameter element to work on.
  132. * @param name The token name.
  133. * @param value The token value.
  134. */
  135. #define osip_uri_header_add(url_headers, name, value) osip_uri_param_add(url_headers, name, value)
  136. /**
  137. * Find in a generic parameter element in a list.
  138. * @param url_headers The list of generic parameter element to work on.
  139. * @param name The name of the parameter element to find.
  140. * @param dest A pointer on the element found.
  141. */
  142. #define osip_uri_header_get_byname(url_headers, name, dest) osip_uri_param_get_byname(url_headers, name, dest)
  143. /**
  144. * Structure for referencing SIP urls.
  145. * @var osip_uri_t
  146. */
  147. typedef struct osip_uri osip_uri_t;
  148. /**
  149. * Structure for referencing SIP urls.
  150. * @struct osip_uri
  151. */
  152. struct osip_uri {
  153. char *scheme; /**< Uri Scheme (sip or sips) */
  154. char *username; /**< Username */
  155. char *password; /**< Password */
  156. char *host; /**< Domain */
  157. char *port; /**< Port number */
  158. osip_list_t url_params; /**< Uri parameters */
  159. osip_list_t url_headers;
  160. /**< Uri headers */
  161. char *string;
  162. /**< Space for other url schemes. (http, mailto...) */
  163. };
  164. /**
  165. * Allocate a url element.
  166. * @param url The element to work on.
  167. */
  168. int osip_uri_init(osip_uri_t **url);
  169. /**
  170. * Free a url element.
  171. * @param url The element to work on.
  172. */
  173. void osip_uri_free(osip_uri_t *url);
  174. /**
  175. * Parse a url.
  176. * @param url The element to work on.
  177. * @param buf The buffer to parse.
  178. */
  179. int osip_uri_parse(osip_uri_t *url, const char *buf);
  180. #ifndef DOXYGEN
  181. /**
  182. * Parse the header part of a url.
  183. * @param url The element to work on.
  184. * @param buf The buffer to parse.
  185. */
  186. int osip_uri_parse_headers(osip_uri_t *url, const char *buf);
  187. /**
  188. * Parse the parameter part of a url.
  189. * @param url The element to work on.
  190. * @param buf The buffer to parse.
  191. */
  192. int osip_uri_parse_params(osip_uri_t *url, const char *buf);
  193. #endif
  194. /**
  195. * Get a string representation of a url element.
  196. * @param url The element to work on.
  197. * @param dest The resulting new allocated buffer.
  198. */
  199. int osip_uri_to_str(const osip_uri_t *url, char **dest);
  200. /**
  201. * Clone a url element.
  202. * @param url The element to work on.
  203. * @param dest The resulting new allocated element.
  204. */
  205. int osip_uri_clone(const osip_uri_t *url, osip_uri_t **dest);
  206. /**
  207. * Get a canonical string representation of a url element.
  208. * as defined in 10.3-5
  209. * @param url The element to work on.
  210. * @param dest The resulting new allocated buffer.
  211. */
  212. int osip_uri_to_str_canonical(const osip_uri_t *url, char **dest);
  213. /**
  214. * Set the scheme of a url element.
  215. * @param url The element to work on.
  216. * @param value The token value.
  217. */
  218. void osip_uri_set_scheme(osip_uri_t *url, char *value);
  219. /**
  220. * Get the scheme of a url element.
  221. * @param url The element to work on.
  222. */
  223. char *osip_uri_get_scheme(osip_uri_t *url);
  224. /**
  225. * Set the host of a url element.
  226. * @param url The element to work on.
  227. * @param value The token value.
  228. */
  229. void osip_uri_set_host(osip_uri_t *url, char *value);
  230. /**
  231. * Get the host of a url element.
  232. * @param url The element to work on.
  233. */
  234. char *osip_uri_get_host(osip_uri_t *url);
  235. /**
  236. * Set the username of a url element.
  237. * @param url The element to work on.
  238. * @param value The token value.
  239. */
  240. void osip_uri_set_username(osip_uri_t *url, char *value);
  241. /**
  242. * Get the username of a url element.
  243. * @param url The element to work on.
  244. */
  245. char *osip_uri_get_username(osip_uri_t *url);
  246. /**
  247. * Set the password of a url element.
  248. * @param url The element to work on.
  249. * @param value The token value.
  250. */
  251. void osip_uri_set_password(osip_uri_t *url, char *value);
  252. /**
  253. * Get the password of a url element.
  254. * @param url The element to work on.
  255. */
  256. char *osip_uri_get_password(osip_uri_t *url);
  257. /**
  258. * Set the port of a url element.
  259. * @param url The element to work on.
  260. * @param value The token value.
  261. */
  262. void osip_uri_set_port(osip_uri_t *url, char *value);
  263. /**
  264. * Get the port of a url element.
  265. * @param url The element to work on.
  266. */
  267. char *osip_uri_get_port(osip_uri_t *url);
  268. /**
  269. * Set the transport parameter to UDP in a url element.
  270. * @param url The element to work on.
  271. */
  272. #define osip_uri_set_transport_udp(url) osip_uri_param_add((&(url)->url_params), osip_strdup("transport"), osip_strdup("udp"))
  273. /**
  274. * Set the transport parameter to TCP in a url element.
  275. * @param url The element to work on.
  276. */
  277. #define osip_uri_set_transport_tcp(url) osip_uri_param_add((&(url)->url_params), osip_strdup("transport"), osip_strdup("tcp"))
  278. /**
  279. * Set the transport parameter to SCTP in a url element.
  280. * @param url The element to work on.
  281. */
  282. #define osip_uri_set_transport_sctp(url) osip_uri_param_add((&(url)->url_params), osip_strdup("transport"), osip_strdup("sctp"))
  283. /**
  284. * Set the transport parameter to TLS in a url element.
  285. * @param url The element to work on.
  286. */
  287. #define osip_uri_set_transport_tls(url) osip_uri_param_add((&(url)->url_params), osip_strdup("transport"), osip_strdup("tls"))
  288. /**
  289. * Set the transport parameter to TLS in a url element.
  290. * @param url The element to work on.
  291. * @param value The value describing the transport protocol.
  292. */
  293. #define osip_uri_set_transport(url, value) osip_uri_param_add((&(url)->url_params), osip_strdup("transport"), value)
  294. /**
  295. * Set the user parameter to PHONE in a url element.
  296. * @param url The element to work on.
  297. */
  298. #define osip_uri_set_user_phone(url) osip_uri_param_add((&(url)->url_params), osip_strdup("user"), osip_strdup("phone"))
  299. /**
  300. * Set the user parameter to IP in a url element.
  301. * @param url The element to work on.
  302. */
  303. #define osip_uri_set_user_ip(url) osip_uri_param_add((&(url)->url_params), osip_strdup("user"), osip_strdup("ip"))
  304. /**
  305. * Set a method parameter to INVITE in a url element.
  306. * @param url The element to work on.
  307. */
  308. #define osip_uri_set_method_invite(url) osip_uri_param_add((&(url)->url_params), osip_strdup("method"), osip_strdup("INVITE"))
  309. /**
  310. * Set a method parameter to ACK in a url element.
  311. * @param url The element to work on.
  312. */
  313. #define osip_uri_set_method_ack(url) osip_uri_param_add((&(url)->url_params), osip_strdup("method"), osip_strdup("ACK"))
  314. /**
  315. * Set a method parameter to OPTIONS in a url element.
  316. * @param url The element to work on.
  317. */
  318. #define osip_uri_set_method_options(url) osip_uri_param_add((&(url)->url_params), osip_strdup("method"), osip_strdup("OPTIONS"))
  319. /**
  320. * Set a method parameter to BYE in a url element.
  321. * @param url The element to work on.
  322. */
  323. #define osip_uri_set_method_bye(url) osip_uri_param_add((&(url)->url_params), osip_strdup("method"), osip_strdup("BYE"))
  324. /**
  325. * Set a method parameter to CANCEL in a url element.
  326. * @param url The element to work on.
  327. */
  328. #define osip_uri_set_method_cancel(url) osip_uri_param_add((&(url)->url_params), osip_strdup("method"), osip_strdup("CANCEL"))
  329. /**
  330. * Set a method parameter to REGISTER in a url element.
  331. * @param url The element to work on.
  332. */
  333. #define osip_uri_set_method_register(url) osip_uri_param_add((&(url)->url_params), osip_strdup("method"), osip_strdup("REGISTER"))
  334. /**
  335. * Set a method parameter in a url element.
  336. * @param url The element to work on.
  337. * @param value The value for the method parameter.
  338. */
  339. #define osip_uri_set_method(url, value) osip_uri_param_add((&(url)->url_params), osip_strdup("method"), value)
  340. /**
  341. * Set a ttl parameter in a url element.
  342. * @param url The element to work on.
  343. * @param value The value for the ttl parameter.
  344. */
  345. #define osip_uri_set_ttl(url, value) osip_uri_param_add((&(url)->url_params), osip_strdup("ttl"), value)
  346. /**
  347. * Set a maddr parameter in a url element.
  348. * @param url The element to work on.
  349. * @param value The value for the maddr parameter.
  350. */
  351. #define osip_uri_set_maddr(url, value) osip_uri_param_add((&(url)->url_params), osip_strdup("maddr"), value)
  352. /**
  353. * Allocate and add a url parameter element in a url element.
  354. * @param url The element to work on.
  355. * @param name The token name.
  356. * @param value The token value.
  357. */
  358. #define osip_uri_uparam_add(url, name, value) osip_uri_param_add((&(url)->url_params), name, value)
  359. /**
  360. * Find in a url parameter element in a url element.
  361. * @param url The element to work on.
  362. * @param name The name of the url parameter element to find.
  363. * @param dest A pointer on the element found.
  364. */
  365. #define osip_uri_uparam_get_byname(url, name, dest) osip_uri_param_get_byname((&(url)->url_params), name, dest)
  366. /**
  367. * Allocate and add a url header element in a url element.
  368. * @param url The element to work on.
  369. * @param name The token name.
  370. * @param value The token value.
  371. */
  372. #define osip_uri_uheader_add(url, name, value) osip_uri_header_add((&(url)->url_headers), name, value)
  373. /**
  374. * Find in a url header element in a url element.
  375. * @param url The element to work on.
  376. * @param name The name of the url header element to find.
  377. * @param dest A pointer on the element found.
  378. */
  379. #define osip_uri_uheader_get_byname(url, name, dest) osip_uri_header_get_byname((&(url)->url_headers), name, dest)
  380. #ifndef DOXYGEN
  381. /* internal method */
  382. const char *next_separator(const char *ch, int separator_osip_to_find, int before_separator);
  383. char *__osip_uri_escape_nonascii_and_nondef(const char *string, const char *def);
  384. char *__osip_uri_escape_userinfo(const char *string);
  385. char *__osip_uri_escape_password(const char *string);
  386. char *__osip_uri_escape_uri_param(char *string);
  387. char *__osip_uri_escape_header_param(char *string);
  388. void __osip_uri_unescape(char *string);
  389. #endif
  390. /** @} */
  391. #ifdef __cplusplus
  392. }
  393. #endif
  394. #endif /* _URLS_H_ */