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"
32 LinphoneTunnel* linphone_core_get_tunnel(LinphoneCore *lc){
36 struct _LinphoneTunnel {
37 belledonnecomm::TunnelManager *manager;
41 extern "C" LinphoneTunnel* linphone_core_tunnel_new(LinphoneCore *lc){
42 LinphoneTunnel* tunnel = ms_new0(LinphoneTunnel, 1);
43 tunnel->manager = new belledonnecomm::TunnelManager(lc);
47 static inline belledonnecomm::TunnelManager *bcTunnel(LinphoneTunnel *tunnel){
48 return tunnel->manager;
51 static inline _LpConfig *config(LinphoneTunnel *tunnel){
52 return tunnel->manager->getLinphoneCore()->config;
55 void linphone_tunnel_destroy(LinphoneTunnel *tunnel){
56 delete tunnel->manager;
60 static char *linphone_tunnel_config_to_string(const LinphoneTunnelConfig *tunnel_config) {
62 if(linphone_tunnel_config_get_remote_udp_mirror_port(tunnel_config) != -1) {
63 str = ms_strdup_printf("%s:%d:%d:%d",
64 linphone_tunnel_config_get_host(tunnel_config),
65 linphone_tunnel_config_get_port(tunnel_config),
66 linphone_tunnel_config_get_remote_udp_mirror_port(tunnel_config),
67 linphone_tunnel_config_get_delay(tunnel_config));
69 str = ms_strdup_printf("%s:%d",
70 linphone_tunnel_config_get_host(tunnel_config),
71 linphone_tunnel_config_get_port(tunnel_config));
76 static LinphoneTunnelConfig *linphone_tunnel_config_from_string(const char *str) {
77 LinphoneTunnelConfig *tunnel_config = NULL;
78 char * dstr = ms_strdup(str);
79 const char *host = NULL;
81 int remote_udp_mirror_port = -1;
85 pch = strtok(dstr, ":");
95 remote_udp_mirror_port = atoi(pch);
107 pch = strtok(NULL, ":");
110 tunnel_config = linphone_tunnel_config_new();
111 linphone_tunnel_config_set_host(tunnel_config, host);
112 linphone_tunnel_config_set_port(tunnel_config, port);
115 linphone_tunnel_config_set_remote_udp_mirror_port(tunnel_config, remote_udp_mirror_port);
118 linphone_tunnel_config_set_delay(tunnel_config, delay);
121 return tunnel_config;
125 static void linphone_tunnel_save_config(LinphoneTunnel *tunnel) {
126 MSList *elem = tunnel->config_list;
127 char *tmp = NULL, *old_tmp = NULL, *tc_str = NULL;
128 while(elem != NULL) {
129 LinphoneTunnelConfig *tunnel_config = (LinphoneTunnelConfig *)elem->data;
130 tc_str = linphone_tunnel_config_to_string(tunnel_config);
133 tmp = ms_strdup_printf("%s %s", old_tmp, tc_str);
141 lp_config_set_string(config(tunnel), "tunnel", "server_addresses", tmp);
148 static void linphone_tunnel_add_server_intern(LinphoneTunnel *tunnel, LinphoneTunnelConfig *tunnel_config) {
149 if(linphone_tunnel_config_get_remote_udp_mirror_port(tunnel_config) == -1) {
150 bcTunnel(tunnel)->addServer(linphone_tunnel_config_get_host(tunnel_config),
151 linphone_tunnel_config_get_port(tunnel_config));
153 bcTunnel(tunnel)->addServer(linphone_tunnel_config_get_host(tunnel_config),
154 linphone_tunnel_config_get_port(tunnel_config),
155 linphone_tunnel_config_get_remote_udp_mirror_port(tunnel_config),
156 linphone_tunnel_config_get_delay(tunnel_config));
158 tunnel->config_list = ms_list_append(tunnel->config_list, tunnel_config);
162 static void linphone_tunnel_load_config(LinphoneTunnel *tunnel){
163 const char * confaddress = lp_config_get_string(config(tunnel), "tunnel", "server_addresses", NULL);
166 LinphoneTunnelConfig *tunnel_config;
168 if(confaddress != NULL) {
169 tmp = ms_strdup(confaddress);
171 while(confaddress[0] != '\0') {
172 int ret = sscanf(it,"%s%n", tmp, &adv);
175 tunnel_config = linphone_tunnel_config_from_string(tmp);
176 if(tunnel_config != NULL) {
177 linphone_tunnel_add_server_intern(tunnel, tunnel_config);
179 ms_error("Tunnel server address incorrectly specified from config file: %s", tmp);
187 static void linphone_tunnel_refresh_config(LinphoneTunnel *tunnel) {
188 MSList *old_list = tunnel->config_list;
189 tunnel->config_list = NULL;
190 bcTunnel(tunnel)->cleanServers();
191 while(old_list != NULL) {
192 LinphoneTunnelConfig *tunnel_config = (LinphoneTunnelConfig *)old_list->data;
193 linphone_tunnel_add_server_intern(tunnel, tunnel_config);
194 old_list = old_list->next;
198 void linphone_tunnel_add_server(LinphoneTunnel *tunnel, LinphoneTunnelConfig *tunnel_config) {
199 linphone_tunnel_add_server_intern(tunnel, tunnel_config);
200 linphone_tunnel_save_config(tunnel);
203 void linphone_tunnel_remove_server(LinphoneTunnel *tunnel, LinphoneTunnelConfig *tunnel_config) {
204 MSList *elem = ms_list_find(tunnel->config_list, tunnel_config);
206 tunnel->config_list = ms_list_remove(tunnel->config_list, tunnel_config);
207 linphone_tunnel_config_destroy(tunnel_config);
208 linphone_tunnel_refresh_config(tunnel);
209 linphone_tunnel_save_config(tunnel);
213 const MSList *linphone_tunnel_get_servers(LinphoneTunnel *tunnel){
214 return tunnel->config_list;
217 void linphone_tunnel_clean_servers(LinphoneTunnel *tunnel){
218 bcTunnel(tunnel)->cleanServers();
221 ms_list_for_each(tunnel->config_list, (void (*)(void *))linphone_tunnel_config_destroy);
222 tunnel->config_list = ms_list_free(tunnel->config_list);
224 linphone_tunnel_save_config(tunnel);
227 void linphone_tunnel_enable(LinphoneTunnel *tunnel, bool_t enabled){
228 lp_config_set_int(config(tunnel),"tunnel","enabled",(int)enabled);
229 bcTunnel(tunnel)->enable(enabled);
232 bool_t linphone_tunnel_enabled(LinphoneTunnel *tunnel){
233 return bcTunnel(tunnel)->isEnabled();
236 bool_t linphone_tunnel_connected(LinphoneTunnel *tunnel){
237 return bcTunnel(tunnel)->isReady();
240 static OrtpLogFunc tunnelOrtpLogHandler=NULL;
243 #define TUNNEL_DEBUG (1)
244 #define TUNNEL_INFO (1<<1)
245 #define TUNNEL_NOTICE (1<<2)
246 #define TUNNEL_WARN (1<<3)
247 #define TUNNEL_ERROR (1<<4)
248 #define TUNNEL_ALERT (1<<5)
249 #define TUNNEL_FATAL (1<<6)
252 static void tunnelLogHandler(int level, const char *fmt, va_list l){
253 if (tunnelOrtpLogHandler){
254 OrtpLogLevel ortp_level=ORTP_DEBUG;
257 ortp_level=ORTP_DEBUG;
260 ortp_level=ORTP_MESSAGE;
263 ortp_level=ORTP_MESSAGE;
266 ortp_level=ORTP_WARNING;
269 ortp_level=ORTP_ERROR;
272 ortp_level=ORTP_ERROR;
275 ortp_level=ORTP_FATAL;
278 ms_fatal("Unexepcted tunnel log %i: %s",level,fmt);
281 tunnelOrtpLogHandler(ortp_level,fmt,l);
285 void linphone_tunnel_enable_logs_with_handler(LinphoneTunnel *tunnel, bool_t enabled, OrtpLogFunc logHandler){
286 tunnelOrtpLogHandler=logHandler;
287 bcTunnel(tunnel)->enableLogs(enabled, tunnelLogHandler);
290 void linphone_tunnel_set_http_proxy_auth_info(LinphoneTunnel *tunnel, const char* username,const char* passwd){
291 bcTunnel(tunnel)->setHttpProxyAuthInfo(username, passwd);
294 void linphone_tunnel_set_http_proxy(LinphoneTunnel*tunnel, const char *host, int port, const char* username,const char* passwd){
295 bcTunnel(tunnel)->setHttpProxy(host, port, username, passwd);
296 lp_config_set_string(config(tunnel),"tunnel","http_proxy_host",host);
297 lp_config_set_int(config(tunnel),"tunnel","http_proxy_port",port);
298 lp_config_set_string(config(tunnel),"tunnel","http_proxy_username",username);
299 lp_config_set_string(config(tunnel),"tunnel","http_proxy_password",passwd);
302 void linphone_tunnel_get_http_proxy(LinphoneTunnel*tunnel,const char **host, int *port, const char **username, const char **passwd){
303 if (host) *host=lp_config_get_string(config(tunnel),"tunnel","http_proxy_host",NULL);
304 if (port) *port=lp_config_get_int(config(tunnel),"tunnel","http_proxy_port",0);
305 if (username) *username=lp_config_get_string(config(tunnel),"tunnel","http_proxy_username",NULL);
306 if (passwd) *passwd=lp_config_get_string(config(tunnel),"tunnel","http_proxy_password",NULL);
309 void linphone_tunnel_reconnect(LinphoneTunnel *tunnel){
310 bcTunnel(tunnel)->reconnect();
313 void linphone_tunnel_auto_detect(LinphoneTunnel *tunnel){
314 bcTunnel(tunnel)->autoDetect();
317 static void my_ortp_logv(OrtpLogLevel level, const char *fmt, va_list args){
318 ortp_logv(level,fmt,args);
322 * Startup tunnel using configuration.
323 * Called internally from linphonecore at startup.
325 void linphone_tunnel_configure(LinphoneTunnel *tunnel){
326 bool_t enabled=(bool_t)lp_config_get_int(config(tunnel),"tunnel","enabled",FALSE);
327 linphone_tunnel_enable_logs_with_handler(tunnel,TRUE,my_ortp_logv);
328 linphone_tunnel_load_config(tunnel);
329 linphone_tunnel_enable(tunnel, enabled);