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