00001
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #ifndef _SOCKETS_Socket_H
00031 #define _SOCKETS_Socket_H
00032 #include "sockets-config.h"
00033
00034 #include <string>
00035 #include <vector>
00036 #include <list>
00037 #ifdef HAVE_OPENSSL
00038 #include <openssl/ssl.h>
00039 #endif
00040
00041 #include "socket_include.h"
00042 #include <time.h>
00043 #include "Utility.h"
00044 #include "SocketAddress.h"
00045 #include "Thread.h"
00046
00047
00048 #ifdef SOCKETS_NAMESPACE
00049 namespace SOCKETS_NAMESPACE {
00050 #endif
00051
00052
00053 class ISocketHandler;
00054 class SocketAddress;
00055 class IFile;
00056
00057
00061 class Socket
00062 {
00063 friend class ISocketHandler;
00064 #ifdef ENABLE_DETACH
00065
00067 class SocketThread : public Thread
00068 {
00069 public:
00070 SocketThread(Socket *p);
00071 ~SocketThread();
00072
00073 void Run();
00074
00075 private:
00076 Socket *GetSocket() const { return m_socket; }
00077 SocketThread(const SocketThread& s) : m_socket(s.GetSocket()) {}
00078 SocketThread& operator=(const SocketThread& ) { return *this; }
00079 Socket *m_socket;
00080 };
00081 #endif // ENABLE_DETACH
00082
00083 #ifdef ENABLE_TRIGGERS
00084 public:
00086 class TriggerData
00087 {
00088 public:
00089 TriggerData() : m_src(NULL) {}
00090 virtual ~TriggerData() {}
00091
00092 Socket *GetSource() const { return m_src; }
00093 void SetSource(Socket *x) { m_src = x; }
00094
00095 private:
00096 Socket *m_src;
00097 };
00098 #endif // ENABLE_TRIGGERS
00099
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138 public:
00140 Socket(ISocketHandler&);
00141
00142 virtual ~Socket();
00143
00148 virtual Socket *Create() { return NULL; }
00149
00153 ISocketHandler& Handler() const;
00154
00159 ISocketHandler& MasterHandler() const;
00160
00168 virtual void Init();
00169
00174 SOCKET CreateSocket(int af,int type,const std::string& protocol = "");
00175
00178 void Attach(SOCKET s);
00179
00181 SOCKET GetSocket();
00182
00185 virtual int Close();
00186
00188 void Set(bool bRead,bool bWrite,bool bException = true);
00189
00192 virtual bool Ready();
00193
00196 Socket *GetParent();
00197
00200 void SetParent(Socket *);
00201
00203 virtual port_t GetPort();
00204
00206 bool SetNonblocking(bool);
00207
00209 bool SetNonblocking(bool, SOCKET);
00210
00212 time_t Uptime();
00213
00215 void SetClientRemoteAddress(SocketAddress&);
00216
00218 std::auto_ptr<SocketAddress> GetClientRemoteAddress();
00219
00221 virtual void SendBuf(const char *,size_t,int = 0);
00222
00224 virtual void Send(const std::string&,int = 0);
00225
00227 virtual uint64_t GetBytesSent(bool clear = false);
00228
00230 virtual uint64_t GetBytesReceived(bool clear = false);
00231
00232
00233
00235 void SetTimeout(time_t secs);
00236
00238 bool Timeout(time_t tnow);
00239
00241 void SetRemoteAddress(SocketAddress&);
00242
00245
00247 virtual void OnRead();
00249 virtual void OnWrite();
00251 virtual void OnException();
00253 virtual void OnDelete();
00255 virtual void OnConnect();
00257 virtual void OnAccept();
00260 virtual void OnLine(const std::string& );
00262 virtual void OnConnectFailed();
00269 virtual void OnOptions(int family,int type,int protocol,SOCKET s) = 0;
00271 virtual bool OnConnectRetry();
00272 #ifdef ENABLE_RECONNECT
00273
00274 virtual void OnReconnect();
00276 virtual void OnDisconnect();
00277 #endif
00278
00279 virtual void OnTimeout();
00281 virtual void OnConnectTimeout();
00283
00288 void SetDeleteByHandler(bool = true);
00291 bool DeleteByHandler();
00292
00293
00294
00296 void SetCloseAndDelete(bool = true);
00299 bool CloseAndDelete();
00300
00302 time_t TimeSinceClose();
00303
00305 void DisableRead(bool x = true);
00308 bool IsDisableRead();
00309
00311 void SetConnected(bool = true);
00314 bool IsConnected();
00315
00317 void SetErasedByHandler(bool x = true);
00319 bool ErasedByHandler();
00320
00322
00326 std::auto_ptr<SocketAddress> GetRemoteSocketAddress();
00328 ipaddr_t GetRemoteIP4();
00329 #ifdef ENABLE_IPV6
00330
00331 #ifdef IPPROTO_IPV6
00332 struct in6_addr GetRemoteIP6();
00333 #endif
00334 #endif
00335
00336 port_t GetRemotePort();
00338 std::string GetRemoteAddress();
00340 std::string GetRemoteHostname();
00342
00343
00349
00350
00351
00353
00354 bool SetIpOptions(const void *p, socklen_t len);
00355 bool SetIpTOS(unsigned char tos);
00356 unsigned char IpTOS();
00357 bool SetIpTTL(int ttl);
00358 int IpTTL();
00359 bool SetIpHdrincl(bool x = true);
00360 bool SetIpMulticastTTL(int);
00361 int IpMulticastTTL();
00362 bool SetMulticastLoop(bool x = true);
00363 bool IpAddMembership(struct ip_mreq&);
00364 bool IpDropMembership(struct ip_mreq&);
00365
00366 #ifdef IP_PKTINFO
00367 bool SetIpPktinfo(bool x = true);
00368 #endif
00369 #ifdef IP_RECVTOS
00370 bool SetIpRecvTOS(bool x = true);
00371 #endif
00372 #ifdef IP_RECVTTL
00373 bool SetIpRecvTTL(bool x = true);
00374 #endif
00375 #ifdef IP_RECVOPTS
00376 bool SetIpRecvopts(bool x = true);
00377 #endif
00378 #ifdef IP_RETOPTS
00379 bool SetIpRetopts(bool x = true);
00380 #endif
00381 #ifdef IP_RECVERR
00382 bool SetIpRecverr(bool x = true);
00383 #endif
00384 #ifdef IP_MTU_DISCOVER
00385 bool SetIpMtudiscover(bool x = true);
00386 #endif
00387 #ifdef IP_MTU
00388 int IpMtu();
00389 #endif
00390 #ifdef IP_ROUTER_ALERT
00391 bool SetIpRouterAlert(bool x = true);
00392 #endif
00393 #ifdef LINUX
00394 bool IpAddMembership(struct ip_mreqn&);
00395 #endif
00396 #ifdef LINUX
00397 bool IpDropMembership(struct ip_mreqn&);
00398 #endif
00399
00400
00401
00404
00405 bool SoAcceptconn();
00406 bool SetSoBroadcast(bool x = true);
00407 bool SetSoDebug(bool x = true);
00408 int SoError();
00409 bool SetSoDontroute(bool x = true);
00410 bool SetSoLinger(int onoff, int linger);
00411 bool SetSoOobinline(bool x = true);
00412 bool SetSoRcvlowat(int);
00413 bool SetSoSndlowat(int);
00414 bool SetSoRcvtimeo(struct timeval&);
00415 bool SetSoSndtimeo(struct timeval&);
00416 bool SetSoRcvbuf(int);
00417 int SoRcvbuf();
00418 bool SetSoSndbuf(int);
00419 int SoSndbuf();
00420 int SoType();
00421 bool SetSoReuseaddr(bool x = true);
00422 bool SetSoKeepalive(bool x = true);
00423
00424 #ifdef SO_BSDCOMPAT
00425 bool SetSoBsdcompat(bool x = true);
00426 #endif
00427 #ifdef SO_BINDTODEVICE
00428 bool SetSoBindtodevice(const std::string& intf);
00429 #endif
00430 #ifdef SO_PASSCRED
00431 bool SetSoPasscred(bool x = true);
00432 #endif
00433 #ifdef SO_PEERCRED
00434 bool SoPeercred(struct ucred& );
00435 #endif
00436 #ifdef SO_PRIORITY
00437 bool SetSoPriority(int);
00438 #endif
00439 #ifdef SO_RCVBUFFORCE
00440 bool SetSoRcvbufforce(int);
00441 #endif
00442 #ifdef SO_SNDBUFFORCE
00443 bool SetSoSndbufforce(int);
00444 #endif
00445 #ifdef SO_TIMESTAMP
00446 bool SetSoTimestamp(bool x = true);
00447 #endif
00448 #ifdef SO_NOSIGPIPE
00449 bool SetSoNosigpipe(bool x = true);
00450 #endif
00451
00452
00453
00454
00455
00456 #ifdef HAVE_OPENSSL
00457
00460 virtual void OnSSLConnect();
00462 virtual void OnSSLAccept();
00464 virtual void OnSSLConnectFailed();
00466 virtual void OnSSLAcceptFailed();
00468 virtual bool SSLNegotiate();
00471 bool IsSSL();
00473 void EnableSSL(bool x = true);
00476 bool IsSSLNegotiate();
00478 void SetSSLNegotiate(bool x = true);
00481 bool IsSSLServer();
00483 void SetSSLServer(bool x = true);
00485 virtual SSL_CTX *GetSslContext() { return NULL; }
00487 virtual SSL *GetSsl() { return NULL; }
00489 #endif // HAVE_OPENSSL
00490
00491 #ifdef ENABLE_IPV6
00492
00493 void SetIpv6(bool x = true);
00496 bool IsIpv6();
00497 #endif
00498
00499 #ifdef ENABLE_POOL
00500
00503 void SetIsClient();
00505 void SetSocketType(int x);
00507 int GetSocketType();
00509 void SetSocketProtocol(const std::string& x);
00511 const std::string& GetSocketProtocol();
00518 void SetRetain();
00521 bool Retain();
00523 void SetLost();
00526 bool Lost();
00528 void CopyConnection(Socket *sock);
00530 #endif // ENABLE_POOL
00531
00532 #ifdef ENABLE_SOCKS4
00533
00536 virtual void OnSocks4Connect();
00538 virtual void OnSocks4ConnectFailed();
00540 virtual bool OnSocks4Read();
00544 bool Socks4();
00546 void SetSocks4(bool x = true);
00547
00549 void SetSocks4Host(ipaddr_t a);
00551 void SetSocks4Host(const std::string& );
00553 void SetSocks4Port(port_t p);
00555 void SetSocks4Userid(const std::string& x);
00558 ipaddr_t GetSocks4Host();
00561 port_t GetSocks4Port();
00564 const std::string& GetSocks4Userid();
00566 #endif // ENABLE_SOCKS4
00567
00568 #ifdef ENABLE_RESOLVER
00569
00575 int Resolve(const std::string& host,port_t port = 0);
00576 #ifdef ENABLE_IPV6
00577 int Resolve6(const std::string& host, port_t port = 0);
00578 #endif
00579
00583 virtual void OnResolved(int id,ipaddr_t a,port_t port);
00584 #ifdef ENABLE_IPV6
00585 virtual void OnResolved(int id,in6_addr& a,port_t port);
00586 #endif
00587
00589 int Resolve(ipaddr_t a);
00590 #ifdef ENABLE_IPV6
00591 int Resolve(in6_addr& a);
00592 #endif
00593
00596 virtual void OnReverseResolved(int id,const std::string& name);
00599 virtual void OnResolveFailed(int id);
00601 #endif // ENABLE_RESOLVER
00602
00603 #ifdef ENABLE_DETACH
00604
00609 virtual void OnDetached();
00610
00611
00612
00614 void SetDetach(bool x = true);
00617 bool IsDetach();
00618
00620 void SetDetached(bool x = true);
00623 const bool IsDetached() const;
00626 bool Detach();
00628 void SetSlaveHandler(ISocketHandler *);
00630 void DetachSocket();
00632 #endif // ENABLE_DETACH
00633
00635 void SetTrafficMonitor(IFile *p) { m_traffic_monitor = p; }
00636
00637 #ifdef ENABLE_TRIGGERS
00638
00641 void Subscribe(int id);
00643 void Unsubscribe(int id);
00645 virtual void OnTrigger(int id, const TriggerData& data);
00647 virtual void OnCancelled(int id);
00649 #endif
00650
00651 protected:
00653 Socket() : m_handler(m_handler) {}
00655 Socket(const Socket& s) : m_handler(s.m_handler) {}
00656
00658 Socket& operator=(const Socket& ) { return *this; }
00659
00661 IFile *GetTrafficMonitor() { return m_traffic_monitor; }
00662
00663
00664
00665 private:
00666 ISocketHandler& m_handler;
00667 SOCKET m_socket;
00668 bool m_bDel;
00669 bool m_bClose;
00670 time_t m_tCreate;
00671 Socket *m_parent;
00672 bool m_b_disable_read;
00673 bool m_connected;
00674 bool m_b_erased_by_handler;
00675 time_t m_tClose;
00676 std::auto_ptr<SocketAddress> m_client_remote_address;
00677 std::auto_ptr<SocketAddress> m_remote_address;
00678 IFile *m_traffic_monitor;
00679 time_t m_timeout_start;
00680 time_t m_timeout_limit;
00681
00682 #ifdef _WIN32
00683 static WSAInitializer m_winsock_init;
00684 #endif
00685
00686 #ifdef HAVE_OPENSSL
00687 bool m_b_enable_ssl;
00688 bool m_b_ssl;
00689 bool m_b_ssl_server;
00690 #endif
00691
00692 #ifdef ENABLE_IPV6
00693 bool m_ipv6;
00694 #endif
00695
00696 #ifdef ENABLE_POOL
00697 int m_socket_type;
00698 std::string m_socket_protocol;
00699 bool m_bClient;
00700 bool m_bRetain;
00701 bool m_bLost;
00702 #endif
00703
00704 #ifdef ENABLE_SOCKS4
00705 bool m_bSocks4;
00706 ipaddr_t m_socks4_host;
00707 port_t m_socks4_port;
00708 std::string m_socks4_userid;
00709 #endif
00710
00711 #ifdef ENABLE_DETACH
00712 bool m_detach;
00713 bool m_detached;
00714 SocketThread *m_pThread;
00715 ISocketHandler *m_slave_handler;
00716 #endif
00717 };
00718
00719 #ifdef SOCKETS_NAMESPACE
00720 }
00721 #endif
00722
00723
00724 #endif // _SOCKETS_Socket_H