Browse Source

Reduce memory usage a bit

pull/136/head
Francisco Blas (klondike) Izquierdo Riera 10 years ago
parent
commit
610fd2ac67
  1. 40
      SOCKS.cpp
  2. 12
      SOCKS.h

40
SOCKS.cpp

@ -193,14 +193,14 @@ namespace proxy
switch (m_pstate) switch (m_pstate)
{ {
case GET5_AUTHNUM: case GET5_AUTHNUM:
m_authleft = *sock_buff; m_parseleft = *sock_buff;
m_pstate = GET5_AUTH; m_pstate = GET5_AUTH;
break; break;
case GET5_AUTH: case GET5_AUTH:
m_authleft --; m_parseleft --;
if (*sock_buff == AUTH_NONE) if (*sock_buff == AUTH_NONE)
m_authchosen = AUTH_NONE; m_authchosen = AUTH_NONE;
if ( m_authleft == 0 ) { if ( m_parseleft == 0 ) {
if (m_authchosen == AUTH_UNACCEPTABLE) { if (m_authchosen == AUTH_UNACCEPTABLE) {
//TODO: we maybe want support for other methods! //TODO: we maybe want support for other methods!
LogPrint(eLogError,"--- SOCKS5 couldn't negotiate authentication"); LogPrint(eLogError,"--- SOCKS5 couldn't negotiate authentication");
@ -276,25 +276,25 @@ namespace proxy
m_cmd = (SOCKSHandler::cmdTypes)*sock_buff; m_cmd = (SOCKSHandler::cmdTypes)*sock_buff;
switch (m_socksv) { switch (m_socksv) {
case SOCKS5: m_pstate = GET5_GETRSV; break; case SOCKS5: m_pstate = GET5_GETRSV; break;
case SOCKS4: m_pstate = GET_PORT; m_addrleft = 2; break; case SOCKS4: m_pstate = GET_PORT; m_parseleft = 2; break;
} }
break; break;
case GET_PORT: case GET_PORT:
m_port = (m_port << 8)|((uint16_t)*sock_buff); m_port = (m_port << 8)|((uint16_t)*sock_buff);
m_addrleft--; m_parseleft--;
if (m_addrleft == 0) { if (m_parseleft == 0) {
switch (m_socksv) { switch (m_socksv) {
case SOCKS5: m_pstate = DONE; break; case SOCKS5: m_pstate = DONE; break;
case SOCKS4: m_pstate = GET_IPV4; m_address.ip = 0; m_addrleft = 4; break; case SOCKS4: m_pstate = GET_IPV4; m_address.ip = 0; m_parseleft = 4; break;
} }
} }
break; break;
case GET_IPV4: case GET_IPV4:
m_address.ip = (m_address.ip << 8)|((uint32_t)*sock_buff); m_address.ip = (m_address.ip << 8)|((uint32_t)*sock_buff);
m_addrleft--; m_parseleft--;
if (m_addrleft == 0) { if (m_parseleft == 0) {
switch (m_socksv) { switch (m_socksv) {
case SOCKS5: m_pstate = GET_PORT; m_addrleft = 2; break; case SOCKS5: m_pstate = GET_PORT; m_parseleft = 2; break;
case SOCKS4: m_pstate = GET4_IDENT; m_4aip = m_address.ip; break; case SOCKS4: m_pstate = GET4_IDENT; m_4aip = m_address.ip; break;
} }
} }
@ -341,8 +341,8 @@ namespace proxy
break; break;
case GET5_GETADDRTYPE: case GET5_GETADDRTYPE:
switch (*sock_buff) { switch (*sock_buff) {
case ADDR_IPV4: m_pstate = GET_IPV4; m_address.ip = 0; m_addrleft = 4; break; case ADDR_IPV4: m_pstate = GET_IPV4; m_address.ip = 0; m_parseleft = 4; break;
case ADDR_IPV6: m_pstate = GET5_IPV6; m_addrleft = 16; break; case ADDR_IPV6: m_pstate = GET5_IPV6; m_parseleft = 16; break;
case ADDR_DNS : m_pstate = GET5_HOST_SIZE; break; case ADDR_DNS : m_pstate = GET5_HOST_SIZE; break;
default: default:
LogPrint(eLogError,"--- SOCKS5 unknown address type: ", ((int)*sock_buff)); LogPrint(eLogError,"--- SOCKS5 unknown address type: ", ((int)*sock_buff));
@ -352,24 +352,24 @@ namespace proxy
m_addrtype = (SOCKSHandler::addrTypes)*sock_buff; m_addrtype = (SOCKSHandler::addrTypes)*sock_buff;
break; break;
case GET5_IPV6: case GET5_IPV6:
m_address.ipv6[16-m_addrleft] = *sock_buff; m_address.ipv6[16-m_parseleft] = *sock_buff;
m_addrleft--; m_parseleft--;
if (m_addrleft == 0) { if (m_parseleft == 0) {
m_pstate = GET_PORT; m_pstate = GET_PORT;
m_addrleft = 2; m_parseleft = 2;
} }
break; break;
case GET5_HOST_SIZE: case GET5_HOST_SIZE:
m_addrleft = *sock_buff; m_parseleft = *sock_buff;
m_address.dns.size = 0; m_address.dns.size = 0;
m_pstate = GET5_HOST; m_pstate = GET5_HOST;
break; break;
case GET5_HOST: case GET5_HOST:
m_address.dns.push_back(*sock_buff); m_address.dns.push_back(*sock_buff);
m_addrleft--; m_parseleft--;
if (m_addrleft == 0) { if (m_parseleft == 0) {
m_pstate = GET_PORT; m_pstate = GET_PORT;
m_addrleft = 2; m_parseleft = 2;
} }
break; break;
default: default:

12
SOCKS.h

@ -123,19 +123,17 @@ namespace proxy
std::shared_ptr<i2p::stream::Stream> m_stream; std::shared_ptr<i2p::stream::Stream> m_stream;
state m_state; state m_state;
parseState m_pstate; parseState m_pstate;
uint8_t m_command; uint8_t response[7+max_socks_hostname_size];
uint16_t m_port; address m_address; //Address
uint32_t m_4aip; //Used in 4a requests uint32_t m_4aip; //Used in 4a requests
uint8_t m_authleft; //Authentication methods left uint16_t m_port;
//TODO: this will probably be more elegant as enums uint8_t m_command;
uint8_t m_parseleft; //Octets left to parse
authMethods m_authchosen; //Authentication chosen authMethods m_authchosen; //Authentication chosen
addrTypes m_addrtype; //Address type chosen addrTypes m_addrtype; //Address type chosen
address m_address; //Address
uint8_t m_addrleft; //Octets of current address left
socksVersions m_socksv; //Socks version socksVersions m_socksv; //Socks version
cmdTypes m_cmd; // Command requested cmdTypes m_cmd; // Command requested
bool m_need_more; //The parser still needs to receive more data bool m_need_more; //The parser still needs to receive more data
uint8_t response[7+max_socks_hostname_size];
public: public:
SOCKSHandler(SOCKSServer * parent, boost::asio::ip::tcp::socket * sock) : SOCKSHandler(SOCKSServer * parent, boost::asio::ip::tcp::socket * sock) :

Loading…
Cancel
Save