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