Browse code

- duplicate_parameters function added - duplicate_rr function added

Jan Janak authored on 31/03/2003 15:49:15
Showing 4 changed files
... ...
@@ -418,3 +418,40 @@ void print_params(param_t* _p)
418 418
 		ptr = ptr->next;
419 419
 	}
420 420
 }
421
+
422
+
423
+/*
424
+ * Duplicate linked list of parameters
425
+ */
426
+int duplicate_params(param_t** _n, param_t* _p)
427
+{
428
+	param_t* last, *ptr, *t;
429
+
430
+	if (!_n || !_p) {
431
+		LOG(L_ERR, "duplicate_params(): Invalid parameter value\n");
432
+		return -1;
433
+	}
434
+	
435
+	last = 0;
436
+	*_n = 0;
437
+	ptr = _p;
438
+	while(ptr) {
439
+		t = (param_t*)pkg_malloc(sizeof(param_t));
440
+		if (!t) {
441
+			LOG(L_ERR, "duplicate_params(): Invalid parameter value\n");
442
+			goto err;
443
+		}
444
+		memcpy(t, ptr, sizeof(param_t));
445
+		t->next = 0;
446
+
447
+		if (!*_n) *_n = t;
448
+		if (last) last->next = t;
449
+		last = t;
450
+
451
+		ptr = ptr->next;
452
+	}
453
+
454
+ err:
455
+	free_params(*_n);
456
+	return 0;
457
+}
... ...
@@ -123,4 +123,10 @@ void free_params(param_t* _p);
123 123
 void print_params(param_t* _p);
124 124
 
125 125
 
126
+/*
127
+ * Duplicate linked list of parameters
128
+ */
129
+int duplicate_params(param_t** _n, param_t* _p);
130
+
131
+
126 132
 #endif /* PARSE_PARAM_H */
... ...
@@ -173,3 +173,62 @@ void print_rr(rr_t* _r)
173 173
 		ptr = ptr->next;
174 174
 	}
175 175
 }
176
+
177
+
178
+/*
179
+ * Translate all pointers in the structure and also
180
+ * in all parameters in the list
181
+ */
182
+static inline void xlate_pointers(struct sip_msg* _m, rr_t* _r)
183
+{
184
+	param_t* ptr;
185
+	_r->nameaddr.uri.s = translate_pointer(_r->nameaddr.name.s, _m->buf, _r->nameaddr.uri.s);
186
+	
187
+	ptr = _r->params;
188
+	while(ptr) {
189
+		if (ptr->type == P_R2) _r->r2 = ptr;
190
+		ptr->name.s = translate_pointer(_r->nameaddr.name.s, _m->buf, ptr->name.s);
191
+		ptr->body.s = translate_pointer(_r->nameaddr.name.s, _m->buf, ptr->body.s);		
192
+		ptr = ptr->next;
193
+	}
194
+}
195
+
196
+
197
+/*
198
+ * Duplicate a single rr_t structure using pkg_malloc
199
+ */
200
+int duplicate_rr(struct sip_msg* _m, rr_t** _new, rr_t* _r)
201
+{
202
+	int len;
203
+	rr_t* res;
204
+
205
+	if (!_m || !_new || _r) {
206
+		LOG(L_ERR, "duplicate_rr(): Invalid parameter value\n");
207
+		return -1;
208
+	}
209
+
210
+	if (_r->params) {
211
+		len = _r->params->name.s + _r->params->len - _r->nameaddr.name.s;
212
+	} else {
213
+		len = _r->nameaddr.len;
214
+	}
215
+
216
+	res = pkg_malloc(sizeof(rr_t) + len);
217
+	if (!res) {
218
+		LOG(L_ERR, "duplicate_rr(): No memory left\n");
219
+		return -2;
220
+	}
221
+
222
+        res->nameaddr.name.s = (char*)res + sizeof(rr_t);
223
+	memcpy(res->nameaddr.name.s, _r->nameaddr.name.s, len);
224
+
225
+	if (duplicate_params(&res->params, _r->params) < 0) {
226
+		LOG(L_ERR, "Error while duplicating parameters\n");
227
+		pkg_free(res);
228
+		return -3;
229
+	}
230
+
231
+	xlate_pointers(_m, res);
232
+	*_new = res;
233
+	return 0;
234
+}
... ...
@@ -31,7 +31,7 @@
31 31
 #ifndef PARSE_RR_H
32 32
 #define PARSE_RR_H
33 33
 
34
-
34
+#include "msg_parser.h"
35 35
 #include "parse_nameaddr.h"
36 36
 #include "parse_param.h"
37 37
 #include "hf.h"
... ...
@@ -67,4 +67,10 @@ void free_rr(rr_t** _r);
67 67
 void print_rr(rr_t* _r);
68 68
 
69 69
 
70
+/*
71
+ * Duplicate a single rr_t structure using pkg_malloc
72
+ */
73
+int duplicate_rr(struct sip_msg* _m, rr_t** _new, rr_t* _r);
74
+
75
+
70 76
 #endif /* PARSE_RR_H */