| 1 |
/* |
| 2 |
* Copyright (c) 2006 dustin sallings |
| 3 |
*/ |
| 4 |
|
| 5 |
#include <stdlib.h> |
| 6 |
#include <stdio.h> |
| 7 |
#include <unistd.h> |
| 8 |
#include <string.h> |
| 9 |
#include <ctype.h> |
| 10 |
#include <assert.h> |
| 11 |
/* |
| 12 |
#include <getopt.h> |
| 13 |
*/ |
| 14 |
#include "mymalloc.h" |
| 15 |
#include "multisniff.h" |
| 16 |
|
| 17 |
void |
| 18 |
usage(char *name) |
| 19 |
{ |
| 20 |
fprintf(stderr, "Usage: %s -i <intf> " |
| 21 |
#ifdef HAVE_PCAP_DUMP_FLUSH |
| 22 |
"[-f] " |
| 23 |
#endif |
| 24 |
"[-p] [-d <outdir>] " |
| 25 |
"[-m seconds] [-c seconds] [-F <filterfile>] [<filter>]\n", |
| 26 |
name); |
| 27 |
fprintf(stderr, " -i specifies the interface to sniff (required).\n"); |
| 28 |
fprintf(stderr, " -d specifies the output directory.\n"); |
| 29 |
#ifdef HAVE_PCAP_DUMP_FLUSH |
| 30 |
fprintf(stderr, " -F get a filter from a file.\n"); |
| 31 |
#endif |
| 32 |
fprintf(stderr, " -f flush pcap files on each cleanup run.\n"); |
| 33 |
fprintf(stderr, " -p turns on promiscious sniffing.\n"); |
| 34 |
fprintf(stderr, " -m maximum age before closing file [60s].\n"); |
| 35 |
fprintf(stderr, " -c status update/flush frequency [5s].\n"); |
| 36 |
fprintf(stderr, " <filter> pcap filter expression.\n"); |
| 37 |
exit(1); |
| 38 |
} |
| 39 |
|
| 40 |
static char * |
| 41 |
readFile(const char *filename) |
| 42 |
{ |
| 43 |
FILE *in=NULL; |
| 44 |
char *rv=NULL; |
| 45 |
int rvsize=128; |
| 46 |
char buf[1024]; |
| 47 |
|
| 48 |
in=fopen(filename, "r"); |
| 49 |
if(in == NULL) { |
| 50 |
perror("fopen"); |
| 51 |
exit(1); |
| 52 |
} |
| 53 |
|
| 54 |
rv=calloc(1, rvsize); |
| 55 |
assert(rv); |
| 56 |
while(fgets(buf, sizeof(buf), in) != NULL) { |
| 57 |
if(strlen(rv) + strlen(buf) > rvsize) { |
| 58 |
rv=realloc(rv, rvsize+=(strlen(buf)+1)); |
| 59 |
assert(rv); |
| 60 |
} |
| 61 |
strcat(rv, buf); |
| 62 |
} |
| 63 |
|
| 64 |
/* Strip off the trailing whitespace */ |
| 65 |
while(strlen(rv) > 0 && isspace(rv[strlen(rv)-1])) { |
| 66 |
rv[strlen(rv)-1]=0x00; |
| 67 |
} |
| 68 |
return rv; |
| 69 |
} |
| 70 |
|
| 71 |
int |
| 72 |
main(int argc, char **argv) |
| 73 |
{ |
| 74 |
int flags = 0; |
| 75 |
int c = 0; |
| 76 |
extern char *optarg; |
| 77 |
char *filter = NULL; |
| 78 |
char *outdir = "."; |
| 79 |
char *intf = NULL; |
| 80 |
struct cleanupConfig conf; |
| 81 |
|
| 82 |
conf.maxAge=DEFAULT_MAX_PKT_AGE; |
| 83 |
conf.refreshTime=DEFAULT_CLEANUP_INTERVAL; |
| 84 |
|
| 85 |
while ((c = getopt(argc, argv, "fpi:d:F:m:c:")) != -1) { |
| 86 |
switch (c) { |
| 87 |
case 'f': |
| 88 |
flags |= FLAG_BIT(FLAG_FLUSH); |
| 89 |
break; |
| 90 |
case 'p': |
| 91 |
flags |= FLAG_BIT(FLAG_PROMISC); |
| 92 |
break; |
| 93 |
case 'F': |
| 94 |
filter = readFile(optarg); |
| 95 |
break; |
| 96 |
case 'd': |
| 97 |
outdir = strdup(optarg); |
| 98 |
break; |
| 99 |
case 'i': |
| 100 |
intf = strdup(optarg); |
| 101 |
break; |
| 102 |
case 'm': |
| 103 |
conf.maxAge=atoi(optarg); |
| 104 |
break; |
| 105 |
case 'c': |
| 106 |
conf.refreshTime=atoi(optarg); |
| 107 |
break; |
| 108 |
default: |
| 109 |
usage(argv[0]); |
| 110 |
exit(-1); |
| 111 |
break; /* not reached */ |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
if (optind >= argc) { |
| 116 |
if(filter == NULL) { |
| 117 |
filter = ""; |
| 118 |
} |
| 119 |
} else { |
| 120 |
int i=0; |
| 121 |
int size=0; |
| 122 |
for(i=optind; i<argc; i++) { |
| 123 |
size+=strlen(argv[i]); |
| 124 |
size+=1; |
| 125 |
} |
| 126 |
size+=1; |
| 127 |
filter=calloc(1, size); |
| 128 |
assert(filter); |
| 129 |
for(i=optind; i<argc; i++) { |
| 130 |
strcat(filter, argv[i]); |
| 131 |
strcat(filter, " "); |
| 132 |
assert(strlen(filter) < size); |
| 133 |
} |
| 134 |
/* Trim the trailing space */ |
| 135 |
assert(filter[strlen(filter)-1] == ' '); |
| 136 |
filter[strlen(filter)-1]=0x00; |
| 137 |
} |
| 138 |
|
| 139 |
if (intf == NULL) { |
| 140 |
fprintf(stderr, "Must supply an interface\n"); |
| 141 |
usage(argv[0]); |
| 142 |
} |
| 143 |
process(flags, intf, conf, outdir, filter); |
| 144 |
return (0); |
| 145 |
} |