Browse Source

Use enums instead of magic numbers on SOCKS

pull/134/head
Francisco Blas (klondike) Izquierdo Riera 10 years ago
parent
commit
46b16237b6
  1. 47
      SOCKS.cpp
  2. 52
      SOCKS.h

47
SOCKS.cpp

@ -60,23 +60,19 @@ namespace proxy @@ -60,23 +60,19 @@ namespace proxy
void SOCKSHandler::SocksRequestFailed()
{
switch (m_socksv) {
case 4:
case SOCKS4:
LogPrint(eLogWarning,"--- SOCKS4 failed");
//TODO: send the right response
boost::asio::async_write(*m_sock, boost::asio::buffer("\x00\x5b\x00\x00\x00\x00\x00\x00",8),
std::bind(&SOCKSHandler::SentSocksFailed, this, std::placeholders::_1));
break;
case 5:
assert(m_error <= 8);
case SOCKS5:
assert(m_error <= SOCKS5_ADDR_UNSUP);
LogPrint(eLogWarning,"--- SOCKS5 failed");
//TODO: use error properly and address type m_error
boost::asio::async_write(*m_sock, boost::asio::buffer(socks5Replies[m_error],10),
std::bind(&SOCKSHandler::SentSocksFailed, this, std::placeholders::_1));
break;
default:
LogPrint (eLogError,"--- SOCKS had invalid version");
Terminate();
break;
}
}
@ -84,24 +80,20 @@ namespace proxy @@ -84,24 +80,20 @@ namespace proxy
{
std::shared_ptr<std::vector<uint8_t>> response(new std::vector<uint8_t>);
switch (m_socksv) {
case 4:
case SOCKS4:
LogPrint(eLogInfo,"--- SOCKS4 connection success");
//TODO: send the right response
boost::asio::async_write(*m_sock, boost::asio::buffer("\x00\x5a\x00\x00\x00\x00\x00\x00",8),
std::bind(&SOCKSHandler::SentSocksResponse, this,
std::placeholders::_1, nullptr));
break;
case 5:
case SOCKS5:
LogPrint(eLogInfo,"--- SOCKS5 connection success");
//TODO: send the right response using the port? and the localside i2p address
boost::asio::async_write(*m_sock, boost::asio::buffer("\x05\x00\x00\x01\x00\x00\x00\x00\x00\x00",10),
std::bind(&SOCKSHandler::SentSocksResponse, this,
std::placeholders::_1, response));
break;
default:
LogPrint (eLogError,"--- SOCKS had invalid version");
Terminate();
break;
}
}
@ -149,12 +141,12 @@ namespace proxy @@ -149,12 +141,12 @@ namespace proxy
case 4:
m_state = SOCKS4A; // Switch to the 4a handler
m_pstate = GET4A_COMMAND; //Initialize the parser at the right position
m_socksv = 4;
m_socksv = SOCKS4;
return 1;
case 5:
m_state = SOCKS5_S1; // Switch to the 4a handler
m_pstate = GET5_AUTHNUM; //Initialize the parser at the right position
m_socksv = 5;
m_socksv = SOCKS5;
return 1;
default:
LogPrint(eLogError,"--- SOCKS rejected invalid version", ((int)*sock_buff));
@ -171,7 +163,7 @@ namespace proxy @@ -171,7 +163,7 @@ namespace proxy
switch (m_pstate)
{
case GET4A_COMMAND:
if ( *sock_buff != 1 ) {
if ( *sock_buff != CMD_CONNECT ) {
//TODO: we need to support binds and other shit!
LogPrint(eLogError,"--- SOCKS4a unsupported command", ((int)*sock_buff));
SocksRequestFailed();
@ -250,10 +242,10 @@ namespace proxy @@ -250,10 +242,10 @@ namespace proxy
break;
case GET5_AUTH:
m_authleft --;
if (*sock_buff == 0)
m_authchosen = 0;
if (*sock_buff == AUTH_NONE)
m_authchosen = AUTH_NONE;
if ( m_authleft == 0 ) {
if (m_authchosen == 0xff) {
if (m_authchosen == AUTH_UNACCEPTABLE) {
//TODO: we maybe want support for other methods!
LogPrint(eLogError,"--- SOCKS5 couldn't negotiate authentication");
Socks5AuthNegoFailed();
@ -286,19 +278,19 @@ namespace proxy @@ -286,19 +278,19 @@ namespace proxy
switch (m_pstate)
{
case GET5_REQUESTV:
if (*sock_buff != 5) {
if (*sock_buff != SOCKS5) {
LogPrint(eLogError,"--- SOCKS rejected unknown request version", ((int)*sock_buff));
m_error = 0x7;
m_error = SOCKS5_GEN_FAIL;
SocksRequestFailed();
return 0;
}
m_pstate = GET5_COMMAND;
break;
case GET5_COMMAND:
if ( *sock_buff != 1 ) {
if ( *sock_buff != CMD_CONNECT ) {
//TODO: we need to support binds and other shit!
LogPrint(eLogError,"--- SOCKS5 unsupported command", ((int)*sock_buff));
m_error = 0x7;
m_error = SOCKS5_CMD_UNSUP;
SocksRequestFailed();
return 0;
}
@ -307,20 +299,21 @@ namespace proxy @@ -307,20 +299,21 @@ namespace proxy
case GET5_GETRSV:
if ( *sock_buff != 0 ) {
LogPrint(eLogError,"--- SOCKS5 unknown reserved field", ((int)*sock_buff));
m_error = 0x7;
m_error = SOCKS5_GEN_FAIL;
SocksRequestFailed();
return 0;
}
m_pstate = GET5_GETADDRTYPE;
break;
case GET5_GETADDRTYPE:
if ( *sock_buff != 0x3 ) {
if ( *sock_buff != ADDR_DNS ) {
//TODO: we may want to support other address types!
LogPrint(eLogError,"--- SOCKS5 unsupported address type", ((int)*sock_buff));
m_error = 0x8;
m_error = SOCKS5_ADDR_UNSUP;
SocksRequestFailed();
return 0;
}
m_addrtype = ADDR_DNS;
m_pstate = GET5_HOST_SIZE;
break;
case GET5_HOST_SIZE:
@ -431,7 +424,7 @@ namespace proxy @@ -431,7 +424,7 @@ namespace proxy
m_stream = stream;
SocksRequestSuccess();
} else {
m_error = 0x4;
m_error = SOCKS5_HOST_UNREACH;
LogPrint (eLogError,"--- SOCKS Issue when creating the stream, check the previous warnings for more info.");
SocksRequestFailed();
}

52
SOCKS.h

@ -70,6 +70,42 @@ namespace proxy @@ -70,6 +70,42 @@ namespace proxy
GET5_PORT2,
DONE
};
enum authMethods {
AUTH_NONE = 0, //No authentication, skip to next step
AUTH_GSSAPI = 1, //GSSAPI authentication
AUTH_USERPASSWD = 2, //Username and password
AUTH_UNACCEPTABLE = 0xff //No acceptable method found
};
enum addrTypes {
ADDR_IPV4 = 1, //IPv4 address (4 octets)
ADDR_DNS = 3, // DNS name (up to 255 octets)
ADDR_IPV6 = 4 //IPV6 address (16 octets)
};
enum errTypes {
SOCKS5_OK = 0, // No error for SOCKS5
SOCKS5_GEN_FAIL = 1, // General server failure
SOCKS5_RULE_DENIED = 2, // Connection disallowed by ruleset
SOCKS5_NET_UNREACH = 3, // Network unreachable
SOCKS5_HOST_UNREACH = 4, // Host unreachable
SOCKS5_CONN_REFUSED = 5, // Connection refused by the peer
SOCKS5_TTL_EXPIRED = 6, // TTL Expired
SOCKS5_CMD_UNSUP = 7, // Command unsuported
SOCKS5_ADDR_UNSUP = 8, // Address type unsuported
SOCKS4_OK = 90, // No error for SOCKS4
SOCKS4_FAIL = 91, // Failed establishing connecting or not allowed
SOCKS4_IDENTD_MISSING = 92, // Couldn't connect to the identd server
SOCKS4_IDENTD_DIFFER = 93 // The ID reported by the application and by identd differ
};
enum cmdTypes {
CMD_CONNECT = 1, // TCP Connect
CMD_BIND = 2, // TCP Bind
CMD_UDP = 3 // UDP associate
};
enum socksVersions {
SOCKS4 = 4, // SOCKS4
SOCKS5 = 5 // SOCKS5
};
void GotClientRequest(boost::system::error_code & ecode, std::string & host, uint16_t port);
std::size_t HandleData(uint8_t *sock_buff, std::size_t len);
@ -92,7 +128,6 @@ namespace proxy @@ -92,7 +128,6 @@ namespace proxy
void HandleStreamRequestComplete (std::shared_ptr<i2p::stream::Stream> stream);
uint8_t m_sock_buff[socks_buffer_size];
SOCKSServer * m_parent;
boost::asio::ip::tcp::socket * m_sock;
std::shared_ptr<i2p::stream::Stream> m_stream;
@ -104,19 +139,18 @@ namespace proxy @@ -104,19 +139,18 @@ namespace proxy
std::string m_destination;
uint8_t m_authleft; //Authentication methods left
//TODO: this will probably be more elegant as enums
uint8_t m_authchosen; //Authentication chosen
uint8_t m_addrtype; //Address type chosen
uint8_t m_addrleft; //Address type chosen
uint8_t m_error; //Address type chosen
uint8_t m_socksv; //Address type chosen
bool m_need_more; //Address type chosen
authMethods m_authchosen; //Authentication chosen
addrTypes m_addrtype; //Address type chosen
uint8_t m_addrleft; //Octets of DNS address left
errTypes m_error; //Error cause
socksVersions m_socksv; //Socks version
bool m_need_more; //The parser still needs to receive more data
public:
SOCKSHandler(SOCKSServer * parent, boost::asio::ip::tcp::socket * sock) :
m_parent(parent), m_sock(sock), m_stream(nullptr), m_state(GET_VERSION),
m_authchosen(0xff), m_addrtype(0x01), m_error(0x01)
m_authchosen(AUTH_UNACCEPTABLE), m_addrtype(ADDR_IPV4), m_error(SOCKS5_GEN_FAIL)
{ AsyncSockRead(); m_destination.reserve(max_socks_hostname_size+1); }
~SOCKSHandler() { CloseSock(); CloseStream(); }
};

Loading…
Cancel
Save