]> sjero.net Git - linphone/blob - coreapi/linphone_tunnel.cc
- do not register outside of tunnel when tunnel is activated but not yet connected.
[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 LinphoneTunnel* linphone_core_get_tunnel(LinphoneCore *lc){
33         return lc->tunnel;
34 }
35
36 struct _LinphoneTunnel {
37         belledonnecomm::TunnelManager *manager;
38         MSList *config_list;
39 };
40
41 extern "C" LinphoneTunnel* linphone_core_tunnel_new(LinphoneCore *lc){
42         LinphoneTunnel* tunnel = ms_new0(LinphoneTunnel, 1);
43         tunnel->manager = new belledonnecomm::TunnelManager(lc);
44         return tunnel;
45 }
46
47 static inline belledonnecomm::TunnelManager *bcTunnel(LinphoneTunnel *tunnel){
48         return tunnel->manager;
49 }
50
51 static inline _LpConfig *config(LinphoneTunnel *tunnel){
52         return tunnel->manager->getLinphoneCore()->config;
53 }
54
55 void linphone_tunnel_destroy(LinphoneTunnel *tunnel){
56         delete tunnel->manager;
57         ms_free(tunnel);
58 }
59
60 static char *linphone_tunnel_config_to_string(const LinphoneTunnelConfig *tunnel_config) {
61         char *str = NULL;
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));
68         } else {
69                 str = ms_strdup_printf("%s:%d",
70                         linphone_tunnel_config_get_host(tunnel_config),
71                         linphone_tunnel_config_get_port(tunnel_config));
72         }
73         return str;
74 }
75
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;
80         int port = -1;
81         int remote_udp_mirror_port = -1;
82         int delay = -1;
83         int pos = 0;
84         char *pch;
85         pch = strtok(dstr, ":");
86         while(pch != NULL) {
87                 switch(pos) {
88                 case 0:
89                         host = pch;
90                         break;
91                 case 1:
92                         port = atoi(pch);
93                         break;
94                 case 2:
95                         remote_udp_mirror_port = atoi(pch);
96                         break;
97                 case 3:
98                         delay = atoi(pch);
99                         break;  
100                 default:
101                         // Abort
102                         pos = 0;
103                         break;
104                         
105                 }
106                 ++pos;
107                 pch = strtok(NULL, ":");
108         }
109         if(pos >= 2) {
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);
113         }
114         if(pos >= 3) {
115                 linphone_tunnel_config_set_remote_udp_mirror_port(tunnel_config, remote_udp_mirror_port);
116         }
117         if(pos == 4) {
118                 linphone_tunnel_config_set_delay(tunnel_config, delay);
119         }
120         ms_free(dstr);  
121         return tunnel_config;
122 }
123
124
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);
131                 if(tmp != NULL) {
132                         old_tmp = tmp;
133                         tmp = ms_strdup_printf("%s %s", old_tmp, tc_str);
134                         ms_free(old_tmp);
135                         ms_free(tc_str);
136                 } else {
137                         tmp = tc_str;
138                 }
139                 elem = elem->next;
140         }
141         lp_config_set_string(config(tunnel), "tunnel", "server_addresses", tmp);
142         if(tmp != NULL) {
143                 ms_free(tmp);
144         }
145 }
146
147
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));
152         } else {
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));
157         }
158         tunnel->config_list = ms_list_append(tunnel->config_list, tunnel_config);
159 }
160
161
162 static void linphone_tunnel_load_config(LinphoneTunnel *tunnel){
163         const char * confaddress = lp_config_get_string(config(tunnel), "tunnel", "server_addresses", NULL);
164         char *tmp;
165         const char *it;
166         LinphoneTunnelConfig *tunnel_config;
167         int adv;
168         if(confaddress != NULL) {
169                 tmp = ms_strdup(confaddress);
170                 it = confaddress;
171                 while(confaddress[0] != '\0') {
172                         int ret = sscanf(it,"%s%n", tmp, &adv);
173                         if (ret >= 1){
174                                 it += adv;
175                                 tunnel_config = linphone_tunnel_config_from_string(tmp);
176                                 if(tunnel_config != NULL) {
177                                         linphone_tunnel_add_server_intern(tunnel, tunnel_config);
178                                 } else {
179                                         ms_error("Tunnel server address incorrectly specified from config file: %s", tmp);
180                                 }
181                         } else break;
182                 }
183                 ms_free(tmp);
184         }
185 }
186
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;
195         }
196 }
197
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);
201 }
202
203 void linphone_tunnel_remove_server(LinphoneTunnel *tunnel, LinphoneTunnelConfig *tunnel_config) {
204         MSList *elem = ms_list_find(tunnel->config_list, tunnel_config);
205         if(elem != NULL) {
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);
210         }       
211 }
212
213 const MSList *linphone_tunnel_get_servers(LinphoneTunnel *tunnel){
214         return tunnel->config_list;
215 }
216
217 void linphone_tunnel_clean_servers(LinphoneTunnel *tunnel){
218         bcTunnel(tunnel)->cleanServers();
219         
220         /* Free the list */
221         ms_list_for_each(tunnel->config_list, (void (*)(void *))linphone_tunnel_config_destroy); 
222         tunnel->config_list = ms_list_free(tunnel->config_list);
223         
224         linphone_tunnel_save_config(tunnel);
225 }
226
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);
230 }
231
232 bool_t linphone_tunnel_enabled(LinphoneTunnel *tunnel){
233         return bcTunnel(tunnel)->isEnabled();
234 }
235
236 bool_t linphone_tunnel_connected(LinphoneTunnel *tunnel){
237         return bcTunnel(tunnel)->isReady();
238 }
239
240 static OrtpLogFunc tunnelOrtpLogHandler=NULL;
241
242 /*
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)
250 */
251
252 static void tunnelLogHandler(int level, const char *fmt, va_list l){
253         if (tunnelOrtpLogHandler){
254                 OrtpLogLevel ortp_level=ORTP_DEBUG;
255                 switch(level){
256                         case TUNNEL_DEBUG:
257                                 ortp_level=ORTP_DEBUG;
258                         break;
259                         case TUNNEL_INFO:
260                                 ortp_level=ORTP_MESSAGE;
261                         break;
262                         case TUNNEL_NOTICE:
263                                 ortp_level=ORTP_MESSAGE;
264                         break;
265                         case TUNNEL_WARN:
266                                 ortp_level=ORTP_WARNING;
267                         break;
268                         case TUNNEL_ERROR:
269                                 ortp_level=ORTP_ERROR;
270                         break;
271                         case TUNNEL_ALERT:
272                                 ortp_level=ORTP_ERROR;
273                         break;
274                         case TUNNEL_FATAL:
275                                 ortp_level=ORTP_FATAL;
276                         break;
277                         default:
278                                 ms_fatal("Unexepcted tunnel log %i: %s",level,fmt);
279                         break;
280                 }
281                 tunnelOrtpLogHandler(ortp_level,fmt,l);
282         }
283 }
284
285 void linphone_tunnel_enable_logs_with_handler(LinphoneTunnel *tunnel, bool_t enabled, OrtpLogFunc logHandler){
286         tunnelOrtpLogHandler=logHandler;
287         bcTunnel(tunnel)->enableLogs(enabled, tunnelLogHandler);
288 }
289
290 void linphone_tunnel_set_http_proxy_auth_info(LinphoneTunnel *tunnel, const char* username,const char* passwd){
291         bcTunnel(tunnel)->setHttpProxyAuthInfo(username, passwd);
292 }
293
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);
300 }
301
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);
307 }
308
309 void linphone_tunnel_reconnect(LinphoneTunnel *tunnel){
310         bcTunnel(tunnel)->reconnect();
311 }
312
313 void linphone_tunnel_auto_detect(LinphoneTunnel *tunnel){
314         bcTunnel(tunnel)->autoDetect();
315 }
316
317 static void my_ortp_logv(OrtpLogLevel level, const char *fmt, va_list args){
318         ortp_logv(level,fmt,args);
319 }
320
321 /**
322  * Startup tunnel using configuration.
323  * Called internally from linphonecore at startup.
324  */
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);
330 }
331