3 * Forwarding UDP packets over a stream
5 * (c) 2003 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 ------------------------------------------------------*/
37 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
46 #include <mLib/alloc.h>
47 #include <mLib/bits.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>
54 #include <mLib/selpk.h>
58 /*----- Data structures ---------------------------------------------------*/
61 struct pk *next; /* Next packet in the chain */
62 octet *p, *o; /* Buffer start and current posn */
63 size_t n; /* Size of packet remaining */
66 typedef struct pkstream {
67 unsigned f; /* Flags... */
68 #define PKF_FULL 1u /* Buffer is full: stop reading */
69 sel_file r, w; /* Read and write selectors */
70 pk *pks, **pk_tail; /* Packet queue */
71 size_t npk, szpk; /* Number and size of data */
72 selpk p; /* Packet parser */
75 typedef struct connwait {
76 sel_file a; /* Selector */
77 struct sockaddr_in me; /* Who I'm meant to be */
78 struct in_addr peer; /* Who my peer is */
81 /*----- Static variables --------------------------------------------------*/
86 static size_t pk_nmax = 128, pk_szmax = 1024 * 1024;
88 /*----- Main code ---------------------------------------------------------*/
90 static int nonblockify(int fd)
91 { return (fdflags(fd, O_NONBLOCK, O_NONBLOCK, 0, 0)); }
93 static int cloexec(int fd)
94 { return (fdflags(fd, 0, 0, FD_CLOEXEC, FD_CLOEXEC)); }
96 static void dolisten(void);
98 static void doclose(pkstream *p)
102 close(p->p.reader.fd);
103 selpk_destroy(&p->p);
104 if (!(p->f & PKF_FULL))
108 for (pk = p->pks; pk; pk = ppk) {
114 if (cw.me.sin_port != 0)
120 static void rdtcp(octet *b, size_t sz, pkbuf *pk, size_t *k, void *vp)
130 if (pksz + 2 == sz) {
131 DISCARD(write(fd_udp, b + 2, pksz));
132 selpk_want(&p->p, 2);
134 selpk_want(&p->p, pksz + 2);
139 static void wrtcp(int fd, unsigned mode, void *vp)
142 struct iovec iov[NPK];
148 for (i = 0, pk = p->pks; i < NPK && pk; i++, pk = pk->next) {
149 iov[i].iov_base = pk->o;
150 iov[i].iov_len = pk->n;
153 if ((n = writev(fd, iov, i)) < 0) {
154 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
156 moan("couldn't write to TCP socket: %s", strerror(errno));
162 for (pk = p->pks; n && pk; pk = ppk) {
177 p->pk_tail = &p->pks;
180 if ((p->f & PKF_FULL) && p->npk < pk_nmax && p->szpk < pk_szmax) {
186 static void rdudp(int fd, unsigned mode, void *vp)
193 if ((n = read(fd, buf, sizeof(buf))) < 0) {
194 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
196 moan("couldn't read from UDP socket: %s", strerror(errno));
199 pk = xmalloc(sizeof(*pk));
201 pk->p = xmalloc(n + 2);
203 memcpy(pk->p + 2, buf, n);
207 p->pk_tail = &pk->next;
213 if (p->npk >= pk_nmax || p->szpk >= pk_szmax) {
219 static void dofwd(int fd_in, int fd_out)
221 pkstream *p = xmalloc(sizeof(*p));
222 sel_initfile(&sel, &p->r, fd_udp, SEL_READ, rdudp, p);
223 sel_initfile(&sel, &p->w, fd_out, SEL_WRITE, wrtcp, p);
224 selpk_init(&p->p, &sel, fd_in, rdtcp, p);
225 selpk_want(&p->p, 2);
227 p->pk_tail = &p->pks;
228 p->npk = p->szpk = 0;
233 static void doaccept(int fd_s, unsigned mode, void *p)
236 struct sockaddr_in sin;
237 socklen_t sz = sizeof(sin);
239 if ((fd = accept(fd_s, (struct sockaddr *)&sin, &sz)) < 0) {
240 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
242 moan("couldn't accept incoming connection: %s", strerror(errno));
245 if (cw.peer.s_addr != INADDR_ANY &&
246 cw.peer.s_addr != sin.sin_addr.s_addr) {
248 moan("rejecting connection from %s", inet_ntoa(sin.sin_addr));
251 if (nonblockify(fd) || cloexec(fd)) {
253 moan("couldn't accept incoming connection: %s", strerror(errno));
261 static void dolisten(void)
266 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0 ||
267 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) ||
268 bind(fd, (struct sockaddr *)&cw.me, sizeof(cw.me)) ||
269 listen(fd, 1) || nonblockify(fd) || cloexec(fd))
270 die(1, "couldn't set up listening socket: %s", strerror(errno));
271 sel_initfile(&sel, &cw.a, fd, SEL_READ, doaccept, 0);
275 static void parseaddr(const char *pp, struct in_addr *a, unsigned short *pt)
277 char *p = xstrdup(pp);
283 die(1, "missing port number in address `%s'", p);
290 if ((h = gethostbyname(p)) == 0)
291 die(1, "unknown host `%s'", p);
292 memcpy(a, h->h_addr, sizeof(*a));
299 if ((s = getservbyname(q, "tcp")) != 0)
301 else if ((n = strtoul(q, &qq, 0)) == 0 || *qq || n > 0xffff)
302 die(1, "bad port number `%s'", q);
308 static void usage(FILE *fp)
311 "Usage: $ [-l PORT] [-b ADDR] [-p ADDR] [-c ADDR:PORT]\n\
312 ADDR:PORT ADDR:PORT\n");
315 static void version(FILE *fp)
316 { pquis(fp, "$, tripe version " VERSION "\n"); }
318 static void help(FILE *fp)
326 -h, --help Display this help text.\n\
327 -v, --version Display version number.\n\
328 -u, --usage Display pointless usage message.\n\
330 -l, --listen=PORT Listen for connections to TCP PORT.\n\
331 -p, --peer=ADDR Only accept connections from IP ADDR.\n\
332 -b, --bind=ADDR Bind to ADDR before connecting.\n\
333 -c, --connect=ADDR:PORT Connect to IP ADDR, TCP PORT.\n\
335 Forwards UDP packets over a reliable stream. By default, uses stdin and\n\
336 stdout; though it can use TCP sockets instead.\n\
340 int main(int argc, char *argv[])
344 struct sockaddr_in connaddr, bindaddr;
345 struct sockaddr_in udp_me, udp_peer;
351 bindaddr.sin_family = AF_INET;
352 bindaddr.sin_addr.s_addr = INADDR_ANY;
353 bindaddr.sin_port = 0;
354 connaddr.sin_family = AF_INET;
355 connaddr.sin_addr.s_addr = INADDR_ANY;
356 cw.me.sin_family = AF_INET;
357 cw.me.sin_addr.s_addr = INADDR_ANY;
359 cw.peer.s_addr = INADDR_ANY;
362 static struct option opt[] = {
363 { "help", 0, 0, 'h' },
364 { "version", 0, 0, 'v' },
365 { "usage", 0, 0, 'u' },
366 { "listen", OPTF_ARGREQ, 0, 'l' },
367 { "peer", OPTF_ARGREQ, 0, 'p' },
368 { "bind", OPTF_ARGREQ, 0, 'b' },
369 { "connect", OPTF_ARGREQ, 0, 'c' },
374 i = mdwopt(argc, argv, "hvul:p:b:c:", opt, 0, 0, 0);
388 parseaddr(optarg, 0, &pt);
392 parseaddr(optarg, &cw.peer, 0);
395 parseaddr(optarg, &bindaddr.sin_addr, 0);
396 cw.me.sin_addr = bindaddr.sin_addr;
399 parseaddr(optarg, &connaddr.sin_addr, &pt);
400 connaddr.sin_port = pt;
407 if (optind + 2 != argc || (f & f_bogus)) {
412 udp_me.sin_family = udp_peer.sin_family = AF_INET;
413 parseaddr(argv[optind], &udp_me.sin_addr, &pt);
414 udp_me.sin_port = pt;
415 parseaddr(argv[optind + 1], &udp_peer.sin_addr, &pt);
416 udp_peer.sin_port = pt;
418 if ((fd_udp = socket(PF_INET, SOCK_DGRAM, 0)) < 0 ||
419 bind(fd_udp, (struct sockaddr *)&udp_me, sizeof(udp_me)) ||
420 connect(fd_udp, (struct sockaddr *)&udp_peer, sizeof(udp_peer)) ||
421 setsockopt(fd_udp, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)) ||
422 setsockopt(fd_udp, SOL_SOCKET, SO_SNDBUF, &len, sizeof(len)) ||
423 nonblockify(fd_udp) || cloexec(fd_udp))
424 die(1, "couldn't set up UDP socket: %s", strerror(errno));
426 if (cw.me.sin_port != 0)
428 else if (connaddr.sin_addr.s_addr != INADDR_ANY) {
430 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0 ||
431 bind(fd, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) ||
432 connect(fd, (struct sockaddr *)&connaddr, sizeof(connaddr)) ||
433 nonblockify(fd) || cloexec(fd))
434 die(1, "couldn't connect to TCP server: %s", strerror(errno));
437 dofwd(STDIN_FILENO, STDOUT_FILENO);
444 /*----- That's all, folks -------------------------------------------------*/