-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetrecord.c
482 lines (352 loc) · 13.1 KB
/
netrecord.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
475
476
477
478
479
480
481
482
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <limits.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <poll.h>
#include <pthread.h>
#include <sys/time.h>
#include <signal.h>
typedef int fd_t;
typedef struct {
fd_t socket;
int id;
const char *log_filename;
struct sockaddr_in *server_addr;
struct timeval start_time;
} start_info_t;
void interrupt_handler(int signum) {
/* Do nothing; we will break out of the accept() loop when it gets EINTR */
}
void *handle_connection(void *);
int main(int argc, char *argv[]) {
int res;
/* Parse arguments */
if (argc != 5) {
fprintf(stderr, "Usage:\n");
fprintf(stderr, "%s <server host> <server port> <proxy port> <log file root>\n", argv[0]);
goto exit_badly;
}
struct hostent *server_host = gethostbyname(argv[1]);
if (!server_host) {
fprintf(stderr, "Could not find host '%s': %s\n", argv[1], hstrerror(h_errno));
goto exit_badly;
}
int server_port = htons(atoi(argv[2]));
if (server_port == 0) {
fprintf(stderr, "Could not parse port '%s'\n", argv[2]);
goto exit_badly;
}
int proxy_port = atoi(argv[3]);
if (proxy_port == 0) {
fprintf(stderr, "Could not parse port '%s'\n", argv[3]);
goto exit_badly;
}
const char *log_filename = argv[4];
/* Record start time */
struct timeval start_time;
res = gettimeofday(&start_time, NULL);
if (res < 0) {
fprintf(stderr, "Could not get time of day: %s\n", strerror(errno));
goto exit_badly;
}
/* Prepare server address */
struct sockaddr_in server_addr;
memset(&server_addr, 0, sizeof(struct sockaddr_in));
server_addr.sin_family = AF_INET;
server_addr.sin_port = server_port;
memcpy(&server_addr.sin_addr, server_host->h_addr_list[0], 4);
/* Start listener socket */
fd_t listener = socket(PF_INET, SOCK_STREAM, 0);
if (listener < 0) {
fprintf(stderr, "Could not create listener: %s\n", strerror(errno));
goto exit_badly;
}
struct sockaddr_in proxy_addr;
memset(&proxy_addr, 0, sizeof(struct sockaddr_in));
proxy_addr.sin_family = AF_INET;
proxy_addr.sin_port = htons(proxy_port);
proxy_addr.sin_addr.s_addr = INADDR_ANY;
res = bind(listener, (struct sockaddr *)&proxy_addr, sizeof(struct sockaddr_in));
if (res < 0) {
fprintf(stderr, "Could not bind socket: %s\n", strerror(errno));
goto close_listener_and_exit_badly;
}
res = listen(listener, 1);
if (res < 0) {
fprintf(stderr, "Could not listen: %s\n", strerror(errno));
goto close_listener_and_exit_badly;
}
/* Install signal handler */
struct sigaction action;
bzero((char*)&action, sizeof(action));
action.sa_handler = interrupt_handler;
res = sigaction(SIGINT, &action, NULL);
if (res < 0) {
fprintf(stderr, "Could not install SIGINT handler: %s\n", strerror(errno));
goto close_listener_and_exit_badly;
}
/* Main loop */
fprintf(stderr, "Listening on port %d.\n", proxy_port);
int next_connection_id = 1;
for (;;) {
/* Accept a new connection */
fd_t client = accept(listener, NULL, NULL);
if (client < 0) {
if (errno == EINTR) {
break;
} else {
fprintf(stderr, "Could not accept: %s\n", strerror(errno));
goto close_listener_and_exit_badly;
}
}
/* Start a new thread to handle it */
start_info_t *info = malloc(sizeof(start_info_t));
info->socket = client;
info->id = next_connection_id++;
info->log_filename = log_filename;
info->server_addr = &server_addr;
info->start_time = start_time;
pthread_t thread_id;
res = pthread_create(&thread_id, NULL, handle_connection, info);
if (res < 0) {
fprintf(stderr, "Could not create thread: %s\n", strerror(errno));
goto close_listener_and_exit_badly;
}
}
/* Clean up */
fprintf(stderr, "Shutting down.\n");
close(listener);
return 0;
/* Error handling */
close_listener_and_exit_badly:
close(listener);
exit_badly:
return 1;
}
typedef struct {
int id;
FILE *log;
struct timeval start_time;
} thread_info_t;
typedef struct {
fd_t fd;
struct pollfd *pollfd;
const char *name;
char log_name;
} entity_info_t;
#define BUFFER_SIZE 4096
typedef struct {
char buffer[BUFFER_SIZE];
size_t backup;
entity_info_t *in, *out;
} pipe_info_t;
int write_log_entry(thread_info_t *thread, const char *name, const char *msg);
int setnonblocking(thread_info_t *thread, entity_info_t *ent);
int check(thread_info_t *thread, entity_info_t *ent, entity_info_t *other, int *running);
int pump(thread_info_t *thread, pipe_info_t *pipe);
void *handle_connection(void *data) {
int res;
/* Initialize thread */
start_info_t *start_info = data;
thread_info_t thread;
thread.id = start_info->id;
thread.start_time = start_info->start_time;
struct pollfd pollfds[2];
fprintf(stderr, "Opened connection %d.\n", thread.id);
/* Create log file */
char log_filename[PATH_MAX];
snprintf(log_filename, PATH_MAX, "%s_%d", start_info->log_filename, thread.id);
thread.log = fopen(log_filename, "w");
if (!thread.log) {
fprintf(stderr, "Connection %d could not open log file '%s': %s\n",
thread.id, log_filename, strerror(errno));
goto free_start_info;
}
/* Write first log entry to show when connection was established */
res = write_log_entry(&thread, "client", "con");
if (res < 0) goto close_log;
/* Prepare client */
entity_info_t client;
client.name = "client";
client.fd = start_info->socket;
res = setnonblocking(&thread, &client);
if (res == -1) goto close_log;
pollfds[0].fd = client.fd;
pollfds[0].events = POLLIN | POLLHUP | POLLRDHUP | POLLERR;
client.pollfd = &pollfds[0];
/* Connect to the server */
entity_info_t server;
server.name = "server";
server.fd = socket(PF_INET, SOCK_STREAM, 0);
if (server.fd < 0) {
fprintf(stderr, "Connection %d socket() failed: %s\n", thread.id, strerror(errno));
goto close_log;
}
res = connect(server.fd, (struct sockaddr *)start_info->server_addr, sizeof(struct sockaddr_in));
if (res < 0) {
fprintf(stderr, "Connection %d could not connect to server: %s\n", thread.id, strerror(errno));
goto close_server;
}
res = setnonblocking(&thread, &server);
if (res < 0) goto close_server;
pollfds[1].fd = server.fd;
pollfds[1].events = POLLIN | POLLHUP | POLLRDHUP | POLLERR;
server.pollfd = &pollfds[1];
/* Create pipes */
pipe_info_t client_to_server;
client_to_server.backup = 0;
client_to_server.in = &client;
client_to_server.out = &server;
pipe_info_t server_to_client;
server_to_client.backup = 0;
server_to_client.in = &server;
server_to_client.out = &client;
/* Main loop */
for (;;) {
res = poll(pollfds, 2, -1);
if (res <= 0) fprintf(stderr, "poll() failed with rc %d: %s\n", res, strerror(errno));
/* Check for error conditions and hangups */
int running = 1;
res = check(&thread, &client, &server, &running);
if (res < 0) goto close_server;
if (!running) break;
res = check(&thread, &server, &client, &running);
if (res < 0) goto close_server;
if (!running) break;
/* Transfer data from client to server */
res = pump(&thread, &client_to_server);
if (res < 0) goto close_server;
/* Transfer data from server to client */
res = pump(&thread, &server_to_client);
if (res < 0) goto close_server;
}
/* Clean up */
close_server:
close(server.fd);
close_log:
fclose(thread.log);
free_start_info:
close(start_info->socket);
free(start_info);
return NULL;
}
int write_log_entry(thread_info_t *thread, const char *name, const char *msg) {
int res;
struct timeval current_time;
res = gettimeofday(¤t_time, NULL);
if (res < 0) {
fprintf(stderr, "Connection %d gettimeofday() failed: %s\n", thread->id, strerror(errno));
return -1;
}
struct timeval delta;
if (current_time.tv_usec > thread->start_time.tv_usec) {
delta.tv_sec = current_time.tv_sec - thread->start_time.tv_sec;
delta.tv_usec = current_time.tv_usec - thread->start_time.tv_usec;
} else {
delta.tv_sec = current_time.tv_sec - thread->start_time.tv_sec - 1;
delta.tv_usec = current_time.tv_usec + 1000000 - thread->start_time.tv_usec;
}
res = fprintf(thread->log, "%c %d.%.6d %s\n", name[0], (int)delta.tv_sec, (int)delta.tv_usec, msg);
if (res < 0) {
fprintf(stderr, "Connection %d could not write to log: %s\n", thread->id, strerror(errno));
return -1;
}
return 0;
}
int setnonblocking(thread_info_t *thread, entity_info_t *ent) {
int res;
int flags = fcntl(ent->fd, F_GETFL);
flags |= O_NONBLOCK;
res = fcntl(ent->fd, F_SETFL, flags);
if (res < 0) {
fprintf(stderr, "Connection %d could not make %s nonblocking: %s\n",
thread->id, ent->name, strerror(errno));
return -1;
}
return 0;
}
int check(thread_info_t *thread, entity_info_t *ent, entity_info_t *other, int *running) {
int res;
if (ent->pollfd->revents & POLLERR) {
res = write_log_entry(thread, ent->name, "err");
if (res < 0) return -1;
fprintf(stderr, "Connection %d %s error.\n", thread->id, ent->name);
return -1;
}
if ((ent->pollfd->revents & POLLHUP) || (ent->pollfd->revents & POLLRDHUP)) {
res = write_log_entry(thread, ent->name, "hup");
if (res < 0) return -1;
res = shutdown(other->fd, SHUT_RDWR);
if (res < 0) return -1;
fprintf(stderr, "Connection %d %s hangup.\n", thread->id, ent->name);
*running = 0;
}
/* TODO: Can we detect and properly handle the case where one half of a duplex connection is
shut down but the other half remains open? */
return 0;
}
int try_write(thread_info_t *thread, pipe_info_t *pipe);
int pump(thread_info_t *thread, pipe_info_t *pipe) {
int res;
if (pipe->out->pollfd->revents & POLLOUT) {
res = try_write(thread, pipe);
if (res < 0) return -1;
}
if (pipe->in->pollfd->revents & POLLIN && pipe->backup < BUFFER_SIZE) {
ssize_t bytes_read = read(pipe->in->fd, pipe->buffer + pipe->backup, BUFFER_SIZE - pipe->backup);
if (bytes_read < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return 0;
}
fprintf(stderr, "Connection %d could not receive from %s: %s\n",
thread->id, pipe->in->name, strerror(errno));
return -1;
}
/* Put an entry into the log. When we write the log, we use blocking IO calls. We presume
that the disk is faster than the network.
Maybe it would be better to do this in try_write() so that the entry's timestamp is closer
to the actual time that the data is sent to the server or client. */
char count_buffer[20];
sprintf(count_buffer, "%d", (int)bytes_read);
res = write_log_entry(thread, pipe->in->name, count_buffer);
if (res < 0) return -1;
res = fwrite(pipe->buffer + pipe->backup, 1, bytes_read, thread->log);
if (res != bytes_read) {
fprintf(stderr, "Connection %d could not write to log: %s\n", thread->id, strerror(errno));
return -1;
}
pipe->backup += bytes_read;
}
if (pipe->backup) {
res = try_write(thread, pipe);
if (res < 0) return -1;
}
if (pipe->backup)
pipe->out->pollfd->events |= POLLOUT;
else
pipe->out->pollfd->events &= ~POLLOUT;
return 0;
}
int try_write(thread_info_t *thread, pipe_info_t *pipe) {
ssize_t bytes_written = write(pipe->out->fd, pipe->buffer, pipe->backup);
if (bytes_written < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
return 0;
}
fprintf(stderr, "Connection %d could not send to %s: %s\n",
thread->id, pipe->out->name, strerror(errno));
return -1;
}
memmove(pipe->buffer, pipe->buffer + bytes_written, pipe->backup - bytes_written);
pipe->backup -= bytes_written;
return 0;
}