Browse code

dialog: dlg_get_var(ci, ft, tt, key, dst_var)

Victor Seva authored on 06/05/2022 11:30:01 • Daniel-Constantin Mierla committed on 17/05/2022 10:34:59
Showing 2 changed files
... ...
@@ -194,7 +194,9 @@ static int w_is_known_dlg(struct sip_msg *);
194 194
 static int w_dlg_set_ruri(sip_msg_t *, char *, char *);
195 195
 static int w_dlg_db_load_callid(sip_msg_t *msg, char *ci, char *p2);
196 196
 static int w_dlg_db_load_extra(sip_msg_t *msg, char *p1, char *p2);
197
-
197
+static int fixup_dlg_get_var(void** param, int param_no);
198
+static int fixup_dlg_get_var_free(void** param, int param_no);
199
+static int w_dlg_get_var(struct sip_msg*, char*, char*, char*, char*, char*);
198 200
 static int w_dlg_remote_profile(sip_msg_t *msg, char *cmd, char *pname,
199 201
 		char *pval, char *puid, char *expires);
200 202
 static int fixup_dlg_remote_profile(void** param, int param_no);
... ...
@@ -256,6 +258,8 @@ static cmd_export_t cmds[]={
256 258
 			0, ANY_ROUTE },
257 259
 	{"dlg_db_load_extra", (cmd_function)w_dlg_db_load_extra, 0, 0,
258 260
 			0, ANY_ROUTE },
261
+	{"dlg_get_var",(cmd_function)w_dlg_get_var, 5, fixup_dlg_get_var,
262
+			fixup_dlg_get_var_free, ANY_ROUTE },
259 263
 
260 264
 	{"load_dlg",  (cmd_function)load_dlg,   0, 0, 0, 0},
261 265
 	{0,0,0,0,0,0}
... ...
@@ -1502,6 +1506,108 @@ static int fixup_dlg_bridge(void** param, int param_no)
1502 1506
 	return 0;
1503 1507
 }
1504 1508
 
1509
+static str *ki_dlg_get_var(sip_msg_t *msg, str *sc, str *sf, str *st, str *key)
1510
+{
1511
+	dlg_cell_t *dlg = NULL;
1512
+	unsigned int dir = 0;
1513
+	str *val = NULL;
1514
+
1515
+	if(sc==NULL || sc->s==NULL || sc->len == 0) {
1516
+		LM_ERR("invalid Call-ID parameter\n");
1517
+		return val;
1518
+	}
1519
+	if(sf==NULL || sf->s==NULL || sf->len == 0) {
1520
+		LM_ERR("invalid From tag parameter\n");
1521
+		return val;
1522
+	}
1523
+	if(st==NULL || st->s==NULL || st->len == 0) {
1524
+		LM_ERR("invalid To tag parameter\n");
1525
+		return val;
1526
+	}
1527
+
1528
+	dlg = get_dlg(sc, sf, st, &dir);
1529
+	if(dlg==NULL)
1530
+		return val;
1531
+	val = get_dlg_variable(dlg, key);
1532
+	dlg_release(dlg);
1533
+	return val;
1534
+}
1535
+
1536
+static int w_dlg_get_var(struct sip_msg *msg, char *ci, char *ft, char *tt, char *key, char *pv)
1537
+{
1538
+	str sc = STR_NULL;
1539
+	str sf = STR_NULL;
1540
+	str st = STR_NULL;
1541
+	str k = STR_NULL;
1542
+	str *val = NULL;
1543
+	pv_value_t dst_val;
1544
+	pv_spec_t* dst_pv;
1545
+
1546
+	if(ci==0 || ft==0 || tt==0)
1547
+	{
1548
+		LM_ERR("invalid parameters\n");
1549
+		return -1;
1550
+	}
1551
+
1552
+	if(fixup_get_svalue(msg, (gparam_p)ci, &sc)!=0)
1553
+	{
1554
+		LM_ERR("unable to get Call-ID\n");
1555
+		return -1;
1556
+	}
1557
+
1558
+	if(fixup_get_svalue(msg, (gparam_p)ft, &sf)!=0)
1559
+	{
1560
+		LM_ERR("unable to get From tag\n");
1561
+		return -1;
1562
+	}
1563
+
1564
+	if(fixup_get_svalue(msg, (gparam_p)tt, &st)!=0)
1565
+	{
1566
+		LM_ERR("unable to get To Tag\n");
1567
+		return -1;
1568
+	}
1569
+	if(st.s==NULL || st.len == 0)
1570
+	{
1571
+		LM_ERR("invalid To tag parameter\n");
1572
+		return -1;
1573
+	}
1574
+	if(fixup_get_svalue(msg, (gparam_p)key, &k)!=0)
1575
+	{
1576
+		LM_ERR("unable to get key name\n");
1577
+		return -1;
1578
+	}
1579
+	dst_pv = (pv_spec_t *)pv;
1580
+	val = ki_dlg_get_var(msg, &sc, &sf, &st, &k);
1581
+	if(val) {
1582
+		memset(&dst_val, 0, sizeof(pv_value_t));
1583
+		dst_val.flags |= PV_VAL_STR;
1584
+		dst_val.rs.s = val->s;
1585
+		dst_val.rs.len = val->len;
1586
+		if(pv_set_spec_value(msg, dst_pv, 0, &dst_val) != 0) return -1;
1587
+	} else {
1588
+		if(pv_get_null(msg, NULL, &dst_val) != 0) return -1;
1589
+	}
1590
+	return 1;
1591
+}
1592
+
1593
+static int fixup_dlg_get_var(void** param, int param_no)
1594
+{
1595
+	if(param_no>=1 && param_no<=4)
1596
+		return fixup_spve_null(param, 1);
1597
+	if(param_no==5)
1598
+		return fixup_pvar_all(param, 1);
1599
+	return 0;
1600
+}
1601
+
1602
+static int fixup_dlg_get_var_free(void** param, int param_no)
1603
+{
1604
+	if (param_no <= 4)
1605
+		return fixup_free_spve_null(param, 1);
1606
+	if (param_no == 5)
1607
+		return fixup_free_pvar_all(param, 1);
1608
+	return -1;
1609
+}
1610
+
1505 1611
 static int ki_dlg_get(sip_msg_t *msg, str *sc, str *sf, str *st)
