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