2
0

dset.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /*
  2. * $Id$
  3. *
  4. * destination set
  5. *
  6. * Copyright (C) 2001-2004 FhG FOKUS
  7. *
  8. * This file is part of ser, a free SIP server.
  9. *
  10. * ser is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version
  14. *
  15. * For a license to use the ser software under conditions
  16. * other than those described here, or to purchase support for this
  17. * software, please contact iptel.org by e-mail at the following addresses:
  18. * [email protected]
  19. *
  20. * ser is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. */
  29. /** destination set / branches support.
  30. * @file dset.c
  31. * @ingroup core
  32. * Module: @ref core
  33. */
  34. #include <string.h>
  35. #include "dprint.h"
  36. #include "config.h"
  37. #include "parser/parser_f.h"
  38. #include "parser/msg_parser.h"
  39. #include "ut.h"
  40. #include "hash_func.h"
  41. #include "error.h"
  42. #include "dset.h"
  43. #include "mem/mem.h"
  44. #include "ip_addr.h"
  45. #define CONTACT "Contact: "
  46. #define CONTACT_LEN (sizeof(CONTACT) - 1)
  47. #define CONTACT_DELIM ", "
  48. #define CONTACT_DELIM_LEN (sizeof(CONTACT_DELIM) - 1)
  49. #define Q_PARAM ">;q="
  50. #define Q_PARAM_LEN (sizeof(Q_PARAM) - 1)
  51. /*
  52. * Where we store URIs of additional transaction branches
  53. * (-1 because of the default branch, #0)
  54. */
  55. static struct branch branches[MAX_BRANCHES - 1];
  56. /* how many of them we have */
  57. unsigned int nr_branches = 0;
  58. /* branch iterator */
  59. static int branch_iterator = 0;
  60. /* used to mark ruris "consumed" when branching (1 new, 0 consumed) */
  61. int ruri_is_new = 0;
  62. /* The q parameter of the Request-URI */
  63. static qvalue_t ruri_q = Q_UNSPECIFIED;
  64. /* Branch flags of the Request-URI */
  65. static flag_t ruri_bflags;
  66. /*! \brief
  67. * Return pointer to branch[idx] structure
  68. * @param idx - branch index
  69. *
  70. * @return pointer to branch or NULL if invalid branch
  71. */
  72. branch_t *get_sip_branch(int idx)
  73. {
  74. if(nr_branches==0)
  75. return NULL;
  76. if(idx<0)
  77. {
  78. if(nr_branches + idx >= 0)
  79. return &branches[nr_branches+idx];
  80. return NULL;
  81. }
  82. if(idx < nr_branches)
  83. return &branches[idx];
  84. return 0;
  85. }
  86. /*! \brief
  87. * Drop branch[idx]
  88. * @param idx - branch index
  89. *
  90. * @return 0 on success, -1 on error
  91. */
  92. int drop_sip_branch(int idx)
  93. {
  94. if(nr_branches==0 || idx>=nr_branches)
  95. return 0;
  96. if(idx<0 && nr_branches+idx<0)
  97. return 0;
  98. /* last branch */
  99. if(idx==nr_branches-1)
  100. {
  101. nr_branches--;
  102. return 0;
  103. }
  104. if(idx<0)
  105. idx = nr_branches+idx;
  106. /* shift back one position */
  107. for(; idx<nr_branches-1; idx++)
  108. memcpy(&branches[idx], &branches[idx+1], sizeof(branch_t));
  109. nr_branches--;
  110. return 0;
  111. }
  112. static inline flag_t* get_bflags_ptr(unsigned int branch)
  113. {
  114. if (branch == 0) return &ruri_bflags;
  115. if (branch - 1 < nr_branches) return &branches[branch - 1].flags;
  116. return NULL;
  117. }
  118. int setbflag(unsigned int branch, flag_t flag)
  119. {
  120. flag_t* flags;
  121. if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
  122. (*flags) |= 1 << flag;
  123. return 1;
  124. }
  125. int isbflagset(unsigned int branch, flag_t flag)
  126. {
  127. flag_t* flags;
  128. if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
  129. return ((*flags) & (1 << flag)) ? 1 : -1;
  130. }
  131. int resetbflag(unsigned int branch, flag_t flag)
  132. {
  133. flag_t* flags;
  134. if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
  135. (*flags) &= ~ (1 << flag);
  136. return 1;
  137. }
  138. int getbflagsval(unsigned int branch, flag_t* res)
  139. {
  140. flag_t* flags;
  141. if (res == NULL) return -1;
  142. if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
  143. *res = *flags;
  144. return 1;
  145. }
  146. int setbflagsval(unsigned int branch, flag_t val)
  147. {
  148. flag_t* flags;
  149. if ((flags = get_bflags_ptr(branch)) == NULL) return -1;
  150. *flags = val;
  151. return 1;
  152. }
  153. /*
  154. * Initialize the branch iterator, the next
  155. * call to next_branch will return the first
  156. * contact from the dset array
  157. */
  158. void init_branch_iterator(void)
  159. {
  160. branch_iterator = 0;
  161. }
  162. /**
  163. * return the value of current branch iterator
  164. */
  165. int get_branch_iterator(void)
  166. {
  167. return branch_iterator;
  168. }
  169. /**
  170. * set the value of current branch interator
  171. */
  172. void set_branch_iterator(int n)
  173. {
  174. branch_iterator = n;
  175. }
  176. /** \brief Get a branch from the destination set
  177. * \return Return the 'i' branch from the dset
  178. * array, 0 is returned if there are no
  179. * more branches
  180. */
  181. char* get_branch(unsigned int i, int* len, qvalue_t* q, str* dst_uri,
  182. str* path, unsigned int *flags,
  183. struct socket_info** force_socket)
  184. {
  185. if (i < nr_branches) {
  186. *len = branches[i].len;
  187. *q = branches[i].q;
  188. if (dst_uri) {
  189. dst_uri->len = branches[i].dst_uri_len;
  190. dst_uri->s = (dst_uri->len)?branches[i].dst_uri:0;
  191. }
  192. if (path) {
  193. path->len = branches[i].path_len;
  194. path->s = (path->len)?branches[i].path:0;
  195. }
  196. if (force_socket)
  197. *force_socket = branches[i].force_send_socket;
  198. if (flags)
  199. *flags = branches[i].flags;
  200. return branches[i].uri;
  201. } else {
  202. *len = 0;
  203. *q = Q_UNSPECIFIED;
  204. if (dst_uri) {
  205. dst_uri->s = 0;
  206. dst_uri->len = 0;
  207. }
  208. if (path) {
  209. path->s = 0;
  210. path->len = 0;
  211. }
  212. if (force_socket)
  213. *force_socket = 0;
  214. if (flags)
  215. *flags = 0;
  216. return 0;
  217. }
  218. }
  219. /** Return the next branch from the dset array.
  220. * 0 is returned if there are no more branches
  221. */
  222. char* next_branch(int* len, qvalue_t* q, str* dst_uri, str* path,
  223. unsigned int* flags, struct socket_info** force_socket)
  224. {
  225. char* ret;
  226. ret=get_branch(branch_iterator, len, q, dst_uri, path, flags,
  227. force_socket);
  228. if (likely(ret))
  229. branch_iterator++;
  230. return ret;
  231. }
  232. /*
  233. * Empty the dset array
  234. */
  235. void clear_branches(void)
  236. {
  237. nr_branches = 0;
  238. ruri_q = Q_UNSPECIFIED;
  239. ruri_bflags = 0;
  240. ruri_mark_consumed();
  241. }
  242. /** Add a new branch to the current transaction.
  243. * @param msg - sip message, used for getting the uri if not specified (0).
  244. * @param uri - uri, can be 0 (in which case the uri is taken from msg)
  245. * @param dst_uri - destination uri, can be 0.
  246. * @param path - path vector (passed in a string), can be 0.
  247. * @param q - q value.
  248. * @param flags - per branch flags.
  249. * @param force_socket - socket that should be used when sending.
  250. *
  251. * @return <0 (-1) on failure, 1 on success (script convention).
  252. */
  253. int append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
  254. qvalue_t q, unsigned int flags, struct socket_info* force_socket)
  255. {
  256. str luri;
  257. /* if we have already set up the maximum number
  258. * of branches, don't try new ones
  259. */
  260. if (unlikely(nr_branches == MAX_BRANCHES - 1)) {
  261. LOG(L_ERR, "max nr of branches exceeded\n");
  262. ser_error = E_TOO_MANY_BRANCHES;
  263. return -1;
  264. }
  265. /* if not parameterized, take current uri */
  266. if (uri==0 || uri->len==0 || uri->s==0) {
  267. if (msg->new_uri.s)
  268. luri = msg->new_uri;
  269. else
  270. luri = msg->first_line.u.request.uri;
  271. } else {
  272. luri = *uri;
  273. }
  274. if (unlikely(luri.len > MAX_URI_SIZE - 1)) {
  275. LOG(L_ERR, "too long uri: %.*s\n", luri.len, luri.s);
  276. return -1;
  277. }
  278. /* copy the dst_uri */
  279. if (dst_uri && dst_uri->len && dst_uri->s) {
  280. if (unlikely(dst_uri->len > MAX_URI_SIZE - 1)) {
  281. LOG(L_ERR, "too long dst_uri: %.*s\n", dst_uri->len, dst_uri->s);
  282. return -1;
  283. }
  284. memcpy(branches[nr_branches].dst_uri, dst_uri->s, dst_uri->len);
  285. branches[nr_branches].dst_uri[dst_uri->len] = 0;
  286. branches[nr_branches].dst_uri_len = dst_uri->len;
  287. } else {
  288. branches[nr_branches].dst_uri[0] = '\0';
  289. branches[nr_branches].dst_uri_len = 0;
  290. }
  291. /* copy the path string */
  292. if (unlikely(path && path->len && path->s)) {
  293. if (unlikely(path->len > MAX_PATH_SIZE - 1)) {
  294. LOG(L_ERR, "too long path: %.*s\n", path->len, path->s);
  295. return -1;
  296. }
  297. memcpy(branches[nr_branches].path, path->s, path->len);
  298. branches[nr_branches].path[path->len] = 0;
  299. branches[nr_branches].path_len = path->len;
  300. } else {
  301. branches[nr_branches].path[0] = '\0';
  302. branches[nr_branches].path_len = 0;
  303. }
  304. /* copy the ruri */
  305. memcpy(branches[nr_branches].uri, luri.s, luri.len);
  306. branches[nr_branches].uri[luri.len] = 0;
  307. branches[nr_branches].len = luri.len;
  308. branches[nr_branches].q = q;
  309. branches[nr_branches].force_send_socket = force_socket;
  310. branches[nr_branches].flags = flags;
  311. nr_branches++;
  312. return 1;
  313. }
  314. /*
  315. * Create a Contact header field from the dset
  316. * array
  317. */
  318. char* print_dset(struct sip_msg* msg, int* len)
  319. {
  320. int cnt, i;
  321. unsigned int qlen;
  322. qvalue_t q;
  323. str uri;
  324. char* p, *qbuf;
  325. int crt_branch;
  326. static char dset[MAX_REDIRECTION_LEN];
  327. if (msg->new_uri.s) {
  328. cnt = 1;
  329. *len = msg->new_uri.len;
  330. if (ruri_q != Q_UNSPECIFIED) {
  331. *len += 1 + Q_PARAM_LEN + len_q(ruri_q);
  332. }
  333. } else {
  334. cnt = 0;
  335. *len = 0;
  336. }
  337. /* backup current branch index to restore it later */
  338. crt_branch = get_branch_iterator();
  339. init_branch_iterator();
  340. while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0))) {
  341. cnt++;
  342. *len += uri.len;
  343. if (q != Q_UNSPECIFIED) {
  344. *len += 1 + Q_PARAM_LEN + len_q(q);
  345. }
  346. }
  347. if (cnt == 0) return 0;
  348. *len += CONTACT_LEN + CRLF_LEN + (cnt - 1) * CONTACT_DELIM_LEN;
  349. if (*len + 1 > MAX_REDIRECTION_LEN) {
  350. LOG(L_ERR, "ERROR: redirection buffer length exceed\n");
  351. goto error;
  352. }
  353. memcpy(dset, CONTACT, CONTACT_LEN);
  354. p = dset + CONTACT_LEN;
  355. if (msg->new_uri.s) {
  356. if (ruri_q != Q_UNSPECIFIED) {
  357. *p++ = '<';
  358. }
  359. memcpy(p, msg->new_uri.s, msg->new_uri.len);
  360. p += msg->new_uri.len;
  361. if (ruri_q != Q_UNSPECIFIED) {
  362. memcpy(p, Q_PARAM, Q_PARAM_LEN);
  363. p += Q_PARAM_LEN;
  364. qbuf = q2str(ruri_q, &qlen);
  365. memcpy(p, qbuf, qlen);
  366. p += qlen;
  367. }
  368. i = 1;
  369. } else {
  370. i = 0;
  371. }
  372. init_branch_iterator();
  373. while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0))) {
  374. if (i) {
  375. memcpy(p, CONTACT_DELIM, CONTACT_DELIM_LEN);
  376. p += CONTACT_DELIM_LEN;
  377. }
  378. if (q != Q_UNSPECIFIED) {
  379. *p++ = '<';
  380. }
  381. memcpy(p, uri.s, uri.len);
  382. p += uri.len;
  383. if (q != Q_UNSPECIFIED) {
  384. memcpy(p, Q_PARAM, Q_PARAM_LEN);
  385. p += Q_PARAM_LEN;
  386. qbuf = q2str(q, &qlen);
  387. memcpy(p, qbuf, qlen);
  388. p += qlen;
  389. }
  390. i++;
  391. }
  392. memcpy(p, CRLF " ", CRLF_LEN + 1);
  393. set_branch_iterator(crt_branch);
  394. return dset;
  395. error:
  396. set_branch_iterator(crt_branch);
  397. return 0;
  398. }
  399. /*
  400. * Sets the q parameter of the Request-URI
  401. */
  402. void set_ruri_q(qvalue_t q)
  403. {
  404. ruri_q = q;
  405. }
  406. /*
  407. * Return the q value of the Request-URI
  408. */
  409. qvalue_t get_ruri_q(void)
  410. {
  411. return ruri_q;
  412. }
  413. /*
  414. * Rewrite Request-URI
  415. */
  416. int rewrite_uri(struct sip_msg* _m, str* _s)
  417. {
  418. char* buf;
  419. buf = (char*)pkg_malloc(_s->len + 1);
  420. if (!buf) {
  421. LOG(L_ERR, "ERROR: rewrite_uri: No memory left\n");
  422. return -1;
  423. }
  424. memcpy(buf, _s->s, _s->len);
  425. buf[_s->len] = '\0';
  426. _m->parsed_uri_ok = 0;
  427. if (_m->new_uri.s) {
  428. pkg_free(_m->new_uri.s);
  429. }
  430. _m->new_uri.s = buf;
  431. _m->new_uri.len = _s->len;
  432. /* mark ruri as new and available for forking */
  433. ruri_mark_new();
  434. return 1;
  435. }