3 * An evil proxy for TrIPE
5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
12 * TrIPE is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * TrIPE is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Header files ------------------------------------------------------*/
38 #include <sys/types.h>
43 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
48 #include <mLib/alloc.h>
49 #include <mLib/dstr.h>
50 #include <mLib/fdflags.h>
51 #include <mLib/mdwopt.h>
52 #include <mLib/quis.h>
53 #include <mLib/report.h>
58 #include <catacomb/buf.h>
60 #include <catacomb/key.h>
62 #include <catacomb/mp.h>
63 #include <catacomb/mprand.h>
64 #include <catacomb/dh.h>
66 #include <catacomb/noise.h>
67 #include <catacomb/rand.h>
68 #include <catacomb/rc4.h>
72 /*----- Data structures ---------------------------------------------------*/
81 typedef struct filter {
84 void (*func)(struct filter */*f*/, const octet */*buf*/, size_t /*sz*/);
88 typedef struct qnode {
93 /*----- Static variables --------------------------------------------------*/
99 static unsigned npeer = 0;
100 static key_file keys;
103 #define PASS(f, buf, sz) ((f) ? (f)->func((f), (buf), (sz)) : (void)0)
104 #define RND(i) (rng->ops->range(rng, (i)))
106 /*----- Peer management ---------------------------------------------------*/
108 static void dopacket(int fd, unsigned mode, void *vv)
112 int r = read(fd, buf, sizeof(buf));
114 printf("recv from `%s'\n", p->name);
119 static void addpeer(unsigned ac, char **av)
121 key_packstruct kps[DH_PUBFETCHSZ];
124 struct sockaddr_in sin;
131 die(1, "syntax: peer:NAME:PORT:ADDR:PORT");
133 die(1, "enough peers already");
135 p->name = xstrdup(av[0]);
136 kp = key_fetchinit(dh_pubfetch, kps, &p->kpub);
137 e = key_fetchbyname(kp, &keys, av[0]);
140 die(1, "key_fetch `%s': %s", av[0], key_strerror(e));
141 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
142 die(1, "socket: %s", strerror(errno));
143 fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
144 memset(&sin, 0, sizeof(sin));
145 sin.sin_family = AF_INET;
146 sin.sin_addr.s_addr = INADDR_ANY;
147 sin.sin_port = htons(atoi(av[1]));
148 if (bind(fd, (struct sockaddr *)&sin, sizeof(sin)))
149 die(1, "bind: %s", strerror(errno));
150 memset(&sin, 0, sizeof(sin));
151 sin.sin_family = AF_INET;
152 if ((h = gethostbyname(av[2])) == 0)
153 die(1, "gethostbyname `%s'", av[2]);
154 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)) ||
155 setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &len, sizeof(len)))
156 die(1, "setsockopt: %s", strerror(errno));
157 memcpy(&sin.sin_addr, h->h_addr, sizeof(sin.sin_addr));
158 sin.sin_port = htons(atoi(av[3]));
159 if (connect(fd, (struct sockaddr *)&sin, sizeof(sin)))
160 die(1, "connect: %s", strerror(errno));
161 sel_initfile(&sel, &p->sf, fd, SEL_READ, dopacket, p);
165 /*----- Fork filter -------------------------------------------------------*/
167 typedef struct forknode {
168 struct forknode *next;
172 typedef struct forkfilt {
177 static void dofork(filter *f, const octet *buf, size_t sz)
179 forkfilt *ff = f->state;
184 for (fn = ff->fn; fn; fn = fn->next) {
185 printf("fork branch %u of fork `%s'\n", i++, ff->name);
186 PASS(fn->f, buf, sz);
188 printf("fork branch %u of fork `%s'\n", i++, ff->name);
189 PASS(f->next, buf, sz);
192 static void addfork(filter *f, unsigned ac, char **av)
196 die(1, "syntax: filt:fork:NAME");
197 ff = CREATE(forkfilt);
198 ff->name = xstrdup(av[0]);
204 static void nextfork(unsigned ac, char **av)
213 die(1, "syntax: next:NAME:...");
214 for (i = 0; i < 2; i++) {
216 for (f = p->f; f; f = f->next) {
217 if (f->func != dofork)
220 for (j = 0; j < ac; j++) {
221 if (strcmp(av[j], ff->name) == 0)
226 fn = CREATE(forknode);
227 for (ffn = &ff->fn; *ffn; ffn = &(*ffn)->next)
237 /*----- Corrupt filter ----------------------------------------------------*/
239 typedef struct corrupt {
243 static void docorrupt(filter *f, const octet *buf, size_t sz)
245 corrupt *c = f->state;
249 while (!RND(c->p_corrupt)) {
250 puts("corrupt packet");
251 b[RND(sz)] ^= RND(256);
253 PASS(f->next, b, sz);
256 static void addcorrupt(filter *f, unsigned ac, char **av)
260 die(1, "syntax: filt:corrupt[:P-CORRUPT]");
263 c->p_corrupt = atoi(av[0]);
270 /*----- Drop filter -------------------------------------------------------*/
272 typedef struct drop {
276 static void dodrop(filter *f, const octet *buf, size_t sz)
283 PASS(f->next, buf, sz);
286 static void adddrop(filter *f, unsigned ac, char **av)
290 die(1, "syntax: filt:drop[:P-DROP]");
293 d->p_drop = atoi(av[0]);
300 /*----- Delay filter ------------------------------------------------------*/
302 typedef struct delaynode {
312 typedef struct delay {
320 static void dtimer(struct timeval *tv, void *vv);
322 static void dinsert(delaynode *dn)
326 unsigned long tdelta = RND(dn->d->t);
327 gettimeofday(&tv, 0);
328 TV_ADDL(&tv, &tv, 0, tdelta);
330 sel_addtimer(&sel, &dn->tm, &tv, dtimer, dn);
332 for (ta = tb = sel.timers; ta; ta = ta->next) {
333 ta = ta->next; if (!ta) break; assert(ta != tb);
334 ta = ta->next; if (!ta) break; assert(ta != tb);
337 printf(" delay %lu usecs", tdelta);
340 static void dsend(delaynode *dn, unsigned force)
344 fputs(" send...\n", stdout);
346 PASS(d->f->next, dn->buf, dn->sz);
347 fputs("delay ...", stdout);
356 sel_rmtimer(&ddn->tm);
357 sel_addtimer(&sel, &dn->tm, &ddn->tm.tv, dtimer, dn);
364 printf(" move id %u from slot %u to slot %u", ddn->seq, ddn->i, dn->i);
366 { unsigned i; for (i = 0; i < d->n; i++) assert(d->q[i].buf); }
367 fputs(" remove", stdout);
371 static void dtimer(struct timeval *tv, void *vv)
374 printf("delay timer peer `%s' id %u slot %u",
375 dn->d->f->p_from->name, dn->seq, dn->i);
377 dsend(dn, RND(dn->d->p_replay));
381 static void dodelay(filter *f, const octet *buf, size_t sz)
385 static unsigned seq = 0;
387 fputs("delay", stdout);
388 if (d->n == d->max) {
389 dn = &d->q[RND(d->n)];
390 printf(" force uid %u", dn->seq);
391 sel_rmtimer(&dn->tm);
398 printf(" new id %u in slot %u", dn->seq, dn->i);
399 dn->buf = xmalloc(sz);
401 memcpy(dn->buf, buf, sz);
406 static void adddelay(filter *f, unsigned ac, char **av)
411 if (ac < 1 || ac > 3)
412 die(1, "syntax: filt:delay:QLEN[:MILLIS:P-REPLAY]");
414 d->max = atoi(av[0]);
416 d->t = strtoul(av[1], 0, 10);
421 d->p_replay = atoi(av[2]);
425 d->q = xmalloc(d->max * sizeof(delaynode));
429 for (i = 0; i < d->max; i++) {
437 /*----- Filters -----------------------------------------------------------*/
439 static void dosend(filter *f, const octet *buf, size_t sz)
441 printf("send to `%s'\n", f->p_to->name);
442 IGNORE(write(f->p_to->sf.fd, buf, sz));
445 static void addsend(filter *f, unsigned ac, char **av)
448 die(1, "syntax: filt:send");
452 const struct filtab {
454 void (*func)(filter */*f*/, unsigned /*ac*/, char **/*av*/);
458 { "delay", adddelay },
460 { "corrupt", addcorrupt },
464 static void dofilter(peer *from, peer *to, unsigned ac, char **av)
466 filter **ff, *f = CREATE(filter);
467 const struct filtab *ft;
469 die(1, "syntax: {l,r,}filt:NAME:...");
474 for (ff = &from->f; *ff; ff = &(*ff)->next)
477 for (ft = filtab; ft->name; ft++) {
478 if (strcmp(av[0], ft->name) == 0) {
479 ft->func(f, ac - 1, av + 1);
483 die(1, "unknown filter `%s'", av[0]);
486 /*----- Flooding ----------------------------------------------------------*/
488 typedef struct flood {
496 static void setflood(flood *f);
498 static void floodtimer(struct timeval *tv, void *vv)
510 rng->ops->fill(rng, buf, sz);
513 puts("flood packet");
514 PASS(f->p->f, buf, sz);
518 static void setflood(flood *f)
521 gettimeofday(&tv, 0);
522 TV_ADDL(&tv, &tv, 0, RND(f->t));
523 sel_addtimer(&sel, &f->tm, &tv, floodtimer, f);
526 static void doflood(peer *p, unsigned ac, char **av)
530 die(1, "syntax: flood[:TYPE:MILLIS:SIZE]");
534 f->type = strtoul(av[0], 0, 16);
549 /*----- Configuration commands --------------------------------------------*/
551 static void parse(char *p);
553 static void addflood(unsigned ac, char **av) {
554 doflood(&peers[0], ac, av);
555 doflood(&peers[1], ac, av);
557 static void addlflood(unsigned ac, char **av) {
558 doflood(&peers[0], ac, av);
560 static void addrflood(unsigned ac, char **av) {
561 doflood(&peers[1], ac, av);
564 static void addfilter(unsigned ac, char **av) {
565 dofilter(&peers[0], &peers[1], ac, av);
566 dofilter(&peers[1], &peers[0], ac, av);
568 static void addlfilter(unsigned ac, char **av) {
569 dofilter(&peers[0], &peers[1], ac, av);
571 static void addrfilter(unsigned ac, char **av) {
572 dofilter(&peers[1], &peers[0], ac, av);
575 static void include(unsigned ac, char **av)
580 die(1, "syntax: include:FILE:...");
582 if ((fp = fopen(*av, "r")) == 0)
583 die(1, "fopen `%s': %s", *av, strerror(errno));
584 while (dstr_putline(&d, fp) != EOF) {
593 const struct cmdtab {
595 void (*func)(unsigned /*ac*/, char **/*av*/);
598 { "include", include },
599 { "filt", addfilter },
600 { "lfilt", addlfilter },
601 { "rfilt", addrfilter },
602 { "next", nextfork },
603 { "flood", addflood },
604 { "lflood", addlflood },
605 { "rflood", addrflood },
611 static void parse(char *p)
615 const struct cmdtab *ct;
623 } while (p && c < AVMAX - 1);
625 for (ct = cmdtab; ct->name; ct++) {
626 if (strcmp(ct->name, v[0]) == 0) {
627 ct->func(c - 1, v + 1);
631 die(1, "unknown command `%s'", v[0]);
634 /*----- Main driver -------------------------------------------------------*/
636 static void version(FILE *fp)
637 { pquis(fp, "$, TrIPE version " VERSION "\n"); }
639 static void usage(FILE *fp)
640 { pquis(fp, "Usage: $ [-k KEYRING] DIRECTIVE...\n"); }
642 static void help(FILE *fp)
650 -h, --help Show this help text.\n\
651 -v, --version Show the version number.\n\
652 -u, --usage Show terse usage summary.\n\
654 -k, --keyring=FILE Fetch keys from FILE.\n\
657 peer:NAME:LOCAL-PORT:REMOTE-ADDR:REMOTE-PORT\n\
659 {,l,r}filt:FILTER:ARGS:...\n\
661 {,l,r}flood:TYPE:MILLIS:SIZE\n\
666 delay:QLEN[:MILLIS:P-REPLAY]\n\
668 corrupt[:P-CORRUPT]\n",
672 int main(int argc, char *argv[])
674 const char *kfname = "keyring.pub";
683 static const struct option opt[] = {
684 { "help", 0, 0, 'h' },
685 { "version", 0, 0, 'v' },
686 { "usage", 0, 0, 'u' },
687 { "keyring", OPTF_ARGREQ, 0, 'k' },
690 if ((i = mdwopt(argc, argv, "hvuk:", opt, 0, 0, 0)) < 0)
714 rand_noisesrc(RAND_GLOBAL, &noise_source);
715 rand_seed(RAND_GLOBAL, 160);
716 rand_get(RAND_GLOBAL, buf, sizeof(buf));
717 rng = rc4_rand(buf, sizeof(buf));
719 if (key_open(&keys, kfname, KOPEN_READ, key_moan, 0))
720 die(1, "couldn't open `%s': %s", kfname, strerror(errno));
721 for (i = optind; i < argc; i++)
724 die(1, "need two peers");
731 /*----- That's all, folks -------------------------------------------------*/