TunnelManager.cc 17.1 KB
Newer Older
Guillaume Beraudo's avatar
Guillaume Beraudo committed
1 2 3
/*
 *  C Implementation: tunnel
 *
4
 * Description:
Guillaume Beraudo's avatar
Guillaume Beraudo committed
5 6 7 8 9 10 11 12 13 14 15 16
 *
 *
 * Author: Simon Morlat <simon.morlat@linphone.org>, (C) 2009
 *
 * Copyright (C) 2010  Belledonne Comunications, Grenoble, France
 *
 */


#include "TunnelManager.hh"

#include "ortp/rtpsession.h"
17 18
#include "linphone/core.h"
#include "linphone/core_utils.h"
Ghislain MARY's avatar
Ghislain MARY committed
19
#include "private.h"
Guillaume Beraudo's avatar
Guillaume Beraudo committed
20

21
#ifdef __ANDROID__
Guillaume Beraudo's avatar
Guillaume Beraudo committed
22 23 24
#include <android/log.h>
#endif

25
belledonnecomm::TunnelManager *bcTunnel(const LinphoneTunnel *tunnel);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
26 27

using namespace belledonnecomm;
28
using namespace ::std;
Guillaume Beraudo's avatar
Guillaume Beraudo committed
29 30

void TunnelManager::addServer(const char *ip, int port,unsigned int udpMirrorPort,unsigned int delay) {
31 32
	if (ip == NULL) {
		ms_warning("Adding tunnel server with empty ip, it will not work!");
Simon Morlat's avatar
Simon Morlat committed
33
		return;
34
	}
Guillaume Beraudo's avatar
Guillaume Beraudo committed
35
	addServer(ip,port);
36
	mUdpMirrorClients.push_back(UdpMirrorClient(ServerAddr(ip, (int)udpMirrorPort), delay));
Guillaume Beraudo's avatar
Guillaume Beraudo committed
37 38 39
}

void TunnelManager::addServer(const char *ip, int port) {
Ghislain MARY's avatar
Ghislain MARY committed
40 41
	if (ip == NULL) {
		ms_warning("Adding tunnel server with empty ip, it will not work!");
Simon Morlat's avatar
Simon Morlat committed
42
		return;
Ghislain MARY's avatar
Ghislain MARY committed
43
	}
44 45 46 47
	if (mUseDualClient) {
		ms_warning("TunnelManager is configured in dual mode, use addServerPair instead");
		return;
	}
48

Guillaume Beraudo's avatar
Guillaume Beraudo committed
49
	mServerAddrs.push_back(ServerAddr(ip,port));
50
	if (mTunnelClient && !mUseDualClient) {
Sylvain Berfini's avatar
Sylvain Berfini committed
51
		static_cast<TunnelClient*>(mTunnelClient)->addServer(ip,port);
52 53 54
	}
}

55 56 57 58 59 60
void TunnelManager::addServerPair(const char *ip1, int port1, const char *ip2, int port2, unsigned int udpMirrorPort, unsigned int delay) {
	if (ip1 == NULL || ip2 == NULL) {
		ms_warning("Adding tunnel server with empty ip, it will not work!");
		return;
	}
	addServerPair(ip1, port1, ip2, port2);
61
	mUdpMirrorClients.push_back(UdpMirrorClient(ServerAddr(ip1, (int)udpMirrorPort), delay));
62 63
}

64 65 66 67 68 69 70 71 72
void TunnelManager::addServerPair(const char *ip1, int port1, const char *ip2, int port2) {
	if (ip1 == NULL || ip2 == NULL) {
		ms_warning("Adding tunnel server with empty ip, it will not work!");
		return;
	}
	if (!mUseDualClient) {
		ms_warning("TunnelManager is configured in single mode, use addServer instead");
		return;
	}
73

Sylvain Berfini's avatar
Sylvain Berfini committed
74
	mDualServerAddrs.push_back(DualServerAddr(ip1, port1, ip2, port2));
75
	if (mTunnelClient && mUseDualClient) {
Sylvain Berfini's avatar
Sylvain Berfini committed
76
		static_cast<DualTunnelClient*>(mTunnelClient)->addServerPair(ip1, port1, ip2, port2);
77
	}
Guillaume Beraudo's avatar
Guillaume Beraudo committed
78 79 80 81
}

