Browse code

Kamailio compatibility: dset manipulation functions

This patch adds two extra kamailio compatibility functions:
* get_branch which allows the caller to obtain the parameters of a
branch using its index
* km_append_branch which is similar to append_branch in sip-router
except that the parameters of the function are slightly different.

Jan Janak authored on 15/03/2009 23:48:06
Showing 2 changed files
... ...
@@ -174,6 +174,46 @@ char* next_branch(int* len, qvalue_t* q, char** dst_uri, int* dst_len, struct so
174 174
 }
175 175
 
176 176
 
177
+/** \brief Get a branch from the destination set
178
+ * \return Return the 'i' branch from the dset
179
+ * array, 0 is returned if there are no
180
+ * more branches
181
+ */
182
+char* get_branch(unsigned int i, int* len, qvalue_t* q, str* dst_uri,
183
+				 str* path, unsigned int *flags, struct socket_info** force_socket)
184
+{
185
+	if (i < nr_branches) {
186
+		*len = branches[i].len;
187
+		*q = branches[i].q;
188
+		if (dst_uri) {
189
+			dst_uri->len = branches[i].dst_uri_len;
190
+			dst_uri->s = (dst_uri->len)?branches[i].dst_uri:0;
191
+		}
192
+		if (path) {
193
+			path->len = branches[i].path_len;
194
+			path->s = (path->len)?branches[i].path:0;
195
+		}
196
+		if (force_socket)
197
+			*force_socket = branches[i].force_send_socket;
198
+		if (flags)
199
+			*flags = branches[i].flags;
200
+		return branches[i].uri;
201
+	} else {
202
+		*len = 0;
203
+		*q = Q_UNSPECIFIED;
204
+		if (dst_uri) {
205
+			dst_uri->s = 0;
206
+			dst_uri->len = 0;
207
+		}
208
+		if (force_socket)
209
+			*force_socket = 0;
210
+		if (flags)
211
+			*flags = 0;
212
+		return 0;
213
+	}
214
+}
215
+
216
+
177 217
 /*
178 218
  * Empty the dset array
179 219
  */
... ...
@@ -244,6 +284,87 @@ int append_branch(struct sip_msg* msg, char* uri, int uri_len, char* dst_uri, in
244 244
 }
245 245
 
246 246
 
247
+/* ! \brief
248
+ * Add a new branch to current transaction using str parameters
249
+ * Kamailio compatibility version
250
+ */
251
+int km_append_branch(struct sip_msg* msg, str* uri, str* dst_uri, str* path,
252
+		qvalue_t q, unsigned int flags, struct socket_info* force_socket)
253
+{
254
+	str luri;
255
+
256
+#ifdef USE_LOCAL_ROUTE
257
+	if (dset_state==0)
258
+		return -1;
259
+#endif
260
+
261
+	/* if we have already set up the maximum number
262
+	 * of branches, don't try new ones 
263
+	 */
264
+	if (nr_branches == MAX_BRANCHES - 1) {
265
+		LOG(L_ERR, "max nr of branches exceeded\n");
266
+		ser_error = E_TOO_MANY_BRANCHES;
267
+		return -1;
268
+	}
269
+
270
+	/* if not parameterized, take current uri */
271
+	if (uri==0 || uri->len==0 || uri->s==0) {
272
+		if (msg->new_uri.s)
273
+			luri = msg->new_uri;
274
+		else
275
+			luri = msg->first_line.u.request.uri;
276
+	} else {
277
+		luri = *uri;
278
+	}
279
+
280
+	if (luri.len > MAX_URI_SIZE - 1) {
281
+		LOG(L_ERR, "too long uri: %.*s\n", luri.len, luri.s);
282
+		return -1;
283
+	}
284
+
285
+	/* copy the dst_uri */
286
+	if (dst_uri && dst_uri->len && dst_uri->s) {
287
+		if (dst_uri->len > MAX_URI_SIZE - 1) {
288
+			LOG(L_ERR, "too long dst_uri: %.*s\n",
289
+				dst_uri->len, dst_uri->s);
290
+			return -1;
291
+		}
292
+		memcpy(branches[nr_branches].dst_uri, dst_uri->s, dst_uri->len);
293
+		branches[nr_branches].dst_uri[dst_uri->len] = 0;
294
+		branches[nr_branches].dst_uri_len = dst_uri->len;
295
+	} else {
296
+		branches[nr_branches].dst_uri[0] = '\0';
297
+		branches[nr_branches].dst_uri_len = 0;
298
+	}
299
+
300
+	/* copy the path string */
301
+	if (path && path->len && path->s) {
302
+		if (path->len > MAX_PATH_SIZE - 1) {
303
+			LOG(L_ERR, "too long path: %.*s\n", path->len, path->s);
304
+			return -1;
305
+		}
306
+		memcpy(branches[nr_branches].path, path->s, path->len);
307
+		branches[nr_branches].path[path->len] = 0;
308
+		branches[nr_branches].path_len = path->len;
309
+	} else {
310
+		branches[nr_branches].path[0] = '\0';
311
+		branches[nr_branches].path_len = 0;
312
+	}
313
+
314
+	/* copy the ruri */
315
+	memcpy(branches[nr_branches].uri, luri.s, luri.len);
316
+	branches[nr_branches].uri[luri.len] = 0;
317
+	branches[nr_branches].len = luri.len;
318
+	branches[nr_branches].q = q;
319
+
320
+	branches[nr_branches].force_send_socket = force_socket;
321
+	branches[nr_branches].flags = flags;
322
+
323
+	nr_branches++;
324
+	return 1;
325
+}
326
+
327
+
247 328
 /*
248 329
  * Create a Contact header field from the dset
249 330
  * array
... ...
@@ -44,6 +44,10 @@ int append_branch(struct sip_msg* msg, char* uri, int uri_len, char* dst_uri, in
44 44
 		  qvalue_t q, struct socket_info* force_socket);
45 45
 
46 46
 
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);
49
+
50
+
47 51
 /* 
48 52
  * Iterate through the list of transaction branches 
49 53
  */
... ...
@@ -56,6 +60,10 @@ void init_branch_iterator(void);
56 56
 char* next_branch(int* len, qvalue_t* q, char** dst_uri, int* dst_len, struct socket_info** force_socket);
57 57
 
58 58
 
59
+char* get_branch( unsigned int i, int* len, qvalue_t* q, str* dst_uri,
60
+				  str* path, unsigned int *flags, struct socket_info** force_socket);
61
+
62
+
59 63
 /*
60 64
  * Empty the array of branches
61 65
  */