sip_caller_prefs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * This file is part of the Sofia-SIP package
  3. *
  4. * Copyright (C) 2005 Nokia Corporation.
  5. *
  6. * Contact: Pekka Pessi <pekka.pessi@nokia.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation; either version 2.1 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  21. * 02110-1301 USA
  22. *
  23. */
  24. /**@CFILE sip_caller_prefs.c
  25. * @brief SIP headers related to Caller Preferences
  26. *
  27. * Implementation of header classes for Caller-Preferences-related SIP
  28. * headers @AcceptContact, @RejectContact, and @RequestDisposition.
  29. *
  30. * @author Remeres Jacobs <remeres.jacobs@nokia.com>
  31. * @author Pekka Pessi <Pekka.Pessi@nokia.com>
  32. *
  33. * @date Created: Th 23.01.2003
  34. */
  35. #include "config.h"
  36. /* Avoid casting sip_t to msg_pub_t and sip_header_t to msg_header_t */
  37. #define MSG_PUB_T struct sip_s
  38. #define MSG_HDR_T union sip_header_u
  39. #include "sofia-sip/sip_parser.h"
  40. #include <stddef.h>
  41. #include <stdlib.h>
  42. #include <string.h>
  43. #include <stdio.h>
  44. #include <assert.h>
  45. /* ====================================================================== */
  46. /**@SIP_HEADER sip_request_disposition Request-Disposition Header
  47. *
  48. * The Request-Disposition header syntax is defined in
  49. * @RFC3841 section 10 as follows:
  50. *
  51. * @code
  52. * Request-Disposition = ( "Request-Disposition" | "d" ) HCOLON
  53. * directive *(COMMA directive)
  54. * directive = proxy-directive / cancel-directive /
  55. * fork-directive / recurse-directive /
  56. * parallel-directive / queue-directive)
  57. * proxy-directive = "proxy" / "redirect"
  58. * cancel-directive = "cancel" / "no-cancel"
  59. * fork-directive = "fork" / "no-fork"
  60. * recurse-directive = "recurse" / "no-recurse"
  61. * parallel-directive = "parallel" / "sequential"
  62. * queue-directive = "queue" / "no-queue"
  63. * @endcode
  64. *
  65. *
  66. * The parsed Request-Disposition header
  67. * is stored in #sip_request_disposition_t structure.
  68. */
  69. /**@ingroup sip_request_disposition
  70. * @typedef typedef struct sip_request_disposition_s sip_request_disposition_t;
  71. *
  72. * The structure #sip_request_disposition_t contains representation of
  73. * @RequestDisposition header.
  74. *
  75. * The #sip_request_disposition_t is defined as follows:
  76. * @code
  77. * typedef struct sip_request_disposition_s
  78. * {
  79. * sip_common_t rd_common[1]; // Common fragment info
  80. * sip_unknown_t *rd_next; // Link to next (dummy)
  81. * msg_param_t *rd_items; // List of directives
  82. * } sip_request_disposition_t;
  83. * @endcode
  84. */
  85. static msg_xtra_f sip_request_disposition_dup_xtra;
  86. static msg_dup_f sip_request_disposition_dup_one;
  87. #define sip_request_disposition_update NULL
  88. msg_hclass_t sip_request_disposition_class[] =
  89. SIP_HEADER_CLASS(request_disposition, "Request-Disposition", "d", rd_items,
  90. list, request_disposition);
  91. issize_t sip_request_disposition_d(su_home_t *home, sip_header_t *h,
  92. char *s, isize_t slen)
  93. {
  94. sip_request_disposition_t *rd = h->sh_request_disposition;
  95. return msg_commalist_d(home, &s, &rd->rd_items, msg_token_scan);
  96. }
  97. issize_t sip_request_disposition_e(char b[], isize_t bsiz, sip_header_t const *h, int flags)
  98. {
  99. char *end = b + bsiz, *b0 = b;
  100. sip_request_disposition_t const *o = h->sh_request_disposition;
  101. assert(sip_is_request_disposition(h));
  102. MSG_COMMALIST_E(b, end, o->rd_items, flags);
  103. return b - b0;
  104. }
  105. isize_t sip_request_disposition_dup_xtra(sip_header_t const *h, isize_t offset)
  106. {
  107. sip_request_disposition_t const *o = h->sh_request_disposition;
  108. MSG_PARAMS_SIZE(offset, o->rd_items);
  109. return offset;
  110. }
  111. /** Duplicate one #sip_request_disposition_t object */
  112. char *sip_request_disposition_dup_one(sip_header_t *dst,
  113. sip_header_t const *src,
  114. char *b, isize_t xtra)
  115. {
  116. char *end = b + xtra;
  117. sip_request_disposition_t *o_dst = dst->sh_request_disposition;
  118. sip_request_disposition_t const *o_src = src->sh_request_disposition;
  119. msg_param_t const **dst_items = (msg_param_t const **)&o_dst->rd_items;
  120. b = msg_params_dup(dst_items, o_src->rd_items, b, xtra);
  121. assert(b <= end); (void)end;
  122. return b;
  123. }
  124. /* ====================================================================== */
  125. /**@ingroup sip_caller_prefs
  126. *
  127. * Add a parameter to a @AcceptContact or @RejectContact header object.
  128. *
  129. * @note This function does not duplicate @p param.
  130. *
  131. * @param home memory home
  132. * @param cp pointer to #sip_accept_contact_t or #sip_reject_contact_t
  133. * @param param parameter string
  134. *
  135. * @retval 0 when successful
  136. * @retval -1 upon an error
  137. *
  138. * @deprecated Use msg_header_replace_param() instead.
  139. */
  140. int sip_caller_prefs_add_param(su_home_t *home,
  141. sip_caller_prefs_t *cp,
  142. char const *param)
  143. {
  144. return msg_header_replace_param(home, cp->cp_common, param);
  145. }
  146. static
  147. size_t span_attribute_value(char *s)
  148. {
  149. size_t n;
  150. n = span_token_lws(s);
  151. if (n > 0 && s[n] == '=') {
  152. n += 1 + span_lws(s + n + 1);
  153. if (s[n] == '"')
  154. n += span_quoted(s + n);
  155. else
  156. n += span_token(s + n);
  157. n += span_lws(s + n);
  158. }
  159. return n;
  160. }
  161. static
  162. issize_t sip_caller_prefs_d(su_home_t *home, sip_header_t *h,
  163. char *s, isize_t slen)
  164. {
  165. for(;;) {
  166. sip_caller_prefs_t *cp = (sip_caller_prefs_t *)h;
  167. url_t url[1];
  168. char const *ignore = NULL;
  169. int kludge = 0;
  170. assert(h);
  171. while (*s == ',') /* Ignore empty entries (comma-whitespace) */
  172. *s = '\0', s += span_lws(s + 1) + 1;
  173. /* Kludge: support PoC IS spec with a typo... */
  174. if (su_casenmatch(s, "*,", 2))
  175. s[1] = ';', kludge = 0;
  176. else if (s[0] != '*' && s[0] != '<') {
  177. /* Kludge: missing URL - */
  178. size_t n = span_attribute_value(s);
  179. kludge = n > 0 && (s[n] == '\0' || s[n] == ',' || s[n] == ';');
  180. }
  181. if (kludge) {
  182. if (msg_any_list_d(home, &s, (msg_param_t **)&cp->cp_params,
  183. msg_attribute_value_scanner, ';') == -1)
  184. return -1;
  185. }
  186. /* Parse params (and ignore display name and url) */
  187. else if (sip_name_addr_d(home, &s, &ignore, url, &cp->cp_params, NULL)
  188. == -1)
  189. return -1;
  190. /* Be liberal... */
  191. /* if (url->url_type != url_any)
  192. return -1; */
  193. msg_parse_next_field_without_recursion();
  194. }
  195. }
  196. static
  197. issize_t sip_caller_prefs_e(char b[], isize_t bsiz, sip_header_t const *h, int flags)
  198. {
  199. sip_caller_prefs_t const *cp = h->sh_caller_prefs;
  200. char *b0 = b, *end = b + bsiz;
  201. MSG_CHAR_E(b, end, '*');
  202. MSG_PARAMS_E(b, end, cp->cp_params, flags);
  203. MSG_TERM_E(b, end);
  204. return b - b0;
  205. }
  206. static
  207. isize_t sip_caller_prefs_dup_xtra(sip_header_t const *h, isize_t offset)
  208. {
  209. sip_caller_prefs_t const *cp = h->sh_caller_prefs;
  210. MSG_PARAMS_SIZE(offset, cp->cp_params);
  211. return offset;
  212. }
  213. static
  214. char *sip_caller_prefs_dup_one(sip_header_t *dst, sip_header_t const *src,
  215. char *b, isize_t xtra)
  216. {
  217. char *end = b + xtra;
  218. sip_caller_prefs_t *cp = dst->sh_caller_prefs;
  219. sip_caller_prefs_t const *o = src->sh_caller_prefs;
  220. b = msg_params_dup(&cp->cp_params, o->cp_params, b, xtra);
  221. assert(b <= end); (void)end;
  222. return b;
  223. }
  224. /**@SIP_HEADER sip_accept_contact Accept-Contact Header
  225. *
  226. * The Accept-Contact syntax is defined in @RFC3841 section 10 as follows:
  227. *
  228. * @code
  229. * Accept-Contact = ("Accept-Contact" / "a") HCOLON ac-value
  230. * *(COMMA ac-value)
  231. * ac-value = "*" *(SEMI ac-params)
  232. * ac-params = feature-param / req-param
  233. * / explicit-param / generic-param
  234. * ;;feature param from RFC 3840
  235. * ;;generic-param from RFC 3261
  236. * req-param = "require"
  237. * explicit-param = "explicit"
  238. * @endcode
  239. *
  240. * Despite the BNF, there MUST NOT be more than one req-param or
  241. * explicit-param in an ac-params. Furthermore, there can only be one
  242. * instance of any feature tag in feature-param.
  243. *
  244. * @sa @RFC3840, @RFC3841, sip_contact_accept(), sip_contact_score().
  245. *
  246. * The parsed Accept-Contact header
  247. * is stored in #sip_accept_contact_t structure.
  248. */
  249. /**@ingroup sip_accept_contact
  250. * @typedef struct sip_caller_prefs_s sip_accept_contact_t;
  251. *
  252. * The structure #sip_accept_contact_t contains representation of SIP
  253. * @AcceptContact header.
  254. *
  255. * The #sip_accept_contact_t is defined as follows:
  256. * @code
  257. * typedef struct caller_prefs_s {
  258. * sip_common_t cp_common[1]; // Common fragment info
  259. * sip_caller_prefs_t *cp_next; // Link to next ac-value
  260. * msg_param_t const *cp_params; // List of parameters
  261. * char const *cp_q; // Priority
  262. * unsigned cp_require :1; // Shortcut to "require" parameter
  263. * unsigned cp_explicit :1; // Shortcut to "explicit" parameter
  264. * } sip_accept_contact_t;
  265. * @endcode
  266. */
  267. #define sip_accept_contact_dup_xtra sip_caller_prefs_dup_xtra
  268. #define sip_accept_contact_dup_one sip_caller_prefs_dup_one
  269. static int sip_accept_contact_update(msg_common_t *h,
  270. char const *name, isize_t namelen,
  271. char const *value);
  272. msg_hclass_t sip_accept_contact_class[] =
  273. SIP_HEADER_CLASS(accept_contact, "Accept-Contact", "a", cp_params, append,
  274. accept_contact);
  275. issize_t sip_accept_contact_d(su_home_t *home, sip_header_t *h, char *s, isize_t slen)
  276. {
  277. return sip_caller_prefs_d(home, h, s, slen);
  278. }
  279. issize_t sip_accept_contact_e(char b[], isize_t bsiz, sip_header_t const *h, int flags)
  280. {
  281. return sip_caller_prefs_e(b, bsiz, h, flags);
  282. }
  283. static int sip_accept_contact_update(msg_common_t *h,
  284. char const *name, isize_t namelen,
  285. char const *value)
  286. {
  287. sip_caller_prefs_t *cp = (sip_caller_prefs_t *)h;
  288. if (name == NULL) {
  289. cp->cp_q = NULL;
  290. cp->cp_require = 0;
  291. cp->cp_explicit = 0;
  292. }
  293. #define MATCH(s) (namelen == strlen(#s) && su_casenmatch(name, #s, strlen(#s)))
  294. #if nomore
  295. else if (MATCH(q)) {
  296. cp->cp_q = value;
  297. }
  298. #endif
  299. else if (MATCH(require)) {
  300. cp->cp_require = value != NULL;
  301. }
  302. else if (MATCH(explicit)) {
  303. cp->cp_explicit = value != NULL;
  304. }
  305. #undef MATCH
  306. return 0;
  307. }
  308. /**@SIP_HEADER sip_reject_contact Reject-Contact Header
  309. *
  310. * The Reject-Contact syntax is defined in @RFC3841 section 10 as follows:
  311. *
  312. * @code
  313. * Reject-Contact = ("Reject-Contact" / "j") HCOLON rc-value
  314. * *(COMMA rc-value)
  315. * rc-value = "*" *(SEMI rc-params)
  316. * rc-params = feature-param / generic-param
  317. * ;;feature param from RFC 3840
  318. * ;;generic-param from RFC 3261
  319. * @endcode
  320. *
  321. * Despite the BNF, there MUST NOT be more than one instance of any feature
  322. * tag in feature-param.
  323. *
  324. * @sa @RFC3840, @RFC3841, sip_contact_reject(), sip_contact_score().
  325. *
  326. * The parsed Reject-Contact header
  327. * is stored in #sip_reject_contact_t structure.
  328. */
  329. /**@ingroup sip_reject_contact
  330. * @typedef struct sip_caller_prefs_s sip_reject_contact_t;
  331. *
  332. * The structure #sip_reject_contact_t contains representation of SIP
  333. * @RejectContact header.
  334. *
  335. * The #sip_reject_contact_t is defined as follows:
  336. * @code
  337. * typedef struct caller_prefs_s {
  338. * sip_common_t cp_common[1]; // Common fragment info
  339. * sip_caller_prefs_t *cp_next; // Link to next rc-value
  340. * msg_param_t const *cp_params; // List of parameters
  341. * } sip_reject_contact_t;
  342. * @endcode
  343. *
  344. * @note Fields @c cp_q, @c cp_require and @c cp_explicit are ignored for
  345. * @RejectContact header.
  346. */
  347. #define sip_reject_contact_dup_xtra sip_caller_prefs_dup_xtra
  348. #define sip_reject_contact_dup_one sip_caller_prefs_dup_one
  349. #define sip_reject_contact_update NULL
  350. msg_hclass_t sip_reject_contact_class[] =
  351. SIP_HEADER_CLASS(reject_contact, "Reject-Contact", "j", cp_params, append,
  352. reject_contact);
  353. issize_t sip_reject_contact_d(su_home_t *home, sip_header_t *h, char *s, isize_t slen)
  354. {
  355. return sip_caller_prefs_d(home, h, s, slen);
  356. }
  357. issize_t sip_reject_contact_e(char b[], isize_t bsiz, sip_header_t const *h, int flags)
  358. {
  359. return sip_caller_prefs_e(b, bsiz, h, flags);
  360. }