From b67d2dd4a71626a150e7d57156f548985c1a32cc Mon Sep 17 00:00:00 2001
From: Simon Morlat <simon.morlat@linphone.org>
Date: Thu, 20 Sep 2012 21:30:42 +0200
Subject: [PATCH] fix: reuse stun discovered ip/port accross reinvites.

---
 coreapi/linphonecall.c | 42 +++++++++++++++++++-----------------------
 coreapi/misc.c         |  4 +++-
 coreapi/private.h      | 16 +++++++++-------
 3 files changed, 31 insertions(+), 31 deletions(-)

diff --git a/coreapi/linphonecall.c b/coreapi/linphonecall.c
index 7166d1c13a..7f11028ab2 100644
--- a/coreapi/linphonecall.c
+++ b/coreapi/linphonecall.c
@@ -193,6 +193,22 @@ static MSList *make_codec_list(LinphoneCore *lc, const MSList *codecs, int bandw
 	return l;
 }
 
+static void update_media_description_from_stun(SalMediaDescription *md, const StunCandidate *ac, const StunCandidate *vc){
+	if (ac->port!=0){
+		strcpy(md->streams[0].rtp_addr,ac->addr);
+		md->streams[0].rtp_port=ac->port;
+		if ((ac->addr[0]!='\0' && vc->addr[0]!='\0' && strcmp(ac->addr,vc->addr)==0) || md->nstreams==1){
+			strcpy(md->addr,ac->addr);
+		}
+	}
+	if (vc->port!=0){
+		strcpy(md->streams[1].rtp_addr,vc->addr);
+		md->streams[1].rtp_port=vc->port;
+	}
+	
+}
+
+
 static SalMediaDescription *_create_local_media_description(LinphoneCore *lc, LinphoneCall *call, unsigned int session_id, unsigned int session_ver){
 	MSList *l;
 	PayloadType *pt;
@@ -257,7 +273,7 @@ static SalMediaDescription *_create_local_media_description(LinphoneCore *lc, Li
 			ice_session_add_check_list(call->ice_session, ice_check_list_new());
 		}
 	}
-	
+	update_media_description_from_stun(md,&call->ac,&call->vc);
 	linphone_address_destroy(addr);
 	return md;
 }
@@ -327,20 +343,6 @@ void linphone_call_init_stats(LinphoneCallStats *stats, int type) {
 	stats->ice_state = LinphoneIceStateNotActivated;
 }
 
-static void update_media_description_from_stun(SalMediaDescription *md, const StunCandidate *ac, const StunCandidate *vc){
-	if (ac->port!=0){
-		strcpy(md->streams[0].rtp_addr,ac->addr);
-		md->streams[0].rtp_port=ac->port;
-		if ((ac->addr[0]!='\0' && vc->addr[0]!='\0' && strcmp(ac->addr,vc->addr)==0) || md->nstreams==1){
-			strcpy(md->addr,ac->addr);
-		}
-	}
-	if (vc->port!=0){
-		strcpy(md->streams[1].rtp_addr,vc->addr);
-		md->streams[1].rtp_port=vc->port;
-	}
-	
-}
 
 static void discover_mtu(LinphoneCore *lc, const char *remote){
 	int mtu;
@@ -355,12 +357,9 @@ static void discover_mtu(LinphoneCore *lc, const char *remote){
 	}
 }
 
