2
0

data_lump_rpl.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * $Id$
  3. *
  4. *
  5. * Copyright (C) 2001-2003 FhG Fokus
  6. *
  7. * This file is part of ser, a free SIP server.
  8. *
  9. * ser is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version
  13. *
  14. * For a license to use the ser software under conditions
  15. * other than those described here, or to purchase support for this
  16. * software, please contact iptel.org by e-mail at the following addresses:
  17. * [email protected]
  18. *
  19. * ser is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. * History:
  29. * 2002-02-14 : created by bogdan
  30. * 2003-09-11 : lump_rpl type added - LUMP_RPL_BODY & LUMP_RPL_HDR (bogdan)
  31. * 2003-11-11 : build_lump_rpl merged into add_lump_rpl; types -> flags ;
  32. * flags LUMP_RPL_NODUP and LUMP_RPL_NOFREE added (bogdan)
  33. * 2006-10-16 add_lump_rpl2 added: same as the old add_lump_rpl, but
  34. * returns a lump_rpl**, making a specific lump removal much
  35. * more easy (andrei)
  36. */
  37. /*!
  38. * \file
  39. * \brief SIP-router core ::
  40. * \ingroup core
  41. * Module: \ref core
  42. */
  43. #include <string.h>
  44. #include "dprint.h"
  45. #include "mem/mem.h"
  46. #include "data_lump_rpl.h"
  47. struct lump_rpl** add_lump_rpl2(struct sip_msg *msg, char *s,
  48. int len, int flags)
  49. {
  50. struct lump_rpl *lump = 0;
  51. struct lump_rpl *foo;
  52. struct lump_rpl** ret;
  53. /* some checking */
  54. if ( (flags&(LUMP_RPL_HDR|LUMP_RPL_BODY))==(LUMP_RPL_HDR|LUMP_RPL_BODY)
  55. || (flags&(LUMP_RPL_HDR|LUMP_RPL_BODY))==0 || (flags&LUMP_RPL_SHMEM) ) {
  56. LOG(L_ERR,"ERROR:add_lump_rpl: bad flags combination (%d)!\n",flags);
  57. goto error;
  58. }
  59. if (len<=0 || s==0) {
  60. LOG(L_ERR,"ERROR:add_lump_rpl: I won't add an empty lump!\n");
  61. goto error;
  62. }
  63. /* build the lump */
  64. lump = (struct lump_rpl*) pkg_malloc
  65. ( sizeof(struct lump_rpl) + ((flags&LUMP_RPL_NODUP)?0:len) );
  66. if (!lump) {
  67. LOG(L_ERR,"ERROR:add_lump_rpl : no free pkg memory !\n");
  68. goto error;
  69. }
  70. if (flags&LUMP_RPL_NODUP) {
  71. lump->text.s = s;
  72. } else {
  73. lump->text.s = ((char*)lump)+sizeof(struct lump_rpl);
  74. memcpy( lump->text.s, s, len);
  75. }
  76. lump->text.len = len;
  77. lump->flags = flags;
  78. lump->next = 0;
  79. /* add the lump to the msg */
  80. if (!msg->reply_lump) {
  81. msg->reply_lump = lump;
  82. ret=&msg->reply_lump;
  83. }else{
  84. if (!(flags&LUMP_RPL_BODY))
  85. for(foo=msg->reply_lump;foo->next;foo=foo->next);
  86. else
  87. for(foo=msg->reply_lump; ;foo=foo->next) {
  88. if (foo->flags&LUMP_RPL_BODY) {
  89. LOG(L_ERR,"ERROR:add_lump_rpl: LUMP_RPL_BODY "
  90. "already added!\n");
  91. pkg_free(lump);
  92. goto error;
  93. }
  94. if (foo->next==0)
  95. break;
  96. }
  97. foo->next = lump;
  98. ret= &(foo->next);
  99. }
  100. return ret;
  101. error:
  102. return 0;
  103. }
  104. void free_lump_rpl(struct lump_rpl* lump)
  105. {
  106. if (lump) {
  107. if (!((lump->flags)&LUMP_RPL_NOFREE) && ((lump->flags)&LUMP_RPL_NODUP)
  108. && lump->text.s)
  109. pkg_free(lump->text.s);
  110. pkg_free(lump);
  111. }
  112. }
  113. void unlink_lump_rpl(struct sip_msg * msg, struct lump_rpl* lump)
  114. {
  115. struct lump_rpl *foo,*prev;
  116. /* look for the lump to be unlink */
  117. foo = msg->reply_lump;
  118. prev = 0;
  119. while( foo && foo!=lump ) {
  120. prev = foo;
  121. foo = foo->next;
  122. }
  123. /* if the lump was found into the list -> unlink it */
  124. if (foo) {
  125. if (prev)
  126. prev->next = foo->next;
  127. else
  128. msg->reply_lump = foo->next;
  129. }
  130. }
  131. void del_nonshm_lump_rpl(struct lump_rpl** list)
  132. {
  133. struct lump_rpl* it, *tmp;
  134. struct lump_rpl** pred;
  135. it = *list;
  136. pred = list;
  137. while(it) {
  138. if (!(it->flags & LUMP_RPL_SHMEM)) {
  139. tmp = it;
  140. *pred = it->next;
  141. it = it->next;
  142. free_lump_rpl(tmp);
  143. continue;
  144. }
  145. pred = &it->next;
  146. it = it->next;
  147. }
  148. }