sip_uri.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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_URI_H__
  20. #define __PJSIP_SIP_URI_H__
  21. /**
  22. * @file sip_uri.h
  23. * @brief SIP URL Structures and Manipulations
  24. */
  25. #include <pjsip/sip_types.h>
  26. #include <pjsip/sip_config.h>
  27. #include <pj/assert.h>
  28. #include <pj/list.h>
  29. #include <pjlib-util/scanner.h>
  30. PJ_BEGIN_DECL
  31. /**
  32. * @defgroup PJSIP_URI URI
  33. * @brief URI types and manipulations.
  34. * @ingroup PJSIP_MSG
  35. */
  36. /**
  37. * @addtogroup PJSIP_URI_PARAM URI Parameter Container
  38. * @ingroup PJSIP_URI
  39. * @brief Generic parameter elements container.
  40. * @{
  41. */
  42. /**
  43. * Generic parameter, normally used in other_param or header_param.
  44. */
  45. typedef struct pjsip_param
  46. {
  47. PJ_DECL_LIST_MEMBER(struct pjsip_param); /**< Generic list member. */
  48. pj_str_t name; /**< Param/header name. */
  49. pj_str_t value; /**< Param/header value. */
  50. } pjsip_param;
  51. /**
  52. * Find the specified parameter name in the list. The name will be compared
  53. * in case-insensitive comparison.
  54. *
  55. * @param param_list List of parameters to find.
  56. * @param name Parameter/header name to find.
  57. *
  58. * @return The parameter if found, or NULL.
  59. */
  60. PJ_DECL(pjsip_param*) pjsip_param_find( const pjsip_param *param_list,
  61. const pj_str_t *name );
  62. /**
  63. * Alias for pjsip_param_find()
  64. */
  65. PJ_INLINE(pjsip_param*) pjsip_param_cfind(const pjsip_param *param_list,
  66. const pj_str_t *name)
  67. {
  68. return pjsip_param_find(param_list, name);
  69. }
  70. /**
  71. * Compare two parameter lists.
  72. *
  73. * @param param_list1 First parameter list.
  74. * @param param_list2 Second parameter list.
  75. * @param ig_nf If set to 1, do not compare parameters that only
  76. * appear in one of the list.
  77. *
  78. * @return Zero if the parameter list are equal, non-zero
  79. * otherwise.
  80. */
  81. PJ_DECL(int) pjsip_param_cmp(const pjsip_param *param_list1,
  82. const pjsip_param *param_list2,
  83. pj_bool_t ig_nf);
  84. /**
  85. * Duplicate the parameters.
  86. *
  87. * @param pool Pool to allocate memory from.
  88. * @param dst_list Destination list.
  89. * @param src_list Source list.
  90. */
  91. PJ_DECL(void) pjsip_param_clone(pj_pool_t *pool, pjsip_param *dst_list,
  92. const pjsip_param *src_list);
  93. /**
  94. * Duplicate the parameters.
  95. *
  96. * @param pool Pool to allocate memory from.
  97. * @param dst_list Destination list.
  98. * @param src_list Source list.
  99. */
  100. PJ_DECL(void) pjsip_param_shallow_clone(pj_pool_t *pool,
  101. pjsip_param *dst_list,
  102. const pjsip_param *src_list);
  103. /**
  104. * Print parameters.
  105. *
  106. * @param param_list The parameter list.
  107. * @param buf Buffer.
  108. * @param size Size of buffer.
  109. * @param pname_unres Specification of allowed characters in pname.
  110. * @param pvalue_unres Specification of allowed characters in pvalue.
  111. * @param sep Separator character (either ';', ',', or '?').
  112. * When separator is set to '?', this function will
  113. * automatically adjust the separator character to
  114. * '&' after the first parameter is printed.
  115. *
  116. * @return The number of bytes printed, or -1 on errr.
  117. */
  118. PJ_DECL(pj_ssize_t) pjsip_param_print_on(const pjsip_param *param_list,
  119. char *buf, pj_size_t size,
  120. const pj_cis_t *pname_unres,
  121. const pj_cis_t *pvalue_unres,
  122. int sep);
  123. /**
  124. * @}
  125. */
  126. /**
  127. * @defgroup PJSIP_URI_GENERIC Generic URI
  128. * @ingroup PJSIP_URI
  129. * @brief Generic representation for all types of URI.
  130. * @{
  131. */
  132. /**
  133. * URI context.
  134. */
  135. typedef enum pjsip_uri_context_e
  136. {
  137. PJSIP_URI_IN_REQ_URI, /**< The URI is in Request URI. */
  138. PJSIP_URI_IN_FROMTO_HDR, /**< The URI is in From/To header. */
  139. PJSIP_URI_IN_CONTACT_HDR, /**< The URI is in Contact header. */
  140. PJSIP_URI_IN_ROUTING_HDR, /**< The URI is in Route/Record-Route header. */
  141. PJSIP_URI_IN_OTHER /**< Other context (web page, business card, etc.) */
  142. } pjsip_uri_context_e;
  143. /**
  144. * URI 'virtual' function table.
  145. * All types of URI in this library (such as sip:, sips:, tel:, and name-addr)
  146. * will have pointer to this table as their first struct member. This table
  147. * provides polimorphic behaviour to the URI.
  148. */
  149. typedef struct pjsip_uri_vptr
  150. {
  151. /**
  152. * Get URI scheme.
  153. * @param uri the URI (self).
  154. * @return the URI scheme.
  155. */
  156. const pj_str_t* (*p_get_scheme)(const void *uri);
  157. /**
  158. * Get the URI object contained by this URI, or the URI itself if
  159. * it doesn't contain another URI.
  160. * @param uri the URI (self).
  161. */
  162. void* (*p_get_uri)(void *uri);
  163. /**
  164. * Print URI components to the buffer, following the rule of which
  165. * components are allowed for the context.
  166. * @param context the context where the URI will be placed.
  167. * @param uri the URI (self).
  168. * @param buf the buffer.
  169. * @param size the size of the buffer.
  170. * @return the length printed.
  171. */
  172. pj_ssize_t (*p_print)(pjsip_uri_context_e context,
  173. const void *uri,
  174. char *buf, pj_size_t size);
  175. /**
  176. * Compare two URIs according to the context.
  177. * @param context the context.
  178. * @param uri1 the first URI (self).
  179. * @param uri2 the second URI.
  180. * @return PJ_SUCCESS if equal, or otherwise the error status which
  181. * should point to the mismatch part.
  182. */
  183. pj_status_t (*p_compare)(pjsip_uri_context_e context,
  184. const void *uri1, const void *uri2);
  185. /**
  186. * Clone URI.
  187. * @param pool the pool.
  188. * @param the URI to clone (self).
  189. * @return new URI.
  190. */
  191. void *(*p_clone)(pj_pool_t *pool, const void *uri);
  192. } pjsip_uri_vptr;
  193. /**
  194. * The declaration of 'base class' for all URI scheme.
  195. */
  196. struct pjsip_uri
  197. {
  198. /** All URIs must have URI virtual function table as their first member. */
  199. pjsip_uri_vptr *vptr;
  200. };
  201. /**
  202. * This macro checks that the URL is a "sip:" URL.
  203. * @param url The URL (pointer to)
  204. * @return non-zero if TRUE.
  205. */
  206. #define PJSIP_URI_SCHEME_IS_SIP(url) \
  207. (pj_stricmp2(pjsip_uri_get_scheme(url), "sip")==0)
  208. /**
  209. * This macro checks that the URL is a "sips:" URL (not SIP).
  210. * @param url The URL (pointer to)
  211. * @return non-zero if TRUE.
  212. */
  213. #define PJSIP_URI_SCHEME_IS_SIPS(url) \
  214. (pj_stricmp2(pjsip_uri_get_scheme(url), "sips")==0)
  215. /**
  216. * This macro checks that the URL is a "tel:" URL.
  217. * @param url The URL (pointer to)
  218. * @return non-zero if TRUE.
  219. */
  220. #define PJSIP_URI_SCHEME_IS_TEL(url) \
  221. (pj_stricmp2(pjsip_uri_get_scheme(url), "tel")==0)
  222. /**
  223. * Generic function to get the URI scheme.
  224. * @param uri the URI object.
  225. * @return the URI scheme.
  226. */
  227. PJ_INLINE(const pj_str_t*) pjsip_uri_get_scheme(const void *uri)
  228. {
  229. return (*((pjsip_uri*)uri)->vptr->p_get_scheme)(uri);
  230. }
  231. /**
  232. * Generic function to get the URI object contained by this URI, or the URI
  233. * itself if it doesn't contain another URI.
  234. *
  235. * @param uri the URI.
  236. * @return the URI.
  237. */
  238. PJ_INLINE(void*) pjsip_uri_get_uri(const void *uri)
  239. {
  240. PJ_ASSERT_RETURN(uri, NULL);
  241. return (*((pjsip_uri*)uri)->vptr->p_get_uri)((void*)uri);
  242. }
  243. /**
  244. * Generic function to compare two URIs.
  245. *
  246. * @param context Comparison context.
  247. * @param uri1 The first URI.
  248. * @param uri2 The second URI.
  249. * @return PJ_SUCCESS if equal, or otherwise the error status which
  250. * should point to the mismatch part.
  251. */
  252. PJ_INLINE(pj_status_t) pjsip_uri_cmp(pjsip_uri_context_e context,
  253. const void *uri1, const void *uri2)
  254. {
  255. return (*((const pjsip_uri*)uri1)->vptr->p_compare)(context, uri1, uri2);
  256. }
  257. /**
  258. * Generic function to print an URI object.
  259. *
  260. * @param context Print context.
  261. * @param uri The URI to print.
  262. * @param buf The buffer.
  263. * @param size Size of the buffer.
  264. * @return Length printed if successful, negative value if failed.
  265. */
  266. PJ_INLINE(int) pjsip_uri_print(pjsip_uri_context_e context,
  267. const void *uri,
  268. char *buf, pj_size_t size)
  269. {
  270. return (int)(*((const pjsip_uri*)uri)->vptr->p_print)(context, uri,
  271. buf, size);
  272. }
  273. /**
  274. * Generic function to clone an URI object.
  275. *
  276. * @param pool Pool.
  277. * @param uri URI to clone.
  278. * @return New URI.
  279. */
  280. PJ_INLINE(void*) pjsip_uri_clone( pj_pool_t *pool, const void *uri )
  281. {
  282. return (*((const pjsip_uri*)uri)->vptr->p_clone)(pool, uri);
  283. }
  284. /**
  285. * @}
  286. */
  287. /**
  288. * @defgroup PJSIP_SIP_URI SIP URI Scheme and Name address
  289. * @ingroup PJSIP_URI
  290. * @brief SIP URL structure ("sip:" and "sips:")
  291. * @{
  292. */
  293. /**
  294. * SIP and SIPS URL scheme.
  295. */
  296. typedef struct pjsip_sip_uri
  297. {
  298. pjsip_uri_vptr *vptr; /**< Pointer to virtual function table.*/
  299. pj_str_t user; /**< Optional user part. */
  300. pj_str_t passwd; /**< Optional password part. */
  301. pj_str_t orig_userpass; /**< Optional original user&pass. */
  302. pj_str_t host; /**< Host part, always exists. */
  303. int port; /**< Optional port number, or zero. */
  304. pj_str_t user_param; /**< Optional user parameter */
  305. pj_str_t method_param; /**< Optional method parameter. */
  306. pj_str_t transport_param; /**< Optional transport parameter. */
  307. int ttl_param; /**< Optional TTL param, or -1. */
  308. int lr_param; /**< Optional loose routing param, or zero */
  309. pj_str_t maddr_param; /**< Optional maddr param */
  310. pjsip_param other_param; /**< Other parameters grouped together. */
  311. pjsip_param header_param; /**< Optional header parameter. */
  312. } pjsip_sip_uri;
  313. /**
  314. * SIP name-addr, which typically appear in From, To, and Contact header.
  315. * The SIP name-addr contains a generic URI and a display name.
  316. */
  317. typedef struct pjsip_name_addr
  318. {
  319. /** Pointer to virtual function table. */
  320. pjsip_uri_vptr *vptr;
  321. /** Optional display name. */
  322. pj_str_t display;
  323. /** URI part. */
  324. pjsip_uri *uri;
  325. } pjsip_name_addr;
  326. /**
  327. * Create new SIP URL and initialize all fields with zero or NULL.
  328. * @param pool The pool.
  329. * @param secure Flag to indicate whether secure transport should be used.
  330. * @return SIP URL.
  331. */
  332. PJ_DECL(pjsip_sip_uri*) pjsip_sip_uri_create( pj_pool_t *pool,
  333. pj_bool_t secure );
  334. /**
  335. * Change the SIP URI scheme to sip or sips based on the secure flag.
  336. * This would not change anything except the scheme.
  337. * @param uri The URI
  338. * @param secure Non-zero if sips is wanted.
  339. */
  340. PJ_DECL(void) pjsip_sip_uri_set_secure( pjsip_sip_uri *uri,
  341. pj_bool_t secure );
  342. /**
  343. * Initialize SIP URL (all fields are set to NULL or zero).
  344. * @param url The URL.
  345. * @param secure Create sips URI?
  346. */
  347. PJ_DECL(void) pjsip_sip_uri_init(pjsip_sip_uri *url, pj_bool_t secure);
  348. /**
  349. * Perform full assignment to the SIP URL.
  350. * @param pool The pool.
  351. * @param url Destination URL.
  352. * @param rhs The source URL.
  353. */
  354. PJ_DECL(void) pjsip_sip_uri_assign(pj_pool_t *pool, pjsip_sip_uri *url,
  355. const pjsip_sip_uri *rhs);
  356. /**
  357. * Create new instance of name address and initialize all fields with zero or
  358. * NULL.
  359. * @param pool The pool.
  360. * @return New SIP name address.
  361. */
  362. PJ_DECL(pjsip_name_addr*) pjsip_name_addr_create(pj_pool_t *pool);
  363. /**
  364. * Initialize with default value.
  365. * @param name_addr The name address.
  366. */
  367. PJ_DECL(void) pjsip_name_addr_init(pjsip_name_addr *name_addr);
  368. /**
  369. * Perform full assignment to the name address.
  370. * @param pool The pool.
  371. * @param addr The destination name address.
  372. * @param rhs The source name address.
  373. */
  374. PJ_DECL(void) pjsip_name_addr_assign(pj_pool_t *pool,
  375. pjsip_name_addr *addr,
  376. const pjsip_name_addr *rhs);
  377. /**
  378. * @}
  379. */
  380. /**
  381. * @defgroup PJSIP_OTHER_URI Other URI schemes
  382. * @ingroup PJSIP_URI
  383. * @brief Container for non SIP/tel URI scheme (e.g. "http:", "mailto:")
  384. * @{
  385. */
  386. /**
  387. * Generic URI container for non SIP/tel URI scheme.
  388. */
  389. typedef struct pjsip_other_uri
  390. {
  391. pjsip_uri_vptr *vptr; /**< Pointer to virtual function table. */
  392. pj_str_t scheme; /**< The URI scheme (e.g. "mailto") */
  393. pj_str_t content; /**< The whole URI content */
  394. } pjsip_other_uri;
  395. /**
  396. * Create a generic URI object.
  397. *
  398. * @param pool The pool to allocate memory from.
  399. *
  400. * @return The URI instance.
  401. */
  402. PJ_DECL(pjsip_other_uri*) pjsip_other_uri_create(pj_pool_t *pool);
  403. /**
  404. * @}
  405. */
  406. PJ_END_DECL
  407. #endif /* __PJSIP_URL_H__ */