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