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
00037 #include "utils.h"
00038
00039 #include "../../parser/msg_parser.h"
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
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)
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
00102
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
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 }