1 /***************************************************************************
5 * Copyright 2011 Belledonne Communications
6 * Author: Guillaume Beraudo
7 * Email: guillaume dot beraudo at linphone dot org
8 ****************************************************************************/
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 #include "TunnelManager.hh"
29 #include "linphone_tunnel.h"
30 #include "linphonecore.h"
34 LinphoneTunnel* linphone_core_get_tunnel(LinphoneCore *lc){
38 struct _LinphoneTunnelConfig {
41 int remote_udp_mirror_port;
45 LinphoneTunnelConfig *linphone_tunnel_config_new() {
46 LinphoneTunnelConfig *ltc = ms_new0(LinphoneTunnelConfig,1);
47 ltc->remote_udp_mirror_port = 12345;
52 void linphone_tunnel_config_set_host(LinphoneTunnelConfig *tunnel, const char *host) {
53 if(tunnel->host != NULL) {
54 ms_free(tunnel->host);
57 if(host != NULL && strlen(host)) {
58 tunnel->host = ms_strdup(host);
62 const char *linphone_tunnel_config_get_host(LinphoneTunnelConfig *tunnel) {
66 void linphone_tunnel_config_set_port(LinphoneTunnelConfig *tunnel, int port) {
70 int linphone_tunnel_config_get_port(LinphoneTunnelConfig *tunnel) {
74 void linphone_tunnel_config_set_remote_udp_mirror_port(LinphoneTunnelConfig *tunnel, int remote_udp_mirror_port) {
75 tunnel->remote_udp_mirror_port = remote_udp_mirror_port;
78 int linphone_tunnel_config_get_remote_udp_mirror_port(LinphoneTunnelConfig *tunnel) {
79 return tunnel->remote_udp_mirror_port;
82 void linphone_tunnel_config_set_delay(LinphoneTunnelConfig *tunnel, int delay) {
83 tunnel->delay = delay;
86 int linphone_tunnel_config_get_delay(LinphoneTunnelConfig *tunnel) {
90 void linphone_tunnel_config_destroy(LinphoneTunnelConfig *tunnel) {
91 if(tunnel->host != NULL) {
92 ms_free(tunnel->host);
99 struct _LinphoneTunnel {
100 belledonnecomm::TunnelManager *manager;
104 extern "C" LinphoneTunnel* linphone_core_tunnel_new(LinphoneCore *lc){
105 LinphoneTunnel* tunnel = ms_new0(LinphoneTunnel, 1);
106 tunnel->manager = new belledonnecomm::TunnelManager(lc);
110 static inline belledonnecomm::TunnelManager *bcTunnel(LinphoneTunnel *tunnel){
111 return tunnel->manager;
114 static inline _LpConfig *config(LinphoneTunnel *tunnel){
115 return tunnel->manager->getLinphoneCore()->config;
118 void linphone_tunnel_destroy(LinphoneTunnel *tunnel){
119 delete tunnel->manager;
123 static char *linphone_tunnel_config_to_string(const LinphoneTunnelConfig *tunnel_config) {
125 if(tunnel_config->remote_udp_mirror_port != -1) {
126 str = ms_strdup_printf("%s:%d:%d:%d",
129 tunnel_config->remote_udp_mirror_port,
130 tunnel_config->delay);
132 str = ms_strdup_printf("%s:%d",
134 tunnel_config->port);
139 static LinphoneTunnelConfig *linphone_tunnel_config_from_string(const char *str) {
140 LinphoneTunnelConfig *tunnel_config = NULL;
141 char * dstr = ms_strdup(str);
142 const char *host = NULL;
144 int remote_udp_mirror_port = -1;
148 pch = strtok(dstr, ":");
158 remote_udp_mirror_port = atoi(pch);
170 pch = strtok(NULL, ":");
173 tunnel_config = linphone_tunnel_config_new();
174 linphone_tunnel_config_set_host(tunnel_config, host);
175 linphone_tunnel_config_set_port(tunnel_config, port);
178 linphone_tunnel_config_set_remote_udp_mirror_port(tunnel_config, remote_udp_mirror_port);
181 linphone_tunnel_config_set_delay(tunnel_config, delay);
184 return tunnel_config;
188 static void linphone_tunnel_save_config(LinphoneTunnel *tunnel) {
189 MSList *elem = tunnel->config_list;
190 char *tmp = NULL, *old_tmp = NULL, *tc_str = NULL;
191 while(elem != NULL) {
192 LinphoneTunnelConfig *tunnel_config = (LinphoneTunnelConfig *)elem->data;
193 tc_str = linphone_tunnel_config_to_string(tunnel_config);
196 tmp = ms_strdup_printf("%s %s", old_tmp, tc_str);
204 lp_config_set_string(config(tunnel), "tunnel", "server_addresses", tmp);
211 static void linphone_tunnel_add_server_intern(LinphoneTunnel *tunnel, LinphoneTunnelConfig *tunnel_config) {
212 if(tunnel_config->remote_udp_mirror_port == -1) {
213 bcTunnel(tunnel)->addServer(tunnel_config->host, tunnel_config->port);
215 bcTunnel(tunnel)->addServer(tunnel_config->host, tunnel_config->port,
216 tunnel_config->remote_udp_mirror_port, tunnel_config->delay);
218 tunnel->config_list = ms_list_append(tunnel->config_list, tunnel_config);
222 static void linphone_tunnel_load_config(LinphoneTunnel *tunnel){
223 const char * confaddress = lp_config_get_string(config(tunnel), "tunnel", "server_addresses", NULL);
226 LinphoneTunnelConfig *tunnel_config;
228 if(confaddress != NULL) {
229 tmp = ms_strdup(confaddress);
231 while(confaddress[0] != '\0') {
232 int ret = sscanf(it,"%s%n", tmp, &adv);
235 tunnel_config = linphone_tunnel_config_from_string(tmp);
236 if(tunnel_config != NULL) {
237 linphone_tunnel_add_server_intern(tunnel, tunnel_config);
239 ms_error("Tunnel server address incorrectly specified from config file: %s", tmp);
247 static void linphone_tunnel_refresh_config(LinphoneTunnel *tunnel) {
248 MSList *old_list = tunnel->config_list;
249 tunnel->config_list = NULL;
250 bcTunnel(tunnel)->cleanServers();
251 while(old_list != NULL) {
252 LinphoneTunnelConfig *tunnel_config = (LinphoneTunnelConfig *)old_list->data;
253 linphone_tunnel_add_server_intern(tunnel, tunnel_config);
254 old_list = old_list->next;
258 void linphone_tunnel_add_server(LinphoneTunnel *tunnel, LinphoneTunnelConfig *tunnel_config) {
259 linphone_tunnel_add_server_intern(tunnel, tunnel_config);
260 linphone_tunnel_save_config(tunnel);
263 void linphone_tunnel_remove_server(LinphoneTunnel *tunnel, LinphoneTunnelConfig *tunnel_config) {
264 MSList *elem = ms_list_find(tunnel->config_list, tunnel_config);
266 tunnel->config_list = ms_list_remove(tunnel->config_list, tunnel_config);
267 linphone_tunnel_config_destroy(tunnel_config);
268 linphone_tunnel_refresh_config(tunnel);
269 linphone_tunnel_save_config(tunnel);
273 const MSList *linphone_tunnel_get_servers(LinphoneTunnel *tunnel){
274 return tunnel->config_list;
277 void linphone_tunnel_clean_servers(LinphoneTunnel *tunnel){
278 bcTunnel(tunnel)->cleanServers();
281 ms_list_for_each(tunnel->config_list, (void (*)(void *))linphone_tunnel_config_destroy);
282 tunnel->config_list = ms_list_free(tunnel->config_list);
284 linphone_tunnel_save_config(tunnel);
287 void linphone_tunnel_enable(LinphoneTunnel *tunnel, bool_t enabled){
288 lp_config_set_int(config(tunnel),"tunnel","enabled",(int)enabled);
289 bcTunnel(tunnel)->enable(enabled);
292 bool_t linphone_tunnel_enabled(LinphoneTunnel *tunnel){
293 return bcTunnel(tunnel)->isEnabled();
296 static OrtpLogFunc tunnelOrtpLogHandler=NULL;
299 #define TUNNEL_DEBUG (1)
300 #define TUNNEL_INFO (1<<1)
301 #define TUNNEL_NOTICE (1<<2)
302 #define TUNNEL_WARN (1<<3)
303 #define TUNNEL_ERROR (1<<4)
304 #define TUNNEL_ALERT (1<<5)
305 #define TUNNEL_FATAL (1<<6)
308 static void tunnelLogHandler(int level, const char *fmt, va_list l){
309 if (tunnelOrtpLogHandler){
310 OrtpLogLevel ortp_level=ORTP_DEBUG;
313 ortp_level=ORTP_DEBUG;
316 ortp_level=ORTP_MESSAGE;
319 ortp_level=ORTP_MESSAGE;
322 ortp_level=ORTP_WARNING;
325 ortp_level=ORTP_ERROR;
328 ortp_level=ORTP_ERROR;
331 ortp_level=ORTP_FATAL;
334 ms_fatal("Unexepcted tunnel log %i: %s",level,fmt);
337 tunnelOrtpLogHandler(ortp_level,fmt,l);
341 void linphone_tunnel_enable_logs_with_handler(LinphoneTunnel *tunnel, bool_t enabled, OrtpLogFunc logHandler){
342 tunnelOrtpLogHandler=logHandler;
343 bcTunnel(tunnel)->enableLogs(enabled, tunnelLogHandler);
346 void linphone_tunnel_set_http_proxy_auth_info(LinphoneTunnel *tunnel, const char* username,const char* passwd){
347 bcTunnel(tunnel)->setHttpProxyAuthInfo(username, passwd);
350 void linphone_tunnel_set_http_proxy(LinphoneTunnel*tunnel, const char *host, int port, const char* username,const char* passwd){
351 bcTunnel(tunnel)->setHttpProxy(host, port, username, passwd);
352 lp_config_set_string(config(tunnel),"tunnel","http_proxy_host",host);
353 lp_config_set_int(config(tunnel),"tunnel","http_proxy_port",port);
354 lp_config_set_string(config(tunnel),"tunnel","http_proxy_username",username);
355 lp_config_set_string(config(tunnel),"tunnel","http_proxy_password",passwd);
358 void linphone_tunnel_get_http_proxy(LinphoneTunnel*tunnel,const char **host, int *port, const char **username, const char **passwd){
359 if (host) *host=lp_config_get_string(config(tunnel),"tunnel","http_proxy_host",NULL);
360 if (port) *port=lp_config_get_int(config(tunnel),"tunnel","http_proxy_port",0);
361 if (username) *username=lp_config_get_string(config(tunnel),"tunnel","http_proxy_username",NULL);
362 if (passwd) *passwd=lp_config_get_string(config(tunnel),"tunnel","http_proxy_password",NULL);
365 void linphone_tunnel_reconnect(LinphoneTunnel *tunnel){
366 bcTunnel(tunnel)->reconnect();
369 void linphone_tunnel_auto_detect(LinphoneTunnel *tunnel){
370 bcTunnel(tunnel)->autoDetect();
373 static void my_ortp_logv(OrtpLogLevel level, const char *fmt, va_list args){
374 ortp_logv(level,fmt,args);
378 * Startup tunnel using configuration.
379 * Called internally from linphonecore at startup.
381 void linphone_tunnel_configure(LinphoneTunnel *tunnel){
382 bool_t enabled=(bool_t)lp_config_get_int(config(tunnel),"tunnel","enabled",FALSE);
383 linphone_tunnel_enable_logs_with_handler(tunnel,TRUE,my_ortp_logv);
384 linphone_tunnel_load_config(tunnel);
385 linphone_tunnel_enable(tunnel, enabled);
390 /*stubs to avoid to have #ifdef TUNNEL_ENABLED in upper layers*/
392 void linphone_tunnel_destroy(LinphoneTunnel *tunnel){
396 void linphone_tunnel_add_server(LinphoneTunnel *tunnel, LinphoneTunnelConfig *tunnel_config){
399 void linphone_tunnel_remove_server(LinphoneTunnel *tunnel, LinphoneTunnelConfig *tunnel_config){
402 const MSList *linphone_tunnel_get_servers(LinphoneTunnel *tunnel){
406 void linphone_tunnel_clean_servers(LinphoneTunnel *tunnel){
409 void linphone_tunnel_enable(LinphoneTunnel *tunnel, bool_t enabled){
412 bool_t linphone_tunnel_enabled(LinphoneTunnel *tunnel){
417 void linphone_tunnel_enable_logs_with_handler(LinphoneTunnel *tunnel, bool_t enabled, OrtpLogFunc logHandler){
420 void linphone_tunnel_set_http_proxy_auth_info(LinphoneTunnel *tunnel, const char* username,const char* passwd){
423 void linphone_tunnel_set_http_proxy(LinphoneTunnel*tunnel, const char *host, int port, const char* username,const char* passwd){
426 void linphone_tunnel_get_http_proxy(LinphoneTunnel*tunnel,const char **host, int *port, const char **username, const char **passwd){
429 void linphone_tunnel_reconnect(LinphoneTunnel *tunnel){
432 void linphone_tunnel_auto_detect(LinphoneTunnel *tunnel){
435 void linphone_tunnel_configure(LinphoneTunnel *tunnel){