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.
26 #include "TunnelManager.hh"
27 #include "linphone_tunnel.h"
28 #include "linphonecore.h"
33 LinphoneTunnel* linphone_core_get_tunnel(LinphoneCore *lc){
37 static inline belledonnecomm::TunnelManager *bcTunnel(LinphoneTunnel *tunnel){
38 return (belledonnecomm::TunnelManager *)tunnel;
41 static inline _LpConfig *config(LinphoneTunnel *tunnel){
42 return ((belledonnecomm::TunnelManager *)tunnel)->getLinphoneCore()->config;
45 extern "C" LinphoneTunnel* linphone_core_tunnel_new(LinphoneCore *lc){
46 LinphoneTunnel* tunnel= (LinphoneTunnel*) new belledonnecomm::TunnelManager(lc);
50 void linphone_tunnel_destroy(LinphoneTunnel *tunnel){
51 delete bcTunnel(tunnel);
54 static void add_server_to_config(LinphoneTunnel *tunnel, const char *host, int port){
55 const char *orig=lp_config_get_string(config(tunnel),"tunnel","server_addresses", NULL);
58 tmp=ms_strdup_printf("%s %s:%i",orig,host,port);
59 }else tmp=ms_strdup_printf("%s:%i",host, port);
60 lp_config_set_string(config(tunnel),"tunnel","server_addresses",tmp);
64 void linphone_tunnel_add_server(LinphoneTunnel *tunnel, const char *host, int port){
65 bcTunnel(tunnel)->addServer(host, port);
66 add_server_to_config(tunnel,host,port);
69 void linphone_tunnel_add_server_and_mirror(LinphoneTunnel *tunnel, const char *host, int port, int remote_udp_mirror, int delay){
70 bcTunnel(tunnel)->addServer(host, port, remote_udp_mirror, delay);
71 /*FIXME, udp-mirror feature not saved in config*/
72 add_server_to_config(tunnel,host,port);
75 char *linphone_tunnel_get_servers(LinphoneTunnel *tunnel){
76 const char *tmp=lp_config_get_string(config(tunnel),"tunnel","server_addresses",NULL);
77 if (tmp) return ms_strdup(tmp);
81 void linphone_tunnel_clean_servers(LinphoneTunnel *tunnel){
82 bcTunnel(tunnel)->cleanServers();
83 lp_config_set_string(config(tunnel),"tunnel","server_addresses",NULL);
86 void linphone_tunnel_enable(LinphoneTunnel *tunnel, bool_t enabled){
87 lp_config_set_int(config(tunnel),"tunnel","enabled",(int)enabled);
88 bcTunnel(tunnel)->enable(enabled);
91 bool_t linphone_tunnel_enabled(LinphoneTunnel *tunnel){
92 return bcTunnel(tunnel)->isEnabled();
95 static OrtpLogFunc tunnelOrtpLogHandler=NULL;
98 #define TUNNEL_DEBUG (1)
99 #define TUNNEL_INFO (1<<1)
100 #define TUNNEL_NOTICE (1<<2)
101 #define TUNNEL_WARN (1<<3)
102 #define TUNNEL_ERROR (1<<4)
103 #define TUNNEL_ALERT (1<<5)
104 #define TUNNEL_FATAL (1<<6)
107 static void tunnelLogHandler(int level, const char *fmt, va_list l){
108 if (tunnelOrtpLogHandler){
109 OrtpLogLevel ortp_level=ORTP_DEBUG;
112 ortp_level=ORTP_DEBUG;
115 ortp_level=ORTP_MESSAGE;
118 ortp_level=ORTP_MESSAGE;
121 ortp_level=ORTP_WARNING;
124 ortp_level=ORTP_ERROR;
127 ortp_level=ORTP_ERROR;
130 ortp_level=ORTP_FATAL;
133 ms_fatal("Unexepcted tunnel log %i: %s",level,fmt);
136 tunnelOrtpLogHandler(ortp_level,fmt,l);
140 void linphone_tunnel_enable_logs_with_handler(LinphoneTunnel *tunnel, bool_t enabled, OrtpLogFunc logHandler){
141 tunnelOrtpLogHandler=logHandler;
142 bcTunnel(tunnel)->enableLogs(enabled, tunnelLogHandler);
145 void linphone_tunnel_set_http_proxy_auth_info(LinphoneTunnel *tunnel, const char* username,const char* passwd){
146 bcTunnel(tunnel)->setHttpProxyAuthInfo(username, passwd);
149 void linphone_tunnel_set_http_proxy(LinphoneTunnel*tunnel, const char *host, int port, const char* username,const char* passwd){
150 bcTunnel(tunnel)->setHttpProxy(host, port, username, passwd);
151 lp_config_set_string(config(tunnel),"tunnel","http_proxy_host",host);
152 lp_config_set_int(config(tunnel),"tunnel","http_proxy_port",port);
153 lp_config_set_string(config(tunnel),"tunnel","http_proxy_username",username);
154 lp_config_set_string(config(tunnel),"tunnel","http_proxy_password",passwd);
157 void linphone_tunnel_get_http_proxy(LinphoneTunnel*tunnel,const char **host, int *port, const char **username, const char **passwd){
158 if (host) *host=lp_config_get_string(config(tunnel),"tunnel","http_proxy_host",NULL);
159 if (port) *port=lp_config_get_int(config(tunnel),"tunnel","http_proxy_port",0);
160 if (username) *username=lp_config_get_string(config(tunnel),"tunnel","http_proxy_username",NULL);
161 if (passwd) *passwd=lp_config_get_string(config(tunnel),"tunnel","http_proxy_password",NULL);
164 void linphone_tunnel_reconnect(LinphoneTunnel *tunnel){
165 bcTunnel(tunnel)->reconnect();
168 void linphone_tunnel_auto_detect(LinphoneTunnel *tunnel){
169 bcTunnel(tunnel)->autoDetect();
172 static void tunnel_add_servers_from_config(LinphoneTunnel *tunnel, const char* confaddress){
173 char *tmp=(char*)ms_malloc0(strlen(confaddress)+1);
174 const char *it=confaddress;
177 int ret=sscanf(it,"%s%n",tmp,&adv);
180 char *port=strchr(tmp,':');
182 ms_error("Tunnel server addresses incorrectly specified from config file: %s",it);
187 bcTunnel(tunnel)->addServer(tmp, atoi(port));
194 static void my_ortp_logv(OrtpLogLevel level, const char *fmt, va_list args){
195 ortp_logv(level,fmt,args);
199 * Startup tunnel using configuration.
200 * Called internally from linphonecore at startup.
202 void linphone_tunnel_configure(LinphoneTunnel *tunnel){
203 bool_t enabled=(bool_t)lp_config_get_int(config(tunnel),"tunnel","enabled",FALSE);
204 const char* addresses=lp_config_get_string(config(tunnel),"tunnel","server_addresses", NULL);
205 linphone_tunnel_enable_logs_with_handler(tunnel,TRUE,my_ortp_logv);
207 tunnel_add_servers_from_config(tunnel,addresses);
208 linphone_tunnel_enable(tunnel, enabled);