-#define STUN_CANDIDATE_INIT {{0},0}
-
 LinphoneCall * linphone_call_new_outgoing(struct _LinphoneCore *lc, LinphoneAddress *from, LinphoneAddress *to, const LinphoneCallParams *params)
 {
 	LinphoneCall *call=ms_new0(LinphoneCall,1);
-	StunCandidate ac=STUN_CANDIDATE_INIT,vc=STUN_CANDIDATE_INIT;
 	int ping_time=-1;
 	call->dir=LinphoneCallOutgoing;
 	call->op=sal_op_new(lc->sal);
@@ -374,13 +373,12 @@ LinphoneCall * linphone_call_new_outgoing(struct _LinphoneCore *lc, LinphoneAddr
 		ice_session_set_role(call->ice_session, IR_Controlling);
 	}
 	if (linphone_core_get_firewall_policy(call->core) == LinphonePolicyUseStun) {
-		ping_time=linphone_core_run_stun_tests(call->core,call,&ac, &vc);
+		ping_time=linphone_core_run_stun_tests(call->core,call);
 	}
 	if (ping_time>=0) {
 		linphone_core_adapt_to_network(lc,ping_time,&call->params);
 	}
 	call->localdesc=create_local_media_description(lc,call);
-	update_media_description_from_stun(call->localdesc,&ac,&vc);
 	call->camera_active=params->has_video;
 	
 	discover_mtu(lc,linphone_address_get_domain (to));
@@ -395,7 +393,6 @@ LinphoneCall * linphone_call_new_incoming(LinphoneCore *lc, LinphoneAddress *fro
 	LinphoneCall *call=ms_new0(LinphoneCall,1);
 	char *from_str;
 	int ping_time=-1;
-	StunCandidate ac=STUN_CANDIDATE_INIT,vc=STUN_CANDIDATE_INIT;
 
 	call->dir=LinphoneCallIncoming;
 	sal_op_set_user_pointer(op,call);
@@ -434,7 +431,7 @@ LinphoneCall * linphone_call_new_incoming(LinphoneCore *lc, LinphoneAddress *fro
 			}
 			break;
 		case LinphonePolicyUseStun:
-			ping_time=linphone_core_run_stun_tests(call->core,call,&ac, &vc);
+			ping_time=linphone_core_run_stun_tests(call->core,call);
 			/* No break to also destroy ice session in this case. */
 		default:
 			break;
@@ -443,7 +440,6 @@ LinphoneCall * linphone_call_new_incoming(LinphoneCore *lc, LinphoneAddress *fro
 		linphone_core_adapt_to_network(lc,ping_time,&call->params);
 	};
 	call->localdesc=create_local_media_description(lc,call);
-	update_media_description_from_stun(call->localdesc,&ac,&vc);
 	call->camera_active=call->params.has_video;
 	
 	discover_mtu(lc,linphone_address_get_domain(from));
diff --git a/coreapi/misc.c b/coreapi/misc.c
index 8cdf2bb27b..15fc5374c5 100644
--- a/coreapi/misc.c
+++ b/coreapi/misc.c
@@ -467,8 +467,10 @@ static int recvStunResponse(ortp_socket_t sock, char *ipaddr, int *port, int *id
 }
 
 /* this functions runs a simple stun test and return the number of milliseconds to complete the tests, or -1 if the test were failed.*/
-int linphone_core_run_stun_tests(LinphoneCore *lc, LinphoneCall *call, StunCandidate *ac, StunCandidate *vc){
+int linphone_core_run_stun_tests(LinphoneCore *lc, LinphoneCall *call){
 	const char *server=linphone_core_get_stun_server(lc);
+	StunCandidate *ac=&call->ac;
+	StunCandidate *vc=&call->vc;
 	
 	if (lc->sip_conf.ipv6_enabled){
 		ms_warning("stun support is not implemented for ipv6");
diff --git a/coreapi/private.h b/coreapi/private.h
index f0b3b2c302..1e93c19d78 100644
--- a/coreapi/private.h
+++ b/coreapi/private.h
@@ -99,7 +99,13 @@ struct _LinphoneChatMessage {
 	char* external_body_url;
 	LinphoneAddress* from;
 };
-	
+
+typedef struct StunCandidate{
+	char addr[64];
+	int port;
+}StunCandidate;
+
+
 struct _LinphoneCall
 {
 	int magic; /*used to distinguish from proxy config*/
@@ -123,6 +129,7 @@ struct _LinphoneCall
 	void * user_pointer;
 	int audio_port;
 	int video_port;
+	StunCandidate ac,vc; /*audio video ip/port discovered by STUN*/
 	struct _AudioStream *audiostream;  /**/
 	struct _VideoStream *videostream;
 	MSAudioEndpoint *endpoint; /*used for conferencing*/
@@ -233,12 +240,7 @@ MSList *linphone_find_friend(MSList *fl, const LinphoneAddress *fri, LinphoneFri
 void linphone_core_update_allocated_audio_bandwidth(LinphoneCore *lc);
 void linphone_core_update_allocated_audio_bandwidth_in_call(LinphoneCall *call, const PayloadType *pt);
 
-typedef struct StunCandidate{
-	char addr[64];
-	int port;
-}StunCandidate;
-
-int linphone_core_run_stun_tests(LinphoneCore *lc, LinphoneCall *call, StunCandidate *ac, StunCandidate *vc);
+int linphone_core_run_stun_tests(LinphoneCore *lc, LinphoneCall *call);
 void linphone_core_adapt_to_network(LinphoneCore *lc, int ping_time_ms, LinphoneCallParams *params);
 int linphone_core_gather_ice_candidates(LinphoneCore *lc, LinphoneCall *call);
 void linphone_core_update_ice_state_in_call_stats(LinphoneCall *call);
-- 
GitLab