C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 11-27-2008, 08:41 AM   #1
Registered User
 
carrotcake1029's Avatar
 
Join Date: Apr 2008
Posts: 309
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);
carrotcake1029 is offline   Reply With Quote
Old 11-27-2008, 10:11 AM   #2
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,650
> 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.

Salem is offline   Reply With Quote
Old 11-27-2008, 10:38 AM   #3
Registered User
 
carrotcake1029's Avatar
 
Join Date: Apr 2008
Posts: 309
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.
carrotcake1029 is offline   Reply With Quote
Old 11-27-2008, 11:02 AM   #4
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,650
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.

Salem is offline   Reply With Quote
Old 11-27-2008, 11:11 AM   #5
Registered User
 
carrotcake1029's Avatar
 
Join Date: Apr 2008
Posts: 309
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.
carrotcake1029 is offline   Reply With Quote
Old 11-27-2008, 11:16 AM   #6
Registered User
 
Join Date: Apr 2008
Posts: 282
use either select() or poll().
root4 is offline   Reply With Quote
Old 11-27-2008, 11:51 AM   #7
Registered User
 
carrotcake1029's Avatar
 
Join Date: Apr 2008
Posts: 309
Thank you.

I am assuming select() is for Win32 and poll() is for unix variants?
carrotcake1029 is offline   Reply With Quote
Old 11-27-2008, 12:44 PM   #8
Registered User
 
Join Date: Apr 2008
Posts: 282
both exist on unix. I don't know for windows.
root4 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Force connection through proxy ShadowBeast C# Programming 6 05-02-2007 01:35 PM
Simple Proxy Lina C Programming 0 04-01-2007 12:36 PM
Connecting through a proxy... jverkoey Networking/Device Communication 1 07-20-2005 11:53 AM
Alice.... Lurker General Discussions 16 06-20-2005 02:51 PM
Question... TechWins A Brief History of Cprogramming.com 16 07-28-2003 09:47 PM


All times are GMT -6. The time now is 12:01 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22