diff --git a/Csocket.cpp b/Csocket.cpp index a5e18cbb..34778cc1 100644 --- a/Csocket.cpp +++ b/Csocket.cpp @@ -28,7 +28,7 @@ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * -* $Revision: 1.135 $ +* $Revision: 1.138 $ */ #include "Csocket.h" @@ -1026,25 +1026,21 @@ cs_sock_t Csock::Accept( CS_STRING & sHost, u_short & iRPort ) if( iSock != CS_INVALID_SOCK ) { getpeername( iSock, (struct sockaddr *) &client, &clen ); - sHost = inet_ntoa( client.sin_addr ); + sHost = ConvertAddress( &client.sin_addr, false ); iRPort = ntohs( client.sin_port ); } } #ifdef HAVE_IPV6 else { - char straddr[INET6_ADDRSTRLEN]; struct sockaddr_in6 client; socklen_t clen = sizeof( client ); iSock = accept( m_iReadSock, (struct sockaddr *) &client, &clen ); if( iSock != CS_INVALID_SOCK ) { getpeername( iSock, (struct sockaddr *) &client, &clen ); - if( inet_ntop( AF_INET6, &client.sin6_addr, straddr, sizeof(straddr) ) > 0 ) - { - sHost = straddr; - iRPort = ntohs( client.sin6_port ); - } + sHost = ConvertAddress( &client.sin6_addr, true ); + iRPort = ntohs( client.sin6_port ); } } #endif /* HAVE_IPV6 */ @@ -1640,25 +1636,40 @@ CS_STRING Csock::GetRemoteIP() struct sockaddr_in mRemoteAddr; socklen_t mRemoteLen = sizeof( mRemoteAddr ); if ( getpeername( iSock, (struct sockaddr *) &mRemoteAddr, &mRemoteLen ) == 0 ) - m_sRemoteIP = inet_ntoa( mRemoteAddr.sin_addr ); + m_sRemoteIP = ConvertAddress( &mRemoteAddr.sin_addr, false ); } #ifdef HAVE_IPV6 else { - char straddr[INET6_ADDRSTRLEN]; struct sockaddr_in6 mRemoteAddr; socklen_t mRemoteLen = sizeof( mRemoteAddr ); - if ( ( getpeername( iSock, (struct sockaddr *) &mRemoteAddr, &mRemoteLen ) == 0 ) - && ( inet_ntop( AF_INET6, &mRemoteAddr.sin6_addr, straddr, sizeof(straddr) ) ) ) - { - m_sRemoteIP = straddr; - } + if ( getpeername( iSock, (struct sockaddr *) &mRemoteAddr, &mRemoteLen ) == 0 ) + m_sRemoteIP = ConvertAddress( &mRemoteAddr.sin6_addr, true ); } #endif /* HAVE_IPV6 */ return( m_sRemoteIP ); } +CS_STRING Csock::ConvertAddress( void *addr, bool bIPv6 ) +{ + CS_STRING sRet; + + if( !bIPv6 ) + { + in_addr *p = (in_addr*) addr; + sRet = inet_ntoa(*p); + } + else + { + char straddr[INET6_ADDRSTRLEN]; + if( inet_ntop( AF_INET6, addr, straddr, sizeof(straddr) ) > 0 ) + sRet = straddr; + } + + return( sRet ); +} + bool Csock::IsConnected() { return( m_bIsConnected ); } void Csock::SetIsConnected( bool b ) { m_bIsConnected = b; } @@ -2169,6 +2180,7 @@ int Csock::GetPending() #else int iBytes = SSL_pending( m_ssl ); ERR_clear_error(); // to get safer handling, upgrade your openssl version! + return( iBytes ); #endif /* OPENSSL_VERSION_NUMBER */ } else diff --git a/Csocket.h b/Csocket.h index 3e5ccca8..c01a573e 100644 --- a/Csocket.h +++ b/Csocket.h @@ -28,7 +28,7 @@ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * -* $Revision: 1.228 $ +* $Revision: 1.230 $ */ // note to compile with win32 need to link to winsock2, using gcc its -lws2_32 @@ -599,6 +599,8 @@ public: CS_STRING GetLocalIP(); CS_STRING GetRemoteIP(); + virtual CS_STRING ConvertAddress( void *addr, bool bIPv6 = false ); + //! Tells you if the socket is connected virtual bool IsConnected(); //! Sets the sock, telling it its connected (internal use only) diff --git a/Socket.cpp b/Socket.cpp index 2a1d29f2..866e4a58 100644 --- a/Socket.cpp +++ b/Socket.cpp @@ -28,6 +28,12 @@ unsigned int CSockManager::GetAnonConnectionCount(const CString &sIP) const { return ret; } +CS_STRING CZNCSock::ConvertAddress(void *addr, bool ipv6) { + CString sRet = Csock::ConvertAddress(addr, ipv6); + sRet.TrimPrefix("::ffff:"); + return sRet; +} + /////////////////// CSocket /////////////////// CSocket::CSocket(CModule* pModule) : CZNCSock() { m_pModule = pModule; diff --git a/Socket.h b/Socket.h index 9af881e6..9c9507f1 100644 --- a/Socket.h +++ b/Socket.h @@ -18,6 +18,8 @@ public: CZNCSock(int timeout = 60) : Csock(timeout) {} CZNCSock(const CString& sHost, u_short port, int timeout = 60) : Csock(sHost, port, timeout) {} ~CZNCSock() {} + + virtual CS_STRING ConvertAddress(void *addr, bool ipv6 = false); }; enum EAddrType {