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 "SocketAddress.h"
00044 #include "Thread.h"
00045
00046
00047 #ifdef SOCKETS_NAMESPACE
00048 namespace SOCKETS_NAMESPACE {
00049 #endif
00050
00051
00052 class ISocketHandler;
00053 class SocketAddress;
00054 class IFile;
00055
00056
00060 class Socket
00061 {
00062
00063 #ifdef ENABLE_DETACH
00064
00066 class SocketThread : public Thread
00067 {
00068 public:
00069 SocketThread(Socket *p);
00070 ~SocketThread();
00071
00072 void Run();
00073
00074 private:
00075 Socket *GetSocket() const { return m_socket; }
00076 SocketThread(const SocketThread& s) : m_socket(s.GetSocket()) {}
00077 SocketThread& operator=(const SocketThread& ) { return *this; }
00078 Socket *m_socket;
00079 };
00080 #endif // ENABLE_DETACH
00081
00082 #ifdef ENABLE_TRIGGERS
00083 public:
00085 class TriggerData
00086 {
00087 public:
00088 TriggerData() : m_src(NULL) {}
00089 virtual ~TriggerData() {}
00090
00091 Socket *GetSource() const { return m_src; }
00092 void SetSource(Socket *x) { m_src = x; }
00093
00094 private:
00095 Socket *m_src;
00096 };
00097 #endif // ENABLE_TRIGGERS
00098
00100
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 public:
00139 Socket(ISocketHandler&);
00140
00141 virtual ~Socket();
00142
00147 virtual Socket *Create() { return NULL; }
00148
00152 ISocketHandler& Handler() const;
00153
00158 ISocketHandler& MasterHandler() const;
00159
00167 virtual void Init();
00168
00173 SOCKET CreateSocket(int af,int type,const std::string& protocol = "");
00174
00177 void Attach(SOCKET s);
00178
00180 SOCKET GetSocket();
00181
00184 virtual int Close();
00185
00187 void Set(bool bRead,bool bWrite,bool bException = true);
00188
00191 virtual bool Ready();
00192
00195 Socket *GetParent();
00196
00199 void SetParent(Socket *);
00200
00202 virtual port_t GetPort();
00203
00205 bool SetNonblocking(bool);
00206
00208 bool SetNonblocking(bool, SOCKET);
00209
00211 time_t Uptime();
00212
00214 void SetClientRemoteAddress(SocketAddress&);
00215
00217 std::auto_ptr<SocketAddress> GetClientRemoteAddress();
00218
00220 virtual void SendBuf(const char *,size_t,int = 0);
00221
00223 virtual void Send(const std::string&,int = 0);
00224
00226 virtual uint64_t GetBytesSent(bool clear = false);
00227
00229 virtual uint64_t GetBytesReceived(bool clear = false);
00230
00231
00232
00234 void SetTimeout(time_t secs);
00235
00237 bool Timeout(time_t tnow);
00238
00240 void SetRemoteAddress(SocketAddress&);
00241
00244
00246 virtual void OnRead();
00248 virtual void OnWrite();
00250 virtual void OnException();
00252 virtual void OnDelete();
00254 virtual void OnConnect();
00256 virtual void OnAccept();
00259 virtual void OnLine(const std::string& );
00261 virtual void OnConnectFailed();
00268 virtual void OnOptions(int family,int type,int protocol,SOCKET s) = 0;
00270 virtual bool OnConnectRetry();
00271 #ifdef ENABLE_RECONNECT
00272
00273 virtual void OnReconnect();
00274 #endif
00275
00276 virtual void OnDisconnect();
00283 virtual void OnDisconnect(short info, int code);
00285 virtual void OnTimeout();
00287 virtual void OnConnectTimeout();
00289
00294 void SetDeleteByHandler(bool = true);
00297 bool DeleteByHandler();
00298
00299
00300
00302 void SetCloseAndDelete(bool = true);
00305 bool CloseAndDelete();
00306
00308 time_t TimeSinceClose();
00309
00311 void DisableRead(bool x = true);
00314 bool IsDisableRead();
00315
00317 void SetConnected(bool = true);
00320 bool IsConnected();
00321
00323 void SetLost();
00326 bool Lost();
00327
00329 void SetErasedByHandler(bool x = true);
00331 bool ErasedByHandler();
00332
00334
00338 std::auto_ptr<SocketAddress> GetRemoteSocketAddress();
00340 ipaddr_t GetRemoteIP4();
00341 #ifdef ENABLE_IPV6
00342
00343 #ifdef IPPROTO_IPV6
00344 struct in6_addr GetRemoteIP6();
00345 #endif
00346 #endif
00347
00348 port_t GetRemotePort();
00350 std::string GetRemoteAddress();
00352 std::string GetRemoteHostname();
00354
00356 port_t GetSockPort();
00358 ipaddr_t GetSockIP4();
00360 std::string GetSockAddress();
00361 #ifdef ENABLE_IPV6
00362 #ifdef IPPROTO_IPV6
00363
00364 struct in6_addr GetSockIP6();
00366 std::string GetSockAddress6();
00367 #endif
00368 #endif
00369
00375
00376
00377
00379
00380 bool SetIpOptions(const void *p, socklen_t len);
00381 bool SetIpTOS(unsigned char tos);
00382 unsigned char IpTOS();
00383 bool SetIpTTL(int ttl);
00384 int IpTTL();
00385 bool SetIpHdrincl(bool x = true);
00386 bool SetIpMulticastTTL(int);
00387 int IpMulticastTTL();
00388 bool SetMulticastLoop(bool x = true);
00389 bool IpAddMembership(struct ip_mreq&);
00390 bool IpDropMembership(struct ip_mreq&);
00391
00392 #ifdef IP_PKTINFO
00393 bool SetIpPktinfo(bool x = true);
00394 #endif
00395 #ifdef IP_RECVTOS
00396 bool SetIpRecvTOS(bool x = true);
00397 #endif
00398 #ifdef IP_RECVTTL
00399 bool SetIpRecvTTL(bool x = true);
00400 #endif
00401 #ifdef IP_RECVOPTS
00402 bool SetIpRecvopts(bool x = true);
00403 #endif
00404 #ifdef IP_RETOPTS
00405 bool SetIpRetopts(bool x = true);
00406 #endif
00407 #ifdef IP_RECVERR
00408 bool SetIpRecverr(bool x = true);
00409 #endif
00410 #ifdef IP_MTU_DISCOVER
00411 bool SetIpMtudiscover(bool x = true);
00412 #endif
00413 #ifdef IP_MTU
00414 int IpMtu();
00415 #endif
00416 #ifdef IP_ROUTER_ALERT
00417 bool SetIpRouterAlert(bool x = true);
00418 #endif
00419 #ifdef LINUX
00420 bool IpAddMembership(struct ip_mreqn&);
00421 #endif
00422 #ifdef LINUX
00423 bool IpDropMembership(struct ip_mreqn&);
00424 #endif
00425
00426
00427
00430
00431 bool SoAcceptconn();
00432 bool SetSoBroadcast(bool x = true);
00433 bool SetSoDebug(bool x = true);
00434 int SoError();
00435 bool SetSoDontroute(bool x = true);
00436 bool SetSoLinger(int onoff, int linger);
00437 bool SetSoOobinline(bool x = true);
00438 bool SetSoRcvlowat(int);
00439 bool SetSoSndlowat(int);
00440 bool SetSoRcvtimeo(struct timeval&);
00441 bool SetSoSndtimeo(struct timeval&);
00442 bool SetSoRcvbuf(int);
00443 int SoRcvbuf();
00444 bool SetSoSndbuf(int);
00445 int SoSndbuf();
00446 int SoType();
00447 bool SetSoReuseaddr(bool x = true);
00448 bool SetSoKeepalive(bool x = true);
00449
00450 #ifdef SO_BSDCOMPAT
00451 bool SetSoBsdcompat(bool x = true);
00452 #endif
00453 #ifdef SO_BINDTODEVICE
00454 bool SetSoBindtodevice(const std::string& intf);
00455 #endif
00456 #ifdef SO_PASSCRED
00457 bool SetSoPasscred(bool x = true);
00458 #endif
00459 #ifdef SO_PEERCRED
00460 bool SoPeercred(struct ucred& );
00461 #endif
00462 #ifdef SO_PRIORITY
00463 bool SetSoPriority(int);
00464 #endif
00465 #ifdef SO_RCVBUFFORCE
00466 bool SetSoRcvbufforce(int);
00467 #endif
00468 #ifdef SO_SNDBUFFORCE
00469 bool SetSoSndbufforce(int);
00470 #endif
00471 #ifdef SO_TIMESTAMP
00472 bool SetSoTimestamp(bool x = true);
00473 #endif
00474 #ifdef SO_NOSIGPIPE
00475 bool SetSoNosigpipe(bool x = true);
00476 #endif
00477
00478
00479
00480
00481
00482 #ifdef HAVE_OPENSSL
00483
00486 virtual void OnSSLConnect();
00488 virtual void OnSSLAccept();
00490 virtual void OnSSLConnectFailed();
00492 virtual void OnSSLAcceptFailed();
00494 virtual bool SSLNegotiate();
00497 bool IsSSL();
00499 void EnableSSL(bool x = true);
00502 bool IsSSLNegotiate();
00504 void SetSSLNegotiate(bool x = true);
00507 bool IsSSLServer();
00509 void SetSSLServer(bool x = true);
00511 virtual SSL_CTX *GetSslContext() { return NULL; }
00513 virtual SSL *GetSsl() { return NULL; }
00515 #endif // HAVE_OPENSSL
00516
00517 #ifdef ENABLE_IPV6
00518
00519 void SetIpv6(bool x = true);
00522 bool IsIpv6();
00523 #endif
00524
00525 #ifdef ENABLE_POOL
00526
00529 void SetIsClient();
00531 void SetSocketType(int x);
00533 int GetSocketType();
00535 void SetSocketProtocol(const std::string& x);
00537 const std::string& GetSocketProtocol();
00544 void SetRetain();
00547 bool Retain();
00549 void CopyConnection(Socket *sock);
00551 #endif // ENABLE_POOL
00552
00553 #ifdef ENABLE_SOCKS4
00554
00557 virtual void OnSocks4Connect();
00559 virtual void OnSocks4ConnectFailed();
00561 virtual bool OnSocks4Read();
00565 bool Socks4();
00567 void SetSocks4(bool x = true);
00568
00570 void SetSocks4Host(ipaddr_t a);
00572 void SetSocks4Host(const std::string& );
00574 void SetSocks4Port(port_t p);
00576 void SetSocks4Userid(const std::string& x);
00579 ipaddr_t GetSocks4Host();
00582 port_t GetSocks4Port();
00585 const std::string& GetSocks4Userid();
00587 #endif // ENABLE_SOCKS4
00588
00589 #ifdef ENABLE_RESOLVER
00590
00596 int Resolve(const std::string& host,port_t port = 0);
00597 #ifdef ENABLE_IPV6
00598 int Resolve6(const std::string& host, port_t port = 0);
00599 #endif
00600
00604 virtual void OnResolved(int id,ipaddr_t a,port_t port);
00605 #ifdef ENABLE_IPV6
00606 virtual void OnResolved(int id,in6_addr& a,port_t port);
00607 #endif
00608
00610 int Resolve(ipaddr_t a);
00611 #ifdef ENABLE_IPV6
00612 int Resolve(in6_addr& a);
00613 #endif
00614
00617 virtual void OnReverseResolved(int id,const std::string& name);
00620 virtual void OnResolveFailed(int id);
00622 #endif // ENABLE_RESOLVER
00623
00624 #ifdef ENABLE_DETACH
00625
00630 virtual void OnDetached();
00631
00632
00633
00635 void SetDetach(bool x = true);
00638 bool IsDetach();
00639
00641 void SetDetached(bool x = true);
00644 const bool IsDetached() const;
00647 bool Detach();
00649 void SetSlaveHandler(ISocketHandler *);
00651 void DetachSocket();
00653 #endif // ENABLE_DETACH
00654
00656 void SetTrafficMonitor(IFile *p) { m_traffic_monitor = p; }
00657
00658 #ifdef ENABLE_TRIGGERS
00659
00662 void Subscribe(int id);
00664 void Unsubscribe(int id);
00666 virtual void OnTrigger(int id, const TriggerData& data);
00668 virtual void OnCancelled(int id);
00670 #endif
00671
00672 protected:
00674 Socket() : m_handler(m_handler) {}
00676 Socket(const Socket& s) : m_handler(s.m_handler) {}
00677
00679 Socket& operator=(const Socket& ) { return *this; }
00680
00682 IFile *GetTrafficMonitor() { return m_traffic_monitor; }
00683
00684
00685
00686 private:
00687 ISocketHandler& m_handler;
00688 SOCKET m_socket;
00689 bool m_bDel;
00690 bool m_bClose;
00691 time_t m_tCreate;
00692 Socket *m_parent;
00693 bool m_b_disable_read;
00694 bool m_connected;
00695 bool m_b_erased_by_handler;
00696 time_t m_tClose;
00697 std::auto_ptr<SocketAddress> m_client_remote_address;
00698 std::auto_ptr<SocketAddress> m_remote_address;
00699 IFile *m_traffic_monitor;
00700 time_t m_timeout_start;
00701 time_t m_timeout_limit;
00702 bool m_bLost;
00703
00704 #ifdef _WIN32
00705 static WSAInitializer m_winsock_init;
00706 #endif
00707
00708 #ifdef HAVE_OPENSSL
00709 bool m_b_enable_ssl;
00710 bool m_b_ssl;
00711 bool m_b_ssl_server;
00712 #endif
00713
00714 #ifdef ENABLE_IPV6
00715 bool m_ipv6;
00716 #endif
00717
00718 #ifdef ENABLE_POOL
00719 int m_socket_type;
00720 std::string m_socket_protocol;
00721 bool m_bClient;
00722 bool m_bRetain;
00723 #endif
00724
00725 #ifdef ENABLE_SOCKS4
00726 bool m_bSocks4;
00727 ipaddr_t m_socks4_host;
00728 port_t m_socks4_port;
00729 std::string m_socks4_userid;
00730 #endif
00731
00732 #ifdef ENABLE_DETACH
00733 bool m_detach;
00734 bool m_detached;
00735 SocketThread *m_pThread;
00736 ISocketHandler *m_slave_handler;
00737 #endif
00738 };
00739
00740 #ifdef SOCKETS_NAMESPACE
00741 }
00742 #endif
00743
00744
00745 #endif // _SOCKETS_Socket_H
00746