Thread: socks4/5 proxy question

  1. #1
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404

    socks4/5 proxy question

    I am in the process of writing my own socks4/5 proxy. I am to the point where the client has connected, sent the data with the requested ip, and have connected to that ip.

    From other topics I have seen terms like blocking/nonblocking and I think that one of these apply here.

    I have one main do loop that is basically is a recv() loop. When I make the connection to the server, where do I insert that recv?

    initconn() makes a connection to the server. I only support socks4 atm.

    Code:
    	do 
    	{
            err = recv(sckclient, recvbuf, 2048, 0);
            if (err > 0) 
    		{
    			msgnum++;
    			if (msgnum == 1)
    			{
    				if ((recvbuf[0] == 0x04) && (recvbuf[1] == 0x01))
    				{
    					sckdestin = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    				    if (sckdestin == INVALID_SOCKET) 
    					{
    						printf("Invalid Socket\n");
    				        errout(WSAGetLastError());
    				        WSACleanup();
    				        return 1;
    				    }
    					
    					err = initconn(sckdestin, destinservice);
    					if (acceptdeny(sckclient, err) == 0x01)
    						return 1;
    					printf("SOCKS 4 Connection established with TCP/IP stream connection\n");
    				}
    				else
    				{
    					printf("SOCKS 4 Connection failed\n");
    					acceptdeny(sckclient, 0x02);
    				}
    				
    			}
    			else
    			{
    				printf("Message Received\n");
    			}
    		}
            else if (err == 0)
                printf("Connection closing...\n");
            else  
    		{
                errout(WSAGetLastError());
                closesocket(sckclient);
                WSACleanup();
                return 1;
            }
    
        } while (err > 0);

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > err = initconn(sckdestin, destinservice);
    So where did these two parameters get defined?

    Also, your initial recv() makes no account of fragmentation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Sorry for maybe not being as clear as I'd hoped, but here is how they are defined...
    Code:
    SOCKET sckdestin;
    struct sockaddr_in destinservice;
    I am unsure what you mean by how recv makes no account of fragmentation.

    My question is just a logic one. If my client recv loop is always looping, where do I put my server recv loop in order for me to not lose data from either one.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    destinservice = someRandomBitOfMagic;
    Is that what happens, or is there something else?

    > I am unsure what you mean by how recv makes no account of fragmentation.
    If you send 10 bytes in a single send() call, then there's NO guarantee that the recv() will get those 10 bytes in a single call.
    You could easily get them as 6 bytes + 4 bytes.
    Or in extreme cases, 1 at a time.
    If your code doesn't allow for that, then it's not going to work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Good point about the fragmentation thing, shouldn't be too hard to fix. I remember having issues like that from my BASIC days.

    If you really want to know about destinservice, here is how it gets modified:
    Code:
    	serv.sin_family = AF_INET;
        serv.sin_addr.s_addr = ip;
        serv.sin_port = port;
    That is inside my initconn function.
    I tend to write code to get the job done first, then make it all spiffy-like later.

    My real question though is how do I get my program to be able to receive data on 2 different sockets at the same time.

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    use either select() or poll().

  7. #7
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Thank you.

    I am assuming select() is for Win32 and poll() is for unix variants?

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    396
    both exist on unix. I don't know for windows.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Force connection through proxy
    By ShadowBeast in forum C# Programming
    Replies: 6
    Last Post: 05-02-2007, 01:35 PM
  2. Simple Proxy
    By Lina in forum C Programming
    Replies: 0
    Last Post: 04-01-2007, 12:36 PM
  3. Connecting through a proxy...
    By jverkoey in forum Networking/Device Communication
    Replies: 1
    Last Post: 07-20-2005, 11:53 AM
  4. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM