chiark / gitweb /
Makefile.am: Only check SLIP on distcheck.
[tripe] / uslip / uslip.c
1 /* -*-c-*-
2  *
3  * A simple SLIP implementation drivable from the command-line
4  *
5  * (c) 2008 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Trivial IP Encryption (TrIPE).
11  *
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.
16  *
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.
21  *
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.
25  */
26
27 /*----- Header files ------------------------------------------------------*/
28
29 #include "config.h"
30
31 #include <assert.h>
32 #include <ctype.h>
33 #include <errno.h>
34 #include <signal.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include <sys/types.h>
40 #include <sys/time.h>
41 #include <unistd.h>
42
43 #include <sys/socket.h>
44 #include <sys/un.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
47 #include <netdb.h>
48
49 #include <mLib/alloc.h>
50 #include <mLib/bits.h>
51 #include <mLib/conn.h>
52 #include <mLib/dstr.h>
53 #include <mLib/fdflags.h>
54 #include <mLib/mdwopt.h>
55 #include <mLib/quis.h>
56 #include <mLib/report.h>
57 #include <mLib/sel.h>
58 #include <mLib/sub.h>
59 #include <mLib/tv.h>
60
61 #include "slip.h"
62
63 #undef sun
64
65 /*----- Data structures ---------------------------------------------------*/
66
67 typedef struct pkq_node {
68   struct pkq_node *next;
69   unsigned char *buf;
70   size_t n, sz;
71 } pkq_node;
72
73 typedef struct pkq {
74   pkq_node *head, **tail;
75 } pkq;
76
77 enum { START, OK, ESC, BAD, SYNC1, SYNC2 };
78
79 typedef struct client {
80   sel_file f;
81   dstr d;
82   int mode;
83 } client;
84
85 typedef struct gobbler {
86   sel_file f;
87   void (*done)(struct gobbler *, int, void *);
88   void *p;
89 } gobbler;
90
91 typedef struct dribbler {
92   sel_file f;
93   pkq q;
94   void (*done)(struct dribbler *, int, void *);
95   void *p;
96 } dribbler;
97
98 typedef struct waiter {
99   struct waiter *next;
100   int fd;
101   gobbler *g;
102 } waiter;
103
104 /*----- Static variables --------------------------------------------------*/
105
106 static pkq q_in;
107 static dribbler *dribble_out;
108 static dstr slipbuf = DSTR_INIT;
109 static int slipstate = SYNC1;
110 static sel_file slip_in, listener;
111 static sel_state sel;
112 static unsigned reasons;
113 static waiter *wait_head, **wait_tail = &wait_head;
114 static unsigned char buf[16384];
115 static const char *name;
116
117 /*----- Utilities ---------------------------------------------------------*/
118
119 static void socketaddr(struct sockaddr_un *sun, size_t *sz)
120 {
121   size_t n = strlen(name) + 1;
122   if (n + offsetof(struct sockaddr_un, sun_path) > sizeof(*sun))
123     die(EXIT_FAILURE, "name too long: `%s'", name);
124   sun->sun_family = AF_UNIX;
125   memcpy(sun->sun_path, name, n);
126   if (sz)
127     *sz = n + offsetof(struct sockaddr_un, sun_path);
128 }
129
130 /*------ Packet queue -----------------------------------------------------*
131  *
132  * A packet queue contains a sequence of octet strings.  Packets can be added
133  * to the end and read off the front.
134  */
135
136 static void initqueue(pkq *q) { q->head = 0; q->tail = &q->head; }
137
138 static pkq_node *make_pkqnode(void *p, size_t n)
139 {
140   pkq_node *pn;
141
142   if (!n) return (0);
143   pn = CREATE(pkq_node);
144   pn->next = 0;
145   pn->buf = xmalloc(n);
146   if (p) memcpy(pn->buf, p, n);
147   pn->sz = n;
148   pn->n = 0;
149   return (pn);
150 }
151
152 static int enqueue(pkq *q, pkq_node *pn)
153 {
154   int rc = 0;
155
156   if (!pn) return (0);
157   rc = !q->head;
158   pn->next = 0;
159   *q->tail = pn;
160   q->tail = &pn->next;
161   return (rc);
162 }
163
164 static void destroy_pkqnode(pkq_node *pn) { xfree(pn->buf); DESTROY(pn); }
165
166 static int dequeue(pkq *q, int freep)
167 {
168   pkq_node *pn = q->head;
169   assert(pn);
170   q->head = pn->next;
171   if (freep)
172     destroy_pkqnode(pn);
173   if (!q->head) {
174     q->tail = &q->head;
175     return (1);
176   }
177   return (0);
178 }
179
180 static void destroy_pkq(pkq *q)
181 {
182   pkq_node *pn, *pnn;
183
184   for (pn = q->head; pn; pn = pnn) {
185     pnn = pn->next;
186     destroy_pkqnode(pn);
187   }
188   q->head = 0; q->tail = &q->head;
189 }
190
191 /*----- Gobblers ----------------------------------------------------------*
192  *
193  * A gobbler just eats everything it sees on its input descriptor.
194  * Eventually, when it sees end-of-file, it closes the input descriptor,
195  * calls a user-supplied calback function, and quits.
196  */
197
198 static void gobbler_close(gobbler *g)
199   { if (g->f.fd != -1) { sel_rmfile(&g->f); close(g->f.fd); g->f.fd = -1; } }
200
201 static void gobbler_destroy(gobbler *g) { gobbler_close(g); DESTROY(g); }
202
203 static void do_gobble_in(int fd, unsigned mode, void *p)
204 {
205   gobbler *g = p;
206   ssize_t n;
207
208   for (;;) {
209     n = read(fd, buf, sizeof(buf));
210     if (n < 0) {
211       if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
212         break;
213       else {
214         moan("read (gobble): %s", strerror(errno));
215         if (g->done) g->done(g, errno, g->p);
216         gobbler_close(g);
217         break;
218       }
219     } else if (n == 0) {
220       if (g->done) g->done(g, 0, g->p);
221       gobbler_close(g);
222       break;
223     }
224   }
225 }
226
227 static gobbler *make_gobbler(int fd,
228                              void (*done)(gobbler *, int, void *),
229                              void *p)
230 {
231   gobbler *g;
232
233   g = CREATE(gobbler);
234   g->done = done;
235   g->p = p;
236   sel_initfile(&sel, &g->f, fd, SEL_READ, do_gobble_in, g);
237   sel_addfile(&g->f);
238   do_gobble_in(fd, SEL_READ, g);
239   return (g);
240 }
241
242 /*----- Dribbler ----------------------------------------------------------*
243  *
244  * A dribbler hands out data from a packet queue to a file descriptor.  It
245  * makes no attempt to preserve the record boundaries inherent in the packet
246  * queue structure.  If the dribbler reaches the end of its queue, it invokes
247  * a user-supplied `done' function and stops selecting its output descriptor
248  * for writing.
249  */
250
251 static void cripple_dribbler(dribbler *d) { close(d->f.fd); d->f.fd = -1; }
252
253 static void destroy_dribbler(dribbler *d)
254   { cripple_dribbler(d); DESTROY(d); }
255
256 static void dribble_done(dribbler *d, int err)
257 {
258   if (d->q.head) {
259     sel_rmfile(&d->f);
260     destroy_pkq(&d->q);
261   }
262   d->done(d, err, d->p);
263 }
264
265 static void do_dribble_out(int fd, unsigned mode, void *p)
266 {
267   dribbler *d = p;
268   ssize_t n;
269   pkq_node *pn;
270
271   for (;;) {
272     pn = d->q.head;
273     n = write(fd, pn->buf + pn->n, pn->sz - pn->n);
274     if (n < 0) {
275       if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
276         break;
277       else {
278         dribble_done(d, errno);
279         break;
280       }
281     }
282     pn->n += n;
283     if (pn->n == pn->sz && dequeue(&d->q, 1)) {
284       sel_rmfile(&d->f);
285       dribble_done(d, 0);
286       break;
287     }
288   }
289 }
290
291 static int enqueue_dribble(dribbler *d, pkq_node *pn)
292 {
293   if (d->f.fd == -1) {
294     destroy_pkqnode(pn);
295     return (0);
296   }
297   if (enqueue(&d->q, pn)) {
298     sel_addfile(&d->f);
299     do_dribble_out(d->f.fd, SEL_WRITE, d);
300     return (1);
301   }
302   return (0);
303 }
304
305 static dribbler *make_dribbler(int fd,
306                                void (*done)(dribbler *, int, void *),
307                                void *p)
308 {
309   dribbler *d = CREATE(dribbler);
310   sel_initfile(&sel, &d->f, fd, SEL_WRITE, do_dribble_out, d);
311   initqueue(&d->q);
312   d->done = done;
313   d->p = p;
314   return (d);
315 }
316
317 /*----- Clients -----------------------------------------------------------*/
318
319 static void done_client_dribble(dribbler *d, int err, void *p)
320 {
321   if (err)
322     moan("write (client): %s", strerror(err));
323   gobbler_destroy(p);
324   destroy_dribbler(d);
325   reasons--;
326 }
327
328 static void dequeue_to_waiter(void)
329 {
330   waiter *w;
331   dribbler *d;
332   pkq_node *pn;
333
334   while (q_in.head && wait_head) {
335     w = wait_head;
336     wait_head = w->next;
337     if (!wait_head)
338       wait_tail = &wait_head;
339     d = make_dribbler(w->fd, done_client_dribble, w->g);
340     DESTROY(w);
341     pn = q_in.head;
342     if (dequeue(&q_in, 0))
343       reasons--;
344     enqueue_dribble(d, pn);
345   }
346 }
347
348 static void client_destroy(client *c)
349 {
350   sel_rmfile(&c->f);
351   close(c->f.fd);
352   dstr_destroy(&c->d);
353   reasons--;
354   DESTROY(c);
355 }
356
357 static void do_client_in(int fd, unsigned mode, void *p)
358 {
359   client *c = p;
360   ssize_t n, i, i0;
361   waiter *w;
362
363   /* --- Attention --- *
364    *
365    * The queue for outbound packets is SLIP-encoded; we need to encode it
366    * here. The queue for inbound packets is raw.
367    */
368
369   for (;;) {
370     n = read(fd, buf, sizeof(buf));
371     i0 = 0;
372     if (n < 0) {
373       if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
374         break;
375       else {
376         moan("read (client): %s", strerror(errno));
377         client_destroy(c);
378         return;
379       }
380     } else if (n == 0) {
381       if (c->mode == '>') {
382         DPUTC(&c->d, SL_END);
383         if (enqueue_dribble(dribble_out, make_pkqnode(c->d.buf, c->d.len)))
384           reasons++;
385       }
386       client_destroy(c);
387       return;
388     }
389     if (c->mode == '?') {
390       switch (buf[0]) {
391         case '>':
392           i0 = 1;
393           c->mode = '>';
394           break;
395         case '<':
396           w = CREATE(waiter);
397           w->g = make_gobbler(fd, 0, 0);
398           w->next = 0;
399           w->fd = fd;
400           *wait_tail = w;
401           wait_tail = &w->next;
402           sel_rmfile(&c->f);
403           DESTROY(c);
404           dequeue_to_waiter();
405           return;
406         default:
407           moan("bad client mode `%c'", buf[0]);
408           client_destroy(c);
409           return;
410       }
411     }
412     for (i = i0; i < n; i++) {
413       switch (buf[i]) {
414         case SL_ESC:
415           DPUTC(&c->d, SL_ESC);
416           DPUTC(&c->d, SL_ESCESC);
417           break;
418         case SL_END:
419           DPUTC(&c->d, SL_ESC);
420           DPUTC(&c->d, SL_ESCEND);
421           break;
422         default:
423           DPUTC(&c->d, buf[i]);
424           break;
425       }
426     }
427   }
428 }
429
430 static void do_accept(int fd, unsigned mode, void *hunoz)
431 {
432   client *c;
433   struct sockaddr_un sun;
434   socklen_t n = sizeof(sun);
435   int nfd;
436
437   if ((nfd = accept(fd, (struct sockaddr *)&sun, &n)) < 0) {
438     if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
439       return;
440     else
441       die(EXIT_FAILURE, "accept: %s", strerror(errno));
442   }
443   c = CREATE(client);
444   c->mode = '?';
445   dstr_create(&c->d);
446   fdflags(nfd, O_NONBLOCK, O_NONBLOCK, 0, 0);
447   sel_initfile(&sel, &c->f, nfd, SEL_READ, do_client_in, c);
448   sel_addfile(&c->f);
449   reasons++;
450 }
451
452 /*----- Main daemon -------------------------------------------------------*/
453
454 static void done_slip_dribble(dribbler *d, int err, void *p)
455 {
456   if (!err)
457     reasons--;
458   else if (err != EPIPE)
459     die(EXIT_FAILURE, "write (slip): %s", strerror(errno));
460   else
461     cripple_dribbler(d);
462 }
463
464 static void do_slip_in(int fd, unsigned mode, void *hunoz)
465 {
466   ssize_t i, n;
467
468   /* --- Attention --- *
469    *
470    * The queue for inbound packets contains raw data; we need to decode it
471    * here.  The queue for outbound packets is SLIP-encoded.
472    *
473    * TrIPE sends two empty packets on start-up, in order to resynchronize the
474    * target.  We don't need this and it messes us up.
475    */
476
477   for (;;) {
478     n = read(fd, buf, sizeof(buf));
479     if (n == 0) {
480       switch (slipstate) {
481         case SYNC1:
482         case SYNC2:
483         case START:
484         case BAD:
485           break;
486         default:
487           moan("eof found while processing packet (discarding)");
488           break;
489       }
490       close(fd);
491       sel_rmfile(&slip_in);
492       reasons--;
493       return;
494     } else if (n < 0) {
495       if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
496         break;
497       die(EXIT_FAILURE, "read (slip in): %s", strerror(errno));
498     }
499     for (i = 0; i < n; i++) {
500       switch (slipstate) {
501         case SYNC1:
502           switch (buf[i]) {
503             case SL_END:
504               slipstate = SYNC2;
505               break;
506             default:
507               goto start;
508           }
509           break;
510         case SYNC2:
511           switch (buf[i]) {
512             case SL_END:
513               slipstate = START;
514               break;
515             default:
516               goto start;
517           }
518           break;
519         case BAD:
520           switch (buf[i]) {
521             case SL_END:
522               DRESET(&slipbuf);
523               slipstate = OK;
524               break;
525             default:
526               break;
527           }
528           break;
529         case ESC:
530           switch (buf[i]) {
531             case SL_ESCEND:
532               DPUTC(&slipbuf, SL_END);
533               slipstate = OK;
534               break;
535             case SL_ESCESC:
536               DPUTC(&slipbuf, SL_ESC);
537               slipstate = OK;
538               break;
539             case SL_END:
540               moan("found escaped end byte (discard packet and resync");
541               DRESET(&slipbuf);
542               slipstate = OK;
543               break;
544             default:
545               moan("unexpected escape char 0x%02x", buf[i]);
546               slipstate = BAD;
547               break;
548           }
549           break;
550         case START:
551         case OK:
552         start:
553           switch (buf[i]) {
554             case SL_ESC:
555               slipstate = ESC;
556               break;
557             case SL_END:
558               if (enqueue(&q_in, make_pkqnode(slipbuf.buf, slipbuf.len)))
559                 reasons++;
560               DRESET(&slipbuf);
561               dequeue_to_waiter();
562               slipstate = START;
563               break;
564             default:
565               DPUTC(&slipbuf, buf[i]);
566               slipstate = OK;
567               break;
568           }
569           break;
570       }
571     }
572   }
573 }
574
575 static void slipif(void)
576 {
577   int fd;
578   dstr d = DSTR_INIT;
579   struct sockaddr_un sun;
580   size_t sz;
581
582   /* --- Make the socket --- */
583
584   if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
585     die(EXIT_FAILURE, "socket: %s", strerror(errno));
586   socketaddr(&sun, &sz);
587   if (bind(fd, (struct sockaddr *)&sun, sz))
588     die(EXIT_FAILURE, "bind: %s", strerror(errno));
589   if (listen(fd, 5))
590     die(EXIT_FAILURE, "listen: %s", strerror(errno));
591
592   /* --- Set up listeners for things --- */
593
594   fdflags(fd, O_NONBLOCK, O_NONBLOCK, 0, 0);
595   sel_initfile(&sel, &listener, fd, SEL_READ, do_accept, 0);
596   sel_addfile(&listener);
597
598   fdflags(STDIN_FILENO, O_NONBLOCK, O_NONBLOCK, 0, 0);
599   fdflags(STDOUT_FILENO, O_NONBLOCK, O_NONBLOCK, 0, 0);
600   sel_initfile(&sel, &slip_in, STDIN_FILENO, SEL_READ, do_slip_in, 0);
601   dribble_out = make_dribbler(STDOUT_FILENO, done_slip_dribble, 0);
602   sel_addfile(&slip_in);
603
604   initqueue(&q_in);
605   reasons++;
606
607   /* --- Write the interface name --- */
608
609   dstr_putf(&d, "%s-%s\n", QUIS, name);
610   if (enqueue_dribble(dribble_out, make_pkqnode(d.buf, d.len)))
611     reasons++;
612   dstr_destroy(&d);
613
614   /* --- Main loop --- */
615
616   while (reasons) {
617     if (sel_select(&sel))
618       die(EXIT_FAILURE, "select: %s", strerror(errno));
619   }
620
621   /* --- Done --- */
622
623   unlink(name);
624 }
625
626 /*----- Putting and getting -----------------------------------------------*/
627
628 static int make_sock(int mode)
629 {
630   struct sockaddr_un sun;
631   size_t sz;
632   int fd;
633   char ch;
634
635   if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
636     die(EXIT_FAILURE, "socket: %s", strerror(errno));
637   socketaddr(&sun, &sz);
638   if (connect(fd, (struct sockaddr *)&sun, sz))
639     die(EXIT_FAILURE, "connect: %s", strerror(errno));
640   ch = mode;
641   if (write(fd, &ch, 1) < 0)
642     die(EXIT_FAILURE, "write (mode): %s", strerror(errno));
643   return (fd);
644 }
645
646 static void shovel(int from, int to)
647 {
648   ssize_t n;
649   size_t sz;
650   unsigned char *p;
651
652   for (;;) {
653     n = read(from, buf, sizeof(buf));
654     if (n < 0) {
655       if (errno == EINTR)
656         continue;
657       else
658         die(EXIT_FAILURE, "read (shovel): %s", strerror(errno));
659     } else if (n == 0)
660       break;
661
662     sz = n;
663     p = buf;
664     while (sz) {
665       n = write(to, p, sz);
666       if (n < 0) {
667         if (errno == EINTR)
668           continue;
669         else
670           die(EXIT_FAILURE, "write (shovel): %s", strerror(errno));
671       }
672       p += n;
673       sz -= n;
674     }
675   }
676   close(from);
677   close(to);
678 }
679
680 static void put(void) { shovel(STDIN_FILENO, make_sock('>')); }
681 static void get(void) { shovel(make_sock('<'), STDOUT_FILENO); }
682
683 /*----- Flooding and sinking ----------------------------------------------*/
684
685 /* --- Connection jobs --- */
686
687 typedef struct conninfo {
688   struct conninfo *next;
689   conn c;
690 } conninfo;
691
692 #define MAXCONN 32
693 static conninfo conns[MAXCONN], *freeconns;
694
695 static void (*connhook)(int, conninfo *);
696
697 static void connrecycle(conninfo *c) { c->next = freeconns; freeconns = c; }
698
699 static void connerr(conninfo *c)
700 {
701   if (errno == EAGAIN) connrecycle(c);
702   else die(EXIT_FAILURE, "connect: %s", strerror(errno));
703 }
704
705 static void connected(int fd, void *p)
706   { if (fd == -1) connerr(p); else connhook(fd, p); }
707
708 static void conndoconnect(conninfo *c)
709 {
710   int fd;
711   struct sockaddr_un sun;
712   size_t sz;
713
714   if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
715     die(EXIT_FAILURE, "socket: %s", strerror(errno));
716   socketaddr(&sun, &sz);
717   if (conn_init(&c->c, &sel, fd, (struct sockaddr *)&sun, sz, connected, c))
718     connerr(c);
719 }
720
721 static int timerflag;
722
723 static void conndrip(struct timeval *tv, void *p)
724 {
725   conninfo *c, *cc;
726
727   timerflag = 0;
728   while (freeconns) {
729     c = freeconns; freeconns = 0;
730     while (c) { cc = c->next; conndoconnect(c); c = cc; }
731   }
732 }
733
734 static void connloop(void (*connected)(int, conninfo *))
735 {
736   int i;
737   conninfo **cc;
738   sel_timer t;
739   struct timeval tv;
740
741   connhook = connected;
742   for (i = 0, cc = &freeconns; i < MAXCONN; i++) {
743     *cc = &conns[i];
744     cc = &conns[i].next;
745   }
746   *cc = 0;
747
748   for (;;) {
749     if (freeconns && !timerflag) {
750       gettimeofday(&tv, 0);
751       TV_ADDL(&tv, &tv, 0, 10000);
752       sel_addtimer(&sel, &t, &tv, conndrip, 0);
753       timerflag = 1;
754     }
755     if (sel_select(&sel))
756       die(EXIT_FAILURE, "select: %s", strerror(errno));
757   }
758 }
759
760 /* --- Sinking (glug glug) --- */
761
762 static void sink_close(gobbler *g, int err, void *p)
763 {
764   static char baton[4] = "/-\\|";
765   static int pos = 0;
766   static int count = 0;
767   static int state = '?';
768
769   conninfo *c = p;
770
771   if (!err) {
772     if (state == '?')
773       state = isatty(STDOUT_FILENO) ? 'y' : 'n';
774     if (state == 'y') {
775       if (count) count--;
776       else {
777         putchar(baton[pos]);
778         putchar('\b');
779         fflush(stdout);
780         pos++;
781         if (pos >= sizeof(baton)) pos = 0;
782         count = 128;
783       }
784     }
785   }
786   connrecycle(c);
787 }
788
789 static void sink_connected(int fd, conninfo *c)
790 {
791   char dir = '<';
792
793   if (write(fd, &dir, 1) != 1) {
794     moan("write: %s (continuing)", strerror(errno));
795     close(fd);
796     connrecycle(c);
797     return;
798   }
799   make_gobbler(fd, sink_close, c);
800 }
801
802 static void sink(void) { connloop(sink_connected); }
803
804 /* --- Flooding --- */
805
806 static void flood_close(dribbler *d, int err, void *p)
807 {
808   conninfo *c = p;
809
810   if (err) moan("write: %s (continuing)\n", strerror(errno));
811   destroy_dribbler(d);
812   connrecycle(c);
813 }
814
815 static void flood_connected(int fd, conninfo *c)
816 {
817   static uint32 seq;
818
819   dribbler *d;
820   pkq_node *pn;
821   int i;
822
823 #define FLOOD_PKSZ 1024
824
825   pn = make_pkqnode(0, 1 + FLOOD_PKSZ);
826   pn->buf[0] = '>';
827   STORE32(pn->buf + 1, seq);
828   for (i = 4; i < FLOOD_PKSZ; i++)
829     pn->buf[i + 1] = i & 0xff;
830   seq++;
831   d = make_dribbler(fd, flood_close, c);
832   enqueue_dribble(d, pn);
833 }
834
835 static void flood(void) { connloop(flood_connected); }
836
837 /*----- Main code ---------------------------------------------------------*/
838
839 static void usage(FILE *fp) { pquis(fp, "Usage: $ [-fgps] SOCKET\n"); }
840
841 static void version(void)
842   { pquis(stdout, "$ (" PACKAGE " version " VERSION")\n"); }
843
844 static void help(void)
845 {
846   version();
847   fputc('\n', stdout);
848   usage(stdout);
849   puts("\n\
850 With no options, provides a SLIP interface for TrIPE.\n\
851 \n\
852 Options:\n\
853   -f, --flood   Send packets to TrIPE as fast as possible.\n\
854   -g, --get     Receive packet from TrIPE and write to stdout.\n\
855   -p, --put     Send packet on stdin to TrIPE.\n\
856   -s, --sink    Slurp packets out of TrIPE and display progress.");
857 }
858
859 int main(int argc, char *argv[])
860 {
861   int mode = 'd';
862   int i;
863
864   ego(argv[0]);
865   for (;;) {
866     const struct option opt[] = {
867       { "help",                 0,              0,              'h' },
868       { "version",              0,              0,              'v' },
869       { "put",                  0,              0,              'p' },
870       { "get",                  0,              0,              'g' },
871       { "flood",                0,              0,              'f' },
872       { "sink",                 0,              0,              's' },
873       { 0,                      0,              0,              0 }
874     };
875     i = mdwopt(argc, argv, "hvpgfs", opt, 0, 0, 0);
876     if (i < 0)
877       break;
878     switch (i) {
879       case 'h': help(); return (0);
880       case 'v': version(); return (0);
881       case 'p': case 'g': case 's': case 'f': mode = i; break;
882       default: usage(stderr); exit(EXIT_FAILURE); break;
883     }
884   }
885   if (argc - optind != 1) { usage(stderr); exit(EXIT_FAILURE); }
886   name = argv[optind];
887   signal(SIGPIPE, SIG_IGN);
888   switch (mode) {
889     case 'd': slipif(); break;
890     case 'p': put(); break;
891     case 'g': get(); break;
892     case 'f': flood(); break;
893     case 's': sink(); break;
894   }
895   return (0);
896 }
897
898 /*----- That's all, folks -------------------------------------------------*/