X-Git-Url: https://git.distorted.org.uk/~mdw/tripe/blobdiff_plain/23df0e5b6d5dfe65beaf6fee394746bfbf574035..1afc65b695d97ffb79a023e72bf2d255f167d7f7:/pkstream/pkstream.c diff --git a/pkstream/pkstream.c b/pkstream/pkstream.c index d431b820..88ff381a 100644 --- a/pkstream/pkstream.c +++ b/pkstream/pkstream.c @@ -9,19 +9,18 @@ * * This file is part of Trivial IP Encryption (TrIPE). * - * TrIPE is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. + * TrIPE is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation; either version 3 of the License, or (at your + * option) any later version. * - * TrIPE is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. + * TrIPE is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. * * You should have received a copy of the GNU General Public License - * along with TrIPE; if not, write to the Free Software Foundation, - * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * along with TrIPE. If not, see . */ /*----- Header files ------------------------------------------------------*/ @@ -84,7 +83,7 @@ typedef struct connwait { static sel_state sel; static connwait cw; static int fd_udp; -static size_t pk_nmax = 128, pk_szmax = 1024 * 1024; +static size_t pk_nmax = 128, pk_szmax = 1024*1024; /*----- Main code ---------------------------------------------------------*/ @@ -102,20 +101,16 @@ static void doclose(pkstream *p) close(p->w.fd); close(p->p.reader.fd); selpk_destroy(&p->p); - if (!(p->f & PKF_FULL)) - sel_rmfile(&p->r); - if (p->npk) - sel_rmfile(&p->w); + if (!(p->f&PKF_FULL)) sel_rmfile(&p->r); + if (p->npk) sel_rmfile(&p->w); for (pk = p->pks; pk; pk = ppk) { ppk = pk->next; xfree(pk->p); xfree(pk); } xfree(p); - if (cw.me.sin_port != 0) - dolisten(); - else - exit(0); + if (cw.me.sin_port) dolisten(); + else exit(0); } static void rdtcp(octet *b, size_t sz, pkbuf *pk, size_t *k, void *vp) @@ -123,13 +118,10 @@ static void rdtcp(octet *b, size_t sz, pkbuf *pk, size_t *k, void *vp) pkstream *p = vp; size_t pksz; - if (!sz) { - doclose(p); - return; - } + if (!sz) { doclose(p); return; } pksz = LOAD16(b); if (pksz + 2 == sz) { - IGNORE(write(fd_udp, b + 2, pksz)); + DISCARD(write(fd_udp, b + 2, pksz)); selpk_want(&p->p, 2); } else { selpk_want(&p->p, pksz + 2); @@ -152,8 +144,7 @@ static void wrtcp(int fd, unsigned mode, void *vp) } if ((n = writev(fd, iov, i)) < 0) { - if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) - return; + if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) return; moan("couldn't write to TCP socket: %s", strerror(errno)); doclose(p); return; @@ -174,14 +165,9 @@ static void wrtcp(int fd, unsigned mode, void *vp) } } p->pks = pk; - if (!pk) { - p->pk_tail = &p->pks; - sel_rmfile(&p->w); - } - if ((p->f & PKF_FULL) && p->npk < pk_nmax && p->szpk < pk_szmax) { - p->f &= ~PKF_FULL; - sel_addfile(&p->r); - } + if (!pk) { p->pk_tail = &p->pks; sel_rmfile(&p->w); } + if ((p->f&PKF_FULL) && p->npk < pk_nmax && p->szpk < pk_szmax) + { p->f &= ~PKF_FULL; sel_addfile(&p->r); } } static void rdudp(int fd, unsigned mode, void *vp) @@ -206,15 +192,12 @@ static void rdudp(int fd, unsigned mode, void *vp) pk->n = n + 2; *p->pk_tail = pk; p->pk_tail = &pk->next; - if (!p->npk) - sel_addfile(&p->w); + if (!p->npk) sel_addfile(&p->w); sel_force(&p->w); p->npk++; p->szpk += n + 2; - if (p->npk >= pk_nmax || p->szpk >= pk_szmax) { - sel_rmfile(&p->r); - p->f |= PKF_FULL; - } + if (p->npk >= pk_nmax || p->szpk >= pk_szmax) + { sel_rmfile(&p->r); p->f |= PKF_FULL; } } static void dofwd(int fd_in, int fd_out) @@ -238,21 +221,18 @@ static void doaccept(int fd_s, unsigned mode, void *p) socklen_t sz = sizeof(sin); if ((fd = accept(fd_s, (struct sockaddr *)&sin, &sz)) < 0) { - if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) - return; + if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR) return; moan("couldn't accept incoming connection: %s", strerror(errno)); return; } if (cw.peer.s_addr != INADDR_ANY && cw.peer.s_addr != sin.sin_addr.s_addr) { - close(fd); moan("rejecting connection from %s", inet_ntoa(sin.sin_addr)); - return; + close(fd); return; } if (nonblockify(fd) || cloexec(fd)) { - close(fd); moan("couldn't accept incoming connection: %s", strerror(errno)); - return; + close(fd); return; } dofwd(fd, fd); close(fd_s); @@ -277,32 +257,28 @@ static void parseaddr(const char *pp, struct in_addr *a, unsigned short *pt) { char *p = xstrdup(pp); char *q = 0; - if (a && pt) { + struct hostent *h; + struct servent *s; + char *qq; + unsigned long n; + + if (!pt); + else if (a) q = p; + else { strtok(p, ":"); q = strtok(0, ""); - if (!q) - die(1, "missing port number in address `%s'", p); - } else if (pt) { - q = p; + if (!q) die(1, "missing port number in address `%s'", p); } if (a) { - struct hostent *h; - if ((h = gethostbyname(p)) == 0) - die(1, "unknown host `%s'", p); + if ((h = gethostbyname(p)) == 0) die(1, "unknown host `%s'", p); memcpy(a, h->h_addr, sizeof(*a)); } if (pt) { - struct servent *s; - char *qq; - unsigned long n; - if ((s = getservbyname(q, "tcp")) != 0) - *pt = s->s_port; - else if ((n = strtoul(q, &qq, 0)) == 0 || *qq || n > 0xffff) - die(1, "bad port number `%s'", q); - else - *pt = htons(n); + if ((n = strtoul(q, &qq, 0)) > 0 && !*qq && n <= 0xffff) *pt = htons(n); + else if ((s = getservbyname(q, "tcp")) != 0) *pt = s->s_port; + else die(1, "bad port number `%s'", q); } } @@ -344,6 +320,7 @@ int main(int argc, char *argv[]) unsigned short pt; struct sockaddr_in connaddr, bindaddr; struct sockaddr_in udp_me, udp_peer; + int fd; int len = 65536; #define f_bogus 1u @@ -376,38 +353,23 @@ int main(int argc, char *argv[]) if (i < 0) break; switch (i) { - case 'h': - help(stdout); - exit(0); - case 'v': - version(stdout); - exit(0); - case 'u': - usage(stdout); - exit(0); - case 'l': - parseaddr(optarg, 0, &pt); - cw.me.sin_port = pt; - break; - case 'p': - parseaddr(optarg, &cw.peer, 0); - break; + case 'h': help(stdout); exit(0); + case 'v': version(stdout); exit(0); + case 'u': usage(stdout); exit(0); + case 'l': parseaddr(optarg, 0, &pt); cw.me.sin_port = pt; break; + case 'p': parseaddr(optarg, &cw.peer, 0); break; case 'b': parseaddr(optarg, &bindaddr.sin_addr, 0); + cw.me.sin_addr = bindaddr.sin_addr; break; case 'c': - parseaddr(optarg, &connaddr.sin_addr, &pt); + parseaddr(optarg, &connaddr.sin_addr, 0); connaddr.sin_port = pt; break; - default: - f |= f_bogus; - break; + default: f |= f_bogus; break; } } - if (optind + 2 != argc || (f & f_bogus)) { - usage(stderr); - exit(1); - } + if (optind + 2 != argc || (f&f_bogus)) { usage(stderr); exit(1); } udp_me.sin_family = udp_peer.sin_family = AF_INET; parseaddr(argv[optind], &udp_me.sin_addr, &pt); @@ -425,19 +387,21 @@ int main(int argc, char *argv[]) if (cw.me.sin_port != 0) dolisten(); - else if (connaddr.sin_addr.s_addr != INADDR_ANY) { - int fd; + else if (connaddr.sin_addr.s_addr == INADDR_ANY) + dofwd(STDIN_FILENO, STDOUT_FILENO); + else { if ((fd = socket(PF_INET, SOCK_STREAM, 0)) < 0 || bind(fd, (struct sockaddr *)&bindaddr, sizeof(bindaddr)) || connect(fd, (struct sockaddr *)&connaddr, sizeof(connaddr)) || nonblockify(fd) || cloexec(fd)) die(1, "couldn't connect to TCP server: %s", strerror(errno)); dofwd(fd, fd); - } else - dofwd(STDIN_FILENO, STDOUT_FILENO); + } - for (;;) - sel_select(&sel); + for (;;) { + if (sel_select(&sel) && errno != EINTR) + die(1, "select failed: %s", strerror(errno)); + } return (0); }