modules/siputils/utils.c
31ccf6a2
 /*
  * mangler module
  *
  * Copyright (C) 2001-2003 FhG Fokus
  *
964fc8cc
  * This file is part of Kamailio, a free SIP server.
31ccf6a2
  *
964fc8cc
  * Kamailio is free software; you can redistribute it and/or modify
31ccf6a2
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version
  *
964fc8cc
  * Kamailio is distributed in the hope that it will be useful,
31ccf6a2
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
964fc8cc
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
9e1ff448
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
31ccf6a2
  *
  */
 
964fc8cc
 
2caae8fd
 /*!
  * \file
  * \brief SIP-utils :: Mangler
  * \ingroup siputils
  * - Module; \ref siputils
  */
 
 
31ccf6a2
 #include "utils.h"
 
 #include "../../parser/msg_parser.h"	/* struct sip_msg */
 #include "../../mem/mem.h"
 #include "../../data_lump.h"
 
 #include <stdio.h>
 
 
 int
 patch (struct sip_msg *msg, char *oldstr, unsigned int oldlen, char *newstr,
        unsigned int newlen)
 {
 	int off;
 	struct lump *anchor;
 
 	if (oldstr == NULL)
 		return -1;
 
 	if (newstr == NULL)
 		return -2;
 	off = oldstr - msg->buf;
 	if (off < 0)
 		return -3;
 	if ((anchor = del_lump (msg, off, oldlen, 0)) == 0)
 	{
789504a6
 		LM_ERR("lumping with del_lump\n");
31ccf6a2
 		return -4;
 	}
 	if ((insert_new_lump_after (anchor, newstr, newlen, 0)) == 0)
 	{
789504a6
 		LM_ERR("lumping with insert_new_lump_after\n");
31ccf6a2
 		return -5;
 	}
 
 	return 0;
 }
 
 
 /* TESTED */
 int
 patch_content_length (struct sip_msg *msg, unsigned int newValue)
 {
 
 	struct hdr_field *contentLength;
 	char *s, pos[11];
 	int len;
 
 	contentLength = msg->content_length;
 	if (contentLength == NULL)	/* maybe not yet parsed */
 	{
b68fbcd1
 		if (parse_headers (msg, HDR_CONTENTLENGTH_F, 0) == -1)
31ccf6a2
 		{
789504a6
 			LM_ERR("parse headers on Content-Length failed\n");
31ccf6a2
 			return -1;
 		}
 		contentLength = msg->content_length;
 		if (contentLength == NULL)
 		{
789504a6
 			LM_ERR("failed to parse headers on Content-Length "
 					"succeeded but msg->content_length is still NULL\n");
31ccf6a2
 			return -2;
 		}
 	}
 	/* perhaps dangerous because buffer is static ? */
 	//pos = int2str(newValue,&len);
 	len = snprintf ((char *) pos, 10, "%u", newValue);
 	s = pkg_malloc (len);
 	if (s == NULL)
 	{
789504a6
 		LM_ERR("unable to allocate %d bytes in pkg mem\n", len);
31ccf6a2
 		return -3;
 	}
 	memcpy (s, pos, len);
 	/* perhaps we made it and no one called int2str,might use sprintf */
 	if (patch
 	    (msg, contentLength->body.s, contentLength->body.len, s, len) < 0)
 	{
 		pkg_free (s);
789504a6
 		LM_ERR("lumping failed\n");
31ccf6a2
 		return -4;
 	}
 
789504a6
 	LM_DBG ("succeeded in altering Content-Length to new value %u\n",newValue);
31ccf6a2
 
 	return 0;
 
 }