From aeb78764822d1cc0fbf701fec80c02f4ac9c663e Mon Sep 17 00:00:00 2001 From: Simon Morlat Date: Fri, 30 Aug 2013 21:29:29 +0200 Subject: [PATCH] transition to belle-sip automatic contact and new nat-helper. --- configure.ac | 2 +- coreapi/bellesip_sal/sal_impl.c | 72 +++------------------- coreapi/bellesip_sal/sal_impl.h | 3 +- coreapi/bellesip_sal/sal_op_impl.c | 2 +- coreapi/bellesip_sal/sal_op_presence.c | 1 - coreapi/bellesip_sal/sal_op_registration.c | 7 +-- coreapi/linphonecore.c | 4 +- include/sal/sal.h | 1 + 8 files changed, 18 insertions(+), 74 deletions(-) diff --git a/configure.ac b/configure.ac index 7954f08c5..c4042828a 100644 --- a/configure.ac +++ b/configure.ac @@ -681,7 +681,7 @@ if test x$enable_msg_storage != xfalse; then fi -PKG_CHECK_MODULES(BELLESIP, [belle-sip]) +PKG_CHECK_MODULES(BELLESIP, [belle-sip >= 1.1.0]) SIPSTACK_CFLAGS="$BELLESIP_CFLAGS" SIPSTACK_LIBS="$BELLESIP_LIBS" diff --git a/coreapi/bellesip_sal/sal_impl.c b/coreapi/bellesip_sal/sal_impl.c index ab943db49..7930a0f04 100644 --- a/coreapi/bellesip_sal/sal_impl.c +++ b/coreapi/bellesip_sal/sal_impl.c @@ -271,15 +271,6 @@ static void process_response_event(void *user_ctx, const belle_sip_response_even } else { SalOp* op = (SalOp*)belle_sip_transaction_get_application_data(BELLE_SIP_TRANSACTION(client_transaction)); belle_sip_request_t* request=belle_sip_transaction_get_request(BELLE_SIP_TRANSACTION(client_transaction)); - belle_sip_header_contact_t* original_contact; - belle_sip_header_address_t* contact_address=NULL; - belle_sip_header_via_t* via_header; - belle_sip_uri_t* contact_uri; - unsigned int contact_port; - const char* received; - int rport; - bool_t contact_updated=FALSE; - char* new_contact; if (op->state == SalOpStateTerminated) { belle_sip_message("Op is terminated, nothing to do with this [%i]",response_code); @@ -295,59 +286,6 @@ static void process_response_event(void *user_ctx, const belle_sip_response_even sal_op_assign_recv_headers(op,(belle_sip_message_t*)response); if (op->callbacks.process_response_event) { - - if (op->base.root->nat_helper_enabled) { - /*Fix contact if needed*/ - via_header= (belle_sip_header_via_t*)belle_sip_message_get_header(BELLE_SIP_MESSAGE(response),BELLE_SIP_VIA); - received = belle_sip_header_via_get_received(via_header); - rport = belle_sip_header_via_get_rport(via_header); - if ((original_contact=belle_sip_message_get_header_by_type(request,belle_sip_header_contact_t))) { - /*update contact with sent values in any cases*/ - contact_address=belle_sip_header_address_create(NULL,belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(original_contact))); - sal_op_set_contact_address(op,(const SalAddress *)contact_address); - belle_sip_object_unref(contact_address); - } - if (sal_op_get_contact(op)){ - if (received!=NULL || rport>0) { - contact_address = BELLE_SIP_HEADER_ADDRESS(sal_address_clone(sal_op_get_contact_address(op))); - contact_uri=belle_sip_header_address_get_uri(BELLE_SIP_HEADER_ADDRESS(contact_address)); - if (received && strcmp(received,belle_sip_uri_get_host(contact_uri))!=0) { - /*need to update host*/ - belle_sip_uri_set_host(contact_uri,received); - contact_updated=TRUE; - } - contact_port = belle_sip_uri_get_port(contact_uri); - if (rport>0 && rport!=contact_port && (contact_port+rport)!=5060) { - /*need to update port*/ - belle_sip_uri_set_port(contact_uri,rport); - contact_updated=TRUE; - } - - /*try to fix transport if needed (very unlikely)*/ - if (strcasecmp(belle_sip_header_via_get_transport(via_header),"UDP")!=0) { - if (!belle_sip_uri_get_transport_param(contact_uri) - ||strcasecmp(belle_sip_uri_get_transport_param(contact_uri),belle_sip_header_via_get_transport(via_header))!=0) { - belle_sip_uri_set_transport_param(contact_uri,belle_sip_header_via_get_transport_lowercase(via_header)); - contact_updated=TRUE; - } - } else { - if (belle_sip_uri_get_transport_param(contact_uri)) { - contact_updated=TRUE; - belle_sip_uri_set_transport_param(contact_uri,NULL); - } - } - if (contact_updated) { - char* old_contact=belle_sip_object_to_string(BELLE_SIP_OBJECT(sal_op_get_contact_address(op))); - new_contact=belle_sip_object_to_string(BELLE_SIP_OBJECT(contact_address)); - ms_message("Updating contact from [%s] to [%s] for [%p]",old_contact,new_contact,op); - sal_op_set_contact_address(op,(const SalAddress *)contact_address); - belle_sip_free(new_contact); - belle_sip_free(old_contact); - } - if (contact_address)belle_sip_object_unref(contact_address); - } - } - } /*handle authorization*/ switch (response_code) { @@ -422,7 +360,7 @@ static void process_auth_requested(void *sal, belle_sip_auth_event_t *auth_event Sal * sal_init(){ belle_sip_listener_callbacks_t listener_callbacks; Sal * sal=ms_new0(Sal,1); - sal->nat_helper_enabled=TRUE; + sal->auto_contacts=TRUE; sal->user_agent=belle_sip_header_user_agent_new(); #if defined(PACKAGE_NAME) && defined(LINPHONE_VERSION) belle_sip_header_user_agent_add_product(sal->user_agent, PACKAGE_NAME "/" LINPHONE_VERSION); @@ -432,6 +370,7 @@ Sal * sal_init(){ belle_sip_set_log_handler(_belle_sip_log); sal->stack = belle_sip_stack_new(NULL); sal->prov = belle_sip_stack_create_provider(sal->stack,NULL); + sal_nat_helper_enable(sal,TRUE); memset(&listener_callbacks,0,sizeof(listener_callbacks)); listener_callbacks.process_dialog_terminated=process_dialog_terminated; listener_callbacks.process_io_error=process_io_error; @@ -727,6 +666,7 @@ void sal_set_recv_error(Sal *sal,int value) { } void sal_nat_helper_enable(Sal *sal,bool_t enable) { sal->nat_helper_enabled=enable; + belle_sip_provider_enable_nat_helper(sal->prov,enable); ms_message("Sal nat helper [%s]",enable?"enabled":"disabled"); } bool_t sal_nat_helper_enabled(Sal *sal) { @@ -878,9 +818,15 @@ belle_sip_response_t* sal_create_response_from_request ( Sal* sal, belle_sip_req belle_sip_message_add_header(BELLE_SIP_MESSAGE(resp),BELLE_SIP_HEADER(sal->user_agent)); return resp; } + void sal_set_refresher_retry_after(Sal *sal,int value) { sal->refresher_retry_after=value; } + int sal_get_refresher_retry_after(const Sal *sal) { return sal->refresher_retry_after; } + +void sal_enable_auto_contacts(Sal *ctx, bool_t enabled){ + ctx->auto_contacts=enabled; +} diff --git a/coreapi/bellesip_sal/sal_impl.h b/coreapi/bellesip_sal/sal_impl.h index 3989ee54a..096aa0598 100644 --- a/coreapi/bellesip_sal/sal_impl.h +++ b/coreapi/bellesip_sal/sal_impl.h @@ -38,13 +38,14 @@ struct Sal{ unsigned int keep_alive; char *root_ca; char *uuid; + int refresher_retry_after; /*retry after value for refresher*/ bool_t one_matching_codec; bool_t use_tcp_tls_keep_alive; bool_t nat_helper_enabled; bool_t tls_verify; bool_t tls_verify_cn; bool_t use_dates; - int refresher_retry_after; /*retry after value for refresher*/ + bool_t auto_contacts; }; typedef enum SalOpState { diff --git a/coreapi/bellesip_sal/sal_op_impl.c b/coreapi/bellesip_sal/sal_op_impl.c index 54a3b56dd..29aa88729 100644 --- a/coreapi/bellesip_sal/sal_op_impl.c +++ b/coreapi/bellesip_sal/sal_op_impl.c @@ -92,6 +92,7 @@ belle_sip_header_contact_t* sal_op_create_contact(SalOp *op){ } else { contact_header= belle_sip_header_contact_new(); } + belle_sip_header_contact_set_automatic(contact_header,op->base.root->auto_contacts); if (op->base.root->uuid){ if (belle_sip_parameters_has_parameter(BELLE_SIP_PARAMETERS(contact_header),"+sip.instance")==0){ char *instance_id=belle_sip_strdup_printf("\"\"",op->base.root->uuid); @@ -463,7 +464,6 @@ int sal_op_send_and_create_refresher(SalOp* op,belle_sip_request_t* req, int exp belle_sip_object_unref(op->refresher); } if ((op->refresher = belle_sip_client_transaction_create_refresher(op->pending_client_trans))) { - belle_sip_refresher_enable_nat_helper(op->refresher,op->base.root->nat_helper_enabled); belle_sip_refresher_set_listener(op->refresher,listener,op); belle_sip_refresher_set_retry_after(op->refresher,op->base.root->refresher_retry_after); return 0; diff --git a/coreapi/bellesip_sal/sal_op_presence.c b/coreapi/bellesip_sal/sal_op_presence.c index 24ea3aefc..ca6b86cab 100644 --- a/coreapi/bellesip_sal/sal_op_presence.c +++ b/coreapi/bellesip_sal/sal_op_presence.c @@ -125,7 +125,6 @@ static void presence_response_event(void *op_base, const belle_sip_response_even } if (expires>0){ op->refresher=belle_sip_client_transaction_create_refresher(client_transaction); - belle_sip_refresher_enable_nat_helper(op->refresher,op->base.root->nat_helper_enabled); belle_sip_refresher_set_listener(op->refresher,presence_refresher_listener,op); } } diff --git a/coreapi/bellesip_sal/sal_op_registration.c b/coreapi/bellesip_sal/sal_op_registration.c index 11a503bdd..cfdedc9e5 100644 --- a/coreapi/bellesip_sal/sal_op_registration.c +++ b/coreapi/bellesip_sal/sal_op_registration.c @@ -28,12 +28,7 @@ static void register_refresher_listener ( const belle_sip_refresher_t* refresher SalReason sal_reason; belle_sip_response_t* response=belle_sip_transaction_get_response(BELLE_SIP_TRANSACTION(belle_sip_refresher_get_transaction(refresher))); ms_message("Register refresher [%i] reason [%s] for proxy [%s]",status_code,reason_phrase,sal_op_get_proxy(op)); - /*fix contact if needed*/ - if (op->base.root->nat_helper_enabled && belle_sip_refresher_get_nated_contact(refresher)) { - belle_sip_header_address_t* contact_address = BELLE_SIP_HEADER_ADDRESS(belle_sip_object_clone(BELLE_SIP_OBJECT(belle_sip_refresher_get_nated_contact(refresher)))); - sal_op_set_contact_address(op,(SalAddress*)contact_address); - belle_sip_object_unref(contact_address); - } + if (belle_sip_refresher_get_auth_events(refresher)) { if (op->auth_info) sal_auth_info_delete(op->auth_info); /*only take first one for now*/ diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c index 6cb08c276..eaebda6fe 100644 --- a/coreapi/linphonecore.c +++ b/coreapi/linphonecore.c @@ -4484,10 +4484,12 @@ void linphone_core_set_firewall_policy(LinphoneCore *lc, LinphoneFirewallPolicy switch(pol) { case LinphonePolicyUseUpnp: sal_nat_helper_enable(lc->sal, FALSE); + sal_enable_auto_contacts(lc->sal,FALSE); sal_use_rport(lc->sal, FALSE); - break; + break; default: sal_nat_helper_enable(lc->sal, lp_config_get_int(lc->config,"net","enable_nat_helper",1)); + sal_enable_auto_contacts(lc->sal,TRUE); sal_use_rport(lc->sal, lp_config_get_int(lc->config,"sip","use_rport",1)); break; } diff --git a/include/sal/sal.h b/include/sal/sal.h index 427234f57..239bd949c 100644 --- a/include/sal/sal.h +++ b/include/sal/sal.h @@ -443,6 +443,7 @@ void sal_reuse_authorization(Sal *ctx, bool_t enabled); void sal_use_one_matching_codec_policy(Sal *ctx, bool_t one_matching_codec); void sal_use_rport(Sal *ctx, bool_t use_rports); void sal_use_101(Sal *ctx, bool_t use_101); +void sal_enable_auto_contacts(Sal *ctx, bool_t enabled); void sal_set_root_ca(Sal* ctx, const char* rootCa); const char *sal_get_root_ca(Sal* ctx); void sal_verify_server_certificates(Sal *ctx, bool_t verify);