Skip to content

Commit

Permalink
Fix evaluating the SIP port from a SIP URI.
Browse files Browse the repository at this point in the history
Use the new function get_uri_port() to evaluate the port and proto, in a SIP wise manner, form a SIP URI. If port/proto are not explicitly set in the URI, consider the default port for the used transpor proto. If protocol misses, we assume the default protos according to the URI schema.

NOTE: it is completly bogus (and dangerous) to assue 5061 if SIPS schema is used as:
1) SIPS can use used with WSS, which actually has 443 port
2) TLS can pe required via transport param in a SIP schema URI too
  • Loading branch information
bogdan-iancu committed Jun 8, 2016
1 parent 39e0deb commit 4c54bcd
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
11 changes: 6 additions & 5 deletions modules/dispatcher/dispatch.c
Expand Up @@ -998,6 +998,7 @@ static inline int get_uri_hash_keys(str* key1, str* key2,
str* uri, struct sip_uri* parsed_uri, int flags)
{
struct sip_uri tmp_p_uri; /* used only if parsed_uri==0 */
unsigned short proto;

if (parsed_uri==0)
{
Expand All @@ -1016,8 +1017,8 @@ static inline int get_uri_hash_keys(str* key1, str* key2,
goto error;
}

/* we want: user@host:port if port !=5060
* user@host if port==5060
/* we want: user@host:port if port is not the defaut one
* user@host if port is the default one
* user if the user flag is set*/
*key1=parsed_uri->user;
key2->s=0;
Expand All @@ -1028,9 +1029,9 @@ static inline int get_uri_hash_keys(str* key1, str* key2,
/* add port if needed */
if (parsed_uri->port.s!=0)
{ /* uri has a port */
/* skip port if == 5060 or sips and == 5061 */
if (parsed_uri->port_no !=
((parsed_uri->type==SIPS_URI_T)?SIPS_PORT:SIP_PORT))
/* skip port if the default one ( first extract proto from URI) */
if ( get_uri_port(parsed_uri, &proto) &&
parsed_uri->port_no != protos[proto].default_port )
key2->len+=parsed_uri->port.len+1 /* ':' */;
}
}
Expand Down
5 changes: 2 additions & 3 deletions modules/nathelper/nathelper.c
Expand Up @@ -898,11 +898,10 @@ contact_rport(struct sip_msg* msg)
struct sip_uri uri;
contact_t* c;
struct hdr_field *hdr;
int ct_port;

for( hdr=NULL,c=NULL ; get_contact_uri(msg, &uri, &c, &hdr)==0 ; ) {
ct_port=uri.port_no?uri.port_no:((uri.type==SIPS_URI_T)?SIPS_PORT:SIP_PORT);
if ( msg->rcv.src_port != ct_port ) return 1;
if ( msg->rcv.src_port != get_uri_port( &uri, NULL) )
return 1;
}

return 0;
Expand Down
11 changes: 1 addition & 10 deletions modules/rr/loose.c
Expand Up @@ -233,16 +233,7 @@ static inline int is_myself(struct sip_uri* _uri)
unsigned short port;
unsigned short proto;

/* known protocol? */
if ((proto=_uri->proto)==PROTO_NONE) {
/* use UDP as default proto, but TLS for secure schemas */
proto = (_uri->type==SIPS_URI_T || _uri->type==TELS_URI_T)?
PROTO_TLS : PROTO_UDP ;
}

/* known port? */
if ((port=_uri->port_no)==0)
port = protos[proto].default_port;
port = get_uri_port(_uri, &proto);

ret = check_self(&_uri->host, port, proto);
if (ret < 0) return 0;
Expand Down
27 changes: 27 additions & 0 deletions parser/parse_uri.h
Expand Up @@ -29,6 +29,7 @@


#include "../str.h"
#include "../net/trans.h"
#include "../parser/msg_parser.h"

/* buf= pointer to beginning of uri (sip:x@foo.bar:5060;a=b?h=i)
Expand All @@ -49,4 +50,30 @@ char * uri_type2str(const uri_type type, char *result);
int uri_typestrlen(const uri_type type);
uri_type str2uri_type(char * buf);

/* Gets (in a SIP wise manner) the SIP port from a SIP URI ; if the port
is not explicitly set in the URI, it returns the default port corresponding
to the user transport protocol (if protocol misses, we assume the default
protos according to the URI schema) */
static inline unsigned short get_uri_port(struct sip_uri* _uri,
unsigned short *_proto)
{
unsigned short port;
unsigned short proto;

/* known protocol? */
if ((proto=_uri->proto)==PROTO_NONE) {
/* use UDP as default proto, but TLS for secure schemas */
proto = (_uri->type==SIPS_URI_T || _uri->type==TELS_URI_T)?
PROTO_TLS : PROTO_UDP ;
}

/* known port? */
if ((port=_uri->port_no)==0)
port = protos[proto].default_port;

if (_proto) *_proto = proto;

return port;
}

#endif /* PARSE_URI_H */

0 comments on commit 4c54bcd

Please sign in to comment.