From 73722ac3ee8819f6915f7f7a987ece3940f520b5 Mon Sep 17 00:00:00 2001 From: Jehan Monnier Date: Mon, 25 Feb 2013 11:24:56 +0100 Subject: [PATCH] add function to disable contact fixing --- coreapi/bellesip_sal/sal_impl.c | 131 +++++++++++++++++--------------- coreapi/bellesip_sal/sal_impl.h | 1 + coreapi/linphonecore.c | 4 +- coreapi/sal.h | 3 + 4 files changed, 78 insertions(+), 61 deletions(-) diff --git a/coreapi/bellesip_sal/sal_impl.c b/coreapi/bellesip_sal/sal_impl.c index 754c0ce46..c7ced7384 100644 --- a/coreapi/bellesip_sal/sal_impl.c +++ b/coreapi/bellesip_sal/sal_impl.c @@ -222,74 +222,77 @@ static void process_response_event(void *user_ctx, const belle_sip_response_even } if (op->callbacks.process_response_event) { - /*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 (!sal_op_get_contact(op)) { - /*check if contqct set in reauest*/ - if ((original_contact=belle_sip_message_get_header_by_type(request,belle_sip_header_contact_t))) { - /*no contact set yet, try to see if sip tack has an updated one*/ - 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); - } else { + 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 (!sal_op_get_contact(op)) { + /*check if contqct set in reauest*/ - /*hmm update contact from via, maybe useless, some op may not need any contact at all*/ - contact_address=belle_sip_header_address_new(); - contact_uri=belle_sip_uri_create(NULL,belle_sip_header_via_get_host(via_header)); - belle_sip_header_address_set_uri(contact_address,contact_uri); + if ((original_contact=belle_sip_message_get_header_by_type(request,belle_sip_header_contact_t))) { + /*no contact set yet, try to see if sip tack has an updated one*/ + 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); + } else { + /*hmm update contact from via, maybe useless, some op may not need any contact at all*/ + contact_address=belle_sip_header_address_new(); + contact_uri=belle_sip_uri_create(NULL,belle_sip_header_via_get_host(via_header)); + belle_sip_header_address_set_uri(contact_address,contact_uri); + + if (strcasecmp(belle_sip_header_via_get_transport(via_header),"UDP")!=0) { + belle_sip_uri_set_transport_param(contact_uri,belle_sip_header_via_get_transport_lowercase(via_header)); + } + if (belle_sip_header_via_get_listening_port(via_header) + != belle_sip_listening_point_get_well_known_port(belle_sip_header_via_get_transport(via_header))) { + belle_sip_uri_set_port(contact_uri,belle_sip_header_via_get_listening_port(via_header) ); + } + contact_updated=TRUE; + } + } + + if (received!=NULL || rport>0) { + if (sal_op_get_contact(op)){ + 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) { - belle_sip_uri_set_transport_param(contact_uri,belle_sip_header_via_get_transport_lowercase(via_header)); + 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 (belle_sip_header_via_get_listening_port(via_header) - != belle_sip_listening_point_get_well_known_port(belle_sip_header_via_get_transport(via_header))) { - belle_sip_uri_set_port(contact_uri,belle_sip_header_via_get_listening_port(via_header) ); + if (contact_updated) { + new_contact=belle_sip_object_to_string(BELLE_SIP_OBJECT(contact_address)); + ms_message("Updating contact from [%s] to [%s] for [%p]",sal_op_get_contact(op),new_contact,op); + sal_op_set_contact(op,new_contact); + belle_sip_free(new_contact); } - contact_updated=TRUE; + if (contact_address)belle_sip_object_unref(contact_address); } } - - if (received!=NULL || rport>0) { - if (sal_op_get_contact(op)){ - 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) { - new_contact=belle_sip_object_to_string(BELLE_SIP_OBJECT(contact_address)); - ms_message("Updating contact from [%s] to [%s] for [%p]",sal_op_get_contact(op),new_contact,op); - sal_op_set_contact(op,new_contact); - belle_sip_free(new_contact); - } - if (contact_address)belle_sip_object_unref(contact_address); - } /*update request/response * maybe only the transaction should be kept*/ old_request=op->request; @@ -369,6 +372,7 @@ Sal * sal_init(){ char stack_string[64]; belle_sip_listener_t* listener; Sal * sal=ms_new0(Sal,1); + sal->nat_helper_enabled=TRUE; snprintf(stack_string,sizeof(stack_string)-1,"(belle-sip/%s)",belle_sip_version_to_string()); sal->user_agent=belle_sip_header_user_agent_new(); belle_sip_header_user_agent_add_product(sal->user_agent, PACKAGE_NAME "/" LINPHONE_VERSION); @@ -590,3 +594,10 @@ void sal_set_send_error(Sal *sal,int value) { void sal_set_recv_error(Sal *sal,int value) { belle_sip_provider_set_recv_error(sal->prov,value); } +void sal_nat_helper_enable(Sal *sal,bool_t enable) { + sal->nat_helper_enabled=enable; + ms_message("Sal nat helper [%s]",enable?"enabled":"disabled"); +} +bool_t sal_nat_helper_enabled(Sal *sal) { + return sal->nat_helper_enabled; +} diff --git a/coreapi/bellesip_sal/sal_impl.h b/coreapi/bellesip_sal/sal_impl.h index f6f395fb5..dc72545a0 100644 --- a/coreapi/bellesip_sal/sal_impl.h +++ b/coreapi/bellesip_sal/sal_impl.h @@ -35,6 +35,7 @@ struct Sal{ bool_t one_matching_codec; unsigned int keep_alive; bool_t use_tcp_tls_keep_alive; + bool_t nat_helper_enabled; }; typedef enum SalOpSate { diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c index fc0a218bc..cb0f01e82 100644 --- a/coreapi/linphonecore.c +++ b/coreapi/linphonecore.c @@ -465,7 +465,8 @@ static void net_config_read (LinphoneCore *lc) linphone_core_set_mtu(lc,tmp); tmp=lp_config_get_int(lc->config,"net","download_ptime",0); linphone_core_set_download_ptime(lc,tmp); - + /*only set at setup*/ + sal_nat_helper_enable(lc->sal,lp_config_get_int(lc->config,"net","enable_nat_helper",1)); } static void build_sound_devices_table(LinphoneCore *lc){ @@ -1284,6 +1285,7 @@ static void linphone_core_init (LinphoneCore * lc, const LinphoneCoreVTable *vta lp_config_read_file(lc->config,factory_config_path); lc->sal=sal_init(); + sal_set_user_pointer(lc->sal,lc); sal_set_callbacks(lc->sal,&linphone_sal_callbacks); diff --git a/coreapi/sal.h b/coreapi/sal.h index e48b593e4..d7c75b46c 100644 --- a/coreapi/sal.h +++ b/coreapi/sal.h @@ -516,4 +516,7 @@ void __sal_op_free(SalOp *b); void sal_set_send_error(Sal *sal,int value); /*1 for no error*/ void sal_set_recv_error(Sal *sal,int value); +/*enable contact fixing*/ +void sal_nat_helper_enable(Sal *sal,bool_t enable); +bool_t sal_nat_helper_enabled(Sal *sal); #endif