data_lump_rpl.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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. LM_ERR("bad flags combination (%d)!\n",flags);
  57. goto error;
  58. }
  59. if (len<=0 || s==0) {
  60. LM_ERR("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. LM_ERR("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. LM_ERR("LUMP_RPL_BODY already added!\n");
  90. pkg_free(lump);
  91. goto error;
  92. }
  93. if (foo->next==0)
  94. break;
  95. }
  96. foo->next = lump;
  97. ret= &(foo->next);
  98. }
  99. return ret;
  100. error:
  101. return 0;
  102. }
  103. void free_lump_rpl(struct lump_rpl* lump)
  104. {
  105. if (lump) {
  106. if (!((lump->flags)&LUMP_RPL_NOFREE) && ((lump->flags)&LUMP_RPL_NODUP)
  107. && lump->text.s)
  108. pkg_free(lump->text.s);
  109. pkg_free(lump);
  110. }
  111. }
  112. void unlink_lump_rpl(struct sip_msg * msg, struct lump_rpl* lump)
  113. {
  114. struct lump_rpl *foo,*prev;
  115. /* look for the lump to be unlink */
  116. foo = msg->reply_lump;
  117. prev = 0;
  118. while( foo && foo!=lump ) {
  119. prev = foo;
  120. foo = foo->next;
  121. }
  122. /* if the lump was found into the list -> unlink it */
  123. if (foo) {
  124. if (prev)
  125. prev->next = foo->next;
  126. else
  127. msg->reply_lump = foo->next;
  128. }
  129. }
  130. void del_nonshm_lump_rpl(struct lump_rpl** list)
  131. {
  132. struct lump_rpl* it, *tmp;
  133. struct lump_rpl** pred;
  134. it = *list;
  135. pred = list;
  136. while(it) {
  137. if (!(it->flags & LUMP_RPL_SHMEM)) {
  138. tmp = it;
  139. *pred = it->next;
  140. it = it->next;
  141. free_lump_rpl(tmp);
  142. continue;
  143. }
  144. pred = &it->next;
  145. it = it->next;
  146. }
  147. }