Browse code

core: append_branch & next_branch api changes

- added path, branch flags support and switched the parameters to
str for append_branch and next_branch. append_branch is now
exactly the same as km_append_branch.
- added ser_append_branch for compatibility with ser modules which
don't need the extra parameters.
- updated core code to use the modified api.

Andrei Pelinescu-Onciul authored on 04/09/2009 13:09:08
Showing 4 changed files
... ...
@@ -344,7 +344,7 @@ int do_action(struct run_act_ctx* h, struct action* a, struct sip_msg* msg)
344 344
 				ret=E_BUG;
345 345
 				goto error;
346 346
 			}
347
-			ret=append_branch( msg, a->val[0].u.string,
347
+			ret=ser_append_branch( msg, a->val[0].u.string,
348 348
 					   a->val[0].u.string ? strlen(a->val[0].u.string):0,
349 349
 					   0, 0, a->val[1].u.number, 0);
350 350
 			break;
... ...
@@ -160,41 +160,6 @@ int get_branch_iterator(void)
160 160
 	return branch_iterator;
161 161
 }
162 162
 
163
-/*
164
- * Return the next branch from the dset
165
- * array, 0 is returned if there are no
166
- * more branches
167
- */
168
-char* next_branch(int* len, qvalue_t* q, char** dst_uri, int* dst_len, struct socket_info** force_socket)
169
-{
170
-	unsigned int i;
171
-
172
-	i = branch_iterator;
173
-	if (i < nr_branches) {
174
-		branch_iterator++;
175
-		*len = branches[i].len;
176
-		*q = branches[i].q;
177
-		if (dst_uri && dst_len) {
178
-			*dst_uri = branches[i].dst_uri;
179
-			*dst_len = branches[i].dst_uri_len;
180
-		}
181
-		if (force_socket) {
182
-			*force_socket = branches[i].force_send_socket;
183
-		}
184
-		return branches[i].uri;
185
-	} else {
186
-		*len = 0;
187
-		*q = Q_UNSPECIFIED;
188
-		if (dst_uri && dst_len) {
189
-			*dst_uri = 0;
190
-			*dst_len = 0;
191
-		}
192
-		if (force_socket) {
193
-			*force_socket = 0;
194
-		}
195
-		return 0;
196
-	}
197
-}
198 163
 
199 164
 
200 165
 /** \brief Get a branch from the destination set
... ...
@@ -203,7 +168,8 @@ char* next_branch(int* len, qvalue_t* q, char** dst_uri, int* dst_len, struct so
203 203
  * more branches
204 204
  */
