siputils/utils.c

Go to the documentation of this file.
00001 /*
00002  * $Id: utils.c 5318 2008-12-08 16:38:47Z henningw $
00003  *
00004  * mangler module
00005  *
00006  * Copyright (C) 2001-2003 FhG Fokus
00007  *
00008  * This file is part of Kamailio, a free SIP server.
00009  *
00010  * Kamailio is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version
00014  *
00015  * Kamailio is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License 
00021  * along with this program; if not, write to the Free Software 
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  * History:
00025  * --------
00026  *  2003-04-07 first version.  
00027  */
00028 
00029 #include "utils.h"
00030 
00031 #include "../../parser/msg_parser.h"   /* struct sip_msg */
00032 #include "../../mem/mem.h"
00033 #include "../../data_lump.h"
00034 
00035 #include <stdio.h>
00036 
00037 
00038 int
00039 patch (struct sip_msg *msg, char *oldstr, unsigned int oldlen, char *newstr,
00040        unsigned int newlen)
00041 {
00042    int off;
00043    struct lump *anchor;
00044 
00045    if (oldstr == NULL)
00046       return -1;
00047 
00048    if (newstr == NULL)
00049       return -2;
00050    off = oldstr - msg->buf;
00051    if (off < 0)
00052       return -3;
00053    if ((anchor = del_lump (msg, off, oldlen, 0)) == 0)
00054    {
00055       LM_ERR("lumping with del_lump\n");
00056       return -4;
00057    }
00058    if ((insert_new_lump_after (anchor, newstr, newlen, 0)) == 0)
00059    {
00060       LM_ERR("lumping with insert_new_lump_after\n");
00061       return -5;
00062    }
00063 
00064    return 0;
00065 }
00066 
00067 
00068 /* TESTED */
00069 int
00070 patch_content_length (struct sip_msg *msg, unsigned int newValue)
00071 {
00072 
00073    struct hdr_field *contentLength;
00074    char *s, pos[11];
00075    int len;
00076 
00077    contentLength = msg->content_length;
00078    if (contentLength == NULL) /* maybe not yet parsed */
00079    {
00080       if (parse_headers (msg, HDR_CONTENTLENGTH_F, 0) == -1)
00081       {
00082          LM_ERR("parse headers on Content-Length failed\n");
00083          return -1;
00084       }
00085       contentLength = msg->content_length;
00086       if (contentLength == NULL)
00087       {
00088          LM_ERR("failed to parse headers on Content-Length "
00089                "succeeded but msg->content_length is still NULL\n");
00090          return -2;
00091       }
00092    }
00093    /* perhaps dangerous because buffer is static ? */
00094    //pos = int2str(newValue,&len);
00095    len = snprintf ((char *) pos, 10, "%u", newValue);
00096    s = pkg_malloc (len);
00097    if (s == NULL)
00098    {
00099       LM_ERR("unable to allocate %d bytes in pkg mem\n", len);
00100       return -3;
00101    }
00102    memcpy (s, pos, len);
00103    /* perhaps we made it and no one called int2str,might use sprintf */
00104    if (patch
00105        (msg, contentLength->body.s, contentLength->body.len, s, len) < 0)
00106    {
00107       pkg_free (s);
00108       LM_ERR("lumping failed\n");
00109       return -4;
00110    }
00111 
00112    LM_DBG ("succeeded in altering Content-Length to new value %u\n",newValue);
00113 
00114    return 0;
00115 
00116 }

Generated on Thu May 24 12:00:32 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6