Răsfoiți Sursa

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 16 ani în urmă
părinte
comite
c43dc0cd35
4 a modificat fișierele cu 83 adăugiri și 123 ștergeri
  1. 1 1
      action.c
  2. 43 109
      dset.c
  3. 36 10
      dset.h
  4. 3 3
      select_core.c

+ 1 - 1
action.c

@@ -344,7 +344,7 @@ int do_action(struct run_act_ctx* h, struct action* a, struct sip_msg* msg)
 				ret=E_BUG;
 				ret=E_BUG;
 				goto error;
 				goto error;
 			}
 			}
-			ret=append_branch( msg, a->val[0].u.string,
+			ret=ser_append_branch( msg, a->val[0].u.string,
 					   a->val[0].u.string ? strlen(a->val[0].u.string):0,
 					   a->val[0].u.string ? strlen(a->val[0].u.string):0,
 					   0, 0, a->val[1].u.number, 0);
 					   0, 0, a->val[1].u.number, 0);
 			break;
 			break;

+ 43 - 109
dset.c

@@ -160,41 +160,6 @@ int get_branch_iterator(void)
 	return branch_iterator;
 	return branch_iterator;
 }
 }
 
 
-/*
- * Return the next branch from the dset
- * array, 0 is returned if there are no
- * more branches
- */
-char* next_branch(int* len, qvalue_t* q, char** dst_uri, int* dst_len, struct socket_info** force_socket)
-{
-	unsigned int i;
-
-	i = branch_iterator;
-	if (i < nr_branches) {
-		branch_iterator++;
-		*len = branches[i].len;
-		*q = branches[i].q;
-		if (dst_uri && dst_len) {
-			*dst_uri = branches[i].dst_uri;
-			*dst_len = branches[i].dst_uri_len;
-		}
-		if (force_socket) {
-			*force_socket = branches[i].force_send_socket;
-		}
-		return branches[i].uri;
-	} else {
-		*len = 0;
-		*q = Q_UNSPECIFIED;
-		if (dst_uri && dst_len) {
-			*dst_uri = 0;
-			*dst_len = 0;
-		}
-		if (force_socket) {
-			*force_socket = 0;
-		}
-		return 0;
-	}
-}
 
 
 
 
 /** \brief Get a branch from the destination set
 /** \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
  * more branches
  * more branches
  */
  */
 char* get_branch(unsigned int i, int* len, qvalue_t* q, str* dst_uri,
 char* get_branch(unsigned int i, int* len, qvalue_t* q, str* dst_uri,
-				 str* path, unsigned int *flags, struct socket_info** force_socket)
+				 str* path, unsigned int *flags,
+				 struct socket_info** force_socket)
 {
 {
 	if (i < nr_branches) {
 	if (i < nr_branches) {
 		*len = branches[i].len;
 		*len = branches[i].len;
@@ -228,6 +194,10 @@ char* get_branch(unsigned int i, int* len, qvalue_t* q, str* dst_uri,
 			dst_uri->s = 0;
 			dst_uri->s = 0;
 			dst_uri->len = 0;
 			dst_uri->len = 0;
 		}
 		}
+		if (path) {
+			path->s = 0;
+			path->len = 0;
+		}
 		if (force_socket)
 		if (force_socket)
 			*force_socket = 0;
 			*force_socket = 0;
 		if (flags)
 		if (flags)
@@ -237,6 +207,23 @@ char* get_branch(unsigned int i, int* len, qvalue_t* q, str* dst_uri,
 }
 }
 
 
 
 
+
+/** Return the next branch from the dset array.
+ * 0 is returned if there are no more branches
+ */
+char* next_branch(int* len, qvalue_t* q, str* dst_uri, str* path,
+					unsigned int* flags, struct socket_info** force_socket)
+{
+	char* ret;
+	
+	ret=get_branch(branch_iterator, len, q, dst_uri, path, flags,
+					force_socket);
+	if (likely(ret))
+		branch_iterator++;
+	return ret;
+}
+
+
 /*
 /*
  * Empty the dset array
  * Empty the dset array
  */
  */
@@ -247,84 +234,32 @@ void clear_branches(void)
 }
 }
 
 
 
 
