-
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);
-
> err = initconn(sckdestin, destinservice);
So where did these two parameters get defined?
Also, your initial recv() makes no account of fragmentation.
-
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.
-
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.
-
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.
-
use either select() or poll().
-
Thank you.
I am assuming select() is for Win32 and poll() is for unix variants?
-
both exist on unix. I don't know for windows.