2
0

dset.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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((int)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 && (int)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,
  255. struct socket_info* force_socket,
  256. str* instance, unsigned int reg_id)
  257. {
  258. str luri;
  259. /* if we have already set up the maximum number
  260. * of branches, don't try new ones
  261. */
  262. if (unlikely(nr_branches == MAX_BRANCHES - 1)) {
  263. LOG(L_ERR, "max nr of branches exceeded\n");
  264. ser_error = E_TOO_MANY_BRANCHES;
  265. return -1;
  266. }
  267. /* if not parameterized, take current uri */
  268. if (uri==0 || uri->len==0 || uri->s==0) {
  269. if (msg->new_uri.s)
  270. luri = msg->new_uri;
  271. else
  272. luri = msg->first_line.u.request.uri;
  273. } else {
  274. luri = *uri;
  275. }
  276. if (unlikely(luri.len > MAX_URI_SIZE - 1)) {
  277. LOG(L_ERR, "too long uri: %.*s\n", luri.len, luri.s);
  278. return -1;
  279. }
  280. /* copy the dst_uri */
  281. if (dst_uri && dst_uri->len && dst_uri->s) {
  282. if (unlikely(dst_uri->len > MAX_URI_SIZE - 1)) {
  283. LOG(L_ERR, "too long dst_uri: %.*s\n", dst_uri->len, dst_uri->s);
  284. return -1;
  285. }
  286. memcpy(branches[nr_branches].dst_uri, dst_uri->s, dst_uri->len);
  287. branches[nr_branches].dst_uri[dst_uri->len] = 0;
  288. branches[nr_branches].dst_uri_len = dst_uri->len;
  289. } else {
  290. branches[nr_branches].dst_uri[0] = '\0';
  291. branches[nr_branches].dst_uri_len = 0;
  292. }
  293. /* copy the path string */
  294. if (unlikely(path && path->len && path->s)) {
  295. if (unlikely(path->len > MAX_PATH_SIZE - 1)) {
  296. LOG(L_ERR, "too long path: %.*s\n", path->len, path->s);
  297. return -1;
  298. }
  299. memcpy(branches[nr_branches].path, path->s, path->len);
  300. branches[nr_branches].path[path->len] = 0;
  301. branches[nr_branches].path_len = path->len;
  302. } else {
  303. branches[nr_branches].path[0] = '\0';
  304. branches[nr_branches].path_len = 0;
  305. }
  306. /* copy the ruri */
  307. memcpy(branches[nr_branches].uri, luri.s, luri.len);
  308. branches[nr_branches].uri[luri.len] = 0;
  309. branches[nr_branches].len = luri.len;
  310. branches[nr_branches].q = q;
  311. branches[nr_branches].force_send_socket = force_socket;
  312. branches[nr_branches].flags = flags;
  313. /* copy instance string */
  314. if (unlikely(instance && instance->len && instance->s)) {
  315. if (unlikely(instance->len > MAX_INSTANCE_SIZE - 1)) {
  316. LOG(L_ERR, "too long instance: %.*s\n",
  317. instance->len, instance->s);
  318. return -1;
  319. }
  320. memcpy(branches[nr_branches].instance, instance->s,
  321. instance->len);
  322. branches[nr_branches].instance[instance->len] = 0;
  323. branches[nr_branches].instance_len = instance->len;
  324. } else {
  325. branches[nr_branches].instance[0] = '\0';
  326. branches[nr_branches].instance_len = 0;
  327. }
  328. /* copy reg_id */
  329. branches[nr_branches].reg_id = reg_id;
  330. nr_branches++;
  331. return 1;
  332. }
  333. /*
  334. * Create a Contact header field from the dset
  335. * array
  336. */
  337. char* print_dset(struct sip_msg* msg, int* len)
  338. {
  339. int cnt, i;
  340. unsigned int qlen;
  341. qvalue_t q;
  342. str uri;
  343. char* p, *qbuf;
  344. int crt_branch;
  345. static char dset[MAX_REDIRECTION_LEN];
  346. if (msg->new_uri.s) {
  347. cnt = 1;
  348. *len = msg->new_uri.len + 1 /*'<'*/;
  349. if (ruri_q != Q_UNSPECIFIED) {
  350. *len += Q_PARAM_LEN + len_q(ruri_q);
  351. } else {
  352. *len += 1 /*'>'*/;
  353. }
  354. } else {
  355. cnt = 0;
  356. *len = 0;
  357. }
  358. /* backup current branch index to restore it later */
  359. crt_branch = get_branch_iterator();
  360. init_branch_iterator();
  361. while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0))) {
  362. cnt++;
  363. *len += uri.len + 1 /*'<'*/;
  364. if (q != Q_UNSPECIFIED) {
  365. *len += Q_PARAM_LEN + len_q(q);
  366. } else {
  367. *len += 1 /*'>'*/;
  368. }
  369. }
  370. if (cnt == 0) return 0;
  371. *len += CONTACT_LEN + CRLF_LEN + (cnt - 1) * CONTACT_DELIM_LEN;
  372. if (*len + 1 > MAX_REDIRECTION_LEN) {
  373. LOG(L_ERR, "ERROR: redirection buffer length exceed\n");
  374. goto error;
  375. }
  376. memcpy(dset, CONTACT, CONTACT_LEN);
  377. p = dset + CONTACT_LEN;
  378. if (msg->new_uri.s) {
  379. *p++ = '<';
  380. memcpy(p, msg->new_uri.s, msg->new_uri.len);
  381. p += msg->new_uri.len;
  382. if (ruri_q != Q_UNSPECIFIED) {
  383. memcpy(p, Q_PARAM, Q_PARAM_LEN);
  384. p += Q_PARAM_LEN;
  385. qbuf = q2str(ruri_q, &qlen);
  386. memcpy(p, qbuf, qlen);
  387. p += qlen;
  388. } else {
  389. *p++ = '>';
  390. }
  391. i = 1;
  392. } else {
  393. i = 0;
  394. }
  395. init_branch_iterator();
  396. while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0))) {
  397. if (i) {
  398. memcpy(p, CONTACT_DELIM, CONTACT_DELIM_LEN);
  399. p += CONTACT_DELIM_LEN;
  400. }
  401. *p++ = '<';
  402. memcpy(p, uri.s, uri.len);
  403. p += uri.len;
  404. if (q != Q_UNSPECIFIED) {
  405. memcpy(p, Q_PARAM, Q_PARAM_LEN);
  406. p += Q_PARAM_LEN;
  407. qbuf = q2str(q, &qlen);
  408. memcpy(p, qbuf, qlen);
  409. p += qlen;
  410. } else {
  411. *p++ = '>';
  412. }
  413. i++;
  414. }
  415. memcpy(p, CRLF " ", CRLF_LEN + 1);
  416. set_branch_iterator(crt_branch);
  417. return dset;
  418. error:
  419. set_branch_iterator(crt_branch);
  420. return 0;
  421. }
  422. /*
  423. * Sets the q parameter of the Request-URI
  424. */
  425. void set_ruri_q(qvalue_t q)
  426. {
  427. ruri_q = q;
  428. }
  429. /*
  430. * Return the q value of the Request-URI
  431. */
  432. qvalue_t get_ruri_q(void)
  433. {
  434. return ruri_q;
  435. }
  436. /*
  437. * Rewrite Request-URI
  438. */
  439. int rewrite_uri(struct sip_msg* _m, str* _s)
  440. {
  441. char *buf = NULL;
  442. if(_m->new_uri.s==NULL || _m->new_uri.len<_s->len) {
  443. buf = (char*)pkg_malloc(_s->len + 1);
  444. if (!buf) {
  445. LM_ERR("No memory left to rewrite r-uri\n");
  446. return -1;
  447. }
  448. }
  449. if(buf!=NULL) {
  450. if(_m->new_uri.s)
  451. pkg_free(_m->new_uri.s);
  452. } else {
  453. buf = _m->new_uri.s;
  454. }
  455. memcpy(buf, _s->s, _s->len);
  456. buf[_s->len] = '\0';
  457. _m->parsed_uri_ok = 0;
  458. _m->new_uri.s = buf;
  459. _m->new_uri.len = _s->len;
  460. /* mark ruri as new and available for forking */
  461. ruri_mark_new();
  462. return 1;
  463. }