TunnelManager.hh 6.79 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>
Simon Morlat's avatar
Simon Morlat committed
17
#include "linphonecore.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
	 * 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
	 * - provision tunnel servers ip addresses and ports
	 * - start/stop the tunneling service
36
	 * - be informed of connection and disconnection events to the tunnel server
Guillaume Beraudo's avatar
Guillaume Beraudo committed
37
	 * - 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
	 * 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.
	**/
42
	class TunnelManager {
43

Guillaume Beraudo's avatar
Guillaume Beraudo committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
	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
59
		 * @param delay udp packet round trip delay in ms considered as acceptable. recommended value is 1000 ms.
Guillaume Beraudo's avatar
Guillaume Beraudo committed
60 61 62 63
		 */
		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 70 71 72 73
		void cleanServers();
		/**
		 * 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();
		/**
74 75
		 * @brief setMode
		 * @param mode
Guillaume Beraudo's avatar
Guillaume Beraudo committed
76
		 */
77
		void setMode(LinphoneTunnelMode mode);
Guillaume Beraudo's avatar
Guillaume Beraudo committed
78
		/**
79 80 81 82
		 * @brief Return the tunnel mode
		 * @return #LinphoneTunnelMode
		 */
		LinphoneTunnelMode getMode() const;
Guillaume Beraudo's avatar
Guillaume Beraudo committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
		/**
		 * 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);
99
		void setHttpProxy(const char *host,int port, const char *username, const char *passwd);
100 101 102 103 104 105 106
		/**
		 * 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.
		 */
107 108 109 110 111 112
		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;
113 114 115 116
		/**
		 * @brief Constructor
		 * @param lc The LinphoneCore instance of which the TunnelManager will be associated to.
		 */
Guillaume Beraudo's avatar
Guillaume Beraudo committed
117 118
		TunnelManager(LinphoneCore* lc);
		/**
119
		 * @brief Destructor
Guillaume Beraudo's avatar
Guillaume Beraudo committed
120
		 */
121
		~TunnelManager();
Guillaume Beraudo's avatar
Guillaume Beraudo committed
122
		/**
123 124 125
		 * @brief Create an RtpTransport
		 * @param port
		 * @return
Guillaume Beraudo's avatar
Guillaume Beraudo committed
126 127 128
		 */
		RtpTransport *createRtpTransport(int port);
		/**
129 130 131 132 133 134 135 136
		 * @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
137
		 */
138
		LinphoneCore *getLinphoneCore() const;
139 140 141 142 143
		/**
		 * @brief Check wehter the tunnel is connected
		 * @return True whether the tunnel is connected
		 */
		bool isConnected() const;
144

145
		bool isActivated() const;
Guillaume Beraudo's avatar
Guillaume Beraudo committed
146
	private:
147 148 149 150 151 152 153
		enum State {
			disabled,
			connecting,
			ready,
			autodetecting
		};

154 155 156 157 158 159 160 161 162 163 164
		enum EventType{
			UdpMirrorClientEvent,
			TunnelEvent,
		};
		struct Event{
			EventType mType;
			union EventData{
				bool mConnected;
				bool mHaveUdp;
			}mData;
		};
Guillaume Beraudo's avatar
Guillaume Beraudo committed
165 166 167 168 169 170 171 172
		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);
		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);
173
		static void sUdpMirrorClientCallback(bool result, void* data);
174
		static void networkReachableCb(LinphoneCore *lc, bool_t reachable);
175 176 177

	private:
		void onIterate();
178
		void doRegistration();
179
		void doUnregistration();
180
		void startClient();
181
		bool startAutoDetection();
182 183 184
		void processTunnelEvent(const Event &ev);
		void processUdpMirrorEvent(const Event &ev);
		void postEvent(const Event &ev);
185 186

	private:
Guillaume Beraudo's avatar
Guillaume Beraudo committed
187
		LinphoneCore* mCore;
188
		LinphoneTunnelMode mMode;
189
		State mState;
190
		bool mTunnelizeSipPackets;
Guillaume Beraudo's avatar
Guillaume Beraudo committed
191
		TunnelClient* mTunnelClient;
jehan's avatar
jehan committed
192
		std::string mHttpUserName;
193 194
		std::string mHttpPasswd;
		std::string mHttpProxyHost;
195
		int mHttpProxyPort;
196
		LinphoneCoreVTable *mVTable;
197 198 199 200 201 202
		std::list <ServerAddr> mServerAddrs;
		UdpMirrorClientList mUdpMirrorClients;
		UdpMirrorClientList::iterator mCurrentUdpMirrorClient;
		LinphoneRtpTransportFactories mTransportFactories;
		Mutex mMutex;
		std::queue<Event> mEvq;
jehan's avatar
jehan committed
203
		char mLocalAddr[64];
Guillaume Beraudo's avatar
Guillaume Beraudo committed
204 205 206 207 208 209 210 211 212 213 214
	};

/**
 * @}
**/

}



#endif /*__TUNNEL_CLIENT_MANAGER_H__*/