From e3c60a69c8fd702f4916fc68fc0e15f74bdf9e77 Mon Sep 17 00:00:00 2001 From: Yann Diorcet Date: Fri, 15 Feb 2013 11:48:53 +0100 Subject: [PATCH] Update REGISTER contact with uPnP external informations --- coreapi/proxy.c | 23 +++++++++++++++++++---- coreapi/upnp.c | 29 +++++++++++++++++++++++++++-- coreapi/upnp.h | 1 + 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/coreapi/proxy.c b/coreapi/proxy.c index fb650c85..4aae6d3b 100644 --- a/coreapi/proxy.c +++ b/coreapi/proxy.c @@ -263,15 +263,30 @@ static char *guess_contact_for_register(LinphoneProxyConfig *obj){ if (proxy==NULL) return NULL; host=linphone_address_get_domain (proxy); if (host!=NULL){ - char localip[LINPHONE_IPADDR_SIZE]; + int localport = -1; + char localip_tmp[LINPHONE_IPADDR_SIZE] = {'\0'}; + const char *localip = NULL; char *tmp; LCSipTransports tr; LinphoneAddress *contact; - linphone_core_get_local_ip(obj->lc,host,localip); contact=linphone_address_new(obj->reg_identity); - linphone_address_set_domain (contact,localip); - linphone_address_set_port_int(contact,linphone_core_get_sip_port(obj->lc)); +#ifdef BUILD_UPNP + if (obj->lc->upnp != NULL && linphone_core_get_firewall_policy(obj->lc)==LinphonePolicyUseUpnp && + linphone_upnp_context_get_state(obj->lc->upnp) == LinphoneUpnpStateOk) { + localip = linphone_upnp_context_get_external_ipaddress(obj->lc->upnp); + localport = linphone_upnp_context_get_external_port(obj->lc->upnp); + } +#endif //BUILD_UPNP + if(localip == NULL) { + localip = localip_tmp; + linphone_core_get_local_ip(obj->lc,host,localip_tmp); + } + if(localport == -1) { + localport = linphone_core_get_sip_port(obj->lc); + } + linphone_address_set_port_int(contact,localport); + linphone_address_set_domain(contact,localip); linphone_address_set_display_name(contact,NULL); linphone_core_get_sip_transports(obj->lc,&tr); diff --git a/coreapi/upnp.c b/coreapi/upnp.c index d86c8a42..105f4a8d 100644 --- a/coreapi/upnp.c +++ b/coreapi/upnp.c @@ -368,8 +368,32 @@ void linphone_upnp_context_destroy(UpnpContext *lupnp) { ms_free(lupnp); } -LinphoneUpnpState linphone_upnp_context_get_state(UpnpContext *ctx) { - return ctx->state; +LinphoneUpnpState linphone_upnp_context_get_state(UpnpContext *lupnp) { + LinphoneUpnpState state; + ms_mutex_lock(&lupnp->mutex); + state = lupnp->state; + ms_mutex_unlock(&lupnp->mutex); + return state; +} + +int linphone_upnp_context_get_external_port(UpnpContext *lupnp) { + int port = -1; + ms_mutex_lock(&lupnp->mutex); + + /* Send port binding removes */ + if(lupnp->sip_udp != NULL) { + if(lupnp->sip_udp->state == LinphoneUpnpStateOk) + port = lupnp->sip_udp->external_port; + } else if(lupnp->sip_tcp != NULL) { + if(lupnp->sip_tcp->state == LinphoneUpnpStateOk) + port = lupnp->sip_tcp->external_port; + } else if(lupnp->sip_tls != NULL) { + if(lupnp->sip_tls->state == LinphoneUpnpStateOk) + port = lupnp->sip_tls->external_port; + } + + ms_mutex_unlock(&lupnp->mutex); + return port; } const char* linphone_upnp_context_get_external_ipaddress(UpnpContext *ctx) { @@ -964,6 +988,7 @@ LinphoneUpnpState linphone_upnp_session_get_state(UpnpSession *session) { return session->state; } + /* * uPnP Config */ diff --git a/coreapi/upnp.h b/coreapi/upnp.h index b3a5b9e4..08483588 100644 --- a/coreapi/upnp.h +++ b/coreapi/upnp.h @@ -40,6 +40,7 @@ UpnpContext *linphone_upnp_context_new(LinphoneCore *lc); void linphone_upnp_context_destroy(UpnpContext *ctx); LinphoneUpnpState linphone_upnp_context_get_state(UpnpContext *ctx); const char *linphone_upnp_context_get_external_ipaddress(UpnpContext *ctx); +int linphone_upnp_context_get_external_port(UpnpContext *ctx); void linphone_core_update_upnp_state_in_call_stats(LinphoneCall *call); #endif //LINPHONE_UPNP_H -- 2.39.2