Browse code

sr_module: new fixup function: get_is_fparam() - retrieve parameter value as string or/and integer (if possible)

Ovidiu Sas authored on 20/05/2014 17:45:40
Showing 2 changed files
... ...
@@ -1776,6 +1776,100 @@ int get_int_fparam(int* dst, struct sip_msg* msg, fparam_t* param)
1776 1776
 	return 0;
1777 1777
 }
1778 1778
 
1779
+/** Get the function parameter value as string or/and integer (if possible).
1780
+ *  @return  0 - Success
1781
+ *          -1 - Cannot get value
1782
+ */
1783
+int get_is_fparam(int* i_dst, str* s_dst, struct sip_msg* msg, fparam_t* param, unsigned int *flags)
1784
+{
1785
+	int_str val;
1786
+	int ret;
1787
+	avp_t* avp;
1788
+	str tmp;
1789
+	pv_value_t pv_val;
1790
+
1791
+	*flags = 0;
1792
+	switch(param->type) {
1793
+		case FPARAM_INT:
1794
+			*i_dst = param->v.i;
1795
+			*flags |= PARAM_INT;
1796
+			return 0;
1797
+		case FPARAM_REGEX:
1798
+		case FPARAM_UNSPEC:
1799
+		case FPARAM_STRING:
1800
+			s_dst->s = param->v.asciiz;
1801
+			s_dst->len = strlen(param->v.asciiz);
1802
+			*flags |= PARAM_STR;
1803
+			break;
1804
+		case FPARAM_STR:
1805
+			*s_dst = param->v.str;
1806
+			*flags |= PARAM_STR;
1807
+			break;
1808
+		case FPARAM_AVP:
1809
+			avp = search_first_avp(param->v.avp.flags, param->v.avp.name,
1810
+									&val, 0);
1811
+			if (unlikely(!avp)) {
1812
+				DBG("Could not find AVP from function parameter '%s'\n",
1813
+						param->orig);
1814
+				return -1;
1815
+			}
1816
+			if (avp->flags & AVP_VAL_STR) {
1817
+				*s_dst = val.s;
1818
+				*flags |= PARAM_STR;
1819
+				if (str2int(&val.s, (unsigned int*)i_dst) < 0) {
1820
+					ERR("Could not convert AVP string value to int\n");
1821
+					return -1;
1822
+				}
1823
+			} else {
1824
+				*i_dst = val.n;
1825
+				*flags |= PARAM_INT;
1826
+			}
1827
+			break;
1828
+		case FPARAM_SELECT:
1829
+			ret = run_select(&tmp, param->v.select, msg);
1830
+			if (unlikely(ret < 0 || ret > 0)) return -1;
1831
+			if (unlikely(str2int(&tmp, (unsigned int*)i_dst) < 0)) {
1832
+				ERR("Could not convert select result to int\n");
1833
+				return -1;
1834
+			}
1835
+			*flags |= PARAM_INT;
1836
+			break;
1837
+		case FPARAM_PVS:
1838
+			if (likely(pv_get_spec_value(msg, param->v.pvs, &pv_val)==0)) {
1839
+				if ((pv_val.flags&(PV_VAL_NULL|PV_VAL_INT))==PV_VAL_INT){
1840
+					*i_dst=pv_val.ri;
1841
+					*flags |= PARAM_INT;
1842
+				}
1843
+				if ((pv_val.flags&(PV_VAL_NULL|PV_VAL_STR))==PV_VAL_STR){
1844
+					*s_dst=pv_val.rs;
1845
+					*flags |= PARAM_STR;
1846
+				}
1847
+			}else{
1848
+				ERR("Could not get PV\n");
1849
+				return -1;
1850
+			}
1851
+			break;
1852
+		case FPARAM_PVE:
1853
+			s_dst->s=pv_get_buffer();
1854
+			s_dst->len=pv_get_buffer_size();
1855
+			if (unlikely(pv_printf(msg, param->v.pve, s_dst->s, &s_dst->len)!=0)){
1856
+				ERR("Could not convert the PV-formated string to str\n");
1857
+				s_dst->len=0;
1858
+				return -1;
1859
+			}
1860
+			*flags |= PARAM_STR;
1861
+			break;
1862
+	}
1863
+
1864
+	/* Let's convert to int, if possible */
1865
+	if (!(*flags & PARAM_INT) && (*flags & PARAM_STR) && str2sint(s_dst, i_dst) == 0)
1866
+		*flags |= PARAM_INT;
1867
+
1868
+	if (!*flags) return -1;
1869
+
1870
+	return 0;
1871
+}
1872
+
1779 1873
 /**
1780 1874
  * Retrieve the compiled RegExp.
1781 1875
  * @return: 0 for success, negative on error.
... ...
@@ -630,6 +630,17 @@ int get_str_fparam(str* dst, struct sip_msg* msg, fparam_t* param);
630 630
 int get_int_fparam(int* dst, struct sip_msg* msg, fparam_t* param);
631 631
 
632 632
 /**
633
+ * @brief Get the function parameter value as integer/string
634
+ * @param i_dst int destination
635
+ * @param s_dst string destination
636
+ * @param msg SIP message
637
+ * @param param function parameters
638
+ * @param flags flags to indicate destiantions
639
+ * @return 0 on success, 1 on error, e.g. cannot get value
640
+ */
641
+int get_is_fparam(int* i_dst, str* s_dst, struct sip_msg* msg, fparam_t* param, unsigned int *flags);
642
+
643
+/**
633 644
  * @brief Get the function parameter value as compiled regular expression
634 645
  * @param dst string destination
635 646
  * @param msg SIP message