24#pragma GCC diagnostic ignored "-Weffc++"
25#pragma GCC diagnostic ignored "-Wshadow"
36 demux(demux_),myflow(flow_),dir(unknown),isn(isn_),nsn(0),
37 syn_count(0),fin_count(0),fin_size(0),pos(0),
38 flow_pathname(),fd(-1),file_created(false),
39 flow_index_pathname(),idx_file(),
40 seen(new recon_set()),
42 last_packet_number(),out_of_order_count(0),violations(0)
56 for(recon_set::const_iterator
it =
seen->begin();
it!=
seen->end();
it++){
57 std::cerr << *
it <<
", ";
59 std::cerr << std::endl;
65 static const std::string fileobject_str(
"fileobject");
66 static const std::string filesize_str(
"filesize");
67 static const std::string filename_str(
"filename");
68 static const std::string tcpflow_str(
"tcpflow");
75 std::stringstream attrs;
76 attrs <<
"startime='" << dfxml_writer::to8601(
myflow.
tstart) <<
"' ";
77 attrs <<
"endtime='" << dfxml_writer::to8601(
myflow.
tlast) <<
"' ";
90 xreport->xmlout(tcpflow_str,
"",attrs.str(),
false);
91 if(xmladd.size()>0)
xreport->xmlout(
"",xmladd,
"",
false);
109#pragma GCC diagnostic warning "-Weffc++"
110#pragma GCC diagnostic warning "-Wshadow"
131 struct timeval times[2];
137#if defined(HAVE_FUTIMES)
139 for (
int i=0; i<2; i++){
140 if ( times[i].tv_usec < 0 || times[i].tv_usec >= 1000000 ){
141 times[i].tv_usec = 0;
144 if (futimes(
fd,times)){
145 fprintf(stderr,
"%s: futimes(fd=%d,[%ld:%ld,%ld:%ld])\n",
147 times[0].tv_sec,times[1].tv_usec,
148 times[1].tv_sec,times[1].tv_usec);
150#elif defined(HAVE_FUTIMENS)
151 struct timespec tstimes[2];
152 for(
int i=0;i<2;i++){
153 tstimes[i].tv_sec = times[i].tv_sec;
154 tstimes[i].tv_nsec = times[i].tv_usec * 1000;
156 if(futimens(
fd,tstimes)){
157 perror(
"futimens(fd=%d)",
fd);
179 int create_idx_needed =
false;
186 create_idx_needed =
true;
215 if(create_idx_needed){
246 const char *color[3] = {
"\033[0;32m",
"\033[0;34m",
"\033[0;31m" };
251 if(
last_byte > max_bytes_per_flow)
return;
252 if(length > max_bytes_per_flow -
last_byte){
254 if(length==0)
return;
260 if(sem_wait(semlock)){
261 fprintf(stderr,
"%s: attempt to acquire semaphore failed: %s\n",
progname,strerror(errno));
276 const size_t bytes_per_line = 32;
277 size_t max_spaces = 0;
278 for(u_int i=0;i<length;i+=bytes_per_line){
283 size_t count = snprintf(b,
sizeof(b),
"%04x: ",(
int)i);
284 if(fwrite(b,1,count,stdout)!=count){
290 for(
size_t j=0;j<bytes_per_line && i+j<length ;j++){
291 unsigned char ch = data[i+j];
292 fprintf(stdout,
"%02x",ch); spaces += 2;
299 if(spaces>max_spaces) max_spaces=spaces;
300 for(;spaces<max_spaces;spaces++){
305 for(
size_t j=0;j<bytes_per_line && i+j<length;j++){
306 unsigned char ch = data[i+j];
307 if(ch>=
' ' && ch<=
'~') fputc(ch,stdout);
308 else fputc(
'.',stdout);
322 std::string hoststr = std::string();
325 printf(
"\"src_host\":\"");
328 size_t src_end_pos = 0;
329 size_t src_pos_counter = 0;
332 for(
size_t i = 0; i < pathname_len; ++i) {
335 printf(
"%d%s", atoi(hoststr.c_str()), (src_pos_counter != 4 ?
"." :
""));
340 if(src_pos_counter == 4) {
345 src_end_pos = src_pos;
346 for(;src_end_pos < pathname_len; ++src_end_pos) {
351 printf(
"\",\"src_port\":%d,\"dst_host\":\"", atoi(
flow_pathname.substr(src_pos + 1, src_end_pos - src_pos).c_str()));
353 size_t dst_pos = src_end_pos + 1;
354 size_t dst_end_pos = dst_pos;
355 size_t dst_pos_counter = 0;
357 for(
size_t i = dst_pos; i < pathname_len; ++i) {
360 printf(
"%d%s", atoi(hoststr.c_str()), (dst_pos_counter != 4 ?
"." :
""));
365 if(dst_pos_counter == 4) {
370 dst_end_pos = dst_pos;
371 for(;dst_end_pos < pathname_len; ++dst_end_pos) {
376 printf(
"\",\"dst_port\":%d,\"payload\": [", atoi(
flow_pathname.substr(dst_pos + 1, dst_end_pos - dst_pos).c_str()));
378 for(
size_t i = 0; i < length; ++i) {
379 printf(
"%d%s", data[i], (i != length - 1 ?
"," :
"]}"));
382 for(
const u_char *cc = data;cc<data+length;cc++){
383 if(isprint(*cc) || (*cc==
'\n') || (*cc==
'\r')){
384 int ret = fputc(*cc,stdout);
386 std::cerr <<
"EOF on write to stdout\n";
391 else fputc(
'.',stdout);
396 written = fwrite(data,1,length,stdout);
397 if(length != written) std::cerr <<
"\nwrite error to stdout (" << length <<
"!=" << written <<
") \n";
409 if(sem_post(semlock)){
410 fprintf(stderr,
"%s: attempt to post semaphore failed: %s\n",
progname,strerror(errno));
427 enum { BUFFERSIZE = 64 * 1024 };
428 char buffer[BUFFERSIZE];
431 DEBUG(100)(
"shift_file(%d,%d)",fd,(int)inslen);
433 if (fstat(fd, &sb) != 0)
return -1;
436 size_t bytes_to_move = sb.st_size;
437 off_t read_end_offset = sb.st_size;
438 while (bytes_to_move != 0) {
439 ssize_t bytes_this_time = bytes_to_move < BUFFERSIZE ? bytes_to_move : BUFFERSIZE ;
440 ssize_t rd_off = read_end_offset - bytes_this_time;
441 ssize_t wr_off = rd_off + inslen;
443 if (read(fd, buffer, bytes_this_time) != bytes_this_time)
446 if (write(fd, buffer, bytes_this_time) != bytes_this_time)
448 bytes_to_move -= bytes_this_time;
453#pragma GCC diagnostic ignored "-Weffc++"
457 (*seen) += boost::icl::discrete_interval<uint64_t>::closed(pos,pos+length-1);
477 if(length==0)
return;
479 uint32_t insert_bytes=0;
480 uint64_t offset =
pos+delta;
482 if((int64_t)offset < 0){
488 DEBUG(2)(
"packet received with offset %" PRId64
"; ignoring",offset);
492 insert_bytes = -offset;
499 uint32_t wlength = length;
503 if(offset >= max_bytes_per_flow){
506 if(offset < max_bytes_per_flow && offset+length > max_bytes_per_flow){
508 wlength = max_bytes_per_flow - offset;
519 DEBUG(1)(
"unable to open TCP file %s fd=%d wlength=%d",
534 DEBUG(25)(
"%s: insert(0,%d); lseek(%d,0,SEEK_SET) out_of_order_count=%" PRId64,
546 if(delta == -1 && length == 1) {
551 if(
fd>=0) lseek(
fd,(off_t)delta,SEEK_CUR);
553 DEBUG(25)(
"%s: lseek(%d,%d,SEEK_CUR) offset=%" PRId64
" pos=%" PRId64
" out_of_order_count=%" PRId64,
560 DEBUG(25) (
"%s: %s write %ld bytes @%" PRId64,
562 fd>=0 ?
"will" :
"won't",
563 (long) wlength, offset);
566 if ((uint32_t)write(
fd,data, wlength) != wlength) {
568 if (
debug >= 1) perror(
"");
572 idx_file << offset <<
"|" << ts.tv_sec <<
"." << std::setw(6) << std::setfill(
'0') << ts.tv_usec <<
"|"
581 if(wlength != length){
582 off_t p = lseek(
fd,length-wlength,SEEK_CUR);
583 DEBUG(100)(
" lseek(%" PRId64
",SEEK_CUR)=%" PRId64,(int64_t)(length-wlength),(int64_t)p);
597 uint64_t rpos = lseek(
fd,(off_t)0,SEEK_CUR);
598 DEBUG(100)(
" pos=%" PRId64
" lseek(fd,0,SEEK_CUR)=%" PRId64,
pos,rpos);
602#ifdef DEBUG_REOPEN_LOGIC
614 std::stringstream ss_a(a),ss_b(b);
632 std::vector<std::string> idx;
637 DEBUG(5)(
"Skipping index file sort. Unusual behavior.\n");
645 if (!ix_file->eof()) {
648 }
while (ix_file->good());
652 for (std::vector<std::string>::iterator s = idx.begin(); s != idx.end();
654 *ix_file << *s <<
"\n";
667#pragma GCC diagnostic ignored "-Weffc++"
668#pragma GCC diagnostic ignored "-Wshadow"
std::string new_filename(int *fd, int flags, int mode)
std::string filename(uint32_t connection_count, bool)
bool console_output_nonewline
int64_t max_bytes_per_flow
bool output_strip_nonprint
int retrying_open(const std::string &filename, int oflag, int mask)
intrusive_list< tcpip > open_flows
void close_tcpip_fd(tcpip *)
unsigned int max_open_flows
uint64_t out_of_order_count
void dump_xml(class dfxml_writer *xmlreport, const std::string &xmladd)
void store_packet(const u_char *data, uint32_t length, int32_t delta, struct timeval ts)
std::string flow_pathname
intrusive_list< tcpip >::iterator it
void print_packet(const u_char *data, uint32_t length)
std::string flow_index_pathname
static bool compare(std::string a, std::string b)
std::string macaddr(const uint8_t *addr)
#define DEBUG(message_level)
void update_seen(recon_set *seen, uint64_t pos, uint32_t length)
static int shift_file(int fd, size_t inslen)