TunnelManager.hh 7.08 KB
Newer Older
Guillaume Beraudo's avatar
Guillaume Beraudo committed
1 2 3
/*
 *  C Implementation: tunnel
 *
4
 * Description:
Guillaume Beraudo's avatar
Guillaume Beraudo committed
5 6 7 8 9 10 11 12 13
 *
 *
 *
 *Copyright (C) 2011  Belledonne Comunications, Grenoble, France
 */

#ifndef __TUNNEL_CLIENT_MANAGER_H__
#define __TUNNEL_CLIENT_MANAGER_H__
#include <list>
jehan's avatar
jehan committed
14
#include <string>
Guillaume Beraudo's avatar
Guillaume Beraudo committed
15
#include "tunnel/client.hh"
Simon Morlat's avatar
Simon Morlat committed
16 17
#include "linphonecore.h"

Ghislain MARY's avatar
Ghislain MARY committed
18
#ifndef USE_BELLESIP
Guillaume Beraudo's avatar
Guillaume Beraudo committed
19 20 21
extern "C" {
	#include "eXosip2/eXosip_transport_hook.h"
}
Ghislain MARY's avatar
Ghislain MARY committed
22 23
#endif

Guillaume Beraudo's avatar
Guillaume Beraudo committed
24 25 26 27
namespace belledonnecomm {
class TunnelClient;
class UdpMirrorClient;
/**
28
 * @addtogroup tunnel_client
Guillaume Beraudo's avatar
Guillaume Beraudo committed
29 30 31 32
 * @{
**/

	/**
33
	 * The TunnelManager class extends the LinphoneCore functionnality in order to provide an easy to use API to
Guillaume Beraudo's avatar
Guillaume Beraudo committed
34 35 36 37
	 * - provision tunnel servers ip addresses and ports
	 * - start/stop the tunneling service
	 * - be informed of of connection and disconnection events to the tunnel server
	 * - perform auto-detection whether tunneling is required, based on a test of sending/receiving a flow of UDP packets.
38
	 *
Guillaume Beraudo's avatar
Guillaume Beraudo committed
39 40 41 42
	 * It takes in charge automatically the SIP registration procedure when connecting or disconnecting to a tunnel server.
	 * No other action on LinphoneCore is required to enable full operation in tunnel mode.
	**/
	class TunnelManager : public TunnelClientController{
43

Guillaume Beraudo's avatar
Guillaume Beraudo committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	public:
		/**
		 * Add a tunnel server. At least one should be provided to be able to connect.
		 * When several addresses are provided, the tunnel client may try each of them until it gets connected.
		 *
		 * @param ip tunnMethod definition for '-isInitialStateOn' not foundel server ip address
		 * @param port tunnel server tls port, recommended value is 443
		 */
		void addServer(const char *ip, int port);
		/**
		 *Add tunnel server with auto detection capabilities
		 *
		 * @param ip tunnel server ip address
		 * @param port tunnel server tls port, recommended value is 443
		 * @param udpMirrorPort remote port on the tunnel server side  used to test udp reachability
		 * @param delay udp packet round trip delay in ms considered as acceptable. recommanded value is 1000 ms.
		 */
		void addServer(const char *ip, int port,unsigned int udpMirrorPort,unsigned int delay);
		/**
		 * Removes all tunnel server address previously entered with addServer()
64
		**/
Guillaume Beraudo's avatar
Guillaume Beraudo committed
65 66 67 68 69
		void cleanServers();
		/**
		 * Register a state callback to be notified whenever the tunnel client is connected or disconnected to the tunnel server.
		 * @param cb application callback function to use for notifying of connection/disconnection events.
		 * @param userdata An opaque pointer passed to the callback, used optionally by the application to retrieve a context.
70
		**/
Guillaume Beraudo's avatar
Guillaume Beraudo committed
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
		void setCallback(StateCallback cb, void *userdata);
		/**
		 * Start connecting to a tunnel server.
		 * At this step, nothing is tunneled yet. The enable() method must be used to state whether SIP and RTP traffic
		 * need to be tunneled or not.
		**/
		void start();
		/**
		 * Forces reconnection to the tunnel server.
		 * This method is useful when the device switches from wifi to Edge/3G or vice versa. In most cases the tunnel client socket
		 * won't be notified promptly that its connection is now zombie, so it is recommended to call this method that will cause
		 * the lost connection to be closed and new connection to be issued.
		**/
		void reconnect();
		/**
		 * Sets whether tunneling of SIP and RTP is required.
		 * @param isEnabled If true enter in tunneled mode, if false exits from tunneled mode.
		 * The TunnelManager takes care of refreshing SIP registration when switching on or off the tunneled mode.
		 *
		**/
		void enable(bool isEnabled);
		/**
		 * In auto detect mode, the tunnel manager try to establish a real time rtp cummunication with the tunnel server on  specified port.
		 *<br>In case of success, the tunnel is automatically turned off. Otherwise, if no udp commmunication is feasible, tunnel mode is turned on.
		 *<br> Call this method each time to run the auto detection algorithm
		 */
		void autoDetect();
		/**
		 * Returns a boolean indicating whether tunneled operation is enabled.
		**/
		bool isEnabled();
		/**
		 * Enables debug logs of the Tunnel subsystem.
		**/
		void enableLogs(bool isEnabled);
		/**
		 * Enables debugs logs of the Tunnel subsystem and specify a callback where to receive the debug messages.
		**/
		void enableLogs(bool isEnabled,LogHandler logHandler);
		/**
		 * iOS only feature: specify http proxy credentials.
		 * When the iOS device has an http proxy configured in the iOS settings, the tunnel client will connect to the server
		 * through this http proxy. Credentials might be needed depending on the proxy configuration.
		 * @param username The username.
		 * @param passwd The password.
		**/
		void setHttpProxyAuthInfo(const char* username,const char* passwd);
		~TunnelManager();
		TunnelManager(LinphoneCore* lc);
		/**
		 * Destroy the given RtpTransport.
		 */
123
		void closeRtpTransport(RtpTransport *t, TunnelSocket *s);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
124 125 126 127 128 129 130 131 132 133

