From e585187b248f48b3c6e9247b49fa07c6565d65e5 Mon Sep 17 00:00:00 2001 From: maximius Date: Sat, 17 Oct 2009 15:51:44 -0700 Subject: *Backed out changeset 3be01fb200a5 --HG-- branch : trunk --- dep/src/sockets/Socket.cpp | 186 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 186 insertions(+) (limited to 'dep/src/sockets/Socket.cpp') diff --git a/dep/src/sockets/Socket.cpp b/dep/src/sockets/Socket.cpp index a0544c37db5..bf1a73abf6e 100644 --- a/dep/src/sockets/Socket.cpp +++ b/dep/src/sockets/Socket.cpp @@ -4,20 +4,25 @@ **/ /* Copyright (C) 2004-2007 Anders Hedstrom + This library is made available under the terms of the GNU GPL. + If you would like to use this library in a closed-source application, a separate license agreement is available. For information about the closed-source license agreement for the C++ sockets library, please visit http://www.alhem.net/Sockets/license.html and/or email license@alhem.net. + This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. @@ -34,28 +39,34 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #endif #include #include + #include "ISocketHandler.h" #include "Utility.h" + #include "SocketAddress.h" #include "SocketHandler.h" #ifdef ENABLE_EXCEPTIONS #include "Exception.h" #endif #include "Ipv4Address.h" + //#ifdef _DEBUG //#define DEB(x) x; fflush(stderr); //#else #define DEB(x) //#endif + #ifdef SOCKETS_NAMESPACE namespace SOCKETS_NAMESPACE { #endif + // statics #ifdef _WIN32 WSAInitializer Socket::m_winsock_init; #endif + Socket::Socket(ISocketHandler& h) //:m_flags(0) :m_handler(h) @@ -100,6 +111,7 @@ Socket::Socket(ISocketHandler& h) { } + Socket::~Socket() { Handler().Remove(this); @@ -113,18 +125,22 @@ Socket::~Socket() } } + void Socket::Init() { } + void Socket::OnRead() { } + void Socket::OnWrite() { } + void Socket::OnException() { // %! exception doesn't always mean something bad happened, this code should be reworked @@ -134,18 +150,22 @@ void Socket::OnException() SetCloseAndDelete(); } + void Socket::OnDelete() { } + void Socket::OnConnect() { } + void Socket::OnAccept() { } + int Socket::Close() { if (m_socket == INVALID_SOCKET) // this could happen @@ -171,10 +191,12 @@ int Socket::Close() return n; } + SOCKET Socket::CreateSocket(int af,int type, const std::string& protocol) { struct protoent *p = NULL; SOCKET s; + #ifdef ENABLE_POOL m_socket_type = type; m_socket_protocol = protocol; @@ -193,6 +215,7 @@ SOCKET Socket::CreateSocket(int af,int type, const std::string& protocol) } } int protno = p ? p -> p_proto : 0; + s = socket(af, type, protno); if (s == INVALID_SOCKET) { @@ -209,26 +232,31 @@ SOCKET Socket::CreateSocket(int af,int type, const std::string& protocol) return s; } + void Socket::Attach(SOCKET s) { m_socket = s; } + SOCKET Socket::GetSocket() { return m_socket; } + void Socket::SetDeleteByHandler(bool x) { m_bDel = x; } + bool Socket::DeleteByHandler() { return m_bDel; } + void Socket::SetCloseAndDelete(bool x) { if (x != m_bClose) @@ -242,21 +270,25 @@ void Socket::SetCloseAndDelete(bool x) } } + bool Socket::CloseAndDelete() { return m_bClose; } + void Socket::SetRemoteAddress(SocketAddress& ad) //struct sockaddr* sa, socklen_t l) { m_remote_address = ad.GetCopy(); } + std::auto_ptr Socket::GetRemoteSocketAddress() { return m_remote_address -> GetCopy(); } + ISocketHandler& Socket::Handler() const { #ifdef ENABLE_DETACH @@ -266,11 +298,13 @@ ISocketHandler& Socket::Handler() const return m_handler; } + ISocketHandler& Socket::MasterHandler() const { return m_handler; } + ipaddr_t Socket::GetRemoteIP4() { ipaddr_t l = 0; @@ -289,6 +323,7 @@ ipaddr_t Socket::GetRemoteIP4() return l; } + #ifdef ENABLE_IPV6 #ifdef IPPROTO_IPV6 struct in6_addr Socket::GetRemoteIP6() @@ -312,6 +347,7 @@ struct in6_addr Socket::GetRemoteIP6() #endif #endif + port_t Socket::GetRemotePort() { if (!m_remote_address.get()) @@ -321,6 +357,7 @@ port_t Socket::GetRemotePort() return m_remote_address -> GetPort(); } + std::string Socket::GetRemoteAddress() { if (!m_remote_address.get()) @@ -330,6 +367,7 @@ std::string Socket::GetRemoteAddress() return m_remote_address -> Convert(false); } + std::string Socket::GetRemoteHostname() { if (!m_remote_address.get()) @@ -339,6 +377,7 @@ std::string Socket::GetRemoteHostname() return m_remote_address -> Reverse(); } + bool Socket::SetNonblocking(bool bNb) { #ifdef _WIN32 @@ -371,6 +410,7 @@ bool Socket::SetNonblocking(bool bNb) #endif } + bool Socket::SetNonblocking(bool bNb, SOCKET s) { #ifdef _WIN32 @@ -403,11 +443,13 @@ bool Socket::SetNonblocking(bool bNb, SOCKET s) #endif } + void Socket::Set(bool bRead, bool bWrite, bool bException) { Handler().Set(m_socket, bRead, bWrite, bException); } + bool Socket::Ready() { if (m_socket != INVALID_SOCKET && !CloseAndDelete()) @@ -415,115 +457,138 @@ bool Socket::Ready() return false; } + void Socket::OnLine(const std::string& ) { } + void Socket::OnConnectFailed() { } + Socket *Socket::GetParent() { return m_parent; } + void Socket::SetParent(Socket *x) { m_parent = x; } + port_t Socket::GetPort() { Handler().LogError(this, "GetPort", 0, "GetPort only implemented for ListenSocket", LOG_LEVEL_WARNING); return 0; } + bool Socket::OnConnectRetry() { return true; } + #ifdef ENABLE_RECONNECT void Socket::OnReconnect() { } #endif + time_t Socket::Uptime() { return time(NULL) - m_tCreate; } + #ifdef ENABLE_IPV6 void Socket::SetIpv6(bool x) { m_ipv6 = x; } + bool Socket::IsIpv6() { return m_ipv6; } #endif + void Socket::DisableRead(bool x) { m_b_disable_read = x; } + bool Socket::IsDisableRead() { return m_b_disable_read; } + void Socket::SendBuf(const char *,size_t,int) { } + void Socket::Send(const std::string&,int) { } + void Socket::SetConnected(bool x) { m_connected = x; } + bool Socket::IsConnected() { return m_connected; } + void Socket::OnDisconnect() { } + void Socket::SetLost() { m_bLost = true; } + bool Socket::Lost() { return m_bLost; } + void Socket::SetErasedByHandler(bool x) { m_b_erased_by_handler = x; } + bool Socket::ErasedByHandler() { return m_b_erased_by_handler; } + time_t Socket::TimeSinceClose() { return time(NULL) - m_tClose; } + void Socket::SetClientRemoteAddress(SocketAddress& ad) { if (!ad.IsValid()) @@ -533,6 +598,7 @@ void Socket::SetClientRemoteAddress(SocketAddress& ad) m_client_remote_address = ad.GetCopy(); } + std::auto_ptr Socket::GetClientRemoteAddress() { if (!m_client_remote_address.get()) @@ -542,69 +608,83 @@ std::auto_ptr Socket::GetClientRemoteAddress() return m_client_remote_address -> GetCopy(); } + uint64_t Socket::GetBytesSent(bool) { return 0; } + uint64_t Socket::GetBytesReceived(bool) { return 0; } + #ifdef HAVE_OPENSSL void Socket::OnSSLConnect() { } + void Socket::OnSSLAccept() { } + bool Socket::SSLNegotiate() { return false; } + bool Socket::IsSSL() { return m_b_enable_ssl; } + void Socket::EnableSSL(bool x) { m_b_enable_ssl = x; } + bool Socket::IsSSLNegotiate() { return m_b_ssl; } + void Socket::SetSSLNegotiate(bool x) { m_b_ssl = x; } + bool Socket::IsSSLServer() { return m_b_ssl_server; } + void Socket::SetSSLServer(bool x) { m_b_ssl_server = x; } + void Socket::OnSSLConnectFailed() { } + void Socket::OnSSLAcceptFailed() { } #endif // HAVE_OPENSSL + #ifdef ENABLE_POOL void Socket::CopyConnection(Socket *sock) { @@ -614,110 +694,132 @@ void Socket::CopyConnection(Socket *sock) #endif SetSocketType( sock -> GetSocketType() ); SetSocketProtocol( sock -> GetSocketProtocol() ); + SetClientRemoteAddress( *sock -> GetClientRemoteAddress() ); SetRemoteAddress( *sock -> GetRemoteSocketAddress() ); } + void Socket::SetIsClient() { m_bClient = true; } + void Socket::SetSocketType(int x) { m_socket_type = x; } + int Socket::GetSocketType() { return m_socket_type; } + void Socket::SetSocketProtocol(const std::string& x) { m_socket_protocol = x; } + const std::string& Socket::GetSocketProtocol() { return m_socket_protocol; } + void Socket::SetRetain() { if (m_bClient) m_bRetain = true; } + bool Socket::Retain() { return m_bRetain; } + #endif // ENABLE_POOL + #ifdef ENABLE_SOCKS4 void Socket::OnSocks4Connect() { Handler().LogError(this, "OnSocks4Connect", 0, "Use with TcpSocket only"); } + void Socket::OnSocks4ConnectFailed() { Handler().LogError(this, "OnSocks4ConnectFailed", 0, "Use with TcpSocket only"); } + bool Socket::OnSocks4Read() { Handler().LogError(this, "OnSocks4Read", 0, "Use with TcpSocket only"); return true; } + void Socket::SetSocks4Host(const std::string& host) { Utility::u2ip(host, m_socks4_host); } + bool Socket::Socks4() { return m_bSocks4; } + void Socket::SetSocks4(bool x) { m_bSocks4 = x; } + void Socket::SetSocks4Host(ipaddr_t a) { m_socks4_host = a; } + void Socket::SetSocks4Port(port_t p) { m_socks4_port = p; } + void Socket::SetSocks4Userid(const std::string& x) { m_socks4_userid = x; } + ipaddr_t Socket::GetSocks4Host() { return m_socks4_host; } + port_t Socket::GetSocks4Port() { return m_socks4_port; } + const std::string& Socket::GetSocks4Userid() { return m_socks4_userid; } #endif // ENABLE_SOCKS4 + #ifdef ENABLE_DETACH bool Socket::Detach() { @@ -731,6 +833,7 @@ bool Socket::Detach() return true; } + void Socket::DetachSocket() { SetDetached(); @@ -738,36 +841,43 @@ void Socket::DetachSocket() m_pThread -> SetRelease(true); } + void Socket::OnDetached() { } + void Socket::SetDetach(bool x) { Handler().AddList(m_socket, LIST_DETACH, x); m_detach = x; } + bool Socket::IsDetach() { return m_detach; } + void Socket::SetDetached(bool x) { m_detached = x; } + const bool Socket::IsDetached() const { return m_detached; } + void Socket::SetSlaveHandler(ISocketHandler *p) { m_slave_handler = p; } + Socket::SocketThread::SocketThread(Socket *p) :Thread(false) ,m_socket(p) @@ -775,6 +885,7 @@ Socket::SocketThread::SocketThread(Socket *p) // Creator will release } + Socket::SocketThread::~SocketThread() { if (IsRunning()) @@ -789,6 +900,7 @@ Socket::SocketThread::~SocketThread() } } + void Socket::SocketThread::Run() { SocketHandler h; @@ -807,12 +919,14 @@ void Socket::SocketThread::Run() } #endif // ENABLE_DETACH + #ifdef ENABLE_RESOLVER int Socket::Resolve(const std::string& host,port_t port) { return Handler().Resolve(this, host, port); } + #ifdef ENABLE_IPV6 int Socket::Resolve6(const std::string& host,port_t port) { @@ -820,11 +934,13 @@ int Socket::Resolve6(const std::string& host,port_t port) } #endif + int Socket::Resolve(ipaddr_t a) { return Handler().Resolve(this, a); } + #ifdef ENABLE_IPV6 int Socket::Resolve(in6_addr& a) { @@ -832,27 +948,33 @@ int Socket::Resolve(in6_addr& a) } #endif + void Socket::OnResolved(int,ipaddr_t,port_t) { } + #ifdef ENABLE_IPV6 void Socket::OnResolved(int,in6_addr&,port_t) { } #endif + void Socket::OnReverseResolved(int,const std::string&) { } + void Socket::OnResolveFailed(int) { } #endif // ENABLE_RESOLVER + /* IP options */ + bool Socket::SetIpOptions(const void *p, socklen_t len) { #ifdef IP_OPTIONS @@ -868,6 +990,7 @@ bool Socket::SetIpOptions(const void *p, socklen_t len) #endif } + #ifdef IP_PKTINFO bool Socket::SetIpPktinfo(bool x) { @@ -881,6 +1004,7 @@ bool Socket::SetIpPktinfo(bool x) } #endif + #ifdef IP_RECVTOS bool Socket::SetIpRecvTOS(bool x) { @@ -894,6 +1018,7 @@ bool Socket::SetIpRecvTOS(bool x) } #endif + #ifdef IP_RECVTTL bool Socket::SetIpRecvTTL(bool x) { @@ -907,6 +1032,7 @@ bool Socket::SetIpRecvTTL(bool x) } #endif + #ifdef IP_RECVOPTS bool Socket::SetIpRecvopts(bool x) { @@ -920,6 +1046,7 @@ bool Socket::SetIpRecvopts(bool x) } #endif + #ifdef IP_RETOPTS bool Socket::SetIpRetopts(bool x) { @@ -933,6 +1060,7 @@ bool Socket::SetIpRetopts(bool x) } #endif + bool Socket::SetIpTOS(unsigned char tos) { #ifdef IP_TOS @@ -948,6 +1076,7 @@ bool Socket::SetIpTOS(unsigned char tos) #endif } + unsigned char Socket::IpTOS() { unsigned char tos = 0; @@ -963,6 +1092,7 @@ unsigned char Socket::IpTOS() return tos; } + bool Socket::SetIpTTL(int ttl) { #ifdef IP_TTL @@ -978,6 +1108,7 @@ bool Socket::SetIpTTL(int ttl) #endif } + int Socket::IpTTL() { int ttl = 0; @@ -993,6 +1124,7 @@ int Socket::IpTTL() return ttl; } + bool Socket::SetIpHdrincl(bool x) { #ifdef IP_HDRINCL @@ -1009,6 +1141,7 @@ bool Socket::SetIpHdrincl(bool x) #endif } + #ifdef IP_RECVERR bool Socket::SetIpRecverr(bool x) { @@ -1022,6 +1155,7 @@ bool Socket::SetIpRecverr(bool x) } #endif + #ifdef IP_MTU_DISCOVER bool Socket::SetIpMtudiscover(bool x) { @@ -1035,6 +1169,7 @@ bool Socket::SetIpMtudiscover(bool x) } #endif + #ifdef IP_MTU int Socket::IpMtu() { @@ -1048,6 +1183,7 @@ int Socket::IpMtu() } #endif + #ifdef IP_ROUTER_ALERT bool Socket::SetIpRouterAlert(bool x) { @@ -1061,6 +1197,7 @@ bool Socket::SetIpRouterAlert(bool x) } #endif + bool Socket::SetIpMulticastTTL(int ttl) { #ifdef IP_MULTICAST_TTL @@ -1076,6 +1213,7 @@ bool Socket::SetIpMulticastTTL(int ttl) #endif } + int Socket::IpMulticastTTL() { int ttl = 0; @@ -1091,6 +1229,7 @@ int Socket::IpMulticastTTL() return ttl; } + bool Socket::SetMulticastLoop(bool x) { #ifdef IP_MULTICAST_LOOP @@ -1107,6 +1246,7 @@ bool Socket::SetMulticastLoop(bool x) #endif } + #ifdef LINUX bool Socket::IpAddMembership(struct ip_mreqn& ref) { @@ -1124,6 +1264,7 @@ bool Socket::IpAddMembership(struct ip_mreqn& ref) } #endif + bool Socket::IpAddMembership(struct ip_mreq& ref) { #ifdef IP_ADD_MEMBERSHIP @@ -1139,6 +1280,7 @@ bool Socket::IpAddMembership(struct ip_mreq& ref) #endif } + #ifdef LINUX bool Socket::IpDropMembership(struct ip_mreqn& ref) { @@ -1156,6 +1298,7 @@ bool Socket::IpDropMembership(struct ip_mreqn& ref) } #endif + bool Socket::IpDropMembership(struct ip_mreq& ref) { #ifdef IP_DROP_MEMBERSHIP @@ -1171,8 +1314,10 @@ bool Socket::IpDropMembership(struct ip_mreq& ref) #endif } + /* SOCKET options */ + bool Socket::SetSoReuseaddr(bool x) { #ifdef SO_REUSEADDR @@ -1189,6 +1334,7 @@ bool Socket::SetSoReuseaddr(bool x) #endif } + bool Socket::SetSoKeepalive(bool x) { #ifdef SO_KEEPALIVE @@ -1205,6 +1351,7 @@ bool Socket::SetSoKeepalive(bool x) #endif } + #ifdef SO_NOSIGPIPE bool Socket::SetSoNosigpipe(bool x) { @@ -1218,6 +1365,7 @@ bool Socket::SetSoNosigpipe(bool x) } #endif + bool Socket::SoAcceptconn() { int value = 0; @@ -1233,6 +1381,7 @@ bool Socket::SoAcceptconn() return value ? true : false; } + #ifdef SO_BSDCOMPAT bool Socket::SetSoBsdcompat(bool x) { @@ -1246,6 +1395,7 @@ bool Socket::SetSoBsdcompat(bool x) } #endif + #ifdef SO_BINDTODEVICE bool Socket::SetSoBindtodevice(const std::string& intf) { @@ -1258,6 +1408,7 @@ bool Socket::SetSoBindtodevice(const std::string& intf) } #endif + bool Socket::SetSoBroadcast(bool x) { #ifdef SO_BROADCAST @@ -1274,6 +1425,7 @@ bool Socket::SetSoBroadcast(bool x) #endif } + bool Socket::SetSoDebug(bool x) { #ifdef SO_DEBUG @@ -1290,6 +1442,7 @@ bool Socket::SetSoDebug(bool x) #endif } + int Socket::SoError() { int value = 0; @@ -1305,6 +1458,7 @@ int Socket::SoError() return value; } + bool Socket::SetSoDontroute(bool x) { #ifdef SO_DONTROUTE @@ -1321,6 +1475,7 @@ bool Socket::SetSoDontroute(bool x) #endif } + bool Socket::SetSoLinger(int onoff, int linger) { #ifdef SO_LINGER @@ -1339,6 +1494,7 @@ bool Socket::SetSoLinger(int onoff, int linger) #endif } + bool Socket::SetSoOobinline(bool x) { #ifdef SO_OOBINLINE @@ -1355,6 +1511,7 @@ bool Socket::SetSoOobinline(bool x) #endif } + #ifdef SO_PASSCRED bool Socket::SetSoPasscred(bool x) { @@ -1368,6 +1525,7 @@ bool Socket::SetSoPasscred(bool x) } #endif + #ifdef SO_PEERCRED bool Socket::SoPeercred(struct ucred& ucr) { @@ -1380,6 +1538,7 @@ bool Socket::SoPeercred(struct ucred& ucr) } #endif + #ifdef SO_PRIORITY bool Socket::SetSoPriority(int x) { @@ -1392,6 +1551,7 @@ bool Socket::SetSoPriority(int x) } #endif + bool Socket::SetSoRcvlowat(int x) { #ifdef SO_RCVLOWAT @@ -1407,6 +1567,7 @@ bool Socket::SetSoRcvlowat(int x) #endif } + bool Socket::SetSoSndlowat(int x) { #ifdef SO_SNDLOWAT @@ -1422,6 +1583,7 @@ bool Socket::SetSoSndlowat(int x) #endif } + bool Socket::SetSoRcvtimeo(struct timeval& tv) { #ifdef SO_RCVTIMEO @@ -1437,6 +1599,7 @@ bool Socket::SetSoRcvtimeo(struct timeval& tv) #endif } + bool Socket::SetSoSndtimeo(struct timeval& tv) { #ifdef SO_SNDTIMEO @@ -1452,6 +1615,7 @@ bool Socket::SetSoSndtimeo(struct timeval& tv) #endif } + bool Socket::SetSoRcvbuf(int x) { #ifdef SO_RCVBUF @@ -1467,6 +1631,7 @@ bool Socket::SetSoRcvbuf(int x) #endif } + int Socket::SoRcvbuf() { int value = 0; @@ -1482,6 +1647,7 @@ int Socket::SoRcvbuf() return value; } + #ifdef SO_RCVBUFFORCE bool Socket::SetSoRcvbufforce(int x) { @@ -1494,6 +1660,7 @@ bool Socket::SetSoRcvbufforce(int x) } #endif + bool Socket::SetSoSndbuf(int x) { #ifdef SO_SNDBUF @@ -1509,6 +1676,7 @@ bool Socket::SetSoSndbuf(int x) #endif } + int Socket::SoSndbuf() { int value = 0; @@ -1524,6 +1692,7 @@ int Socket::SoSndbuf() return value; } + #ifdef SO_SNDBUFFORCE bool Socket::SetSoSndbufforce(int x) { @@ -1536,6 +1705,7 @@ bool Socket::SetSoSndbufforce(int x) } #endif + #ifdef SO_TIMESTAMP bool Socket::SetSoTimestamp(bool x) { @@ -1549,6 +1719,7 @@ bool Socket::SetSoTimestamp(bool x) } #endif + int Socket::SoType() { int value = 0; @@ -1564,26 +1735,31 @@ int Socket::SoType() return value; } + #ifdef ENABLE_TRIGGERS void Socket::Subscribe(int id) { Handler().Subscribe(id, this); } + void Socket::Unsubscribe(int id) { Handler().Unsubscribe(id, this); } + void Socket::OnTrigger(int, const TriggerData&) { } + void Socket::OnCancelled(int) { } #endif + void Socket::SetTimeout(time_t secs) { if (!secs) @@ -1596,14 +1772,17 @@ void Socket::SetTimeout(time_t secs) m_timeout_limit = secs; } + void Socket::OnTimeout() { } + void Socket::OnConnectTimeout() { } + bool Socket::Timeout(time_t tnow) { if (tnow - m_timeout_start > m_timeout_limit) @@ -1611,6 +1790,7 @@ bool Socket::Timeout(time_t tnow) return false; } + /** Returns local port number for bound socket file descriptor. */ port_t Socket::GetSockPort() { @@ -1633,6 +1813,7 @@ port_t Socket::GetSockPort() return ntohs(sa.sin_port); } + /** Returns local ipv4 address for bound socket file descriptor. */ ipaddr_t Socket::GetSockIP4() { @@ -1653,6 +1834,7 @@ ipaddr_t Socket::GetSockIP4() return a; } + /** Returns local ipv4 address as text for bound socket file descriptor. */ std::string Socket::GetSockAddress() { @@ -1672,6 +1854,7 @@ std::string Socket::GetSockAddress() return addr.Convert(); } + #ifdef ENABLE_IPV6 #ifdef IPPROTO_IPV6 /** Returns local ipv6 address for bound socket file descriptor. */ @@ -1690,6 +1873,7 @@ struct in6_addr Socket::GetSockIP6() return a; } + /** Returns local ipv6 address as text for bound socket file descriptor. */ std::string Socket::GetSockAddress6() { @@ -1707,7 +1891,9 @@ std::string Socket::GetSockAddress6() #endif #endif + #ifdef SOCKETS_NAMESPACE } #endif + -- cgit v1.2.3