dset.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. #include <string.h>
  30. #include "dprint.h"
  31. #include "config.h"
  32. #include "parser/parser_f.h"
  33. #include "parser/msg_parser.h"
  34. #include "ut.h"
  35. #include "hash_func.h"
  36. #include "error.h"
  37. #include "dset.h"
  38. #include "mem/mem.h"
  39. #include "ip_addr.h"
  40. #define CONTACT "Contact: "
  41. #define CONTACT_LEN (sizeof(CONTACT) - 1)
  42. #define CONTACT_DELIM ", "
  43. #define CONTACT_DELIM_LEN (sizeof(CONTACT_DELIM) - 1)
  44. #define Q_PARAM ">;q="
  45. #define Q_PARAM_LEN (sizeof(Q_PARAM) - 1)
  46. struct branch
  47. {
  48. char uri[MAX_URI_SIZE];
  49. unsigned int len;
  50. /* Real destination of the request */
  51. char dst_uri[MAX_URI_SIZE];
  52. unsigned int dst_uri_len;
  53. int q; /* Preference of the contact among
  54. * contact within the array */
  55. struct socket_info* force_send_socket;
  56. };
  57. /*
  58. * Where we store URIs of additional transaction branches
  59. * (-1 because of the default branch, #0)
  60. */
  61. static struct branch branches[MAX_BRANCHES - 1];
  62. /* how many of them we have */
  63. unsigned int nr_branches = 0;
  64. /* branch iterator */
  65. static int branch_iterator = 0;
  66. /* The q parameter of the Request-URI */
  67. static qvalue_t ruri_q = Q_UNSPECIFIED;
  68. /*
  69. * Initialize the branch iterator, the next
  70. * call to next_branch will return the first
  71. * contact from the dset array
  72. */
  73. void init_branch_iterator(void)
  74. {
  75. branch_iterator = 0;
  76. }
  77. /*
  78. * Return the next branch from the dset
  79. * array, 0 is returned if there are no
  80. * more branches
  81. */
  82. char* next_branch(int* len, qvalue_t* q, char** dst_uri, int* dst_len, struct socket_info** force_socket)
  83. {
  84. unsigned int i;
  85. i = branch_iterator;
  86. if (i < nr_branches) {
  87. branch_iterator++;
  88. *len = branches[i].len;
  89. *q = branches[i].q;
  90. if (dst_uri && dst_len) {
  91. *dst_uri = branches[i].dst_uri;
  92. *dst_len = branches[i].dst_uri_len;
  93. }
  94. if (force_socket) {
  95. *force_socket = branches[i].force_send_socket;
  96. }
  97. return branches[i].uri;
  98. } else {
  99. *len = 0;
  100. *q = Q_UNSPECIFIED;
  101. if (dst_uri && dst_len) {
  102. *dst_uri = 0;
  103. *dst_len = 0;
  104. }
  105. if (force_socket) {
  106. *force_socket = 0;
  107. }
  108. return 0;
  109. }
  110. }
  111. /*
  112. * Empty the dset array
  113. */
  114. void clear_branches(void)
  115. {
  116. nr_branches = 0;
  117. ruri_q = Q_UNSPECIFIED;
  118. }
  119. /*
  120. * Add a new branch to current transaction
  121. */
  122. int append_branch(struct sip_msg* msg, char* uri, int uri_len, char* dst_uri, int dst_uri_len,
  123. qvalue_t q, struct socket_info* force_socket)
  124. {
  125. /* if we have already set up the maximum number
  126. * of branches, don't try new ones
  127. */
  128. if (nr_branches == MAX_BRANCHES - 1) {
  129. LOG(L_ERR, "ERROR: append_branch: max nr of branches exceeded\n");
  130. ser_error = E_TOO_MANY_BRANCHES;
  131. return -1;
  132. }
  133. if (uri_len > MAX_URI_SIZE - 1) {
  134. LOG(L_ERR, "ERROR: append_branch: too long uri: %.*s\n",
  135. uri_len, uri);
  136. return -1;
  137. }
  138. if (dst_uri_len > MAX_URI_SIZE - 1) {
  139. LOG(L_ERR, "ERROR: append_branch: too long dst_uri: %.*s\n",
  140. dst_uri_len, ZSW(dst_uri));
  141. return -1;
  142. }
  143. /* if not parameterized, take current uri */
  144. if (uri == 0) {
  145. if (msg->new_uri.s) {
  146. uri = msg->new_uri.s;
  147. uri_len = msg->new_uri.len;
  148. } else {
  149. uri = msg->first_line.u.request.uri.s;
  150. uri_len = msg->first_line.u.request.uri.len;
  151. }
  152. }
  153. memcpy(branches[nr_branches].uri, uri, uri_len);
  154. /* be safe -- add zero termination */
  155. branches[nr_branches].uri[uri_len] = 0;
  156. branches[nr_branches].len = uri_len;
  157. branches[nr_branches].q = q;
  158. if (dst_uri && dst_uri_len) {
  159. memcpy(branches[nr_branches].dst_uri, dst_uri, dst_uri_len);
  160. branches[nr_branches].dst_uri[dst_uri_len] = 0;
  161. branches[nr_branches].dst_uri_len = dst_uri_len;
  162. } else {
  163. branches[nr_branches].dst_uri[0] = '\0';
  164. branches[nr_branches].dst_uri_len = 0;
  165. }
  166. branches[nr_branches].force_send_socket = force_socket;
  167. nr_branches++;
  168. return 1;
  169. }
  170. /*
  171. * Create a Contact header field from the dset
  172. * array
  173. */
  174. char* print_dset(struct sip_msg* msg, int* len)
  175. {
  176. int cnt, i;
  177. unsigned int qlen;
  178. qvalue_t q;
  179. str uri;
  180. char* p, *qbuf;
  181. static char dset[MAX_REDIRECTION_LEN];
  182. if (msg->new_uri.s) {
  183. cnt = 1;
  184. *len = msg->new_uri.len;
  185. if (ruri_q != Q_UNSPECIFIED) {
  186. *len += 1 + Q_PARAM_LEN + len_q(ruri_q);
  187. }
  188. } else {
  189. cnt = 0;
  190. *len = 0;
  191. }
  192. init_branch_iterator();
  193. while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0))) {
  194. cnt++;
  195. *len += uri.len;
  196. if (q != Q_UNSPECIFIED) {
  197. *len += 1 + Q_PARAM_LEN + len_q(q);
  198. }
  199. }
  200. if (cnt == 0) return 0;
  201. *len += CONTACT_LEN + CRLF_LEN + (cnt - 1) * CONTACT_DELIM_LEN;
  202. if (*len + 1 > MAX_REDIRECTION_LEN) {
  203. LOG(L_ERR, "ERROR: redirection buffer length exceed\n");
  204. return 0;
  205. }
  206. memcpy(dset, CONTACT, CONTACT_LEN);
  207. p = dset + CONTACT_LEN;
  208. if (msg->new_uri.s) {
  209. if (ruri_q != Q_UNSPECIFIED) {
  210. *p++ = '<';
  211. }
  212. memcpy(p, msg->new_uri.s, msg->new_uri.len);
  213. p += msg->new_uri.len;
  214. if (ruri_q != Q_UNSPECIFIED) {
  215. memcpy(p, Q_PARAM, Q_PARAM_LEN);
  216. p += Q_PARAM_LEN;
  217. qbuf = q2str(ruri_q, &qlen);
  218. memcpy(p, qbuf, qlen);
  219. p += qlen;
  220. }
  221. i = 1;
  222. } else {
  223. i = 0;
  224. }
  225. init_branch_iterator();
  226. while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0))) {
  227. if (i) {
  228. memcpy(p, CONTACT_DELIM, CONTACT_DELIM_LEN);
  229. p += CONTACT_DELIM_LEN;
  230. }
  231. if (q != Q_UNSPECIFIED) {
  232. *p++ = '<';
  233. }
  234. memcpy(p, uri.s, uri.len);
  235. p += uri.len;
  236. if (q != Q_UNSPECIFIED) {
  237. memcpy(p, Q_PARAM, Q_PARAM_LEN);
  238. p += Q_PARAM_LEN;
  239. qbuf = q2str(q, &qlen);
  240. memcpy(p, qbuf, qlen);
  241. p += qlen;
  242. }
  243. i++;
  244. }
  245. memcpy(p, CRLF " ", CRLF_LEN + 1);
  246. return dset;
  247. }
  248. /*
  249. * Sets the q parameter of the Request-URI
  250. */
  251. void set_ruri_q(qvalue_t q)
  252. {
  253. ruri_q = q;
  254. }
  255. /*
  256. * Return the q value of the Request-URI
  257. */
  258. qvalue_t get_ruri_q(void)
  259. {
  260. return ruri_q;
  261. }
  262. /*
  263. * Get actual Request-URI
  264. */
  265. int get_request_uri(struct sip_msg* _m, str* _u)
  266. {
  267. /* Use new_uri if present */
  268. if (_m->new_uri.s) {
  269. _u->s = _m->new_uri.s;
  270. _u->len = _m->new_uri.len;
  271. } else {
  272. _u->s = _m->first_line.u.request.uri.s;
  273. _u->len = _m->first_line.u.request.uri.len;
  274. }
  275. return 0;
  276. }
  277. /*
  278. * Rewrite Request-URI
  279. */
  280. int rewrite_uri(struct sip_msg* _m, str* _s)
  281. {
  282. char* buf;
  283. buf = (char*)pkg_malloc(_s->len + 1);
  284. if (!buf) {
  285. LOG(L_ERR, "ERROR: rewrite_uri: No memory left\n");
  286. return -1;
  287. }
  288. memcpy(buf, _s->s, _s->len);
  289. buf[_s->len] = '\0';
  290. _m->parsed_uri_ok = 0;
  291. if (_m->new_uri.s) {
  292. pkg_free(_m->new_uri.s);
  293. }
  294. _m->new_uri.s = buf;
  295. _m->new_uri.len = _s->len;
  296. DBG("rewrite_uri: Rewriting Request-URI with '%.*s'\n", _s->len,
  297. buf);
  298. return 1;
  299. }