Browse code

vp: new variable $via0(attr)

- get the attributes of first Via

Daniel-Constantin Mierla authored on 02/07/2021 14:18:55
Showing 3 changed files
... ...
@@ -546,6 +546,8 @@ static pv_export_t mod_pvs[] = {
546 546
 		pv_parse_rpl_attrs_name, 0, 0, 0},
547 547
 	{{"ccp", (sizeof("ccp")-1)}, PVT_OTHER, pv_get_ccp_attrs, pv_set_ccp_attrs,
548 548
 		pv_parse_ccp_attrs_name, 0, 0, 0},
549
+	{{"via0", (sizeof("via0")-1)}, PVT_OTHER, pv_get_via0, 0,
550
+		pv_parse_via_name, 0, 0, 0},
549 551
 
550 552
 	{ {0, 0}, 0, 0, 0, 0, 0, 0, 0 }
551 553
 };
... ...
@@ -4473,3 +4473,124 @@ int pv_set_ccp_attrs(struct sip_msg* msg, pv_param_t *param,
4473 4473
 	}
4474 4474
 	return 0;
4475 4475
 }
4476
+
4477
+/**
4478
+ *
4479
+ */
4480
+int pv_parse_via_name(pv_spec_p sp, str *in)
4481
+{
4482
+	if(sp==NULL || in==NULL || in->len<=0)
4483
+		return -1;
4484
+
4485
+	/* attributes not related to dst of reply get an id starting with 20 */
4486
+	switch(in->len) {
4487
+		case 1:
4488
+			if(in->s[0]=='i')
4489
+				sp->pvp.pvn.u.isname.name.n = 7;
4490
+			else goto error;
4491
+		case 4:
4492
+			if(strncmp(in->s, "host", 4)==0)
4493
+				sp->pvp.pvn.u.isname.name.n = 0;
4494
+			else if(strncmp(in->s, "port", 4)==0)
4495
+				sp->pvp.pvn.u.isname.name.n = 1;
4496
+			else goto error;
4497
+		break;
4498
+		case 5:
4499
+			if(strncmp(in->s, "proto", 5)==0)
4500
+				sp->pvp.pvn.u.isname.name.n = 2;
4501
+			else if(strncmp(in->s, "rport", 5)==0)
4502
+				sp->pvp.pvn.u.isname.name.n = 5;
4503
+			else goto error;
4504
+		break;
4505
+		case 6:
4506
+			if(strncmp(in->s, "branch", 6)==0)
4507
+				sp->pvp.pvn.u.isname.name.n = 4;
4508
+			else goto error;
4509
+		break;
4510
+		case 7:
4511
+			if(strncmp(in->s, "protoid", 7)==0)
4512
+				sp->pvp.pvn.u.isname.name.n = 3;
4513
+			else goto error;
4514
+		case 8:
4515
+			if(strncmp(in->s, "received", 8)==0)
4516
+				sp->pvp.pvn.u.isname.name.n = 6;
4517
+			else goto error;
4518
+		break;
4519
+
4520
+		default:
4521
+			goto error;
4522
+	}
4523
+	sp->pvp.pvn.type = PV_NAME_INTSTR;
4524
+	sp->pvp.pvn.u.isname.type = 0;
4525
+
4526
+	return 0;
4527
+
4528
+error:
4529
+	LM_ERR("unknown PV rpl key: %.*s\n", in->len, in->s);
4530
+	return -1;
4531
+}
4532
+
4533
+/**
4534
+ *
4535
+ */
4536
+int pv_get_via_attr(sip_msg_t *msg, via_body_t *vb, pv_param_t *param,
4537
+		pv_value_t *res)
4538
+{
4539
+	if(vb==NULL) {
4540
+		LM_DBG("invalid via header\n");
4541
+		return pv_get_null(msg, param, res);
4542
+	}
4543
+
4544
+	switch(param->pvn.u.isname.name.n) {
4545
+		case 0: /* host */
4546
+			if(vb->host.s!=NULL && vb->host.len>0) {
4547
+				return pv_get_strval(msg, param, res, &vb->host);
4548
+			}
4549
+			break;
4550
+		case 1: /* port */
4551
+			return pv_get_uintval(msg, param, res, (vb->port)?vb->port:SIP_PORT);
4552
+		case 2: /* proto */
4553
+			return pv_get_strval(msg, param, res, &vb->transport);
4554
+		case 3: /* protoid */
4555
+			return pv_get_sintval(msg, param, res, (int)vb->proto);
4556
+		case 4: /* branch */
4557
+			if(vb->branch!=NULL && vb->branch->value.len>0) {
4558
+				return pv_get_strval(msg, param, res, &vb->branch->value);
4559
+			}
4560
+			break;
4561
+		case 5: /* rport */
4562
+			if(vb->rport!=NULL && vb->rport->value.len>0) {
4563
+				return pv_get_strval(msg, param, res, &vb->rport->value);
4564
+			}
4565
+			break;
4566
+		case 6: /* received */
4567
+			if(vb->received!=NULL && vb->received->value.len>0) {
4568
+				return pv_get_strval(msg, param, res, &vb->received->value);
4569
+			}
4570
+			break;
4571
+		case 7: /* i */
4572
+			if(vb->i!=NULL && vb->i->value.len>0) {
4573
+				return pv_get_strval(msg, param, res, &vb->i->value);
4574
+			}
4575
+			break;
4576
+
4577
+		default:
4578
+			return pv_get_null(msg, param, res);
4579
+	}
4580
+
4581
+	return pv_get_null(msg, param, res);
4582
+}
4583
+
4584
+
4585
+/**
4586
+ *
4587
+ */
4588
+int pv_get_via0(sip_msg_t *msg, pv_param_t *param, pv_value_t *res)
4589
+{
4590
+	if (parse_headers(msg, HDR_EOH_F, 0)<0) {
4591
+		LM_DBG("failed to parse sip headers\n");
4592
+		return pv_get_null(msg, param, res);
4593
+	}
4594
+
4595
+	return pv_get_via_attr(msg, msg->via1, param, res);
4596
+}
... ...
@@ -411,5 +411,8 @@ int pv_get_ccp_attrs(sip_msg_t *msg, pv_param_t *param,
411 411
 int pv_set_ccp_attrs(struct sip_msg* msg, pv_param_t *param,
412 412
 		int op, pv_value_t *val);
413 413
 
414
+int pv_parse_via_name(pv_spec_p sp, str *in);
415
+int pv_get_via0(sip_msg_t *msg, pv_param_t *param, pv_value_t *res);
416
+
414 417
 #endif
415 418