@@ -53,6 +53,52 @@ static const str uri_type_names[7] = {
53
53
str_init ("urn:nena:service" )
54
54
};
55
55
56
+ struct uri_match_part uri_match_parts [] = {
57
+ { str_const_init ("type" ), URI_MATCH_TYPE },
58
+ { str_const_init ("user" ), URI_MATCH_USER },
59
+ { str_const_init ("password" ), URI_MATCH_PASSWD },
60
+ { str_const_init ("host" ), URI_MATCH_HOST },
61
+ { str_const_init ("port" ), URI_MATCH_PORT },
62
+ { str_const_init ("transport" ), URI_MATCH_TRANSPORT },
63
+ { str_const_init ("ttl" ), URI_MATCH_TTL },
64
+ { str_const_init ("userparam" ), URI_MATCH_USERPARAM },
65
+ { str_const_init ("maddr" ), URI_MATCH_MADDR },
66
+ { str_const_init ("method" ), URI_MATCH_METHOD },
67
+ { str_const_init ("lr" ), URI_MATCH_LR },
68
+ { str_const_init ("r2" ), URI_MATCH_R2 },
69
+ { str_const_init ("all" ), URI_MATCH_ALL },
70
+ };
71
+
72
+ enum uri_match_flags parse_uri_options (str * options_str )
73
+ {
74
+ enum uri_match_flags flags = URI_MATCH_NONE ;
75
+ char * p , * e ;
76
+ str * s = options_str ;
77
+ int i ;
78
+
79
+ if (options_str == NULL || options_str -> s == NULL || options_str -> len == 0 ) {
80
+ return flags ;
81
+ }
82
+ e = s -> s + strlen (s -> s );
83
+ while (s -> s < e ) {
84
+ p = strchr (s -> s , ';' );
85
+ s -> len = (p ? (p - s -> s ) : strlen (s -> s ));
86
+ str_trim_spaces_lr (* s );
87
+ /* check if this is among the possible options */
88
+ for (i = 0 ; i < (sizeof (uri_match_parts )/sizeof (uri_match_parts [0 ])); i ++ ) {
89
+ if (s -> len == uri_match_parts [i ].name .len &&
90
+ strncasecmp (s -> s , uri_match_parts [i ].name .s ,
91
+ uri_match_parts [i ].name .len ) == 0 )
92
+ flags |= uri_match_parts [i ].flag ;
93
+ }
94
+ if (p )
95
+ s -> s = p + 1 ;
96
+ else
97
+ break ;
98
+ }
99
+ return flags ;
100
+ }
101
+
56
102
char * uri_type2str (const uri_type type , char * result )
57
103
{
58
104
if (type == ERROR_URI_T )
@@ -1855,3 +1901,89 @@ int compare_uris(str *raw_uri_a,struct sip_uri* parsed_uri_a,
1855
1901
compare_uri_val (headers ,strncasecmp );
1856
1902
return 0 ;
1857
1903
}
1904
+
1905
+ /* Compare 2 SIP URIs by parts according to opts
1906
+ *
1907
+ * Return value : 0 if URIs match
1908
+ * 1 if URIs don't match
1909
+ * -1 if errors have occurred
1910
+ */
1911
+ int compare_uris_parts (str * raw_uri_a , str * raw_uri_b , enum uri_match_flags opts )
1912
+ {
1913
+ #define UNESCAPED_BUF_LEN 1024
1914
+ char unescaped_a [UNESCAPED_BUF_LEN ], unescaped_b [UNESCAPED_BUF_LEN ];
1915
+
1916
+ str unescaped_userA = {unescaped_a , UNESCAPED_BUF_LEN };
1917
+ str unescaped_userB = {unescaped_b , UNESCAPED_BUF_LEN };
1918
+
1919
+ struct sip_uri first ;
1920
+ struct sip_uri second ;
1921
+
1922
+ if ( (!raw_uri_a ) || (!raw_uri_b ) )
1923
+ {
1924
+ LM_ERR ("Provide either a raw form of a SIP URI\n" );
1925
+ return -1 ;
1926
+ }
1927
+
1928
+ /* maybe we're lucky and straight-forward comparison succeeds */
1929
+ if ((opts & URI_MATCH_ALL ) && (raw_uri_a -> len == raw_uri_b -> len ))
1930
+ if (strncasecmp (raw_uri_a -> s ,raw_uri_b -> s ,raw_uri_a -> len ) == 0 )
1931
+ {
1932
+ LM_DBG ("straight-forward URI match\n" );
1933
+ return 0 ;
1934
+ }
1935
+
1936
+ if (parse_uri (raw_uri_a -> s ,raw_uri_a -> len ,& first ) < 0 )
1937
+ {
1938
+ LM_ERR ("Failed to parse first URI\n" );
1939
+ return -1 ;
1940
+ }
1941
+
1942
+ if (parse_uri (raw_uri_b -> s ,raw_uri_b -> len ,& second ) < 0 )
1943
+ {
1944
+ LM_ERR ("Failed to parse second URI\n" );
1945
+ return -1 ;
1946
+ }
1947
+
1948
+ if (opts & URI_MATCH_TYPE )
1949
+ if (first .type != second .type )
1950
+ {
1951
+ LM_DBG ("Different uri types\n" );
1952
+ return 1 ;
1953
+ }
1954
+
1955
+ if (unescape_user (& first .user , & unescaped_userA ) < 0 ||
1956
+ unescape_user (& second .user , & unescaped_userB ) < 0 ) {
1957
+ LM_ERR ("Failed to unescape user!\n" );
1958
+ return -1 ;
1959
+ }
1960
+
1961
+ first .user = unescaped_userA ;
1962
+ second .user = unescaped_userB ;
1963
+
1964
+ if (opts & URI_MATCH_USER )
1965
+ compare_uri_val (user ,strncmp );
1966
+ if (opts & URI_MATCH_PASSWD )
1967
+ compare_uri_val (passwd ,strncmp );
1968
+ if (opts & URI_MATCH_HOST )
1969
+ compare_uri_val (host ,strncasecmp );
1970
+ if (opts & URI_MATCH_PORT )
1971
+ compare_uri_val (port ,strncmp );
1972
+
1973
+ if (opts & URI_MATCH_TRANSPORT )
1974
+ compare_uri_val (transport_val ,strncasecmp );
1975
+ if (opts & URI_MATCH_TTL )
1976
+ compare_uri_val (ttl_val ,strncasecmp );
1977
+ if (opts & URI_MATCH_USERPARAM )
1978
+ compare_uri_val (user_param_val ,strncasecmp );
1979
+ if (opts & URI_MATCH_MADDR )
1980
+ compare_uri_val (maddr_val ,strncasecmp );
1981
+ if (opts & URI_MATCH_METHOD )
1982
+ compare_uri_val (method_val ,strncasecmp );
1983
+ if (opts & URI_MATCH_LR )
1984
+ compare_uri_val (lr_val ,strncasecmp );
1985
+ if (opts & URI_MATCH_R2 )
1986
+ compare_uri_val (r2_val ,strncasecmp );
1987
+
1988
+ return 0 ;
1989
+ }
0 commit comments