]> sjero.net Git - linphone/blob - coreapi/linphone_tunnel.cc
refine tunnel implementation
[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 #ifdef TUNNEL_ENABLED
38
39 static inline belledonnecomm::TunnelManager *bcTunnel(LinphoneTunnel *tunnel){
40         return (belledonnecomm::TunnelManager *)tunnel;
41 }
42
43 static inline _LpConfig *config(LinphoneTunnel *tunnel){
44         return ((belledonnecomm::TunnelManager *)tunnel)->getLinphoneCore()->config;
45 }
46
47 extern "C" LinphoneTunnel* linphone_core_tunnel_new(LinphoneCore *lc){
48         LinphoneTunnel* tunnel= (LinphoneTunnel*) new belledonnecomm::TunnelManager(lc);
49         return tunnel;
50 }
51
52 void linphone_tunnel_destroy(LinphoneTunnel *tunnel){
53         delete bcTunnel(tunnel);
54 }
55
56 static void add_server_to_config(LinphoneTunnel *tunnel, const char *host, int port){
57         const char *orig=lp_config_get_string(config(tunnel),"tunnel","server_addresses", NULL);
58         char *tmp;
59         if (orig){
60                 tmp=ms_strdup_printf("%s %s:%i",orig,host,port);
61         }else tmp=ms_strdup_printf("%s:%i",host, port);
62         lp_config_set_string(config(tunnel),"tunnel","server_addresses",tmp);
63         ms_free(tmp);
64 }
65
66 void linphone_tunnel_add_server(LinphoneTunnel *tunnel, const char *host, int port){
67         bcTunnel(tunnel)->addServer(host, port);
68         add_server_to_config(tunnel,host,port);
69 }
70
71 void linphone_tunnel_add_server_and_mirror(LinphoneTunnel *tunnel, const char *host, int port, int remote_udp_mirror, int delay){
72         bcTunnel(tunnel)->addServer(host, port, remote_udp_mirror, delay);
73         /*FIXME, udp-mirror feature not saved in config*/
74         add_server_to_config(tunnel,host,port);
75 }
76
77 char *linphone_tunnel_get_servers(LinphoneTunnel *tunnel){
78         const char *tmp=lp_config_get_string(config(tunnel),"tunnel","server_addresses",NULL);
79         if (tmp) return ms_strdup(tmp);
80         return NULL;
81 }
82
83 void linphone_tunnel_clean_servers(LinphoneTunnel *tunnel){
84         bcTunnel(tunnel)->cleanServers();
85         lp_config_set_string(config(tunnel),"tunnel","server_addresses",NULL);
86 }
87
88 void linphone_tunnel_enable(LinphoneTunnel *tunnel, bool_t enabled){
89         lp_config_set_int(config(tunnel),"tunnel","enabled",(int)enabled);
90         bcTunnel(tunnel)->enable(enabled);
91 }
92
93 bool_t linphone_tunnel_enabled(LinphoneTunnel *tunnel){
94         return bcTunnel(tunnel)->isEnabled();
95 }
96
97 static OrtpLogFunc tunnelOrtpLogHandler=NULL;
98
99 /*
100 #define TUNNEL_DEBUG (1)
101 #define TUNNEL_INFO  (1<<1)
102 #define TUNNEL_NOTICE (1<<2)
103 #define TUNNEL_WARN  (1<<3)
104 #define TUNNEL_ERROR (1<<4)
105 #define TUNNEL_ALERT (1<<5)
106 #define TUNNEL_FATAL (1<<6)
107 */
108
109 static void tunnelLogHandler(int level, const char *fmt, va_list l){
110         if (tunnelOrtpLogHandler){
111                 OrtpLogLevel ortp_level=ORTP_DEBUG;
112                 switch(level){
113                         case TUNNEL_DEBUG:
114                                 ortp_level=ORTP_DEBUG;
115                         break;
116                         case TUNNEL_INFO:
117                                 ortp_level=ORTP_MESSAGE;
118                         break;
119                         case TUNNEL_NOTICE:
120                                 ortp_level=ORTP_MESSAGE;
121                         break;
122                         case TUNNEL_WARN:
123                                 ortp_level=ORTP_WARNING;
124                         break;
125                         case TUNNEL_ERROR:
126                                 ortp_level=ORTP_ERROR;
127                         break;
128                         case TUNNEL_ALERT:
129                                 ortp_level=ORTP_ERROR;
130                         break;
131                         case TUNNEL_FATAL:
132                                 ortp_level=ORTP_FATAL;
133                         break;
134                         default:
135                                 ms_fatal("Unexepcted tunnel log %i: %s",level,fmt);
136                         break;
137                 }
138                 tunnelOrtpLogHandler(ortp_level,fmt,l);
139         }
140 }
141
142 void linphone_tunnel_enable_logs_with_handler(LinphoneTunnel *tunnel, bool_t enabled, OrtpLogFunc logHandler){
143         tunnelOrtpLogHandler=logHandler;
144         bcTunnel(tunnel)->enableLogs(enabled, tunnelLogHandler);
145 }
146
147 void linphone_tunnel_set_http_proxy_auth_info(LinphoneTunnel *tunnel, const char* username,const char* passwd){
148         bcTunnel(tunnel)->setHttpProxyAuthInfo(username, passwd);
149 }
150
151 void linphone_tunnel_set_http_proxy(LinphoneTunnel*tunnel, const char *host, int port, const char* username,const char* passwd){
152         bcTunnel(tunnel)->setHttpProxy(host, port, username, passwd);
153 }
154
155 void linphone_tunnel_reconnect(LinphoneTunnel *tunnel){
156         bcTunnel(tunnel)->reconnect();
157 }
158
159 void linphone_tunnel_auto_detect(LinphoneTunnel *tunnel){
160         bcTunnel(tunnel)->autoDetect();
161 }
162
163 static void tunnel_add_servers_from_config(LinphoneTunnel *tunnel, const char* confaddress){
164         char *addresses=(char*)ms_strdup(confaddress);
165         char *str1;
166         for(str1=addresses;;str1=NULL){
167                 char *port;
168                 char *address=strtok(str1," "); // Not thread safe
169                 if (!address) break;
170                 port=strchr(address, ':');
171                 if (!port) ms_fatal("Bad tunnel address %s", address);
172                 *port++='\0';
173                 linphone_tunnel_add_server(tunnel, address, atoi(port));
174         }
175         ms_free(addresses);
176 }
177
178 static void my_ortp_logv(OrtpLogLevel level, const char *fmt, va_list args){
179         ortp_logv(level,fmt,args);
180 }
181
182 /**
183  * Startup tunnel using configuration.
184  * Called internally from linphonecore at startup.
185  */
186 void linphone_tunnel_configure(LinphoneTunnel *tunnel){
187         bool_t enabled=(bool_t)lp_config_get_int(config(tunnel),"tunnel","enabled",FALSE);
188         const char* addresses=lp_config_get_string(config(tunnel),"tunnel","server_addresses", NULL);
189         linphone_tunnel_enable_logs_with_handler(tunnel,TRUE,my_ortp_logv);
190         linphone_tunnel_clean_servers(tunnel);
191         if (addresses){
192                 tunnel_add_servers_from_config(tunnel,addresses);
193         }
194         linphone_tunnel_enable(tunnel, enabled);
195 }
196
197
198 #else
199
200 /*stubs to avoid to have #ifdef TUNNEL_ENABLED in upper layers*/
201
202 void linphone_tunnel_destroy(LinphoneTunnel *tunnel){
203 }
204
205
206 void linphone_tunnel_add_server(LinphoneTunnel *tunnel, const char *host, int port){
207 }
208
209 void linphone_tunnel_add_server_and_mirror(LinphoneTunnel *tunnel, const char *host, int port, int remote_udp_mirror, int delay){
210 }
211
212 char *linphone_tunnel_get_servers(LinphoneTunnel *tunnel){
213         return NULL;
214 }
215
216 void linphone_tunnel_clean_servers(LinphoneTunnel *tunnel){
217 }
218
219 void linphone_tunnel_enable(LinphoneTunnel *tunnel, bool_t enabled){
220 }
221
222 bool_t linphone_tunnel_enabled(LinphoneTunnel *tunnel){
223         return FALSE;
224 }
225
226 void linphone_tunnel_enable_logs(LinphoneTunnel *tunnel, bool_t enabled){
227 }
228
229 void linphone_tunnel_enable_logs_with_handler(LinphoneTunnel *tunnel, bool_t enabled, OrtpLogFunc logHandler){
230 }
231
232 void linphone_tunnel_set_http_proxy_auth_info(LinphoneTunnel *tunnel, const char* username,const char* passwd){
233 }
234
235 void linphone_tunnel_set_http_proxy(LinphoneTunnel*tunnel, const char *host, int port, const char* username,const char* passwd){
236 }
237
238 void linphone_tunnel_reconnect(LinphoneTunnel *tunnel){
239 }
240
241 void linphone_tunnel_auto_detect(LinphoneTunnel *tunnel){
242 }
243
244 void linphone_tunnel_configure(LinphoneTunnel *tunnel){
245 }
246
247
248 #endif
249
250
251
252
253