Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
const char MSG[] =
"GET / HTTP/1.1\r\n\
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.8)\r\n\
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/
*;q=0.5\r\n\
Accept-Language: en-us,en;q=0.5\r\n\
Accept-Encoding: gzip,deflate\r\n\
Accept-Charset: ISO-8859-1,utf-8;q=0.7\r\n\
Keep-Alive: 300\r\n\
Connection: keep-alive\r\n\
";
int main(int argc, char *argv[]) {
int simpleSocket = 0;
int simplePort = 0;
int returnStatus = 0;
int clientSock = 0;
int msgLength = 0;
int clientConnect = 0;
char buf[1024] = { 0 };
char bufProxy[1024] = { 0 };
struct sockaddr_in simpleServer;
struct sockaddr_in simpleClient;
if (4 != argc) {
fprintf(stderr, "usage: %s <listen-port> <remote-http-server> <remote-http-port>\n", argv[0]);
exit(1);
}
simpleSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (simpleSocket == -1) {
fprintf(stderr, "could not create socket\n");
exit(1); } else {
fprintf(stderr, "socket created\n");
}
simplePort = atoi(argv[1]);
bzero(&simpleServer, sizeof(simpleServer));
simpleServer.sin_family = AF_INET;
simpleServer.sin_addr.s_addr = htonl(INADDR_ANY);
simpleServer.sin_port = htons(simplePort);
returnStatus = bind(simpleSocket, (struct sockaddr*)&simpleServer, sizeof(simpleServer));
if (returnStatus == 0) {
fprintf(stderr, "bind completed\n");
} else {
fprintf(stderr, "could not bind\n");
close(simpleServer);
exit(1);
}
returnStatus = listen(simpleSocket, 5);
if (returnStatus == -1) {
fprintf(stderr, "can not listen on socket\n");
close(simpleSocket);
exit(1);
}
while (1) {
struct sockaddr_in clientName = { 0 };
int simpleChildSocket = 0;
int clientNameLength = sizeof(clientName);
simpleChildSocket = accept (simpleSocket, (struct sockaddr*)&clientName, &clientNameLength);
if (simpleChildSocket == -1) {
fprintf(stderr, "canot accept connection\n");
close(simpleServer);
exit (1);
}
printf("accepted client\n");
// let's receive the client's GET request
returnStatus = read(simpleChildSocket, buf, sizeof(buf));
printf("received from client %d bytes: %s \n", returnStatus, buf);
// connecting to the web server as a client:
clientSock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
printf("clientSocket = socket() - %d\n", clientSock);
bzero(&simpleClient, sizeof(simpleClient));
printf("bzero for simpleClient\n");
simpleClient.sin_family = AF_INET;
inet_addr(argv[2], &simpleClient.sin_addr.s_addr);
printf("inet_addr copy\n");
simpleClient.sin_port = htons(atoi(argv[3]));
printf("simpleclient port assignment\n");
clientConnect = connect(clientSock, (struct sockaddr*)&simpleClient, sizeof(simpleClient));
printf("connecting to remote server - %d\n", clientConnect);
returnStatus = write(clientConnect, MSG, strlen(MSG));
printf("writing to proxy server\n");
/*
printf("wrote to proxied server %d bytes: %s\n", returnStatus, buf);
returnStatus = read(clientSock, bufProxy, sizeof(bufProxy));
printf("read from proxied server %d bytes: %s\n", returnStatus, bufProxy);
*/
close(clientSock);
write(simpleChildSocket, bufProxy, strlen(bufProxy));
close(simpleChildSocket);
}
close(simpleServer);
return 0;
}
The code is a bit messy, I know, but it should be straight forward.