]> sjero.net Git - linphone/blob - coreapi/TunnelManager.hh
Merge branch 'master' into tunnel
[linphone] / coreapi / TunnelManager.hh
1 /*
2  *  C Implementation: tunnel
3  *
4  * Description: 
5  *
6  *
7  *
8  *Copyright (C) 2011  Belledonne Comunications, Grenoble, France
9  */
10
11 #ifndef __TUNNEL_CLIENT_MANAGER_H__
12 #define __TUNNEL_CLIENT_MANAGER_H__
13 #include <list>
14 #include <string>
15 #include "linphonecore.h"
16 #include "tunnel/client.hh"
17 extern "C" {
18         #include "eXosip2/eXosip_transport_hook.h"
19 }
20 namespace belledonnecomm {
21 class TunnelClient;
22 class UdpMirrorClient;
23 /**
24  * @addtogroup tunnel_client 
25  * @{
26 **/
27
28         /**
29          * The TunnelManager class extends the LinphoneCore functionnality in order to provide an easy to use API to 
30          * - provision tunnel servers ip addresses and ports
31          * - start/stop the tunneling service
32          * - be informed of of connection and disconnection events to the tunnel server
33          * - perform auto-detection whether tunneling is required, based on a test of sending/receiving a flow of UDP packets.
34          * 
35          * It takes in charge automatically the SIP registration procedure when connecting or disconnecting to a tunnel server.
36          * No other action on LinphoneCore is required to enable full operation in tunnel mode.
37         **/
38         class TunnelManager : public TunnelClientController{
39                 
40         public:
41                 /**
42                  * Add a tunnel server. At least one should be provided to be able to connect.
43                  * When several addresses are provided, the tunnel client may try each of them until it gets connected.
44                  *
45                  * @param ip tunnMethod definition for '-isInitialStateOn' not foundel server ip address
46                  * @param port tunnel server tls port, recommended value is 443
47                  */
48                 void addServer(const char *ip, int port);
49                 /**
50                  *Add tunnel server with auto detection capabilities
51                  *
52                  * @param ip tunnel server ip address
53                  * @param port tunnel server tls port, recommended value is 443
54                  * @param udpMirrorPort remote port on the tunnel server side  used to test udp reachability
55                  * @param delay udp packet round trip delay in ms considered as acceptable. recommanded value is 1000 ms.
56                  */
57                 void addServer(const char *ip, int port,unsigned int udpMirrorPort,unsigned int delay);
58                 /**
59                  * Removes all tunnel server address previously entered with addServer()
60                 **/ 
61                 void cleanServers();
62                 /**
63                  * Register a state callback to be notified whenever the tunnel client is connected or disconnected to the tunnel server.
64                  * @param cb application callback function to use for notifying of connection/disconnection events.
65                  * @param userdata An opaque pointer passed to the callback, used optionally by the application to retrieve a context.
66                 **/             
67                 void setCallback(StateCallback cb, void *userdata);
68                 /**
69                  * Start connecting to a tunnel server.
70                  * At this step, nothing is tunneled yet. The enable() method must be used to state whether SIP and RTP traffic
71                  * need to be tunneled or not.
72                 **/
73                 void start();
74                 /**
75                  * Forces reconnection to the tunnel server.
76                  * This method is useful when the device switches from wifi to Edge/3G or vice versa. In most cases the tunnel client socket
77                  * won't be notified promptly that its connection is now zombie, so it is recommended to call this method that will cause
78                  * the lost connection to be closed and new connection to be issued.
79                 **/
80                 void reconnect();
81                 /**
82                  * Sets whether tunneling of SIP and RTP is required.
83                  * @param isEnabled If true enter in tunneled mode, if false exits from tunneled mode.
84                  * The TunnelManager takes care of refreshing SIP registration when switching on or off the tunneled mode.
85                  *
86                 **/
87                 void enable(bool isEnabled);
88                 /**
89                  * In auto detect mode, the tunnel manager try to establish a real time rtp cummunication with the tunnel server on  specified port.
90                  *<br>In case of success, the tunnel is automatically turned off. Otherwise, if no udp commmunication is feasible, tunnel mode is turned on.
91                  *<br> Call this method each time to run the auto detection algorithm
92                  */
93                 void autoDetect();
94                 /**
95                  * Returns a boolean indicating whether tunneled operation is enabled.
96                 **/
97                 bool isEnabled();
98                 /**
99                  * Enables debug logs of the Tunnel subsystem.
100                 **/
101                 void enableLogs(bool isEnabled);
102                 /**
103                  * Enables debugs logs of the Tunnel subsystem and specify a callback where to receive the debug messages.
104                 **/
105                 void enableLogs(bool isEnabled,LogHandler logHandler);
106                 /**
107                  * iOS only feature: specify http proxy credentials.
108                  * When the iOS device has an http proxy configured in the iOS settings, the tunnel client will connect to the server
109                  * through this http proxy. Credentials might be needed depending on the proxy configuration.
110                  * @param username The username.
111                  * @param passwd The password.
112                 **/
113                 void setHttpProxyAuthInfo(const char* username,const char* passwd);
114                 ~TunnelManager();
115                 TunnelManager(LinphoneCore* lc);
116                 /**
117                  * Destroy the given RtpTransport.
118                  */
119                 void closeRtpTransport(RtpTransport *t, TunnelSocket *s);
120
121                 /**
122                  * Create an RtpTransport.
123                  */
124                 RtpTransport *createRtpTransport(int port);
125
126                 /**
127                  * Get associated Linphone Core.
128                  */
129                 LinphoneCore *getLinphoneCore();
130         private:
131                 typedef std::list<UdpMirrorClient> UdpMirrorClientList;
132                 virtual bool isStarted();
133                 virtual bool isReady() const;
134                 static int customSendto(struct _RtpTransport *t, mblk_t *msg , int flags, const struct sockaddr *to, socklen_t tolen);
135                 static int customRecvfrom(struct _RtpTransport *t, mblk_t *msg, int flags, struct sockaddr *from, socklen_t *fromlen);
136                 static int eXosipSendto(int fd,const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen,void* userdata);
137                 static int eXosipRecvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen,void* userdata);
138                 static int eXosipSelect(int nfds, fd_set *s1, fd_set *s2, fd_set *s3, struct timeval *tv,void* userdata);
139                 static void tunnelCallback(bool connected, TunnelManager *zis);
140                 static void sOnIterate(TunnelManager *zis);
141                 static void UdpMirrorClientListener(bool result, void* data);
142
143                 void processTunnelEvent();
144                 LinphoneCore* mCore;
145                 LCSipTransports mRegularTransport;
146                 TunnelSocket *mSipSocket;
147                 eXosip_transport_hooks_t mExosipTransport;
148                 StateCallback mCallback;
149                 void * mCallbackData;
150                 bool mEnabled;
151                 bool mStateChanged;
152                 std::list <ServerAddr> mServerAddrs;
153                 UdpMirrorClientList mUdpMirrorClients;
154                 UdpMirrorClientList::iterator mCurrentUdpMirrorClient;
155                 TunnelClient* mTunnelClient;
156                 void stopClient();
157                 static Mutex sMutex;
158                 bool mAutoDetectStarted;
159                 LinphoneRtpTransportFactories mTransportFactories;
160                 std::string mHttpUserName;
161                 std::string mHttpPasswd;                
162         };
163
164 /**
165  * @}
166 **/
167
168 }
169
170
171
172 #endif /*__TUNNEL_CLIENT_MANAGER_H__*/