| 1 | /* -*-c-*- |
| 2 | * |
| 3 | * Forwarding UDP packets over a stream |
| 4 | * |
| 5 | * (c) 2003 Straylight/Edgeware |
| 6 | */ |
| 7 | |
| 8 | /*----- Licensing notice --------------------------------------------------* |
| 9 | * |
| 10 | * This file is part of Trivial IP Encryption (TrIPE). |
| 11 | * |
| 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. |
| 16 | * |
| 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 |
| 20 | * for more details. |
| 21 | * |
| 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/>. |
| 24 | */ |
| 25 | |
| 26 | /*----- Header files ------------------------------------------------------*/ |
| 27 | |
| 28 | #include "config.h" |
| 29 | |
| 30 | #include <ctype.h> |
| 31 | #include <errno.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
| 34 | #include <string.h> |
| 35 | |
| 36 | #include <sys/time.h> |
| 37 | #include <sys/types.h> |
| 38 | #include <unistd.h> |
| 39 | #include <fcntl.h> |
| 40 | #include <sys/uio.h> |
| 41 | #include <sys/socket.h> |
| 42 | #include <netinet/in.h> |
| 43 | #include <arpa/inet.h> |
| 44 | #include <netdb.h> |
| 45 | |
| 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> |
| 53 | #include <mLib/sel.h> |
| 54 | #include <mLib/selpk.h> |
| 55 | |
| 56 | #include "util.h" |
| 57 | |
| 58 | /*----- Data structures ---------------------------------------------------*/ |
| 59 | |
| 60 | typedef struct pk { |
| 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 */ |
| 64 | } pk; |
| 65 | |
| 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 */ |
| 73 | } pkstream; |
| 74 | |
| 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 */ |
| 79 | } connwait; |
| 80 | |
| 81 | /*----- Static variables --------------------------------------------------*/ |
| 82 | |
| 83 | static sel_state sel; |
| 84 | static connwait cw; |
| 85 | static int fd_udp; |
| 86 | static size_t pk_nmax = 128, pk_szmax = 1024*1024; |
| 87 | |
| 88 | /*----- Main code ---------------------------------------------------------*/ |
| 89 | |
| 90 | static int nonblockify(int fd) |
| 91 | { return (fdflags(fd, O_NONBLOCK, O_NONBLOCK, 0, 0)); } |
| 92 | |
| 93 | static int cloexec(int fd) |
| 94 | { return (fdflags(fd, 0, 0, FD_CLOEXEC, FD_CLOEXEC)); } |
| 95 | |
| 96 | static void dolisten(void); |
| 97 | |
| 98 | static void doclose(pkstream *p) |
| 99 | { |
| 100 | pk *pk, *ppk; |
| 101 | close(p->w.fd); |
| 102 | close(p->p.reader.fd); |
| 103 | selpk_destroy(&p->p); |
| 104 | if (!(p->f&PKF_FULL)) sel_rmfile(&p->r); |
| 105 | if (p->npk) sel_rmfile(&p->w); |
| 106 | for (pk = p->pks; pk; pk = ppk) { |
| 107 | ppk = pk->next; |
| 108 | xfree(pk->p); |
| 109 | xfree(pk); |
| 110 | } |
| 111 | xfree(p); |
| 112 | if (cw.me.sin_port) dolisten(); |
| 113 | else exit(0); |
| 114 | } |
| 115 | |
| 116 | static void rdtcp(octet *b, size_t sz, pkbuf *pk, size_t *k, void *vp) |
| 117 | { |
| 118 | pkstream *p = vp; |
| 119 | size_t pksz; |
| 120 | |
| 121 | if (!sz) { doclose(p); return; } |
| 122 | pksz = LOAD16(b); |
| 123 | if (pksz + 2 == sz) { |
| 124 | DISCARD(write(fd_udp, b + 2, pksz)); |
| 125 | selpk_want(&p->p, 2); |
| 126 | } else { |
| 127 | selpk_want(&p->p, pksz + 2); |
| 128 | *k = sz; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | static void wrtcp(int fd, unsigned mode, void *vp) |
| 133 | { |
| 134 | #define NPK 16 |
| 135 | struct iovec iov[NPK]; |
| 136 | pkstream *p = vp; |
| 137 | size_t i; |
| 138 | ssize_t n; |
| 139 | pk *pk, *ppk; |
| 140 | |
| 141 | for (i = 0, pk = p->pks; i < NPK && pk; i++, pk = pk->next) { |
| 142 | iov[i].iov_base = pk->o; |
| 143 | iov[i].iov_len = pk->n; |
| 144 | } |
| 145 | |
| 146 | if ((n = writev(fd, iov, i)) < 0) { |
| 147 | if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) return; |
| 148 | moan("couldn't write to TCP socket: %s", strerror(errno)); |
| 149 | doclose(p); |
| 150 | return; |
| 151 | } |
| 152 | |
| 153 | p->szpk -= n; |
| 154 | for (pk = p->pks; n && pk; pk = ppk) { |
| 155 | ppk = pk->next; |
| 156 | if (pk->n <= n) { |
| 157 | p->npk--; |
| 158 | n -= pk->n; |
| 159 | xfree(pk->p); |
| 160 | xfree(pk); |
| 161 | } else { |
| 162 | pk->n -= n; |
| 163 | pk->o += n; |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | p->pks = pk; |
| 168 | if (!pk) { p->pk_tail = &p->pks; sel_rmfile(&p->w); } |
| 169 | if ((p->f&PKF_FULL) && p->npk < pk_nmax && p->szpk < pk_szmax) |
| 170 | { p->f &= ~PKF_FULL; sel_addfile(&p->r); } |
| 171 | } |
| 172 | |
| 173 | static void rdudp(int fd, unsigned mode, void *vp) |
| 174 | { |
| 175 | octet buf[65536]; |
| 176 | ssize_t n; |
| 177 | pkstream *p = vp; |
| 178 | pk *pk; |
| 179 | |
| 180 | if ((n = read(fd, buf, sizeof(buf))) < 0) { |
| 181 | if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) |
| 182 | return; |
| 183 | moan("couldn't read from UDP socket: %s", strerror(errno)); |
| 184 | return; |
| 185 | } |
| 186 | pk = xmalloc(sizeof(*pk)); |
| 187 | pk->next = 0; |
| 188 | pk->p = xmalloc(n + 2); |
| 189 | STORE16(pk->p, n); |
| 190 | memcpy(pk->p + 2, buf, n); |
| 191 | pk->o = pk->p; |
| 192 | pk->n = n + 2; |
| 193 | *p->pk_tail = pk; |
| 194 | p->pk_tail = &pk->next; |
| 195 | if (!p->npk) sel_addfile(&p->w); |
| 196 | sel_force(&p->w); |
| 197 | p->npk++; |
| 198 | p->szpk += n + 2; |
| 199 | if (p->npk >= pk_nmax || p->szpk >= pk_szmax) |
| 200 | { sel_rmfile(&p->r); p->f |= PKF_FULL; } |
| 201 | } |
| 202 | |
| 203 | static void dofwd(int fd_in, int fd_out) |
| 204 | { |
| 205 | pkstream *p = xmalloc(sizeof(*p)); |
| 206 | sel_initfile(&sel, &p->r, fd_udp, SEL_READ, rdudp, p); |
| 207 | sel_initfile(&sel, &p->w, fd_out, SEL_WRITE, wrtcp, p); |
| 208 | selpk_init(&p->p, &sel, fd_in, rdtcp, p); |
| 209 | selpk_want(&p->p, 2); |
| 210 | p->pks = 0; |
| 211 | p->pk_tail = &p->pks; |
| 212 | p->npk = p->szpk = 0; |
| 213 | p->f = 0; |
| 214 | sel_addfile(&p->r); |
| 215 | } |
| 216 | |
| 217 | static void doaccept(int fd_s, unsigned mode, void *p) |
| 218 | { |
| 219 | int fd; |
| 220 | struct sockaddr_in sin; |
| 221 | socklen_t sz = sizeof(sin); |
| 222 | |
| 223 | if ((fd = accept(fd_s, (struct sockaddr *)&sin, &sz)) < 0) { |
| 224 | if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) return; |
| 225 | moan("couldn't accept incoming connection: %s", strerror(errno)); |
| 226 | return; |
| 227 | } |
| 228 | if (cw.peer.s_addr != INADDR_ANY && |
| 229 | cw.peer.s_addr != sin.sin_addr.s_addr) { |
| 230 | moan("rejecting connection from %s", inet_ntoa(sin.sin_addr)); |
| 231 | close(fd); return; |
| 232 | } |
| 233 | if (nonblockify(fd) || cloexec(fd)) { |
| 234 | moan("couldn't accept incoming connection: %s", strerror(errno)); |
| 235 | close(fd); return; |
| 236 | } |
| 237 | dofwd(fd, fd); |
| 238 | close(fd_s); |
| 239 | sel_rmfile(&cw.a); |
| 240 | } |
| 241 | |
| 242 | static void dolisten(void) |
| 243 | { |
| 244 | int fd; |
| 245 | int opt = 1; |
| 246 | |
| 247 | if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0 || |
| 248 | setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) || |
| 249 | bind(fd, (struct sockaddr *)&cw.me, sizeof(cw.me)) || |
| 250 | listen(fd, 1) || nonblockify(fd) || cloexec(fd)) |
| 251 | die(1, "couldn't set up listening socket: %s", strerror(errno)); |
| 252 | sel_initfile(&sel, &cw.a, fd, SEL_READ, doaccept, 0); |
| 253 | sel_addfile(&cw.a); |
| 254 | } |
| 255 | |
| 256 | static void parseaddr(const char *pp, struct in_addr *a, unsigned short *pt) |
| 257 | { |
| 258 | char *p = xstrdup(pp); |
| 259 | char *q = 0; |
| 260 | struct hostent *h; |
| 261 | struct servent *s; |
| 262 | char *qq; |
| 263 | unsigned long n; |
| 264 | |
| 265 | if (!pt); |
| 266 | else if (a) q = p; |
| 267 | else { |
| 268 | strtok(p, ":"); |
| 269 | q = strtok(0, ""); |
| 270 | if (!q) die(1, "missing port number in address `%s'", p); |
| 271 | } |
| 272 | |
| 273 | if (a) { |
| 274 | if ((h = gethostbyname(p)) == 0) die(1, "unknown host `%s'", p); |
| 275 | memcpy(a, h->h_addr, sizeof(*a)); |
| 276 | } |
| 277 | |
| 278 | if (pt) { |
| 279 | if ((n = strtoul(q, &qq, 0)) > 0 && !*qq && n <= 0xffff) *pt = htons(n); |
| 280 | else if ((s = getservbyname(q, "tcp")) != 0) *pt = s->s_port; |
| 281 | else die(1, "bad port number `%s'", q); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | static void usage(FILE *fp) |
| 286 | { |
| 287 | pquis(fp, |
| 288 | "Usage: $ [-l PORT] [-b ADDR] [-p ADDR] [-c ADDR:PORT]\n\ |
| 289 | ADDR:PORT ADDR:PORT\n"); |
| 290 | } |
| 291 | |
| 292 | static void version(FILE *fp) |
| 293 | { pquis(fp, "$, tripe version " VERSION "\n"); } |
| 294 | |
| 295 | static void help(FILE *fp) |
| 296 | { |
| 297 | version(fp); |
| 298 | fputc('\n', fp); |
| 299 | usage(fp); |
| 300 | fputs("\n\ |
| 301 | Options:\n\ |
| 302 | \n\ |
| 303 | -h, --help Display this help text.\n\ |
| 304 | -v, --version Display version number.\n\ |
| 305 | -u, --usage Display pointless usage message.\n\ |
| 306 | \n\ |
| 307 | -l, --listen=PORT Listen for connections to TCP PORT.\n\ |
| 308 | -p, --peer=ADDR Only accept connections from IP ADDR.\n\ |
| 309 | -b, --bind=ADDR Bind to ADDR before connecting.\n\ |
| 310 | -c, --connect=ADDR:PORT Connect to IP ADDR, TCP PORT.\n\ |
| 311 | \n\ |
| 312 | Forwards UDP packets over a reliable stream. By default, uses stdin and\n\ |
| 313 | stdout; though it can use TCP sockets instead.\n\ |
| 314 | ", fp); |
| 315 | } |
| 316 | |
| 317 | int main(int argc, char *argv[]) |
| 318 | { |
| 319 | unsigned f = 0; |
| 320 | unsigned short pt; |
| 321 | struct sockaddr_in connaddr, bindaddr; |
| 322 | struct sockaddr_in udp_me, udp_peer; |
| 323 | int fd; |
| 324 | int len = 65536; |
| 325 | |
| 326 | #define f_bogus 1u |
| 327 | |
| 328 | ego(argv[0]); |
| 329 | bindaddr.sin_family = AF_INET; |
| 330 | bindaddr.sin_addr.s_addr = INADDR_ANY; |
| 331 | bindaddr.sin_port = 0; |
| 332 | connaddr.sin_family = AF_INET; |
| 333 | connaddr.sin_addr.s_addr = INADDR_ANY; |
| 334 | cw.me.sin_family = AF_INET; |
| 335 | cw.me.sin_addr.s_addr = INADDR_ANY; |
| 336 | cw.me.sin_port = 0; |
| 337 | cw.peer.s_addr = INADDR_ANY; |
| 338 | sel_init(&sel); |
| 339 | for (;;) { |
| 340 | static struct option opt[] = { |
| 341 | { "help", 0, 0, 'h' }, |
| 342 | { "version", 0, 0, 'v' }, |
| 343 | { "usage", 0, 0, 'u' }, |
| 344 | { "listen", OPTF_ARGREQ, 0, 'l' }, |
| 345 | { "peer", OPTF_ARGREQ, 0, 'p' }, |
| 346 | { "bind", OPTF_ARGREQ, 0, 'b' }, |
| 347 | { "connect", OPTF_ARGREQ, 0, 'c' }, |
| 348 | { 0, 0, 0, 0 } |
| 349 | }; |
| 350 | int i; |
| 351 | |
| 352 | i = mdwopt(argc, argv, "hvul:p:b:c:", opt, 0, 0, 0); |
| 353 | if (i < 0) |
| 354 | break; |
| 355 | switch (i) { |
| 356 | case 'h': help(stdout); exit(0); |
| 357 | case 'v': version(stdout); exit(0); |
| 358 | case 'u': usage(stdout); exit(0); |
| 359 | case 'l': parseaddr(optarg, 0, &pt); cw.me.sin_port = pt; break; |
| 360 | case 'p': parseaddr(optarg, &cw.peer, 0); break; |
| 361 | case 'b': |
| 362 | parseaddr(optarg, &bindaddr.sin_addr, 0); |
| 363 | cw.me.sin_addr = bindaddr.sin_addr; |
| 364 | break; |
| 365 | case 'c': |
| 366 | parseaddr(optarg, &connaddr.sin_addr, 0); |
| 367 | connaddr.sin_port = pt; |
| 368 | break; |
| 369 | default: f |= f_bogus; break; |
| 370 | } |
| 371 | } |
| 372 | if (optind + 2 != argc || (f&f_bogus)) { usage(stderr); exit(1); } |
| 373 | |
| 374 | udp_me.sin_family = udp_peer.sin_family = AF_INET; |
| 375 | parseaddr(argv[optind], &udp_me.sin_addr, &pt); |
| 376 | udp_me.sin_port = pt; |
| 377 | parseaddr(argv[optind + 1], &udp_peer.sin_addr, &pt); |
| 378 | udp_peer.sin_port = pt; |
| 379 | |
| 380 | if ((fd_udp = socket(PF_INET, SOCK_DGRAM, 0)) < 0 || |
| 381 | bind(fd_udp, (struct sockaddr *)&udp_me, sizeof(udp_me)) || |
| 382 | connect(fd_udp, (struct sockaddr *)&udp_peer, sizeof(udp_peer)) || |
| 383 | setsockopt(fd_udp, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)) || |
| 384 | setsockopt(fd_udp, SOL_SOCKET, SO_SNDBUF, &len, sizeof(len)) || |
| 385 | nonblockify(fd_udp) || cloexec(fd_udp)) |
| 386 | die(1, "couldn't set up UDP socket: %s", strerror(errno)); |
| 387 | |
| 388 | if (cw.me.sin_port != 0) |
| 389 | dolisten(); |
| 390 | else if (connaddr.sin_addr.s_addr == INADDR_ANY) |
| 391 | dofwd(STDIN_FILENO, STDOUT_FILENO); |
| 392 | else { |
| 393 | if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0 || |
| 394 | bind(fd, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) || |
| 395 | connect(fd, (struct sockaddr *)&connaddr, sizeof(connaddr)) || |
| 396 | nonblockify(fd) || cloexec(fd)) |
| 397 | die(1, "couldn't connect to TCP server: %s", strerror(errno)); |
| 398 | dofwd(fd, fd); |
| 399 | } |
| 400 | |
| 401 | for (;;) { |
| 402 | if (sel_select(&sel) && errno != EINTR) |
| 403 | die(1, "select failed: %s", strerror(errno)); |
| 404 | } |
| 405 | return (0); |
| 406 | } |
| 407 | |
| 408 | /*----- That's all, folks -------------------------------------------------*/ |