00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <limits.h>
00025 #include "Net.h"
00026 #include "Disk.h"
00027 #include "Main.h"
00028 #include "DNS.h"
00029 #include "OneWayTunnel.h"
00030
00031 struct TestProxy:Continuation
00032 {
00033
00034 NetVConnection *vc;
00035 NetVConnection *remote;
00036 MIOBuffer *inbuf;
00037 MIOBuffer *outbuf;
00038 char *host, *url, *url_end;
00039 char s[256];
00040
00041 int done()
00042 {
00043 if (inbuf)
00044 free_MIOBuffer(inbuf);
00045 if (outbuf)
00046 free_MIOBuffer(outbuf);
00047 if (vc)
00048 vc->do_io(VIO::CLOSE);
00049 if (remote)
00050 remote->do_io(VIO::CLOSE);
00051 delete this;
00052 return EVENT_DONE;
00053 }
00054
00055 int startEvent(int event, VIO * vio)
00056 {
00057 if (event != VC_EVENT_READ_READY) {
00058 printf("TestProxy startEvent error %d\n", event);
00059 return done();
00060 }
00061 if (vio->buffer.mbuf->gets(s, 255)) {
00062 host = s + 11;
00063 url = strchr(host, '/');
00064 url_end = strchr(url, ' ');
00065 *url = 0;
00066 dnsProcessor.gethostbyname(host, this);
00067 *url = '/';
00068 SET_HANDLER(dnsEvent);
00069 vc = (NetVConnection *) vio->vc_server;
00070 return EVENT_DONE;
00071 }
00072 return EVENT_CONT;
00073 }
00074
00075 int dnsEvent(int event, HostEnt * ent)
00076 {
00077 if (!ent) {
00078 printf("TestProxy dnsEvent error %d\n", event);
00079 return done();
00080 }
00081 unsigned int ip = *(unsigned int *) ent->h_addr_list[0];
00082 netProcessor.connect(this, ip, 80);
00083 SET_HANDLER(connectEvent);
00084 return EVENT_DONE;
00085 }
00086
00087 int connectEvent(int event, NetVConnection * aremote)
00088 {
00089 if (!aremote) {
00090 printf("TestProxy connectEvent error %d\n", event);
00091 return done();
00092 }
00093 remote = aremote;
00094 outbuf = new_MIOBuffer();
00095 remote->do_io(VIO::WRITE, this, INT64_MAX, outbuf);
00096 *url_end = 0;
00097 sprintf(outbuf->start, "GET %s HTTP/1.0\n\n\n", url);
00098 outbuf->fill(strlen(outbuf->start) + 1);
00099 printf("sending [%s]\n", outbuf->start);
00100 SET_HANDLER(sendEvent);
00101 return EVENT_CONT;
00102 }
00103
00104 int sendEvent(int event, VIO * vio)
00105 {
00106 if (event != VC_EVENT_WRITE_READY) {
00107 printf("TestProxy sendEvent error %d\n", event);
00108 return done();
00109 }
00110 if (vio->buffer.size())
00111 return EVENT_CONT;
00112 new OneWayTunnel(remote, vc, this, TUNNEL_TILL_DONE, true, true, true);
00113 SET_HANDLER(tunnelEvent);
00114 return EVENT_DONE;
00115 }
00116
00117 int tunnelEvent(int event, Continuation * cont)
00118 {
00119 (void) cont;
00120 if (event != VC_EVENT_EOS) {
00121 printf("TestProxy sendEvent error %d\n", event);
00122 return done();
00123 }
00124 remote = 0;
00125 vc = 0;
00126 printf("sucessful proxy of %s\n", url);
00127 return done();
00128 }
00129
00130 TestProxy(MIOBuffer * abuf)
00131 : Continuation(new_ProxyMutex()), vc(0), remote(0), inbuf(abuf), outbuf(0), host(0), url(0), url_end(0) {
00132 SET_HANDLER(startEvent);
00133 }
00134 };
00135
00136
00137 struct TestAccept:Continuation
00138 {
00139 int startEvent(int event, NetVConnection * e)
00140 {
00141 if (!event) {
00142 MIOBuffer *buf = new_MIOBuffer();
00143 e->do_io(VIO::READ, new TestProxy(buf), INT64_MAX, buf);
00144 } else
00145 {
00146 printf("TestAccept error %d\n", event);
00147 return EVENT_DONE;
00148 }
00149 return EVENT_CONT;
00150 }
00151 TestAccept():Continuation(new_ProxyMutex()) {
00152 SET_HANDLER(startEvent);
00153 }
00154 };
00155
00156 void
00157 test()
00158 {
00159 netProcessor.accept(new TestAccept, accept_port_number);
00160 }