Click here to Skip to main content
Licence 
First Posted 18 Jan 2005
Views 42,613
Bookmarked 19 times

Socks Wrapper

By | 18 Jan 2005 | Article
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();
// Memeber 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

RFC: SOCKS5 visit: RFC 1928 (RFC1928).

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);

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

About the Author

M Ravi Kiran

Web Developer

United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberSyed Javed20:38 10 Aug '10  
GeneralThanks! Pinmemberbmnot16:01 18 Feb '10  
Generalbullsh*t ! PinmemberElmue12:50 13 Oct '06  
Generalfix to socks request code Pinmemberkbrichan8:21 10 May '05  
GeneralRe: fix to socks request code Pinmemberkbrichan8:46 23 May '05  
GeneralSocks 5 Authentication PinmemberHatemMostafa4:09 23 Jan '05  
Generalcomplete sample project Pinmemberdspeziale21:53 19 Jan '05  

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

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 19 Jan 2005
Article Copyright 2005 by M Ravi Kiran
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid