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