diff --git a/Csocket.cpp b/Csocket.cpp index 42db9486..78f395b2 100644 --- a/Csocket.cpp +++ b/Csocket.cpp @@ -28,7 +28,7 @@ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * -* $Revision: 1.123 $ +* $Revision: 1.131 $ */ #include "Csocket.h" @@ -965,9 +965,17 @@ bool Csock::Listen( u_short iPort, int iMaxConns, const CS_STRING & sBindHost, u if ( m_iReadSock == CS_INVALID_SOCK ) return( false ); +#ifdef HAVE_IPV6 +#ifdef IPV6_V6ONLY + // per RFC3493#5.3 + const int on = ( m_address.GetAFRequire() == CSSockAddr::RAF_INET6 ? 1 : 0 ); + if( setsockopt( m_iReadSock, IPPROTO_IPV6, IPV6_V6ONLY, (char *)&on, sizeof( on ) ) != 0 ) + PERROR( "IPV6_V6ONLY" ); +#endif /* IPV6_V6ONLY */ +#endif /* HAVE_IPV6 */ + m_address.SinFamily(); m_address.SinPort( iPort ); - if( !GetIPv6() ) { if ( bind( m_iReadSock, (struct sockaddr *) m_address.GetSockAddr(), m_address.GetSockAddrLen() ) == -1 ) @@ -1352,7 +1360,7 @@ bool Csock::AllowWrite( unsigned long long & iNOW ) const return( true ); } -bool Csock::Write( const char *data, int len ) +bool Csock::Write( const char *data, size_t len ) { m_sSend.append( data, len ); @@ -1369,7 +1377,7 @@ bool Csock::Write( const char *data, int len ) } // rate shaping - u_int iBytesToSend = 0; + u_long iBytesToSend = 0; #ifdef HAVE_LIBSSL if( m_bssl && m_sSSLBuffer.empty() && !m_bsslEstablished ) @@ -1414,7 +1422,7 @@ bool Csock::Write( const char *data, int len ) if ( m_sSSLBuffer.empty() ) // on retrying to write data, ssl wants the data in the SAME spot and the SAME size m_sSSLBuffer.append( m_sSend.data(), iBytesToSend ); - int iErr = SSL_write( m_ssl, m_sSSLBuffer.data(), m_sSSLBuffer.length() ); + int iErr = SSL_write( m_ssl, m_sSSLBuffer.data(), (int)m_sSSLBuffer.length() ); if ( ( iErr < 0 ) && ( GetSockError() == ECONNREFUSED ) ) { @@ -1467,9 +1475,9 @@ bool Csock::Write( const char *data, int len ) } #endif /* HAVE_LIBSSL */ #ifdef _WIN32 - int bytes = send( m_iWriteSock, m_sSend.data(), iBytesToSend, 0 ); + ssize_t bytes = send( m_iWriteSock, m_sSend.data(), iBytesToSend, 0 ); #else - int bytes = write( m_iWriteSock, m_sSend.data(), iBytesToSend ); + ssize_t bytes = write( m_iWriteSock, m_sSend.data(), iBytesToSend ); #endif /* _WIN32 */ if ( ( bytes == -1 ) && ( GetSockError() == ECONNREFUSED ) ) @@ -1503,9 +1511,9 @@ bool Csock::Write( const CS_STRING & sData ) return( Write( sData.c_str(), sData.length() ) ); } -int Csock::Read( char *data, int len ) +ssize_t Csock::Read( char *data, size_t len ) { - int bytes = 0; + ssize_t bytes = 0; if ( ( IsReadPaused() ) && ( SslIsEstablished() ) ) return( READ_EAGAIN ); // allow the handshake to complete first @@ -1525,7 +1533,7 @@ int Csock::Read( char *data, int len ) #ifdef HAVE_LIBSSL if ( m_bssl ) - bytes = SSL_read( m_ssl, data, len ); + bytes = SSL_read( m_ssl, data, (int)len ); else #endif /* HAVE_LIBSSL */ #ifdef _WIN32 @@ -1552,7 +1560,7 @@ int Csock::Read( char *data, int len ) #ifdef HAVE_LIBSSL if ( m_bssl ) { - int iErr = SSL_get_error( m_ssl, bytes ); + int iErr = SSL_get_error( m_ssl, (int)bytes ); if ( ( iErr != SSL_ERROR_WANT_READ ) && ( iErr != SSL_ERROR_WANT_WRITE ) ) return( READ_ERR ); else @@ -1712,12 +1720,12 @@ bool Csock::CheckTimeout( time_t iNow ) return( false ); } -void Csock::PushBuff( const char *data, int len, bool bStartAtZero ) +void Csock::PushBuff( const char *data, size_t len, bool bStartAtZero ) { if ( !m_bEnableReadLine ) return; // If the ReadLine event is disabled, just ditch here - u_int iStartPos = ( m_sbuffer.empty() || bStartAtZero ? 0 : m_sbuffer.length() - 1 ); + size_t iStartPos = ( m_sbuffer.empty() || bStartAtZero ? 0 : m_sbuffer.length() - 1 ); if ( data ) m_sbuffer.append( data, len ); @@ -2334,17 +2342,19 @@ cs_sock_t Csock::CreateSocket( bool bListen ) cs_sock_t iRet = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP ); #endif /* HAVE_IPV6 */ - if ( iRet != CS_INVALID_SOCK ) { + if ( iRet != CS_INVALID_SOCK ) + { set_close_on_exec( iRet ); - if ( bListen ) { + if ( bListen ) + { const int on = 1; - if ( setsockopt( iRet, SOL_SOCKET, SO_REUSEADDR, - (char *)&on, sizeof( on ) ) != 0 ) - PERROR( "setsockopt" ); + if ( setsockopt( iRet, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof( on ) ) != 0 ) + PERROR( "SO_REUSEADDR" ); } - } else + } + else PERROR( "socket" ); return( iRet ); diff --git a/Csocket.h b/Csocket.h index 477b7bce..fddfa872 100644 --- a/Csocket.h +++ b/Csocket.h @@ -28,7 +28,7 @@ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * -* $Revision: 1.222 $ +* $Revision: 1.223 $ */ // note to compile with win32 need to link to winsock2, using gcc its -lws2_32 @@ -565,7 +565,7 @@ public: * @param len the length of data * */ - virtual bool Write( const char *data, int len ); + virtual bool Write( const char *data, size_t len ); /** * convience function @@ -588,7 +588,7 @@ public: * Returns READ_TIMEDOUT for a connection that timed out at the TCP level * Otherwise returns the bytes read into data */ - virtual int Read( char *data, int len ); + virtual ssize_t Read( char *data, size_t len ); CS_STRING GetLocalIP(); CS_STRING GetRemoteIP(); @@ -641,7 +641,7 @@ public: * pushes data up on the buffer, if a line is ready * it calls the ReadLine event */ - virtual void PushBuff( const char *data, int len, bool bStartAtZero = false ); + virtual void PushBuff( const char *data, size_t len, bool bStartAtZero = false ); //! This gives access to the internal read buffer, if your //! not going to use ReadLine(), then you may want to clear this out @@ -829,7 +829,7 @@ public: * * Ready to read data event */ - virtual void ReadData( const char *data, int len ) {} + virtual void ReadData( const char *data, size_t len ) {} /** * Override these functions for an easy interface when using the Socket Manager * Don't bother using these callbacks if you are using this class directly (without Socket Manager) @@ -898,7 +898,7 @@ public: //! return how long it has been (in seconds) since the last read or successful write - int GetTimeSinceLastDataTransaction( time_t iNow = 0 ) + time_t GetTimeSinceLastDataTransaction( time_t iNow = 0 ) { if( m_iLastCheckTimeoutTime == 0 ) return( 0 ); @@ -1004,7 +1004,8 @@ private: ECloseType m_eCloseType; unsigned long long m_iMaxMilliSeconds, m_iLastSendTime, m_iBytesRead, m_iBytesWritten, m_iStartTime; - unsigned int m_iMaxBytes, m_iLastSend, m_iMaxStoredBufferLength, m_iTimeoutType; + unsigned int m_iMaxBytes, m_iMaxStoredBufferLength, m_iTimeoutType; + size_t m_iLastSend; CSSockAddr m_address, m_bindhost; bool m_bIsIPv6, m_bSkipConnect; @@ -1487,7 +1488,7 @@ public: CSCharBuffer cBuff( iLen ); - int bytes = pcSock->Read( cBuff(), iLen ); + ssize_t bytes = pcSock->Read( cBuff(), iLen ); if ( ( bytes != T::READ_TIMEDOUT ) && ( bytes != T::READ_CONNREFUSED ) && ( !pcSock->IsConnected() ) )