1 | /** \file Socket.h |
---|
2 | ** \date 2004-02-13 |
---|
3 | ** \author grymse@alhem.net |
---|
4 | **/ |
---|
5 | /* |
---|
6 | Copyright (C) 2004-2007 Anders Hedstrom |
---|
7 | |
---|
8 | This software is made available under the terms of the GNU GPL. |
---|
9 | |
---|
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. |
---|
15 | |
---|
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. |
---|
20 | |
---|
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. |
---|
25 | |
---|
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. |
---|
29 | */ |
---|
30 | #ifndef _SOCKETS_Socket_H |
---|
31 | #define _SOCKETS_Socket_H |
---|
32 | #include "sockets-config.h" |
---|
33 | |
---|
34 | #include <string> |
---|
35 | #include <vector> |
---|
36 | #include <list> |
---|
37 | #ifdef HAVE_OPENSSL |
---|
38 | #include <openssl/ssl.h> |
---|
39 | #endif |
---|
40 | |
---|
41 | #include "socket_include.h" |
---|
42 | #include <time.h> |
---|
43 | #include "SocketAddress.h" |
---|
44 | #include "Thread.h" |
---|
45 | |
---|
46 | |
---|
47 | #ifdef SOCKETS_NAMESPACE |
---|
48 | namespace SOCKETS_NAMESPACE { |
---|
49 | #endif |
---|
50 | |
---|
51 | |
---|
52 | class ISocketHandler; |
---|
53 | class SocketAddress; |
---|
54 | class IFile; |
---|
55 | |
---|
56 | |
---|
57 | /** \defgroup basic Basic sockets */ |
---|
58 | /** Socket base class. |
---|
59 | \ingroup basic */ |
---|
60 | class Socket |
---|
61 | { |
---|
62 | friend class ISocketHandler; |
---|
63 | #ifdef ENABLE_DETACH |
---|
64 | /** Detached socket run thread. |
---|
65 | \ingroup internal */ |
---|
66 | class SocketThread : public Thread |
---|
67 | { |
---|
68 | public: |
---|
69 | SocketThread(Socket *p); |
---|
70 | ~SocketThread(); |
---|
71 | |
---|
72 | void Run(); |
---|
73 | |
---|
74 | private: |
---|
75 | Socket *GetSocket() const { return m_socket; } |
---|
76 | SocketThread(const SocketThread& s) : m_socket(s.GetSocket()) {} |
---|
77 | SocketThread& operator=(const SocketThread& ) { return *this; } |
---|
78 | Socket *m_socket; |
---|
79 | }; |
---|
80 | #endif // ENABLE_DETACH |
---|
81 | |
---|
82 | #ifdef ENABLE_TRIGGERS |
---|
83 | public: |
---|
84 | /** Data pass class from source to destination. */ |
---|
85 | class TriggerData |
---|
86 | { |
---|
87 | public: |
---|
88 | TriggerData() : m_src(NULL) {} |
---|
89 | virtual ~TriggerData() {} |
---|
90 | |
---|
91 | Socket *GetSource() const { return m_src; } |
---|
92 | void SetSource(Socket *x) { m_src = x; } |
---|
93 | |
---|
94 | private: |
---|
95 | Socket *m_src; |
---|
96 | }; |
---|
97 | #endif // ENABLE_TRIGGERS |
---|
98 | |
---|
99 | /** Socket mode flags. */ |
---|
100 | /* |
---|
101 | enum { |
---|
102 | // Socket |
---|
103 | SOCK_DEL = 0x01, ///< Delete by handler flag |
---|
104 | SOCK_CLOSE = 0x02, ///< Close and delete flag |
---|
105 | SOCK_DISABLE_READ = 0x04, ///< Disable checking for read events |
---|
106 | SOCK_CONNECTED = 0x08, ///< Socket is connected (tcp/udp) |
---|
107 | |
---|
108 | SOCK_ERASED_BY_HANDLER = 0x10, ///< Set by handler before delete |
---|
109 | // HAVE_OPENSSL |
---|
110 | SOCK_ENABLE_SSL = 0x20, ///< Enable SSL for this TcpSocket |
---|
111 | SOCK_SSL = 0x40, ///< ssl negotiation mode (TcpSocket) |
---|
112 | SOCK_SSL_SERVER = 0x80, ///< True if this is an incoming ssl TcpSocket connection |
---|
113 | |
---|
114 | // ENABLE_IPV6 |
---|
115 | SOCK_IPV6 = 0x0100, ///< This is an ipv6 socket if this one is true |
---|
116 | // ENABLE_POOL |
---|
117 | SOCK_CLIENT = 0x0200, ///< only client connections are pooled |
---|
118 | SOCK_RETAIN = 0x0400, ///< keep connection on close |
---|
119 | SOCK_LOST = 0x0800, ///< connection lost |
---|
120 | |
---|
121 | // ENABLE_SOCKS4 |
---|
122 | SOCK_SOCKS4 = 0x1000, ///< socks4 negotiation mode (TcpSocket) |
---|
123 | // ENABLE_DETACH |
---|
124 | SOCK_DETACH = 0x2000, ///< Socket ordered to detach flag |
---|
125 | SOCK_DETACHED = 0x4000, ///< Socket has been detached |
---|
126 | // StreamSocket |
---|
127 | STREAMSOCK_CONNECTING = 0x8000, ///< Flag indicating connection in progress |
---|
128 | |
---|
129 | STREAMSOCK_FLUSH_BEFORE_CLOSE = 0x010000L, ///< Send all data before closing (default true) |
---|
130 | STREAMSOCK_CALL_ON_CONNECT = 0x020000L, ///< OnConnect will be called next ISocketHandler cycle if true |
---|
131 | STREAMSOCK_RETRY_CONNECT = 0x040000L, ///< Try another connection attempt next ISocketHandler cycle |
---|
132 | STREAMSOCK_LINE_PROTOCOL = 0x080000L, ///< Line protocol mode flag |
---|
133 | |
---|
134 | }; |
---|
135 | */ |
---|
136 | |
---|
137 | public: |
---|
138 | /** "Default" constructor */ |
---|
139 | Socket(ISocketHandler&); |
---|
140 | |
---|
141 | virtual ~Socket(); |
---|
142 | |
---|
143 | /** Socket class instantiation method. Used when a "non-standard" constructor |
---|
144 | * needs to be used for the socket class. Note: the socket class still needs |
---|
145 | * the "default" constructor with one ISocketHandler& as input parameter. |
---|
146 | */ |
---|
147 | virtual Socket *Create() { return NULL; } |
---|
148 | |
---|
149 | /** Returns reference to sockethandler that owns the socket. |
---|
150 | If the socket is detached, this is a reference to the slave sockethandler. |
---|
151 | */ |
---|
152 | ISocketHandler& Handler() const; |
---|
153 | |
---|
154 | /** Returns reference to sockethandler that owns the socket. |
---|
155 | This one always returns the reference to the original sockethandler, |
---|
156 | even if the socket is detached. |
---|
157 | */ |
---|
158 | ISocketHandler& MasterHandler() const; |
---|
159 | |
---|
160 | /** Called by ListenSocket after accept but before socket is added to handler. |
---|
161 | * CTcpSocket uses this to create its ICrypt member variable. |
---|
162 | * The ICrypt member variable is created by a virtual method, therefore |
---|
163 | * it can't be called directly from the CTcpSocket constructor. |
---|
164 | * Also used to determine if incoming HTTP connection is normal (port 80) |
---|
165 | * or ssl (port 443). |
---|
166 | */ |
---|
167 | virtual void Init(); |
---|
168 | |
---|
169 | /** Create a socket file descriptor. |
---|
170 | \param af Address family AF_INET / AF_INET6 / ... |
---|
171 | \param type SOCK_STREAM / SOCK_DGRAM / ... |
---|
172 | \param protocol "tcp" / "udp" / ... */ |
---|
173 | SOCKET CreateSocket(int af,int type,const std::string& protocol = ""); |
---|
174 | |
---|
175 | /** Assign this socket a file descriptor created |
---|
176 | by a call to socket() or otherwise. */ |
---|
177 | void Attach(SOCKET s); |
---|
178 | |
---|
179 | /** Return file descriptor assigned to this socket. */ |
---|
180 | SOCKET GetSocket(); |
---|
181 | |
---|
182 | /** Close connection immediately - internal use. |
---|
183 | \sa SetCloseAndDelete */ |
---|
184 | virtual int Close(); |
---|
185 | |
---|
186 | /** Add file descriptor to sockethandler fd_set's. */ |
---|
187 | void Set(bool bRead,bool bWrite,bool bException = true); |
---|
188 | |
---|
189 | /** Returns true when socket file descriptor is valid |
---|
190 | and socket is not about to be closed. */ |
---|
191 | virtual bool Ready(); |
---|
192 | |
---|
193 | /** Returns pointer to ListenSocket that created this instance |
---|
194 | * on an incoming connection. */ |
---|
195 | Socket *GetParent(); |
---|
196 | |
---|
197 | /** Used by ListenSocket to set parent pointer of newly created |
---|
198 | * socket instance. */ |
---|
199 | void SetParent(Socket *); |
---|
200 | |
---|
201 | /** Get listening port from ListenSocket<>. */ |
---|
202 | virtual port_t GetPort(); |
---|
203 | |
---|
204 | /** Set socket non-block operation. */ |
---|
205 | bool SetNonblocking(bool); |
---|
206 | |
---|
207 | /** Set socket non-block operation. */ |
---|
208 | bool SetNonblocking(bool, SOCKET); |
---|
209 | |
---|
210 | /** Total lifetime of instance. */ |
---|
211 | time_t Uptime(); |
---|
212 | |
---|
213 | /** Set address/port of last connect() call. */ |
---|
214 | void SetClientRemoteAddress(SocketAddress&); |
---|
215 | |
---|
216 | /** Get address/port of last connect() call. */ |
---|
217 | std::auto_ptr<SocketAddress> GetClientRemoteAddress(); |
---|
218 | |
---|
219 | /** Common interface for SendBuf used by Tcp and Udp sockets. */ |
---|
220 | virtual void SendBuf(const char *,size_t,int = 0); |
---|
221 | |
---|
222 | /** Common interface for Send used by Tcp and Udp sockets. */ |
---|
223 | virtual void Send(const std::string&,int = 0); |
---|
224 | |
---|
225 | /** Outgoing traffic counter. */ |
---|
226 | virtual uint64_t GetBytesSent(bool clear = false); |
---|
227 | |
---|
228 | /** Incoming traffic counter. */ |
---|
229 | virtual uint64_t GetBytesReceived(bool clear = false); |
---|
230 | |
---|
231 | // LIST_TIMEOUT |
---|
232 | |
---|
233 | /** Enable timeout control. 0=disable timeout check. */ |
---|
234 | void SetTimeout(time_t secs); |
---|
235 | |
---|
236 | /** Check timeout. \return true if time limit reached */ |
---|
237 | bool Timeout(time_t tnow); |
---|
238 | |
---|
239 | /** Used by ListenSocket. ipv4 and ipv6 */ |
---|
240 | void SetRemoteAddress(SocketAddress&); |
---|
241 | |
---|
242 | /** \name Event callbacks */ |
---|
243 | //@{ |
---|
244 | |
---|
245 | /** Called when there is something to be read from the file descriptor. */ |
---|
246 | virtual void OnRead(); |
---|
247 | /** Called when there is room for another write on the file descriptor. */ |
---|
248 | virtual void OnWrite(); |
---|
249 | /** Called on socket exception. */ |
---|
250 | virtual void OnException(); |
---|
251 | /** Called before a socket class is deleted by the ISocketHandler. */ |
---|
252 | virtual void OnDelete(); |
---|
253 | /** Called when a connection has completed. */ |
---|
254 | virtual void OnConnect(); |
---|
255 | /** Called when an incoming connection has been completed. */ |
---|
256 | virtual void OnAccept(); |
---|
257 | /** Called when a complete line has been read and the socket is in |
---|
258 | * line protocol mode. */ |
---|
259 | virtual void OnLine(const std::string& ); |
---|
260 | /** Called on connect timeout (5s). */ |
---|
261 | virtual void OnConnectFailed(); |
---|
262 | /** Called when a client socket is created, to set socket options. |
---|
263 | \param family AF_INET, AF_INET6, etc |
---|
264 | \param type SOCK_STREAM, SOCK_DGRAM, etc |
---|
265 | \param protocol Protocol number (tcp, udp, sctp, etc) |
---|
266 | \param s Socket file descriptor |
---|
267 | */ |
---|
268 | virtual void OnOptions(int family,int type,int protocol,SOCKET s) = 0; |
---|
269 | /** Connection retry callback - return false to abort connection attempts */ |
---|
270 | virtual bool OnConnectRetry(); |
---|
271 | #ifdef ENABLE_RECONNECT |
---|
272 | /** a reconnect has been made */ |
---|
273 | virtual void OnReconnect(); |
---|
274 | #endif |
---|
275 | /** TcpSocket: When a disconnect has been detected (recv/SSL_read returns 0 bytes). */ |
---|
276 | virtual void OnDisconnect(); |
---|
277 | /** Timeout callback. */ |
---|
278 | virtual void OnTimeout(); |
---|
279 | /** Connection timeout. */ |
---|
280 | virtual void OnConnectTimeout(); |
---|
281 | //@} |
---|
282 | |
---|
283 | /** \name Socket mode flags, set/reset */ |
---|
284 | //@{ |
---|
285 | /** Set delete by handler true when you want the sockethandler to |
---|
286 | delete the socket instance after use. */ |
---|
287 | void SetDeleteByHandler(bool = true); |
---|
288 | /** Check delete by handler flag. |
---|
289 | \return true if this instance should be deleted by the sockethandler */ |
---|
290 | bool DeleteByHandler(); |
---|
291 | |
---|
292 | // LIST_CLOSE - conditional event queue |
---|
293 | |
---|
294 | /** Set close and delete to terminate the connection. */ |
---|
295 | void SetCloseAndDelete(bool = true); |
---|
296 | /** Check close and delete flag. |
---|
297 | \return true if this socket should be closed and the instance removed */ |
---|
298 | bool CloseAndDelete(); |
---|
299 | |
---|
300 | /** Return number of seconds since socket was ordered to close. \sa SetCloseAndDelete */ |
---|
301 | time_t TimeSinceClose(); |
---|
302 | |
---|
303 | /** Ignore read events for an output only socket. */ |
---|
304 | void DisableRead(bool x = true); |
---|
305 | /** Check ignore read events flag. |
---|
306 | \return true if read events should be ignored */ |
---|
307 | bool IsDisableRead(); |
---|
308 | |
---|
309 | /** Set connected status. */ |
---|
310 | void SetConnected(bool = true); |
---|
311 | /** Check connected status. |
---|
312 | \return true if connected */ |
---|
313 | bool IsConnected(); |
---|
314 | |
---|
315 | /** Connection lost - error while reading/writing from a socket - TcpSocket only. */ |
---|
316 | void SetLost(); |
---|
317 | /** Check connection lost status flag, used by TcpSocket only. |
---|
318 | \return true if there was an error while r/w causing the socket to close */ |
---|
319 | bool Lost(); |
---|
320 | |
---|
321 | /** Set flag indicating the socket is being actively deleted by the sockethandler. */ |
---|
322 | void SetErasedByHandler(bool x = true); |
---|
323 | /** Get value of flag indicating socket is deleted by sockethandler. */ |
---|
324 | bool ErasedByHandler(); |
---|
325 | |
---|
326 | //@} |
---|
327 | |
---|
328 | /** \name Information about remote connection */ |
---|
329 | //@{ |
---|
330 | /** Returns address of remote end. */ |
---|
331 | std::auto_ptr<SocketAddress> GetRemoteSocketAddress(); |
---|
332 | /** Returns address of remote end: ipv4. */ |
---|
333 | ipaddr_t GetRemoteIP4(); |
---|
334 | #ifdef ENABLE_IPV6 |
---|
335 | /** Returns address of remote end: ipv6. */ |
---|
336 | #ifdef IPPROTO_IPV6 |
---|
337 | struct in6_addr GetRemoteIP6(); |
---|
338 | #endif |
---|
339 | #endif |
---|
340 | /** Returns remote port number: ipv4 and ipv6. */ |
---|
341 | port_t GetRemotePort(); |
---|
342 | /** Returns remote ip as string? ipv4 and ipv6. */ |
---|
343 | std::string GetRemoteAddress(); |
---|
344 | /** ipv4 and ipv6(not implemented) */ |
---|
345 | std::string GetRemoteHostname(); |
---|
346 | //@} |
---|
347 | |
---|
348 | /** Returns local port number for bound socket file descriptor. */ |
---|
349 | port_t GetSockPort(); |
---|
350 | /** Returns local ipv4 address for bound socket file descriptor. */ |
---|
351 | ipaddr_t GetSockIP4(); |
---|
352 | /** Returns local ipv4 address as text for bound socket file descriptor. */ |
---|
353 | std::string GetSockAddress(); |
---|
354 | #ifdef ENABLE_IPV6 |
---|
355 | #ifdef IPPROTO_IPV6 |
---|
356 | /** Returns local ipv6 address for bound socket file descriptor. */ |
---|
357 | struct in6_addr GetSockIP6(); |
---|
358 | /** Returns local ipv6 address as text for bound socket file descriptor. */ |
---|
359 | std::string GetSockAddress6(); |
---|
360 | #endif |
---|
361 | #endif |
---|
362 | // -------------------------------------------------------------------------- |
---|
363 | /** @name IP options |
---|
364 | When an ip or socket option is available on all of the operating systems |
---|
365 | I'm testing on (linux 2.4.x, _win32, macosx, solaris9 intel) they are not |
---|
366 | checked with an #ifdef below. |
---|
367 | This might cause a compile error on other operating systems. */ |
---|
368 | // -------------------------------------------------------------------------- |
---|
369 | |
---|
370 | // IP options |
---|
371 | //@{ |
---|
372 | |
---|
373 | bool SetIpOptions(const void *p, socklen_t len); |
---|
374 | bool SetIpTOS(unsigned char tos); |
---|
375 | unsigned char IpTOS(); |
---|
376 | bool SetIpTTL(int ttl); |
---|
377 | int IpTTL(); |
---|
378 | bool SetIpHdrincl(bool x = true); |
---|
379 | bool SetIpMulticastTTL(int); |
---|
380 | int IpMulticastTTL(); |
---|
381 | bool SetMulticastLoop(bool x = true); |
---|
382 | bool IpAddMembership(struct ip_mreq&); |
---|
383 | bool IpDropMembership(struct ip_mreq&); |
---|
384 | |
---|
385 | #ifdef IP_PKTINFO |
---|
386 | bool SetIpPktinfo(bool x = true); |
---|
387 | #endif |
---|
388 | #ifdef IP_RECVTOS |
---|
389 | bool SetIpRecvTOS(bool x = true); |
---|
390 | #endif |
---|
391 | #ifdef IP_RECVTTL |
---|
392 | bool SetIpRecvTTL(bool x = true); |
---|
393 | #endif |
---|
394 | #ifdef IP_RECVOPTS |
---|
395 | bool SetIpRecvopts(bool x = true); |
---|
396 | #endif |
---|
397 | #ifdef IP_RETOPTS |
---|
398 | bool SetIpRetopts(bool x = true); |
---|
399 | #endif |
---|
400 | #ifdef IP_RECVERR |
---|
401 | bool SetIpRecverr(bool x = true); |
---|
402 | #endif |
---|
403 | #ifdef IP_MTU_DISCOVER |
---|
404 | bool SetIpMtudiscover(bool x = true); |
---|
405 | #endif |
---|
406 | #ifdef IP_MTU |
---|
407 | int IpMtu(); |
---|
408 | #endif |
---|
409 | #ifdef IP_ROUTER_ALERT |
---|
410 | bool SetIpRouterAlert(bool x = true); |
---|
411 | #endif |
---|
412 | #ifdef LINUX |
---|
413 | bool IpAddMembership(struct ip_mreqn&); |
---|
414 | #endif |
---|
415 | #ifdef LINUX |
---|
416 | bool IpDropMembership(struct ip_mreqn&); |
---|
417 | #endif |
---|
418 | //@} |
---|
419 | |
---|
420 | // SOCKET options |
---|
421 | /** @name Socket Options */ |
---|
422 | //@{ |
---|
423 | |
---|
424 | bool SoAcceptconn(); |
---|
425 | bool SetSoBroadcast(bool x = true); |
---|
426 | bool SetSoDebug(bool x = true); |
---|
427 | int SoError(); |
---|
428 | bool SetSoDontroute(bool x = true); |
---|
429 | bool SetSoLinger(int onoff, int linger); |
---|
430 | bool SetSoOobinline(bool x = true); |
---|
431 | bool SetSoRcvlowat(int); |
---|
432 | bool SetSoSndlowat(int); |
---|
433 | bool SetSoRcvtimeo(struct timeval&); |
---|
434 | bool SetSoSndtimeo(struct timeval&); |
---|
435 | bool SetSoRcvbuf(int); |
---|
436 | int SoRcvbuf(); |
---|
437 | bool SetSoSndbuf(int); |
---|
438 | int SoSndbuf(); |
---|
439 | int SoType(); |
---|
440 | bool SetSoReuseaddr(bool x = true); |
---|
441 | bool SetSoKeepalive(bool x = true); |
---|
442 | |
---|
443 | #ifdef SO_BSDCOMPAT |
---|
444 | bool SetSoBsdcompat(bool x = true); |
---|
445 | #endif |
---|
446 | #ifdef SO_BINDTODEVICE |
---|
447 | bool SetSoBindtodevice(const std::string& intf); |
---|
448 | #endif |
---|
449 | #ifdef SO_PASSCRED |
---|
450 | bool SetSoPasscred(bool x = true); |
---|
451 | #endif |
---|
452 | #ifdef SO_PEERCRED |
---|
453 | bool SoPeercred(struct ucred& ); |
---|
454 | #endif |
---|
455 | #ifdef SO_PRIORITY |
---|
456 | bool SetSoPriority(int); |
---|
457 | #endif |
---|
458 | #ifdef SO_RCVBUFFORCE |
---|
459 | bool SetSoRcvbufforce(int); |
---|
460 | #endif |
---|
461 | #ifdef SO_SNDBUFFORCE |
---|
462 | bool SetSoSndbufforce(int); |
---|
463 | #endif |
---|
464 | #ifdef SO_TIMESTAMP |
---|
465 | bool SetSoTimestamp(bool x = true); |
---|
466 | #endif |
---|
467 | #ifdef SO_NOSIGPIPE |
---|
468 | bool SetSoNosigpipe(bool x = true); |
---|
469 | #endif |
---|
470 | //@} |
---|
471 | |
---|
472 | // TCP options in TcpSocket.h/TcpSocket.cpp |
---|
473 | |
---|
474 | |
---|
475 | #ifdef HAVE_OPENSSL |
---|
476 | /** @name SSL Support */ |
---|
477 | //@{ |
---|
478 | /** SSL client/server support - internal use. \sa TcpSocket */ |
---|
479 | virtual void OnSSLConnect(); |
---|
480 | /** SSL client/server support - internal use. \sa TcpSocket */ |
---|
481 | virtual void OnSSLAccept(); |
---|
482 | /** SSL negotiation failed for client connect. */ |
---|
483 | virtual void OnSSLConnectFailed(); |
---|
484 | /** SSL negotiation failed for server accept. */ |
---|
485 | virtual void OnSSLAcceptFailed(); |
---|
486 | /** new SSL support */ |
---|
487 | virtual bool SSLNegotiate(); |
---|
488 | /** Check if SSL is Enabled for this TcpSocket. |
---|
489 | \return true if this is a TcpSocket with SSL enabled */ |
---|
490 | bool IsSSL(); |
---|
491 | /** Enable SSL operation for a TcpSocket. */ |
---|
492 | void EnableSSL(bool x = true); |
---|
493 | /** Still negotiating ssl connection. |
---|
494 | \return true if ssl negotiating is still in progress */ |
---|
495 | bool IsSSLNegotiate(); |
---|
496 | /** Set flag indicating ssl handshaking still in progress. */ |
---|
497 | void SetSSLNegotiate(bool x = true); |
---|
498 | /** OnAccept called with SSL Enabled. |
---|
499 | \return true if this is a TcpSocket with an incoming SSL connection */ |
---|
500 | bool IsSSLServer(); |
---|
501 | /** Set flag indicating that this is a TcpSocket with incoming SSL connection. */ |
---|
502 | void SetSSLServer(bool x = true); |
---|
503 | /** SSL; Get pointer to ssl context structure. */ |
---|
504 | virtual SSL_CTX *GetSslContext() { return NULL; } |
---|
505 | /** SSL; Get pointer to ssl structure. */ |
---|
506 | virtual SSL *GetSsl() { return NULL; } |
---|
507 | //@} |
---|
508 | #endif // HAVE_OPENSSL |
---|
509 | |
---|
510 | #ifdef ENABLE_IPV6 |
---|
511 | /** Enable ipv6 for this socket. */ |
---|
512 | void SetIpv6(bool x = true); |
---|
513 | /** Check ipv6 socket. |
---|
514 | \return true if this is an ipv6 socket */ |
---|
515 | bool IsIpv6(); |
---|
516 | #endif |
---|
517 | |
---|
518 | #ifdef ENABLE_POOL |
---|
519 | /** @name Connection Pool */ |
---|
520 | //@{ |
---|
521 | /** Client = connecting TcpSocket. */ |
---|
522 | void SetIsClient(); |
---|
523 | /** Socket type from socket() call. */ |
---|
524 | void SetSocketType(int x); |
---|
525 | /** Socket type from socket() call. */ |
---|
526 | int GetSocketType(); |
---|
527 | /** Protocol type from socket() call. */ |
---|
528 | void SetSocketProtocol(const std::string& x); |
---|
529 | /** Protocol type from socket() call. */ |
---|
530 | const std::string& GetSocketProtocol(); |
---|
531 | /** Instruct a client socket to stay open in the connection pool after use. |
---|
532 | If you have connected to a server using tcp, you can call SetRetain |
---|
533 | to leave the connection open after your socket instance has been deleted. |
---|
534 | The next connection you make to the same server will reuse the already |
---|
535 | opened connection, if it is still available. |
---|
536 | */ |
---|
537 | void SetRetain(); |
---|
538 | /** Check retain flag. |
---|
539 | \return true if the socket should be moved to connection pool after use */ |
---|
540 | bool Retain(); |
---|
541 | /** Copy connection parameters from sock. */ |
---|
542 | void CopyConnection(Socket *sock); |
---|
543 | //@} |
---|
544 | #endif // ENABLE_POOL |
---|
545 | |
---|
546 | #ifdef ENABLE_SOCKS4 |
---|
547 | /** \name Socks4 support */ |
---|
548 | //@{ |
---|
549 | /** Socks4 client support internal use. \sa TcpSocket */ |
---|
550 | virtual void OnSocks4Connect(); |
---|
551 | /** Socks4 client support internal use. \sa TcpSocket */ |
---|
552 | virtual void OnSocks4ConnectFailed(); |
---|
553 | /** Socks4 client support internal use. \sa TcpSocket */ |
---|
554 | virtual bool OnSocks4Read(); |
---|
555 | /** Called when the last write caused the tcp output buffer to |
---|
556 | * become empty. */ |
---|
557 | /** socket still in socks4 negotiation mode */ |
---|
558 | bool Socks4(); |
---|
559 | /** Set flag indicating Socks4 handshaking in progress */ |
---|
560 | void SetSocks4(bool x = true); |
---|
561 | |
---|
562 | /** Set socks4 server host address to use */ |
---|
563 | void SetSocks4Host(ipaddr_t a); |
---|
564 | /** Set socks4 server hostname to use. */ |
---|
565 | void SetSocks4Host(const std::string& ); |
---|
566 | /** Socks4 server port to use. */ |
---|
567 | void SetSocks4Port(port_t p); |
---|
568 | /** Provide a socks4 userid if required by the socks4 server. */ |
---|
569 | void SetSocks4Userid(const std::string& x); |
---|
570 | /** Get the ip address of socks4 server to use. |
---|
571 | \return socks4 server host address */ |
---|
572 | ipaddr_t GetSocks4Host(); |
---|
573 | /** Get the socks4 server port to use. |
---|
574 | \return socks4 server port */ |
---|
575 | port_t GetSocks4Port(); |
---|
576 | /** Get socks4 userid. |
---|
577 | \return Socks4 userid */ |
---|
578 | const std::string& GetSocks4Userid(); |
---|
579 | //@} |
---|
580 | #endif // ENABLE_SOCKS4 |
---|
581 | |
---|
582 | #ifdef ENABLE_RESOLVER |
---|
583 | /** \name Asynchronous Resolver */ |
---|
584 | //@{ |
---|
585 | /** Request an asynchronous dns resolution. |
---|
586 | \param host hostname to be resolved |
---|
587 | \param port port number passed along for the ride |
---|
588 | \return Resolve ID */ |
---|
589 | int Resolve(const std::string& host,port_t port = 0); |
---|
590 | #ifdef ENABLE_IPV6 |
---|
591 | int Resolve6(const std::string& host, port_t port = 0); |
---|
592 | #endif |
---|
593 | /** Callback returning a resolved address. |
---|
594 | \param id Resolve ID from Resolve call |
---|
595 | \param a resolved ip address |
---|
596 | \param port port number passed to Resolve */ |
---|
597 | virtual void OnResolved(int id,ipaddr_t a,port_t port); |
---|
598 | #ifdef ENABLE_IPV6 |
---|
599 | virtual void OnResolved(int id,in6_addr& a,port_t port); |
---|
600 | #endif |
---|
601 | /** Request asynchronous reverse dns lookup. |
---|
602 | \param a in_addr to be translated */ |
---|
603 | int Resolve(ipaddr_t a); |
---|
604 | #ifdef ENABLE_IPV6 |
---|
605 | int Resolve(in6_addr& a); |
---|
606 | #endif |
---|
607 | /** Callback returning reverse resolve results. |
---|
608 | \param id Resolve ID |
---|
609 | \param name Resolved hostname */ |
---|
610 | virtual void OnReverseResolved(int id,const std::string& name); |
---|
611 | /** Callback indicating failed dns lookup. |
---|
612 | \param id Resolve ID */ |
---|
613 | virtual void OnResolveFailed(int id); |
---|
614 | //@} |
---|
615 | #endif // ENABLE_RESOLVER |
---|
616 | |
---|
617 | #ifdef ENABLE_DETACH |
---|
618 | /** \name Thread Support */ |
---|
619 | //@{ |
---|
620 | /** Callback fires when a new socket thread has started and this |
---|
621 | socket is ready for operation again. |
---|
622 | \sa ResolvSocket */ |
---|
623 | virtual void OnDetached(); |
---|
624 | |
---|
625 | // LIST_DETACH |
---|
626 | |
---|
627 | /** Internal use. */ |
---|
628 | void SetDetach(bool x = true); |
---|
629 | /** Check detach flag. |
---|
630 | \return true if the socket should detach to its own thread */ |
---|
631 | bool IsDetach(); |
---|
632 | |
---|
633 | /** Internal use. */ |
---|
634 | void SetDetached(bool x = true); |
---|
635 | /** Check detached flag. |
---|
636 | \return true if the socket runs in its own thread. */ |
---|
637 | const bool IsDetached() const; |
---|
638 | /** Order this socket to start its own thread and call OnDetached |
---|
639 | when ready for operation. */ |
---|
640 | bool Detach(); |
---|
641 | /** Store the slave sockethandler pointer. */ |
---|
642 | void SetSlaveHandler(ISocketHandler *); |
---|
643 | /** Create new thread for this socket to run detached in. */ |
---|
644 | void DetachSocket(); |
---|
645 | //@} |
---|
646 | #endif // ENABLE_DETACH |
---|
647 | |
---|
648 | /** Write traffic to an IFile. Socket will not delete this object. */ |
---|
649 | void SetTrafficMonitor(IFile *p) { m_traffic_monitor = p; } |
---|
650 | |
---|
651 | #ifdef ENABLE_TRIGGERS |
---|
652 | /** \name Triggers */ |
---|
653 | //@{ |
---|
654 | /** Subscribe to trigger id. */ |
---|
655 | void Subscribe(int id); |
---|
656 | /** Unsubscribe from trigger id. */ |
---|
657 | void Unsubscribe(int id); |
---|
658 | /** Trigger callback, with data passed from source to destination. */ |
---|
659 | virtual void OnTrigger(int id, const TriggerData& data); |
---|
660 | /** Trigger cancelled because source has been deleted (as in delete). */ |
---|
661 | virtual void OnCancelled(int id); |
---|
662 | //@} |
---|
663 | #endif |
---|
664 | |
---|
665 | protected: |
---|
666 | /** default constructor not available */ |
---|
667 | Socket() : m_handler(m_handler) {} |
---|
668 | /** copy constructor not available */ |
---|
669 | Socket(const Socket& s) : m_handler(s.m_handler) {} |
---|
670 | |
---|
671 | /** assignment operator not available. */ |
---|
672 | Socket& operator=(const Socket& ) { return *this; } |
---|
673 | |
---|
674 | /** All traffic will be written to this IFile, if set. */ |
---|
675 | IFile *GetTrafficMonitor() { return m_traffic_monitor; } |
---|
676 | |
---|
677 | // unsigned long m_flags; ///< boolean flags, replacing old 'bool' members |
---|
678 | |
---|
679 | private: |
---|
680 | ISocketHandler& m_handler; ///< Reference of ISocketHandler in control of this socket |
---|
681 | SOCKET m_socket; ///< File descriptor |
---|
682 | bool m_bDel; ///< Delete by handler flag |
---|
683 | bool m_bClose; ///< Close and delete flag |
---|
684 | time_t m_tCreate; ///< Time in seconds when this socket was created |
---|
685 | Socket *m_parent; ///< Pointer to ListenSocket class, valid for incoming sockets |
---|
686 | bool m_b_disable_read; ///< Disable checking for read events |
---|
687 | bool m_connected; ///< Socket is connected (tcp/udp) |
---|
688 | bool m_b_erased_by_handler; ///< Set by handler before delete |
---|
689 | time_t m_tClose; ///< Time in seconds when ordered to close |
---|
690 | std::auto_ptr<SocketAddress> m_client_remote_address; ///< Address of last connect() |
---|
691 | std::auto_ptr<SocketAddress> m_remote_address; ///< Remote end address |
---|
692 | IFile *m_traffic_monitor; |
---|
693 | time_t m_timeout_start; ///< Set by SetTimeout |
---|
694 | time_t m_timeout_limit; ///< Defined by SetTimeout |
---|
695 | bool m_bLost; ///< connection lost |
---|
696 | |
---|
697 | #ifdef _WIN32 |
---|
698 | static WSAInitializer m_winsock_init; ///< Winsock initialization singleton class |
---|
699 | #endif |
---|
700 | |
---|
701 | #ifdef HAVE_OPENSSL |
---|
702 | bool m_b_enable_ssl; ///< Enable SSL for this TcpSocket |
---|
703 | bool m_b_ssl; ///< ssl negotiation mode (TcpSocket) |
---|
704 | bool m_b_ssl_server; ///< True if this is an incoming ssl TcpSocket connection |
---|
705 | #endif |
---|
706 | |
---|
707 | #ifdef ENABLE_IPV6 |
---|
708 | bool m_ipv6; ///< This is an ipv6 socket if this one is true |
---|
709 | #endif |
---|
710 | |
---|
711 | #ifdef ENABLE_POOL |
---|
712 | int m_socket_type; ///< Type of socket, from socket() call |
---|
713 | std::string m_socket_protocol; ///< Protocol, from socket() call |
---|
714 | bool m_bClient; ///< only client connections are pooled |
---|
715 | bool m_bRetain; ///< keep connection on close |
---|
716 | #endif |
---|
717 | |
---|
718 | #ifdef ENABLE_SOCKS4 |
---|
719 | bool m_bSocks4; ///< socks4 negotiation mode (TcpSocket) |
---|
720 | ipaddr_t m_socks4_host; ///< socks4 server address |
---|
721 | port_t m_socks4_port; ///< socks4 server port number |
---|
722 | std::string m_socks4_userid; ///< socks4 server usedid |
---|
723 | #endif |
---|
724 | |
---|
725 | #ifdef ENABLE_DETACH |
---|
726 | bool m_detach; ///< Socket ordered to detach flag |
---|
727 | bool m_detached; ///< Socket has been detached |
---|
728 | SocketThread *m_pThread; ///< Detach socket thread class pointer |
---|
729 | ISocketHandler *m_slave_handler; ///< Actual sockethandler while detached |
---|
730 | #endif |
---|
731 | }; |
---|
732 | |
---|
733 | #ifdef SOCKETS_NAMESPACE |
---|
734 | } |
---|
735 | #endif |
---|
736 | |
---|
737 | |
---|
738 | #endif // _SOCKETS_Socket_H |
---|
739 | |
---|