Update to latest Csocket

This adds Csock::ConvertAddress() which produces printable addresses. We use
this function in CZNCSock to strip away 4in6 prefixes (e.g. ::ffff:127.0.0.1).


git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@1930 726aef4b-f618-498e-8847-2d620e286838
This commit is contained in:
psychon
2010-04-25 11:17:42 +00:00
parent e2b91a32c9
commit 635fb4bc00
4 changed files with 38 additions and 16 deletions

View File

@@ -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