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