205 205
 char* get_branch(unsigned int i, int* len, qvalue_t* q, str* dst_uri,
206
-				 str* path, unsigned int *flags, struct socket_info** force_socket)
206
+				 str* path, unsigned int *flags,
207
+				 struct socket_info** force_socket)
207 208
 {
208 209
 	if (i < nr_branches) {
209 210
 		*len = branches[i].len;
... ...
@@ -228,6 +194,10 @@ char* get_branch(unsigned int i, int* len, qvalue_t* q, str* dst_uri,
228 228
 			dst_uri->s = 0;
229 229
 			dst_uri->len = 0;
230 230
 		}
231
+		if (path) {
232
+			path->s = 0;
233
+			path->len = 0;
234
+		}
231 235
 		if (force_socket)
232 236
 			*force_socket = 0;
233 237
 		if (flags)
... ...
@@ -237,6 +207,23 @@ char* get_branch(unsigned int i, int* len, qvalue_t* q, str* dst_uri,
237 237
 }
238 238
 
239 239
 
240
+
241
+/** Return the next branch from the dset array.
242
+ * 0 is returned if there are no more branches
243
+ */
244
+char* next_branch(int* len, qvalue_t* q, str* dst_uri, str* path,
245
+					unsigned int* flags, struct socket_info** force_socket)
246
+{
247
+	char* ret;
248
+	
249
+	ret=get_branch(branch_iterator, len, q, dst_uri, path, flags,
250
+					force_socket);
251
+	if (likely(ret))
252
+		branch_iterator++;
253
+	return ret;
254
+}
255
+
256
+
240 257
 /*
241 258
  * Empty the dset array
242 259
  */
... ...
@@ -247,84 +234,32 @@ void clear_branches(void)
247 247
 }
248 248
 
249 249
 
250
-/* 
251
- * Add a new branch to current transaction 
252
- */
253
-int append_branch(struct sip_msg* msg, char* uri, int uri_len, char* dst_uri, int dst_uri_len, 
254
-		  qvalue_t q, struct socket_info* force_socket)
255
-{
256
-	     /* if we have already set up the maximum number
257
-	      * of branches, don't try new ones 
258
-	      */
259
-	if (nr_branches == MAX_BRANCHES - 1) {
260
-		LOG(L_ERR, "ERROR: append_branch: max nr of branches exceeded\n");
261
-		ser_error = E_TOO_MANY_BRANCHES;
262
-		return -1;
263
-	}
264
-
265
-	if (uri_len > MAX_URI_SIZE - 1) {
266
-		LOG(L_ERR, "ERROR: append_branch: too long uri: %.*s\n",
267
-		    uri_len, uri);
268
-		return -1;
269
-	}
270
-	
271
-	if (dst_uri_len > MAX_URI_SIZE - 1) {
272
-		LOG(L_ERR, "ERROR: append_branch: too long dst_uri: %.*s\n",
273
-		    dst_uri_len, ZSW(dst_uri));
274
-		return -1;
275
-	}
276
-
277
-	     /* if not parameterized, take current uri */
278
-	if (uri == 0) {
279
-		if (msg->new_uri.s) { 
280
-			uri = msg->new_uri.s;
281
-			uri_len = msg->new_uri.len;
282
-		} else {
283
-			uri = msg->first_line.u.request.uri.s;
284
-			uri_len = msg->first_line.u.request.uri.len;
285
-		}
286
-	}
287
-	
288
-	memcpy(branches[nr_branches].uri, uri, uri_len);
289
-	     /* be safe -- add zero termination */
290
-	branches[nr_branches].uri[uri_len] = 0;
291
-	branches[nr_branches].len = uri_len;
292
-	branches[nr_branches].q = q;
293
-	
294
- 	if (dst_uri && dst_uri_len) {
295
-  		memcpy(branches[nr_branches].dst_uri, dst_uri, dst_uri_len);
296
-  		branches[nr_branches].dst_uri[dst_uri_len] = 0;
297
-  		branches[nr_branches].dst_uri_len = dst_uri_len;
298
- 	} else {
299
- 		branches[nr_branches].dst_uri[0] = '\0';
300
- 		branches[nr_branches].dst_uri_len = 0;
301
-	}
302
-
303
-	branches[nr_branches].force_send_socket = force_socket;
304
-	
305
-	nr_branches++;
306
-	return 1;
307
-}
308
-
309 250
 
310
-/* ! \brief
311
- * Add a new branch to current transaction using str parameters
312
- * Kamailio compatibility version
251
+/**  Add a new branch to the current transaction.
252
+ * @param msg - sip message, used for getting the uri if not specified (0).
253
+ * @param uri - uri, can be 0 (in which case the uri is taken from msg)
254
+ * @param dst_uri - destination uri, can be 0.
255
+ * @param path - path vector (passed in a string), can be 0.
256
+ * @param q  - q value.
257
+ * @param flags - per branch flags.
258
+ * @param force_socket - socket that should be used when sending.
259
+ *
260
+ * @return  <0 (-1) on failure, 1 on success (script convention).
313 261
  */
314
-int km_append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
262
+int append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
315 263
 		qvalue_t q, unsigned int flags, struct socket_info* force_socket)
