Commit 5fb30729 authored by jehan's avatar jehan

implement sdp helpers

parent d2ea243c
......@@ -291,6 +291,13 @@ void belle_sdp_version_set_version(belle_sdp_version_t* version, int value);
typedef struct _belle_sdp_session_description belle_sdp_session_description_t;
belle_sdp_session_description_t* belle_sdp_session_description_new();
belle_sdp_session_description_t* belle_sdp_session_description_parse (const char* session_description);
/*sdp managementy helper method*/
/*
* create a session description object from a message
* @param message to get sdp from
* @returns belle_sdp_session_description_t or NULL if no sdp present
* */
belle_sdp_session_description_t* belle_sdp_session_description_create(belle_sip_message_t* message);
const char* belle_sdp_session_description_get_attribute(const belle_sdp_session_description_t* session_description, const char* name);
int belle_sdp_session_description_get_bandwidth(const belle_sdp_session_description_t* session_description, const char* name);
belle_sip_list_t* belle_sdp_session_description_get_bandwidths(const belle_sdp_session_description_t* session_description);
......
......@@ -31,6 +31,8 @@ typedef enum belle_sip_dialog_state belle_sip_dialog_state_t;
BELLE_SIP_BEGIN_DECLS
const char* belle_sip_dialog_state_to_string(const belle_sip_dialog_state_t state);
belle_sip_request_t *belle_sip_dialog_create_ack(belle_sip_dialog_t *dialog, unsigned int cseq);
belle_sip_request_t *belle_sip_dialog_create_request(belle_sip_dialog_t *dialog, const char *method);
......
......@@ -310,6 +310,14 @@ belle_sip_header_expires_t* belle_sip_header_expires_create(int expires);
belle_sip_header_user_agent_t* belle_sip_header_user_agent_parse (const char* user_agent) ;
belle_sip_list_t* belle_sip_header_user_agent_get_products(const belle_sip_header_user_agent_t* user_agent);
/**
* concatenates products
* @param user_agent [in] user agent header
* @param value [out]buffer where to put result in
* @param value_size [in] size of the buffer
* @return number of written characters or -1 inca se of error;
*/
int belle_sip_header_user_agent_get_products_as_string(const belle_sip_header_user_agent_t* user_agent,char* value,unsigned int value_size);
void belle_sip_header_user_agent_set_products(belle_sip_header_user_agent_t* user_agent,belle_sip_list_t* value);
void belle_sip_header_user_agent_add_product(belle_sip_header_user_agent_t* user_agent,const char* product);
#define BELLE_SIP_HEADER_USER_AGENT(t) BELLE_SIP_CAST(t,belle_sip_header_user_agent_t)
......
......@@ -56,9 +56,11 @@ void belle_sip_request_set_uri(belle_sip_request_t* request, belle_sip_uri_t* ur
const char* belle_sip_request_get_method(const belle_sip_request_t* request);
void belle_sip_request_set_method(belle_sip_request_t* request,const char* method);
/**
* Guess the origin
* Guess the origin of the received sip message from VIA header (thanks to received/rport)
* @param req request to be annylized
* @ return a newly allocated uri
* */
belle_sip_uri_t* belle_sip_request_extract_origin(belle_sip_request_t* req);
belle_sip_uri_t* belle_sip_request_extract_origin(const belle_sip_request_t* req);
int belle_sip_message_is_response(const belle_sip_message_t *msg);
......@@ -106,6 +108,7 @@ belle_sip_response_t *belle_sip_response_new(void);
belle_sip_response_t *belle_sip_response_create_from_request(belle_sip_request_t *req, int status_code);
BELLE_SIP_END_DECLS
#endif
......
......@@ -15,7 +15,7 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "belle-sip/belle-sdp.h"
#include "belle-sip/belle-sip.h"
#include "belle_sdpParser.h"
#include "belle_sdpLexer.h"
#include "belle_sip_internal.h"
......@@ -871,6 +871,15 @@ int belle_sdp_session_description_marshal(belle_sdp_session_description_t* sessi
BELLE_SDP_NEW(session_description,belle_sdp_base_description)
BELLE_SDP_PARSE(session_description)
belle_sdp_session_description_t* belle_sdp_session_description_create(belle_sip_message_t* message) {
belle_sdp_session_description_t* session_desc=NULL;
belle_sip_header_content_type_t* content_type=belle_sip_message_get_header_by_type(message,belle_sip_header_content_type_t);
if (strcmp("application",belle_sip_header_content_type_get_type(content_type))==0
&& strcmp("sdp",belle_sip_header_content_type_get_subtype(content_type))==0) {
session_desc=belle_sdp_session_description_parse(belle_sip_message_get_body(message));
}
return session_desc;
}
const char* belle_sdp_session_description_get_attribute(const belle_sdp_session_description_t* session_description, const char* name) {
return belle_sdp_base_description_get_attribute(BELLE_SIP_CAST(session_description,belle_sdp_base_description_t),name);
}
......
......@@ -397,6 +397,19 @@ void belle_sip_header_user_agent_set_products(belle_sip_header_user_agent_t* use
void belle_sip_header_user_agent_add_product(belle_sip_header_user_agent_t* user_agent,const char* product) {
user_agent->products = belle_sip_list_append(user_agent->products ,belle_sip_strdup(product));
}
int belle_sip_header_user_agent_get_products_as_string(const belle_sip_header_user_agent_t* user_agent,char* value,unsigned int value_size) {
int result = 0;
belle_sip_list_t* list = user_agent->products;
for(;list!=NULL;list=list->next){
result+=snprintf(value+result
,value_size-result
,"%s "
,(const char *)list->data);
}
if (result>0) value[result]='\0'; /*remove last space */
return result-1;
}
/**************************
* Via header object inherits from parameters
......
......@@ -331,7 +331,7 @@ char *belle_sip_strdup_printf(const char *fmt,...);
type object_type##_get_##func_name (const object_type##_t* obj) {\
const char* l_value = belle_sip_parameters_get_parameter(BELLE_SIP_PARAMETERS(obj),#attribute);\
if (l_value == NULL) { \
belle_sip_error("cannot find parameters [%s]",#attribute);\
/*belle_sip_error("cannot find parameters [%s]",#attribute);*/\
return -1;\
}\
return ATO_(type,l_value);\
......
......@@ -47,6 +47,15 @@ BELLE_SIP_INSTANCIATE_CUSTOM_VPTR(belle_sip_dialog_t)={
NULL,
NULL
};
const char* belle_sip_dialog_state_to_string(const belle_sip_dialog_state_t state) {
switch(state) {
case BELLE_SIP_DIALOG_NULL: return "BELLE_SIP_DIALOG_NULL";
case BELLE_SIP_DIALOG_EARLY: return "BELLE_SIP_DIALOG_EARLY";
case BELLE_SIP_DIALOG_CONFIRMED: return "BELLE_SIP_DIALOG_CONFIRMED";
case BELLE_SIP_DIALOG_TERMINATED: return "BELLE_SIP_DIALOG_TERMINATED";
default: return "Unknown state";
}
}
static void set_to_tag(belle_sip_dialog_t *obj, belle_sip_header_to_t *to){
const char *to_tag=belle_sip_header_to_get_tag(to);
......
......@@ -280,6 +280,27 @@ belle_sip_uri_t * belle_sip_request_get_uri(belle_sip_request_t *request){
return request->uri;
}
belle_sip_uri_t* belle_sip_request_extract_origin(const belle_sip_request_t* req) {
belle_sip_header_via_t* via_header = belle_sip_message_get_header_by_type(req,belle_sip_header_via_t);
belle_sip_uri_t* uri=NULL;
const char* received = belle_sip_header_via_get_received(via_header);
int rport = belle_sip_header_via_get_rport(via_header);
uri = belle_sip_uri_new();
if (received!=NULL) {
belle_sip_uri_set_host(uri,received);
} else {
belle_sip_uri_set_host(uri,belle_sip_header_via_get_host(via_header));
}
if (rport>0) {
belle_sip_uri_set_port(uri,rport);
} else if (belle_sip_header_via_get_port(via_header)) {
belle_sip_uri_set_port(uri,belle_sip_header_via_get_port(via_header));
}
if (belle_sip_header_via_get_transport(via_header)) {
belle_sip_uri_set_transport_param(uri,belle_sip_header_via_get_transport(via_header));
}
return uri;
}
int belle_sip_message_is_request(belle_sip_message_t *msg){
return BELLE_SIP_IS_INSTANCE_OF(BELLE_SIP_OBJECT(msg),belle_sip_request_t);
}
......
......@@ -186,14 +186,65 @@ static void testOptionMessage(void) {
belle_sip_object_unref(message);
}
static void test_extract_source() {
const char * invite_1="INVITE sip:jehan@81.56.113.2:50343;transport=tcp;line=f18e0009dd6cc43 SIP/2.0\r\n"
"Via: SIP/2.0/TCP 37.59.129.73;branch=z9hG4bK.SKvK9U327e8mU68XUv5rt144pg\r\n"
"Via: SIP/2.0/UDP 192.168.1.12:15060;rport=15060;branch=z9hG4bK1596944937;received=81.56.113.2\r\n"
"Record-Route: <sip:37.59.129.73;lr;transport=tcp>\r\n"
"Record-Route: <sip:37.59.129.73;lr>\r\n"
"Max-Forwards: 70\r\n"
"From: <sip:jehan@sip.linphone.org>;tag=711138653\r\n"
"To: <sip:jehan@sip.linphone.org>\r\n"
"Call-ID: 977107319\r\n"
"CSeq: 21 INVITE\r\n"
"Contact: <sip:jehan@81.56.113.2:15060>\r\n"
"Subject: Phone call\r\n"
"User-Agent: Linphone/3.5.2 (eXosip2/3.6.0)\r\n"
"Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO\r\n"
"Content-Length: 0\r\n\r\n";
const char * invite_2="INVITE sip:jehan@81.56.113.2:50343;transport=tcp;line=f18e0009dd6cc43 SIP/2.0\r\n"
"Via: SIP/2.0/UDP 192.168.1.12:15060;rport=15060;branch=z9hG4bK1596944937;received=81.56.113.2\r\n"
"Via: SIP/2.0/TCP 37.59.129.73;branch=z9hG4bK.SKvK9U327e8mU68XUv5rt144pg\r\n"
"Record-Route: <sip:37.59.129.73;lr;transport=tcp>\r\n"
"Record-Route: <sip:37.59.129.73;lr>\r\n"
"Max-Forwards: 70\r\n"
"From: <sip:jehan@sip.linphone.org>;tag=711138653\r\n"
"To: <sip:jehan@sip.linphone.org>\r\n"
"Call-ID: 977107319\r\n"
"CSeq: 21 INVITE\r\n"
"Contact: <sip:jehan@81.56.113.2:15060>\r\n"
"Subject: Phone call\r\n"
"User-Agent: Linphone/3.5.2 (eXosip2/3.6.0)\r\n"
"Allow: INVITE, ACK, CANCEL, OPTIONS, BYE, REFER, NOTIFY, MESSAGE, SUBSCRIBE, INFO\r\n"
"Content-Length: 0\r\n\r\n";
belle_sip_message_t* message = belle_sip_message_parse(invite_1);
belle_sip_request_t* request = BELLE_SIP_REQUEST(message);
belle_sip_uri_t* source =belle_sip_request_extract_origin(request);
CU_ASSERT_PTR_NOT_NULL(source);
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(source),"37.59.129.73");
CU_ASSERT_EQUAL(belle_sip_uri_get_port(source),0);
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_transport_param(source),"TCP");
belle_sip_object_unref(message);
message = belle_sip_message_parse(invite_2);
request = BELLE_SIP_REQUEST(message);
source =belle_sip_request_extract_origin(request);
CU_ASSERT_PTR_NOT_NULL(source);
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_host(source),"81.56.113.2");
CU_ASSERT_EQUAL(belle_sip_uri_get_port(source),15060);
CU_ASSERT_STRING_EQUAL(belle_sip_uri_get_transport_param(source),"UDP");
belle_sip_object_unref(message);
}
int belle_sip_message_test_suite () {
CU_pSuite pSuite = NULL;
/* add a suite to the registry */
pSuite = CU_add_suite("message suite", init_suite_message, clean_suite_message);
pSuite = CU_add_suite("Message", init_suite_message, clean_suite_message);
if (NULL == pSuite) {
return CU_get_error();
}
......@@ -216,7 +267,9 @@ int belle_sip_message_test_suite () {
if (NULL == CU_add_test(pSuite, "test of 401 response", test401Response)) {
return CU_get_error();
}
if (NULL == CU_add_test(pSuite, "test belle_sip_request_extract_origin", test_extract_source)) {
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