28 if( location.
socks() )
32 throw SocksError(
"invalid port" ) ;
34 m_request = buildPdu( location.
socksFarHost() , far_port ) ;
35 m_request_offset = 0U ;
39 std::string GNet::Socks::buildPdu(
const std::string & far_host ,
unsigned int far_port )
41 g_port_t far_port_n = htons( static_cast<g_port_t>(far_port) ) ;
42 g_port_t far_port_lo = far_port_n & 0xffU ;
43 g_port_t far_port_hi = (far_port_n>>8U) & g_port_t(0xffU) ;
47 data.append( 1U , 4 ) ;
48 data.append( 1U , 1 ) ;
49 data.append( 1U , static_cast<char>(far_port_lo) ) ;
50 data.append( 1U , static_cast<char>(far_port_hi) ) ;
51 data.append( 1U , 0 ) ;
52 data.append( 1U , 0 ) ;
53 data.append( 1U , 0 ) ;
54 data.append( 1U , 1 ) ;
55 data.append( userid ) ;
56 data.append( 1U , 0 ) ;
57 data.append( far_host ) ;
58 data.append( 1U , 0 ) ;
65 if( m_request_offset >= m_request.size() )
68 const char * p = m_request.data() + m_request_offset ;
69 size_t n = m_request.size() - m_request_offset ;
71 ssize_t rc = io.
write( p , n ) ;
74 throw SocksError(
"socket write error" ) ;
76 else if( rc < 0 || static_cast<size_t>(rc) < n )
78 size_t nsent = rc < 0 ? size_t(0U) : static_cast<size_t>(rc) ;
79 m_request_offset += nsent ;
84 m_request_offset = m_request.size() ;
92 ssize_t rc = io.
read( buffer ,
sizeof(buffer) ) ;
95 throw SocksError(
"disconnected" ) ;
99 throw SocksError(
"socket read error" ) ;
107 G_ASSERT( rc >= 1 && rc <= 8 ) ;
108 m_response.append( buffer , static_cast<size_t>(rc) ) ;
111 if( m_response.size() >= 8U )
113 G_ASSERT( m_response.size() == 8U ) ;
114 if( m_response[0] != 0 )
116 throw SocksError(
"invalid response" ) ;
118 else if( m_response[1] !=
'Z' )
120 throw SocksError(
"request rejected" ) ;
An abstract interface for reading and writing from a non-blocking i/o channel.
virtual ssize_type write(const char *buf, size_type len)=0
Sends data.
bool socks() const
Returns true if a socks location.
bool send(G::ReadWrite &)
Sends the connect-request pdu. Returns true if fully sent.
bool read(G::ReadWrite &)
Reads the response.
virtual bool eWouldBlock()=0
See read() and write().
virtual ssize_type read(char *buffer, size_type buffer_length)=0
Reads data.
Socks(const Location &)
Constructor.
A class that holds a host/service name pair and the preferred address family (if any), and also the results of a name-to-address lookup, ie.
std::string socksFarHost() const
Returns the port for the socks far server.
static bool validPort(unsigned int n)
Returns true if the port number is within the valid range.
unsigned int socksFarPort() const
Returns the port number for the socks far server.