siputils/utils.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "utils.h"
00030
00031 #include "../../parser/msg_parser.h"
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
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)
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
00094
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
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 }