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 168
  * more branches
204 169
  */
205 170
 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)
171
+				 str* path, unsigned int *flags,
172
+				 struct socket_info** force_socket)
207 173
 {
208 174
 	if (i < nr_branches) {
209 175
 		*len = branches[i].len;
... ...
@@ -228,6 +194,10 @@ char* get_branch(unsigned int i, int* len, qvalue_t* q, str* dst_uri,
228 194
 			dst_uri->s = 0;
229 195
 			dst_uri->len = 0;
230 196
 		}
197
+		if (path) {
198
+			path->s = 0;
199
+			path->len = 0;
200
+		}
231 201
 		if (force_socket)
232 202
 			*force_socket = 0;
233 203
 		if (flags)
... ...
@@ -237,6 +207,23 @@ char* get_branch(unsigned int i, int* len, qvalue_t* q, str* dst_uri,
237 207
 }
238 208
 
239 209
 
210
+
211
+/** Return the next branch from the dset array.
212
+ * 0 is returned if there are no more branches
213
+ */
214
+char* next_branch(int* len, qvalue_t* q, str* dst_uri, str* path,
215
+					unsigned int* flags, struct socket_info** force_socket)
216
+{
217
+	char* ret;
218
+	
219
+	ret=get_branch(branch_iterator, len, q, dst_uri, path, flags,
220
+					force_socket);
221
+	if (likely(ret))
222
+		branch_iterator++;
223
+	return ret;
224
+}
225
+
226
+
240 227
 /*
241 228
  * Empty the dset array
242 229
  */
... ...
@@ -247,84 +234,32 @@ void clear_branches(void)
247 234
 }
248 235
 
249 236
 
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 237
 
310
-/* ! \brief
311
- * Add a new branch to current transaction using str parameters
312
- * Kamailio compatibility version
238
+/**  Add a new branch to the current transaction.
239
+ * @param msg - sip message, used for getting the uri if not specified (0).
240
+ * @param uri - uri, can be 0 (in which case the uri is taken from msg)
241
+ * @param dst_uri - destination uri, can be 0.
242
+ * @param path - path vector (passed in a string), can be 0.
243
+ * @param q  - q value.
244
+ * @param flags - per branch flags.
245
+ * @param force_socket - socket that should be used when sending.
246
+ *
247
+ * @return  <0 (-1) on failure, 1 on success (script convention).
313 248
  */
314
-int km_append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
249
+int append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
315 250
 		qvalue_t q, unsigned int flags, struct socket_info* force_socket)
316 251
 {
317 252
 	str luri;
318 253
 
319 254
 #ifdef USE_LOCAL_ROUTE
320
-	if (dset_state==0)
255
+	if (unlikely(dset_state==0))
321 256
 		return -1;
322 257
 #endif
323 258
 
324 259
 	/* if we have already set up the maximum number
325 260
 	 * of branches, don't try new ones 
326 261
 	 */
327
-	if (nr_branches == MAX_BRANCHES - 1) {
262
+	if (unlikely(nr_branches == MAX_BRANCHES - 1)) {
328 263
 		LOG(L_ERR, "max nr of branches exceeded\n");
329 264
 		ser_error = E_TOO_MANY_BRANCHES;
330 265
 		return -1;
... ...
@@ -340,16 +275,15 @@ int km_append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
340 275
 		luri = *uri;
341 276
 	}
342 277
 
343
-	if (luri.len > MAX_URI_SIZE - 1) {
278
+	if (unlikely(luri.len > MAX_URI_SIZE - 1)) {
344 279
 		LOG(L_ERR, "too long uri: %.*s\n", luri.len, luri.s);
345 280
 		return -1;
346 281
 	}
347 282
 
348 283
 	/* copy the dst_uri */
349 284
 	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);
285
+		if (unlikely(dst_uri->len > MAX_URI_SIZE - 1)) {
286
+			LOG(L_ERR, "too long dst_uri: %.*s\n", dst_uri->len, dst_uri->s);
353 287
 			return -1;
354 288
 		}
355 289
 		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 295
 	}
362 296
 
363 297
 	/* copy the path string */
364
-	if (path && path->len && path->s) {
365
-		if (path->len > MAX_PATH_SIZE - 1) {
298
+	if (unlikely(path && path->len && path->s)) {
299
+		if (unlikely(path->len > MAX_PATH_SIZE - 1)) {
366 300
 			LOG(L_ERR, "too long path: %.*s\n", path->len, path->s);
367 301
 			return -1;
368 302
 		}
... ...
@@ -413,7 +347,7 @@ char* print_dset(struct sip_msg* msg, int* len)
413 347
 	}
414 348
 
415 349
 	init_branch_iterator();
416
-	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0))) {
350
+	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0))) {
417 351
 		cnt++;
418 352
 		*len += uri.len;
419 353
 		if (q != Q_UNSPECIFIED) {
... ...
@@ -454,7 +388,7 @@ char* print_dset(struct sip_msg* msg, int* len)
454 388
 	}
455 389
 
456 390
 	init_branch_iterator();
457
-	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0))) {
391
+	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0))) {
458 392
 		if (i) {
459 393
 			memcpy(p, CONTACT_DELIM, CONTACT_DELIM_LEN);
460 394
 			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 79
  */
59 80
 int get_branch_iterator(void);
60 81
 
61
-/*
62
- * Get the next branch in the current transaction
82
+
83
+/** Get the next branch in the current transaction.
84
+ * @return pointer to the uri of the next branch (which the length written in
85
+ *  *len) or 0 if there are no more branches.
63 86
  */
64
-char* next_branch(int* len, qvalue_t* q, char** dst_uri, int* dst_len, struct socket_info** force_socket);
87
+char* next_branch(int* len, qvalue_t* q, str* dst_uri, str* path,
88
+					unsigned int* flags, struct socket_info** force_socket);
65 89
 
66 90
 
67 91
 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);
92
+				  str* path, unsigned int *flags,
93
+				  struct socket_info** force_socket);
94
+
69 95
 
70 96
 
71 97
 /*
... ...
@@ -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) {