Commit 10122c8b authored by jehan's avatar jehan
Browse files

uri parser first complete implementation

parent 16e30ac6
......@@ -3,9 +3,12 @@ grammar belle_sip_uri;
options {
language = C;
}
}
@header { #include "belle_sip_uri.h"}
to_header : 'TO' ':' an_sip_uri;
an_sip_uri returns [belle_sip_uri* ret]
scope { belle_sip_uri* current; }
@init { $an_sip_uri::current = belle_sip_uri_new(); }
......@@ -33,37 +36,31 @@ port : DIGIT+ {belle_sip_uri_set_port($an_sip_uri::current,atoi((const char *)$t
uri_parameters
: ( ';' uri_parameter)+;
uri_parameter
: {strcmp("transport=",(const char*)(INPUT->toStringTT(INPUT,LT(1),LT(10)))->chars) == 0}? transport_param
| {strcmp("user=",(const char*)(INPUT->toStringTT(INPUT,LT(1),LT(5)))->chars) == 0}? user_param
| {strcmp("ttl=",(const char*)(INPUT->toStringTT(INPUT,LT(1),LT(4)))->chars) == 0}? ttl_param
| {strcmp("maddr=",(const char*)(INPUT->toStringTT(INPUT,LT(1),LT(6)))->chars) == 0}? maddr_param
| {strcmp("lr",(const char*)(INPUT->toStringTT(INPUT,LT(1),LT(2)))->chars) == 0}? lr_param
| other_param ;
transport_param
: pname '=' transport_value;
transport_value: token
{belle_sip_uri_set_transport_param($an_sip_uri::current,(const char *)$text->chars);};
user_param
: other_param;
ttl_param
: pname '=' ttl;
maddr_param
: pname '=' host {belle_sip_uri_set_maddr_param($an_sip_uri::current,(const char *)$host.text->chars);};
lr_param
: alpha alpha {belle_sip_uri_set_lr_param($an_sip_uri::current,1);};
other_param : pname ( '=' pvalue )?;
: ( ';' uri_parameter )+;
uri_parameter //all parameters are considered as other
: other_param ;
other_param : pname ( '=' pvalue )?
{
if (strcmp("lr",$pname.text->chars) == 0) {
belle_sip_uri_set_lr_param($an_sip_uri::current,1);
} else if (strcmp("transport",$pname.text->chars)==0) {
belle_sip_uri_set_transport_param($an_sip_uri::current,(const char *)$pvalue.text->chars);
} else if (strcmp("user",$pname.text->chars)==0) {
belle_sip_uri_set_user_param($an_sip_uri::current,(const char *)$pvalue.text->chars);
} else if (strcmp("maddr",$pname.text->chars)==0) {
belle_sip_uri_set_maddr_param($an_sip_uri::current,(const char *)$pvalue.text->chars);
} else if (strcmp("ttl",$pname.text->chars)==0) {
belle_sip_uri_set_ttl_param($an_sip_uri::current,atoi((const char *)$pvalue.text->chars));
}
};
pname
: paramchar+;
pvalue
: paramchar+;
paramchar
: param_unreserved | unreserved | escaped;
: param_unreserved | unreserved | escaped;
param_unreserved
: '[' | ']' | '/' | ':' | '&' | '+' | '$';
: '[' | ']' | '/' | ':' | '&' | '+' | '$' | '.';
headers : '?' header ( '&' header )* ;
header : hname '=' hvalue? {belle_sip_uri_set_header($an_sip_uri::current,$hname.text->chars,$hvalue.text->chars);};
......
......@@ -25,15 +25,16 @@
#define SIP_URI_GET_SET_STRING(attribute) GET_SET_STRING(belle_sip_uri,attribute)
#define GET_SET_UINT(object_type,attribute) \
unsigned int object_type##_get_##attribute (object_type* obj) {\
#define GET_SET_INT(object_type,attribute,type) \
type object_type##_get_##attribute (object_type* obj) {\
return obj->attribute;\
}\
void object_type##_set_##attribute (object_type* obj,unsigned int value) {\
void object_type##_set_##attribute (object_type* obj,type value) {\
obj->attribute=value;\
}
#define SIP_URI_GET_SET_UINT(attribute) GET_SET_UINT(belle_sip_uri,attribute)
#define SIP_URI_GET_SET_UINT(attribute) GET_SET_INT(belle_sip_uri,attribute,unsigned int)
#define SIP_URI_GET_SET_INT(attribute) GET_SET_INT(belle_sip_uri,attribute,int)
#define GET_SET_BOOL(object_type,attribute,getter) \
unsigned int object_type##_##getter##_##attribute (object_type* obj) {\
......@@ -43,6 +44,7 @@
obj->attribute=value;\
}
#define SIP_URI_GET_SET_BOOL(attribute) GET_SET_BOOL(belle_sip_uri,attribute,is)
#define SIP_URI_HAS_SET_BOOL(attribute) GET_SET_BOOL(belle_sip_uri,attribute,has)
......@@ -50,21 +52,30 @@
struct _belle_sip_uri {
unsigned int secure;
const char* user;
const char* host;
const char* transport_param;
unsigned int port;
unsigned int secure;
const char* transport_param;
const char* user_param;
const char* method_param;
unsigned int lr_param;
const char* maddr_param;
int ttl_param;
belle_sip_list* header_list;
belle_sip_list* headernames_list;
};
void belle_sip_uri_delete(belle_sip_uri* uri) {
if (uri->user) free (uri->user);
if (uri->host) free (uri->host);
if (uri->transport_param) free (uri->transport_param);
if (uri->user_param) free (uri->user_param);
if (uri->method_param) free (uri->method_param);
if (uri->maddr_param) free (uri->maddr_param);
if (uri->header_list) belle_sip_list_free (uri->header_list);
if (uri->headernames_list) belle_sip_list_free (uri->headernames_list);
free(uri);
......@@ -116,6 +127,7 @@ belle_sip_uri* belle_sip_uri_parse (const char* uri) {
belle_sip_uri* belle_sip_uri_new () {
belle_sip_uri* lUri = (belle_sip_uri*)malloc(sizeof(belle_sip_uri));
memset(lUri,0,sizeof(belle_sip_uri));
lUri->ttl_param==-1;
return lUri;
}
......@@ -166,11 +178,15 @@ belle_sip_list* belle_sip_uri_get_header_names(belle_sip_uri* uri) {
SIP_URI_GET_SET_BOOL(secure)
SIP_URI_GET_SET_STRING(user)
SIP_URI_GET_SET_STRING(host)
SIP_URI_GET_SET_UINT(port)
SIP_URI_GET_SET_STRING(transport_param)
SIP_URI_GET_SET_STRING(user_param)
SIP_URI_GET_SET_STRING(method_param)
SIP_URI_GET_SET_STRING(maddr_param)
SIP_URI_GET_SET_UINT(port)
SIP_URI_GET_SET_BOOL(secure)
SIP_URI_GET_SET_INT(ttl_param)
SIP_URI_HAS_SET_BOOL(lr_param)
......@@ -61,6 +61,19 @@ void test_maddr(void) {
belle_sip_uri_delete(L_uri);
}
void test_uri_parameters () {
belle_sip_uri* L_uri = belle_sip_uri_parse("sip:192.168.0.1;ttl=12");
belle_sip_uri_delete(L_uri);
L_uri = belle_sip_uri_parse("sip:maddr=@192.168.0.1;lr;maddr=192.168.0.1;user=ip;ttl=140;transport=sctp;method=INVITE;rport=5060");
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_maddr_param(L_uri), "192.168.0.1");
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_user_param(L_uri), "ip");
CU_ASSERT_EQUAL(belle_sip_uri_get_ttl_param(L_uri),160);
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_transport_param(L_uri), "sctp");
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_method_param(L_uri), "INVITE");
belle_sip_uri_delete(L_uri);
}
void test_headers(void) {
belle_sip_uri* L_uri = belle_sip_uri_parse("sip:192.168.0.1?toto=titi");
CU_ASSERT_PTR_NOT_NULL_FATAL(belle_sip_uri_get_header(L_uri,"toto"));
......@@ -100,7 +113,7 @@ int main (int argc, char *argv[]) {
|| (NULL == CU_add_test(pSuite, "test of lr uri", test_lr))
|| (NULL == CU_add_test(pSuite, "test of maddr uri", test_maddr))
|| (NULL == CU_add_test(pSuite, "test of headers", test_headers))
|| (NULL == CU_add_test(pSuite, "test of uri parameters", test_uri_parameters))
|| (NULL == CU_add_test(pSuite, "test of sips uri", testSIPSURI)))
{
CU_cleanup_registry();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment