00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "wvfile.h"
00010 #include "wvstreamlist.h"
00011 #include "wvtcp.h"
00012 #include "wvlog.h"
00013 #include <signal.h>
00014
00015
00016 static void bouncer(WvStream &s, void *userdata)
00017 {
00018 WvStreamList &l = *(WvStreamList *)userdata;
00019 WvStreamList::Iter i(l);
00020 char buf[1024];
00021 size_t len;
00022
00023 len = s.read(buf, sizeof(buf));
00024 if (!len) return;
00025
00026 for (i.rewind(); i.next(); )
00027 {
00028 WvStream &out = i;
00029
00030 if (&s == &out)
00031 continue;
00032
00033 if (!out.isok())
00034 continue;
00035
00036 out.delay_output(true);
00037 out.write(buf, len);
00038 }
00039 }
00040
00041
00042 int main(int argc, char **argv)
00043 {
00044 WvStreamList biglist, l;
00045 int count;
00046 char *cptr;
00047 WvLog log("simpleconn", WvLog::Info);
00048
00049 signal( SIGPIPE, SIG_IGN );
00050
00051 if (argc < 2)
00052 {
00053 fprintf(stderr, "Usage: %s file [file...]\n"
00054 " Passes data between the given files/devices/etc. "
00055 "If a filename is\n"
00056 " '-', it refers to stdin/stdout.\n",
00057 argv[0]);
00058 return 1;
00059 }
00060
00061 biglist.append(&l, false);
00062
00063 for (count = 1; count < argc; count++)
00064 {
00065 WvStream *f;
00066
00067 if (!strcmp(argv[count], "-"))
00068 {
00069 log("File %s is stdin/stdout\n", count);
00070 f = wvcon;
00071 l.append(f, false);
00072 f->setcallback(bouncer, &l);
00073 }
00074 else if (!strncasecmp(argv[count], "tcp:", 4))
00075 {
00076
00077 cptr = argv[count] + 4;
00078 if (strchr(cptr, ':'))
00079 {
00080 log("File %s is a TCP client\n", count);
00081 f = new WvTCPConn(WvIPPortAddr(cptr));
00082 l.append(f, true);
00083 f->setcallback(bouncer, &l);
00084 }
00085 else
00086 {
00087 log("File %s is a TCP server\n", count);
00088 WvTCPListener *listen = new WvTCPListener(WvIPPortAddr("",
00089 atoi(cptr)));
00090 listen->auto_accept(&l, bouncer, &l);
00091 biglist.append(listen, true);
00092 }
00093 }
00094 else
00095 {
00096 log("File %s is a file (%s)\n", count, argv[count]);
00097 f = new WvFile(argv[count], O_RDWR);
00098
00099 if (!f->isok())
00100 {
00101 delete f;
00102 f = new WvFile(argv[count], O_RDONLY);
00103 if (!f->isok())
00104 {
00105 fprintf(stderr, "%s: %s\n", argv[count], f->errstr());
00106 return 1;
00107 }
00108 }
00109
00110 l.append(f, true);
00111 f->setcallback(bouncer, &l);
00112 }
00113 }
00114
00115
00116
00117 while (l.count() >= 2 || (biglist.count() - 1 >= 1 && l.count() >= 1))
00118 {
00119 if (biglist.select(1000))
00120 biglist.callback();
00121 }
00122
00123 return 0;
00124 }