Browse code

core: save and restore branch_iterator in print_dset

- safe usage in branch_route where branch_interator is used to add the
new brnaches, but some variables can change it if used in config
branch_route
- closes FS#182, reported by Pawel Sternal
(cherry picked from commit c5f101dfac9a50f428e3452893c402d8b1e0400b)
(cherry picked from commit d44956c4f3d5fb98bc2af42d8718a420d624d259)

Daniel-Constantin Mierla authored on 24/11/2011 11:56:28
Showing 2 changed files
... ...
@@ -191,11 +191,21 @@ void init_branch_iterator(void)
191 191
 	branch_iterator = 0;
192 192
 }
193 193
 
194
+/**
195
+ * return the value of current branch iterator
196
+ */
194 197
 int get_branch_iterator(void)
195 198
 {
196 199
 	return branch_iterator;
197 200
 }
198 201
 
202
+/**
203
+ * set the value of current branch interator
204
+ */
205
+void set_branch_iterator(int n)
206
+{
207
+	branch_iterator = n;
208
+}
199 209
 
200 210
 
201 211
 /** \brief Get a branch from the destination set
... ...
@@ -371,6 +381,7 @@ char* print_dset(struct sip_msg* msg, int* len)
371 381
 	qvalue_t q;
372 382
 	str uri;
373 383
 	char* p, *qbuf;
384
+	int crt_branch;
374 385
 	static char dset[MAX_REDIRECTION_LEN];
375 386
 
376 387
 	if (msg->new_uri.s) {
... ...
@@ -384,6 +395,9 @@ char* print_dset(struct sip_msg* msg, int* len)
384 395
 		*len = 0;
385 396
 	}
386 397
 
398
+	/* backup current branch index to restore it later */
399
+	crt_branch = get_branch_iterator();
400
+
387 401
 	init_branch_iterator();
388 402
 	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0))) {
389 403
 		cnt++;
... ...
@@ -399,7 +413,7 @@ char* print_dset(struct sip_msg* msg, int* len)
399 413
 
400 414
 	if (*len + 1 > MAX_REDIRECTION_LEN) {
401 415
 		LOG(L_ERR, "ERROR: redirection buffer length exceed\n");
402
-		return 0;
416
+		goto error;
403 417
 	}
404 418
 
405 419
 	memcpy(dset, CONTACT, CONTACT_LEN);
... ...
@@ -450,7 +464,12 @@ char* print_dset(struct sip_msg* msg, int* len)
450 464
 	}
451 465
 
452 466
 	memcpy(p, CRLF " ", CRLF_LEN + 1);
467
+	set_branch_iterator(crt_branch);
453 468
 	return dset;
469
+
470
+error:
471
+	set_branch_iterator(crt_branch);
472
+	return 0;
454 473
 }
455 474
 
456 475
 
... ...
@@ -107,15 +107,19 @@ static inline int ser_append_branch(struct sip_msg* msg,
107 107
 
108 108
 
109 109
 /*! \brief
110
- * Iterate through the list of transaction branches 
110
+ * Init the index to iterate through the list of transaction branches
111 111
  */
112 112
 void init_branch_iterator(void);
113 113
 
114 114
 /*! \brief
115
- * Return branch iterator position 
115
+ * Return branch iterator position
116 116
  */
117 117
 int get_branch_iterator(void);
118 118
 
119
+/*! \brief
120
+ * Set branch iterator position
121
+ */
122
+void set_branch_iterator(int n);
119 123
 
120 124
 /*! \brief Get the next branch in the current transaction.
121 125
  * @return pointer to the uri of the next branch (which the length written in