]> sjero.net Git - linphone/blob - coreapi/linphone_tunnel.cc
Fix static build split tunnel stubs
[linphone] / coreapi / linphone_tunnel.cc
1 /***************************************************************************
2  *            linphone_tunnel.cc
3  *
4  *  Fri Dec 9, 2011
5  *  Copyright  2011  Belledonne Communications
6  *  Author: Guillaume Beraudo
7  *  Email: guillaume dot beraudo at linphone dot org
8  ****************************************************************************/
9
10 /*
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.
15  *
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.
20  *
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.
24  */
25
26 #include "TunnelManager.hh"
27 #include "linphone_tunnel.h"
28 #include "linphonecore.h"
29 #include "private.h"
30 #include "lpconfig.h"
31
32
33 LinphoneTunnel* linphone_core_get_tunnel(LinphoneCore *lc){
34         return lc->tunnel;
35 }
36
37 static inline belledonnecomm::TunnelManager *bcTunnel(LinphoneTunnel *tunnel){
38         return (belledonnecomm::TunnelManager *)tunnel;
39 }
40
41 static inline _LpConfig *config(LinphoneTunnel *tunnel){
42         return ((belledonnecomm::TunnelManager *)tunnel)->getLinphoneCore()->config;
43 }
44
45 extern "C" LinphoneTunnel* linphone_core_tunnel_new(LinphoneCore *lc){
46         LinphoneTunnel* tunnel= (LinphoneTunnel*) new belledonnecomm::TunnelManager(lc);
47         return tunnel;
48 }
49
50 void linphone_tunnel_destroy(LinphoneTunnel *tunnel){
51         delete bcTunnel(tunnel);
52 }
53
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);
56         char *tmp;
57         if (orig){
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);
61         ms_free(tmp);
62 }
63
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);
67 }
68
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);
73 }
74
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);
78         return NULL;
79 }
80
81 void linphone_tunnel_clean_servers(LinphoneTunnel *tunnel){
82         bcTunnel(tunnel)->cleanServers();
83         lp_config_set_string(config(tunnel),"tunnel","server_addresses",NULL);
84 }
85
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);
89 }
90
91 bool_t linphone_tunnel_enabled(LinphoneTunnel *tunnel){
92         return bcTunnel(tunnel)->isEnabled();
93 }
94
95 static OrtpLogFunc tunnelOrtpLogHandler=NULL;
96
97 /*
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)
105 */
106
107 static void tunnelLogHandler(int level, const char *fmt, va_list l){
108         if (tunnelOrtpLogHandler){
109                 OrtpLogLevel ortp_level=ORTP_DEBUG;
110                 switch(level){
111                         case TUNNEL_DEBUG:
112                                 ortp_level=ORTP_DEBUG;
113                         break;
114                         case TUNNEL_INFO:
115                                 ortp_level=ORTP_MESSAGE;
116                         break;
117                         case TUNNEL_NOTICE:
118                                 ortp_level=ORTP_MESSAGE;
119                         break;
120                         case TUNNEL_WARN:
121                                 ortp_level=ORTP_WARNING;
122                         break;
123                         case TUNNEL_ERROR:
124                                 ortp_level=ORTP_ERROR;
125                         break;
126                         case TUNNEL_ALERT:
127                                 ortp_level=ORTP_ERROR;
128                         break;
129                         case TUNNEL_FATAL:
130                                 ortp_level=ORTP_FATAL;
131                         break;
132                         default:
133                                 ms_fatal("Unexepcted tunnel log %i: %s",level,fmt);
134                         break;
135                 }
136                 tunnelOrtpLogHandler(ortp_level,fmt,l);
137         }
138 }
139
140 void linphone_tunnel_enable_logs_with_handler(LinphoneTunnel *tunnel, bool_t enabled, OrtpLogFunc logHandler){
141         tunnelOrtpLogHandler=logHandler;
142         bcTunnel(tunnel)->enableLogs(enabled, tunnelLogHandler);
143 }
144
145 void linphone_tunnel_set_http_proxy_auth_info(LinphoneTunnel *tunnel, const char* username,const char* passwd){
146         bcTunnel(tunnel)->setHttpProxyAuthInfo(username, passwd);
147 }
148
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);
155 }
156
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);
162 }
163
164 void linphone_tunnel_reconnect(LinphoneTunnel *tunnel){
165         bcTunnel(tunnel)->reconnect();
166 }
167
168 void linphone_tunnel_auto_detect(LinphoneTunnel *tunnel){
169         bcTunnel(tunnel)->autoDetect();
170 }
171
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;
175         int adv;
176         do{
177                 int ret=sscanf(it,"%s%n",tmp,&adv);
178                 if (ret>=1){
179                         it+=adv;
180                         char *port=strchr(tmp,':');
181                         if (!port){
182                                 ms_error("Tunnel server addresses incorrectly specified from config file: %s",it);
183                                 break;
184                         }else{
185                                 *port='\0';
186                                 port++;
187                                 bcTunnel(tunnel)->addServer(tmp, atoi(port));
188                         }
189                 }else break;
190         }while(1);
191         ms_free(tmp);
192 }
193
194 static void my_ortp_logv(OrtpLogLevel level, const char *fmt, va_list args){
195         ortp_logv(level,fmt,args);
196 }
197
198 /**
199  * Startup tunnel using configuration.
200  * Called internally from linphonecore at startup.
201  */
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);
206         if (addresses)
207                 tunnel_add_servers_from_config(tunnel,addresses);
208         linphone_tunnel_enable(tunnel, enabled);
209 }
210