Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Socks Wrapper

0.00/5 (No votes)
18 Jan 2005 1  
Establishes socket through Socks Proxy version 4 and version 5

Introduction

What is Socks Wrapper?

Socks component/wrapper is a C++ class, which is helpful in establishing a socket through a Socks Proxy for TCP-based clients.

How Is It Useful?

This wrapper is useful to establish socket connection behind socks proxy. It supports Socks Version 4.0 and Version 5.0. It supports for TCP proxy across firewalls.

The Problem It Solves

Socks relay TCP sessions at a firewall host to allow application users transparent access across the firewall. This can be used for many different services, such as telnet, FTP, finger, whois, gopher, WWW, etc. Access control can be applied at the beginning of each TCP session; thereafter the server simply relays the data between the client and the application server, incurring minimum processing overhead. It should also be easy for it to accommodate applications, which use encryption to protect their traffic from nosey snoopers.

The Wrapper Class

class CSocks
{
public:
     CSocks();
     ~CSocks();
     CSocks(char *in_szSocksAddress, int in_ScoksPort, 
     char *in_szDestinationAddress , int in_DestinationPort);
//Set Methods 
     void SetSocksPort(int in_Port);
     void SetDestinationPort(int in_Port);
     void SetSocksAddress(char *in_szSocksAddress);
     void SetDestinationAddress(char *in_szDestinationAddress);
     int  Connect();
     void SetVersion(int in_Version);
     void SetUserName(char* in_szUserName);
     void SetPassword(char* in_szPassword);
     void SetSocksInterval(int in_MilliSeconds);
//Get Methods
     char *GetUserName();
     char *GetPassword();
     int  GetVersion();
     char *GetLastErrorMessage();
private:
     int ConnectToSocksFour( SOCKET in_Socket,
          const struct sockaddr FAR *in_szName,
          int nameLen,
          const struct sockaddr FAR *in_Socks,
          int socksLen,
          const char *szUserId );
     int ConnectToSocksFive( SOCKET in_Socket,
          const struct sockaddr FAR *in_szName,
          int nameLen,
          const struct sockaddr FAR *in_Socks,
          int socksLen,
          const char *szUserId );
 
     int InitiateSocket();
// Member Variables
public :
     BOOL m_Authentication;
     int  m_Interval;
     BOOL m_IsError;
private:
     int  m_Version;
     int  m_Socket; 
     int  m_SocksPort;
     int  m_DestinationPort;
     char m_SocksAddress[25];
     char m_DesitnationAddress[25];
     char m_UserName[100];
     char m_Password[100];
     char m_szErrorMessage[255];
};

Reference

Using the Code

This article can be used in any TCP-based client application, which supports Socks complaints. In C++ applications, you can simply attach the Socks.h file and Socks.cpp file to your project/solution. You can implement this as follows:

CSocks cs;
Socket socketId;
cs.SetVersion(SOCKS_VER4);
cs.SetSocksPort(1080);
cs.SetDestinationPort(1001);
cs.SetDestinationAddress("128.0.0.1");
cs.SetSocksAddress("192.0.0.1");
//cs.SetVersion(SOCKS_VER5);      // If you want to connecto Ver 5.0
//cs.SetSocksAddress("192.0.0.1");
socketId = cs.Connect();

// if failed
if (cs.m_IsError)
{
     printf( "\n%s", cs.GetLastErrorMessage());
     getch();
     return 0;
}

// send packet for requesting to a server
if(socketId > 0)
{
     // you can continue with your application
}
else
{
     //Socks Server / Destination Server not started..
}
closesocket(socketId);

History

  • 19th January, 2005: Initial version

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below. A list of licenses authors might use can be found here.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here