2 * C Implementation: tunnel
8 *Copyright (C) 2011 Belledonne Comunications, Grenoble, France
11 #ifndef __TUNNEL_CLIENT_MANAGER_H__
12 #define __TUNNEL_CLIENT_MANAGER_H__
15 #include "tunnel/client.hh"
16 #include "linphonecore.h"
19 #include "eXosip2/eXosip_transport_hook.h"
21 namespace belledonnecomm {
23 class UdpMirrorClient;
25 * @addtogroup tunnel_client
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.
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.
39 class TunnelManager : public TunnelClientController{
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.
46 * @param ip tunnMethod definition for '-isInitialStateOn' not foundel server ip address
47 * @param port tunnel server tls port, recommended value is 443
49 void addServer(const char *ip, int port);
51 *Add tunnel server with auto detection capabilities
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.
58 void addServer(const char *ip, int port,unsigned int udpMirrorPort,unsigned int delay);
60 * Removes all tunnel server address previously entered with addServer()
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.
68 void setCallback(StateCallback cb, void *userdata);
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.
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.
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.
88 void enable(bool isEnabled);
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
96 * Returns a boolean indicating whether tunneled operation is enabled.
100 * Enables debug logs of the Tunnel subsystem.
102 void enableLogs(bool isEnabled);
104 * Enables debugs logs of the Tunnel subsystem and specify a callback where to receive the debug messages.
106 void enableLogs(bool isEnabled,LogHandler logHandler);
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.
114 void setHttpProxyAuthInfo(const char* username,const char* passwd);
116 TunnelManager(LinphoneCore* lc);
118 * Destroy the given RtpTransport.
120 void closeRtpTransport(RtpTransport *t, TunnelSocket *s);
123 * Create an RtpTransport.
125 RtpTransport *createRtpTransport(int port);
128 * Get associated Linphone Core.
130 LinphoneCore *getLinphoneCore();
131 virtual void setHttpProxy(const char *host,int port, const char *username, const char *passwd);
132 virtual bool isReady() const;
135 UdpMirrorClientEvent,
145 typedef std::list<UdpMirrorClient> UdpMirrorClientList;
146 virtual bool isStarted();
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);
161 LCSipTransports mRegularTransport;
162 TunnelSocket *mSipSocket;
163 eXosip_transport_hooks_t mExosipTransport;
164 StateCallback mCallback;
165 void * mCallbackData;
167 std::queue<Event> mEvq;
168 std::list <ServerAddr> mServerAddrs;
169 UdpMirrorClientList mUdpMirrorClients;
170 UdpMirrorClientList::iterator mCurrentUdpMirrorClient;
171 TunnelClient* mTunnelClient;
175 bool mAutoDetectStarted;
177 LinphoneRtpTransportFactories mTransportFactories;
178 std::string mHttpUserName;
179 std::string mHttpPasswd;
180 std::string mHttpProxyHost;
182 LinphoneFirewallPolicy mPreviousFirewallPolicy;
193 #endif /*__TUNNEL_CLIENT_MANAGER_H__*/