-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdriftnet.c
810 lines (680 loc) · 25.3 KB
/
driftnet.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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
/*
* driftnet.c:
* Pick out images from passing network traffic.
*
* Copyright (c) 2001 Chris Lightfoot. All rights reserved.
* Email: chris@ex-parrot.com; WWW: http://www.ex-parrot.com/~chris/
*
*/
static const char rcsid[] = "$Id: driftnet.c,v 1.32 2003/10/16 11:56:37 chris Exp $";
#undef NDEBUG
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <pcap.h>
#include <netinet/in.h> /* needs to be before <arpa/inet.h> on OpenBSD */
#include <arpa/inet.h>
#include <limits.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <ctype.h>
#include <fcntl.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <signal.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "driftnet.h"
#define SNAPLEN 262144 /* largest chunk of data we accept from pcap */
#define WRAPLEN 262144 /* out-of-order packet margin */
/* slots for storing information about connections */
connection *slots;
unsigned int slotsused, slotsalloc;
/* flags: verbose, adjunct mode, temporary directory to use, media types to
* extract, beep on image. */
int extract_images = 1;
int verbose, adjunct, beep;
int tmpdir_specified;
char *tmpdir;
int max_tmpfiles;
enum mediatype extract_type = m_image;
/* ugh. */
pcap_t *pc;
#ifndef NO_DISPLAY_WINDOW
/* PID of display child and file descriptor on pipe to same. */
pid_t dpychld;
int dpychld_fd;
/* display.c */
int dodisplay(int argc, char *argv[]);
#endif /* !NO_DISPLAY_WINDOW */
/* playaudio.c */
void do_mpeg_player(void);
/* clean_temporary_directory:
* Ensure that our temporary directory is clear of any files. */
void clean_temporary_directory(void) {
DIR *d;
/* If in adjunct mode, do not delete any temporary files */
if (adjunct)
return;
/* If tmpdir_specified is true, the user specified a particular temporary
* directory. We presume that the user doesn't want the directory removed
* and that we shouldn't nuke any files in that directory which don't look
* like ours. */
d = opendir(tmpdir);
if (d) {
struct dirent *de;
char *buf;
size_t buflen;
buf = xmalloc(buflen = strlen(tmpdir) + 64);
while ((de = readdir(d))) {
if (!tmpdir_specified || is_driftnet_file(de->d_name)) {
if (buflen < strlen(tmpdir) + strlen(de->d_name) + 1)
buf = xrealloc(buf, buflen = strlen(tmpdir) + strlen(de->d_name) + 64);
sprintf(buf, "%s/%s", tmpdir, de->d_name);
unlink(buf);
}
}
closedir(d);
xfree(buf);
}
if (!tmpdir_specified && rmdir(tmpdir) == -1 && errno != ENOENT) /* lame attempt to avoid race */
fprintf(stderr, PROGNAME": rmdir(%s): %s\n", tmpdir, strerror(errno));
}
/* alloc_connection:
* Find a free slot in which to allocate a connection object. */
connection *alloc_connection(void) {
connection *C;
for (C = slots; C < slots + slotsalloc; ++C) {
if (!*C) return C;
}
/* No connection slots left. */
slots = (connection*)xrealloc(slots, slotsalloc * 2 * sizeof(connection));
memset(slots + slotsalloc, 0, slotsalloc * sizeof(connection));
C = slots + slotsalloc;
slotsalloc *= 2;
return C;
}
/* find_connection:
* Find a connection running between the two named addresses. */
connection *find_connection(const struct in_addr *src, const struct in_addr *dst, const short int sport, const short int dport) {
connection *C;
for (C = slots; C < slots + slotsalloc; ++C) {
connection c = *C;
if (c && c->sport == sport && c->dport == dport
&& memcmp(&(c->src), src, sizeof(struct in_addr)) == 0
&& memcmp(&(c->dst), dst, sizeof(struct in_addr)) == 0)
return C;
}
return NULL;
}
/* sweep_connections:
* Free finished connection slots. */
#define TIMEOUT 5
#define MAXCONNECTIONDATA (8 * 1024 * 1024)
void sweep_connections(void) {
time_t now;
connection *C;
now = time(NULL);
for (C = slots; C < slots + slotsalloc; ++C) {
if (*C) {
connection c = *C;
/* We discard connections which have seen no activity for TIMEOUT
* or for which a FIN has been seen and for which there are no
* gaps in the stream, or where more than MAXCONNECTIONDATA have
* been captured. */
if ((now - c->last) > TIMEOUT
|| (c->fin && (!c->blocks || !c->blocks->next))
|| c->len > MAXCONNECTIONDATA) {
connection_extract_media(c, extract_type);
connection_delete(c);
*C = NULL;
}
}
}
}
/* dump_data:
* Print some binary data on a file descriptor. */
void dump_data(FILE *fp, const unsigned char *data, const unsigned int len) {
const unsigned char *p;
for (p = data; p < data + len; ++p) {
if (isprint((int)*p)) fputc(*p, fp);
else fprintf(fp, "\\x%02x", (unsigned int)*p);
}
}
/* get_link_level_hdr_length:
* Find out how long the link-level header is, based on the datalink layer
* type. This is based on init_linktype in the libpcap distribution; I
* don't know why libpcap doesn't expose the information directly. The
* constants here are taken from 0.6.2, but I've added #ifdefs in the hope
* that it will still compile with earlier versions. */
int get_link_level_hdr_length(int type)
{
switch (type) {
case DLT_EN10MB:
return 14;
case DLT_SLIP:
return 16;
case DLT_SLIP_BSDOS:
return 24;
case DLT_NULL:
#ifdef DLT_LOOP
case DLT_LOOP:
#endif
return 4;
case DLT_PPP:
#ifdef DLT_C_HDLC
case DLT_C_HDLC:
#endif
#ifdef DLT_PPP_SERIAL
case DLT_PPP_SERIAL:
#endif
return 4;
case DLT_PPP_BSDOS:
return 24;
case DLT_FDDI:
return 21;
case DLT_IEEE802:
return 22;
#ifdef DLT_ATM_RFC1483
case DLT_ATM_RFC1483:
return 8;
#endif
#ifdef DLT_PRISM_HEADER
case DLT_PRISM_HEADER:
return 32;
#endif
case DLT_RAW:
return 0;
#ifdef DLT_ATM_CLIP
case DLT_ATM_CLIP: /* Linux ATM defines this */
return 8;
#endif
#ifdef DLT_LINUX_SLL
case DLT_LINUX_SLL: /* fake header for Linux cooked socket */
return 16;
#endif
#ifdef DLT_IEEE802_11 /* 802.11 wireless ethernet */
case DLT_IEEE802_11:
return 32; /* 20030606 email from Nikhil Bobb */ /*44; */
#endif
default:;
}
fprintf(stderr, PROGNAME": unknown data link type %d", type);
exit(1);
}
/* usage:
* Print usage information. */
void usage(FILE *fp) {
fprintf(fp,
"driftnet, version %s\n"
"Capture images from network traffic and display them in an X window.\n"
#ifdef NO_DISPLAY_WINDOW
"\n"
"Actually, this version of driftnet was compiled with the NO_DISPLAY_WINDOW\n"
"option, so that it can only be used in adjunct mode. See below.\n"
#endif /* NO_DISPLAY_WINDOW */
"\n"
"Synopsis: driftnet [options] [filter code]\n"
"\n"
"Options:\n"
"\n"
" -h Display this help message.\n"
" -v Verbose operation.\n"
" -b Beep when a new image is captured.\n"
" -i interface Select the interface on which to listen (default: all\n"
" interfaces).\n"
" -f file Instead of listening on an interface, read captured\n"
" packets from a pcap dump file; file can be a named pipe\n"
" for use with Kismet or similar.\n"
" -p Do not put the listening interface into promiscuous mode.\n"
" -a Adjunct mode: do not display images on screen, but save\n"
" them to a temporary directory and announce their names on\n"
" standard output.\n"
" -m number Maximum number of images to keep in temporary directory\n"
" in adjunct mode.\n"
" -d directory Use the named temporary directory.\n"
" -x prefix Prefix to use when saving images.\n"
" -s Attempt to extract streamed audio data from the network,\n"
" in addition to images. At present this supports MPEG data\n"
" only.\n"
" -S Extract streamed audio but not images.\n"
" -M command Use the given command to play MPEG audio data extracted\n"
" with the -s option; this should process MPEG frames\n"
" supplied on standard input. Default: `mpg123 -'.\n"
"\n"
"Filter code can be specified after any options in the manner of tcpdump(8).\n"
"The filter code will be evaluated as `tcp and (user filter code)'\n"
"\n"
"You can save images to the current directory by clicking on them.\n"
"\n"
"Adjunct mode is designed to be used by other programs which want to use\n"
"driftnet to gather images from the network. With the -m option, driftnet will\n"
"silently drop images if more than the specified number of images are saved\n"
"in its temporary directory. It is assumed that some other process is\n"
"collecting and deleting the image files.\n"
"\n"
"driftnet, copyright (c) 2001-2 Chris Lightfoot <chris@ex-parrot.com>\n"
"home page: http://www.ex-parrot.com/~chris/driftnet/\n"
"\n"
"This program is free software; you can redistribute it and/or modify\n"
"it under the terms of the GNU General Public License as published by\n"
"the Free Software Foundation; either version 2 of the License, or\n"
"(at your option) any later version.\n"
"\n",
DRIFTNET_VERSION);
}
/* terminate_on_signal:
* Terminate on receipt of an appropriate signal. */
sig_atomic_t foad;
void terminate_on_signal(int s) {
extern pid_t mpeg_mgr_pid; /* in playaudio.c */
/* Pass on the signal to the MPEG player manager so that it can abort,
* since it won't die when the pipe into it dies. */
if (mpeg_mgr_pid)
kill(mpeg_mgr_pid, s);
foad = s;
}
/* setup_signals:
* Set up signal handlers. */
void setup_signals(void) {
int *p;
/* Signals to ignore. */
int ignore_signals[] = {SIGPIPE, 0};
/* Signals which mean we should quit, killing the display child if
* applicable. */
int terminate_signals[] = {SIGTERM, SIGINT, /*SIGSEGV,*/ SIGBUS, SIGCHLD, 0};
struct sigaction sa;
sa.sa_flags = SA_RESTART | SA_NOCLDSTOP;
for (p = ignore_signals; *p; ++p) {
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_IGN;
sigaction(*p, &sa, NULL);
}
for (p = terminate_signals; *p; ++p) {
memset(&sa, 0, sizeof(sa));
sa.sa_handler = terminate_on_signal;
sigaction(*p, &sa, NULL);
}
}
/* connection_string:
* Return a string of the form w.x.y.z:foo -> a.b.c.d:bar for a pair of
* addresses and ports. */
char *connection_string(const struct in_addr s, const unsigned short s_port, const struct in_addr d, const unsigned short d_port) {
static char buf[50] = {0};
sprintf(buf, "%s:%d -> ", inet_ntoa(s), (int)s_port);
sprintf(buf + strlen(buf), "%s:%d", inet_ntoa(d), (int)d_port);
return buf;
}
/* process_packet:
* Callback which processes a packet captured by libpcap. */
int pkt_offset; /* offset of IP packet within wire packet */
void process_packet(u_char *user, const struct pcap_pkthdr *hdr, const u_char *pkt) {
struct ip ip;
struct tcphdr tcp;
struct in_addr s, d;
int off, len, delta;
connection *C, c;
if (verbose)
fprintf(stderr, ".");
memcpy(&ip, pkt + pkt_offset, sizeof(ip));
memcpy(&s, &ip.ip_src, sizeof(ip.ip_src));
memcpy(&d, &ip.ip_dst, sizeof(ip.ip_dst));
memcpy(&tcp, pkt + pkt_offset + (ip.ip_hl << 2), sizeof(tcp));
off = pkt_offset + (ip.ip_hl << 2) + (tcp.th_off << 2);
len = hdr->caplen - off;
/* XXX fragmented packets and other nasties. */
/* try to find the connection slot associated with this. */
C = find_connection(&s, &d, ntohs(tcp.th_sport), ntohs(tcp.th_dport));
/* no connection at all, so we need to allocate one. */
if (!C) {
if (verbose)
fprintf(stderr, PROGNAME": new connection: %s\n", connection_string(s, ntohs(tcp.th_sport), d, ntohs(tcp.th_dport)));
C = alloc_connection();
*C = connection_new(&s, &d, ntohs(tcp.th_sport), ntohs(tcp.th_dport));
/* This might or might not be an entirely new connection (SYN flag
* set). Either way we need a sequence number to start at. */
(*C)->isn = ntohl(tcp.th_seq);
}
/* Now we need to process this segment. */
c = *C;
delta = 0;/*tcp.syn ? 1 : 0;*/
/* NB (STD0007):
* SEG.LEN = the number of octets occupied by the data in the
* segment (counting SYN and FIN) */
#if 0
if (tcp.syn)
/* getting a new isn. */
c->isn = htonl(tcp.seq);
#endif
if (tcp.th_flags & TH_RST) {
/* Looks like this connection is bogus, and so might be a
* connection going the other way. */
if (verbose)
fprintf(stderr, PROGNAME": connection reset: %s\n", connection_string(s, ntohs(tcp.th_sport), d, ntohs(tcp.th_dport)));
connection_delete(c);
*C = NULL;
if ((C = find_connection(&d, &s, ntohs(tcp.th_dport), ntohs(tcp.th_sport)))) {
connection_delete(*C);
*C = NULL;
}
return;
}
if (len > 0) {
/* We have some data in the packet. If this data occurred after
* the first data we collected for this connection, then save it
* so that we can look for images. Otherwise, discard it. */
unsigned int offset;
offset = ntohl(tcp.th_seq);
/* Modulo 2**32 arithmetic; offset = seq - isn + delta. */
if (offset < (c->isn + delta))
offset = 0xffffffff - (c->isn + delta - offset);
else
offset -= c->isn + delta;
if (offset > c->len + WRAPLEN) {
/* Out-of-order packet. */
if (verbose)
fprintf(stderr, PROGNAME": out of order packet: %s\n", connection_string(s, ntohs(tcp.th_sport), d, ntohs(tcp.th_dport)));
} else {
connection_push(c, pkt + off, offset, len);
connection_extract_media(c, extract_type);
}
}
if (tcp.th_flags & TH_FIN) {
/* Connection closing; mark it as closed, but let sweep_connections
* free it if appropriate. */
if (verbose)
fprintf(stderr, PROGNAME": connection closing: %s, %d bytes transferred\n", connection_string(s, ntohs(tcp.th_sport), d, ntohs(tcp.th_dport)), c->len);
c->fin = 1;
}
/* sweep out old connections */
sweep_connections();
}
/* packet_capture_thread:
* Thread in which packet capture runs. */
void *packet_capture_thread(void *v) {
while (!foad)
pcap_dispatch(pc, -1, process_packet, NULL);
return NULL;
}
/* main:
* Entry point. Process command line options, start up pcap and enter capture
* loop. */
char optstring[] = "abd:f:hi:M:m:pSsvx:";
int main(int argc, char *argv[]) {
char *interface = NULL, *filterexpr;
int promisc = 1;
struct bpf_program filter;
char ebuf[PCAP_ERRBUF_SIZE];
int c;
#ifndef NO_DISPLAY_WINDOW
extern char *savedimgpfx; /* in display.c */
#endif
extern char *audio_mpeg_player; /* in playaudio.c */
int newpfx = 0;
int mpeg_player_specified = 0;
char *dumpfile = NULL;
pthread_t packetth;
connection *C;
/* Handle command-line options. */
opterr = 0;
while ((c = getopt(argc, argv, optstring)) != -1) {
switch(c) {
case 'h':
usage(stdout);
return 0;
case 'i':
if (dumpfile) {
fprintf(stderr, PROGNAME": can't specify -i and -f\n");
return -1;
}
interface = optarg;
break;
case 'v':
verbose = 1;
break;
case 'b':
if (!isatty(1))
fprintf(stderr, PROGNAME": can't beep unless standard output is a terminal\n");
else
beep = 1;
break;
case 'p':
promisc = 0;
break;
case 's':
extract_type |= m_audio;
break;
case 'S':
extract_type = m_audio;
break;
case 'M':
audio_mpeg_player = optarg;
mpeg_player_specified = 1;
break;
case 'a':
adjunct = 1;
break;
case 'm':
max_tmpfiles = atoi(optarg);
if (max_tmpfiles <= 0) {
fprintf(stderr, PROGNAME": `%s' does not make sense for -m\n", optarg);
return -1;
}
break;
case 'd':
tmpdir = optarg;
tmpdir_specified = 1; /* so we don't delete it. */
break;
case 'f':
if (interface) {
fprintf(stderr, PROGNAME": can't specify -i and -f\n");
return -1;
}
dumpfile = optarg;
break;
#ifndef NO_DISPLAY_WINDOW
case 'x':
savedimgpfx = optarg;
newpfx = 1;
break;
#endif
case '?':
default:
if (strchr(optstring, optopt))
fprintf(stderr, PROGNAME": option -%c requires an argument\n", optopt);
else
fprintf(stderr, PROGNAME": unrecognised option -%c\n", optopt);
usage(stderr);
return 1;
}
}
#ifdef NO_DISPLAY_WINDOW
if (!adjunct) {
fprintf(stderr, PROGNAME": this version of driftnet was compiled without display support\n");
fprintf(stderr, PROGNAME": use the -a option to run it in adjunct mode\n");
return -1;
}
#endif /* !NO_DISPLAY_WINDOW */
/* Let's not be too fascist about option checking.... */
if (max_tmpfiles && !adjunct) {
fprintf(stderr, PROGNAME": warning: -m only makes sense with -a\n");
max_tmpfiles = 0;
}
if (adjunct && newpfx)
fprintf(stderr, PROGNAME": warning: -x ignored -a\n");
if (mpeg_player_specified && !(extract_type & m_audio))
fprintf(stderr, PROGNAME": warning: -M only makes sense with -s\n");
if (mpeg_player_specified && adjunct)
fprintf(stderr, PROGNAME": warning: -M ignored with -a\n");
if (max_tmpfiles && adjunct && verbose)
fprintf(stderr, PROGNAME": a maximum of %d images will be buffered\n", max_tmpfiles);
if (beep && adjunct)
fprintf(stderr, PROGNAME": can't beep in adjunct mode\n");
/* In adjunct mode, it's important that the attached program gets
* notification of images in a timely manner. Make stdout line-buffered
* for this reason. */
if (adjunct)
setvbuf(stdout, NULL, _IOLBF, 0);
/* If a directory name has not been specified, then we need to create one.
* Otherwise, check that it's a directory into which we may write files. */
if (tmpdir) {
struct stat st;
if (stat(tmpdir, &st) == -1) {
fprintf(stderr, PROGNAME": stat(%s): %s\n", tmpdir, strerror(errno));
return -1;
} else if (!S_ISDIR(st.st_mode)) {
fprintf(stderr, PROGNAME": %s: not a directory\n", tmpdir);
return -1;
} else if (access(tmpdir, R_OK | W_OK) != 0) { /* access is unsafe but we don't really care. */
fprintf(stderr, PROGNAME": %s: %s\n", tmpdir, strerror(errno));
return -1;
}
} else {
/* need to make a temporary directory. */
char *tmp;
char template[PATH_MAX+11];
if (!(tmp = getenv("TMPDIR")))
if (!(tmp = getenv("TEMP")))
if (!(tmp = getenv("TMP")))
tmp = "/tmp";
snprintf(template, PATH_MAX+11, "%s/drifnet-XXXXXX", tmp);
tmpdir = mkdtemp(template);
if (!tmpdir) {
perror(PROGNAME": mkdtemp");
return -1;
}
}
if (verbose)
fprintf(stderr, PROGNAME": using temporary file directory %s\n", tmpdir);
if (!interface && !(interface = pcap_lookupdev(ebuf))) {
fprintf(stderr, PROGNAME": pcap_lookupdev: %s\n", ebuf);
fprintf(stderr, PROGNAME": try specifying an interface with -i\n");
return -1;
}
if (verbose)
fprintf(stderr, PROGNAME": listening on %s%s\n", interface ? interface : "all interfaces", promisc ? " in promiscuous mode" : "");
/* Build up filter. */
if (optind < argc) {
if (dumpfile)
fprintf(stderr, PROGNAME": filter code ignored with dump file\n");
else {
char **a;
int l;
for (a = argv + optind, l = sizeof("tcp and ()"); *a; l += strlen(*a) + 1, ++a);
filterexpr = calloc(l, 1);
strcpy(filterexpr, "tcp and (");
for (a = argv + optind; *a; ++a) {
strcat(filterexpr, *a);
if (*(a + 1)) strcat(filterexpr, " ");
}
strcat(filterexpr, ")");
}
} else filterexpr = "tcp";
if (verbose)
fprintf(stderr, PROGNAME": using filter expression `%s'\n", filterexpr);
#ifndef NO_DISPLAY_WINDOW
if (verbose && newpfx && !adjunct)
fprintf(stderr, PROGNAME": using saved image prefix `%s'\n", savedimgpfx);
#endif
setup_signals();
/* Start up the audio player, if required. */
if (!adjunct && (extract_type & m_audio))
do_mpeg_player();
#ifndef NO_DISPLAY_WINDOW
/* Possibly fork to start the display child process */
if (!adjunct && (extract_type & m_image)) {
int pfd[2];
pipe(pfd);
switch (dpychld = fork()) {
case 0:
/* we are the child */
close(pfd[1]);
dpychld_fd = pfd[0];
dodisplay(argc, argv);
return -1;
case -1:
perror(PROGNAME": fork");
return -1;
default:
close(pfd[0]);
dpychld_fd = pfd[1];
if (verbose)
fprintf(stderr, PROGNAME": started display child, pid %d\n", (int)dpychld);
break;
}
} else if (verbose)
fprintf(stderr, PROGNAME": operating in adjunct mode\n");
#endif /* !NO_DISPLAY_WINDOW */
/* Start up pcap. */
if (dumpfile) {
if (!(pc = pcap_open_offline(dumpfile, ebuf))) {
fprintf(stderr, PROGNAME": pcap_open_offline: %s\n", ebuf);
return -1;
}
} else {
if (!(pc = pcap_open_live(interface, SNAPLEN, promisc, 1000, ebuf))) {
fprintf(stderr, PROGNAME": pcap_open_live: %s\n", ebuf);
if (getuid() != 0)
fprintf(stderr, PROGNAME": perhaps you need to be root?\n");
else if (!interface)
fprintf(stderr, PROGNAME": perhaps try selecting an interface with the -i option?\n");
return -1;
}
/* Only apply a filter to live packets. Is this right? */
if (pcap_compile(pc, &filter, (char*)filterexpr, 1, 0) == -1) {
fprintf(stderr, PROGNAME": pcap_compile: %s\n", pcap_geterr(pc));
return -1;
}
if (pcap_setfilter(pc, &filter) == -1) {
fprintf(stderr, PROGNAME": pcap_setfilter: %s\n", pcap_geterr(pc));
return -1;
}
}
/* Figure out the offset from the start of a returned packet to the data in
* it. */
pkt_offset = get_link_level_hdr_length(pcap_datalink(pc));
if (verbose)
fprintf(stderr, PROGNAME": link-level header length is %d bytes\n", pkt_offset);
slotsused = 0;
slotsalloc = 64;
slots = (connection*)xcalloc(slotsalloc, sizeof(connection));
/* Actually start the capture stuff up. Unfortunately, on many platforms,
* libpcap doesn't have read timeouts, so we start the thing up in a
* separate thread. Yay! */
pthread_create(&packetth, NULL, packet_capture_thread, NULL);
while (!foad)
sleep(1);
if (verbose) {
if (foad == SIGCHLD) {
pid_t pp;
int st;
while ((pp = waitpid(-1, &st, WNOHANG)) > 0) {
if (WIFEXITED(st))
fprintf(stderr, PROGNAME": child process %d exited with status %d\n", (int)pp, WEXITSTATUS(st));
else if (WIFSIGNALED(st))
fprintf(stderr, PROGNAME": child process %d killed by signal %d\n", (int)pp, WTERMSIG(st));
else
fprintf(stderr, PROGNAME": child process %d died, not sure why\n", (int)pp);
}
} else
fprintf(stderr, PROGNAME": caught signal %d\n", foad);
}
pthread_cancel(packetth); /* make sure thread quits even if it's stuck in pcap_dispatch */
pthread_join(packetth, NULL);
/* Clean up. */
/* pcap_freecode(pc, &filter);*/ /* not on some systems... */
pcap_close(pc);
clean_temporary_directory();
/* Easier for memory-leak debugging if we deallocate all this here.... */
for (C = slots; C < slots + slotsalloc; ++C)
if (*C) connection_delete(*C);
xfree(slots);
// if (!tmpdir_specified)
// xfree(tmpdir);
return 0;
}