3 * A simple SLIP implementation drivable from the command-line
5 * (c) 2008 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 ------------------------------------------------------*/
39 #include <sys/types.h>
43 #include <sys/socket.h>
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
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>
66 /*----- Data structures ---------------------------------------------------*/
68 typedef struct pkq_node
{
69 struct pkq_node
*next
;
75 pkq_node
*head
, **tail
;
78 enum { START
, OK
, ESC
, BAD
, SYNC1
, SYNC2
};
80 typedef struct client
{
86 typedef struct gobbler
{
88 void (*done
)(struct gobbler
*, int, void *);
92 typedef struct dribbler
{
95 void (*done
)(struct dribbler
*, int, void *);
99 typedef struct waiter
{
105 /*----- Static variables --------------------------------------------------*/
108 static dribbler
*dribble_out
;
109 static dstr slipbuf
= DSTR_INIT
;
110 static int slipstate
= SYNC1
;
111 static sel_file slip_in
, listener
;
112 static sel_state sel
;
113 static unsigned reasons
;
114 static waiter
*wait_head
, **wait_tail
= &wait_head
;
115 static unsigned char buf
[16384];
116 static const char *name
;
118 /*----- Utilities ---------------------------------------------------------*/
120 static void socketaddr(struct sockaddr_un
*sun
, size_t *sz
)
122 size_t n
= strlen(name
) + 1;
123 if (n
+ offsetof(struct sockaddr_un
, sun_path
) > sizeof(*sun
))
124 die(EXIT_FAILURE
, "name too long: `%s'", name
);
125 sun
->sun_family
= AF_UNIX
;
126 memcpy(sun
->sun_path
, name
, n
);
128 *sz
= n
+ offsetof(struct sockaddr_un
, sun_path
);
131 /*------ Packet queue -----------------------------------------------------*
133 * A packet queue contains a sequence of octet strings. Packets can be added
134 * to the end and read off the front.
137 static void initqueue(pkq
*q
) { q
->head
= 0; q
->tail
= &q
->head
; }
139 static pkq_node
*make_pkqnode(const void *p
, size_t n
)
144 pn
= CREATE(pkq_node
);
146 pn
->buf
= xmalloc(n
);
147 if (p
) memcpy(pn
->buf
, p
, n
);
153 static int enqueue(pkq
*q
, pkq_node
*pn
)
165 static void destroy_pkqnode(pkq_node
*pn
) { xfree(pn
->buf
); DESTROY(pn
); }
167 static int dequeue(pkq
*q
, int freep
)
169 pkq_node
*pn
= q
->head
;
181 static void destroy_pkq(pkq
*q
)
185 for (pn
= q
->head
; pn
; pn
= pnn
) {
189 q
->head
= 0; q
->tail
= &q
->head
;
192 /*----- Gobblers ----------------------------------------------------------*
194 * A gobbler just eats everything it sees on its input descriptor.
195 * Eventually, when it sees end-of-file, it closes the input descriptor,
196 * calls a user-supplied calback function, and quits.
199 static void close_gobbler(gobbler
*g
)
200 { if (g
->f
.fd
!= -1) { sel_rmfile(&g
->f
); close(g
->f
.fd
); g
->f
.fd
= -1; } }
202 static void destroy_gobbler(gobbler
*g
) { close_gobbler(g
); DESTROY(g
); }
204 static void do_gobble_in(int fd
, unsigned mode
, void *p
)
210 n
= read(fd
, buf
, sizeof(buf
));
212 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EINTR
)
215 moan("read (gobble): %s", strerror(errno
));
216 if (g
->done
) g
->done(g
, errno
, g
->p
);
221 if (g
->done
) g
->done(g
, 0, g
->p
);
228 static gobbler
*make_gobbler(int fd
,
229 void (*done
)(gobbler
*, int, void *),
237 sel_initfile(&sel
, &g
->f
, fd
, SEL_READ
, do_gobble_in
, g
);
239 do_gobble_in(fd
, SEL_READ
, g
);
243 /*----- Dribbler ----------------------------------------------------------*
245 * A dribbler hands out data from a packet queue to a file descriptor. It
246 * makes no attempt to preserve the record boundaries inherent in the packet
247 * queue structure. If the dribbler reaches the end of its queue, it invokes
248 * a user-supplied `done' function and stops selecting its output descriptor
252 static void cripple_dribbler(dribbler
*d
) { close(d
->f
.fd
); d
->f
.fd
= -1; }
254 static void destroy_dribbler(dribbler
*d
)
255 { cripple_dribbler(d
); DESTROY(d
); }
257 static void dribble_done(dribbler
*d
, int err
)
263 d
->done(d
, err
, d
->p
);
266 static void do_dribble_out(int fd
, unsigned mode
, void *p
)
274 n
= write(fd
, pn
->buf
+ pn
->n
, pn
->sz
- pn
->n
);
276 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EINTR
)
279 dribble_done(d
, errno
);
284 if (pn
->n
== pn
->sz
&& dequeue(&d
->q
, 1)) {
292 static int enqueue_dribble(dribbler
*d
, pkq_node
*pn
)
298 if (enqueue(&d
->q
, pn
)) {
300 do_dribble_out(d
->f
.fd
, SEL_WRITE
, d
);
306 static dribbler
*make_dribbler(int fd
,
307 void (*done
)(dribbler
*, int, void *),
310 dribbler
*d
= CREATE(dribbler
);
311 sel_initfile(&sel
, &d
->f
, fd
, SEL_WRITE
, do_dribble_out
, d
);
318 /*----- Clients -----------------------------------------------------------*/
320 static void done_client_dribble(dribbler
*d
, int err
, void *p
)
323 moan("write (client): %s", strerror(err
));
329 static void dequeue_to_waiter(void)
335 while (q_in
.head
&& wait_head
) {
339 wait_tail
= &wait_head
;
340 d
= make_dribbler(w
->fd
, done_client_dribble
, w
->g
);
343 if (dequeue(&q_in
, 0))
345 enqueue_dribble(d
, pn
);
349 static void destroy_client(client
*c
)
358 static void do_client_in(int fd
, unsigned mode
, void *p
)
364 /* --- Attention --- *
366 * The queue for outbound packets is SLIP-encoded; we need to encode it
367 * here. The queue for inbound packets is raw.
371 n
= read(fd
, buf
, sizeof(buf
));
374 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EINTR
)
377 moan("read (client): %s", strerror(errno
));
382 if (c
->mode
== '>') {
383 DPUTC(&c
->d
, SL_END
);
384 if (enqueue_dribble(dribble_out
, make_pkqnode(c
->d
.buf
, c
->d
.len
)))
390 if (c
->mode
== '?') {
398 w
->g
= make_gobbler(fd
, 0, 0);
402 wait_tail
= &w
->next
;
408 moan("bad client mode `%c'", buf
[0]);
413 for (i
= i0
; i
< n
; i
++) {
416 DPUTC(&c
->d
, SL_ESC
);
417 DPUTC(&c
->d
, SL_ESCESC
);
420 DPUTC(&c
->d
, SL_ESC
);
421 DPUTC(&c
->d
, SL_ESCEND
);
424 DPUTC(&c
->d
, buf
[i
]);
431 static void do_accept(int fd
, unsigned mode
, void *hunoz
)
434 struct sockaddr_un sun
;
435 socklen_t n
= sizeof(sun
);
438 if ((nfd
= accept(fd
, (struct sockaddr
*)&sun
, &n
)) < 0) {
439 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EINTR
)
442 die(EXIT_FAILURE
, "accept: %s", strerror(errno
));
447 fdflags(nfd
, O_NONBLOCK
, O_NONBLOCK
, 0, 0);
448 sel_initfile(&sel
, &c
->f
, nfd
, SEL_READ
, do_client_in
, c
);
453 /*----- Main daemon -------------------------------------------------------*/
455 static void done_slip_dribble(dribbler
*d
, int err
, void *hunoz
)
459 else if (err
!= EPIPE
)
460 die(EXIT_FAILURE
, "write (slip): %s", strerror(errno
));
465 static void close_slip(int fd
)
468 case SYNC1
: case SYNC2
: case START
: case BAD
: break;
469 default: moan("eof found while processing packet (discarding)"); break;
471 close(fd
); sel_rmfile(&slip_in
);
475 static void do_slip_in(int fd
, unsigned mode
, void *hunoz
)
479 /* --- Attention --- *
481 * The queue for inbound packets contains raw data; we need to decode it
482 * here. The queue for outbound packets is SLIP-encoded.
484 * TrIPE sends two empty packets on start-up, in order to resynchronize the
485 * target. We don't need this and it messes us up.
489 n
= read(fd
, buf
, sizeof(buf
));
494 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EINTR
)
496 die(EXIT_FAILURE
, "read (slip in): %s", strerror(errno
));
498 for (i
= 0; i
< n
; i
++) {
531 DPUTC(&slipbuf
, SL_END
);
535 DPUTC(&slipbuf
, SL_ESC
);
539 moan("found escaped end byte (discard packet and resync");
544 moan("unexpected escape char 0x%02x", buf
[i
]);
557 if (enqueue(&q_in
, make_pkqnode(slipbuf
.buf
, slipbuf
.len
)))
564 DPUTC(&slipbuf
, buf
[i
]);
574 static void slip_term(int n
, void *fdp
)
575 { close_slip(*(int *)fdp
); }
577 static void slipif(void)
581 struct sockaddr_un sun
;
585 /* --- Make the socket --- */
587 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
588 die(EXIT_FAILURE
, "socket: %s", strerror(errno
));
589 socketaddr(&sun
, &sz
);
590 if (bind(fd
, (struct sockaddr
*)&sun
, sz
))
591 die(EXIT_FAILURE
, "bind: %s", strerror(errno
));
593 die(EXIT_FAILURE
, "listen: %s", strerror(errno
));
595 /* --- Set up listeners for things --- */
597 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, 0, 0);
598 sel_initfile(&sel
, &listener
, fd
, SEL_READ
, do_accept
, 0);
599 sel_addfile(&listener
);
601 fdflags(STDIN_FILENO
, O_NONBLOCK
, O_NONBLOCK
, 0, 0);
602 fdflags(STDOUT_FILENO
, O_NONBLOCK
, O_NONBLOCK
, 0, 0);
603 sel_initfile(&sel
, &slip_in
, STDIN_FILENO
, SEL_READ
, do_slip_in
, 0);
604 dribble_out
= make_dribbler(STDOUT_FILENO
, done_slip_dribble
, 0);
605 sel_addfile(&slip_in
);
608 sig_add(&term
, SIGTERM
, slip_term
, &fd
);
609 sig_add(&term
, SIGINT
, slip_term
, &fd
);
614 /* --- Write the interface name --- */
616 dstr_putf(&d
, "%s-%s\n", QUIS
, name
);
617 if (enqueue_dribble(dribble_out
, make_pkqnode(d
.buf
, d
.len
)))
621 /* --- Main loop --- */
624 if (sel_select(&sel
))
625 die(EXIT_FAILURE
, "select: %s", strerror(errno
));
633 /*----- Putting and getting -----------------------------------------------*/
635 static int make_sock(int mode
)
637 struct sockaddr_un sun
;
642 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
643 die(EXIT_FAILURE
, "socket: %s", strerror(errno
));
644 socketaddr(&sun
, &sz
);
645 if (connect(fd
, (struct sockaddr
*)&sun
, sz
))
646 die(EXIT_FAILURE
, "connect: %s", strerror(errno
));
648 if (write(fd
, &ch
, 1) < 0)
649 die(EXIT_FAILURE
, "write (mode): %s", strerror(errno
));
653 static void shovel(int from
, int to
)
660 n
= read(from
, buf
, sizeof(buf
));
665 die(EXIT_FAILURE
, "read (shovel): %s", strerror(errno
));
672 n
= write(to
, p
, sz
);
677 die(EXIT_FAILURE
, "write (shovel): %s", strerror(errno
));
687 static void put(void) { shovel(STDIN_FILENO
, make_sock('>')); }
688 static void get(void) { shovel(make_sock('<'), STDOUT_FILENO
); }
690 /*----- Flooding and sinking ----------------------------------------------*/
692 /* --- Connection jobs --- */
694 typedef struct conninfo
{
695 struct conninfo
*next
;
700 static conninfo conns
[MAXCONN
], *freeconns
;
702 static void (*connhook
)(int, conninfo
*);
704 static void connrecycle(conninfo
*c
) { c
->next
= freeconns
; freeconns
= c
; }
706 static void connerr(conninfo
*c
)
708 if (errno
== EAGAIN
) connrecycle(c
);
709 else die(EXIT_FAILURE
, "connect: %s", strerror(errno
));
712 static void connected(int fd
, void *p
)
713 { if (fd
== -1) connerr(p
); else connhook(fd
, p
); }
715 static void conndoconnect(conninfo
*c
)
718 struct sockaddr_un sun
;
721 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
722 die(EXIT_FAILURE
, "socket: %s", strerror(errno
));
723 socketaddr(&sun
, &sz
);
724 if (conn_init(&c
->c
, &sel
, fd
, (struct sockaddr
*)&sun
, sz
, connected
, c
))
728 static int timerflag
;
730 static void conndrip(struct timeval
*tv
, void *hunoz
)
736 c
= freeconns
; freeconns
= 0;
737 while (c
) { cc
= c
->next
; conndoconnect(c
); c
= cc
; }
741 static void connloop(void (*connected
)(int, conninfo
*))
748 connhook
= connected
;
749 for (i
= 0, cc
= &freeconns
; i
< MAXCONN
; i
++) {
756 if (freeconns
&& !timerflag
) {
757 gettimeofday(&tv
, 0);
758 TV_ADDL(&tv
, &tv
, 0, 10000);
759 sel_addtimer(&sel
, &t
, &tv
, conndrip
, 0);
762 if (sel_select(&sel
))
763 die(EXIT_FAILURE
, "select: %s", strerror(errno
));
767 /* --- Sinking (glug glug) --- */
769 static void close_sink(gobbler
*g
, int err
, void *p
)
771 static char baton
[4] = "/-\\|";
773 static int count
= 0;
774 static int state
= '?';
780 state
= isatty(STDOUT_FILENO
) ?
'y' : 'n';
788 if (pos
>= sizeof(baton
)) pos
= 0;
796 static void sink_connected(int fd
, conninfo
*c
)
800 if (write(fd
, &dir
, 1) != 1) {
801 moan("write: %s (continuing)", strerror(errno
));
806 make_gobbler(fd
, close_sink
, c
);
809 static void sink(void) { connloop(sink_connected
); }
811 /* --- Flooding --- */
813 static void close_flood(dribbler
*d
, int err
, void *p
)
817 if (err
) moan("write: %s (continuing)\n", strerror(errno
));
822 static void flood_connected(int fd
, conninfo
*c
)
830 #define FLOOD_PKSZ 1024
832 pn
= make_pkqnode(0, 1 + FLOOD_PKSZ
);
834 STORE32(pn
->buf
+ 1, seq
);
835 for (i
= 4; i
< FLOOD_PKSZ
; i
++)
836 pn
->buf
[i
+ 1] = i
& 0xff;
838 d
= make_dribbler(fd
, close_flood
, c
);
839 enqueue_dribble(d
, pn
);
842 static void flood(void) { connloop(flood_connected
); }
844 /*----- Main code ---------------------------------------------------------*/
846 static void usage(FILE *fp
) { pquis(fp
, "Usage: $ [-fgps] SOCKET\n"); }
848 static void version(void)
849 { pquis(stdout
, "$ (" PACKAGE
" version " VERSION
")\n"); }
851 static void help(void)
857 With no options, provides a SLIP interface for TrIPE.\n\
860 -f, --flood Send packets to TrIPE as fast as possible.\n\
861 -g, --get Receive packet from TrIPE and write to stdout.\n\
862 -p, --put Send packet on stdin to TrIPE.\n\
863 -s, --sink Slurp packets out of TrIPE and display progress.");
866 int main(int argc
, char *argv
[])
873 const struct option opt
[] = {
874 { "help", 0, 0, 'h' },
875 { "version", 0, 0, 'v' },
876 { "put", 0, 0, 'p' },
877 { "get", 0, 0, 'g' },
878 { "flood", 0, 0, 'f' },
879 { "sink", 0, 0, 's' },
882 i
= mdwopt(argc
, argv
, "hvpgfs", opt
, 0, 0, 0);
886 case 'h': help(); return (0);
887 case 'v': version(); return (0);
888 case 'p': case 'g': case 's': case 'f': mode
= i
; break;
889 default: usage(stderr
); exit(EXIT_FAILURE
); break;
892 if (argc
- optind
!= 1) { usage(stderr
); exit(EXIT_FAILURE
); }
894 signal(SIGPIPE
, SIG_IGN
);
896 case 'd': slipif(); break;
897 case 'p': put(); break;
898 case 'g': get(); break;
899 case 'f': flood(); break;
900 case 's': sink(); break;
905 /*----- That's all, folks -------------------------------------------------*/