/
passthru.c
474 lines (390 loc) · 11.2 KB
/
passthru.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/* Passthru.c -- passes through all communications on a given TCP/UDP port through
to another host, logging all transmissions. */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdint.h>
#include <stdarg.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <time.h>
#include <sys/time.h>
#include <event.h>
#include "buffer.h"
#include "dumpbytes.h"
typedef struct Configuration {
int portnumber;
int otherport;
int pass_udp;
int echo_udp;
int pass_tcp;
int have_otherhost;
int hexmode;
unsigned char otherhostaddr[4];
char otherhostname[1024];
struct timeval timeout;
} Configuration;
typedef struct Connection {
BUFFER txfer_buf;
int collecting_from;
time_t last_read_time;
/* index 0 is the incoming connection, index 1 the outgoing */
char *name[2];
int fd[2];
struct event read_event[2];
} Connection;
typedef void (*event_handler_t)(int,short,void*);
static Configuration cfg;
static int tcpserv = -1;
static int udpserv = -1;
static struct event accept_event;
static struct event udp_event;
static void die(char const *format, ...) {
va_list vl;
va_start(vl, format);
vfprintf(stderr, format, vl);
va_end(vl);
exit(1);
}
static void log_fmt(char *fmt, ...) {
time_t t = time(NULL);
char *timestr = ctime(&t);
va_list vl;
timestr[strlen(timestr) - 1] = '\0';
printf("%s (%08lx): ", timestr, t);
va_start(vl, fmt);
vprintf(fmt, vl);
va_end(vl);
fflush(stdout);
}
static void log_time_fmt(time_t t, char *fmt, ...) {
char *timestr = ctime(&t);
va_list vl;
timestr[strlen(timestr) - 1] = '\0';
printf("%s (%08lx): ", timestr, t);
va_start(vl, fmt);
vprintf(fmt, vl);
va_end(vl);
fflush(stdout);
}
static void get_addr_name(char *namebuf, unsigned char *addr) {
struct hostent *h = gethostbyaddr(addr, 4, AF_INET);
if (h == NULL) {
sprintf(namebuf, "%u.%u.%u.%u",
addr[0],
addr[1],
addr[2],
addr[3]
);
return;
}
sprintf(namebuf, "%s", h->h_name);
}
static void dump_configuration_settings(void) {
log_fmt("invoked as: passthru %s%s%s%s-p %d -o %d%s%s -f %g\n",
cfg.pass_udp ? "-u " : "",
cfg.echo_udp ? "-E " : "",
cfg.pass_tcp ? "-t " : "",
cfg.hexmode ? "-x " : "-b ",
cfg.portnumber,
cfg.otherport,
cfg.have_otherhost ? " -h " : "",
cfg.have_otherhost ? cfg.otherhostname : "",
(double) cfg.timeout.tv_sec + (double) cfg.timeout.tv_usec / 1000000.0);
log_fmt("bound to port number %d\n",
cfg.portnumber);
if (cfg.have_otherhost)
log_fmt("will connect to port number %d on %s\n",
cfg.otherport, cfg.otherhostname);
}
static int parse_cmdline(int argc, char *argv[]) {
int show_help = 0;
int have_opt_o = 0;
while (1) {
switch (getopt(argc, argv, "uEtbxp:o:h:f:")) {
case 'u':
cfg.pass_udp = 1;
continue;
case 'E':
cfg.echo_udp = 1;
continue;
case 't':
cfg.pass_tcp = 1;
continue;
case 'b':
cfg.hexmode = 0;
continue;
case 'x':
cfg.hexmode = 1;
continue;
case 'p':
cfg.portnumber = atoi(optarg);
continue;
case 'o':
cfg.otherport = atoi(optarg);
have_opt_o = 1;
continue;
case 'h': {
struct hostent *h = gethostbyname(optarg);
unsigned char addr[4];
if (h == NULL) {
fprintf(stderr, "Host not found. Bailing out.\n");
break;
}
memcpy(addr, h->h_addr_list[0], sizeof(addr));
get_addr_name(cfg.otherhostname, addr);
memcpy(cfg.otherhostaddr, addr, sizeof(cfg.otherhostaddr));
cfg.have_otherhost = 1;
continue;
}
case 'f': {
double seconds;
seconds = strtod(optarg, NULL);
cfg.timeout.tv_sec = (long) seconds;
cfg.timeout.tv_usec = (long) ((seconds - cfg.timeout.tv_sec) * 1000000.0) % 1000000;
continue;
}
case EOF:
break;
default:
show_help = 1;
break;
}
break;
}
if (!have_opt_o)
cfg.otherport = cfg.portnumber;
if (show_help || (!cfg.pass_udp && !cfg.pass_tcp)) {
fprintf(stderr,
"Usage: passthru [-uEtbx] [-p portnumber] [-o destportnumber] [-h host_impersonated] [-f seconds]\n"
"\t-u\tPass UDP traffic through\n"
"\t-E\tEcho UDP traffic back to sender\n"
"\t-t\tPass TCP traffic through\n"
"\t-b\tDump results in binary format\n"
"\t-x\tDump results in hex format (default)\n"
"\t-p\tSpecify the port of TCP and/or UDP traffic to pass\n"
"\t-o\tSpecify the port to connect to at the destination\n"
"\t-h\tSpecify the host to pass through to\n"
"\t-f\tEnable and specify buffer flush interval in seconds\n"
"\tYou must select either -u or -t (or both).\n"
);
return 0;
}
dump_configuration_settings();
return 1;
}
/*
#define NONBLOCKIFY(fd) fcntl((fd), F_SETFL, fcntl((fd), F_GETFL, 0) | O_NONBLOCK)
*/
static void dump_buffer(Connection *c) {
if (c->collecting_from != -1) {
log_time_fmt(c->last_read_time,
"TCP: fd %d (%c; %s) sent %d bytes:\n",
c->fd[c->collecting_from],
c->collecting_from == 0 ? 'I' : 'O',
c->name[c->collecting_from],
c->txfer_buf->pos);
dump_buffer_to_stdout(c->txfer_buf->buf, c->txfer_buf->pos, cfg.hexmode);
fflush(stdout);
c->txfer_buf->pos = 0;
c->collecting_from = -1;
}
}
static void setup_other_sockaddr(struct sockaddr_in *s) {
s->sin_family = AF_INET;
memcpy(&s->sin_addr.s_addr, cfg.otherhostaddr, sizeof(cfg.otherhostaddr));
s->sin_port = htons(cfg.otherport);
}
static int open_otherhost(void) {
int sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in s;
if (sock < 0) {
die("Could not create outbound socket (errno %d: %s).\n",
errno, strerror(errno));
}
setup_other_sockaddr(&s);
if (connect(sock, (struct sockaddr *) &s, sizeof(s)) < 0) {
die("Could not connect outbound socket (errno %d: %s).\n",
errno, strerror(errno));
}
return sock;
}
static void close_conn(Connection *conn, int closing_fd) {
if (conn->txfer_buf->pos != 0) {
dump_buffer(conn);
}
event_del(&conn->read_event[0]);
event_del(&conn->read_event[1]);
close(conn->fd[0]);
close(conn->fd[1]);
log_fmt("connection to %s between I fd %d and O fd %d closed by fd %d\n",
conn->name[0], conn->fd[0], conn->fd[1], closing_fd);
free(conn->name[0]);
killbuf(conn->txfer_buf);
free(conn);
}
static void handle_data(int fd, short what, Connection *conn, int side) {
int from = conn->fd[side];
int to = conn->fd[!side];
char buf[4096];
int nread;
event_add(&conn->read_event[side], &cfg.timeout);
if ((what & EV_TIMEOUT) != 0) {
dump_buffer(conn);
return;
}
if (conn->collecting_from != side) {
dump_buffer(conn);
}
nread = read(from, buf, sizeof(buf));
switch (nread) {
case 0:
close_conn(conn, from);
break;
case -1:
if (errno != EAGAIN && errno != EINTR) {
close_conn(conn, -1);
}
break;
default:
if (cfg.have_otherhost) {
if (write(to, buf, nread) != nread) {
die("Write to fd %d failed: %d (%s)\n", errno, strerror(errno));
}
}
buf_append_n(conn->txfer_buf, buf, nread);
conn->last_read_time = time(NULL);
conn->collecting_from = side;
break;
}
}
static void handle_data0(int fd, short what, Connection *conn) {
handle_data(fd, what, conn, 0);
}
static void handle_data1(int fd, short what, Connection *conn) {
handle_data(fd, what, conn, 1);
}
static void accept_connection(int fd, short what, void *arg) {
struct sockaddr_in addr;
socklen_t addrlen = sizeof(addr);
int newfd = accept(fd, (struct sockaddr *) &addr, &addrlen);
Connection *conn;
if (newfd < 0)
return;
conn = calloc(sizeof(Connection), 1);
conn->txfer_buf = newbuf(4096);
conn->collecting_from = -1;
conn->last_read_time = 0;
conn->name[0] = malloc(1024);
conn->fd[0] = newfd;
get_addr_name(conn->name[0], (unsigned char *) &addr.sin_addr.s_addr);
event_set(&conn->read_event[0], conn->fd[0], EV_READ | EV_PERSIST,
(event_handler_t) handle_data0, conn);
conn->name[1] = cfg.otherhostname;
conn->fd[1] = open_otherhost();
event_set(&conn->read_event[1], conn->fd[1], EV_READ | EV_PERSIST,
(event_handler_t) handle_data1, conn);
log_fmt("connection accepted on I fd %d from %s to O fd %d\n",
newfd, conn->name[0], conn->fd[1]);
event_add(&conn->read_event[0], &cfg.timeout);
event_add(&conn->read_event[1], &cfg.timeout);
}
#define DGRAM_MSGBUF_LEN 65536
static void handle_datagram(int fd, short what, void *arg) {
struct sockaddr_in addr;
socklen_t addrlen = sizeof(addr);
char msgbuf[DGRAM_MSGBUF_LEN];
int msglen;
char newname[1024];
if ((msglen = recvfrom(fd, msgbuf, DGRAM_MSGBUF_LEN, 0,
(struct sockaddr *) &addr, &addrlen)) >= 0) {
get_addr_name(newname, (unsigned char *) &addr.sin_addr.s_addr);
log_fmt("UDP: %s sent %d bytes:\n", newname, msglen);
dump_buffer_to_stdout(msgbuf, msglen, cfg.hexmode);
fflush(stdout);
if (cfg.echo_udp) {
if (sendto(fd, msgbuf, msglen, 0, (struct sockaddr *) &addr, addrlen) == -1)
log_fmt("UDP: echo failed, errno = %d\n", errno);
else
log_fmt("UDP: echo succeeded\n");
}
if (cfg.have_otherhost) {
struct sockaddr_in oa;
setup_other_sockaddr(&oa);
if (sendto(fd, msgbuf, msglen, 0, (struct sockaddr *) &oa, sizeof(oa)) == -1)
log_fmt("UDP: pass through failed, errno = %d\n", errno);
else
log_fmt("UDP: pass through succeeded\n");
}
}
}
static void flush_all_f_buffers(int signo) {
log_fmt("interrupted with signal %d\n", signo);
fflush(NULL);
exit(0);
}
static int init_socket(int type, char *kind, int should_listen) {
int serv = socket(AF_INET, type, 0);
struct sockaddr_in s;
if (serv < 0) {
die("Could not open %s server socket.\n", kind);
}
s.sin_family = AF_INET;
s.sin_addr.s_addr = htonl(INADDR_ANY);
s.sin_port = htons(cfg.portnumber);
{
int i = 1; // 1 == turn on the option
setsockopt(serv, SOL_SOCKET, SO_REUSEADDR, &i, sizeof(i)); // don't care if this fails
}
if (bind(serv, (struct sockaddr *) &s, sizeof(s)) < 0) {
die("Could not bind %s server socket.\n", kind);
}
if (should_listen) {
if (listen(serv, 5) < 0) {
int savedErrno = errno;
die("Could not listen on %s server socket (errno %d: %s).\n",
kind, savedErrno, strerror(savedErrno));
}
}
/*
NONBLOCKIFY(serv);
*/
return serv;
}
static void init_passthru(void) {
if (cfg.pass_tcp) {
tcpserv = init_socket(SOCK_STREAM, "TCP", 1);
event_set(&accept_event, tcpserv, EV_READ | EV_PERSIST, accept_connection, NULL);
event_add(&accept_event, NULL);
}
if (cfg.pass_udp) {
udpserv = init_socket(SOCK_DGRAM, "UDP", 0);
event_set(&udp_event, udpserv, EV_READ | EV_PERSIST, handle_datagram, NULL);
event_add(&udp_event, NULL);
}
}
int main(int argc, char *argv[]) {
cfg.portnumber = cfg.otherport = 80;
cfg.pass_udp = cfg.echo_udp = 0;
cfg.pass_tcp = 0;
cfg.have_otherhost = 0;
cfg.hexmode = 1;
cfg.timeout.tv_sec = 1; cfg.timeout.tv_usec = 0;
memset(cfg.otherhostaddr, 0, sizeof(cfg.otherhostaddr));
if (!parse_cmdline(argc, argv))
return EXIT_FAILURE;
event_init();
init_passthru();
fflush(NULL);
signal(SIGINT, flush_all_f_buffers);
event_dispatch();
return EXIT_SUCCESS;
}