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 "linphonecore.h"
16 #include "tunnel/client.hh"
18 #include "eXosip2/eXosip_transport_hook.h"
20 namespace belledonnecomm {
22 class UdpMirrorClient;
24 * @addtogroup tunnel_client
29 * The TunnelManager class extends the LinphoneCore functionnality in order to provide an easy to use API to
30 * - provision tunnel servers ip addresses and ports
31 * - start/stop the tunneling service
32 * - be informed of of connection and disconnection events to the tunnel server
33 * - perform auto-detection whether tunneling is required, based on a test of sending/receiving a flow of UDP packets.
35 * It takes in charge automatically the SIP registration procedure when connecting or disconnecting to a tunnel server.
36 * No other action on LinphoneCore is required to enable full operation in tunnel mode.
38 class TunnelManager : public TunnelClientController{
42 * Add a tunnel server. At least one should be provided to be able to connect.
43 * When several addresses are provided, the tunnel client may try each of them until it gets connected.
45 * @param ip tunnMethod definition for '-isInitialStateOn' not foundel server ip address
46 * @param port tunnel server tls port, recommended value is 443
48 void addServer(const char *ip, int port);
50 *Add tunnel server with auto detection capabilities
52 * @param ip tunnel server ip address
53 * @param port tunnel server tls port, recommended value is 443
54 * @param udpMirrorPort remote port on the tunnel server side used to test udp reachability
55 * @param delay udp packet round trip delay in ms considered as acceptable. recommanded value is 1000 ms.
57 void addServer(const char *ip, int port,unsigned int udpMirrorPort,unsigned int delay);
59 * Removes all tunnel server address previously entered with addServer()
63 * Register a state callback to be notified whenever the tunnel client is connected or disconnected to the tunnel server.
64 * @param cb application callback function to use for notifying of connection/disconnection events.
65 * @param userdata An opaque pointer passed to the callback, used optionally by the application to retrieve a context.
67 void setCallback(StateCallback cb, void *userdata);
69 * Start connecting to a tunnel server.
70 * At this step, nothing is tunneled yet. The enable() method must be used to state whether SIP and RTP traffic
71 * need to be tunneled or not.
75 * Forces reconnection to the tunnel server.
76 * This method is useful when the device switches from wifi to Edge/3G or vice versa. In most cases the tunnel client socket
77 * won't be notified promptly that its connection is now zombie, so it is recommended to call this method that will cause
78 * the lost connection to be closed and new connection to be issued.
82 * Sets whether tunneling of SIP and RTP is required.
83 * @param isEnabled If true enter in tunneled mode, if false exits from tunneled mode.
84 * The TunnelManager takes care of refreshing SIP registration when switching on or off the tunneled mode.
87 void enable(bool isEnabled);
89 * In auto detect mode, the tunnel manager try to establish a real time rtp cummunication with the tunnel server on specified port.
90 *<br>In case of success, the tunnel is automatically turned off. Otherwise, if no udp commmunication is feasible, tunnel mode is turned on.
91 *<br> Call this method each time to run the auto detection algorithm
95 * Returns a boolean indicating whether tunneled operation is enabled.
99 * Enables debug logs of the Tunnel subsystem.
101 void enableLogs(bool isEnabled);
103 * Enables debugs logs of the Tunnel subsystem and specify a callback where to receive the debug messages.
105 void enableLogs(bool isEnabled,LogHandler logHandler);
107 * iOS only feature: specify http proxy credentials.
108 * When the iOS device has an http proxy configured in the iOS settings, the tunnel client will connect to the server
109 * through this http proxy. Credentials might be needed depending on the proxy configuration.
110 * @param username The username.
111 * @param passwd The password.
113 void setHttpProxyAuthInfo(const char* username,const char* passwd);
115 TunnelManager(LinphoneCore* lc);
117 * Destroy the given RtpTransport.
119 void closeRtpTransport(RtpTransport *t, TunnelSocket *s);
122 * Create an RtpTransport.
124 RtpTransport *createRtpTransport(int port);
127 * Get associated Linphone Core.
129 LinphoneCore *getLinphoneCore();
131 typedef std::list<UdpMirrorClient> UdpMirrorClientList;
132 virtual bool isStarted();
133 virtual bool isReady() const;
134 static int customSendto(struct _RtpTransport *t, mblk_t *msg , int flags, const struct sockaddr *to, socklen_t tolen);
135 static int customRecvfrom(struct _RtpTransport *t, mblk_t *msg, int flags, struct sockaddr *from, socklen_t *fromlen);
136 static int eXosipSendto(int fd,const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen,void* userdata);
137 static int eXosipRecvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen,void* userdata);
138 static int eXosipSelect(int nfds, fd_set *s1, fd_set *s2, fd_set *s3, struct timeval *tv,void* userdata);
139 static void tunnelCallback(bool connected, TunnelManager *zis);
140 static void sOnIterate(TunnelManager *zis);
141 static void UdpMirrorClientListener(bool result, void* data);
143 void processTunnelEvent();
145 LCSipTransports mRegularTransport;
146 TunnelSocket *mSipSocket;
147 eXosip_transport_hooks_t mExosipTransport;
148 StateCallback mCallback;
149 void * mCallbackData;
152 std::list <ServerAddr> mServerAddrs;
153 UdpMirrorClientList mUdpMirrorClients;
154 UdpMirrorClientList::iterator mCurrentUdpMirrorClient;
155 TunnelClient* mTunnelClient;
158 bool mAutoDetectStarted;
159 LinphoneRtpTransportFactories mTransportFactories;
160 std::string mHttpUserName;
161 std::string mHttpPasswd;
172 #endif /*__TUNNEL_CLIENT_MANAGER_H__*/