[Subject Prev][Subject Next][Thread Prev][Thread Next][Subject Index][Thread Index]
HTTP tunnel
Hello,
I am trying to code a HTTP tunnel...something that runs at port 9000
(here), listens to browser requests (setting proxy at 9000 in browser)
and then passes on this HTTP request fom the browser to a squid proxy
running on port 3128 on 127.0.0.1
Here I am having some problems...
- My program is not able to write to browser
- And on the same line, the data returned by squid also cannot be
written to browser, although when I print the squid returned data, it
prints it on the konsole....
Here is the code....
------------------------------------
/***********************************************************
This code runs a server at specified port to which the
browsers will connect. The browser requesst is then
passed on to the real proxy server.
Author : Ajay Dudani <ajay@xxxxxxxxxxxxxx>
***********************************************************/
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <signal.h>
#include <unistd.h>
int main()
{
// here server refers to this server
// client is the browser that wish to surf
//declarations for server and client
int server_sockfd, client_sockfd;
int server_len, client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address;
//declarations for squid connection and data from squid
char data[1000];
char *host;
int sockfd;
int len, result;
struct sockaddr_in address;
server_sockfd = socket(AF_INET, SOCK_STREAM, 0);
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = htonl(INADDR_ANY);
server_address.sin_port = htons(9000);
server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr *) &server_address,
server_len);
listen(server_sockfd, 10);
printf("Server Waiting ... \n");
// prepare for connection to the REAL proxy server.
sockfd = socket(AF_INET, SOCK_STREAM, 0);
address.sin_family = AF_INET;
address.sin_addr.s_addr = inet_addr("127.0.0.1");
address.sin_port = htons(3128);
len = sizeof(address);
client_len = sizeof(client_address);
client_sockfd = accept(server_sockfd, (struct sockaddr
*) &client_address, &client_len);
write(client_sockfd, "<h1>Welcome to this lame HTTP
tunnel</h1>",50);
read(client_sockfd, ch, 1000);
printf("\nBrowser says... \n%s",ch);
write(client_sockfd, "<h1>Can you see this ??</h1>",30);
write(client_sockfd, "<h1>If you can, do write to me at
mailto:ajay@xxxxxxxxxxxxxx</h1>",30);
// Now connect to the REAL proxy server.
result = connect(sockfd, (struct sockaddr *)
&address, len);
//and write browser request to it... (does not work ?? )
write(sockfd,ch, strlen(ch));
//now read squid response...
read(sockfd, &data, 1000);
//print it to konsole and also to browser
printf("\nData # 1 from proxy server ::
%s\n",data);
//again writing to browser does not work....
write(client_sockfd, data, strlen(data));
/*
if(result == -1)
{
//Connection to proxy server has failed
//Write to imart.log
//Yet to write to log....PARTIAL
printf("Proxy not running");
write(client_sockfd, "<html>Proxy Server
Not Running</html>",20);
}
*/
close(client_sockfd);
close(server_sockfd);
exit(0);
}//end of main()
------------end of code--------------
--
.+'''+.
a j a y @crosswinds.net
`+.,.+' Ajay Dudani