Browse code

path: set dst udp uri length for decoding received value in alias format

Daniel-Constantin Mierla authored on 30/03/2021 09:58:53
Showing 1 changed files
... ...
@@ -465,10 +465,13 @@ void path_rr_callback(struct sip_msg *_m, str *r_param, void *cb_param)
465 465
 						p += 10;
466 466
 						memcpy(p, sproto.s, sproto.len);
467 467
 						p += sproto.len;
468
-						*p = '\0';
469
-						dst_uri.len = p - dst_uri_buf;
470
-						break;
468
+					} else {
469
+						/* go back one byte to overwrite ';' */
470
+						p--;
471 471
 					}
472
+					*p = '\0';
473
+					dst_uri.len = p - dst_uri_buf;
474
+					break;
472 475
 				} else {
473 476
 					LM_ERR("invalid number of separators (%d)\n", n);
474 477
 					goto done;
Browse code

path: option to set received param name

Daniel-Constantin Mierla authored on 29/03/2021 13:25:21
Showing 1 changed files
... ...
@@ -57,6 +57,7 @@ const static char *proto_strings[] = {
57 57
 };
58 58
 
59 59
 extern int path_sockname_mode;
60
+extern str path_received_name;
60 61
 
61 62
 static char *path_strzdup(char *src, int len)
62 63
 {
... ...
@@ -105,8 +106,9 @@ static int prepend_path(sip_msg_t* _m, str *user, path_param_t param,
105 106
 	struct hdr_field *hf;
106 107
 
107 108
 	/* maximum possible length of suffix */
108
-	suffix_len = sizeof(";lr;r2=on;received=sip::12345%3Btransport%3Dsctp;ob;>\r\n")
109
-			+ IP_ADDR_MAX_STR_SIZE + 2 + (add_params ? add_params->len : 0) + 1;
109
+	suffix_len = sizeof(";lr;r2=on;=sip::12345%3Btransport%3Dsctp;ob;>\r\n")
110
+			+ IP_ADDR_MAX_STR_SIZE + 2 + (add_params ? add_params->len : 0)
111
+			+ path_received_name.len + 1;
110 112
 
111 113
 	cp = suffix = pkg_malloc(suffix_len);
112 114
 	if (!suffix) {
... ...
@@ -125,21 +127,21 @@ static int prepend_path(sip_msg_t* _m, str *user, path_param_t param,
125 127
 				proto_str = NULL;
126 128
 			}
127 129
 			if(_m->rcv.src_ip.af==AF_INET6) {
128
-				cp += sprintf(cp, ";received=sip:[%s]:%hu%s",
130
+				cp += sprintf(cp, ";%s=sip:[%s]:%hu%s", path_received_name.s,
129 131
 						ip_addr2a(&_m->rcv.src_ip),
130 132
 						_m->rcv.src_port, proto_str ? : "");
131 133
 			} else {
132
-				cp += sprintf(cp, ";received=sip:%s:%hu%s"
133
-						, ip_addr2a(&_m->rcv.src_ip),
134
+				cp += sprintf(cp, ";%s=sip:%s:%hu%s", path_received_name.s,
135
+						ip_addr2a(&_m->rcv.src_ip),
134 136
 						_m->rcv.src_port, proto_str ? : "");
135 137
 			}
136 138
 		} else {
137 139
 			if(_m->rcv.src_ip.af==AF_INET6) {
138
-				cp += sprintf(cp, ";received=[%s]~%hu~%d",
140
+				cp += sprintf(cp, ";%s=[%s]~%hu~%d", path_received_name.s,
139 141
 						ip_addr2a(&_m->rcv.src_ip),
140 142
 						_m->rcv.src_port, (int)_m->rcv.proto);
141 143
 			} else {
142
-				cp += sprintf(cp, ";received=%s~%hu~%d",
144
+				cp += sprintf(cp, ";%s=%s~%hu~%d", path_received_name.s,
143 145
 						ip_addr2a(&_m->rcv.src_ip),
144 146
 						_m->rcv.src_port, (int)_m->rcv.proto);
145 147
 			}
... ...
@@ -375,35 +377,58 @@ void path_rr_callback(struct sip_msg *_m, str *r_param, void *cb_param)
375 377
 {
376 378
 	param_hooks_t hooks;
377 379
 	param_t *params;
380
+	param_t *it;
378 381
 	static char dst_uri_buf[MAX_URI_SIZE];
379 382
 	static str dst_uri;
380 383
 	char *p;
381 384
 	int n;
382 385
 	int nproto;
383 386
 	str sproto;
387
+	str rcvuri = STR_NULL;
384 388
 
385
-	if (parse_params(r_param, CLASS_CONTACT, &hooks, &params) != 0) {
386
-		LM_ERR("failed to parse route parameters\n");
387
-		return;
388
-	}
389
-
390
-	if (hooks.contact.received==NULL
391
-			|| hooks.contact.received->body.len<=0) {
392
-		LM_DBG("no received parameter in route header\n");
393
-		free_params(params);
394
-		return;
389
+	if((path_received_name.len==8)
390
+			&& strncmp(path_received_name.s, "received", 8)==0) {
391
+		if (parse_params(r_param, CLASS_CONTACT, &hooks, &params) != 0) {
392
+			LM_ERR("failed to parse route parameters\n");
393
+			return;
394
+		}
395
+		if (hooks.contact.received==NULL
396
+				|| hooks.contact.received->body.len<=0) {
397
+			LM_DBG("no received parameter in route header\n");
398
+			free_params(params);
399
+			return;
400
+		}
401
+		rcvuri = hooks.contact.received->body;
402
+	} else {
403
+		if (parse_params(r_param, CLASS_ANY, &hooks, &params) != 0) {
404
+			LM_ERR("failed to parse route parameters\n");
405
+			return;
406
+		}
407
+		for(it=params; it; it=it->next) {
408
+			if((it->name.len==path_received_name.len)
409
+					&& strncmp(path_received_name.s, it->name.s,
410
+						it->name.len)==0) {
411
+				break;
412
+			}
413
+		}
414
+		if (it==NULL || it->body.len<=0) {
415
+			LM_DBG("no %s parameter in route header\n", path_received_name.s);
416
+			free_params(params);
417
+			return;
418
+		}
419
+		rcvuri = it->body;
395 420
 	}
396 421
 
397 422
 	/* 24 => sip:...;transport=sctp */
398
-	if(hooks.contact.received->body.len + 24 >= MAX_URI_SIZE) {
399
-		LM_ERR("received uri is too long\n");
423
+	if(rcvuri.len + 24 >= MAX_URI_SIZE) {
424
+		LM_ERR("received uri is too long: %d\n", rcvuri.len);
400 425
 		goto done;
401 426
 	}
402 427
 	dst_uri.s = dst_uri_buf;
403 428
 	dst_uri.len = MAX_URI_SIZE;
404 429
 	if(path_received_format==0) {
405 430
 		/* received=sip:...;transport... */
406
-		if (unescape_user(&(hooks.contact.received->body), &dst_uri) < 0) {
431
+		if (unescape_user(&rcvuri, &dst_uri) < 0) {
407 432
 			LM_ERR("unescaping received failed\n");
408 433
 			free_params(params);
409 434
 			return;
... ...
@@ -411,9 +436,8 @@ void path_rr_callback(struct sip_msg *_m, str *r_param, void *cb_param)
411 436
 	} else {
412 437
 		/* received=ip~port~proto */
413 438
 		memcpy(dst_uri_buf, "sip:", 4);
414
-		memcpy(dst_uri_buf+4, hooks.contact.received->body.s,
415
-					hooks.contact.received->body.len);
416
-		dst_uri_buf[4+hooks.contact.received->body.len] = '\0';
439
+		memcpy(dst_uri_buf+4, rcvuri.s, rcvuri.len);
440
+		dst_uri_buf[4+rcvuri.len] = '\0';
417 441
 		p = dst_uri_buf + 4;
418 442
 		n = 0;
419 443
 		while(*p!='\0') {
Browse code

path: updates for outboud module api

Daniel-Constantin Mierla authored on 15/05/2020 14:51:41
Showing 1 changed files
... ...
@@ -80,7 +80,7 @@ static int handleOutbound(sip_msg_t* _m, str *user, path_param_t *param)
80 80
 	if (path_obb.use_outbound != NULL && path_obb.use_outbound(_m)) {
81 81
 		struct via_body *via;
82 82
 
83
-		if (path_obb.encode_flow_token(user, _m->rcv) != 0) {
83
+		if (path_obb.encode_flow_token(user, &_m->rcv) != 0) {
84 84
 			LM_ERR("encoding outbound flow-token\n");
85 85
 			return -1;
86 86
 		}
Browse code

path: new param sockname_mode

- if set to 1, socket name is added to Path URI in 'sn' param

Daniel-Constantin Mierla authored on 02/04/2020 12:17:36
Showing 1 changed files
... ...
@@ -56,6 +56,8 @@ const static char *proto_strings[] = {
56 56
 	[PROTO_WSS] = "%3Btransport%3Dws",
57 57
 };
58 58
 
59
+extern int path_sockname_mode;
60
+
59 61
 static char *path_strzdup(char *src, int len)
60 62
 {
61 63
 	char *res;
... ...
@@ -187,7 +189,8 @@ static int prepend_path(sip_msg_t* _m, str *user, path_param_t param,
187 189
 
188 190
 	l = insert_new_lump_before(l, prefix, prefix_len, 0);
189 191
 	if (!l) goto out3;
190
-	l = insert_subst_lump_before(l, SUBST_SND_ALL, 0);
192
+	l = insert_subst_lump_before(l,
193
+			(path_sockname_mode)?SUBST_SND_ALL_EX:SUBST_SND_ALL, 0);
191 194
 	if (!l) goto out2;
192 195
 	l = insert_new_lump_before(l, suffix, cp - suffix, 0);
193 196
 	if (!l) goto out2;
... ...
@@ -197,7 +200,8 @@ static int prepend_path(sip_msg_t* _m, str *user, path_param_t param,
197 200
 		if(dp==NULL) goto out1;
198 201
 		l = insert_new_lump_before(l, dp, prefix_len, 0);
199 202
 		if (!l) goto out1;
200
-		l = insert_subst_lump_before(l, SUBST_RCV_ALL, 0);
203
+		l = insert_subst_lump_before(l,
204
+				(path_sockname_mode)?SUBST_RCV_ALL_EX:SUBST_RCV_ALL, 0);
201 205
 		if (!l) goto out1;
202 206
 		dp = path_strzdup(suffix, cp - suffix);
203 207
 		if(dp==NULL) goto out1;
Browse code

path: use memcpy() instead of strncpy()

- avoid gcc 8.2 warning: ‘strncpy’ output truncated before terminating nul

Daniel-Constantin Mierla authored on 20/09/2018 14:37:15
Showing 1 changed files
... ...
@@ -406,8 +406,8 @@ void path_rr_callback(struct sip_msg *_m, str *r_param, void *cb_param)
406 406
 		}
407 407
 	} else {
408 408
 		/* received=ip~port~proto */
409
-		strncpy(dst_uri_buf, "sip:", 4);
410
-		strncpy(dst_uri_buf+4, hooks.contact.received->body.s,
409
+		memcpy(dst_uri_buf, "sip:", 4);
410
+		memcpy(dst_uri_buf+4, hooks.contact.received->body.s,
411 411
 					hooks.contact.received->body.len);
412 412
 		dst_uri_buf[4+hooks.contact.received->body.len] = '\0';
413 413
 		p = dst_uri_buf + 4;
... ...
@@ -433,7 +433,7 @@ void path_rr_callback(struct sip_msg *_m, str *r_param, void *cb_param)
433 433
 							LM_ERR("unknown proto in received param\n");
434 434
 							goto done;
435 435
 						}
436
-						strncpy(p, "transport=", 10);
436
+						memcpy(p, "transport=", 10);
437 437
 						p += 10;
438 438
 						memcpy(p, sproto.s, sproto.len);
439 439
 						p += sproto.len;
Browse code

path: fix typo

Thanks, lintian
> I: kamailio: spelling-error-in-binary usr/lib/x86_64-linux-gnu/kamailio/modules/path.so spearators separators

Victor Seva authored on 08/02/2018 08:38:54
Showing 1 changed files
... ...
@@ -442,7 +442,7 @@ void path_rr_callback(struct sip_msg *_m, str *r_param, void *cb_param)
442 442
 						break;
443 443
 					}
444 444
 				} else {
445
-					LM_ERR("invalid number of spearators (%d)\n", n);
445
+					LM_ERR("invalid number of separators (%d)\n", n);
446 446
 					goto done;
447 447
 				}
448 448
 			}
Browse code

path: insert flow token in path header with received

- added the possiblity to have the received parameter and flow toke
in the add_path_received function

Rick Barenthin authored on 04/10/2017 15:41:39
Showing 1 changed files
... ...
@@ -42,7 +42,7 @@
42 42
 #include "path_mod.h"
43 43
 
44 44
 typedef enum {
45
-	PATH_PARAM_NONE, PATH_PARAM_RECEIVED, PATH_PARAM_OB
45
+	PATH_PARAM_NONE = 0, PATH_PARAM_RECEIVED = 1, PATH_PARAM_OB = 2
46 46
 } path_param_t;
47 47
 
48 48
 #define PATH_PREFIX		"Path: <sip:"
... ...
@@ -73,6 +73,26 @@ static char *path_strzdup(char *src, int len)
73 73
 	return res;
74 74
 }
75 75
 
76
+static int handleOutbound(sip_msg_t* _m, str *user, path_param_t *param)
77
+{
78
+	if (path_obb.use_outbound != NULL && path_obb.use_outbound(_m)) {
79
+		struct via_body *via;
80
+
81
+		if (path_obb.encode_flow_token(user, _m->rcv) != 0) {
82
+			LM_ERR("encoding outbound flow-token\n");
83
+			return -1;
84
+		}
85
+
86
+		/* Only include ;ob parameter if this is the first-hop
87
+		 * (that means only one Via:) */
88
+		if (parse_via_header(_m, 2, &via) < 0) {
89
+			*param |= PATH_PARAM_OB;
90
+		}
91
+	}
92
+
93
+	return 1;
94
+}
95
+
76 96
 static int prepend_path(sip_msg_t* _m, str *user, path_param_t param,
77 97
 		str *add_params)
78 98
 {
... ...
@@ -94,10 +114,7 @@ static int prepend_path(sip_msg_t* _m, str *user, path_param_t param,
94 114
 
95 115
 	cp += sprintf(cp, ";lr");
96 116
 
97
-	switch(param) {
98
-	default:
99
-		break;
100
-	case PATH_PARAM_RECEIVED:
117
+	if (param & PATH_PARAM_RECEIVED) {
101 118
 		if(path_received_format==0) {
102 119
 			if (_m->rcv.proto
103 120
 						< (sizeof(proto_strings) / sizeof(*proto_strings))) {
... ...
@@ -125,14 +142,15 @@ static int prepend_path(sip_msg_t* _m, str *user, path_param_t param,
125 142
 						_m->rcv.src_port, (int)_m->rcv.proto);
126 143
 			}
127 144
 		}
128
-		break;
129
-	case PATH_PARAM_OB:
145
+	}
146
+
147
+	if (param & PATH_PARAM_OB) {
130 148
 		cp += sprintf(cp, ";ob");
131
-		break;
132 149
 	}
133 150
 
134
-	if (add_params && add_params->len)
151
+	if (add_params && add_params->len) {
135 152
 		cp += sprintf(cp, ";%.*s", add_params->len, add_params->s);
153
+	}
136 154
 
137 155
 	if(path_enable_r2==0) {
138 156
 		cp += sprintf(cp, ">\r\n");
... ...
@@ -207,25 +225,16 @@ int ki_add_path(struct sip_msg* _msg)
207 225
 	str user = {0,0};
208 226
 	int ret;
209 227
 	path_param_t param = PATH_PARAM_NONE;
210
-	struct via_body *via;
211 228
 
212
-	if (path_obb.use_outbound != NULL
213
-		&& path_obb.use_outbound(_msg)) {
214
-		if (path_obb.encode_flow_token(&user, _msg->rcv) != 0) {
215
-			LM_ERR("encoding outbound flow-token\n");
216
-			return -1;
217
-		}
229
+	ret = handleOutbound(_msg, &user, &param);
218 230
 
219
-		/* Only include ;ob parameter if this is the first-hop
220
-		 * (that means only one Via:) */
221
-		if (parse_via_header(_msg, 2, &via) < 0)
222
-			param = PATH_PARAM_OB;
231
+	if (ret > 0) {
232
+		ret = prepend_path(_msg, &user, param, NULL);
223 233
 	}
224 234
 
225
-	ret = prepend_path(_msg, &user, param, NULL);
226
-
227
-	if (user.s != NULL)
235
+	if (user.s != NULL) {
228 236
 		pkg_free(user.s);
237
+	}
229 238
 
230 239
 	return ret;
231 240
 }
... ...
@@ -283,18 +292,32 @@ int ki_add_path_user_params(sip_msg_t* _msg, str* _user, str* _params)
283 292
  * Prepend own uri to Path header and append received address as
284 293
  * "received"-param to that uri.
285 294
  */
286
-int add_path_received(struct sip_msg* _msg, char* _a, char* _b)
295
+int ki_add_path_received(sip_msg_t* _msg)
287 296
 {
288
-	return prepend_path(_msg, NULL, PATH_PARAM_RECEIVED, NULL);
297
+	str user = {0,0};
298
+	int ret;
299
+	path_param_t param = PATH_PARAM_RECEIVED;
300
+
301
+	ret = handleOutbound(_msg, &user, &param);
302
+
303
+	if (ret > 0) {
304
+		ret = prepend_path(_msg, &user, param, NULL);
305
+	}
306
+
307
+	if (user.s != NULL) {
308
+		pkg_free(user.s);
309
+	}
310
+
311
+	return ret;
289 312
 }
290 313
 
291 314
 /*! \brief
292 315
  * Prepend own uri to Path header and append received address as
293 316
  * "received"-param to that uri.
294 317
  */
295
-int ki_add_path_received(sip_msg_t* _msg)
318
+int add_path_received(struct sip_msg* _msg, char* _a, char* _b)
296 319
 {
297
-	return prepend_path(_msg, NULL, PATH_PARAM_RECEIVED, NULL);
320
+	return ki_add_path_received(_msg);
298 321
 }
299 322
 
300 323
 /*! \brief
... ...
@@ -338,7 +361,6 @@ int ki_add_path_received_user(sip_msg_t* _msg, str* _user)
338 361
  */
339 362
 int ki_add_path_received_user_params(sip_msg_t* _msg, str* _user, str* _params)
340 363
 {
341
-
342 364
 	return prepend_path(_msg, _user, PATH_PARAM_RECEIVED, _params);
343 365
 }
344 366
 
Browse code

path: fix offset to set ending 0 for received_format=1

Daniel-Constantin Mierla authored on 08/08/2017 19:35:06
Showing 1 changed files
... ...
@@ -387,7 +387,7 @@ void path_rr_callback(struct sip_msg *_m, str *r_param, void *cb_param)
387 387
 		strncpy(dst_uri_buf, "sip:", 4);
388 388
 		strncpy(dst_uri_buf+4, hooks.contact.received->body.s,
389 389
 					hooks.contact.received->body.len);
390
-		dst_uri_buf[hooks.contact.received->body.len] = '\0';
390
+		dst_uri_buf[4+hooks.contact.received->body.len] = '\0';
391 391
 		p = dst_uri_buf + 4;
392 392
 		n = 0;
393 393
 		while(*p!='\0') {
Browse code

path: fix for handling received parameter, issue added by format support

- reported by GH #1205

Daniel-Constantin Mierla authored on 07/08/2017 10:23:18
Showing 1 changed files
... ...
@@ -361,76 +361,83 @@ void path_rr_callback(struct sip_msg *_m, str *r_param, void *cb_param)
361 361
 		return;
362 362
 	}
363 363
 
364
-	if (hooks.contact.received
365
-			&& hooks.contact.received->body.len>0) {
366
-		/* 24 => sip:...;transport=sctp */
367
-		if(hooks.contact.received->body.len + 24 >= MAX_URI_SIZE) {
368
-			LM_ERR("received uri is too long\n");
369
-			goto done;
364
+	if (hooks.contact.received==NULL
365
+			|| hooks.contact.received->body.len<=0) {
366
+		LM_DBG("no received parameter in route header\n");
367
+		free_params(params);
368
+		return;
369
+	}
370
+
371
+	/* 24 => sip:...;transport=sctp */
372
+	if(hooks.contact.received->body.len + 24 >= MAX_URI_SIZE) {
373
+		LM_ERR("received uri is too long\n");
374
+		goto done;
375
+	}
376
+	dst_uri.s = dst_uri_buf;
377
+	dst_uri.len = MAX_URI_SIZE;
378
+	if(path_received_format==0) {
379
+		/* received=sip:...;transport... */
380
+		if (unescape_user(&(hooks.contact.received->body), &dst_uri) < 0) {
381
+			LM_ERR("unescaping received failed\n");
382
+			free_params(params);
383
+			return;
370 384
 		}
371
-		dst_uri.s = dst_uri_buf;
372
-		dst_uri.len = MAX_URI_SIZE;
373
-		if(path_enable_r2==0) {
374
-			if (unescape_user(&(hooks.contact.received->body), &dst_uri) < 0) {
375
-				LM_ERR("unescaping received failed\n");
376
-				free_params(params);
377
-				return;
378
-			} else {
379
-				/* ip~port~proto */
380
-				strncpy(dst_uri_buf, "sip:", 4);
381
-				strncpy(dst_uri_buf+4, hooks.contact.received->body.s,
382
-						hooks.contact.received->body.len);
383
-				dst_uri_buf[hooks.contact.received->body.len] = '\0';
384
-				p = dst_uri_buf + 4;
385
-				n = 0;
386
-				while(*p!='\0') {
387
-					if(*p=='~') {
388
-						n++;
389
-						if(n==1) {
390
-							/* port */
391
-							*p = ':';
392
-						} else if(n==2) {
393
-							/* proto */
394
-							*p = ';';
395
-							p++;
396
-							if(*p=='\0') {
397
-								LM_ERR("invalid received format\n");
398
-								goto done;
399
-							}
400
-							nproto = *p - '0';
401
-							if (nproto != PROTO_UDP) {
402
-								proto_type_to_str(nproto, &sproto);
403
-								if (sproto.len == 0) {
404
-									LM_ERR("unknown proto in received param\n");
405
-									goto done;
406
-								}
407
-								strncpy(p, "transport=", 10);
408
-								p += 10;
409
-								memcpy(p, sproto.s, sproto.len);
410
-								p += sproto.len;
411
-								*p = '\0';
412
-								dst_uri.len = p - dst_uri_buf;
413
-								break;
414
-							}
415
-						} else {
416
-							LM_ERR("invalid number of spearators (%d)\n", n);
385
+	} else {
386
+		/* received=ip~port~proto */
387
+		strncpy(dst_uri_buf, "sip:", 4);
388
+		strncpy(dst_uri_buf+4, hooks.contact.received->body.s,
389
+					hooks.contact.received->body.len);
390
+		dst_uri_buf[hooks.contact.received->body.len] = '\0';
391
+		p = dst_uri_buf + 4;
392
+		n = 0;
393
+		while(*p!='\0') {
394
+			if(*p=='~') {
395
+				n++;
396
+				if(n==1) {
397
+					/* port */
398
+					*p = ':';
399
+				} else if(n==2) {
400
+					/* proto */
401
+					*p = ';';
402
+					p++;
403
+					if(*p=='\0') {
404
+						LM_ERR("invalid received format\n");
405
+						goto done;
406
+					}
407
+					nproto = *p - '0';
408
+					if (nproto != PROTO_UDP) {
409
+						proto_type_to_str(nproto, &sproto);
410
+						if (sproto.len == 0) {
411
+							LM_ERR("unknown proto in received param\n");
417 412
 							goto done;
418 413
 						}
414
+						strncpy(p, "transport=", 10);
415
+						p += 10;
416
+						memcpy(p, sproto.s, sproto.len);
417
+						p += sproto.len;
418
+						*p = '\0';
419
+						dst_uri.len = p - dst_uri_buf;
420
+						break;
419 421
 					}
420
-					p++;
422
+				} else {
423
+					LM_ERR("invalid number of spearators (%d)\n", n);
424
+					goto done;
421 425
 				}
422 426
 			}
427
+			p++;
423 428
 		}
424
-		LM_DBG("setting dst uri: %.*s\n", dst_uri.len, dst_uri.s);
425
-		if (set_dst_uri(_m, &dst_uri) != 0) {
426
-			LM_ERR("failed to set dst-uri\n");
427
-			free_params(params);
428
-			return;
429
-		}
430
-		/* dst_uri changed, so it makes sense to re-use the current uri for
431
-			forking */
432
-		ruri_mark_new(); /* re-use uri for serial forking */
433 429
 	}
430
+
431
+	LM_DBG("setting dst uri: %.*s\n", dst_uri.len, dst_uri.s);
432
+	if (set_dst_uri(_m, &dst_uri) != 0) {
433
+		LM_ERR("failed to set dst-uri\n");
434
+		free_params(params);
435
+		return;
436
+	}
437
+	/* dst_uri changed, so it makes sense to re-use the current uri
438
+	 * for forking */
439
+	ruri_mark_new(); /* re-use uri for serial forking */
440
+
434 441
 done:
435 442
 	free_params(params);
436 443
 }
Browse code

path: handle return code when getting the values for cfg parameters

Daniel-Constantin Mierla authored on 19/07/2017 09:03:49
Showing 1 changed files
... ...
@@ -244,10 +244,18 @@ int add_path_usr(struct sip_msg* _msg, char* _usr, char* _parms)
244 244
 	str user = {0,0};
245 245
 	str parms = {0,0};
246 246
 
247
-	if (_usr)
248
-		get_str_fparam(&user, _msg, (fparam_t *) _usr);
249
-	if (_parms)
250
-		get_str_fparam(&parms, _msg, (fparam_t *) _parms);
247
+	if (_usr) {
248
+		if(get_str_fparam(&user, _msg, (fparam_t *) _usr)<0) {
249
+			LM_ERR("failed to get user value\n");
250
+			return -1;
251
+		}
252
+	}
253
+	if (_parms) {
254
+		if(get_str_fparam(&parms, _msg, (fparam_t *) _parms)<0) {
255
+			LM_ERR("failed to get params value\n");
256
+			return -1;
257
+		}
258
+	}
251 259
 
252 260
 	return prepend_path(_msg, &user, PATH_PARAM_NONE, &parms);
253 261
 }
... ...
@@ -298,10 +306,18 @@ int add_path_received_usr(struct sip_msg* _msg, char* _usr, char* _parms)
298 306
 	str user = {0,0};
299 307
 	str parms = {0,0};
300 308
 
301
-	if (_usr)
302
-		get_str_fparam(&user, _msg, (fparam_t *) _usr);
303
-	if (_parms)
304
-		get_str_fparam(&parms, _msg, (fparam_t *) _parms);
309
+	if (_usr) {
310
+		if(get_str_fparam(&user, _msg, (fparam_t *) _usr)<0) {
311
+			LM_ERR("failed to get user value\n");
312
+			return -1;
313
+		}
314
+	}
315
+	if (_parms) {
316
+		if(get_str_fparam(&parms, _msg, (fparam_t *) _parms)<0) {
317
+			LM_ERR("failed to get params value\n");
318
+			return -1;
319
+		}
320
+	}
305 321
 
306 322
 	return prepend_path(_msg, &user, PATH_PARAM_RECEIVED, &parms);
307 323
 }
Browse code

path: fix for second path insert pointer

Daniel-Constantin Mierla authored on 27/06/2017 13:53:20
Showing 1 changed files
... ...
@@ -183,7 +183,7 @@ static int prepend_path(sip_msg_t* _m, str *user, path_param_t param,
183 183
 		if (!l) goto out1;
184 184
 		dp = path_strzdup(suffix, cp - suffix);
185 185
 		if(dp==NULL) goto out1;
186
-		l = insert_new_lump_before(l, suffix, cp - suffix, 0);
186
+		l = insert_new_lump_before(l, dp, cp - suffix, 0);
187 187
 		if (!l) goto out1;
188 188
 	}
189 189
 
Browse code

path: option to add two path headers and use alias like received value

- enable_r2 - add two path headers, one for incoming socket and one for
outgoing socket
- received_format - parameter to control if received value should be
in sip uri (as so far) or in contact-alias param format

Daniel-Constantin Mierla authored on 27/06/2017 13:04:31
Showing 1 changed files
... ...
@@ -34,6 +34,7 @@
34 34
 #include "../../core/mem/mem.h"
35 35
 #include "../../core/data_lump.h"
36 36
 #include "../../core/parser/parse_param.h"
37
+#include "../../core/parser/parse_uri.h"
37 38
 #include "../../core/strutils.h"
38 39
 #include "../../core/dset.h"
39 40
 
... ...
@@ -55,16 +56,34 @@ const static char *proto_strings[] = {
55 56
 	[PROTO_WSS] = "%3Btransport%3Dws",
56 57
 };
57 58
 
58
-static int prepend_path(struct sip_msg* _m, str *user, path_param_t param, str *add_params)
59
+static char *path_strzdup(char *src, int len)
60
+{
61
+	char *res;
62
+
63
+	if (!src)
64
+		return NULL;
65
+	if(len<0) {
66
+		len = strlen(src);
67
+	}
68
+	if (!(res = (char *) pkg_malloc(len + 1)))
69
+		return NULL;
70
+	strncpy(res, src, len);
71
+	res[len] = 0;
72
+
73
+	return res;
74
+}
75
+
76
+static int prepend_path(sip_msg_t* _m, str *user, path_param_t param,
77
+		str *add_params)
59 78
 {
60 79
 	struct lump *l;
61
-	char *prefix, *suffix, *cp;
80
+	char *prefix, *suffix, *cp, *dp;
62 81
 	const char *proto_str;
63 82
 	int prefix_len, suffix_len;
64 83
 	struct hdr_field *hf;
65 84
 
66 85
 	/* maximum possible length of suffix */
67
-	suffix_len = strlen(";lr;received=sip::12345%3Btransport%3Dsctp;ob;>\r\n")
86
+	suffix_len = sizeof(";lr;r2=on;received=sip::12345%3Btransport%3Dsctp;ob;>\r\n")
68 87
 			+ IP_ADDR_MAX_STR_SIZE + 2 + (add_params ? add_params->len : 0) + 1;
69 88
 
70 89
 	cp = suffix = pkg_malloc(suffix_len);
... ...
@@ -79,17 +98,32 @@ static int prepend_path(struct sip_msg* _m, str *user, path_param_t param, str *
79 98
 	default:
80 99
 		break;
81 100
 	case PATH_PARAM_RECEIVED:
82
-		if (_m->rcv.proto < (sizeof(proto_strings) / sizeof(*proto_strings)))
83
-			proto_str = proto_strings[(unsigned int) _m->rcv.proto];
84
-		else
85
-			proto_str = NULL;
86
-
87
-		if(_m->rcv.src_ip.af==AF_INET6) {
88
-			cp += sprintf(cp, ";received=sip:[%s]:%hu%s", ip_addr2a(&_m->rcv.src_ip),
89
-					_m->rcv.src_port, proto_str ? : "");
101
+		if(path_received_format==0) {
102
+			if (_m->rcv.proto
103
+						< (sizeof(proto_strings) / sizeof(*proto_strings))) {
104
+				proto_str = proto_strings[(unsigned int) _m->rcv.proto];
105
+			} else {
106
+				proto_str = NULL;
107
+			}
108
+			if(_m->rcv.src_ip.af==AF_INET6) {
109
+				cp += sprintf(cp, ";received=sip:[%s]:%hu%s",
110
+						ip_addr2a(&_m->rcv.src_ip),
111
+						_m->rcv.src_port, proto_str ? : "");
112
+			} else {
113
+				cp += sprintf(cp, ";received=sip:%s:%hu%s"
114
+						, ip_addr2a(&_m->rcv.src_ip),
115
+						_m->rcv.src_port, proto_str ? : "");
116
+			}
90 117
 		} else {
91
-			cp += sprintf(cp, ";received=sip:%s:%hu%s", ip_addr2a(&_m->rcv.src_ip),
92
-					_m->rcv.src_port, proto_str ? : "");
118
+			if(_m->rcv.src_ip.af==AF_INET6) {
119
+				cp += sprintf(cp, ";received=[%s]~%hu~%d",
120
+						ip_addr2a(&_m->rcv.src_ip),
121
+						_m->rcv.src_port, (int)_m->rcv.proto);
122
+			} else {
123
+				cp += sprintf(cp, ";received=%s~%hu~%d",
124
+						ip_addr2a(&_m->rcv.src_ip),
125
+						_m->rcv.src_port, (int)_m->rcv.proto);
126
+			}
93 127
 		}
94 128
 		break;
95 129
 	case PATH_PARAM_OB:
... ...
@@ -100,7 +134,11 @@ static int prepend_path(struct sip_msg* _m, str *user, path_param_t param, str *
100 134
 	if (add_params && add_params->len)
101 135
 		cp += sprintf(cp, ";%.*s", add_params->len, add_params->s);
102 136
 
103
-	cp += sprintf(cp, ">\r\n");
137
+	if(path_enable_r2==0) {
138
+		cp += sprintf(cp, ">\r\n");
139
+	} else {
140
+		cp += sprintf(cp, ";r2=on>\r\n");
141
+	}
104 142
 
105 143
 	prefix_len = PATH_PREFIX_LEN + (user ? user->len : 0) + 2;
106 144
 	prefix = pkg_malloc(prefix_len);
... ...
@@ -136,6 +174,19 @@ static int prepend_path(struct sip_msg* _m, str *user, path_param_t param, str *
136 174
 	l = insert_new_lump_before(l, suffix, cp - suffix, 0);
137 175
 	if (!l) goto out2;
138 176
 
177
+	if(path_enable_r2!=0) {
178
+		dp = path_strzdup(prefix, prefix_len);
179
+		if(dp==NULL) goto out1;
180
+		l = insert_new_lump_before(l, dp, prefix_len, 0);
181
+		if (!l) goto out1;
182
+		l = insert_subst_lump_before(l, SUBST_RCV_ALL, 0);
183
+		if (!l) goto out1;
184
+		dp = path_strzdup(suffix, cp - suffix);
185
+		if(dp==NULL) goto out1;
186
+		l = insert_new_lump_before(l, suffix, cp - suffix, 0);
187
+		if (!l) goto out1;
188
+	}
189
+
139 190
 	return 1;
140 191
 
141 192
 out3:
... ...
@@ -162,11 +213,11 @@ int ki_add_path(struct sip_msg* _msg)
162 213
 		&& path_obb.use_outbound(_msg)) {
163 214
 		if (path_obb.encode_flow_token(&user, _msg->rcv) != 0) {
164 215
 			LM_ERR("encoding outbound flow-token\n");
165
-			return -1;	
216
+			return -1;
166 217
 		}
167 218
 
168
-		/* Only include ;ob parameter if this is the first-hop (that
169
-		   means only one Via:) */
219
+		/* Only include ;ob parameter if this is the first-hop
220
+		 * (that means only one Via:) */
170 221
 		if (parse_via_header(_msg, 2, &via) < 0)
171 222
 			param = PATH_PARAM_OB;
172 223
 	}
... ...
@@ -284,20 +335,77 @@ void path_rr_callback(struct sip_msg *_m, str *r_param, void *cb_param)
284 335
 	param_t *params;
285 336
 	static char dst_uri_buf[MAX_URI_SIZE];
286 337
 	static str dst_uri;
287
-			
338
+	char *p;
339
+	int n;
340
+	int nproto;
341
+	str sproto;
342
+
288 343
 	if (parse_params(r_param, CLASS_CONTACT, &hooks, &params) != 0) {
289 344
 		LM_ERR("failed to parse route parameters\n");
290 345
 		return;
291 346
 	}
292 347
 
293
-	if (hooks.contact.received) {
294
-	        dst_uri.s = dst_uri_buf;
348
+	if (hooks.contact.received
349
+			&& hooks.contact.received->body.len>0) {
350
+		/* 24 => sip:...;transport=sctp */
351
+		if(hooks.contact.received->body.len + 24 >= MAX_URI_SIZE) {
352
+			LM_ERR("received uri is too long\n");
353
+			goto done;
354
+		}
355
+		dst_uri.s = dst_uri_buf;
295 356
 		dst_uri.len = MAX_URI_SIZE;
296
-		if (unescape_user(&(hooks.contact.received->body), &dst_uri) < 0) {
297
-		        LM_ERR("unescaping received failed\n");
298
-			free_params(params);
299
-			return;
300
-		}	    
357
+		if(path_enable_r2==0) {
358
+			if (unescape_user(&(hooks.contact.received->body), &dst_uri) < 0) {
359
+				LM_ERR("unescaping received failed\n");
360
+				free_params(params);
361
+				return;
362
+			} else {
363
+				/* ip~port~proto */
364
+				strncpy(dst_uri_buf, "sip:", 4);
365
+				strncpy(dst_uri_buf+4, hooks.contact.received->body.s,
366
+						hooks.contact.received->body.len);
367
+				dst_uri_buf[hooks.contact.received->body.len] = '\0';
368
+				p = dst_uri_buf + 4;
369
+				n = 0;
370
+				while(*p!='\0') {
371
+					if(*p=='~') {
372
+						n++;
373
+						if(n==1) {
374
+							/* port */
375
+							*p = ':';
376
+						} else if(n==2) {
377
+							/* proto */
378
+							*p = ';';
379
+							p++;
380
+							if(*p=='\0') {
381
+								LM_ERR("invalid received format\n");
382
+								goto done;
383
+							}
384
+							nproto = *p - '0';
385
+							if (nproto != PROTO_UDP) {
386
+								proto_type_to_str(nproto, &sproto);
387
+								if (sproto.len == 0) {
388
+									LM_ERR("unknown proto in received param\n");
389
+									goto done;
390
+								}
391
+								strncpy(p, "transport=", 10);
392
+								p += 10;
393
+								memcpy(p, sproto.s, sproto.len);
394
+								p += sproto.len;
395
+								*p = '\0';
396
+								dst_uri.len = p - dst_uri_buf;
397
+								break;
398
+							}
399
+						} else {
400
+							LM_ERR("invalid number of spearators (%d)\n", n);
401
+							goto done;
402
+						}
403
+					}
404
+					p++;
405
+				}
406
+			}
407
+		}
408
+		LM_DBG("setting dst uri: %.*s\n", dst_uri.len, dst_uri.s);
301 409
 		if (set_dst_uri(_m, &dst_uri) != 0) {
302 410
 			LM_ERR("failed to set dst-uri\n");
303 411
 			free_params(params);
... ...
@@ -307,5 +415,6 @@ void path_rr_callback(struct sip_msg *_m, str *r_param, void *cb_param)
307 415
 			forking */
308 416
 		ruri_mark_new(); /* re-use uri for serial forking */
309 417
 	}
418
+done:
310 419
 	free_params(params);
311 420
 }
Browse code

path: exported functions to kemi interface

Daniel-Constantin Mierla authored on 07/05/2017 06:22:57
Showing 1 changed files
... ...
@@ -151,7 +151,7 @@ out1:
151 151
 /*! \brief
152 152
  * Prepend own uri to Path header
153 153
  */
154
-int add_path(struct sip_msg* _msg, char* _a, char* _b)
154
+int ki_add_path(struct sip_msg* _msg)
155 155
 {
156 156
 	str user = {0,0};
157 157
 	int ret;
... ...
@@ -179,6 +179,11 @@ int add_path(struct sip_msg* _msg, char* _a, char* _b)
179 179
 	return ret;
180 180
 }
181 181
 
182
+int add_path(sip_msg_t* _msg, char* _a, char* _b)
183
+{
184
+	return ki_add_path(_msg);
185
+}
186
+
182 187
 /*! \brief
183 188
  * Prepend own uri to Path header and take care of given
184 189
  * user.
... ...
@@ -196,6 +201,25 @@ int add_path_usr(struct sip_msg* _msg, char* _usr, char* _parms)
196 201
 	return prepend_path(_msg, &user, PATH_PARAM_NONE, &parms);
197 202
 }
198 203
 
204
+/*! \brief
205
+ * Prepend own uri to Path header and take care of given
206
+ * user.
207
+ */
208
+int ki_add_path_user(sip_msg_t* _msg, str* _user)
209
+{
210
+	str parms = {0,0};
211
+	return prepend_path(_msg, _user, PATH_PARAM_NONE, &parms);
212
+}
213
+
214
+/*! \brief
215
+ * Prepend own uri to Path header and take care of given
216
+ * user.
217
+ */
218
+int ki_add_path_user_params(sip_msg_t* _msg, str* _user, str* _params)
219
+{
220
+	return prepend_path(_msg, _user, PATH_PARAM_NONE, _params);
221
+}
222
+
199 223
 /*! \brief
200 224
  * Prepend own uri to Path header and append received address as
201 225
  * "received"-param to that uri.
... ...
@@ -205,6 +229,15 @@ int add_path_received(struct sip_msg* _msg, char* _a, char* _b)
205 229
 	return prepend_path(_msg, NULL, PATH_PARAM_RECEIVED, NULL);
206 230
 }
207 231
 
232
+/*! \brief
233
+ * Prepend own uri to Path header and append received address as
234
+ * "received"-param to that uri.
235
+ */
236
+int ki_add_path_received(sip_msg_t* _msg)
237
+{
238
+	return prepend_path(_msg, NULL, PATH_PARAM_RECEIVED, NULL);
239
+}
240
+
208 241
 /*! \brief
209 242
  * Prepend own uri to Path header and append received address as
210 243
  * "received"-param to that uri and take care of given user.
... ...
@@ -222,6 +255,26 @@ int add_path_received_usr(struct sip_msg* _msg, char* _usr, char* _parms)
222 255
 	return prepend_path(_msg, &user, PATH_PARAM_RECEIVED, &parms);
223 256
 }
224 257
 
258
+/*! \brief
259
+ * Prepend own uri to Path header and append received address as
260
+ * "received"-param to that uri and take care of given user.
261
+ */
262
+int ki_add_path_received_user(sip_msg_t* _msg, str* _user)
263
+{
264
+	str parms = {0,0};
265
+	return prepend_path(_msg, _user, PATH_PARAM_RECEIVED, &parms);
266
+}
267
+
268
+/*! \brief
269
+ * Prepend own uri to Path header and append received address as
270
+ * "received"-param to that uri and take care of given user.
271
+ */
272
+int ki_add_path_received_user_params(sip_msg_t* _msg, str* _user, str* _params)
273
+{
274
+
275
+	return prepend_path(_msg, _user, PATH_PARAM_RECEIVED, _params);
276
+}
277
+
225 278
 /*! \brief
226 279
  * rr callback
227 280
  */
Browse code

core, lib, modules: updated include paths for header files

Daniel-Constantin Mierla authored on 07/12/2016 11:07:22
Showing 1 changed files
... ...
@@ -31,11 +31,11 @@
31 31
 #include <string.h>
32 32
 #include <stdio.h>
33 33
 
34
-#include "../../mem/mem.h"
35
-#include "../../data_lump.h"
36
-#include "../../parser/parse_param.h"
37
-#include "../../strutils.h"
38
-#include "../../dset.h"
34
+#include "../../core/mem/mem.h"
35
+#include "../../core/data_lump.h"
36
+#include "../../core/parser/parse_param.h"