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