316 264
 {
317 265
 	str luri;
318 266
 
319 267
 #ifdef USE_LOCAL_ROUTE
320
-	if (dset_state==0)
268
+	if (unlikely(dset_state==0))
321 269
 		return -1;
322 270
 #endif
323 271
 
324 272
 	/* if we have already set up the maximum number
325 273
 	 * of branches, don't try new ones 
326 274
 	 */
327
-	if (nr_branches == MAX_BRANCHES - 1) {
275
+	if (unlikely(nr_branches == MAX_BRANCHES - 1)) {
328 276
 		LOG(L_ERR, "max nr of branches exceeded\n");
329 277
 		ser_error = E_TOO_MANY_BRANCHES;
330 278
 		return -1;
... ...
@@ -340,16 +275,15 @@ int km_append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
340 340
 		luri = *uri;
341 341
 	}
342 342
 
343
-	if (luri.len > MAX_URI_SIZE - 1) {
343
+	if (unlikely(luri.len > MAX_URI_SIZE - 1)) {
344 344
 		LOG(L_ERR, "too long uri: %.*s\n", luri.len, luri.s);
345 345
 		return -1;
346 346
 	}
347 347
 
348 348
 	/* copy the dst_uri */
349 349
 	if (dst_uri && dst_uri->len && dst_uri->s) {
350
-		if (dst_uri->len > MAX_URI_SIZE - 1) {
351
-			LOG(L_ERR, "too long dst_uri: %.*s\n",
352
-				dst_uri->len, dst_uri->s);
350
+		if (unlikely(dst_uri->len > MAX_URI_SIZE - 1)) {
351
+			LOG(L_ERR, "too long dst_uri: %.*s\n", dst_uri->len, dst_uri->s);
353 352
 			return -1;
354 353
 		}
355 354
 		memcpy(branches[nr_branches].dst_uri, dst_uri->s, dst_uri->len);
... ...
@@ -361,8 +295,8 @@ int km_append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
361 361
 	}
362 362
 
363 363
 	/* copy the path string */
364
-	if (path && path->len && path->s) {
365
-		if (path->len > MAX_PATH_SIZE - 1) {
364
+	if (unlikely(path && path->len && path->s)) {
365
+		if (unlikely(path->len > MAX_PATH_SIZE - 1)) {
366 366
 			LOG(L_ERR, "too long path: %.*s\n", path->len, path->s);
367 367
 			return -1;
368 368
 		}
... ...
@@ -413,7 +347,7 @@ char* print_dset(struct sip_msg* msg, int* len)
413 413
 	}
414 414
 
415 415
 	init_branch_iterator();
416
-	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0))) {
416
+	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0))) {
417 417
 		cnt++;
418 418
 		*len += uri.len;
419 419
 		if (q != Q_UNSPECIFIED) {
... ...
@@ -454,7 +388,7 @@ char* print_dset(struct sip_msg* msg, int* len)
454 454
 	}
455 455
 
456 456
 	init_branch_iterator();
457
-	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0))) {
457
+	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0))) {
458 458
 		if (i) {
459 459
 			memcpy(p, CONTACT_DELIM, CONTACT_DELIM_LEN);
460 460
 			p += CONTACT_DELIM_LEN;
... ...
@@ -37,18 +37,39 @@ struct sip_msg;
37 37
 extern unsigned int nr_branches;
38 38
 
39 39
 
40
-/* 
40
+
41
+/*
41 42
  * Add a new branch to current transaction 
42 43
  */
43
-int append_branch(struct sip_msg* msg, char* uri, int uri_len, char* dst_uri, int dst_uri_len, 
44
-		  qvalue_t q, struct socket_info* force_socket);
44
+int append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
45
+					 qvalue_t q, unsigned int flags,
46
+					 struct socket_info* force_socket);
45 47
 
48
+/* kamailio compatible version */
49
+#define km_append_branch(msg, uri, dst_uri, path, q, flags, force_socket) \
50
+	append_branch(msg, uri, dst_uri, path, q, flags, force_socket)
46 51
 
47
-int km_append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
48
-					 qvalue_t q, unsigned int flags, struct socket_info* force_socket);
52
+/** ser compatible append_branch version.
53
+ *  append_branch version compatible with ser: no path or branch flags support
54
+ *  and no str parameters.
55
+ */
56
+static inline int ser_append_branch(struct sip_msg* msg,
57
+									char* uri, int uri_len,
58
+									char* dst_uri, int dst_uri_len,
59
+									qvalue_t q,
60
+									struct socket_info* force_socket)
61
+{
62
+	str s_uri, s_dst_uri;
63
+	s_uri.s=uri;
64
+	s_uri.len=uri_len;
65
+	s_dst_uri.s=dst_uri;
66
+	s_dst_uri.len=dst_uri_len;
67
+	return append_branch(msg, &s_uri, &s_dst_uri, 0, q, 0, force_socket);
68
+}
49 69
 