		/**
		 * Create an RtpTransport.
		 */
		RtpTransport *createRtpTransport(int port);

		/**
		 * Get associated Linphone Core.
		 */
		LinphoneCore *getLinphoneCore();
Simon Morlat's avatar
Simon Morlat committed
134
		virtual void setHttpProxy(const char *host,int port, const char *username, const char *passwd);
135
		virtual bool isReady() const;
Guillaume Beraudo's avatar
Guillaume Beraudo committed
136
	private:
137 138 139 140 141 142 143 144 145 146 147
		enum EventType{
			UdpMirrorClientEvent,
			TunnelEvent,
		};
		struct Event{
			EventType mType;
			union EventData{
				bool mConnected;
				bool mHaveUdp;
			}mData;
		};
Guillaume Beraudo's avatar
Guillaume Beraudo committed
148 149
		typedef std::list<UdpMirrorClient> UdpMirrorClientList;
		virtual bool isStarted();
150
		void onIterate();
Guillaume Beraudo's avatar
Guillaume Beraudo committed
151 152 153 154 155 156 157
		static int customSendto(struct _RtpTransport *t, mblk_t *msg , int flags, const struct sockaddr *to, socklen_t tolen);
		static int customRecvfrom(struct _RtpTransport *t, mblk_t *msg, int flags, struct sockaddr *from, socklen_t *fromlen);
		static int eXosipSendto(int fd,const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen,void* userdata);
		static int eXosipRecvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen,void* userdata);
		static int eXosipSelect(int nfds, fd_set *s1, fd_set *s2, fd_set *s3, struct timeval *tv,void* userdata);
		static void tunnelCallback(bool connected, TunnelManager *zis);
		static void sOnIterate(TunnelManager *zis);
158
		static void sUdpMirrorClientCallback(bool result, void* data);
159
		void registration();
Simon Morlat's avatar
Simon Morlat committed
160
		void waitUnRegistration();
161 162 163
		void processTunnelEvent(const Event &ev);
		void processUdpMirrorEvent(const Event &ev);
		void postEvent(const Event &ev);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
164 165
		LinphoneCore* mCore;
		LCSipTransports mRegularTransport;
Ghislain MARY's avatar
Ghislain MARY committed
166
#ifndef USE_BELLESIP
Guillaume Beraudo's avatar
Guillaume Beraudo committed
167 168
		TunnelSocket *mSipSocket;
		eXosip_transport_hooks_t mExosipTransport;
Ghislain MARY's avatar
Ghislain MARY committed
169
#endif
Guillaume Beraudo's avatar
Guillaume Beraudo committed
170 171 172
		StateCallback mCallback;
		void * mCallbackData;
		bool mEnabled;
173
		std::queue<Event> mEvq;
Guillaume Beraudo's avatar
Guillaume Beraudo committed
174 175 176 177 178
		std::list <ServerAddr> mServerAddrs;
		UdpMirrorClientList mUdpMirrorClients;
		UdpMirrorClientList::iterator mCurrentUdpMirrorClient;
		TunnelClient* mTunnelClient;
		void stopClient();
179
		Mutex mMutex;
Guillaume Beraudo's avatar
Guillaume Beraudo committed
180 181
		static Mutex sMutex;
		bool mAutoDetectStarted;
182
		bool mReady;
Guillaume Beraudo's avatar
Guillaume Beraudo committed
183
		LinphoneRtpTransportFactories mTransportFactories;
jehan's avatar
jehan committed
184
		std::string mHttpUserName;
Simon Morlat's avatar
Simon Morlat committed
185 186
		std::string mHttpPasswd;
		std::string mHttpProxyHost;
187
		int mHttpProxyPort;
188
		LinphoneFirewallPolicy mPreviousFirewallPolicy;
189
		bool mPreviousRegistrationEnabled;
Guillaume Beraudo's avatar
Guillaume Beraudo committed
190 191 192 193 194 195 196 197 198 199 200
	};

/**
 * @}
**/

}



#endif /*__TUNNEL_CLIENT_MANAGER_H__*/