Commit eca24ea7 authored by jehan's avatar jehan

start belle_sip_message_parse_raw returning number of parsed bytes

parent 1884e97a
......@@ -111,11 +111,12 @@ void belle_sip_object_delete(void *obj);
void *belle_sip_object_cast(belle_sip_object_t *obj, belle_sip_type_id_t id, const char *castname, const char *file, int fileno);
unsigned int belle_sip_object_is_instance_of(belle_sip_object_t * obj,belle_sip_type_id_t id);
BELLE_SIP_END_DECLS
#define BELLE_SIP_CAST(obj,_type) ((_type*)belle_sip_object_cast((belle_sip_object_t *)(obj), _type##_id, #_type, __FILE__, __LINE__))
#define BELLE_SIP_OBJECT(obj) BELLE_SIP_CAST(obj,belle_sip_object_t)
#define BELLE_SIP_IS_INSTANCE_OF(obj,_type) belle_sip_object_is_instance_of(obj,_type##_id)
typedef struct belle_sip_listening_point belle_sip_listening_point_t;
......
......@@ -29,7 +29,14 @@ typedef struct _belle_sip_response belle_sip_response_t;
BELLE_SIP_BEGIN_DECLS
belle_sip_message_t* belle_sip_message_parse(const char* raw);
/**
* Parse sip message from a raw buffer
* @param [in] buff buffer to be parsed
* @param [in] buff_length size of the buffer to be parsed
* @param [out] message_length number of bytes read
* @return parsed message
*/
belle_sip_message_t* belle_sip_message_parse_raw (const char* buff, size_t buff_length,size_t* message_length );
int belle_sip_message_is_request(belle_sip_message_t *msg);
belle_sip_request_t* belle_sip_request_new();
......@@ -63,6 +70,11 @@ void belle_sip_message_add_headers(belle_sip_message_t *message, const belle_sip
char *belle_sip_message_to_string(belle_sip_message_t *msg);
int belle_sip_response_get_status_code(const belle_sip_response_t *response);
void belle_sip_response_set_status_code(belle_sip_response_t *response,int status);
const char* belle_sip_response_get_reason_phrase(const belle_sip_response_t *response);
void belle_sip_response_set_reason_phrase(belle_sip_response_t *response,const char* reason_phrase);
belle_sip_response_t *belle_sip_response_new(void);
......
......@@ -29,14 +29,18 @@ options {
message returns [belle_sip_message_t* ret]
message returns [belle_sip_message_t* ret]
scope { size_t message_length; }
: message_raw[&($message::message_length)] {$ret=$message_raw.ret;};
message_raw [size_t* length] returns [belle_sip_message_t* ret]
scope { size_t* message_length; }
@init {$message_raw::message_length=length; }
: request {$ret = BELLE_SIP_MESSAGE($request.ret);}
/*| response*/ ;
| response {$ret = BELLE_SIP_MESSAGE($response.ret);} ;
request returns [belle_sip_request_t* ret]
scope { belle_sip_request_t* current; }
@init {$request::current = belle_sip_request_new(); $ret=$request::current; }
: request_line message_header[BELLE_SIP_MESSAGE($request::current)]+ CRLF /*message_body ?*/ ;
: request_line message_header[BELLE_SIP_MESSAGE($request::current)]+ last_crlf=CRLF {*($message_raw::message_length)=$last_crlf->user1;} /*message_body ?*/ ;
request_line
: method {belle_sip_request_set_method($request::current,(const char*)($method.text->chars));}
......@@ -96,7 +100,7 @@ message_header [belle_sip_message_t* message]
// | warning
// | www_authenticate*/
header_extension[TRUE] {belle_sip_message_add_header(message,BELLE_SIP_HEADER($header_extension.ret));}
) CRLF
) CRLF
;
......@@ -115,12 +119,14 @@ method : /* invitem | ackm | optionm | byem | cancelm | registerm |*/
extension_method
: token;
/*
response
: status_line message-header* CRLF message_body ;
response returns [belle_sip_response_t* ret]
scope { belle_sip_response_t* current; }
@init {$request::current = belle_sip_response_new(); $ret=$request::current; }
: status_line message_header[BELLE_SIP_MESSAGE($request::current)]+ last_crlf=CRLF {*($message_raw::message_length)=0;} /*message_body*/ ;
status_line
: sip_version SP status_code SP reason_phrase CRLF ;
: sip_version LWS status_code LWS reason_phrase CRLF ;
status_code
: extension_code;
......@@ -128,8 +134,8 @@ status_code
extension_code
: DIGIT DIGIT DIGIT;
reason_phrase
: (reserved | unreserved | escaped | utf8_non_ascii | utf8cont | SP | HTAB)*;
: ~(CRLF)*;
/*
utf8cont
:
;
......@@ -810,7 +816,7 @@ mark : '-' | '_' | '.' | '!' | '~' | STAR | '\'' ;
HEX_CHAR: 'a'..'f' |'A'..'F';
DIGIT : '0'..'9' ;
CRLF : '\r\n';
CRLF : '\r\n' { USER1 = GETCHARINDEX();};
......
......@@ -27,7 +27,9 @@ static int has_type(belle_sip_object_t *obj, belle_sip_type_id_t id){
}
return FALSE;
}
unsigned int belle_sip_object_is_instance_of(belle_sip_object_t * obj,belle_sip_type_id_t id) {
return has_type(obj,id);
}
belle_sip_object_t * _belle_sip_object_new(size_t objsize, belle_sip_object_vptr_t *vptr, int initially_unowed){
belle_sip_object_t *obj=(belle_sip_object_t *)belle_sip_malloc0(objsize);
obj->ref=initially_unowed ? 0 : 1;
......
......@@ -50,6 +50,26 @@ BELLE_SIP_INSTANCIATE_VPTR(belle_sip_message_t,belle_sip_object_t,belle_sip_mess
BELLE_SIP_PARSE(message)
belle_sip_message_t* belle_sip_message_parse_raw (const char* buff, size_t buff_length,size_t* message_length ) { \
pANTLR3_INPUT_STREAM input;
pbelle_sip_messageLexer lex;
pANTLR3_COMMON_TOKEN_STREAM tokens;
pbelle_sip_messageParser parser;
input = antlr3NewAsciiStringCopyStream (
(pANTLR3_UINT8)buff,
(ANTLR3_UINT32)buff_length,
((void *)0));
lex = belle_sip_messageLexerNew (input);
tokens = antlr3CommonTokenStreamSourceNew (1025, lex->pLexer->rec->state->tokSource);
parser = belle_sip_messageParserNew (tokens);
belle_sip_message_t* l_parsed_object = parser->message_raw(parser,message_length);
parser ->free(parser);
tokens ->free(tokens);
lex ->free(lex);
input ->close(input);
return l_parsed_object;
}
static int belle_sip_headers_container_comp_func(const headers_container_t *a, const char*b) {
return strcasecmp(a->name,b);
}
......@@ -128,11 +148,11 @@ belle_sip_uri_t * belle_sip_request_get_uri(belle_sip_request_t *request){
}
int belle_sip_message_is_request(belle_sip_message_t *msg){
return 0;
return BELLE_SIP_IS_INSTANCE_OF(BELLE_SIP_OBJECT(msg),belle_sip_request_t);
}
int belle_sip_message_is_response(belle_sip_message_t *msg){
return 0;
return BELLE_SIP_IS_INSTANCE_OF(BELLE_SIP_OBJECT(msg),belle_sip_response_t);
}
belle_sip_header_t *belle_sip_message_get_header(belle_sip_message_t *msg, const char *header_name){
......@@ -235,6 +255,9 @@ static void belle_sip_response_clone(belle_sip_response_t *resp, const belle_sip
}
BELLE_SIP_NEW(response,message);
BELLE_SIP_PARSE(response)
GET_SET_STRING(belle_sip_response,reason_phrase);
GET_SET_INT(belle_sip_response,status_code,int)
static void belle_sip_response_init_default(belle_sip_response_t *resp, int status_code, const char *phrase){
resp->status_code=status_code;
......@@ -257,9 +280,7 @@ belle_sip_response_t *belle_sip_response_new_from_request(belle_sip_request_t *r
return resp;
}
int belle_sip_response_get_status_code(const belle_sip_response_t *response){
return response->status_code;
}
void belle_sip_response_get_return_hop(belle_sip_response_t *msg, belle_sip_hop_t *hop){
belle_sip_header_via_t *via=BELLE_SIP_HEADER_VIA(belle_sip_message_get_header(BELLE_SIP_MESSAGE(msg),"via"));
......
......@@ -93,7 +93,7 @@ static void testInviteMessage(void) {
CU_ASSERT_STRING_EQUAL(belle_sip_request_get_method(request),"INVITE");
check_uri_and_headers(message);
}
static void testByeMessage(void) {
static void testRegisterRaw(void) {
const char* raw_message = "REGISTER sip:192.168.0.20 SIP/2.0\r\n"\
"Via: SIP/2.0/UDP 192.168.1.8:5062;rport;branch=z9hG4bK1439638806\r\n"\
"From: <sip:jehan-mac@sip.linphone.org>;tag=465687829\r\n"\
......@@ -105,7 +105,10 @@ static void testByeMessage(void) {
"User-Agent: Linphone/3.3.99.10 (eXosip2/3.3.0)\r\n"\
"Expires: 3600\r\n"\
"Content-Length: 0\r\n\r\n";
belle_sip_message_t* message = belle_sip_message_parse(raw_message);
size_t size=0;
size_t raw_message_size= strlen(raw_message);
belle_sip_message_t* message = belle_sip_message_parse_raw(raw_message,raw_message_size,&size);
CU_ASSERT_EQUAL(raw_message_size,size);
belle_sip_request_t* request = BELLE_SIP_REQUEST(message);
CU_ASSERT_STRING_EQUAL(belle_sip_request_get_method(request),"REGISTER");
CU_ASSERT_PTR_NOT_NULL(belle_sip_request_get_uri(request));
......@@ -148,6 +151,9 @@ int belle_sip_message_test_suite () {
if (NULL == CU_add_test(pSuite, "test of invite message", testInviteMessage)) {
return CU_get_error();
}
if (NULL == CU_add_test(pSuite, "test of register raw message", testRegisterRaw)) {
return CU_get_error();
}
return CU_get_error();
......
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