-/* 
- * Add a new branch to current transaction 
- */
-int append_branch(struct sip_msg* msg, char* uri, int uri_len, char* dst_uri, int dst_uri_len, 
-		  qvalue_t q, struct socket_info* force_socket)
-{
-	     /* if we have already set up the maximum number
-	      * of branches, don't try new ones 
-	      */
-	if (nr_branches == MAX_BRANCHES - 1) {
-		LOG(L_ERR, "ERROR: append_branch: max nr of branches exceeded\n");
-		ser_error = E_TOO_MANY_BRANCHES;
-		return -1;
-	}
-
-	if (uri_len > MAX_URI_SIZE - 1) {
-		LOG(L_ERR, "ERROR: append_branch: too long uri: %.*s\n",
-		    uri_len, uri);
-		return -1;
-	}
-	
-	if (dst_uri_len > MAX_URI_SIZE - 1) {
-		LOG(L_ERR, "ERROR: append_branch: too long dst_uri: %.*s\n",
-		    dst_uri_len, ZSW(dst_uri));
-		return -1;
-	}
-
-	     /* if not parameterized, take current uri */
-	if (uri == 0) {
-		if (msg->new_uri.s) { 
-			uri = msg->new_uri.s;
-			uri_len = msg->new_uri.len;
-		} else {
-			uri = msg->first_line.u.request.uri.s;
-			uri_len = msg->first_line.u.request.uri.len;
-		}
-	}
-	
-	memcpy(branches[nr_branches].uri, uri, uri_len);
-	     /* be safe -- add zero termination */
-	branches[nr_branches].uri[uri_len] = 0;
-	branches[nr_branches].len = uri_len;
-	branches[nr_branches].q = q;
-	
- 	if (dst_uri && dst_uri_len) {
-  		memcpy(branches[nr_branches].dst_uri, dst_uri, dst_uri_len);
-  		branches[nr_branches].dst_uri[dst_uri_len] = 0;
-  		branches[nr_branches].dst_uri_len = dst_uri_len;
- 	} else {
- 		branches[nr_branches].dst_uri[0] = '\0';
- 		branches[nr_branches].dst_uri_len = 0;
-	}
-
-	branches[nr_branches].force_send_socket = force_socket;
-	
-	nr_branches++;
-	return 1;
-}
-
 
 
-/* ! \brief
- * Add a new branch to current transaction using str parameters
- * Kamailio compatibility version
+/**  Add a new branch to the current transaction.
+ * @param msg - sip message, used for getting the uri if not specified (0).
+ * @param uri - uri, can be 0 (in which case the uri is taken from msg)
+ * @param dst_uri - destination uri, can be 0.
+ * @param path - path vector (passed in a string), can be 0.
+ * @param q  - q value.
+ * @param flags - per branch flags.
+ * @param force_socket - socket that should be used when sending.
+ *
+ * @return  <0 (-1) on failure, 1 on success (script convention).
  */
  */
