pcap_writer.h (tcpflow-1.5.0) | : | pcap_writer.h (tcpflow-1.6.1) | ||
---|---|---|---|---|
skipping to change at line 40 | skipping to change at line 40 | |||
if (count != 2) throw new write_error(); | if (count != 2) throw new write_error(); | |||
} | } | |||
void write4(const uint32_t val) { | void write4(const uint32_t val) { | |||
size_t count = fwrite(&val,1,4,fcap); | size_t count = fwrite(&val,1,4,fcap); | |||
if (count != 4) throw new write_error(); | if (count != 4) throw new write_error(); | |||
} | } | |||
void open(const std::string &fname) { | void open(const std::string &fname) { | |||
fcap = fopen(fname.c_str(),"wb"); // write the output | fcap = fopen(fname.c_str(),"wb"); // write the output | |||
if(fcap==0) throw new write_error(); | if(fcap==0) throw new write_error(); | |||
} | } | |||
void write_header(){ | void write_header(const int pcap_dlt){ | |||
write4(0xa1b2c3d4); | write4(0xa1b2c3d4); | |||
write2(2); // major version number | write2(2); // major version number | |||
write2(4); // minor version number | write2(4); // minor version number | |||
write4(0); // time zone offset; always 0 | write4(0); // time zone offset; always 0 | |||
write4(0); // accuracy of time stamps in the file; a lways 0 | write4(0); // accuracy of time stamps in the file; a lways 0 | |||
write4(PCAP_MAX_PKT_LEN); // snapshot length | write4(PCAP_MAX_PKT_LEN); // snapshot length | |||
write4(DLT_EN10MB); // link layer encapsulation | write4(pcap_dlt); // link layer encapsulation | |||
} | } | |||
void copy_header(const std::string &ifname){ | void copy_header(const std::string &ifname){ | |||
/* assert byte order is correct */ | /* assert byte order is correct */ | |||
FILE *f2 = fopen(ifname.c_str(),"rb"); | FILE *f2 = fopen(ifname.c_str(),"rb"); | |||
if(f2==0) throw new write_error(); | if(f2==0) throw new write_error(); | |||
u_char buf[PCAP_HEADER_SIZE]; | u_char buf[PCAP_HEADER_SIZE]; | |||
if(fread(buf,1,sizeof(buf),f2)!=sizeof(buf)) throw new write_error(); | if(fread(buf,1,sizeof(buf),f2)!=sizeof(buf)) throw new write_error(); | |||
if((buf[0]!=0xd4) || (buf[1]!=0xc3) || (buf[2]!=0xb2) || (buf[3]!=0xa1)) { | if((buf[0]!=0xd4) || (buf[1]!=0xc3) || (buf[2]!=0xb2) || (buf[3]!=0xa1)) { | |||
std::cout << "pcap file " << ifname << " is in wrong byte order. Can not continue.\n"; | std::cout << "pcap file " << ifname << " is in wrong byte order. Can not continue.\n"; | |||
throw new write_error(); | throw new write_error(); | |||
} | } | |||
if(fwrite(buf,1,sizeof(buf),fcap)!=sizeof(buf)) throw new write_error(); | if(fwrite(buf,1,sizeof(buf),fcap)!=sizeof(buf)) throw new write_error(); | |||
if(fclose(f2)!=0) throw new write_error(); | if(fclose(f2)!=0) throw new write_error(); | |||
} | } | |||
pcap_writer():fcap(0){} | ||||
public: | public: | |||
pcap_writer():fcap(0){} | ||||
static pcap_writer *open_new(const std::string &ofname){ | static pcap_writer *open_new(const std::string &ofname){ | |||
pcap_writer *pcw = new pcap_writer(); | pcap_writer *pcw = new pcap_writer(); | |||
pcw->open(ofname); | pcw->open(ofname); | |||
pcw->write_header(); | pcw->write_header(DLT_EN10MB); // static for temporary regression | |||
return pcw; | return pcw; | |||
} | } | |||
static pcap_writer *open_copy(const std::string &ofname,const std::string &i fname){ | static pcap_writer *open_copy(const std::string &ofname,const std::string &i fname){ | |||
pcap_writer *pcw = new pcap_writer(); | pcap_writer *pcw = new pcap_writer(); | |||
pcw->open(ofname); | pcw->open(ofname); | |||
pcw->copy_header(ifname); | pcw->copy_header(ifname); | |||
return pcw; | return pcw; | |||
} | } | |||
virtual ~pcap_writer(){ | virtual ~pcap_writer(){ | |||
if(fcap) fclose(fcap); | if(fcap) fclose(fcap); | |||
} | } | |||
void writepkt(const struct pcap_pkthdr *h,const u_char *p) { | void writepkt(const struct pcap_pkthdr *h,const u_char *p) { | |||
/* Write a packet */ | /* Write a packet */ | |||
write4(h->ts.tv_sec); // time stamp, seconds avalue | write4(h->ts.tv_sec); // time stamp, seconds avalue | |||
write4(h->ts.tv_usec); // time stamp, microseconds | write4(h->ts.tv_usec); // time stamp, microseconds | |||
write4(h->caplen); | write4(h->caplen); | |||
write4(h->len); | write4(h->len); | |||
size_t count = fwrite(p,1,h->caplen,fcap); // the packet | size_t count = fwrite(p,1,h->caplen,fcap); // the packet | |||
if(count!=h->caplen) throw new write_error(); | if(count!=h->caplen) throw new write_error(); | |||
} | } | |||
void refresh_sink(const std::string &fname, const int pcap_dlt) { | ||||
open(fname); | ||||
write_header(pcap_dlt); | ||||
} | ||||
void update_sink(FILE *sink) { | ||||
fcap = sink; | ||||
} | ||||
FILE* yield_sink() { | ||||
return fcap; | ||||
} | ||||
}; | }; | |||
#endif | #endif | |||
End of changes. 6 change blocks. | ||||
4 lines changed or deleted | 15 lines changed or added |