Modified a bit to revert Daniel's host out
[curltunnel.git] / libcurl_tunnel.c
blob396908d9681eab8178573638cc7ee3800d493db9
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <sys/select.h>
4 #include <curl/curl.h>
6 #define _FILE_OFFSET_BITS 64 /* for curl_off_t magic */
7 #define SIZE 655536
9 int fd_read(int fd, void *buf, size_t len)
11 int bytes_read;
12 bytes_read = read(fd,buf,len);
13 return bytes_read;
16 int fd_write(int fd, void *buf, size_t len)
18 int bytes_written;
19 bytes_written = write(fd,buf,len);
20 return bytes_written;
23 int fdcopy(int fdin, int fdout)
25 char buf[SIZE];
26 int n;
28 if((n=fd_read(fdin,buf,SIZE))<0)
30 fprintf(stderr,"Read Error\n");
31 exit(1);
34 if(n==0)
35 return 1;
37 if(fd_write(fdout,buf,n)!=n)
39 fprintf(stderr,"Write Error\n");
40 exit(1);
43 return 0;
46 void wait_and_act(int sockfd)
48 struct timeval timeout;
49 int rc; /* select() return code */
50 long timeout_ms = 1000;
52 fd_set fdread;
53 fd_set fdwrite;
54 fd_set fdexcep;
55 int maxfd=sockfd;
57 while(1==1)
59 FD_ZERO(&fdread);
60 FD_ZERO(&fdwrite);
61 FD_ZERO(&fdexcep);
63 /* set timeout to wait */
64 timeout.tv_sec = timeout_ms/1000;
65 timeout.tv_usec = (timeout_ms%1000)*1000;
67 FD_SET(fileno(stdin), &fdread);
68 FD_SET(sockfd, &fdread);
69 /* FD_SET(sockfd, &fdwrite); */
71 rc = select(maxfd+1,
72 (fd_set *)&fdread,
73 (fd_set *)&fdwrite,
74 (fd_set *)&fdexcep, &timeout);
76 if(rc==-1) {
77 /* select error */
78 fprintf(stderr,"select() error\n");
79 break;
81 if(rc==0){
82 /* timeout! */
83 /* fprintf(stderr,"Timeout\n"); */
85 /* use FD_ISSET() to check what happened, then read/write accordingly */
86 if (FD_ISSET(fileno(stdin), &fdread))
88 if(fdcopy(fileno(stdin),sockfd))
89 break;
91 if (FD_ISSET(sockfd,&fdread))
93 if(fdcopy(sockfd,fileno(stdout)))
94 break;
99 int main(int argc, char *argv[])
101 CURLcode ret;
102 long sckt;
103 CURL *hnd = curl_easy_init();
105 /* Hard coded for convenience currently. Obviously we'll change this to
106 read from the command line at some point */
107 curl_easy_setopt(hnd, CURLOPT_URL, "http://godeater.dyndns.org:443/");
108 curl_easy_setopt(hnd, CURLOPT_PROXY, "10.1.23.219:8080");
109 curl_easy_setopt(hnd, CURLOPT_PROXYUSERPWD, "gbchlb:X344ekl25");
110 curl_easy_setopt(hnd, CURLOPT_USERAGENT, "curl/7.16.4 (i686-pc-linux-gnu) libcurl/7.16.4 GnuTLS/1.4.4 zlib/1.2.3 c-ares/1.4.0");
111 curl_easy_setopt(hnd, CURLOPT_HTTPPROXYTUNNEL, 1);
112 curl_easy_setopt(hnd, CURLOPT_CONNECT_ONLY, 1);
113 curl_easy_setopt(hnd, CURLOPT_VERBOSE, 1);
114 ret = curl_easy_perform(hnd);
116 if(ret == CURLE_OK)
118 /* Extract socket, and select() */
119 ret = curl_easy_getinfo(hnd, CURLINFO_LASTSOCKET, &sckt);
120 if((sckt==-1) || ret){
121 fprintf(stderr,"[ERR] Couldn't get socket");
122 return(-1);
126 wait_and_act((int)sckt);
129 curl_easy_cleanup(hnd);