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 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.
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
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
26 /*----- Header files ------------------------------------------------------*/
38 #include <sys/types.h>
42 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <arpa/inet.h>
48 #include <mLib/alloc.h>
49 #include <mLib/bits.h>
50 #include <mLib/conn.h>
51 #include <mLib/dstr.h>
52 #include <mLib/fdflags.h>
53 #include <mLib/mdwopt.h>
54 #include <mLib/quis.h>
55 #include <mLib/report.h>
65 /*----- Data structures ---------------------------------------------------*/
67 typedef struct pkq_node
{
68 struct pkq_node
*next
;
74 pkq_node
*head
, **tail
;
77 enum { START
, OK
, ESC
, BAD
, SYNC1
, SYNC2
};
79 typedef struct client
{
85 typedef struct gobbler
{
87 void (*done
)(struct gobbler
*, int, void *);
91 typedef struct dribbler
{
94 void (*done
)(struct dribbler
*, int, void *);
98 typedef struct waiter
{
104 /*----- Static variables --------------------------------------------------*/
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
;
117 /*----- Utilities ---------------------------------------------------------*/
119 static void socketaddr(struct sockaddr_un
*sun
, size_t *sz
)
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
);
127 *sz
= n
+ offsetof(struct sockaddr_un
, sun_path
);
130 /*------ Packet queue -----------------------------------------------------*
132 * A packet queue contains a sequence of octet strings. Packets can be added
133 * to the end and read off the front.
136 static void initqueue(pkq
*q
) { q
->head
= 0; q
->tail
= &q
->head
; }
138 static pkq_node
*make_pkqnode(const void *p
, size_t n
)
143 pn
= CREATE(pkq_node
);
145 pn
->buf
= xmalloc(n
);
146 if (p
) memcpy(pn
->buf
, p
, n
);
152 static int enqueue(pkq
*q
, pkq_node
*pn
)
164 static void destroy_pkqnode(pkq_node
*pn
) { xfree(pn
->buf
); DESTROY(pn
); }
166 static int dequeue(pkq
*q
, int freep
)
168 pkq_node
*pn
= q
->head
;
180 static void destroy_pkq(pkq
*q
)
184 for (pn
= q
->head
; pn
; pn
= pnn
) {
188 q
->head
= 0; q
->tail
= &q
->head
;
191 /*----- Gobblers ----------------------------------------------------------*
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.
198 static void close_gobbler(gobbler
*g
)
199 { if (g
->f
.fd
!= -1) { sel_rmfile(&g
->f
); close(g
->f
.fd
); g
->f
.fd
= -1; } }
201 static void destroy_gobbler(gobbler
*g
) { close_gobbler(g
); DESTROY(g
); }
203 static void do_gobble_in(int fd
, unsigned mode
, void *p
)
209 n
= read(fd
, buf
, sizeof(buf
));
211 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EINTR
)
214 moan("read (gobble): %s", strerror(errno
));
215 if (g
->done
) g
->done(g
, errno
, g
->p
);
220 if (g
->done
) g
->done(g
, 0, g
->p
);
227 static gobbler
*make_gobbler(int fd
,
228 void (*done
)(gobbler
*, int, void *),
236 sel_initfile(&sel
, &g
->f
, fd
, SEL_READ
, do_gobble_in
, g
);
238 do_gobble_in(fd
, SEL_READ
, g
);
242 /*----- Dribbler ----------------------------------------------------------*
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
251 static void cripple_dribbler(dribbler
*d
) { close(d
->f
.fd
); d
->f
.fd
= -1; }
253 static void destroy_dribbler(dribbler
*d
)
254 { cripple_dribbler(d
); DESTROY(d
); }
256 static void dribble_done(dribbler
*d
, int err
)
262 d
->done(d
, err
, d
->p
);
265 static void do_dribble_out(int fd
, unsigned mode
, void *p
)
273 n
= write(fd
, pn
->buf
+ pn
->n
, pn
->sz
- pn
->n
);
275 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EINTR
)
278 dribble_done(d
, errno
);
283 if (pn
->n
== pn
->sz
&& dequeue(&d
->q
, 1)) {
291 static int enqueue_dribble(dribbler
*d
, pkq_node
*pn
)
297 if (enqueue(&d
->q
, pn
)) {
299 do_dribble_out(d
->f
.fd
, SEL_WRITE
, d
);
305 static dribbler
*make_dribbler(int fd
,
306 void (*done
)(dribbler
*, int, void *),
309 dribbler
*d
= CREATE(dribbler
);
310 sel_initfile(&sel
, &d
->f
, fd
, SEL_WRITE
, do_dribble_out
, d
);
317 /*----- Clients -----------------------------------------------------------*/
319 static void done_client_dribble(dribbler
*d
, int err
, void *p
)
322 moan("write (client): %s", strerror(err
));
328 static void dequeue_to_waiter(void)
334 while (q_in
.head
&& wait_head
) {
338 wait_tail
= &wait_head
;
339 d
= make_dribbler(w
->fd
, done_client_dribble
, w
->g
);
342 if (dequeue(&q_in
, 0))
344 enqueue_dribble(d
, pn
);
348 static void destroy_client(client
*c
)
357 static void do_client_in(int fd
, unsigned mode
, void *p
)
363 /* --- Attention --- *
365 * The queue for outbound packets is SLIP-encoded; we need to encode it
366 * here. The queue for inbound packets is raw.
370 n
= read(fd
, buf
, sizeof(buf
));
373 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EINTR
)
376 moan("read (client): %s", strerror(errno
));
381 if (c
->mode
== '>') {
382 DPUTC(&c
->d
, SL_END
);
383 if (enqueue_dribble(dribble_out
, make_pkqnode(c
->d
.buf
, c
->d
.len
)))
389 if (c
->mode
== '?') {
397 w
->g
= make_gobbler(fd
, 0, 0);
401 wait_tail
= &w
->next
;
407 moan("bad client mode `%c'", buf
[0]);
412 for (i
= i0
; i
< n
; i
++) {
415 DPUTC(&c
->d
, SL_ESC
);
416 DPUTC(&c
->d
, SL_ESCESC
);
419 DPUTC(&c
->d
, SL_ESC
);
420 DPUTC(&c
->d
, SL_ESCEND
);
423 DPUTC(&c
->d
, buf
[i
]);
430 static void do_accept(int fd
, unsigned mode
, void *hunoz
)
433 struct sockaddr_un sun
;
434 socklen_t n
= sizeof(sun
);
437 if ((nfd
= accept(fd
, (struct sockaddr
*)&sun
, &n
)) < 0) {
438 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EINTR
)
441 die(EXIT_FAILURE
, "accept: %s", strerror(errno
));
446 fdflags(nfd
, O_NONBLOCK
, O_NONBLOCK
, 0, 0);
447 sel_initfile(&sel
, &c
->f
, nfd
, SEL_READ
, do_client_in
, c
);
452 /*----- Main daemon -------------------------------------------------------*/
454 static void done_slip_dribble(dribbler
*d
, int err
, void *hunoz
)
458 else if (err
!= EPIPE
)
459 die(EXIT_FAILURE
, "write (slip): %s", strerror(errno
));
464 static void close_slip(int fd
)
467 case SYNC1
: case SYNC2
: case START
: case BAD
: break;
468 default: moan("eof found while processing packet (discarding)"); break;
470 close(fd
); sel_rmfile(&slip_in
);
474 static void do_slip_in(int fd
, unsigned mode
, void *hunoz
)
478 /* --- Attention --- *
480 * The queue for inbound packets contains raw data; we need to decode it
481 * here. The queue for outbound packets is SLIP-encoded.
483 * TrIPE sends two empty packets on start-up, in order to resynchronize the
484 * target. We don't need this and it messes us up.
488 n
= read(fd
, buf
, sizeof(buf
));
493 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EINTR
)
495 die(EXIT_FAILURE
, "read (slip in): %s", strerror(errno
));
497 for (i
= 0; i
< n
; i
++) {
530 DPUTC(&slipbuf
, SL_END
);
534 DPUTC(&slipbuf
, SL_ESC
);
538 moan("found escaped end byte (discard packet and resync");
543 moan("unexpected escape char 0x%02x", buf
[i
]);
556 if (enqueue(&q_in
, make_pkqnode(slipbuf
.buf
, slipbuf
.len
)))
563 DPUTC(&slipbuf
, buf
[i
]);
573 static void slip_term(int n
, void *fdp
)
574 { close_slip(*(int *)fdp
); }
576 static void slipif(void)
580 struct sockaddr_un sun
;
584 /* --- Make the socket --- */
586 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
587 die(EXIT_FAILURE
, "socket: %s", strerror(errno
));
588 socketaddr(&sun
, &sz
);
589 if (bind(fd
, (struct sockaddr
*)&sun
, sz
))
590 die(EXIT_FAILURE
, "bind: %s", strerror(errno
));
592 die(EXIT_FAILURE
, "listen: %s", strerror(errno
));
594 /* --- Set up listeners for things --- */
596 fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, 0, 0);
597 sel_initfile(&sel
, &listener
, fd
, SEL_READ
, do_accept
, 0);
598 sel_addfile(&listener
);
600 fdflags(STDIN_FILENO
, O_NONBLOCK
, O_NONBLOCK
, 0, 0);
601 fdflags(STDOUT_FILENO
, O_NONBLOCK
, O_NONBLOCK
, 0, 0);
602 sel_initfile(&sel
, &slip_in
, STDIN_FILENO
, SEL_READ
, do_slip_in
, 0);
603 dribble_out
= make_dribbler(STDOUT_FILENO
, done_slip_dribble
, 0);
604 sel_addfile(&slip_in
);
607 sig_add(&term
, SIGTERM
, slip_term
, &fd
);
608 sig_add(&term
, SIGINT
, slip_term
, &fd
);
613 /* --- Write the interface name --- */
615 dstr_putf(&d
, "%s-%s\n", QUIS
, name
);
616 if (enqueue_dribble(dribble_out
, make_pkqnode(d
.buf
, d
.len
)))
620 /* --- Main loop --- */
623 if (sel_select(&sel
))
624 die(EXIT_FAILURE
, "select: %s", strerror(errno
));
632 /*----- Putting and getting -----------------------------------------------*/
634 static int make_sock(int mode
)
636 struct sockaddr_un sun
;
641 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
642 die(EXIT_FAILURE
, "socket: %s", strerror(errno
));
643 socketaddr(&sun
, &sz
);
644 if (connect(fd
, (struct sockaddr
*)&sun
, sz
))
645 die(EXIT_FAILURE
, "connect: %s", strerror(errno
));
647 if (write(fd
, &ch
, 1) < 0)
648 die(EXIT_FAILURE
, "write (mode): %s", strerror(errno
));
652 static void shovel(int from
, int to
)
659 n
= read(from
, buf
, sizeof(buf
));
664 die(EXIT_FAILURE
, "read (shovel): %s", strerror(errno
));
671 n
= write(to
, p
, sz
);
676 die(EXIT_FAILURE
, "write (shovel): %s", strerror(errno
));
686 static void put(void) { shovel(STDIN_FILENO
, make_sock('>')); }
687 static void get(void) { shovel(make_sock('<'), STDOUT_FILENO
); }
689 /*----- Flooding and sinking ----------------------------------------------*/
691 /* --- Connection jobs --- */
693 typedef struct conninfo
{
694 struct conninfo
*next
;
699 static conninfo conns
[MAXCONN
], *freeconns
;
701 static void (*connhook
)(int, conninfo
*);
703 static void connrecycle(conninfo
*c
) { c
->next
= freeconns
; freeconns
= c
; }
705 static void connerr(conninfo
*c
)
707 if (errno
== EAGAIN
) connrecycle(c
);
708 else die(EXIT_FAILURE
, "connect: %s", strerror(errno
));
711 static void connected(int fd
, void *p
)
712 { if (fd
== -1) connerr(p
); else connhook(fd
, p
); }
714 static void conndoconnect(conninfo
*c
)
717 struct sockaddr_un sun
;
720 if ((fd
= socket(PF_UNIX
, SOCK_STREAM
, 0)) < 0)
721 die(EXIT_FAILURE
, "socket: %s", strerror(errno
));
722 socketaddr(&sun
, &sz
);
723 if (conn_init(&c
->c
, &sel
, fd
, (struct sockaddr
*)&sun
, sz
, connected
, c
))
727 static int timerflag
;
729 static void conndrip(struct timeval
*tv
, void *hunoz
)
735 c
= freeconns
; freeconns
= 0;
736 while (c
) { cc
= c
->next
; conndoconnect(c
); c
= cc
; }
740 static void connloop(void (*connected
)(int, conninfo
*))
747 connhook
= connected
;
748 for (i
= 0, cc
= &freeconns
; i
< MAXCONN
; i
++) {
755 if (freeconns
&& !timerflag
) {
756 gettimeofday(&tv
, 0);
757 TV_ADDL(&tv
, &tv
, 0, 10000);
758 sel_addtimer(&sel
, &t
, &tv
, conndrip
, 0);
761 if (sel_select(&sel
))
762 die(EXIT_FAILURE
, "select: %s", strerror(errno
));
766 /* --- Sinking (glug glug) --- */
768 static void close_sink(gobbler
*g
, int err
, void *p
)
770 static char baton
[4] = "/-\\|";
772 static int count
= 0;
773 static int state
= '?';
779 state
= isatty(STDOUT_FILENO
) ?
'y' : 'n';
787 if (pos
>= sizeof(baton
)) pos
= 0;
795 static void sink_connected(int fd
, conninfo
*c
)
799 if (write(fd
, &dir
, 1) != 1) {
800 moan("write: %s (continuing)", strerror(errno
));
805 make_gobbler(fd
, close_sink
, c
);
808 static void sink(void) { connloop(sink_connected
); }
810 /* --- Flooding --- */
812 static void close_flood(dribbler
*d
, int err
, void *p
)
816 if (err
) moan("write: %s (continuing)\n", strerror(errno
));
821 static void flood_connected(int fd
, conninfo
*c
)
829 #define FLOOD_PKSZ 1024
831 pn
= make_pkqnode(0, 1 + FLOOD_PKSZ
);
833 STORE32(pn
->buf
+ 1, seq
);
834 for (i
= 4; i
< FLOOD_PKSZ
; i
++)
835 pn
->buf
[i
+ 1] = i
& 0xff;
837 d
= make_dribbler(fd
, close_flood
, c
);
838 enqueue_dribble(d
, pn
);
841 static void flood(void) { connloop(flood_connected
); }
843 /*----- Main code ---------------------------------------------------------*/
845 static void usage(FILE *fp
) { pquis(fp
, "Usage: $ [-fgps] SOCKET\n"); }
847 static void version(void)
848 { pquis(stdout
, "$ (" PACKAGE
" version " VERSION
")\n"); }
850 static void help(void)
856 With no options, provides a SLIP interface for TrIPE.\n\
859 -f, --flood Send packets to TrIPE as fast as possible.\n\
860 -g, --get Receive packet from TrIPE and write to stdout.\n\
861 -p, --put Send packet on stdin to TrIPE.\n\
862 -s, --sink Slurp packets out of TrIPE and display progress.");
865 int main(int argc
, char *argv
[])
872 const struct option opt
[] = {
873 { "help", 0, 0, 'h' },
874 { "version", 0, 0, 'v' },
875 { "put", 0, 0, 'p' },
876 { "get", 0, 0, 'g' },
877 { "flood", 0, 0, 'f' },
878 { "sink", 0, 0, 's' },
881 i
= mdwopt(argc
, argv
, "hvpgfs", opt
, 0, 0, 0);
885 case 'h': help(); return (0);
886 case 'v': version(); return (0);
887 case 'p': case 'g': case 's': case 'f': mode
= i
; break;
888 default: usage(stderr
); exit(EXIT_FAILURE
); break;
891 if (argc
- optind
!= 1) { usage(stderr
); exit(EXIT_FAILURE
); }
893 signal(SIGPIPE
, SIG_IGN
);
895 case 'd': slipif(); break;
896 case 'p': put(); break;
897 case 'g': get(); break;
898 case 'f': flood(); break;
899 case 's': sink(); break;
904 /*----- That's all, folks -------------------------------------------------*/