3 * $Id: pkstream.c,v 1.3 2004/04/08 01:36:17 mdw Exp $
5 * Forwarding UDP packets over a stream
7 * (c) 2003 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Trivial IP Encryption (TrIPE).
14 * TrIPE is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * TrIPE is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with TrIPE; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 /*----- Header files ------------------------------------------------------*/
40 #include <sys/types.h>
44 #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/dstr.h>
52 #include <mLib/fdflags.h>
53 #include <mLib/mdwopt.h>
54 #include <mLib/quis.h>
55 #include <mLib/report.h>
57 #include <mLib/selpk.h>
59 /*----- Data structures ---------------------------------------------------*/
62 struct pk *next; /* Next packet in the chain */
63 octet *p, *o; /* Buffer start and current posn */
64 size_t n; /* Size of packet remaining */
67 typedef struct pkstream {
68 unsigned f; /* Flags... */
69 #define PKF_FULL 1u /* Buffer is full: stop reading */
70 sel_file r, w; /* Read and write selectors */
71 pk *pks, **pk_tail; /* Packet queue */
72 size_t npk, szpk; /* Number and size of data */
73 selpk p; /* Packet parser */
76 typedef struct connwait {
77 sel_file a; /* Selector */
78 struct sockaddr_in me; /* Who I'm meant to be */
79 struct in_addr peer; /* Who my peer is */
82 /*----- Static variables --------------------------------------------------*/
87 static size_t pk_nmax = 128, pk_szmax = 1024 * 1024;
89 /*----- Main code ---------------------------------------------------------*/
91 static int nonblockify(int fd)
92 { return (fdflags(fd, O_NONBLOCK, O_NONBLOCK, 0, 0)); }
94 static int cloexec(int fd)
95 { return (fdflags(fd, 0, 0, FD_CLOEXEC, FD_CLOEXEC)); }
97 static void dolisten(void);
99 static void doclose(pkstream *p)
103 close(p->p.reader.fd);
104 selpk_destroy(&p->p);
105 if (!(p->f & PKF_FULL))
109 for (pk = p->pks; pk; pk = ppk) {
115 if (cw.me.sin_port != 0)
121 static void rdtcp(octet *b, size_t sz, pkbuf *pk, size_t *k, void *vp)
131 if (pksz + 2 == sz) {
132 write(fd_udp, b + 2, pksz);
133 selpk_want(&p->p, 2);
135 selpk_want(&p->p, pksz + 2);
140 static void wrtcp(int fd, unsigned mode, void *vp)
143 struct iovec iov[NPK];
149 for (i = 0, pk = p->pks; i < NPK && pk; i++, pk = pk->next) {
150 iov[i].iov_base = pk->o;
151 iov[i].iov_len = pk->n;
154 if ((n = writev(fd, iov, i)) < 0) {
155 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
157 moan("couldn't write to TCP socket: %s", strerror(errno));
163 for (pk = p->pks; n && pk; pk = ppk) {
178 p->pk_tail = &p->pks;
181 if ((p->f & PKF_FULL) && p->npk < pk_nmax && p->szpk < pk_szmax) {
187 static void rdudp(int fd, unsigned mode, void *vp)
194 if ((n = read(fd, buf, sizeof(buf))) < 0) {
195 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
197 moan("couldn't read from UDP socket: %s", strerror(errno));
200 pk = xmalloc(sizeof(*pk));
202 pk->p = xmalloc(n + 2);
204 memcpy(pk->p + 2, buf, n);
208 p->pk_tail = &pk->next;
214 if (p->npk >= pk_nmax || p->szpk >= pk_szmax) {
220 static void dofwd(int fd_in, int fd_out)
222 pkstream *p = xmalloc(sizeof(*p));
223 sel_initfile(&sel, &p->r, fd_udp, SEL_READ, rdudp, p);
224 sel_initfile(&sel, &p->w, fd_out, SEL_WRITE, wrtcp, p);
225 selpk_init(&p->p, &sel, fd_in, rdtcp, p);
226 selpk_want(&p->p, 2);
228 p->pk_tail = &p->pks;
229 p->npk = p->szpk = 0;
234 static void doaccept(int fd_s, unsigned mode, void *p)
237 struct sockaddr_in sin;
238 socklen_t sz = sizeof(sin);
240 if ((fd = accept(fd_s, (struct sockaddr *)&sin, &sz)) < 0) {
241 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
243 moan("couldn't accept incoming connection: %s", strerror(errno));
246 if (cw.peer.s_addr != INADDR_ANY &&
247 cw.peer.s_addr != sin.sin_addr.s_addr) {
249 moan("rejecting connection from %s", inet_ntoa(sin.sin_addr));
252 if (nonblockify(fd) || cloexec(fd)) {
254 moan("couldn't accept incoming connection: %s", strerror(errno));
262 static void dolisten(void)
267 if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0 ||
268 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) ||
269 bind(fd, (struct sockaddr *)&cw.me, sizeof(cw.me)) ||
270 listen(fd, 1) || nonblockify(fd) || cloexec(fd))
271 die(1, "couldn't set up listening socket: %s", strerror(errno));
272 sel_initfile(&sel, &cw.a, fd, SEL_READ, doaccept, 0);
276 static void parseaddr(const char *pp, struct in_addr *a, unsigned short *pt)
278 char *p = xstrdup(pp);
284 die(1, "missing port number in address `%s'", p);
291 if ((h = gethostbyname(p)) == 0)
292 die(1, "unknown host `%s'", p);
293 memcpy(a, h->h_addr, sizeof(*a));
300 if ((s = getservbyname(q, "tcp")) != 0)
302 else if ((n = strtoul(q, &qq, 0)) == 0 || *qq || n > 0xffff)
303 die(1, "bad port number `%s'", q);
309 static void usage(FILE *fp)
312 "Usage: $ [-l PORT] [-b ADDR] [-p ADDR] [-c ADDR:PORT]\n\
313 ADDR:PORT ADDR:PORT\n");
316 static void version(FILE *fp)
317 { pquis(fp, "$, tripe version " VERSION "\n"); }
319 static void help(FILE *fp)
327 -h, --help Display this help text.\n\
328 -v, --version Display version number.\n\
329 -u, --usage Display pointless usage message.\n\
331 -l, --listen=PORT Listen for connections to TCP PORT.\n\
332 -p, --peer=ADDR Only accept connections from IP ADDR.\n\
333 -b, --bind=ADDR Bind to ADDR before connecting.\n\
334 -c, --connect=ADDR:PORT Connect to IP ADDR, TCP PORT.\n\
336 Forwards UDP packets over a reliable stream. By default, uses stdin and\n\
337 stdout; though it can use TCP sockets instead.\n\
341 int main(int argc, char *argv[])
345 struct sockaddr_in connaddr, bindaddr;
346 struct sockaddr_in udp_me, udp_peer;
352 bindaddr.sin_family = AF_INET;
353 bindaddr.sin_addr.s_addr = INADDR_ANY;
354 bindaddr.sin_port = 0;
355 connaddr.sin_family = AF_INET;
356 connaddr.sin_addr.s_addr = INADDR_ANY;
357 cw.me.sin_family = AF_INET;
358 cw.me.sin_addr.s_addr = INADDR_ANY;
360 cw.peer.s_addr = INADDR_ANY;
363 static struct option opt[] = {
364 { "help", 0, 0, 'h' },
365 { "version", 0, 0, 'v' },
366 { "usage", 0, 0, 'u' },
367 { "listen", OPTF_ARGREQ, 0, 'l' },
368 { "peer", OPTF_ARGREQ, 0, 'p' },
369 { "bind", OPTF_ARGREQ, 0, 'b' },
370 { "connect", OPTF_ARGREQ, 0, 'c' },
375 i = mdwopt(argc, argv, "hvul:p:b:c:", opt, 0, 0, 0);
389 parseaddr(optarg, 0, &pt);
393 parseaddr(optarg, &cw.peer, 0);
396 parseaddr(optarg, &bindaddr.sin_addr, 0);
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 -------------------------------------------------*/