void TunnelManager::cleanServers() {
	mServerAddrs.clear();
82
	mDualServerAddrs.clear();
83 84 85 86
	if (mLongRunningTaskId > 0) {
		sal_end_background_task(mLongRunningTaskId);
		mLongRunningTaskId = 0;
	}
87 88
	for (auto &udpMirrorClient : mUdpMirrorClients)
		udpMirrorClient.stop();
Guillaume Beraudo's avatar
Guillaume Beraudo committed
89
	mUdpMirrorClients.clear();
Simon Morlat's avatar
Simon Morlat committed
90
	mCurrentUdpMirrorClient = mUdpMirrorClients.end();
Guillaume Beraudo's avatar
Guillaume Beraudo committed
91 92 93
	if (mTunnelClient) mTunnelClient->cleanServers();
}

94 95 96 97 98 99 100 101
void TunnelManager::enableDualMode(bool enable) {
	mUseDualClient = enable;
}

bool TunnelManager::isDualModeEnabled() {
	return mUseDualClient;
}

Guillaume Beraudo's avatar
Guillaume Beraudo committed
102 103 104 105 106
void TunnelManager::reconnect(){
	if (mTunnelClient)
		mTunnelClient->reconnect();
}

107
static void sCloseRtpTransport(RtpTransport *t){
108 109 110 111 112 113 114
	DualSocket *ds = (DualSocket *)t->data;
	TunnelSocket *sendSocket = ds->sendSocket;
	TunnelSocket *recvSocket = ds->recvSocket;
	TunnelManager *manager=(TunnelManager*)sendSocket->getUserPointer();
	manager->closeRtpTransport(t, sendSocket);
	manager->closeRtpTransport(t, recvSocket);
	ms_free(ds);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
115
}
116 117
void TunnelManager::closeRtpTransport(RtpTransport *t, TunnelSocket *s){
	mTunnelClient->closeSocket(s);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
118 119 120
}

static RtpTransport *sCreateRtpTransport(void* userData, int port){
Simon Morlat's avatar
Simon Morlat committed
121
	return ((TunnelManager *) userData)->createRtpTransport(port);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
122 123
}

124 125 126 127
void sDestroyRtpTransport(RtpTransport *t){
	ms_free(t);
}

Guillaume Beraudo's avatar
Guillaume Beraudo committed
128
RtpTransport *TunnelManager::createRtpTransport(int port){
129 130 131 132 133 134 135 136 137 138 139 140
	DualSocket *dualSocket = ms_new0(DualSocket, 1);
	if (!mUseDualClient) {
		TunnelSocket *socket = ((TunnelClient *)mTunnelClient)->createSocket(port);
		socket->setUserPointer(this);
		dualSocket->sendSocket = socket;
		dualSocket->recvSocket = socket;
	} else {
		dualSocket->sendSocket = ((DualTunnelClient *)mTunnelClient)->createSocket(TunnelSendOnly, port);
		dualSocket->sendSocket->setUserPointer(this);
		dualSocket->recvSocket = ((DualTunnelClient *)mTunnelClient)->createSocket(TunnelRecvOnly, port);
		dualSocket->recvSocket->setUserPointer(this);
	}
141

142
	RtpTransport *t = ms_new0(RtpTransport,1);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
143 144 145
	t->t_getsocket=NULL;
	t->t_recvfrom=customRecvfrom;
	t->t_sendto=customSendto;
146
	t->t_close=sCloseRtpTransport;
147
	t->t_destroy=sDestroyRtpTransport;
148
	t->data=dualSocket;
149
	ms_message("Creating tunnel RTP transport for local virtual port %i", port);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
150 151 152
	return t;
}

153
void TunnelManager::startClient() {
François Grisez's avatar
François Grisez committed
154
	ms_message("TunnelManager: Starting tunnel client");
Sylvain Berfini's avatar
Sylvain Berfini committed
155 156 157 158 159 160
	if (!mTunnelClient) {
		if (mUseDualClient) {
			mTunnelClient = DualTunnelClient::create(TRUE);
		} else {
			mTunnelClient = TunnelClient::create(TRUE);
		}
161

162
		mCore->sal->setTunnel(mTunnelClient);
163
		if (!mUseDualClient) {
Sylvain Berfini's avatar
Sylvain Berfini committed
164
			static_cast<TunnelClient*>(mTunnelClient)->setCallback(tunnelCallback,this);
165
		} else {
Sylvain Berfini's avatar
Sylvain Berfini committed
166
			static_cast<DualTunnelClient*>(mTunnelClient)->setCallback(tunnelCallback2,this);
167
		}
Simon Morlat's avatar
Simon Morlat committed
168
	}
169

170 171 172 173 174 175 176 177 178
	if (mVerifyServerCertificate) {
		const char *rootCertificatePath = linphone_core_get_root_ca(mCore);
		if (rootCertificatePath != NULL) {
			ms_message("TunnelManager: Load root certificate from %s", rootCertificatePath);
			mTunnelClient->setRootCertificate(rootCertificatePath); /* give the path to root certificate to the tunnel client in order to be able to verify the server certificate */
		} else {
			ms_warning("TunnelManager is set to verify server certificate but no root certificate is available in linphoneCore");
		}
	}
Simon Morlat's avatar
Simon Morlat committed
179
	mTunnelClient->cleanServers();
180
	if (mUseDualClient) {
Sylvain Berfini's avatar
Sylvain Berfini committed
181
		list<DualServerAddr>::iterator it;
182
		for(it=mDualServerAddrs.begin();it!=mDualServerAddrs.end();++it){
Sylvain Berfini's avatar
Sylvain Berfini committed
183 184
			const DualServerAddr &addr=*it;
			static_cast<DualTunnelClient*>(mTunnelClient)->addServerPair(addr.mAddr1.c_str(), addr.mPort1, addr.mAddr2.c_str(), addr.mPort2);
185 186 187 188 189
		}
	} else {
		list<ServerAddr>::iterator it;
		for(it=mServerAddrs.begin();it!=mServerAddrs.end();++it){
			const ServerAddr &addr=*it;
Sylvain Berfini's avatar
Sylvain Berfini committed
190
			static_cast<TunnelClient*>(mTunnelClient)->addServer(addr.mAddr.c_str(), addr.mPort);
191
		}
Guillaume Beraudo's avatar
Guillaume Beraudo committed
192
	}
193

François Grisez's avatar
François Grisez committed
194
	mTunnelClient->setHttpProxy(mHttpProxyHost.c_str(), mHttpProxyPort, mHttpUserName.c_str(), mHttpPasswd.c_str());
Sylvain Berfini's avatar
Sylvain Berfini committed
195 196
	if (!mTunnelClient->isStarted()) {
		ms_message("Starting tunnel client");
Simon Morlat's avatar
Simon Morlat committed
197
		mTunnelClient->start();
Sylvain Berfini's avatar
Sylvain Berfini committed
198 199 200
	}
	else {
		ms_message("Reconnecting tunnel client");
Simon Morlat's avatar
Simon Morlat committed
201
		mTunnelClient->reconnect(); /*force a reconnection to take into account new parameters*/
Sylvain Berfini's avatar
Sylvain Berfini committed
202
	}
Simon Morlat's avatar
Simon Morlat committed
203 204 205 206 207 208 209 210 211 212 213
}

void TunnelManager::stopClient(){
	if (linphone_core_get_calls_nb(mCore) == 0){
		/*if no calls are running, we can decide to stop the client completely, so that the connection to the tunnel server is terminated.*/
		if (mTunnelClient) {
			ms_message("TunnelManager: stoppping tunnel client");
			mTunnelClient->stop();
		}
		/*otherwise, it doesn't really matter if the tunnel connection is kept alive even if it is not used anymore by the liblinphone.*/
	}
Guillaume Beraudo's avatar
Guillaume Beraudo committed
214 215
}

216
bool TunnelManager::isConnected() const {
217
	return mTunnelClient != NULL && mTunnelClient->isReady();
Guillaume Beraudo's avatar
Guillaume Beraudo committed
218 219 220 221
}

int TunnelManager::customSendto(struct _RtpTransport *t, mblk_t *msg , int flags, const struct sockaddr *to, socklen_t tolen){
	int size;
222
	DualSocket *ds = (DualSocket *)t->data;
223 224 225
	msgpullup(msg, (size_t)-1);
	size = (int)msgdsize(msg);
	ds->sendSocket->sendto(msg->b_rptr, (size_t)size, to, tolen);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
226 227 228 229
	return size;
}

int TunnelManager::customRecvfrom(struct _RtpTransport *t, mblk_t *msg, int flags, struct sockaddr *from, socklen_t *fromlen){
230
	DualSocket *ds = (DualSocket *)t->data;
jehan's avatar
jehan committed
231
	memset(&msg->recv_addr,0,sizeof(msg->recv_addr));
232
	long err=ds->recvSocket->recvfrom(msg->b_wptr, (size_t)(dblk_lim(msg->b_datap) - dblk_base(msg->b_datap)), from, *fromlen);
jehan's avatar
jehan committed
233
	//to make ice happy
234
	inet_aton(((TunnelManager*)(ds->recvSocket)->getUserPointer())->mLocalAddr,&msg->recv_addr.addr.ipi_addr);
235
	msg->recv_addr.family = AF_INET;
236
	msg->recv_addr.port = htons((unsigned short)(ds->recvSocket)->getPort());
237
	if (err>0) return (int)err;
Guillaume Beraudo's avatar
Guillaume Beraudo committed
238 239 240
	return 0;
}

241 242
TunnelManager::TunnelManager(LinphoneCore* lc) :
	mCore(lc),
243
	mMode(LinphoneTunnelModeDisable),
244 245
	mTunnelClient(NULL),
	mHttpProxyPort(0),
246
	mVTable(NULL),
247
	mLongRunningTaskId(0),
248 249
	mSimulateUdpLoss(false),
	mUseDualClient(false)
250
{
Guillaume Beraudo's avatar
Guillaume Beraudo committed
251 252 253 254 255 256 257 258 259
	linphone_core_add_iterate_hook(mCore,(LinphoneCoreIterateHook)sOnIterate,this);
	mTransportFactories.audio_rtcp_func=sCreateRtpTransport;
	mTransportFactories.audio_rtcp_func_data=this;
	mTransportFactories.audio_rtp_func=sCreateRtpTransport;
	mTransportFactories.audio_rtp_func_data=this;
	mTransportFactories.video_rtcp_func=sCreateRtpTransport;
	mTransportFactories.video_rtcp_func_data=this;
	mTransportFactories.video_rtp_func=sCreateRtpTransport;
	mTransportFactories.video_rtp_func_data=this;
260
	mVTable = linphone_core_v_table_new();
261
	mVTable->network_reachable = networkReachableCb;
262
	linphone_core_add_listener(mCore, mVTable);
jehan's avatar
jehan committed
263
	linphone_core_get_local_ip_for(AF_INET, NULL, mLocalAddr);
Simon Morlat's avatar
Simon Morlat committed
264 265 266 267 268
	mAutodetectionRunning = false;
	mState = Off;
	mTargetState = Off;
	mStarted = false;
	mTunnelizeSipPackets = true;
Guillaume Beraudo's avatar
Guillaume Beraudo committed
269 270 271
}

TunnelManager::~TunnelManager(){
272 273 274 275
	if (mLongRunningTaskId > 0) {
		sal_end_background_task(mLongRunningTaskId);
		mLongRunningTaskId = 0;
	}
François Grisez's avatar
François Grisez committed
276 277 278
	for(UdpMirrorClientList::iterator udpMirror = mUdpMirrorClients.begin(); udpMirror != mUdpMirrorClients.end(); udpMirror++) {
		udpMirror->stop();
	}
Simon Morlat's avatar
Simon Morlat committed
279 280 281 282 283
	stopClient();
	if (mTunnelClient) {
		mTunnelClient->stop();
		delete mTunnelClient;
	}
284
	mCore->sal->setTunnel(NULL);
285
	linphone_core_remove_listener(mCore, mVTable);
286
	linphone_core_v_table_destroy(mVTable);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
287 288
}

289
void TunnelManager::doRegistration(){
François Grisez's avatar
François Grisez committed
290
	LinphoneProxyConfig* lProxy;
jehan's avatar
jehan committed
291
	lProxy = linphone_core_get_default_proxy_config(mCore);
François Grisez's avatar
François Grisez committed
292 293 294 295 296 297 298 299
	if (lProxy) {
		ms_message("TunnelManager: New registration");
		lProxy->commit = TRUE;
	}
}

void TunnelManager::doUnregistration() {
	LinphoneProxyConfig *lProxy;
jehan's avatar
jehan committed
300
	lProxy = linphone_core_get_default_proxy_config(mCore);
François Grisez's avatar
François Grisez committed
301 302
	if(lProxy) {
		_linphone_proxy_config_unregister(lProxy);
303 304 305
	}
}

Simon Morlat's avatar
Simon Morlat committed
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
void TunnelManager::tunnelizeLiblinphone(){
	ms_message("LinphoneCore goes into tunneled mode.");
	mState = On; /*do this first because _linphone_core_apply_transports() will use it to know if tunnel listening point is to be used*/
	linphone_core_set_rtp_transport_factories(mCore,&mTransportFactories);
	if (mTunnelizeSipPackets) {
		doUnregistration();
		_linphone_core_apply_transports(mCore);
		doRegistration();
	}
}

void TunnelManager::untunnelizeLiblinphone(){
	ms_message("LinphoneCore leaves tunneled mode.");
	mState = Off;
	linphone_core_set_rtp_transport_factories(mCore, NULL);
	if (mTunnelizeSipPackets) {
		doUnregistration();
		_linphone_core_apply_transports(mCore);
		doRegistration();
	}
}

void TunnelManager::applyState() {
	if (!linphone_core_is_network_reachable(mCore)) return;
	if (mTargetState == On && mState == Off){
		if (!mTunnelClient || !mTunnelClient->isStarted()){
			startClient();
		}
		if (mTunnelClient->isReady()) tunnelizeLiblinphone();
	}else if (mTargetState == Off && mState == On){
		untunnelizeLiblinphone();
		stopClient();
	}
}

void TunnelManager::setState ( TunnelManager::State state ) {
	mTargetState = state;
	applyState();
}

346
void TunnelManager::processTunnelEvent(const Event &ev){
347
	if (ev.mData.mConnected){
François Grisez's avatar
François Grisez committed
348
		ms_message("TunnelManager: tunnel is connected");
Simon Morlat's avatar
Simon Morlat committed
349
		applyState();
350
	} else {
François Grisez's avatar
François Grisez committed
351
		ms_error("TunnelManager: tunnel has been disconnected");
Guillaume Beraudo's avatar
Guillaume Beraudo committed
352 353 354
	}
}

Simon Morlat's avatar
Simon Morlat committed
355 356
void TunnelManager::applyMode() {
	switch(mMode) {
François Grisez's avatar
François Grisez committed
357
	case LinphoneTunnelModeEnable:
Simon Morlat's avatar
Simon Morlat committed
358 359
		stopAutoDetection();
		setState(On);
François Grisez's avatar
François Grisez committed
360 361
		break;
	case LinphoneTunnelModeDisable:
Simon Morlat's avatar
Simon Morlat committed
362 363
		stopAutoDetection();
		setState(Off);
François Grisez's avatar
François Grisez committed
364 365
		break;
	case LinphoneTunnelModeAuto:
Simon Morlat's avatar
Simon Morlat committed
366
		if (linphone_core_is_network_reachable(mCore)) startAutoDetection();
François Grisez's avatar
François Grisez committed
367 368
		break;
	default:
Simon Morlat's avatar
Simon Morlat committed
369
		ms_error("TunnelManager::setMode(): invalid mode (%d)", (int)mMode);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
370 371 372
	}
}

Simon Morlat's avatar
Simon Morlat committed
373 374 375 376 377 378 379
void TunnelManager::setMode(LinphoneTunnelMode mode) {
	if(mMode == mode) return;
	ms_message("TunnelManager: switching mode from %s to %s",
			   linphone_tunnel_mode_to_string(mMode),
			   linphone_tunnel_mode_to_string(mode));
	mMode = mode;
	applyMode();
380

Simon Morlat's avatar
Simon Morlat committed
381 382 383 384 385 386
}

void TunnelManager::stopLongRunningTask() {
	if (mLongRunningTaskId != 0) {
		sal_end_background_task(mLongRunningTaskId);
		mLongRunningTaskId = 0;
387
	}
Simon Morlat's avatar
Simon Morlat committed
388 389 390 391 392
}

void TunnelManager::tunnelCallback(bool connected, void *user_pointer){
	TunnelManager *zis = static_cast<TunnelManager*>(user_pointer);
	Event ev;
393

394 395 396 397 398
	ev.mType=TunnelEvent;
	ev.mData.mConnected=connected;
	zis->postEvent(ev);
}

399 400 401
void TunnelManager::tunnelCallback2(TunnelDirection direction, bool connected, void *user_pointer){
	TunnelManager *zis = static_cast<TunnelManager*>(user_pointer);
	Event ev;
402

403 404 405 406 407
	ev.mType=TunnelEvent;
	ev.mData.mConnected=connected;
	zis->postEvent(ev);
}

408 409 410 411 412 413 414 415 416 417 418 419 420 421
void TunnelManager::onIterate(){
	mMutex.lock();
	while(!mEvq.empty()){
		Event ev=mEvq.front();
		mEvq.pop();
		mMutex.unlock();
		if (ev.mType==TunnelEvent)
			processTunnelEvent(ev);
		else if (ev.mType==UdpMirrorClientEvent){
			processUdpMirrorEvent(ev);
		}
		mMutex.lock();
	}
	mMutex.unlock();
Guillaume Beraudo's avatar
Guillaume Beraudo committed
422 423 424 425
}

/*invoked from linphone_core_iterate() */
void TunnelManager::sOnIterate(TunnelManager *zis){
426
	zis->onIterate();
Guillaume Beraudo's avatar
Guillaume Beraudo committed
427 428
}

429 430
LinphoneTunnelMode TunnelManager::getMode() const {
	return mMode;
Guillaume Beraudo's avatar
Guillaume Beraudo committed
431
}
432 433

void TunnelManager::processUdpMirrorEvent(const Event &ev){
Simon Morlat's avatar
Simon Morlat committed
434
	if (mAutodetectionRunning == false) return; /*auto detection was cancelled, for example by switching to disabled state*/
435 436 437 438 439
	if (mSimulateUdpLoss || !ev.mData.mHaveUdp) {
		if (mSimulateUdpLoss) {
			ms_message("TunnelManager: simulate UDP lost on %s:%d", mCurrentUdpMirrorClient->getServerAddress().mAddr.c_str(), mCurrentUdpMirrorClient->getServerAddress().mPort);
		} else {
			ms_message("TunnelManager: UDP mirror test failed on %s:%d", mCurrentUdpMirrorClient->getServerAddress().mAddr.c_str(), mCurrentUdpMirrorClient->getServerAddress().mPort);
François Grisez's avatar
François Grisez committed
440
		}
441
		mCurrentUdpMirrorClient++;
442
		if (mCurrentUdpMirrorClient !=mUdpMirrorClients.end()) {
443
			ms_message("TunnelManager: trying another UDP mirror on %s:%d", mCurrentUdpMirrorClient->getServerAddress().mAddr.c_str(), mCurrentUdpMirrorClient->getServerAddress().mPort);
444 445
			UdpMirrorClient &lUdpMirrorClient=*mCurrentUdpMirrorClient;
			lUdpMirrorClient.start(TunnelManager::sUdpMirrorClientCallback,(void*)this);
Simon Morlat's avatar
Simon Morlat committed
446 447
			mAutodetectionRunning = true;
			return;
Guillaume Beraudo's avatar
Guillaume Beraudo committed
448
		} else {
François Grisez's avatar
François Grisez committed
449
			ms_message("TunnelManager: all UDP mirror tests failed");
Simon Morlat's avatar
Simon Morlat committed
450
			setState(On);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
451
		}
452
	} else {
Simon Morlat's avatar
Simon Morlat committed
453 454
		ms_message("TunnelManager: UDP mirror test success on %s:%d", mCurrentUdpMirrorClient->getServerAddress().mAddr.c_str(), mCurrentUdpMirrorClient->getServerAddress().mPort);
		setState(Off);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
455
	}
Simon Morlat's avatar
Simon Morlat committed
456 457
	mAutodetectionRunning = false;
	stopLongRunningTask();
458 459 460 461 462 463 464 465 466 467 468 469 470 471
}

void TunnelManager::postEvent(const Event &ev){
	mMutex.lock();
	mEvq.push(ev);
	mMutex.unlock();
}

void TunnelManager::sUdpMirrorClientCallback(bool isUdpAvailable, void* data) {
	TunnelManager* thiz = (TunnelManager*)data;
	Event ev;
	ev.mType=UdpMirrorClientEvent;
	ev.mData.mHaveUdp=isUdpAvailable;
	thiz->postEvent(ev);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
472 473
}

