@ -218,7 +218,7 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket)
@@ -218,7 +218,7 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket)
LogPrintf ( " SOCKS5 connecting %s \n " , strDest ) ;
if ( strDest . size ( ) > 255 )
{
closes ocket( hSocket ) ;
CloseS ocket( hSocket ) ;
return error ( " Hostname too long " ) ;
}
char pszSocks5Init [ ] = " \5 \1 \0 " ;
@ -227,18 +227,18 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket)
@@ -227,18 +227,18 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket)
ssize_t ret = send ( hSocket , pszSocks5Init , nSize , MSG_NOSIGNAL ) ;
if ( ret ! = nSize )
{
closes ocket( hSocket ) ;
CloseS ocket( hSocket ) ;
return error ( " Error sending to proxy " ) ;
}
char pchRet1 [ 2 ] ;
if ( recv ( hSocket , pchRet1 , 2 , 0 ) ! = 2 )
{
closes ocket( hSocket ) ;
CloseS ocket( hSocket ) ;
return error ( " Error reading proxy response " ) ;
}
if ( pchRet1 [ 0 ] ! = 0x05 | | pchRet1 [ 1 ] ! = 0x00 )
{
closes ocket( hSocket ) ;
CloseS ocket( hSocket ) ;
return error ( " Proxy failed to initialize " ) ;
}
string strSocks5 ( " \5 \1 " ) ;
@ -250,23 +250,23 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket)
@@ -250,23 +250,23 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket)
ret = send ( hSocket , strSocks5 . c_str ( ) , strSocks5 . size ( ) , MSG_NOSIGNAL ) ;
if ( ret ! = ( ssize_t ) strSocks5 . size ( ) )
{
closes ocket( hSocket ) ;
CloseS ocket( hSocket ) ;
return error ( " Error sending to proxy " ) ;
}
char pchRet2 [ 4 ] ;
if ( recv ( hSocket , pchRet2 , 4 , 0 ) ! = 4 )
{
closes ocket( hSocket ) ;
CloseS ocket( hSocket ) ;
return error ( " Error reading proxy response " ) ;
}
if ( pchRet2 [ 0 ] ! = 0x05 )
{
closes ocket( hSocket ) ;
CloseS ocket( hSocket ) ;
return error ( " Proxy failed to accept request " ) ;
}
if ( pchRet2 [ 1 ] ! = 0x00 )
{
closes ocket( hSocket ) ;
CloseS ocket( hSocket ) ;
switch ( pchRet2 [ 1 ] )
{
case 0x01 : return error ( " Proxy error: general failure " ) ;
@ -282,7 +282,7 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket)
@@ -282,7 +282,7 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket)
}
if ( pchRet2 [ 2 ] ! = 0x00 )
{
closes ocket( hSocket ) ;
CloseS ocket( hSocket ) ;
return error ( " Error: malformed proxy response " ) ;
}
char pchRet3 [ 256 ] ;
@ -294,23 +294,23 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket)
@@ -294,23 +294,23 @@ bool static Socks5(string strDest, int port, SOCKET& hSocket)
{
ret = recv ( hSocket , pchRet3 , 1 , 0 ) ! = 1 ;
if ( ret ) {
closes ocket( hSocket ) ;
CloseS ocket( hSocket ) ;
return error ( " Error reading from proxy " ) ;
}
int nRecv = pchRet3 [ 0 ] ;
ret = recv ( hSocket , pchRet3 , nRecv , 0 ) ! = nRecv ;
break ;
}
default : closes ocket( hSocket ) ; return error ( " Error: malformed proxy response " ) ;
default : CloseS ocket( hSocket ) ; return error ( " Error: malformed proxy response " ) ;
}
if ( ret )
{
closes ocket( hSocket ) ;
CloseS ocket( hSocket ) ;
return error ( " Error reading from proxy " ) ;
}
if ( recv ( hSocket , pchRet3 , 2 , 0 ) ! = 2 )
{
closes ocket( hSocket ) ;
CloseS ocket( hSocket ) ;
return error ( " Error reading from proxy " ) ;
}
LogPrintf ( " SOCKS5 connected %s \n " , strDest ) ;
@ -344,7 +344,7 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
@@ -344,7 +344,7 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
if ( fcntl ( hSocket , F_SETFL , fFlags | O_NONBLOCK ) = = - 1 )
# endif
{
closes ocket( hSocket ) ;
CloseS ocket( hSocket ) ;
return false ;
}
@ -365,13 +365,13 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
@@ -365,13 +365,13 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
if ( nRet = = 0 )
{
LogPrint ( " net " , " connection to %s timeout \n " , addrConnect . ToString ( ) ) ;
closes ocket( hSocket ) ;
CloseS ocket( hSocket ) ;
return false ;
}
if ( nRet = = SOCKET_ERROR )
{
LogPrintf ( " select() for %s failed: %s \n " , addrConnect . ToString ( ) , NetworkErrorString ( WSAGetLastError ( ) ) ) ;
closes ocket( hSocket ) ;
CloseS ocket( hSocket ) ;
return false ;
}
socklen_t nRetSize = sizeof ( nRet ) ;
@ -382,13 +382,13 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
@@ -382,13 +382,13 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
# endif
{
LogPrintf ( " getsockopt() for %s failed: %s \n " , addrConnect . ToString ( ) , NetworkErrorString ( WSAGetLastError ( ) ) ) ;
closes ocket( hSocket ) ;
CloseS ocket( hSocket ) ;
return false ;
}
if ( nRet ! = 0 )
{
LogPrintf ( " connect() to %s failed after select(): %s \n " , addrConnect . ToString ( ) , NetworkErrorString ( nRet ) ) ;
closes ocket( hSocket ) ;
CloseS ocket( hSocket ) ;
return false ;
}
}
@ -399,7 +399,7 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
@@ -399,7 +399,7 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
# endif
{
LogPrintf ( " connect() to %s failed: %s \n " , addrConnect . ToString ( ) , NetworkErrorString ( WSAGetLastError ( ) ) ) ;
closes ocket( hSocket ) ;
CloseS ocket( hSocket ) ;
return false ;
}
}
@ -415,7 +415,7 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
@@ -415,7 +415,7 @@ bool static ConnectSocketDirectly(const CService &addrConnect, SOCKET& hSocketRe
if ( fcntl ( hSocket , F_SETFL , fFlags & ~ O_NONBLOCK ) = = SOCKET_ERROR )
# endif
{
closes ocket( hSocket ) ;
CloseS ocket( hSocket ) ;
return false ;
}
@ -1258,3 +1258,16 @@ std::string NetworkErrorString(int err)
@@ -1258,3 +1258,16 @@ std::string NetworkErrorString(int err)
return strprintf ( " %s (%d) " , s, err) ;
}
# endif
bool CloseSocket ( SOCKET & hSocket )
{
if ( hSocket = = INVALID_SOCKET )
return false ;
# ifdef WIN32
int ret = closesocket ( hSocket ) ;
# else
int ret = close ( hSocket ) ;
# endif
hSocket = INVALID_SOCKET ;
return ret ! = SOCKET_ERROR ;
}