1506 1612
 {
1507 1613
 	dlg_cell_t *dlg = NULL;
... ...
@@ -2087,6 +2193,11 @@ static sr_kemi_t sr_kemi_dialog_exports[] = {
2087 2193
 		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
2088 2194
 			SR_KEMIP_NONE, SR_KEMIP_NONE, SR_KEMIP_NONE }
2089 2195
 	},
2196
+	{ str_init("dialog"), str_init("dlg_get_var"),
2197
+		SR_KEMIP_STR, ki_dlg_get_var,
2198
+		{ SR_KEMIP_STR, SR_KEMIP_STR, SR_KEMIP_STR,
2199
+			SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE }
2200
+	},
2090 2201
 	{ str_init("dialog"), str_init("set_dlg_profile_static"),
2091 2202
 		SR_KEMIP_INT, ki_set_dlg_profile_static,
2092 2203
 		{ SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE,
... ...
@@ -2103,6 +2103,54 @@ if(dlg_get("abcdef", "123", "456"))
2103 2103
 		</example>
2104 2104
 	</section>
2105 2105
 
2106
+	<section id="dialog.f.dlg_get_var">
2107
+		<title>
2108
+		<function moreinfo="none">dlg_get_var(callid, ftag, ttag, name, dst)</function>
2109
+		</title>
2110
+		<para>
2111
+			Get dlg_var of another dialog value based on Call-ID, From-Tag and To-Tag
2112
+			parameters.
2113
+		</para>
2114
+		<para>Meaning of the parameters is as follows:</para>
2115
+		<itemizedlist>
2116
+		<listitem>
2117
+			<para><emphasis>callid</emphasis> - SIP call-id.
2118
+			</para>
2119
+		</listitem>
2120
+		<listitem>
2121
+			<para><emphasis>ftag</emphasis> - SIP From tag.
2122
+			</para>
2123
+		</listitem>
2124
+		<listitem>
2125
+			<para><emphasis>ttag</emphasis> - SIP To tag.
2126
+			</para>
2127
+		</listitem>
2128
+		<listitem>
2129
+			<para><emphasis>name</emphasis> - key name of the $dlg_var.
2130
+			</para>
2131
+		</listitem>
2132
+		<listitem>
2133
+			<para><emphasis>dst</emphasis> - pv to store the value of $dlg_var(name).
2134
+			</para>
2135
+		</listitem>
2136
+		</itemizedlist>
2137
+		<para>
2138
+		This function can be used from BRANCH_ROUTE,
2139
+			REQUEST_ROUTE, ONREPLY_ROUTE and FAILURE_ROUTE.
2140
+		</para>
2141
+		<example>
2142
+		<title><function>dlg_get_var</function> usage</title>
2143
+		<programlisting format="linespecific">
2144
+...
2145
+if(dlg_get_var("$var(ci)", "$var(ft)", "456", "test", "$var(tmp)"))
2146
+{
2147
+	xdbg("$$dlg_var(test):$var(tmp)\n");
2148
+}
2149
+...
2150
+</programlisting>
2151
+		</example>
2152
+	</section>
2153
+
2106 2154
 	<section id="dialog.f.is_known_dlg">
2107 2155
 		<title>
2108 2156
 		<function moreinfo="none">is_known_dlg()</function>