TunnelManager.hh 9.07 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>
15 16
#include <tunnel/client.hh>
#include <tunnel/udp_mirror.hh>
17
#include "linphone/core.h"
18
#include "linphone/tunnel.h"
Simon Morlat's avatar
Simon Morlat committed
19

20
#ifndef USE_BELLESIP
Guillaume Beraudo's avatar
Guillaume Beraudo committed
21
extern "C" {
22
	#include <eXosip2/eXosip_transport_hook.h>
Guillaume Beraudo's avatar
Guillaume Beraudo committed
23
}
24 25
#endif

Guillaume Beraudo's avatar
Guillaume Beraudo committed
26 27
namespace belledonnecomm {
/**
28
 * @addtogroup tunnel_client
Guillaume Beraudo's avatar
Guillaume Beraudo committed
29 30
 * @{
**/
31 32 33 34 35
	struct DualSocket {
		TunnelSocket *sendSocket;
		TunnelSocket *recvSocket;
	};
		
Guillaume Beraudo's avatar
Guillaume Beraudo committed
36
	/**
37
	 * The TunnelManager class extends the LinphoneCore functionnality in order to provide an easy to use API to
Guillaume Beraudo's avatar
Guillaume Beraudo committed
38 39
	 * - provision tunnel servers ip addresses and ports
	 * - start/stop the tunneling service
40
	 * - be informed of connection and disconnection events to the tunnel server
Guillaume Beraudo's avatar
Guillaume Beraudo committed
41
	 * - perform auto-detection whether tunneling is required, based on a test of sending/receiving a flow of UDP packets.
42
	 *
Guillaume Beraudo's avatar
Guillaume Beraudo committed
43 44 45
	 * 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.
	**/
46
	class TunnelManager {
47

Guillaume Beraudo's avatar
Guillaume Beraudo committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
	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
63
		 * @param delay udp packet round trip delay in ms considered as acceptable. recommended value is 1000 ms.
Guillaume Beraudo's avatar
Guillaume Beraudo committed
64 65
		 */
		void addServer(const char *ip, int port,unsigned int udpMirrorPort,unsigned int delay);
66 67 68 69 70 71 72 73 74 75 76 77
		/**
		 * Add a tunnel server couple. At least one should be provided to be able to connect.
		 * This is used when using the dual socket mode where one client will connect to one ip and the other client to the other ip.
		 *
		 * @param ip1 server ip address n°1
		 * @param port1 tunnel server tls port, recommended value is 443
		 * @param ip2 server ip address n°2
		 * @param port2 tunnel server tls port, recommended value is 443
		 * @param udpMirrorPort remote port on the tunnel server 1 side  used to test udp reachability
		 * @param delay udp packet round trip delay in ms considered as acceptable. recommended value is 1000 ms.
		 */
		void addServerPair(const char *ip1, int port1, const char *ip2, int port2, unsigned int udpMirrorPort, unsigned int delay);
78 79 80 81 82 83 84 85 86 87
		/**
		 * Add a tunnel server couple. At least one should be provided to be able to connect.
		 * This is used when using the dual socket mode where one client will connect to one ip and the other client to the other ip.
		 *
		 * @param ip1 server ip address n°1
		 * @param port1 tunnel server tls port, recommended value is 443
		 * @param ip2 server ip address n°2
		 * @param port2 tunnel server tls port, recommended value is 443
		 */
		void addServerPair(const char *ip1, int port1, const char *ip2, int port2);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
88 89
		/**
		 * Removes all tunnel server address previously entered with addServer()
90
		**/
Guillaume Beraudo's avatar
Guillaume Beraudo committed
91
		void cleanServers();
92 93 94 95 96 97 98 99 100 101 102 103
		
