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