Skip to content

Commit 4e80c74

Browse files
Aleksei Vasilevvasilevalex
authored andcommitted
[uac_registrant] Match reply Contact with binding_URI by parameter.
Added new integer module parameter 'match_contact' to choose, what parts of URI from Contact header should match binding_URI when OpenSIPS chooses expiration time for registration.
1 parent 41a3230 commit 4e80c74

File tree

3 files changed

+107
-2
lines changed

3 files changed

+107
-2
lines changed

modules/uac_registrant/registrant.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ uac_auth_api_t uac_auth_api;
110110

111111
unsigned int default_expires = 3600;
112112
unsigned int timer_interval = 100;
113+
unsigned int match_contact = URI_MATCH_ALL;
113114

114115
reg_table_t reg_htable = NULL;
115116
unsigned int reg_hsize = 1;
@@ -139,6 +140,7 @@ static param_export_t params[]= {
139140
{"hash_size", INT_PARAM, &reg_hsize},
140141
{"default_expires", INT_PARAM, &default_expires},
141142
{"timer_interval", INT_PARAM, &timer_interval},
143+
{"match_contact", INT_PARAM, &match_contact},
142144
{"enable_clustering", INT_PARAM, &enable_clustering},
143145
{"db_url", STR_PARAM, &db_url.s},
144146
{"table_name", STR_PARAM, &reg_table_name.s},
@@ -481,8 +483,7 @@ int run_reg_tm_cback(void *e_data, void *data, void *r_data)
481483
contact = ((contact_body_t*)msg->contact->parsed)->contacts;
482484
while (contact) {
483485
/* Check for binding */
484-
if (contact->uri.len==rec->contact_uri.len &&
485-
strncmp(contact->uri.s,rec->contact_uri.s,contact->uri.len)==0){
486+
if (compare_uris_parts(&contact->uri, &rec->contact_uri, (enum uri_match_flags)match_contact) == 0) {
486487
if (contact->expires && contact->expires->body.len) {
487488
if (str2int(&contact->expires->body, &exp)<0) {
488489
LM_ERR("Unable to extract expires from [%.*s]"

parser/parse_uri.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,3 +1846,89 @@ int compare_uris(str *raw_uri_a,struct sip_uri* parsed_uri_a,
18461846
compare_uri_val(headers,strncasecmp);
18471847
return 0;
18481848
}
1849+
1850+
/* Compare 2 SIP URIs by parts according to opts
1851+
*
1852+
* Return value : 0 if URIs match
1853+
* 1 if URIs don't match
1854+
* -1 if errors have occurred
1855+
*/
1856+
int compare_uris_parts(str *raw_uri_a, str *raw_uri_b, enum uri_match_flags opts)
1857+
{
1858+
#define UNESCAPED_BUF_LEN 1024
1859+
char unescaped_a[UNESCAPED_BUF_LEN], unescaped_b[UNESCAPED_BUF_LEN];
1860+
1861+
str unescaped_userA={unescaped_a, UNESCAPED_BUF_LEN};
1862+
str unescaped_userB={unescaped_b, UNESCAPED_BUF_LEN};
1863+
1864+
struct sip_uri first;
1865+
struct sip_uri second;
1866+
1867+
if ( (!raw_uri_a) || (!raw_uri_b) )
1868+
{
1869+
LM_ERR("Provide either a raw form of a SIP URI\n");
1870+
return -1;
1871+
}
1872+
1873+
/* maybe we're lucky and straight-forward comparison succeeds */
1874+
if ((opts & URI_MATCH_ALL) && (raw_uri_a->len == raw_uri_b->len))
1875+
if (strncasecmp(raw_uri_a->s,raw_uri_b->s,raw_uri_a->len) == 0)
1876+
{
1877+
LM_DBG("straight-forward URI match\n");
1878+
return 0;
1879+
}
1880+
1881+
if (parse_uri(raw_uri_a->s,raw_uri_a->len,&first) < 0)
1882+
{
1883+
LM_ERR("Failed to parse first URI\n");
1884+
return -1;
1885+
}
1886+
1887+
if (parse_uri(raw_uri_b->s,raw_uri_b->len,&second) < 0)
1888+
{
1889+
LM_ERR("Failed to parse second URI\n");
1890+
return -1;
1891+
}
1892+
1893+
if (opts & URI_MATCH_TYPE)
1894+
if (first.type != second.type)
1895+
{
1896+
LM_DBG("Different uri types\n");
1897+
return 1;
1898+
}
1899+
1900+
if (unescape_user(&first.user, &unescaped_userA) < 0 ||
1901+
unescape_user(&second.user, &unescaped_userB) < 0) {
1902+
LM_ERR("Failed to unescape user!\n");
1903+
return -1;
1904+
}
1905+
1906+
first.user = unescaped_userA;
1907+
second.user = unescaped_userB;
1908+
1909+
if (opts & URI_MATCH_USER)
1910+
compare_uri_val(user,strncmp);
1911+
if (opts & URI_MATCH_PASSWD)
1912+
compare_uri_val(passwd,strncmp);
1913+
if (opts & URI_MATCH_HOST)
1914+
compare_uri_val(host,strncasecmp);
1915+
if (opts & URI_MATCH_PORT)
1916+
compare_uri_val(port,strncmp);
1917+
1918+
if (opts & URI_MATCH_TRANSPORT)
1919+
compare_uri_val(transport_val,strncasecmp);
1920+
if (opts & URI_MATCH_TTL)
1921+
compare_uri_val(ttl_val,strncasecmp);
1922+
if (opts & URI_MATCH_USERPARAM)
1923+
compare_uri_val(user_param_val,strncasecmp);
1924+
if (opts & URI_MATCH_MADDR)
1925+
compare_uri_val(maddr_val,strncasecmp);
1926+
if (opts & URI_MATCH_METHOD)
1927+
compare_uri_val(method_val,strncasecmp);
1928+
if (opts & URI_MATCH_LR)
1929+
compare_uri_val(lr_val,strncasecmp);
1930+
if (opts & URI_MATCH_R2)
1931+
compare_uri_val(r2_val,strncasecmp);
1932+
1933+
return 0;
1934+
}

parser/parse_uri.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@
4242
#define URN_NENA_SERVICE_STR ":nena:service:"
4343
#define URN_NENA_SERVICE_STR_LEN (sizeof(URN_NENA_SERVICE_STR) - 1)
4444

45+
enum uri_match_flags {
46+
URI_MATCH_TYPE = (1<<0),
47+
URI_MATCH_USER = (1<<1),
48+
URI_MATCH_PASSWD = (1<<2),
49+
URI_MATCH_HOST = (1<<3),
50+
URI_MATCH_PORT = (1<<4),
51+
URI_MATCH_TRANSPORT = (1<<5),
52+
URI_MATCH_TTL = (1<<6),
53+
URI_MATCH_USERPARAM = (1<<7),
54+
URI_MATCH_MADDR = (1<<8),
55+
URI_MATCH_METHOD = (1<<9),
56+
URI_MATCH_LR = (1<<10),
57+
URI_MATCH_R2 = (1<<11),
58+
URI_MATCH_ALL = (URI_MATCH_TYPE | URI_MATCH_USER | URI_MATCH_PASSWD | URI_MATCH_HOST | URI_MATCH_PORT | URI_MATCH_TRANSPORT |
59+
URI_MATCH_TTL | URI_MATCH_USERPARAM | URI_MATCH_MADDR | URI_MATCH_METHOD | URI_MATCH_LR | URI_MATCH_R2),
60+
};
61+
4562
/* buf= pointer to beginning of uri (sip:[email protected]:5060;a=b?h=i)
4663
* len= len of uri
4764
* returns: fills uri & returns <0 on error or 0 if ok
@@ -69,6 +86,7 @@ int parse_sip_msg_uri(struct sip_msg* msg);
6986
int parse_orig_ruri(struct sip_msg* msg);
7087
int compare_uris(str *raw_uri_a,struct sip_uri* parsed_uri_a,
7188
str *raw_uri_b,struct sip_uri *parsed_uri_b);
89+
int compare_uris_parts(str *raw_uri_a, str *raw_uri_b, enum uri_match_flags opts);
7290
static inline int get_uri_param_val(const struct sip_uri *uri,
7391
const str *param, str *val);
7492
static inline int get_uri_param_idx(const str *param,

0 commit comments

Comments
 (0)