utils.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * $Id$
  3. *
  4. * mangler module
  5. *
  6. * Copyright (C) 2001-2003 FhG Fokus
  7. *
  8. * This file is part of SIP-router, a free SIP server.
  9. *
  10. * SIP-router 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. * SIP-router is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. *
  24. * History:
  25. * --------
  26. * 2003-04-07 first version.
  27. */
  28. /*!
  29. * \file
  30. * \brief SIP-utils :: Mangler
  31. * \ingroup siputils
  32. * - Module; \ref siputils
  33. */
  34. #include "utils.h"
  35. #include "../../parser/msg_parser.h" /* struct sip_msg */
  36. #include "../../mem/mem.h"
  37. #include "../../data_lump.h"
  38. #include <stdio.h>
  39. int
  40. patch (struct sip_msg *msg, char *oldstr, unsigned int oldlen, char *newstr,
  41. unsigned int newlen)
  42. {
  43. int off;
  44. struct lump *anchor;
  45. if (oldstr == NULL)
  46. return -1;
  47. if (newstr == NULL)
  48. return -2;
  49. off = oldstr - msg->buf;
  50. if (off < 0)
  51. return -3;
  52. if ((anchor = del_lump (msg, off, oldlen, 0)) == 0)
  53. {
  54. LM_ERR("lumping with del_lump\n");
  55. return -4;
  56. }
  57. if ((insert_new_lump_after (anchor, newstr, newlen, 0)) == 0)
  58. {
  59. LM_ERR("lumping with insert_new_lump_after\n");
  60. return -5;
  61. }
  62. return 0;
  63. }
  64. /* TESTED */
  65. int
  66. patch_content_length (struct sip_msg *msg, unsigned int newValue)
  67. {
  68. struct hdr_field *contentLength;
  69. char *s, pos[11];
  70. int len;
  71. contentLength = msg->content_length;
  72. if (contentLength == NULL) /* maybe not yet parsed */
  73. {
  74. if (parse_headers (msg, HDR_CONTENTLENGTH_F, 0) == -1)
  75. {
  76. LM_ERR("parse headers on Content-Length failed\n");
  77. return -1;
  78. }
  79. contentLength = msg->content_length;
  80. if (contentLength == NULL)
  81. {
  82. LM_ERR("failed to parse headers on Content-Length "
  83. "succeeded but msg->content_length is still NULL\n");
  84. return -2;
  85. }
  86. }
  87. /* perhaps dangerous because buffer is static ? */
  88. //pos = int2str(newValue,&len);
  89. len = snprintf ((char *) pos, 10, "%u", newValue);
  90. s = pkg_malloc (len);
  91. if (s == NULL)
  92. {
  93. LM_ERR("unable to allocate %d bytes in pkg mem\n", len);
  94. return -3;
  95. }
  96. memcpy (s, pos, len);
  97. /* perhaps we made it and no one called int2str,might use sprintf */
  98. if (patch
  99. (msg, contentLength->body.s, contentLength->body.len, s, len) < 0)
  100. {
  101. pkg_free (s);
  102. LM_ERR("lumping failed\n");
  103. return -4;
  104. }
  105. LM_DBG ("succeeded in altering Content-Length to new value %u\n",newValue);
  106. return 0;
  107. }