		/**
		 * Enables the dual socket mode. In this mode, we have to configure pairs or ServerAddr
		 * 2 TunneClient will be used, one for each IP and each one will only either send or receive the data stream.
		 * @param enable true to enable the DualMode, false otherwise
		 */
		void enableDualMode(bool enable);
		/**
		 * Returns whether or not the DualMode is enabled
		 * @return true if it is enabled, false otherwise
		 */
		bool isDualModeEnabled();
Guillaume Beraudo's avatar
Guillaume Beraudo committed
104 105 106 107 108 109 110 111
		/**
		 * 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();
		/**
112 113
		 * @brief setMode
		 * @param mode
Guillaume Beraudo's avatar
Guillaume Beraudo committed
114
		 */
115
		void setMode(LinphoneTunnelMode mode);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
116
		/**
117 118 119 120
		 * @brief Return the tunnel mode
		 * @return #LinphoneTunnelMode
		 */
		LinphoneTunnelMode getMode() const;
Guillaume Beraudo's avatar
Guillaume Beraudo committed
121 122 123 124 125 126 127 128
		/**
		 * 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);
129
		void setHttpProxy(const char *host,int port, const char *username, const char *passwd);
130 131 132 133 134 135 136
		/**
		 * Indicate to the tunnel manager whether SIP packets must pass
		 * through the tunnel. That featurte is automatically enabled at
		 * the creation of the TunnelManager instance.
		 * @param enable If set to TRUE, SIP packets will pass through the tunnel.
		 * If set to FALSE, SIP packets will pass by the configured proxies.
		 */
137 138 139 140 141 142
		void tunnelizeSipPackets(bool enable);
		/**
		 * @brief Check whether the tunnel manager is set to tunnelize SIP packets
		 * @return True, SIP packets pass through the tunnel
		 */
		bool tunnelizeSipPacketsEnabled() const;
143 144 145 146 147 148 149 150 151 152 153 154
		/**
		 * Indicate to the tunnel manager wether server certificate
		 * must be verified during TLS handshake. Default: disabled
		 * @param enable If set to TRUE, SIP packets will pass through the tunnel.
		 * If set to FALSE, SIP packets will pass by the configured proxies.
		 */
		void verifyServerCertificate(bool enable);
		/**
		 * Check wether the tunnel manager is set to verify server certificate during TLS handshake
		 * @return True, server certificate is verified(using the linphonecore root certificate)
		 */
		bool verifyServerCertificateEnabled() const;
155 156 157 158
		/**
		 * @brief Constructor
		 * @param lc The LinphoneCore instance of which the TunnelManager will be associated to.
		 */
Guillaume Beraudo's avatar
Guillaume Beraudo committed
159 160
		TunnelManager(LinphoneCore* lc);
		/**
161
		 * @brief Destructor
Guillaume Beraudo's avatar
Guillaume Beraudo committed
162
		 */
163
		~TunnelManager();
Guillaume Beraudo's avatar
Guillaume Beraudo committed
164
		/**
165 166 167
		 * @brief Create an RtpTransport
		 * @param port
		 * @return
Guillaume Beraudo's avatar
Guillaume Beraudo committed
168 169 170
		 */
		RtpTransport *createRtpTransport(int port);
		/**
171 172 173 174 175 176 177 178
		 * @brief Destroy the given RtpTransport
		 * @param t
		 * @param s
		 */
		void closeRtpTransport(RtpTransport *t, TunnelSocket *s);
		/**
		 * @brief Get associated Linphone Core
		 * @return pointer on the associated LinphoneCore
Guillaume Beraudo's avatar
Guillaume Beraudo committed
179
		 */
180
		LinphoneCore *getLinphoneCore() const;
181 182 183 184 185
		/**
		 * @brief Check wehter the tunnel is connected
		 * @return True whether the tunnel is connected
		 */
		bool isConnected() const;
186

187
		bool isActivated() const;
188 189 190

		void simulateUdpLoss(bool enabled);

Guillaume Beraudo's avatar
Guillaume Beraudo committed
191
	private:
192 193 194 195 196 197 198 199 200 201 202
		enum EventType{
			UdpMirrorClientEvent,
			TunnelEvent,
		};
		struct Event{
			EventType mType;
			union EventData{
				bool mConnected;
				bool mHaveUdp;
			}mData;
		};
Guillaume Beraudo's avatar
Guillaume Beraudo committed
203 204 205
		typedef std::list<UdpMirrorClient> UdpMirrorClientList;
		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);
206
		static void tunnelCallback(bool connected, void *zis);
207
		static void tunnelCallback2(TunnelDirection direction, bool connected, void *zis);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
208
		static void sOnIterate(TunnelManager *zis);
209
		static void sUdpMirrorClientCallback(bool result, void* data);
210
		static void networkReachableCb(LinphoneCore *lc, bool_t reachable);
211 212

	private:
213 214 215 216
		enum State{
			Off, /*no tunneling */
			On /*tunneling activated*/
		};
217
		void onIterate();
218
		void doRegistration();
219
		void doUnregistration();
220
		void startClient();
221
		bool startAutoDetection();
222 223 224
		void processTunnelEvent(const Event &ev);
		void processUdpMirrorEvent(const Event &ev);
		void postEvent(const Event &ev);
225 226 227 228 229 230 231 232
		void stopClient();
		void stopAutoDetection();
		void stopLongRunningTask();
		void applyMode();
		void setState(State state);
		void applyState();
		void tunnelizeLiblinphone();
		void untunnelizeLiblinphone();
233
	private:
234
		
Guillaume Beraudo's avatar
Guillaume Beraudo committed
235
		LinphoneCore* mCore;
236
		LinphoneTunnelMode mMode;
237
		TunnelClientI* mTunnelClient;
jehan's avatar
jehan committed
238
		std::string mHttpUserName;
239 240
		std::string mHttpPasswd;
		std::string mHttpProxyHost;
241
		int mHttpProxyPort;
242
		LinphoneCoreVTable *mVTable;
243 244
		std::list<ServerAddr> mServerAddrs;
		std::list<DualServerAddr> mDualServerAddrs;
245 246 247 248 249
		UdpMirrorClientList mUdpMirrorClients;
		UdpMirrorClientList::iterator mCurrentUdpMirrorClient;
		LinphoneRtpTransportFactories mTransportFactories;
		Mutex mMutex;
		std::queue<Event> mEvq;
jehan's avatar
jehan committed
250
		char mLocalAddr[64];
251
		unsigned long mLongRunningTaskId;
252 253 254 255 256 257
		State mTargetState;
		State mState;
		bool mVerifyServerCertificate;
		bool mStarted;
		bool mAutodetectionRunning;
		bool mTunnelizeSipPackets;
258
		bool mSimulateUdpLoss;
259
		bool mUseDualClient;
Guillaume Beraudo's avatar
Guillaume Beraudo committed
260 261 262 263 264 265 266 267 268 269 270
	};

/**
 * @}
**/

}



#endif /*__TUNNEL_CLIENT_MANAGER_H__*/