Click here to Skip to main content
15,886,664 members
Articles / Desktop Programming / ATL

Socks Wrapper

Rate me:
Please Sign up or sign in to vote.
1.88/5 (12 votes)
18 Jan 20051 min read 78.5K   3.9K   27   9
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

C++
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:

C++
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.


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
dragomir30-Apr-14 1:45
dragomir30-Apr-14 1:45 
GeneralMy vote of 5 Pin
kch6263-May-13 0:13
kch6263-May-13 0:13 
GeneralMy vote of 1 Pin
Syed J Hashmi10-Aug-10 20:38
Syed J Hashmi10-Aug-10 20:38 
GeneralThanks! Pin
bmnot18-Feb-10 16:01
bmnot18-Feb-10 16:01 
Generalbullsh*t ! Pin
Elmue13-Oct-06 12:50
Elmue13-Oct-06 12:50 
Generalfix to socks request code Pin
kbrichan10-May-05 8:21
kbrichan10-May-05 8:21 
GeneralRe: fix to socks request code Pin
kbrichan23-May-05 8:46
kbrichan23-May-05 8:46 
GeneralSocks 5 Authentication Pin
Hatem Mostafa23-Jan-05 4:09
Hatem Mostafa23-Jan-05 4:09 
Generalcomplete sample project Pin
dspeziale19-Jan-05 21:53
dspeziale19-Jan-05 21:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.