3 * Forwarding UDP packets over a stream
5 * (c) 2003 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Trivial IP Encryption (TrIPE).
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.
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
22 * You should have received a copy of the GNU General Public License
23 * along with TrIPE. If not, see <https://www.gnu.org/licenses/>.
26 /*----- Header files ------------------------------------------------------*/
37 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
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>
54 #include <mLib/selpk.h>
58 /*----- Data structures ---------------------------------------------------*/
61 struct pk
*next
; /* Next packet in the chain */
62 octet
*p
, *o
; /* Buffer start and current posn */
63 size_t n
; /* Size of packet remaining */
66 typedef struct pkstream
{
67 unsigned f
; /* Flags... */
68 #define PKF_FULL 1u /* Buffer is full: stop reading */
69 sel_file r
, w
; /* Read and write selectors */
70 pk
*pks
, **pk_tail
; /* Packet queue */
71 size_t npk
, szpk
; /* Number and size of data */
72 selpk p
; /* Packet parser */
75 typedef struct connwait
{
76 sel_file a
; /* Selector */
77 struct sockaddr_in me
, peer
; /* Who I'm meant to be; who peer is */
80 /*----- Static variables --------------------------------------------------*/
85 static size_t pk_nmax
= 128, pk_szmax
= 1024*1024;
87 /*----- Main code ---------------------------------------------------------*/
89 static int nonblockify(int fd
)
90 { return (fdflags(fd
, O_NONBLOCK
, O_NONBLOCK
, 0, 0)); }
92 static int cloexec(int fd
)
93 { return (fdflags(fd
, 0, 0, FD_CLOEXEC
, FD_CLOEXEC
)); }
95 static void initaddr(struct sockaddr_in
*sin
)
97 sin
->sin_family
= AF_INET
;
98 sin
->sin_addr
.s_addr
= INADDR_ANY
;
102 static void dolisten(void);
104 static void doclose(pkstream
*p
)
108 close(p
->p
.reader
.fd
);
109 selpk_destroy(&p
->p
);
110 if (!(p
->f
&PKF_FULL
)) sel_rmfile(&p
->r
);
111 if (p
->npk
) sel_rmfile(&p
->w
);
112 for (pk
= p
->pks
; pk
; pk
= ppk
) {
118 if (cw
.me
.sin_port
) dolisten();
122 static void rdtcp(octet
*b
, size_t sz
, pkbuf
*pk
, size_t *k
, void *vp
)
127 if (!sz
) { doclose(p
); return; }
129 if (pksz
+ 2 == sz
) {
130 DISCARD(write(fd_udp
, b
+ 2, pksz
));
131 selpk_want(&p
->p
, 2);
133 selpk_want(&p
->p
, pksz
+ 2);
138 static void wrtcp(int fd
, unsigned mode
, void *vp
)
141 struct iovec iov
[NPK
];
147 for (i
= 0, pk
= p
->pks
; i
< NPK
&& pk
; i
++, pk
= pk
->next
) {
148 iov
[i
].iov_base
= pk
->o
;
149 iov
[i
].iov_len
= pk
->n
;
152 if ((n
= writev(fd
, iov
, i
)) < 0) {
153 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EINTR
) return;
154 moan("couldn't write to TCP socket: %s", strerror(errno
));
160 for (pk
= p
->pks
; n
&& pk
; pk
= ppk
) {
174 if (!pk
) { p
->pk_tail
= &p
->pks
; sel_rmfile(&p
->w
); }
175 if ((p
->f
&PKF_FULL
) && p
->npk
< pk_nmax
&& p
->szpk
< pk_szmax
)
176 { p
->f
&= ~PKF_FULL
; sel_addfile(&p
->r
); }
179 static void rdudp(int fd
, unsigned mode
, void *vp
)
186 if ((n
= read(fd
, buf
, sizeof(buf
))) < 0) {
187 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EINTR
)
189 moan("couldn't read from UDP socket: %s", strerror(errno
));
192 pk
= xmalloc(sizeof(*pk
));
194 pk
->p
= xmalloc(n
+ 2);
196 memcpy(pk
->p
+ 2, buf
, n
);
200 p
->pk_tail
= &pk
->next
;
201 if (!p
->npk
) sel_addfile(&p
->w
);
205 if (p
->npk
>= pk_nmax
|| p
->szpk
>= pk_szmax
)
206 { sel_rmfile(&p
->r
); p
->f
|= PKF_FULL
; }
209 static void dofwd(int fd_in
, int fd_out
)
211 pkstream
*p
= xmalloc(sizeof(*p
));
212 sel_initfile(&sel
, &p
->r
, fd_udp
, SEL_READ
, rdudp
, p
);
213 sel_initfile(&sel
, &p
->w
, fd_out
, SEL_WRITE
, wrtcp
, p
);
214 selpk_init(&p
->p
, &sel
, fd_in
, rdtcp
, p
);
215 selpk_want(&p
->p
, 2);
217 p
->pk_tail
= &p
->pks
;
218 p
->npk
= p
->szpk
= 0;
223 static void doaccept(int fd_s
, unsigned mode
, void *p
)
226 struct sockaddr_in sin
;
227 socklen_t sz
= sizeof(sin
);
229 if ((fd
= accept(fd_s
, (struct sockaddr
*)&sin
, &sz
)) < 0) {
230 if (errno
== EAGAIN
|| errno
== EWOULDBLOCK
|| errno
== EINTR
) return;
231 moan("couldn't accept incoming connection: %s", strerror(errno
));
234 if (cw
.peer
.sin_addr
.s_addr
!= INADDR_ANY
&&
235 cw
.peer
.sin_addr
.s_addr
!= sin
.sin_addr
.s_addr
) {
236 moan("rejecting connection from %s", inet_ntoa(sin
.sin_addr
));
239 if (nonblockify(fd
) || cloexec(fd
)) {
240 moan("couldn't accept incoming connection: %s", strerror(errno
));
248 static void dolisten(void)
253 if ((fd
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0 ||
254 setsockopt(fd
, SOL_SOCKET
, SO_REUSEADDR
, &opt
, sizeof(opt
)) ||
255 bind(fd
, (struct sockaddr
*)&cw
.me
, sizeof(cw
.me
)) ||
256 listen(fd
, 1) || nonblockify(fd
) || cloexec(fd
))
257 die(1, "couldn't set up listening socket: %s", strerror(errno
));
258 sel_initfile(&sel
, &cw
.a
, fd
, SEL_READ
, doaccept
, 0);
263 static void parseaddr(const char *host
, const char *svc
, unsigned f
,
264 struct sockaddr_in
*sin
)
266 char *alloc
= 0, *sep
;
273 alloc
= xstrdup(host
);
274 if ((sep
= strchr(alloc
, ':')) == 0)
275 die(1, "missing port number in address `%s'", host
);
276 host
= alloc
; *sep
= 0; svc
= sep
+ 1;
280 if ((h
= gethostbyname(host
)) == 0) die(1, "unknown host `%s'", host
);
281 memcpy(&sin
->sin_addr
, h
->h_addr
, sizeof(sin
->sin_addr
));
285 if ((n
= strtoul(svc
, &qq
, 0)) > 0 && !*qq
&& n
<= 0xffff)
286 sin
->sin_port
= htons(n
);
287 else if ((s
= getservbyname(svc
, "tcp")) != 0)
288 sin
->sin_port
= s
->s_port
;
290 die(1, "bad service name/number `%s'", svc
);
296 static void usage(FILE *fp
)
299 "Usage: $ [-l PORT] [-b ADDR] [-p ADDR] [-c ADDR:PORT]\n\
300 ADDR:PORT ADDR:PORT\n");
303 static void version(FILE *fp
)
304 { pquis(fp
, "$, tripe version " VERSION
"\n"); }
306 static void help(FILE *fp
)
314 -h, --help Display this help text.\n\
315 -v, --version Display version number.\n\
316 -u, --usage Display pointless usage message.\n\
318 -l, --listen=PORT Listen for connections to TCP PORT.\n\
319 -p, --peer=ADDR Only accept connections from IP ADDR.\n\
320 -b, --bind=ADDR Bind to ADDR before connecting.\n\
321 -c, --connect=ADDR:PORT Connect to IP ADDR, TCP PORT.\n\
323 Forwards UDP packets over a reliable stream. By default, uses stdin and\n\
324 stdout; though it can use TCP sockets instead.\n\
328 int main(int argc
, char *argv
[])
331 const char *bindhost
= 0, *bindsvc
= 0, *peerhost
= 0;
332 struct sockaddr_in bindaddr
;
333 const char *connhost
= 0;
334 struct sockaddr_in tmpaddr
;
343 static struct option opt
[] = {
344 { "help", 0, 0, 'h' },
345 { "version", 0, 0, 'v' },
346 { "usage", 0, 0, 'u' },
347 { "listen", OPTF_ARGREQ
, 0, 'l' },
348 { "peer", OPTF_ARGREQ
, 0, 'p' },
349 { "bind", OPTF_ARGREQ
, 0, 'b' },
350 { "connect", OPTF_ARGREQ
, 0, 'c' },
355 i
= mdwopt(argc
, argv
, "hvul:p:b:c:", opt
, 0, 0, 0);
359 case 'h': help(stdout
); exit(0);
360 case 'v': version(stdout
); exit(0);
361 case 'u': usage(stdout
); exit(0);
362 case 'l': bindsvc
= optarg
; break;
363 case 'p': peerhost
= optarg
; break;
364 case 'b': bindhost
= optarg
; break;
365 case 'c': connhost
= optarg
; break;
366 default: f
|= f_bogus
; break;
369 if (optind
+ 2 != argc
|| (f
&f_bogus
)) { usage(stderr
); exit(1); }
371 if (bindhost
&& !bindsvc
&& !connhost
)
372 die(1, "bind addr only makes sense when listening or connecting");
373 if (peerhost
&& !bindsvc
)
374 die(1, "peer addr only makes sense when listening");
375 if (bindsvc
&& connhost
)
376 die(1, "can't listen and connect");
379 if (bindhost
|| bindsvc
) {
381 if (!bindsvc
) parseaddr(bindhost
, 0, 0, &bindaddr
);
382 else parseaddr(bindhost
, bindsvc
, 0, &cw
.me
);
386 if (peerhost
) parseaddr(peerhost
, 0, 0, &cw
.peer
);
390 parseaddr(connhost
, 0, paf_parse
, &tmpaddr
);
391 if ((fd
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) < 0 ||
393 bind(fd
, (struct sockaddr
*)&bindaddr
, sizeof(bindaddr
))) ||
394 connect(fd
, (struct sockaddr
*)&tmpaddr
, sizeof(tmpaddr
)))
395 die(1, "couldn't connect to TCP server: %s", strerror(errno
));
396 if (nonblockify(fd
) || cloexec(fd
))
397 die(1, "couldn't connect to TCP server: %s", strerror(errno
));
401 parseaddr(argv
[optind
], 0, paf_parse
, &tmpaddr
);
402 if ((fd_udp
= socket(AF_INET
, SOCK_DGRAM
, IPPROTO_UDP
)) < 0 ||
403 nonblockify(fd_udp
) || cloexec(fd_udp
) ||
404 setsockopt(fd_udp
, SOL_SOCKET
, SO_RCVBUF
, &len
, sizeof(len
)) ||
405 setsockopt(fd_udp
, SOL_SOCKET
, SO_SNDBUF
, &len
, sizeof(len
)) ||
406 bind(fd_udp
, (struct sockaddr
*)&tmpaddr
, sizeof(tmpaddr
)))
407 die(1, "couldn't set up UDP socket: %s", strerror(errno
));
409 parseaddr(argv
[optind
+ 1], 0, paf_parse
, &tmpaddr
);
410 if (connect(fd_udp
, (struct sockaddr
*)&tmpaddr
, sizeof(tmpaddr
)))
411 die(1, "couldn't set up UDP socket: %s", strerror(errno
));
413 if (bindsvc
) dolisten();
414 else if (connhost
) dofwd(fd
, fd
);
415 else dofwd(STDIN_FILENO
, STDOUT_FILENO
);
418 if (sel_select(&sel
) && errno
!= EINTR
)
419 die(1, "select failed: %s", strerror(errno
));
424 /*----- That's all, folks -------------------------------------------------*/