dset.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * $Id$
  3. *
  4. * destination set
  5. *
  6. * Copyright (C) 2001-2003 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 "dset.h"
  37. #include "error.h"
  38. /* where we store URIs of additional transaction branches
  39. (-1 because of the default branch, #0)
  40. */
  41. static struct branch branches[ MAX_BRANCHES - 1 ];
  42. /* how many of them we have */
  43. static unsigned int nr_branches=0;
  44. /* branch iterator */
  45. static int branch_iterator=0;
  46. void init_branch_iterator(void)
  47. {
  48. branch_iterator=0;
  49. }
  50. char *next_branch( int *len )
  51. {
  52. unsigned int i;
  53. i=branch_iterator;
  54. if (i<nr_branches) {
  55. branch_iterator++;
  56. *len=branches[i].len;
  57. return branches[i].uri;
  58. } else {
  59. *len=0;
  60. return 0;
  61. }
  62. }
  63. void clear_branches()
  64. {
  65. nr_branches=0;
  66. }
  67. /* add a new branch to current transaction */
  68. int append_branch( struct sip_msg *msg, char *uri, int uri_len )
  69. {
  70. /* if we have already set up the maximum number
  71. of branches, don't try new ones */
  72. if (nr_branches==MAX_BRANCHES-1) {
  73. LOG(L_ERR, "ERROR: append_branch: max nr of branches exceeded\n");
  74. ser_error=E_TOO_MANY_BRANCHES;
  75. return -1;
  76. }
  77. if (uri_len>MAX_URI_SIZE-1) {
  78. LOG(L_ERR, "ERROR: append_branch: too long uri: %.*s\n",
  79. uri_len, uri );
  80. return -1;
  81. }
  82. /* if not parameterized, take current uri */
  83. if (uri==0) {
  84. if (msg->new_uri.s) {
  85. uri=msg->new_uri.s;
  86. uri_len=msg->new_uri.len;
  87. } else {
  88. uri=msg->first_line.u.request.uri.s;
  89. uri_len=msg->first_line.u.request.uri.len;
  90. }
  91. }
  92. memcpy( branches[nr_branches].uri, uri, uri_len );
  93. /* be safe -- add zero termination */
  94. branches[nr_branches].uri[uri_len]=0;
  95. branches[nr_branches].len=uri_len;
  96. nr_branches++;
  97. return 1;
  98. }
  99. char *print_dset( struct sip_msg *msg, int *len )
  100. {
  101. int cnt;
  102. str uri;
  103. char *p;
  104. int i;
  105. static char dset[MAX_REDIRECTION_LEN];
  106. if (msg->new_uri.s) {
  107. cnt=1;
  108. *len=msg->new_uri.len;
  109. } else {
  110. cnt=0;
  111. *len=0;
  112. }
  113. init_branch_iterator();
  114. while ((uri.s=next_branch(&uri.len))) {
  115. cnt++;
  116. *len+=uri.len;
  117. }
  118. if (cnt==0) return 0;
  119. *len+=CONTACT_LEN+CRLF_LEN+(cnt-1)*CONTACT_DELIM_LEN;
  120. if (*len+1>MAX_REDIRECTION_LEN) {
  121. LOG(L_ERR, "ERROR: redirection buffer length exceed\n");
  122. return 0;
  123. }
  124. memcpy(dset, CONTACT, CONTACT_LEN );
  125. p=dset+CONTACT_LEN;
  126. if (msg->new_uri.s) {
  127. memcpy(p, msg->new_uri.s, msg->new_uri.len);
  128. p+=msg->new_uri.len;
  129. i=1;
  130. } else i=0;
  131. init_branch_iterator();
  132. while ((uri.s=next_branch(&uri.len))) {
  133. if (i) {
  134. memcpy(p, CONTACT_DELIM, CONTACT_DELIM_LEN );
  135. p+=2;
  136. }
  137. memcpy(p, uri.s, uri.len);
  138. p+=uri.len;
  139. i++;
  140. }
  141. memcpy(p, CRLF " ", CRLF_LEN+1);
  142. return dset;
  143. }