Browse code

- applied patch from Miklos Tirpak <miklos@iptel.org> (closes SER-110)

Andrei Pelinescu-Onciul authored on 07/06/2006 21:34:23
Showing 2 changed files
... ...
@@ -2174,3 +2174,167 @@ char* via_builder( unsigned int *len,
2174 2174
 	*len = via_len;
2175 2175
 	return line_buf;
2176 2176
 }
2177
+
2178
+/* builds a char* buffer from message headers without body
2179
+ * first line is excluded in case of skip_first_line=1
2180
+ * error is set -1 if the memory allocation failes
2181
+ */
2182
+char * build_only_headers( struct sip_msg* msg, int skip_first_line,
2183
+				unsigned int *returned_len,
2184
+				int *error,
2185
+				struct dest_info* send_info)
2186
+{
2187
+	char		*buf, *new_buf;
2188
+	unsigned int	offset, s_offset, len, new_len;
2189
+
2190
+	*error = 0;
2191
+	buf = msg->buf;
2192
+	if (skip_first_line)
2193
+		s_offset = msg->headers->name.s - buf;
2194
+	else
2195
+		s_offset = 0;
2196
+
2197
+	/* original length without body, and without final \r\n */
2198
+	len = msg->unparsed - buf;
2199
+	/* new msg length */
2200
+	new_len =	len - /* original length  */
2201
+			s_offset + /* skipped first line */
2202
+			lumps_len(msg, msg->add_rm, send_info); /* lumps */
2203
+
2204
+	if (new_len == 0) {
2205
+		*returned_len = 0;
2206
+		return 0;
2207
+	}
2208
+
2209
+	new_buf = (char *)pkg_malloc(new_len+1);
2210
+	if (!new_buf) {
2211
+		LOG(L_ERR, "ERROR: build_only_headers: Not enough memory\n");
2212
+		*error = -1;
2213
+		return 0;
2214
+	}
2215
+	new_buf[0] = 0;
2216
+	offset = 0;
2217
+
2218
+	/* copy message lumps */
2219
+	process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
2220
+	/* copy the rest of the message without body */
2221
+	if (len > s_offset) {
2222
+		memcpy(new_buf+offset, buf+s_offset, len-s_offset);
2223
+		offset += (len-s_offset);
2224
+	}
2225
+	new_buf[offset] = 0;
2226
+
2227
+	*returned_len = offset;
2228
+	return new_buf;
2229
+}
2230
+
2231
+/* builds a char* buffer from message body
2232
+ * error is set -1 if the memory allocation failes
2233
+ */
2234
+char * build_body( struct sip_msg* msg,
2235
+			unsigned int *returned_len,
2236
+			int *error,
2237
+			struct dest_info* send_info)
2238
+{
2239
+	char		*buf, *new_buf, *body;
2240
+	unsigned int	offset, s_offset, len, new_len;
2241
+
2242
+	*error = 0;
2243
+	body = get_body(msg);
2244
+
2245
+	if (!body || (body[0] == 0)) {
2246
+		*returned_len = 0;
2247
+		return 0;
2248
+	}
2249
+	buf = msg->buf;
2250
+	s_offset = body - buf;
2251
+
2252
+	/* original length of msg with body */
2253
+	len = msg->len;
2254
+	/* new body length */
2255
+	new_len =	len - /* original length  */
2256
+			s_offset + /* msg without body */
2257
+			lumps_len(msg, msg->body_lumps, send_info); /* lumps */
2258
+
2259
+	new_buf = (char *)pkg_malloc(new_len+1);
2260
+	if (!new_buf) {
2261
+		LOG(L_ERR, "ERROR: build_body: Not enough memory\n");
2262
+		*error = -1;
2263
+		return 0;
2264
+	}
2265
+	new_buf[0] = 0;
2266
+	offset = 0;
2267
+
2268
+	/* copy body lumps */
2269
+	process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, send_info);
2270
+	/* copy the rest of the message without body */
2271
+	if (len > s_offset) {
2272
+		memcpy(new_buf+offset, buf+s_offset, len-s_offset);
2273
+		offset += (len-s_offset);
2274
+	}
2275
+	new_buf[offset] = 0;
2276
+
2277
+	*returned_len = offset;
2278
+	return new_buf;
2279
+}
2280
+
2281
+/* builds a char* buffer from SIP message including body
2282
+ * The function adjusts the Content-Length HF according
2283
+ * to body lumps in case of touch_clen=1.
2284
+ */
2285
+char * build_all( struct sip_msg* msg, int touch_clen,
2286
+			unsigned int *returned_len,
2287
+			int *error,
2288
+			struct dest_info* send_info)
2289
+{
2290
+	char		*buf, *new_buf;
2291
+	unsigned int	offset, s_offset, len, new_len;
2292
+	unsigned int	body_delta;
2293
+
2294
+	*error = 0;
2295
+	/* Calculate message body difference */
2296
+	body_delta = lumps_len(msg, msg->body_lumps, send_info);
2297
+	if (touch_clen) {
2298
+		/* adjust Content-Length */
2299
+		if (adjust_clen(msg, body_delta, send_info->proto) < 0) {
2300
+			LOG(L_ERR, "ERROR: build_all: Error while adjusting"
2301
+					" Content-Length\n");
2302
+			*error = -1;
2303
+			return 0;
2304
+		}
2305
+	}
2306
+
2307
+	buf = msg->buf;
2308
+	/* original msg length */
2309
+	len = msg->len;
2310
+	/* new msg length */
2311
+	new_len =	len + /* original length  */
2312
+			lumps_len(msg, msg->add_rm, send_info) + /* hdr lumps */
2313
+			body_delta; /* body lumps */
2314
+
2315
+	if (new_len == 0) {
2316
+		returned_len = 0;
2317
+		return 0;
2318
+	}
2319
+
2320
+	new_buf = (char *)pkg_malloc(new_len+1);
2321
+	if (!new_buf) {
2322
+		LOG(L_ERR, "ERROR: build_all: Not enough memory\n");
2323
+		*error = -1;
2324
+		return 0;
2325
+	}
2326
+	new_buf[0] = 0;
2327
+	offset = s_offset = 0;
2328
+
2329
+	/* copy message lumps */
2330
+	process_lumps(msg, msg->add_rm, new_buf, &offset, &s_offset, send_info);
2331
+	/* copy body lumps */
2332
+	process_lumps(msg, msg->body_lumps, new_buf, &offset, &s_offset, send_info);
2333
+	/* copy the rest of the message */
2334
+	memcpy(new_buf+offset, buf+s_offset, len-s_offset);
2335
+	offset += (len-s_offset);
2336
+	new_buf[offset] = 0;
2337
+
2338
+	*returned_len = offset;
2339
+	return new_buf;	
2340
+}
... ...
@@ -116,4 +116,30 @@ int branch_builder( unsigned int hash_index,
116 116
 /* check if IP address in Via != source IP address of signaling */
117 117
 int received_test( struct sip_msg *msg );
118 118
 
119
+/* builds a char* buffer from message headers without body
120
+ * first line is excluded in case of skip_first_line=1
121
+ */
122
+char * build_only_headers( struct sip_msg* msg, int skip_first_line,
123
+				unsigned int *returned_len,
124
+				int *error,
125
+				struct dest_info* send_info);
126
+
127
+/* builds a char* buffer from message body
128
+ * error is set -1 if the memory allocation failes
129
+ */
130
+char * build_body( struct sip_msg* msg,
131
+			unsigned int *returned_len,
132
+			int *error,
133
+			struct dest_info* send_info);
134
+
135
+/* builds a char* buffer from SIP message including body
136
+ * The function adjusts the Content-Length HF according
137
+ * to body lumps in case of adjust_clen=1.
138
+ */
139
+char * build_all( struct sip_msg* msg, int adjust_clen,
140
+			unsigned int *returned_len,
141
+			int *error,
142
+			struct dest_info* send_info);
143
+
144
+
119 145
 #endif