2 * C Implementation: tunnel
8 *Copyright (C) 2011 Belledonne Comunications, Grenoble, France
11 #ifndef __TUNNEL_CLIENT_MANAGER_H__
12 #define __TUNNEL_CLIENT_MANAGER_H__
14 #include "linphonecore.h"
15 #include "tunnel/client.hh"
17 #include "eXosip2/eXosip_transport_hook.h"
19 namespace belledonnecomm {
21 class UdpMirrorClient;
23 * @addtogroup tunnel_client
28 * The TunnelManager class extends the LinphoneCore functionnality in order to provide an easy to use API to
29 * - provision tunnel servers ip addresses and ports
30 * - start/stop the tunneling service
31 * - be informed of of connection and disconnection events to the tunnel server
32 * - perform auto-detection whether tunneling is required, based on a test of sending/receiving a flow of UDP packets.
34 * It takes in charge automatically the SIP registration procedure when connecting or disconnecting to a tunnel server.
35 * No other action on LinphoneCore is required to enable full operation in tunnel mode.
37 class TunnelManager : public TunnelClientController{
41 * Add a tunnel server. At least one should be provided to be able to connect.
42 * When several addresses are provided, the tunnel client may try each of them until it gets connected.
44 * @param ip tunnMethod definition for '-isInitialStateOn' not foundel server ip address
45 * @param port tunnel server tls port, recommended value is 443
47 void addServer(const char *ip, int port);
49 *Add tunnel server with auto detection capabilities
51 * @param ip tunnel server ip address
52 * @param port tunnel server tls port, recommended value is 443
53 * @param udpMirrorPort remote port on the tunnel server side used to test udp reachability
54 * @param delay udp packet round trip delay in ms considered as acceptable. recommanded value is 1000 ms.
56 void addServer(const char *ip, int port,unsigned int udpMirrorPort,unsigned int delay);
58 * Removes all tunnel server address previously entered with addServer()
62 * Register a state callback to be notified whenever the tunnel client is connected or disconnected to the tunnel server.
63 * @param cb application callback function to use for notifying of connection/disconnection events.
64 * @param userdata An opaque pointer passed to the callback, used optionally by the application to retrieve a context.
66 void setCallback(StateCallback cb, void *userdata);
68 * Start connecting to a tunnel server.
69 * At this step, nothing is tunneled yet. The enable() method must be used to state whether SIP and RTP traffic
70 * need to be tunneled or not.
74 * Forces reconnection to the tunnel server.
75 * This method is useful when the device switches from wifi to Edge/3G or vice versa. In most cases the tunnel client socket
76 * won't be notified promptly that its connection is now zombie, so it is recommended to call this method that will cause
77 * the lost connection to be closed and new connection to be issued.
81 * Sets whether tunneling of SIP and RTP is required.
82 * @param isEnabled If true enter in tunneled mode, if false exits from tunneled mode.
83 * The TunnelManager takes care of refreshing SIP registration when switching on or off the tunneled mode.
86 void enable(bool isEnabled);
88 * In auto detect mode, the tunnel manager try to establish a real time rtp cummunication with the tunnel server on specified port.
89 *<br>In case of success, the tunnel is automatically turned off. Otherwise, if no udp commmunication is feasible, tunnel mode is turned on.
90 *<br> Call this method each time to run the auto detection algorithm
94 * Returns a boolean indicating whether tunneled operation is enabled.
98 * Enables debug logs of the Tunnel subsystem.
100 void enableLogs(bool isEnabled);
102 * Enables debugs logs of the Tunnel subsystem and specify a callback where to receive the debug messages.
104 void enableLogs(bool isEnabled,LogHandler logHandler);
106 * iOS only feature: specify http proxy credentials.
107 * When the iOS device has an http proxy configured in the iOS settings, the tunnel client will connect to the server
108 * through this http proxy. Credentials might be needed depending on the proxy configuration.
109 * @param username The username.
110 * @param passwd The password.
112 void setHttpProxyAuthInfo(const char* username,const char* passwd);
114 TunnelManager(LinphoneCore* lc);
116 * Destroy the given RtpTransport.
118 void closeRtpTransport(RtpTransport *t, TunnelSocket *s);
121 * Create an RtpTransport.
123 RtpTransport *createRtpTransport(int port);
126 * Get associated Linphone Core.
128 LinphoneCore *getLinphoneCore();
130 typedef std::list<UdpMirrorClient> UdpMirrorClientList;
131 virtual bool isStarted();
132 virtual bool isReady() const;
133 static int customSendto(struct _RtpTransport *t, mblk_t *msg , int flags, const struct sockaddr *to, socklen_t tolen);
134 static int customRecvfrom(struct _RtpTransport *t, mblk_t *msg, int flags, struct sockaddr *from, socklen_t *fromlen);
135 static int eXosipSendto(int fd,const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen,void* userdata);
136 static int eXosipRecvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr *from, socklen_t *fromlen,void* userdata);
137 static int eXosipSelect(int nfds, fd_set *s1, fd_set *s2, fd_set *s3, struct timeval *tv,void* userdata);
138 static void tunnelCallback(bool connected, TunnelManager *zis);
139 static void sOnIterate(TunnelManager *zis);
140 static void UdpMirrorClientListener(bool result, void* data);
142 void processTunnelEvent();
144 LCSipTransports mRegularTransport;
145 TunnelSocket *mSipSocket;
146 eXosip_transport_hooks_t mExosipTransport;
147 StateCallback mCallback;
148 void * mCallbackData;
151 std::list <ServerAddr> mServerAddrs;
152 UdpMirrorClientList mUdpMirrorClients;
153 UdpMirrorClientList::iterator mCurrentUdpMirrorClient;
154 TunnelClient* mTunnelClient;
157 bool mAutoDetectStarted;
158 LinphoneRtpTransportFactories mTransportFactories;
169 #endif /*__TUNNEL_CLIENT_MANAGER_H__*/