modules_k/siputils/utils.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * mangler module
00005  *
00006  * Copyright (C) 2001-2003 FhG Fokus
00007  *
00008  * This file is part of SIP-router, a free SIP server.
00009  *
00010  * SIP-router 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  * SIP-router 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 
00037 #include "utils.h"
00038 
00039 #include "../../parser/msg_parser.h"    /* struct sip_msg */
00040 #include "../../mem/mem.h"
00041 #include "../../data_lump.h"
00042 
00043 #include <stdio.h>
00044 
00045 
00046 int
00047 patch (struct sip_msg *msg, char *oldstr, unsigned int oldlen, char *newstr,
00048        unsigned int newlen)
00049 {
00050         int off;
00051         struct lump *anchor;
00052 
00053         if (oldstr == NULL)
00054                 return -1;
00055 
00056         if (newstr == NULL)
00057                 return -2;
00058         off = oldstr - msg->buf;
00059         if (off < 0)
00060                 return -3;
00061         if ((anchor = del_lump (msg, off, oldlen, 0)) == 0)
00062         {
00063                 LM_ERR("lumping with del_lump\n");
00064                 return -4;
00065         }
00066         if ((insert_new_lump_after (anchor, newstr, newlen, 0)) == 0)
00067         {
00068                 LM_ERR("lumping with insert_new_lump_after\n");
00069                 return -5;
00070         }
00071 
00072         return 0;
00073 }
00074 
00075 
00076 /* TESTED */
00077 int
00078 patch_content_length (struct sip_msg *msg, unsigned int newValue)
00079 {
00080 
00081         struct hdr_field *contentLength;
00082         char *s, pos[11];
00083         int len;
00084 
00085         contentLength = msg->content_length;
00086         if (contentLength == NULL)      /* maybe not yet parsed */
00087         {
00088                 if (parse_headers (msg, HDR_CONTENTLENGTH_F, 0) == -1)
00089                 {
00090                         LM_ERR("parse headers on Content-Length failed\n");
00091                         return -1;
00092                 }
00093                 contentLength = msg->content_length;
00094                 if (contentLength == NULL)
00095                 {
00096                         LM_ERR("failed to parse headers on Content-Length "
00097                                         "succeeded but msg->content_length is still NULL\n");
00098                         return -2;
00099                 }
00100         }
00101         /* perhaps dangerous because buffer is static ? */
00102         //pos = int2str(newValue,&len);
00103         len = snprintf ((char *) pos, 10, "%u", newValue);
00104         s = pkg_malloc (len);
00105         if (s == NULL)
00106         {
00107                 LM_ERR("unable to allocate %d bytes in pkg mem\n", len);
00108                 return -3;
00109         }
00110         memcpy (s, pos, len);
00111         /* perhaps we made it and no one called int2str,might use sprintf */
00112         if (patch
00113             (msg, contentLength->body.s, contentLength->body.len, s, len) < 0)
00114         {
00115                 pkg_free (s);
00116                 LM_ERR("lumping failed\n");
00117                 return -4;
00118         }
00119 
00120         LM_DBG ("succeeded in altering Content-Length to new value %u\n",newValue);
00121 
00122         return 0;
00123 
00124 }