50 70
 
51
-/* 
71
+
72
+/*
52 73
  * Iterate through the list of transaction branches 
53 74
  */
54 75
 void init_branch_iterator(void);
... ...
@@ -58,14 +79,19 @@ void init_branch_iterator(void);
58 58
  */
59 59
 int get_branch_iterator(void);
60 60
 
61
-/*
62
- * Get the next branch in the current transaction
61
+
62
+/** Get the next branch in the current transaction.
63
+ * @return pointer to the uri of the next branch (which the length written in
64
+ *  *len) or 0 if there are no more branches.
63 65
  */
64
-char* next_branch(int* len, qvalue_t* q, char** dst_uri, int* dst_len, struct socket_info** force_socket);
66
+char* next_branch(int* len, qvalue_t* q, str* dst_uri, str* path,
67
+					unsigned int* flags, struct socket_info** force_socket);
65 68
 
66 69
 
67 70
 char* get_branch( unsigned int i, int* len, qvalue_t* q, str* dst_uri,
68
-				  str* path, unsigned int *flags, struct socket_info** force_socket);
71
+				  str* path, unsigned int *flags,
72
+				  struct socket_info** force_socket);
73
+
69 74
 
70 75
 
71 76
 /*
... ...
@@ -1672,7 +1672,7 @@ int select_branch_uri(str* res, select_t* s, struct sip_msg* msg) {
1672 1672
 		char *c;
1673 1673
 		init_branch_iterator();
1674 1674
 		len = 0;
1675
-		while ((c = next_branch(&l, &q, &dst_uri.s, &dst_uri.len, 0))) {
1675
+		while ((c = next_branch(&l, &q, &dst_uri, 0, 0, 0))) {
1676 1676
 
1677 1677
 			if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
1678 1678
 				l = dst_uri.len;
... ...
@@ -1696,7 +1696,7 @@ int select_branch_uri(str* res, select_t* s, struct sip_msg* msg) {
1696 1696
 		init_branch_iterator();
1697 1697
 		res->len = 0;
1698 1698
 		n = 0;
1699
-		while ((c = next_branch(&l, &q, &dst_uri.s, &dst_uri.len, 0))) {
1699
+		while ((c = next_branch(&l, &q, &dst_uri, 0, 0, 0))) {
1700 1700
 			if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
1701 1701
 				l = dst_uri.len;
1702 1702
 				c = dst_uri.s;
... ...
@@ -1738,7 +1738,7 @@ int select_branch_uri(str* res, select_t* s, struct sip_msg* msg) {
1738 1738
 		if (n < 0 || n >= nr_branches) 
1739 1739
 			return -1;
1740 1740
 		init_branch_iterator();
1741
-		for (; (c = next_branch(&l, &q, &dst_uri.s, &dst_uri.len, 0)) && n; n--);
1741
+		for (; (c = next_branch(&l, &q, &dst_uri, 0, 0, 0)) && n; n--);
1742 1742
 		if (!c) return 1;
1743 1743
 		
1744 1744
 		if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {