Tunnel.cpp 4.72 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
Tunnel.cpp
Copyright (C) 2015  Belledonne Communications, Grenoble, France
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

Ghislain MARY's avatar
Ghislain MARY committed
17 18 19 20 21
#include "Tunnel.h"
#include "Enums.h"
#include "ApiLock.h"
#include <collection.h>

22
using namespace BelledonneCommunications::Linphone::Native;
Ghislain MARY's avatar
Ghislain MARY committed
23 24 25 26 27 28
using namespace Platform;
using namespace Platform::Collections;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;


29
TunnelConfig::TunnelConfig(Platform::String^ host, int port, int udpMirrorPort, int roundTripDelay)
30
	: host(host), port(port), udpMirrorPort(udpMirrorPort), roundTripDelay(roundTripDelay)
Ghislain MARY's avatar
Ghislain MARY committed
31 32 33
{
}

34
Platform::String^ TunnelConfig::Host::get()
Ghislain MARY's avatar
Ghislain MARY committed
35 36 37 38
{
	return this->host;
}

39
void TunnelConfig::Host::set(Platform::String^ value)
Ghislain MARY's avatar
Ghislain MARY committed
40
{
Ghislain MARY's avatar
Ghislain MARY committed
41
	API_LOCK;
Ghislain MARY's avatar
Ghislain MARY committed
42 43 44
	this->host = value;
}

45
int TunnelConfig::Port::get()
Ghislain MARY's avatar
Ghislain MARY committed
46 47 48 49
{
	return this->port;
}

50
void TunnelConfig::Port::set(int value)
Ghislain MARY's avatar
Ghislain MARY committed
51
{
Ghislain MARY's avatar
Ghislain MARY committed
52
	API_LOCK;
Ghislain MARY's avatar
Ghislain MARY committed
53 54 55
	this->port = value;
}

56
int TunnelConfig::RoundTripDelay::get()
Ghislain MARY's avatar
Ghislain MARY committed
57 58 59 60
{
	return this->roundTripDelay;
}

61
void TunnelConfig::RoundTripDelay::set(int value)
Ghislain MARY's avatar
Ghislain MARY committed
62
{
Ghislain MARY's avatar
Ghislain MARY committed
63
	API_LOCK;
Ghislain MARY's avatar
Ghislain MARY committed
64 65 66
	this->roundTripDelay = value;
}

67
int TunnelConfig::UdpMirrorPort::get()
Ghislain MARY's avatar
Ghislain MARY committed
68
{
69
	return this->udpMirrorPort;
Ghislain MARY's avatar
Ghislain MARY committed
70 71
}

72
void TunnelConfig::UdpMirrorPort::set(int value)
Ghislain MARY's avatar
Ghislain MARY committed
73
{
Ghislain MARY's avatar
Ghislain MARY committed
74
	API_LOCK;
75
	this->udpMirrorPort = value;
Ghislain MARY's avatar
Ghislain MARY committed
76 77
}

78
Platform::String^ TunnelConfig::ToString()
Ghislain MARY's avatar
Ghislain MARY committed
79
{
80
	return "host[" + this->host + "] port[" + this->port + "] udpMirrorPort[" + this->udpMirrorPort + "] roundTripDelay[" + this->roundTripDelay + "]";
Ghislain MARY's avatar
Ghislain MARY committed
81 82
}

83

Ghislain MARY's avatar
Ghislain MARY committed
84 85 86 87

static void AddServerConfigToVector(void *vServerConfig, void *vector)
{
	::LinphoneTunnelConfig *pc = (LinphoneTunnelConfig *)vServerConfig;
88
	RefToPtrProxy<IVector<Object^>^> *list = reinterpret_cast< RefToPtrProxy<IVector<Object^>^> *>(vector);
Ghislain MARY's avatar
Ghislain MARY committed
89 90 91 92 93 94
	IVector<Object^>^ serverconfigs = (list) ? list->Ref() : nullptr;

	const char *chost = linphone_tunnel_config_get_host(pc);
	int port = linphone_tunnel_config_get_port(pc);
	int udpMirrorPort = linphone_tunnel_config_get_remote_udp_mirror_port(pc);
	int roundTripDelay = linphone_tunnel_config_get_delay(pc);
95
	TunnelConfig^ serverConfig = ref new TunnelConfig(Utils::cctops(chost), port, udpMirrorPort, roundTripDelay);
Ghislain MARY's avatar
Ghislain MARY committed
96 97 98
	serverconfigs->Append(serverConfig);
}

99
Tunnel::Tunnel(::LinphoneTunnel *tunnel)
100
	: lt(tunnel)
Ghislain MARY's avatar
Ghislain MARY committed
101 102 103
{
}

104
Tunnel::~Tunnel()
105 106 107
{
}

108
Platform::Boolean Tunnel::IsEnabled::get()
Ghislain MARY's avatar
Ghislain MARY committed
109
{
Ghislain MARY's avatar
Ghislain MARY committed
110
	API_LOCK;
111
	return (linphone_tunnel_enabled(this->lt) == TRUE);
Ghislain MARY's avatar
Ghislain MARY committed
112 113
}

114
void Tunnel::IsEnabled::set(Platform::Boolean enable)
Ghislain MARY's avatar
Ghislain MARY committed
115
{
Ghislain MARY's avatar
Ghislain MARY committed
116
	API_LOCK;
117
	linphone_tunnel_enable(this->lt, enable);
Ghislain MARY's avatar
Ghislain MARY committed
118 119
}

120
void Tunnel::AddServer(Platform::String^ host, int port)
Ghislain MARY's avatar
Ghislain MARY committed
121
{
Ghislain MARY's avatar
Ghislain MARY committed
122
	API_LOCK;
123
	const char* h = Utils::pstoccs(host);
Ghislain MARY's avatar
Ghislain MARY committed
124 125 126 127 128 129 130
	LinphoneTunnelConfig* config = linphone_tunnel_config_new();
	linphone_tunnel_config_set_host(config, h);
	linphone_tunnel_config_set_port(config, port);
	linphone_tunnel_add_server(this->lt, config);
	delete(h);
}

131
void Tunnel::AddServer(Platform::String^ host, int port, int udpMirrorPort, int roundTripDelay)
Ghislain MARY's avatar
Ghislain MARY committed
132
{
Ghislain MARY's avatar
Ghislain MARY committed
133
	API_LOCK;
134
	const char* h = Utils::pstoccs(host);
Ghislain MARY's avatar
Ghislain MARY committed
135 136 137 138 139 140 141 142
	LinphoneTunnelConfig* config = linphone_tunnel_config_new();
	linphone_tunnel_config_set_host(config, h);
	linphone_tunnel_config_set_port(config, port);
	linphone_tunnel_config_set_delay(config, roundTripDelay);
	linphone_tunnel_config_set_remote_udp_mirror_port(config, udpMirrorPort);
	linphone_tunnel_add_server(this->lt, config);
	delete(h);
}
143

144
void Tunnel::AutoDetect()
145 146 147 148 149
{
	API_LOCK;
	linphone_tunnel_auto_detect(this->lt);
}

150
void Tunnel::CleanServers()
151 152 153 154 155
{
	API_LOCK;
	linphone_tunnel_clean_servers(this->lt);
}

156
IVector<Object^>^ Tunnel::GetServers()
157 158 159 160 161 162 163 164 165
{
	API_LOCK;
	IVector<Object^>^ serverconfigs = ref new Vector<Object^>();
	const MSList *configList = linphone_tunnel_get_servers(this->lt);
	RefToPtrProxy<IVector<Object^>^> *serverConfigPtr = new RefToPtrProxy<IVector<Object^>^>(serverconfigs);
	ms_list_for_each2(configList, AddServerConfigToVector, serverConfigPtr);
	return serverconfigs;
}

166
void Tunnel::SetHttpProxy(Platform::String^ host, int port, Platform::String^ username, Platform::String^ password)
167 168
{
	API_LOCK;
169 170 171
	const char* h = Utils::pstoccs(host);
	const char* u = Utils::pstoccs(username);
	const char* pwd = Utils::pstoccs(password);
172 173 174 175 176
	linphone_tunnel_set_http_proxy(this->lt, h, port, u, pwd);
	delete(h);
	delete(u);
	delete(pwd);
}