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