big svn cleanup
[anytun.git] / src / Sockets / ISocketHandler.h
blobe32463e571ced03cd9e4b342a6191e6cc61df1ce
1 /** \file ISocketHandler.h
2 ** \date 2004-02-13
3 ** \author grymse@alhem.net
4 **/
5 /*
6 Copyright (C) 2004-2007 Anders Hedstrom
8 This library is made available under the terms of the GNU GPL.
10 If you would like to use this library in a closed-source application,
11 a separate license agreement is available. For information about
12 the closed-source license agreement for the C++ sockets library,
13 please visit http://www.alhem.net/Sockets/license.html and/or
14 email license@alhem.net.
16 This program is free software; you can redistribute it and/or
17 modify it under the terms of the GNU General Public License
18 as published by the Free Software Foundation; either version 2
19 of the License, or (at your option) any later version.
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program; if not, write to the Free Software
28 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #ifndef _SOCKETS_ISocketHandler_H
31 #define _SOCKETS_ISocketHandler_H
32 #include "sockets-config.h"
34 #include <map>
35 #include <list>
37 #include "socket_include.h"
38 #include "Socket.h"
39 #include "StdLog.h"
41 #ifdef SOCKETS_NAMESPACE
42 namespace SOCKETS_NAMESPACE {
43 #endif
45 typedef enum {
46 LIST_CALLONCONNECT = 0,
47 #ifdef ENABLE_DETACH
48 LIST_DETACH,
49 #endif
50 LIST_TIMEOUT,
51 LIST_RETRY,
52 LIST_CLOSE
53 } list_t;
55 class SocketAddress;
56 class Mutex;
59 /** Socket container class, event generator.
60 \ingroup basic */
61 class ISocketHandler
63 friend class Socket;
65 public:
66 /** Connection pool class for internal use by the ISocketHandler.
67 \ingroup internal */
68 #ifdef ENABLE_POOL
69 class PoolSocket : public Socket
71 public:
72 PoolSocket(ISocketHandler& h,Socket *src) : Socket(h) {
73 CopyConnection( src );
74 SetIsClient();
77 void OnRead() {
78 Handler().LogError(this, "OnRead", 0, "data on hibernating socket", LOG_LEVEL_FATAL);
79 SetCloseAndDelete();
81 void OnOptions(int,int,int,SOCKET) {}
84 #endif
86 public:
87 virtual ~ISocketHandler() {}
89 /** Get mutex reference for threadsafe operations. */
90 virtual Mutex& GetMutex() const = 0;
92 /** Register StdLog object for error callback.
93 \param log Pointer to log class */
94 virtual void RegStdLog(StdLog *log) = 0;
96 /** Log error to log class for print out / storage. */
97 virtual void LogError(Socket *p,const std::string& user_text,int err,const std::string& sys_err,loglevel_t t = LOG_LEVEL_WARNING) = 0;
99 // -------------------------------------------------------------------------
100 // Socket stuff
101 // -------------------------------------------------------------------------
102 /** Add socket instance to socket map. Removal is always automatic. */
103 virtual void Add(Socket *) = 0;
104 private:
105 /** Remove socket from socket map, used by Socket class. */
106 virtual void Remove(Socket *) = 0;
107 public:
108 /** Get status of read/write/exception file descriptor set for a socket. */
109 virtual void Get(SOCKET s,bool& r,bool& w,bool& e) = 0;
110 /** Set read/write/exception file descriptor sets (fd_set). */
111 virtual void Set(SOCKET s,bool bRead,bool bWrite,bool bException = true) = 0;
113 /** Wait for events, generate callbacks. */
114 virtual int Select(long sec,long usec) = 0;
115 /** This method will not return until an event has been detected. */
116 virtual int Select() = 0;
117 /** Wait for events, generate callbacks. */
118 virtual int Select(struct timeval *tsel) = 0;
120 /** Check that a socket really is handled by this socket handler. */
121 virtual bool Valid(Socket *) = 0;
122 /** Return number of sockets handled by this handler. */
123 virtual size_t GetCount() = 0;
125 /** Override and return false to deny all incoming connections.
126 \param p ListenSocket class pointer (use GetPort to identify which one) */
127 virtual bool OkToAccept(Socket *p) = 0;
129 /** Called by Socket when a socket changes state. */
130 virtual void AddList(SOCKET s,list_t which_one,bool add) = 0;
132 // -------------------------------------------------------------------------
133 // Connection pool
134 // -------------------------------------------------------------------------
135 #ifdef ENABLE_POOL
136 /** Find available open connection (used by connection pool). */
137 virtual ISocketHandler::PoolSocket *FindConnection(int type,const std::string& protocol,SocketAddress&) = 0;
138 /** Enable connection pool (by default disabled). */
139 virtual void EnablePool(bool = true) = 0;
140 /** Check pool status.
141 \return true if connection pool is enabled */
142 virtual bool PoolEnabled() = 0;
143 #endif // ENABLE_POOL
145 // -------------------------------------------------------------------------
146 // Socks4
147 // -------------------------------------------------------------------------
148 #ifdef ENABLE_SOCKS4
149 /** Set socks4 server ip that all new tcp sockets should use. */
150 virtual void SetSocks4Host(ipaddr_t) = 0;
151 /** Set socks4 server hostname that all new tcp sockets should use. */
152 virtual void SetSocks4Host(const std::string& ) = 0;
153 /** Set socks4 server port number that all new tcp sockets should use. */
154 virtual void SetSocks4Port(port_t) = 0;
155 /** Set optional socks4 userid. */
156 virtual void SetSocks4Userid(const std::string& ) = 0;
157 /** If connection to socks4 server fails, immediately try direct connection to final host. */
158 virtual void SetSocks4TryDirect(bool = true) = 0;
159 /** Get socks4 server ip.
160 \return socks4 server ip */
161 virtual ipaddr_t GetSocks4Host() = 0;
162 /** Get socks4 port number.
163 \return socks4 port number */
164 virtual port_t GetSocks4Port() = 0;
165 /** Get socks4 userid (optional).
166 \return socks4 userid */
167 virtual const std::string& GetSocks4Userid() = 0;
168 /** Check status of socks4 try direct flag.
169 \return true if direct connection should be tried if connection to socks4 server fails */
170 virtual bool Socks4TryDirect() = 0;
171 #endif // ENABLE_SOCKS4
173 // -------------------------------------------------------------------------
174 // DNS resolve server
175 // -------------------------------------------------------------------------
176 #ifdef ENABLE_RESOLVER
177 /** Enable asynchronous DNS.
178 \param port Listen port of asynchronous dns server */
179 virtual void EnableResolver(port_t = 16667) = 0;
180 /** Check resolver status.
181 \return true if resolver is enabled */
182 virtual bool ResolverEnabled() = 0;
183 /** Queue a dns request.
184 \param host Hostname to be resolved
185 \param port Port number will be echoed in Socket::OnResolved callback */
186 virtual int Resolve(Socket *,const std::string& host,port_t port) = 0;
187 #ifdef ENABLE_IPV6
188 virtual int Resolve6(Socket *,const std::string& host,port_t port) = 0;
189 #endif
190 /** Do a reverse dns lookup. */
191 virtual int Resolve(Socket *,ipaddr_t a) = 0;
192 #ifdef ENABLE_IPV6
193 virtual int Resolve(Socket *,in6_addr& a) = 0;
194 #endif
195 /** Get listen port of asynchronous dns server. */
196 virtual port_t GetResolverPort() = 0;
197 /** Resolver thread ready for queries. */
198 virtual bool ResolverReady() = 0;
199 /** Returns true if socket waiting for a resolve event. */
200 virtual bool Resolving(Socket *) = 0;
201 #endif // ENABLE_RESOLVER
203 #ifdef ENABLE_TRIGGERS
204 /** Fetch unique trigger id. */
205 virtual int TriggerID(Socket *src) = 0;
206 /** Subscribe socket to trigger id. */
207 virtual bool Subscribe(int id, Socket *dst) = 0;
208 /** Unsubscribe socket from trigger id. */
209 virtual bool Unsubscribe(int id, Socket *dst) = 0;
210 /** Execute OnTrigger for subscribed sockets.
211 \param id Trigger ID
212 \param data Data passed from source to destination
213 \param erase Empty trigger id source and destination maps if 'true',
214 Leave them in place if 'false' - if a trigger should be called many times */
215 virtual void Trigger(int id, Socket::TriggerData& data, bool erase = true) = 0;
216 #endif // ENABLE_TRIGGERS
218 #ifdef ENABLE_DETACH
219 /** Indicates that the handler runs under SocketThread. */
220 virtual void SetSlave(bool x = true) = 0;
221 /** Indicates that the handler runs under SocketThread. */
222 virtual bool IsSlave() = 0;
223 #endif // ENABLE_DETACH
228 #ifdef SOCKETS_NAMESPACE
230 #endif
232 #endif // _SOCKETS_ISocketHandler_H