474 475
void TunnelManager::networkReachableCb(LinphoneCore *lc, bool_t reachable) {
	TunnelManager *tunnel = bcTunnel(linphone_core_get_tunnel(lc));
476

Simon Morlat's avatar
Simon Morlat committed
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
	if (reachable) {
		linphone_core_get_local_ip_for(AF_INET, NULL,tunnel->mLocalAddr);
		if (tunnel->getMode() == LinphoneTunnelModeAuto){
			tunnel->startAutoDetection();
			/*autodetection will call applyState() when finished*/
		}else{
			tunnel->applyState();
		}
	} else if (!reachable) {
		// if network is no more reachable, cancel autodetection if any
		tunnel->stopAutoDetection();
		//turn off the tunnel connection
		tunnel->stopClient();
		tunnel->untunnelizeLiblinphone();
	}
}

void TunnelManager::stopAutoDetection(){
	if (mAutodetectionRunning){
		for(UdpMirrorClientList::iterator udpMirror = mUdpMirrorClients.begin(); udpMirror != mUdpMirrorClients.end(); udpMirror++) {
497 498
			udpMirror->stop();
		}
Simon Morlat's avatar
Simon Morlat committed
499 500
		mAutodetectionRunning = false;
		stopLongRunningTask();
501 502 503
	}
}

François Grisez's avatar
François Grisez committed
504
bool TunnelManager::startAutoDetection() {
Guillaume Beraudo's avatar
Guillaume Beraudo committed
505
	if (mUdpMirrorClients.empty()) {
François Grisez's avatar
François Grisez committed
506
		ms_error("TunnelManager: No UDP mirror server configured aborting auto detection");
François Grisez's avatar
François Grisez committed
507
		return false;
Guillaume Beraudo's avatar
Guillaume Beraudo committed
508
	}
François Grisez's avatar
François Grisez committed
509
	ms_message("TunnelManager: Starting auto-detection");
510
	mCurrentUdpMirrorClient = mUdpMirrorClients.begin();
511 512
	if (mLongRunningTaskId == 0)
		 mLongRunningTaskId = sal_begin_background_task("Tunnel auto detect", NULL, NULL);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
513
	UdpMirrorClient &lUdpMirrorClient=*mCurrentUdpMirrorClient;
Simon Morlat's avatar
Simon Morlat committed
514
	mAutodetectionRunning = true;
515
	lUdpMirrorClient.start(TunnelManager::sUdpMirrorClientCallback,(void*)this);
François Grisez's avatar
François Grisez committed
516
	return true;
Guillaume Beraudo's avatar
Guillaume Beraudo committed
517 518
}

519
bool TunnelManager::isActivated() const{
Simon Morlat's avatar
Simon Morlat committed
520
	return mState == On;
521 522
}

Guillaume Beraudo's avatar
Guillaume Beraudo committed
523
void TunnelManager::setHttpProxyAuthInfo(const char* username,const char* passwd) {
jehan's avatar
jehan committed
524 525 526
	mHttpUserName=username?username:"";
	mHttpPasswd=passwd?passwd:"";
	if (mTunnelClient) mTunnelClient->setHttpProxyAuthInfo(username,passwd);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
527 528
}

529
void TunnelManager::tunnelizeSipPackets(bool enable){
530
	mTunnelizeSipPackets = enable;
531 532
}

533 534 535 536
bool TunnelManager::tunnelizeSipPacketsEnabled() const {
	return mTunnelizeSipPackets;
}

537 538 539 540 541 542 543 544
void TunnelManager::verifyServerCertificate(bool enable){
	mVerifyServerCertificate = enable;
}

bool TunnelManager::verifyServerCertificateEnabled() const {
	return mVerifyServerCertificate;
}

Simon Morlat's avatar
Simon Morlat committed
545 546 547 548 549 550 551 552
void TunnelManager::setHttpProxy(const char *host,int port, const char *username, const char *passwd){
	mHttpUserName=username?username:"";
	mHttpPasswd=passwd?passwd:"";
	mHttpProxyPort=(port>0) ? port : 0;
	mHttpProxyHost=host ? host : "";
	if (mTunnelClient) mTunnelClient->setHttpProxy(host, port, username, passwd);
}

553
LinphoneCore *TunnelManager::getLinphoneCore() const{
Guillaume Beraudo's avatar
Guillaume Beraudo committed
554 555
	return mCore;
}
556 557 558 559

void TunnelManager::simulateUdpLoss(bool enabled) {
	mSimulateUdpLoss = enabled;
}