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
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 ------------------------------------------------------*/
38 #include <sys/types.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
47 #include <mLib/alloc.h>
48 #include <mLib/bits.h>
49 #include <mLib/dstr.h>
50 #include <mLib/fdflags.h>
51 #include <mLib/mdwopt.h>
52 #include <mLib/quis.h>
53 #include <mLib/report.h>
55 #include <mLib/selpk.h>
57 /*----- Data structures ---------------------------------------------------*/
60 struct pk *next; /* Next packet in the chain */
61 octet *p, *o; /* Buffer start and current posn */
62 size_t n; /* Size of packet remaining */
65 typedef struct pkstream {
66 unsigned f; /* Flags... */
67 #define PKF_FULL 1u /* Buffer is full: stop reading */
68 sel_file r, w; /* Read and write selectors */
69 pk *pks, **pk_tail; /* Packet queue */
70 size_t npk, szpk; /* Number and size of data */
71 selpk p; /* Packet parser */
74 typedef struct connwait {
75 sel_file a; /* Selector */
76 struct sockaddr_in me; /* Who I'm meant to be */
77 struct in_addr peer; /* Who my peer is */
80 /*----- Static variables --------------------------------------------------*/
85 static size_t pk_nmax = 128, pk_szmax = 1024 * 1024;
87 /*----- Main code ---------------------------------------------------------*/
89 static int nonblockify(int fd)
90 { return (fdflags(fd, O_NONBLOCK, O_NONBLOCK, 0, 0)); }
92 static int cloexec(int fd)
93 { return (fdflags(fd, 0, 0, FD_CLOEXEC, FD_CLOEXEC)); }
95 static void dolisten(void);
97 static void doclose(pkstream *p)
101 close(p->p.reader.fd);
102 selpk_destroy(&p->p);
103 if (!(p->f & PKF_FULL))
107 for (pk = p->pks; pk; pk = ppk) {
113 if (cw.me.sin_port != 0)
119 static void rdtcp(octet *b, size_t sz, pkbuf *pk, size_t *k, void *vp)
129 if (pksz + 2 == sz) {
130 write(fd_udp, b + 2, pksz);
131 selpk_want(&p->p, 2);
133 selpk_want(&p->p, pksz + 2);
138 static void wrtcp(int fd, unsigned mode, void *vp)
141 struct iovec iov[NPK];
147 for (i = 0, pk = p->pks; i < NPK && pk; i++, pk = pk->next) {
148 iov[i].iov_base = pk->o;
149 iov[i].iov_len = pk->n;
152 if ((n = writev(fd, iov, i)) < 0) {
153 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
155 moan("couldn't write to TCP socket: %s", strerror(errno));
161 for (pk = p->pks; n && pk; pk = ppk) {
176 p->pk_tail = &p->pks;
179 if ((p->f & PKF_FULL) && p->npk < pk_nmax && p->szpk < pk_szmax) {
185 static void rdudp(int fd, unsigned mode, void *vp)
192 if ((n = read(fd, buf, sizeof(buf))) < 0) {
193 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
195 moan("couldn't read from UDP socket: %s", strerror(errno));
198 pk = xmalloc(sizeof(*pk));
200 pk->p = xmalloc(n + 2);
202 memcpy(pk->p + 2, buf, n);
206 p->pk_tail = &pk->next;
212 if (p->npk >= pk_nmax || p->szpk >= pk_szmax) {
218 static void dofwd(int fd_in, int fd_out)
220 pkstream *p = xmalloc(sizeof(*p));
221 sel_initfile(&sel, &p->r, fd_udp, SEL_READ, rdudp, p);
222 sel_initfile(&sel, &p->w, fd_out, SEL_WRITE, wrtcp, p);
223 selpk_init(&p->p, &sel, fd_in, rdtcp, p);
224 selpk_want(&p->p, 2);
226 p->pk_tail = &p->pks;
227 p->npk = p->szpk = 0;
232 static void doaccept(int fd_s, unsigned mode, void *p)
235 struct sockaddr_in sin;
236 socklen_t sz = sizeof(sin);
238 if ((fd = accept(fd_s, (struct sockaddr *)&sin, &sz)) < 0) {
239 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
241 moan("couldn't accept incoming connection: %s", strerror(errno));
244 if (cw.peer.s_addr != INADDR_ANY &&
245 cw.peer.s_addr != sin.sin_addr.s_addr) {
247 moan("rejecting connection from %s", inet_ntoa(sin.sin_addr));
250 if (nonblockify(fd) || cloexec(fd)) {
252 moan("couldn't accept incoming connection: %s", strerror(errno));
260 static void dolisten(void)
265 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0 ||
266 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) ||
267 bind(fd, (struct sockaddr *)&cw.me, sizeof(cw.me)) ||
268 listen(fd, 1) || nonblockify(fd) || cloexec(fd))
269 die(1, "couldn't set up listening socket: %s", strerror(errno));
270 sel_initfile(&sel, &cw.a, fd, SEL_READ, doaccept, 0);
274 static void parseaddr(const char *pp, struct in_addr *a, unsigned short *pt)
276 char *p = xstrdup(pp);
282 die(1, "missing port number in address `%s'", p);
289 if ((h = gethostbyname(p)) == 0)
290 die(1, "unknown host `%s'", p);
291 memcpy(a, h->h_addr, sizeof(*a));
298 if ((s = getservbyname(q, "tcp")) != 0)
300 else if ((n = strtoul(q, &qq, 0)) == 0 || *qq || n > 0xffff)
301 die(1, "bad port number `%s'", q);
307 static void usage(FILE *fp)
310 "Usage: $ [-l PORT] [-b ADDR] [-p ADDR] [-c ADDR:PORT]\n\
311 ADDR:PORT ADDR:PORT\n");
314 static void version(FILE *fp)
315 { pquis(fp, "$, tripe version " VERSION "\n"); }
317 static void help(FILE *fp)
325 -h, --help Display this help text.\n\
326 -v, --version Display version number.\n\
327 -u, --usage Display pointless usage message.\n\
329 -l, --listen=PORT Listen for connections to TCP PORT.\n\
330 -p, --peer=ADDR Only accept connections from IP ADDR.\n\
331 -b, --bind=ADDR Bind to ADDR before connecting.\n\
332 -c, --connect=ADDR:PORT Connect to IP ADDR, TCP PORT.\n\
334 Forwards UDP packets over a reliable stream. By default, uses stdin and\n\
335 stdout; though it can use TCP sockets instead.\n\
339 int main(int argc, char *argv[])
343 struct sockaddr_in connaddr, bindaddr;
344 struct sockaddr_in udp_me, udp_peer;
350 bindaddr.sin_family = AF_INET;
351 bindaddr.sin_addr.s_addr = INADDR_ANY;
352 bindaddr.sin_port = 0;
353 connaddr.sin_family = AF_INET;
354 connaddr.sin_addr.s_addr = INADDR_ANY;
355 cw.me.sin_family = AF_INET;
356 cw.me.sin_addr.s_addr = INADDR_ANY;
358 cw.peer.s_addr = INADDR_ANY;
361 static struct option opt[] = {
362 { "help", 0, 0, 'h' },
363 { "version", 0, 0, 'v' },
364 { "usage", 0, 0, 'u' },
365 { "listen", OPTF_ARGREQ, 0, 'l' },
366 { "peer", OPTF_ARGREQ, 0, 'p' },
367 { "bind", OPTF_ARGREQ, 0, 'b' },
368 { "connect", OPTF_ARGREQ, 0, 'c' },
373 i = mdwopt(argc, argv, "hvul:p:b:c:", opt, 0, 0, 0);
387 parseaddr(optarg, 0, &pt);
391 parseaddr(optarg, &cw.peer, 0);
394 parseaddr(optarg, &bindaddr.sin_addr, 0);
397 parseaddr(optarg, &connaddr.sin_addr, &pt);
398 connaddr.sin_port = pt;
405 if (optind + 2 != argc || (f & f_bogus)) {
410 udp_me.sin_family = udp_peer.sin_family = AF_INET;
411 parseaddr(argv[optind], &udp_me.sin_addr, &pt);
412 udp_me.sin_port = pt;
413 parseaddr(argv[optind + 1], &udp_peer.sin_addr, &pt);
414 udp_peer.sin_port = pt;
416 if ((fd_udp = socket(PF_INET, SOCK_DGRAM, 0)) < 0 ||
417 bind(fd_udp, (struct sockaddr *)&udp_me, sizeof(udp_me)) ||
418 connect(fd_udp, (struct sockaddr *)&udp_peer, sizeof(udp_peer)) ||
419 setsockopt(fd_udp, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)) ||
420 setsockopt(fd_udp, SOL_SOCKET, SO_SNDBUF, &len, sizeof(len)) ||
421 nonblockify(fd_udp) || cloexec(fd_udp))
422 die(1, "couldn't set up UDP socket: %s", strerror(errno));
424 if (cw.me.sin_port != 0)
426 else if (connaddr.sin_addr.s_addr != INADDR_ANY) {
428 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0 ||
429 bind(fd, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) ||
430 connect(fd, (struct sockaddr *)&connaddr, sizeof(connaddr)) ||
431 nonblockify(fd) || cloexec(fd))
432 die(1, "couldn't connect to TCP server: %s", strerror(errno));
435 dofwd(STDIN_FILENO, STDOUT_FILENO);
442 /*----- That's all, folks -------------------------------------------------*/