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
00031
00032 #ifndef _SOCKETS_Socket_H
00033 #define _SOCKETS_Socket_H
00034 #include "sockets-config.h"
00035
00036 #include <string>
00037 #include <vector>
00038 #include <list>
00039 #include "socket_include.h"
00040 #ifdef HAVE_OPENSSL
00041 #include <openssl/ssl.h>
00042 #endif
00043
00044 #include <time.h>
00045 #include "SocketAddress.h"
00046 #include "Thread.h"
00047
00048
00049 #ifdef SOCKETS_NAMESPACE
00050 namespace SOCKETS_NAMESPACE {
00051 #endif
00052
00053
00054 class ISocketHandler;
00055 class SocketAddress;
00056 class IFile;
00057 class SocketThread;
00058
00059
00063 class Socket
00064 {
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 public:
00105 Socket(ISocketHandler&);
00106
00107 virtual ~Socket();
00108
00113 virtual Socket *Create() { return NULL; }
00114
00118 ISocketHandler& Handler() const;
00119
00124 ISocketHandler& MasterHandler() const;
00125
00133 virtual void Init();
00134
00139 SOCKET CreateSocket(int af,int type,const std::string& protocol = "");
00140
00143 void Attach(SOCKET s);
00144
00146 SOCKET GetSocket();
00147
00150 virtual int Close();
00151
00154 virtual bool Ready();
00155
00158 Socket *GetParent();
00159
00162 void SetParent(Socket *);
00163
00165 virtual port_t GetPort();
00166
00168 bool SetNonblocking(bool);
00169
00171 bool SetNonblocking(bool, SOCKET);
00172
00174 time_t Uptime();
00175
00177 void SetClientRemoteAddress(SocketAddress&);
00178
00180 std::auto_ptr<SocketAddress> GetClientRemoteAddress();
00181
00183 virtual void SendBuf(const char *,size_t,int = 0);
00184
00186 virtual void Send(const std::string&,int = 0);
00187
00189 virtual uint64_t GetBytesSent(bool clear = false);
00190
00192 virtual uint64_t GetBytesReceived(bool clear = false);
00193
00194
00195
00197 void SetTimeout(time_t secs);
00198
00199 bool CheckTimeout();
00200
00202 bool Timeout(time_t tnow);
00203
00205 void SetRemoteAddress(SocketAddress&);
00206
00209
00211 virtual void OnRead();
00213 virtual void OnWrite();
00215 virtual void OnException();
00217 virtual void OnDelete();
00219 virtual void OnConnect();
00221 virtual void OnAccept();
00224 virtual void OnLine(const std::string& );
00226 virtual void OnConnectFailed();
00233 virtual void OnOptions(int family,int type,int protocol,SOCKET s) = 0;
00235 virtual bool OnConnectRetry();
00236 #ifdef ENABLE_RECONNECT
00237
00238 virtual void OnReconnect();
00239 #endif
00240
00241 virtual void OnDisconnect();
00248 virtual void OnDisconnect(short info, int code);
00250 virtual void OnTimeout();
00252 virtual void OnConnectTimeout();
00254
00259 void SetDeleteByHandler(bool = true);
00262 bool DeleteByHandler();
00263
00264
00265
00267 void SetCloseAndDelete(bool = true);
00270 bool CloseAndDelete();
00271
00273 time_t TimeSinceClose();
00274
00276 void DisableRead(bool x = true);
00279 bool IsDisableRead();
00280
00282 void SetConnected(bool = true);
00285 bool IsConnected();
00286
00288 void SetLost();
00291 bool Lost();
00292
00294 void SetErasedByHandler(bool x = true);
00296 bool ErasedByHandler();
00297
00299
00303 std::auto_ptr<SocketAddress> GetRemoteSocketAddress();
00305 ipaddr_t GetRemoteIP4();
00306 #ifdef ENABLE_IPV6
00307
00308 #ifdef IPPROTO_IPV6
00309 struct in6_addr GetRemoteIP6();
00310 #endif
00311 #endif
00312
00313 port_t GetRemotePort();
00315 std::string GetRemoteAddress();
00317 std::string GetRemoteHostname();
00319
00321 port_t GetSockPort();
00323 ipaddr_t GetSockIP4();
00325 std::string GetSockAddress();
00326 #ifdef ENABLE_IPV6
00327 #ifdef IPPROTO_IPV6
00328
00329 struct in6_addr GetSockIP6();
00331 std::string GetSockAddress6();
00332 #endif
00333 #endif
00334 socketuid_t UniqueIdentifier() { return m_uid; }
00335
00336
00342
00343
00344
00346
00347 bool SetIpOptions(const void *p, socklen_t len);
00348 bool SetIpTOS(unsigned char tos);
00349 unsigned char IpTOS();
00350 bool SetIpTTL(int ttl);
00351 int IpTTL();
00352 bool SetIpHdrincl(bool x = true);
00353 bool SetIpMulticastTTL(int);
00354 int IpMulticastTTL();
00355 bool SetMulticastLoop(bool x = true);
00356 bool IpAddMembership(struct ip_mreq&);
00357 bool IpDropMembership(struct ip_mreq&);
00358
00359 #ifdef IP_PKTINFO
00360 bool SetIpPktinfo(bool x = true);
00361 #endif
00362 #ifdef IP_RECVTOS
00363 bool SetIpRecvTOS(bool x = true);
00364 #endif
00365 #ifdef IP_RECVTTL
00366 bool SetIpRecvTTL(bool x = true);
00367 #endif
00368 #ifdef IP_RECVOPTS
00369 bool SetIpRecvopts(bool x = true);
00370 #endif
00371 #ifdef IP_RETOPTS
00372 bool SetIpRetopts(bool x = true);
00373 #endif
00374 #ifdef IP_RECVERR
00375 bool SetIpRecverr(bool x = true);
00376 #endif
00377 #ifdef IP_MTU_DISCOVER
00378 bool SetIpMtudiscover(bool x = true);
00379 #endif
00380 #ifdef IP_MTU
00381 int IpMtu();
00382 #endif
00383 #ifdef IP_ROUTER_ALERT
00384 bool SetIpRouterAlert(bool x = true);
00385 #endif
00386 #ifdef LINUX
00387 bool IpAddMembership(struct ip_mreqn&);
00388 #endif
00389 #ifdef LINUX
00390 bool IpDropMembership(struct ip_mreqn&);
00391 #endif
00392
00393
00394
00397
00398 bool SoAcceptconn();
00399 bool SetSoBroadcast(bool x = true);
00400 bool SetSoDebug(bool x = true);
00401 int SoError();
00402 bool SetSoDontroute(bool x = true);
00403 bool SetSoLinger(int onoff, int linger);
00404 bool SetSoOobinline(bool x = true);
00405 bool SetSoRcvlowat(int);
00406 bool SetSoSndlowat(int);
00407 bool SetSoRcvtimeo(struct timeval&);
00408 bool SetSoSndtimeo(struct timeval&);
00409 bool SetSoRcvbuf(int);
00410 int SoRcvbuf();
00411 bool SetSoSndbuf(int);
00412 int SoSndbuf();
00413 int SoType();
00414 bool SetSoReuseaddr(bool x = true);
00415 bool SetSoKeepalive(bool x = true);
00416
00417 #ifdef SO_BSDCOMPAT
00418 bool SetSoBsdcompat(bool x = true);
00419 #endif
00420 #ifdef SO_BINDTODEVICE
00421 bool SetSoBindtodevice(const std::string& intf);
00422 #endif
00423 #ifdef SO_PASSCRED
00424 bool SetSoPasscred(bool x = true);
00425 #endif
00426 #ifdef SO_PEERCRED
00427 bool SoPeercred(struct ucred& );
00428 #endif
00429 #ifdef SO_PRIORITY
00430 bool SetSoPriority(int);
00431 #endif
00432 #ifdef SO_RCVBUFFORCE
00433 bool SetSoRcvbufforce(int);
00434 #endif
00435 #ifdef SO_SNDBUFFORCE
00436 bool SetSoSndbufforce(int);
00437 #endif
00438 #ifdef SO_TIMESTAMP
00439 bool SetSoTimestamp(bool x = true);
00440 #endif
00441 #ifdef SO_NOSIGPIPE
00442 bool SetSoNosigpipe(bool x = true);
00443 #endif
00444
00445
00446
00447
00448
00449
00450
00452 void SetCallOnConnect(bool x = true);
00453
00456 bool CallOnConnect();
00457
00458
00459
00461 void SetRetryClientConnect(bool x = true);
00462
00465 bool RetryClientConnect();
00466
00467 #ifdef HAVE_OPENSSL
00468
00471 virtual void OnSSLConnect();
00473 virtual void OnSSLAccept();
00475 virtual void OnSSLConnectFailed();
00477 virtual void OnSSLAcceptFailed();
00479 virtual bool SSLNegotiate();
00482 bool IsSSL();
00484 void EnableSSL(bool x = true);
00487 bool IsSSLNegotiate();
00489 void SetSSLNegotiate(bool x = true);
00492 bool IsSSLServer();
00494 void SetSSLServer(bool x = true);
00496 virtual SSL_CTX *GetSslContext() { return NULL; }
00498 virtual SSL *GetSsl() { return NULL; }
00500 #endif // HAVE_OPENSSL
00501
00502 #ifdef ENABLE_IPV6
00503
00504 void SetIpv6(bool x = true);
00507 bool IsIpv6();
00508 #endif
00509
00510 #ifdef ENABLE_POOL
00511
00514 void SetIsClient();
00516 void SetSocketType(int x);
00518 int GetSocketType();
00520 void SetSocketProtocol(const std::string& x);
00522 const std::string& GetSocketProtocol();
00529 void SetRetain();
00532 bool Retain();
00534 void CopyConnection(Socket *sock);
00536 #endif // ENABLE_POOL
00537
00538 #ifdef ENABLE_SOCKS4
00539
00542 virtual void OnSocks4Connect();
00544 virtual void OnSocks4ConnectFailed();
00546 virtual bool OnSocks4Read();
00550 bool Socks4();
00552 void SetSocks4(bool x = true);
00553
00555 void SetSocks4Host(ipaddr_t a);
00557 void SetSocks4Host(const std::string& );
00559 void SetSocks4Port(port_t p);
00561 void SetSocks4Userid(const std::string& x);
00564 ipaddr_t GetSocks4Host();
00567 port_t GetSocks4Port();
00570 const std::string& GetSocks4Userid();
00572 #endif // ENABLE_SOCKS4
00573
00574 #ifdef ENABLE_RESOLVER
00575
00581 int Resolve(const std::string& host,port_t port = 0);
00582 #ifdef ENABLE_IPV6
00583 int Resolve6(const std::string& host, port_t port = 0);
00584 #endif
00585
00589 virtual void OnResolved(int id,ipaddr_t a,port_t port);
00590 #ifdef ENABLE_IPV6
00591 virtual void OnResolved(int id,in6_addr& a,port_t port);
00592 #endif
00593
00595 int Resolve(ipaddr_t a);
00596 #ifdef ENABLE_IPV6
00597 int Resolve(in6_addr& a);
00598 #endif
00599
00602 virtual void OnReverseResolved(int id,const std::string& name);
00605 virtual void OnResolveFailed(int id);
00607 #endif // ENABLE_RESOLVER
00608
00609 #ifdef ENABLE_DETACH
00610
00615 virtual void OnDetached();
00616
00617
00618
00620 void SetDetach(bool x = true);
00623 bool IsDetach();
00624
00626 void SetDetached(bool x = true);
00629 bool IsDetached() const;
00632 bool Detach();
00634 void SetSlaveHandler(ISocketHandler *);
00636 void DetachSocket();
00638 #endif // ENABLE_DETACH
00639
00641 void SetTrafficMonitor(IFile *p) { m_traffic_monitor = p; }
00642
00643 protected:
00645 Socket() : m_handler(m_handler) {}
00647 Socket(const Socket& s) : m_handler(s.m_handler) {}
00648
00650 Socket& operator=(const Socket& ) { return *this; }
00651
00653 IFile *GetTrafficMonitor() { return m_traffic_monitor; }
00654
00655
00656
00657 private:
00658 ISocketHandler& m_handler;
00659 SOCKET m_socket;
00660 bool m_bDel;
00661 bool m_bClose;
00662 time_t m_tCreate;
00663 Socket *m_parent;
00664 bool m_b_disable_read;
00665 bool m_connected;
00666 bool m_b_erased_by_handler;
00667 time_t m_tClose;
00668 std::auto_ptr<SocketAddress> m_client_remote_address;
00669 std::auto_ptr<SocketAddress> m_remote_address;
00670 IFile *m_traffic_monitor;
00671 time_t m_timeout_start;
00672 time_t m_timeout_limit;
00673 bool m_bLost;
00674 static socketuid_t m_next_uid;
00675 socketuid_t m_uid;
00676 bool m_call_on_connect;
00677 bool m_b_retry_connect;
00678
00679 #ifdef _WIN32
00680 static WSAInitializer m_winsock_init;
00681 #endif
00682
00683 #ifdef HAVE_OPENSSL
00684 bool m_b_enable_ssl;
00685 bool m_b_ssl;
00686 bool m_b_ssl_server;
00687 #endif
00688
00689 #ifdef ENABLE_IPV6
00690 bool m_ipv6;
00691 #endif
00692
00693 #ifdef ENABLE_POOL
00694 int m_socket_type;
00695 std::string m_socket_protocol;
00696 bool m_bClient;
00697 bool m_bRetain;
00698 #endif
00699
00700 #ifdef ENABLE_SOCKS4
00701 bool m_bSocks4;
00702 ipaddr_t m_socks4_host;
00703 port_t m_socks4_port;
00704 std::string m_socks4_userid;
00705 #endif
00706
00707 #ifdef ENABLE_DETACH
00708 bool m_detach;
00709 bool m_detached;
00710 SocketThread *m_pThread;
00711 ISocketHandler *m_slave_handler;
00712 #endif
00713 };
00714
00715 #ifdef SOCKETS_NAMESPACE
00716 }
00717 #endif
00718
00719 #endif // _SOCKETS_Socket_H
00720