head 1.10; access; symbols; locks; strict; comment @ * @; 1.10 date 2011.01.21.17.17.17; author wessels; state Exp; branches; next 1.9; 1.9 date 2010.01.28.17.02.01; author wessels; state Exp; branches; next 1.8; 1.8 date 2010.01.28.17.01.42; author wessels; state Exp; branches; next 1.7; 1.7 date 2010.01.28.17.01.02; author wessels; state Exp; branches; next 1.6; 1.6 date 2010.01.28.17.00.32; author wessels; state Exp; branches; next 1.5; 1.5 date 2010.01.28.17.00.17; author wessels; state Exp; branches; next 1.4; 1.4 date 2010.01.28.16.55.18; author wessels; state Exp; branches; next 1.3; 1.3 date 2009.11.09.20.49.27; author wessels; state Exp; branches; next 1.2; 1.2 date 2009.08.11.00.11.22; author wessels; state Exp; branches; next 1.1; 1.1 date 2008.04.21.23.39.06; author wessels; state Exp; branches; next ; desc @@ 1.10 log @ignore tcpdump file open errors if we are reading gzip files @ text @#include #include #include #include #include #include #include #include #include #include #include #include #include pcap_dumper_t *out = NULL; unsigned int fifocount = 0; void join(const char *pcapfile) { pcap_t *in = NULL; char errbuf[PCAP_ERRBUF_SIZE + 1]; struct pcap_pkthdr hdr; const u_char *data; char fifoname[256]; int waitstatus; const char *readfile = pcapfile; fifoname[0] = '\0'; if (0 == strcmp(pcapfile + strlen(pcapfile) - 3, ".gz")) { snprintf(fifoname, 256, "/tmp/fifo.%d.%u", getpid(), fifocount++); mkfifo(fifoname, 0600); if (0 == fork()) { close(1); open(fifoname, O_WRONLY); execl("/usr/bin/gzip", "/usr/bin/gzip", "-dc", pcapfile, NULL); perror("gzip"); abort(); } readfile = fifoname; } in = pcap_open_offline(readfile, errbuf); if (fifoname[0]) unlink(fifoname); if (NULL == in && fifoname[0]) { waitpid(-1, &waitstatus, 0); return; } if (NULL == in) { fprintf(stderr, "%s: %s", pcapfile, errbuf); exit(1); } while ((data = pcap_next(in, &hdr))) { if (!out) { out = pcap_dump_open(in, "-"); if (NULL == out) { perror("stdout"); exit(1); } } pcap_dump((void *)out, &hdr, data); } pcap_close(in); waitpid(-1, &waitstatus, 0); } int main(int argc, char *argv[]) { int i; if (argc > 1 && 0 == strcmp(argv[1], "-h")) { fprintf(stderr, "usage: tcpdump-join pcapfiles ...\n tcpdump-join directory\n tcpdump-join\t(read files from stdin)\n"); exit(1); } if (1 == argc) { char buf[512]; /* read file names from stdin */ while (NULL != fgets(buf, 512, stdin)) { strtok(buf, "\r\n"); join(buf); } } else for (i = 1; i < argc; i++) { struct stat sb; if (stat(argv[i], &sb) < 0) err(1, argv[i]); if (S_ISDIR(sb.st_mode)) { DIR *d = opendir(argv[i]); struct dirent *e; if (NULL == d) err(1, argv[i]); while (NULL != (e = readdir(d))) { char path[512]; if (*e->d_name == '.') continue; snprintf(path, sizeof(path), "%s/%s", argv[i], e->d_name); join(path); } closedir(d); } else { join(argv[i]); } } if (out) pcap_dump_close(out); exit(0); } @ 1.9 log @*** empty log message *** @ text @d44 4 @ 1.8 log @*** empty log message *** @ text @d67 1 a67 1 fprintf(stderr, "usage: tcpdump-join pcapfiles ...\n tcpdump-join directory\n tcpdump-join\n"); @ 1.7 log @*** empty log message *** @ text @d67 1 a67 1 fprintf(stderr, "usage: tcpdump-join pcapfiles ...\n"); @ 1.6 log @fix usage/help @ text @d67 1 a67 1 fprintf(stderr, "usage: tcpdump-join pcapfiles ..."); @ 1.5 log @fix uage @ text @d66 1 a66 1 if (argc > 1 && 0 == strcmp(argv[1], "-h)) { @ 1.4 log @new feature: read filenames on stdin @ text @d66 1 a66 1 if (argc < 2) { @ 1.3 log @*** empty log message *** @ text @d70 8 a77 1 for (i = 1; i < argc; i++) { @ 1.2 log @make tcpdump-join.c use readdir() to solve "arument list too long" problemss. @ text @d80 1 d83 2 a84 1 join(e->d_name); @ 1.1 log @Initial revision @ text @d13 1 d15 5 a19 2 int main(int argc, char *argv[]) a21 1 pcap_dumper_t *out = NULL; a24 1 int i; d26 35 d62 4 d71 12 a82 12 char *inname = argv[i]; fifoname[0] = '\0'; int waitstatus; if (0 == strcmp(inname+strlen(inname)-3, ".gz")) { snprintf(fifoname, 256, "/tmp/fifo.%d.%d", getpid(), i); mkfifo(fifoname, 0600); if (0 == fork()) { close(1); open(fifoname, O_WRONLY); execl("/usr/bin/gzip", "/usr/bin/gzip", "-dc", inname, NULL); perror("gzip"); abort(); d84 3 a86 18 inname = fifoname; } in = pcap_open_offline(inname, errbuf); if (fifoname[0]) unlink(fifoname); if (NULL == in) { fprintf(stderr, "%s: %s", argv[i], errbuf); exit(1); } while ((data = pcap_next(in, &hdr))) { if (!out) { out = pcap_dump_open(in, "-"); if (NULL == out) { perror("stdout"); exit(1); } } pcap_dump((void *)out, &hdr, data); a87 2 pcap_close(in); waitpid(-1, &waitstatus, 0); @