-int km_append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
+int append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
 		qvalue_t q, unsigned int flags, struct socket_info* force_socket)
 		qvalue_t q, unsigned int flags, struct socket_info* force_socket)
 {
 {
 	str luri;
 	str luri;
 
 
 #ifdef USE_LOCAL_ROUTE
 #ifdef USE_LOCAL_ROUTE
-	if (dset_state==0)
+	if (unlikely(dset_state==0))
 		return -1;
 		return -1;
 #endif
 #endif
 
 
 	/* if we have already set up the maximum number
 	/* if we have already set up the maximum number
 	 * of branches, don't try new ones 
 	 * of branches, don't try new ones 
 	 */
 	 */
-	if (nr_branches == MAX_BRANCHES - 1) {
+	if (unlikely(nr_branches == MAX_BRANCHES - 1)) {
 		LOG(L_ERR, "max nr of branches exceeded\n");
 		LOG(L_ERR, "max nr of branches exceeded\n");
 		ser_error = E_TOO_MANY_BRANCHES;
 		ser_error = E_TOO_MANY_BRANCHES;
 		return -1;
 		return -1;
@@ -340,16 +275,15 @@ int km_append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
 		luri = *uri;
 		luri = *uri;
 	}
 	}
 
 
-	if (luri.len > MAX_URI_SIZE - 1) {
+	if (unlikely(luri.len > MAX_URI_SIZE - 1)) {
 		LOG(L_ERR, "too long uri: %.*s\n", luri.len, luri.s);
 		LOG(L_ERR, "too long uri: %.*s\n", luri.len, luri.s);
 		return -1;
 		return -1;
 	}
 	}
 
 
 	/* copy the dst_uri */
 	/* copy the dst_uri */
 	if (dst_uri && dst_uri->len && dst_uri->s) {
 	if (dst_uri && dst_uri->len && dst_uri->s) {
-		if (dst_uri->len > MAX_URI_SIZE - 1) {
-			LOG(L_ERR, "too long dst_uri: %.*s\n",
-				dst_uri->len, dst_uri->s);
+		if (unlikely(dst_uri->len > MAX_URI_SIZE - 1)) {
+			LOG(L_ERR, "too long dst_uri: %.*s\n", dst_uri->len, dst_uri->s);
 			return -1;
 			return -1;
 		}
 		}
 		memcpy(branches[nr_branches].dst_uri, dst_uri->s, dst_uri->len);
 		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,
 	}
 	}
 
 
 	/* copy the path string */
 	/* copy the path string */
-	if (path && path->len && path->s) {
-		if (path->len > MAX_PATH_SIZE - 1) {
+	if (unlikely(path && path->len && path->s)) {
+		if (unlikely(path->len > MAX_PATH_SIZE - 1)) {
 			LOG(L_ERR, "too long path: %.*s\n", path->len, path->s);
 			LOG(L_ERR, "too long path: %.*s\n", path->len, path->s);
 			return -1;
 			return -1;
 		}
 		}
@@ -413,7 +347,7 @@ char* print_dset(struct sip_msg* msg, int* len)
 	}
 	}
 
 
 	init_branch_iterator();
 	init_branch_iterator();
-	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0))) {
+	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0))) {
 		cnt++;
 		cnt++;
 		*len += uri.len;
 		*len += uri.len;
 		if (q != Q_UNSPECIFIED) {
 		if (q != Q_UNSPECIFIED) {
@@ -454,7 +388,7 @@ char* print_dset(struct sip_msg* msg, int* len)
 	}
 	}
 
 
 	init_branch_iterator();
 	init_branch_iterator();
-	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0))) {
+	while ((uri.s = next_branch(&uri.len, &q, 0, 0, 0, 0))) {
 		if (i) {
 		if (i) {
 			memcpy(p, CONTACT_DELIM, CONTACT_DELIM_LEN);
 			memcpy(p, CONTACT_DELIM, CONTACT_DELIM_LEN);
 			p += CONTACT_DELIM_LEN;
 			p += CONTACT_DELIM_LEN;

+ 36 - 10
dset.h

@@ -37,18 +37,39 @@ struct sip_msg;
 extern unsigned int nr_branches;
 extern unsigned int nr_branches;
 
 
 
 
-/* 
+
+/*
  * Add a new branch to current transaction 
  * Add a new branch to current transaction 
  */
  */
-int append_branch(struct sip_msg* msg, char* uri, int uri_len, char* dst_uri, int dst_uri_len, 
-		  qvalue_t q, struct socket_info* force_socket);
+int append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
+					 qvalue_t q, unsigned int flags,
+					 struct socket_info* force_socket);
 
 
+/* kamailio compatible version */
+#define km_append_branch(msg, uri, dst_uri, path, q, flags, force_socket) \
+	append_branch(msg, uri, dst_uri, path, q, flags, force_socket)
 
 
-int km_append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
-					 qvalue_t q, unsigned int flags, struct socket_info* force_socket);
+/** ser compatible append_branch version.
+ *  append_branch version compatible with ser: no path or branch flags support
+ *  and no str parameters.
+ */
+static inline int ser_append_branch(struct sip_msg* msg,
+									char* uri, int uri_len,
+									char* dst_uri, int dst_uri_len,
+									qvalue_t q,
+									struct socket_info* force_socket)
+{
+	str s_uri, s_dst_uri;
+	s_uri.s=uri;
+	s_uri.len=uri_len;
+	s_dst_uri.s=dst_uri;
+	s_dst_uri.len=dst_uri_len;
+	return append_branch(msg, &s_uri, &s_dst_uri, 0, q, 0, force_socket);
+}
 
 
 
 
-/* 
+
+/*
  * Iterate through the list of transaction branches 
  * Iterate through the list of transaction branches 
  */
  */
 void init_branch_iterator(void);
 void init_branch_iterator(void);
@@ -58,14 +79,19 @@ void init_branch_iterator(void);
  */
  */
 int get_branch_iterator(void);
 int get_branch_iterator(void);
 
 
-/*
- * Get the next branch in the current transaction
+
+/** Get the next branch in the current transaction.
+ * @return pointer to the uri of the next branch (which the length written in
+ *  *len) or 0 if there are no more branches.
  */
  */
-char* next_branch(int* len, qvalue_t* q, char** dst_uri, int* dst_len, struct socket_info** force_socket);
+char* next_branch(int* len, qvalue_t* q, str* dst_uri, str* path,
+					unsigned int* flags, struct socket_info** force_socket);
 
 
 
 
 char* get_branch( unsigned int i, int* len, qvalue_t* q, str* dst_uri,
 char* get_branch( unsigned int i, int* len, qvalue_t* q, str* dst_uri,
-				  str* path, unsigned int *flags, struct socket_info** force_socket);
+				  str* path, unsigned int *flags,
+				  struct socket_info** force_socket);
+
 
 
 
 
 /*
 /*

+ 3 - 3
select_core.c

@@ -1672,7 +1672,7 @@ int select_branch_uri(str* res, select_t* s, struct sip_msg* msg) {
 		char *c;
 		char *c;
 		init_branch_iterator();
 		init_branch_iterator();
 		len = 0;
 		len = 0;
-		while ((c = next_branch(&l, &q, &dst_uri.s, &dst_uri.len, 0))) {
+		while ((c = next_branch(&l, &q, &dst_uri, 0, 0, 0))) {
 
 
 			if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
 			if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
 				l = dst_uri.len;
 				l = dst_uri.len;
@@ -1696,7 +1696,7 @@ int select_branch_uri(str* res, select_t* s, struct sip_msg* msg) {
 		init_branch_iterator();
 		init_branch_iterator();
 		res->len = 0;
 		res->len = 0;
 		n = 0;
 		n = 0;
-		while ((c = next_branch(&l, &q, &dst_uri.s, &dst_uri.len, 0))) {
+		while ((c = next_branch(&l, &q, &dst_uri, 0, 0, 0))) {
 			if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
 			if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
 				l = dst_uri.len;
 				l = dst_uri.len;
 				c = dst_uri.s;
 				c = dst_uri.s;
@@ -1738,7 +1738,7 @@ int select_branch_uri(str* res, select_t* s, struct sip_msg* msg) {
 		if (n < 0 || n >= nr_branches) 
 		if (n < 0 || n >= nr_branches) 
 			return -1;
 			return -1;
 		init_branch_iterator();
 		init_branch_iterator();
-		for (; (c = next_branch(&l, &q, &dst_uri.s, &dst_uri.len, 0)) && n; n--);
+		for (; (c = next_branch(&l, &q, &dst_uri, 0, 0, 0)) && n; n--);
 		if (!c) return 1;
 		if (!c) return 1;
 		
 		
 		if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {
 		if (s->params[SEL_POS].v.i & SEL_BRANCH_DST_URI) {