Commit | Line | Data |
---|---|---|
50c74957 | 1 | /* -*-c-*- |
50c74957 | 2 | * |
3 | * An evil proxy for TrIPE | |
4 | * | |
5 | * (c) 2001 Straylight/Edgeware | |
6 | */ | |
7 | ||
e04c2d50 | 8 | /*----- Licensing notice --------------------------------------------------* |
50c74957 | 9 | * |
10 | * This file is part of Trivial IP Encryption (TrIPE). | |
11 | * | |
11ad66c2 MW |
12 | * TrIPE is free software: you can redistribute it and/or modify it under |
13 | * the terms of the GNU General Public License as published by the Free | |
14 | * Software Foundation; either version 3 of the License, or (at your | |
15 | * option) any later version. | |
e04c2d50 | 16 | * |
11ad66c2 MW |
17 | * TrIPE is distributed in the hope that it will be useful, but WITHOUT |
18 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
19 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
20 | * for more details. | |
e04c2d50 | 21 | * |
50c74957 | 22 | * You should have received a copy of the GNU General Public License |
11ad66c2 | 23 | * along with TrIPE. If not, see <https://www.gnu.org/licenses/>. |
50c74957 | 24 | */ |
25 | ||
50c74957 | 26 | /*----- Header files ------------------------------------------------------*/ |
27 | ||
28 | #include "config.h" | |
29 | ||
30 | #include <assert.h> | |
31 | #include <errno.h> | |
32 | #include <stdio.h> | |
33 | #include <stdlib.h> | |
34 | #include <string.h> | |
35 | #include <time.h> | |
36 | ||
37 | #include <sys/types.h> | |
38 | #include <sys/time.h> | |
39 | #include <unistd.h> | |
40 | #include <fcntl.h> | |
41 | ||
42 | #include <sys/socket.h> | |
43 | #include <netinet/in.h> | |
44 | #include <arpa/inet.h> | |
45 | #include <netdb.h> | |
46 | ||
47 | #include <mLib/alloc.h> | |
48 | #include <mLib/dstr.h> | |
49 | #include <mLib/fdflags.h> | |
50 | #include <mLib/mdwopt.h> | |
51 | #include <mLib/quis.h> | |
52 | #include <mLib/report.h> | |
53 | #include <mLib/sel.h> | |
54 | #include <mLib/sub.h> | |
55 | #include <mLib/tv.h> | |
56 | ||
c953f4b5 | 57 | #include <catacomb/buf.h> |
58 | ||
50c74957 | 59 | #include <catacomb/key.h> |
60 | ||
61 | #include <catacomb/mp.h> | |
50c74957 | 62 | #include <catacomb/mprand.h> |
63 | #include <catacomb/dh.h> | |
64 | ||
65 | #include <catacomb/noise.h> | |
66 | #include <catacomb/rand.h> | |
67 | #include <catacomb/rc4.h> | |
68 | ||
23df0e5b MW |
69 | #include "util.h" |
70 | ||
50c74957 | 71 | /*----- Data structures ---------------------------------------------------*/ |
72 | ||
73 | typedef struct peer { | |
74 | sel_file sf; | |
50c74957 | 75 | const char *name; |
76 | struct filter *f; | |
77 | } peer; | |
78 | ||
79 | typedef struct filter { | |
80 | struct filter *next; | |
81 | peer *p_from, *p_to; | |
82 | void (*func)(struct filter */*f*/, const octet */*buf*/, size_t /*sz*/); | |
83 | void *state; | |
84 | } filter; | |
85 | ||
86 | typedef struct qnode { | |
87 | octet *buf; | |
88 | size_t sz; | |
89 | } qnode; | |
90 | ||
91 | /*----- Static variables --------------------------------------------------*/ | |
92 | ||
93 | #define PKBUFSZ 65536 | |
94 | ||
95 | static sel_state sel; | |
96 | static peer peers[2]; | |
97 | static unsigned npeer = 0; | |
98 | static key_file keys; | |
99 | static grand *rng; | |
100 | ||
101 | #define PASS(f, buf, sz) ((f) ? (f)->func((f), (buf), (sz)) : (void)0) | |
102 | #define RND(i) (rng->ops->range(rng, (i))) | |
103 | ||
104 | /*----- Peer management ---------------------------------------------------*/ | |
105 | ||
106 | static void dopacket(int fd, unsigned mode, void *vv) | |
107 | { | |
108 | octet buf[PKBUFSZ]; | |
109 | peer *p = vv; | |
110 | int r = read(fd, buf, sizeof(buf)); | |
111 | if (r >= 0) { | |
112 | printf("recv from `%s'\n", p->name); | |
113 | PASS(p->f, buf, r); | |
114 | } | |
115 | } | |
116 | ||
117 | static void addpeer(unsigned ac, char **av) | |
118 | { | |
50c74957 | 119 | struct hostent *h; |
120 | struct sockaddr_in sin; | |
121 | int len = PKBUFSZ; | |
122 | peer *p; | |
123 | int fd; | |
50c74957 | 124 | |
125 | if (ac != 4) | |
126 | die(1, "syntax: peer:NAME:PORT:ADDR:PORT"); | |
127 | if (npeer >= 2) | |
128 | die(1, "enough peers already"); | |
d36b8007 MW |
129 | if (!key_bytag(&keys, av[0])) |
130 | die(1, "no key named `%s'", av[0]); | |
50c74957 | 131 | p = &peers[npeer++]; |
132 | p->name = xstrdup(av[0]); | |
50c74957 | 133 | if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) |
134 | die(1, "socket: %s", strerror(errno)); | |
135 | fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC); | |
136 | memset(&sin, 0, sizeof(sin)); | |
137 | sin.sin_family = AF_INET; | |
138 | sin.sin_addr.s_addr = INADDR_ANY; | |
139 | sin.sin_port = htons(atoi(av[1])); | |
140 | if (bind(fd, (struct sockaddr *)&sin, sizeof(sin))) | |
141 | die(1, "bind: %s", strerror(errno)); | |
142 | memset(&sin, 0, sizeof(sin)); | |
143 | sin.sin_family = AF_INET; | |
144 | if ((h = gethostbyname(av[2])) == 0) | |
145 | die(1, "gethostbyname `%s'", av[2]); | |
146 | if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)) || | |
147 | setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &len, sizeof(len))) | |
148 | die(1, "setsockopt: %s", strerror(errno)); | |
149 | memcpy(&sin.sin_addr, h->h_addr, sizeof(sin.sin_addr)); | |
150 | sin.sin_port = htons(atoi(av[3])); | |
151 | if (connect(fd, (struct sockaddr *)&sin, sizeof(sin))) | |
152 | die(1, "connect: %s", strerror(errno)); | |
153 | sel_initfile(&sel, &p->sf, fd, SEL_READ, dopacket, p); | |
154 | sel_addfile(&p->sf); | |
155 | } | |
156 | ||
157 | /*----- Fork filter -------------------------------------------------------*/ | |
158 | ||
159 | typedef struct forknode { | |
160 | struct forknode *next; | |
161 | filter *f; | |
162 | } forknode; | |
163 | ||
164 | typedef struct forkfilt { | |
165 | const char *name; | |
166 | forknode *fn; | |
167 | } forkfilt; | |
168 | ||
169 | static void dofork(filter *f, const octet *buf, size_t sz) | |
170 | { | |
171 | forkfilt *ff = f->state; | |
172 | forknode *fn; | |
173 | unsigned i = 0; | |
174 | ||
175 | ff = f->state; | |
176 | for (fn = ff->fn; fn; fn = fn->next) { | |
177 | printf("fork branch %u of fork `%s'\n", i++, ff->name); | |
178 | PASS(fn->f, buf, sz); | |
179 | } | |
180 | printf("fork branch %u of fork `%s'\n", i++, ff->name); | |
181 | PASS(f->next, buf, sz); | |
182 | } | |
183 | ||
184 | static void addfork(filter *f, unsigned ac, char **av) | |
185 | { | |
186 | forkfilt *ff; | |
187 | if (ac != 1) | |
188 | die(1, "syntax: filt:fork:NAME"); | |
189 | ff = CREATE(forkfilt); | |
190 | ff->name = xstrdup(av[0]); | |
191 | ff->fn = 0; | |
192 | f->func = dofork; | |
193 | f->state = ff; | |
194 | } | |
195 | ||
196 | static void nextfork(unsigned ac, char **av) | |
197 | { | |
c953f4b5 | 198 | unsigned i, j; |
50c74957 | 199 | filter *f; |
200 | forkfilt *ff; | |
201 | forknode *fn, **ffn; | |
202 | peer *p; | |
203 | ||
c953f4b5 | 204 | if (ac < 1) |
205 | die(1, "syntax: next:NAME:..."); | |
50c74957 | 206 | for (i = 0; i < 2; i++) { |
207 | p = &peers[i]; | |
208 | for (f = p->f; f; f = f->next) { | |
209 | if (f->func != dofork) | |
210 | continue; | |
211 | ff = f->state; | |
c953f4b5 | 212 | for (j = 0; j < ac; j++) { |
213 | if (strcmp(av[j], ff->name) == 0) | |
214 | goto match; | |
215 | } | |
216 | continue; | |
217 | match: | |
50c74957 | 218 | fn = CREATE(forknode); |
219 | for (ffn = &ff->fn; *ffn; ffn = &(*ffn)->next) | |
220 | ; | |
221 | fn->f = f->next; | |
222 | f->next = 0; | |
223 | fn->next = 0; | |
224 | *ffn = fn; | |
225 | } | |
226 | } | |
227 | } | |
228 | ||
229 | /*----- Corrupt filter ----------------------------------------------------*/ | |
230 | ||
231 | typedef struct corrupt { | |
232 | unsigned p_corrupt; | |
233 | } corrupt; | |
234 | ||
235 | static void docorrupt(filter *f, const octet *buf, size_t sz) | |
236 | { | |
237 | corrupt *c = f->state; | |
238 | octet b[PKBUFSZ]; | |
239 | memcpy(b, buf, sz); | |
240 | ||
241 | while (!RND(c->p_corrupt)) { | |
242 | puts("corrupt packet"); | |
243 | b[RND(sz)] ^= RND(256); | |
244 | } | |
245 | PASS(f->next, b, sz); | |
246 | } | |
247 | ||
248 | static void addcorrupt(filter *f, unsigned ac, char **av) | |
249 | { | |
250 | corrupt *c; | |
251 | if (ac > 1) | |
a2e06ed5 | 252 | die(1, "syntax: filt:corrupt[:P-CORRUPT]"); |
50c74957 | 253 | c = CREATE(corrupt); |
254 | if (ac > 0) | |
255 | c->p_corrupt = atoi(av[0]); | |
256 | else | |
257 | c->p_corrupt = 5; | |
258 | f->state = c; | |
259 | f->func = docorrupt; | |
260 | } | |
261 | ||
a2e06ed5 MW |
262 | /*----- Drop filter -------------------------------------------------------*/ |
263 | ||
264 | typedef struct drop { | |
265 | unsigned p_drop; | |
266 | } drop; | |
267 | ||
268 | static void dodrop(filter *f, const octet *buf, size_t sz) | |
269 | { | |
270 | drop *d = f->state; | |
271 | ||
272 | if (!RND(d->p_drop)) | |
273 | puts("drop packet"); | |
274 | else | |
275 | PASS(f->next, buf, sz); | |
276 | } | |
277 | ||
278 | static void adddrop(filter *f, unsigned ac, char **av) | |
279 | { | |
280 | drop *d; | |
281 | if (ac > 1) | |
282 | die(1, "syntax: filt:drop[:P-DROP]"); | |
283 | d = CREATE(drop); | |
284 | if (ac > 0) | |
285 | d->p_drop = atoi(av[0]); | |
286 | else | |
287 | d->p_drop = 5; | |
288 | f->state = d; | |
289 | f->func = dodrop; | |
290 | } | |
291 | ||
50c74957 | 292 | /*----- Delay filter ------------------------------------------------------*/ |
293 | ||
294 | typedef struct delaynode { | |
295 | unsigned flag; | |
296 | sel_timer tm; | |
297 | unsigned i; | |
298 | struct delay *d; | |
299 | octet *buf; | |
300 | size_t sz; | |
301 | unsigned seq; | |
302 | } delaynode; | |
303 | ||
304 | typedef struct delay { | |
305 | unsigned max, n; | |
306 | unsigned long t; | |
307 | unsigned p_replay; | |
308 | filter *f; | |
309 | delaynode *q; | |
310 | } delay; | |
311 | ||
312 | static void dtimer(struct timeval *tv, void *vv); | |
313 | ||
314 | static void dinsert(delaynode *dn) | |
315 | { | |
316 | struct timeval tv; | |
317 | sel_timer *ta, *tb; | |
318 | unsigned long tdelta = RND(dn->d->t); | |
319 | gettimeofday(&tv, 0); | |
320 | TV_ADDL(&tv, &tv, 0, tdelta); | |
321 | assert(!dn->flag); | |
322 | sel_addtimer(&sel, &dn->tm, &tv, dtimer, dn); | |
323 | dn->flag = 1; | |
324 | for (ta = tb = sel.timers; ta; ta = ta->next) { | |
325 | ta = ta->next; if (!ta) break; assert(ta != tb); | |
326 | ta = ta->next; if (!ta) break; assert(ta != tb); | |
327 | tb = tb->next; | |
328 | } | |
329 | printf(" delay %lu usecs", tdelta); | |
330 | } | |
331 | ||
332 | static void dsend(delaynode *dn, unsigned force) | |
333 | { | |
334 | delay *d = dn->d; | |
335 | delaynode *ddn; | |
336 | fputs(" send...\n", stdout); | |
337 | assert(dn->buf); | |
338 | PASS(d->f->next, dn->buf, dn->sz); | |
339 | fputs("delay ...", stdout); | |
340 | if (!force) | |
341 | dinsert(dn); | |
342 | else { | |
343 | xfree(dn->buf); | |
344 | dn->buf = 0; | |
345 | d->n--; | |
346 | if (dn->i < d->n) { | |
347 | ddn = &d->q[d->n]; | |
348 | sel_rmtimer(&ddn->tm); | |
349 | sel_addtimer(&sel, &dn->tm, &ddn->tm.tv, dtimer, dn); | |
350 | dn->flag = 1; | |
351 | dn->buf = ddn->buf; | |
352 | dn->sz = ddn->sz; | |
353 | dn->seq = ddn->seq; | |
354 | ddn->buf = 0; | |
355 | ddn->flag = 0; | |
356 | printf(" move id %u from slot %u to slot %u", ddn->seq, ddn->i, dn->i); | |
357 | } | |
358 | { unsigned i; for (i = 0; i < d->n; i++) assert(d->q[i].buf); } | |
359 | fputs(" remove", stdout); | |
360 | } | |
361 | } | |
362 | ||
363 | static void dtimer(struct timeval *tv, void *vv) | |
364 | { | |
365 | delaynode *dn = vv; | |
366 | printf("delay timer peer `%s' id %u slot %u", | |
367 | dn->d->f->p_from->name, dn->seq, dn->i); | |
368 | dn->flag = 0; | |
369 | dsend(dn, RND(dn->d->p_replay)); | |
370 | fputc('\n', stdout); | |
371 | } | |
372 | ||
373 | static void dodelay(filter *f, const octet *buf, size_t sz) | |
374 | { | |
375 | delay *d = f->state; | |
376 | delaynode *dn; | |
377 | static unsigned seq = 0; | |
378 | ||
379 | fputs("delay", stdout); | |
380 | if (d->n == d->max) { | |
381 | dn = &d->q[RND(d->n)]; | |
382 | printf(" force uid %u", dn->seq); | |
383 | sel_rmtimer(&dn->tm); | |
384 | dn->flag = 0; | |
385 | dsend(dn, 1); | |
386 | fputc(';', stdout); | |
387 | } | |
388 | dn = &d->q[d->n++]; | |
389 | dn->seq = seq++; | |
390 | printf(" new id %u in slot %u", dn->seq, dn->i); | |
391 | dn->buf = xmalloc(sz); | |
392 | dn->sz = sz; | |
393 | memcpy(dn->buf, buf, sz); | |
394 | dinsert(dn); | |
395 | fputc('\n', stdout); | |
396 | } | |
397 | ||
398 | static void adddelay(filter *f, unsigned ac, char **av) | |
399 | { | |
400 | delay *d; | |
401 | unsigned i; | |
402 | ||
403 | if (ac < 1 || ac > 3) | |
a2e06ed5 | 404 | die(1, "syntax: filt:delay:QLEN[:MILLIS:P-REPLAY]"); |
50c74957 | 405 | d = CREATE(delay); |
406 | d->max = atoi(av[0]); | |
407 | if (ac > 1) | |
408 | d->t = strtoul(av[1], 0, 10); | |
409 | else | |
410 | d->t = 100; | |
411 | d->t *= 1000; | |
412 | if (ac > 2) | |
413 | d->p_replay = atoi(av[2]); | |
414 | else | |
415 | d->p_replay = 20; | |
416 | d->n = 0; | |
417 | d->q = xmalloc(d->max * sizeof(delaynode)); | |
418 | d->f = f; | |
419 | f->state = d; | |
420 | f->func = dodelay; | |
421 | for (i = 0; i < d->max; i++) { | |
422 | d->q[i].d = d; | |
423 | d->q[i].i = i; | |
424 | d->q[i].buf = 0; | |
425 | d->q[i].flag = 0; | |
426 | } | |
427 | } | |
428 | ||
429 | /*----- Filters -----------------------------------------------------------*/ | |
430 | ||
431 | static void dosend(filter *f, const octet *buf, size_t sz) | |
432 | { | |
433 | printf("send to `%s'\n", f->p_to->name); | |
99735357 | 434 | DISCARD(write(f->p_to->sf.fd, buf, sz)); |
50c74957 | 435 | } |
436 | ||
437 | static void addsend(filter *f, unsigned ac, char **av) | |
438 | { | |
439 | if (ac) | |
440 | die(1, "syntax: filt:send"); | |
441 | f->func = dosend; | |
442 | } | |
443 | ||
444 | const struct filtab { | |
445 | const char *name; | |
446 | void (*func)(filter */*f*/, unsigned /*ac*/, char **/*av*/); | |
447 | } filtab[] = { | |
448 | { "send", addsend }, | |
449 | { "fork", addfork }, | |
450 | { "delay", adddelay }, | |
a2e06ed5 | 451 | { "drop", adddrop }, |
50c74957 | 452 | { "corrupt", addcorrupt }, |
453 | { 0, 0 } | |
454 | }; | |
455 | ||
456 | static void dofilter(peer *from, peer *to, unsigned ac, char **av) | |
457 | { | |
458 | filter **ff, *f = CREATE(filter); | |
459 | const struct filtab *ft; | |
460 | if (ac < 1) | |
461 | die(1, "syntax: {l,r,}filt:NAME:..."); | |
462 | f->next = 0; | |
463 | f->p_from = from; | |
464 | f->p_to = to; | |
465 | f->state = 0; | |
466 | for (ff = &from->f; *ff; ff = &(*ff)->next) | |
467 | ; | |
468 | *ff = f; | |
469 | for (ft = filtab; ft->name; ft++) { | |
470 | if (strcmp(av[0], ft->name) == 0) { | |
471 | ft->func(f, ac - 1, av + 1); | |
472 | return; | |
473 | } | |
474 | } | |
475 | die(1, "unknown filter `%s'", av[0]); | |
476 | } | |
477 | ||
478 | /*----- Flooding ----------------------------------------------------------*/ | |
479 | ||
480 | typedef struct flood { | |
481 | peer *p; | |
482 | unsigned type; | |
483 | size_t sz; | |
484 | unsigned long t; | |
485 | sel_timer tm; | |
486 | } flood; | |
487 | ||
488 | static void setflood(flood *f); | |
489 | ||
490 | static void floodtimer(struct timeval *tv, void *vv) | |
491 | { | |
492 | flood *f = vv; | |
493 | octet buf[PKBUFSZ]; | |
494 | size_t sz; | |
495 | ||
496 | sz = RND(f->sz); | |
497 | sz += RND(f->sz); | |
498 | sz += RND(f->sz); | |
499 | sz += RND(f->sz); | |
500 | sz /= 2; | |
501 | ||
502 | rng->ops->fill(rng, buf, sz); | |
503 | if (f->type < 0x100) | |
504 | buf[0] = f->type; | |
505 | puts("flood packet"); | |
506 | PASS(f->p->f, buf, sz); | |
507 | setflood(f); | |
508 | } | |
e04c2d50 | 509 | |
50c74957 | 510 | static void setflood(flood *f) |
511 | { | |
512 | struct timeval tv; | |
513 | gettimeofday(&tv, 0); | |
514 | TV_ADDL(&tv, &tv, 0, RND(f->t)); | |
515 | sel_addtimer(&sel, &f->tm, &tv, floodtimer, f); | |
516 | } | |
517 | ||
518 | static void doflood(peer *p, unsigned ac, char **av) | |
519 | { | |
520 | flood *f; | |
521 | if (ac > 3) | |
522 | die(1, "syntax: flood[:TYPE:MILLIS:SIZE]"); | |
523 | f = CREATE(flood); | |
524 | f->p = p; | |
525 | if (ac > 0) | |
526 | f->type = strtoul(av[0], 0, 16); | |
527 | else | |
528 | f->type = 0x100; | |
529 | if (ac > 1) | |
530 | f->t = atoi(av[1]); | |
531 | else | |
532 | f->t = 10; | |
533 | if (ac > 2) | |
534 | f->sz = atoi(av[2]); | |
535 | else | |
536 | f->sz = 128; | |
537 | f->t *= 1000; | |
538 | setflood(f); | |
539 | } | |
540 | ||
541 | /*----- Configuration commands --------------------------------------------*/ | |
542 | ||
543 | static void parse(char *p); | |
544 | ||
545 | static void addflood(unsigned ac, char **av) { | |
546 | doflood(&peers[0], ac, av); | |
547 | doflood(&peers[1], ac, av); | |
548 | } | |
549 | static void addlflood(unsigned ac, char **av) { | |
550 | doflood(&peers[0], ac, av); | |
551 | } | |
552 | static void addrflood(unsigned ac, char **av) { | |
553 | doflood(&peers[1], ac, av); | |
554 | } | |
555 | ||
556 | static void addfilter(unsigned ac, char **av) { | |
557 | dofilter(&peers[0], &peers[1], ac, av); | |
558 | dofilter(&peers[1], &peers[0], ac, av); | |
559 | } | |
560 | static void addlfilter(unsigned ac, char **av) { | |
561 | dofilter(&peers[0], &peers[1], ac, av); | |
562 | } | |
563 | static void addrfilter(unsigned ac, char **av) { | |
564 | dofilter(&peers[1], &peers[0], ac, av); | |
565 | } | |
566 | ||
567 | static void include(unsigned ac, char **av) | |
568 | { | |
569 | FILE *fp; | |
570 | dstr d = DSTR_INIT; | |
571 | if (!ac) | |
572 | die(1, "syntax: include:FILE:..."); | |
573 | while (*av) { | |
574 | if ((fp = fopen(*av, "r")) == 0) | |
575 | die(1, "fopen `%s': %s", *av, strerror(errno)); | |
576 | while (dstr_putline(&d, fp) != EOF) { | |
577 | parse(d.buf); | |
578 | DRESET(&d); | |
579 | } | |
580 | fclose(fp); | |
581 | av++; | |
582 | } | |
583 | } | |
584 | ||
585 | const struct cmdtab { | |
586 | const char *name; | |
587 | void (*func)(unsigned /*ac*/, char **/*av*/); | |
588 | } cmdtab[] = { | |
589 | { "peer", addpeer }, | |
590 | { "include", include }, | |
591 | { "filt", addfilter }, | |
592 | { "lfilt", addlfilter }, | |
593 | { "rfilt", addrfilter }, | |
594 | { "next", nextfork }, | |
595 | { "flood", addflood }, | |
596 | { "lflood", addlflood }, | |
597 | { "rflood", addrflood }, | |
598 | { 0, 0 } | |
599 | }; | |
600 | ||
601 | #define AVMAX 16 | |
602 | ||
603 | static void parse(char *p) | |
604 | { | |
605 | char *v[AVMAX]; | |
606 | unsigned c = 0; | |
607 | const struct cmdtab *ct; | |
608 | ||
609 | p = strtok(p, ":"); | |
610 | if (!p || *p == '#') | |
611 | return; | |
612 | do { | |
613 | v[c++] = p; | |
614 | p = strtok(0, ":"); | |
615 | } while (p && c < AVMAX - 1); | |
616 | v[c] = 0; | |
617 | for (ct = cmdtab; ct->name; ct++) { | |
618 | if (strcmp(ct->name, v[0]) == 0) { | |
619 | ct->func(c - 1, v + 1); | |
620 | return; | |
621 | } | |
622 | } | |
623 | die(1, "unknown command `%s'", v[0]); | |
624 | } | |
625 | ||
626 | /*----- Main driver -------------------------------------------------------*/ | |
627 | ||
628 | static void version(FILE *fp) | |
f98df549 | 629 | { pquis(fp, "$, TrIPE version " VERSION "\n"); } |
50c74957 | 630 | |
631 | static void usage(FILE *fp) | |
f98df549 | 632 | { pquis(fp, "Usage: $ [-k KEYRING] DIRECTIVE...\n"); } |
50c74957 | 633 | |
634 | static void help(FILE *fp) | |
635 | { | |
636 | version(fp); | |
637 | putc('\n', fp); | |
638 | usage(fp); | |
2d752320 | 639 | fputs("\n\ |
3cdc3f3a | 640 | Options:\n\ |
641 | \n\ | |
642 | -h, --help Show this help text.\n\ | |
643 | -v, --version Show the version number.\n\ | |
644 | -u, --usage Show terse usage summary.\n\ | |
645 | \n\ | |
646 | -k, --keyring=FILE Fetch keys from FILE.\n\ | |
647 | \n\ | |
2d752320 | 648 | Directives:\n\ |
649 | peer:NAME:LOCAL-PORT:REMOTE-ADDR:REMOTE-PORT\n\ | |
650 | include:FILE\n\ | |
651 | {,l,r}filt:FILTER:ARGS:...\n\ | |
652 | next:TAG\n\ | |
653 | {,l,r}flood:TYPE:MILLIS:SIZE\n\ | |
654 | \n\ | |
655 | Filters:\n\ | |
656 | send\n\ | |
657 | fork:TAG\n\ | |
658 | delay:QLEN[:MILLIS:P-REPLAY]\n\ | |
a2e06ed5 | 659 | drop[:P-DROP]\n\ |
2d752320 | 660 | corrupt[:P-CORRUPT]\n", |
661 | fp); | |
50c74957 | 662 | } |
663 | ||
664 | int main(int argc, char *argv[]) | |
665 | { | |
666 | const char *kfname = "keyring.pub"; | |
667 | int i; | |
668 | unsigned f = 0; | |
669 | char buf[16]; | |
670 | ||
671 | #define f_bogus 1u | |
672 | ||
673 | ego(argv[0]); | |
674 | for (;;) { | |
675 | static const struct option opt[] = { | |
676 | { "help", 0, 0, 'h' }, | |
677 | { "version", 0, 0, 'v' }, | |
678 | { "usage", 0, 0, 'u' }, | |
679 | { "keyring", OPTF_ARGREQ, 0, 'k' }, | |
680 | { 0, 0, 0, 0 } | |
681 | }; | |
682 | if ((i = mdwopt(argc, argv, "hvuk:", opt, 0, 0, 0)) < 0) | |
683 | break; | |
684 | switch (i) { | |
685 | case 'h': | |
686 | help(stdout); | |
687 | exit(0); | |
688 | case 'v': | |
689 | version(stdout); | |
690 | exit(0); | |
691 | case 'u': | |
692 | usage(stdout); | |
693 | exit(0); | |
694 | case 'k': | |
695 | kfname = optarg; | |
696 | break; | |
697 | default: | |
698 | f |= f_bogus; | |
699 | break; | |
700 | } | |
701 | } | |
702 | if (f & f_bogus) { | |
703 | usage(stderr); | |
704 | exit(1); | |
705 | } | |
706 | rand_noisesrc(RAND_GLOBAL, &noise_source); | |
707 | rand_seed(RAND_GLOBAL, 160); | |
708 | rand_get(RAND_GLOBAL, buf, sizeof(buf)); | |
709 | rng = rc4_rand(buf, sizeof(buf)); | |
710 | sel_init(&sel); | |
711 | if (key_open(&keys, kfname, KOPEN_READ, key_moan, 0)) | |
712 | die(1, "couldn't open `%s': %s", kfname, strerror(errno)); | |
713 | for (i = optind; i < argc; i++) | |
714 | parse(argv[i]); | |
715 | if (npeer != 2) | |
716 | die(1, "need two peers"); | |
c9cb7c0c MW |
717 | for (;;) { |
718 | if (sel_select(&sel) && errno != EINTR) | |
719 | die(1, "select failed: %s", strerror(errno)); | |
720 | } | |
50c74957 | 721 | |
722 | #undef f_bogus | |
723 | } | |
724 | ||
725 | /*----- That's all, folks -------------------------------------------------*/ |