tcpip.cpp (tcpflow-1.5.0) | : | tcpip.cpp (tcpflow-1.6.1) | ||
---|---|---|---|---|
skipping to change at line 103 | skipping to change at line 103 | |||
/** | /** | |||
* Destructor is called when flow is closed. | * Destructor is called when flow is closed. | |||
* It implements "after" processing. | * It implements "after" processing. | |||
* This should only be called from remove_flow() or remove_all_flows() | * This should only be called from remove_flow() or remove_all_flows() | |||
* when a flow is deleted. | * when a flow is deleted. | |||
*/ | */ | |||
tcpip::~tcpip() | tcpip::~tcpip() | |||
{ | { | |||
assert(fd<0); // file must be closed | assert(fd<0); // file must be closed | |||
if(seen) delete seen; | delete seen; // no need to check to see if seen is nu ll or not. | |||
} | } | |||
#pragma GCC diagnostic warning "-Weffc++" | #pragma GCC diagnostic warning "-Weffc++" | |||
#pragma GCC diagnostic warning "-Wshadow" | #pragma GCC diagnostic warning "-Wshadow" | |||
/**************************************************************** | /**************************************************************** | |||
** SAVE FILE MANAGEMENT | ** SAVE FILE MANAGEMENT | |||
**************************************************************** | **************************************************************** | |||
* | * | |||
* Unlike the tcp/ip object, which is created once, the file can be opened, clos ed, and | * Unlike the tcp/ip object, which is created once, the file can be opened, clos ed, and | |||
skipping to change at line 133 | skipping to change at line 133 | |||
void tcpip::close_file() | void tcpip::close_file() | |||
{ | { | |||
if (fd>=0){ | if (fd>=0){ | |||
struct timeval times[2]; | struct timeval times[2]; | |||
times[0] = myflow.tstart; | times[0] = myflow.tstart; | |||
times[1] = myflow.tstart; | times[1] = myflow.tstart; | |||
DEBUG(5) ("%s: closing file in tcpip::close_file", flow_pathname.c_str()) ; | DEBUG(5) ("%s: closing file in tcpip::close_file", flow_pathname.c_str()) ; | |||
/* close the file and remember that it's closed */ | /* close the file and remember that it's closed */ | |||
#if defined(HAVE_FUTIMES) | #if defined(HAVE_FUTIMES) | |||
if(futimes(fd,times)){ | /* fix microseconds if they are invalid */ | |||
fprintf(stderr,"%s: futimes(fd=%d)\n",strerror(errno),fd); | for ( int i=0; i<2; i++){ | |||
abort(); | if ( times[i].tv_usec < 0 || times[i].tv_usec >= 1000000 ){ | |||
times[i].tv_usec = 0; | ||||
} | ||||
} | ||||
if (futimes(fd,times)){ | ||||
fprintf(stderr,"%s: futimes(fd=%d,[%ld:%ld,%ld:%ld])\n", | ||||
strerror(errno),fd, | ||||
times[0].tv_sec,times[1].tv_usec, | ||||
times[1].tv_sec,times[1].tv_usec); | ||||
} | } | |||
#elif defined(HAVE_FUTIMENS) | #elif defined(HAVE_FUTIMENS) | |||
struct timespec tstimes[2]; | struct timespec tstimes[2]; | |||
for(int i=0;i<2;i++){ | for(int i=0;i<2;i++){ | |||
tstimes[i].tv_sec = times[i].tv_sec; | tstimes[i].tv_sec = times[i].tv_sec; | |||
tstimes[i].tv_nsec = times[i].tv_usec * 1000; | tstimes[i].tv_nsec = times[i].tv_usec * 1000; | |||
} | } | |||
if(futimens(fd,tstimes)){ | if(futimens(fd,tstimes)){ | |||
perror("futimens(fd=%d)",fd); | perror("futimens(fd=%d)",fd); | |||
} | } | |||
skipping to change at line 252 | skipping to change at line 260 | |||
#ifdef HAVE_PTHREAD | #ifdef HAVE_PTHREAD | |||
if(semlock){ | if(semlock){ | |||
if(sem_wait(semlock)){ | if(sem_wait(semlock)){ | |||
fprintf(stderr,"%s: attempt to acquire semaphore failed: %s\n",progna me,strerror(errno)); | fprintf(stderr,"%s: attempt to acquire semaphore failed: %s\n",progna me,strerror(errno)); | |||
exit(1); | exit(1); | |||
} | } | |||
} | } | |||
#endif | #endif | |||
if(flow_pathname.size()==0) flow_pathname = myflow.filename(0, false); | ||||
if (demux.opt.use_color) fputs(dir==dir_cs ? color[1] : color[2], stdout); | if (demux.opt.use_color) fputs(dir==dir_cs ? color[1] : color[2], stdout); | |||
if (demux.opt.suppress_header == 0){ | if (demux.opt.suppress_header == 0 && demux.opt.output_json == 0){ | |||
if(flow_pathname.size()==0) flow_pathname = myflow.filename(0); | ||||
printf("%s: ", flow_pathname.c_str()); | printf("%s: ", flow_pathname.c_str()); | |||
if(demux.opt.output_hex) putchar('\n'); | if(demux.opt.output_hex) putchar('\n'); | |||
} | } | |||
size_t written = 0; | size_t written = 0; | |||
if(demux.opt.output_hex){ | if(demux.opt.output_hex){ | |||
const size_t bytes_per_line = 32; | const size_t bytes_per_line = 32; | |||
size_t max_spaces = 0; | size_t max_spaces = 0; | |||
for(u_int i=0;i<length;i+=bytes_per_line){ | for(u_int i=0;i<length;i+=bytes_per_line){ | |||
size_t spaces=0; | size_t spaces=0; | |||
skipping to change at line 298 | skipping to change at line 306 | |||
putchar(' '); | putchar(' '); | |||
/* Print the ascii */ | /* Print the ascii */ | |||
for(size_t j=0;j<bytes_per_line && i+j<length;j++){ | for(size_t j=0;j<bytes_per_line && i+j<length;j++){ | |||
unsigned char ch = data[i+j]; | unsigned char ch = data[i+j]; | |||
if(ch>=' ' && ch<='~') fputc(ch,stdout); | if(ch>=' ' && ch<='~') fputc(ch,stdout); | |||
else fputc('.',stdout); | else fputc('.',stdout); | |||
} | } | |||
fputc('\n',stdout); | fputc('\n',stdout); | |||
} | } | |||
written = length; // just fake it. | written = length; // just fake it. | |||
} | } else if (demux.opt.output_json) { | |||
else if(demux.opt.output_strip_nonprint){ | // { | |||
for(const u_char *cc = data;cc<data+length;cc++){ | // "src_host": "192.168.0.1", | |||
// "src_port": 1234, | ||||
// "dst_host": "1.1.1.1", | ||||
// "dst_port": 80, | ||||
// "payload" : [...] | ||||
// } | ||||
std::string hoststr = std::string(); | ||||
putchar('{'); | ||||
printf("\"src_host\":\""); | ||||
size_t src_pos = 0; | ||||
size_t src_end_pos = 0; | ||||
size_t src_pos_counter = 0; | ||||
size_t pathname_len = flow_pathname.length(); | ||||
for(size_t i = 0; i < pathname_len; ++i) { | ||||
if(flow_pathname[i] == '.') { | ||||
src_pos_counter++; | ||||
printf("%d%s", atoi(hoststr.c_str()), (src_pos_counter != 4 ? ". | ||||
" : "")); | ||||
hoststr.clear(); | ||||
} else { | ||||
hoststr = hoststr + flow_pathname[i]; | ||||
} | ||||
if(src_pos_counter == 4) { | ||||
src_pos = i; | ||||
break; | ||||
} | ||||
} | ||||
src_end_pos = src_pos; | ||||
for(;src_end_pos < pathname_len; ++src_end_pos) { | ||||
if(flow_pathname[src_end_pos] == '-') { | ||||
break; | ||||
} | ||||
} | ||||
printf("\",\"src_port\":%d,\"dst_host\":\"", atoi(flow_pathname.substr(s | ||||
rc_pos + 1, src_end_pos - src_pos).c_str())); | ||||
size_t dst_pos = src_end_pos + 1; | ||||
size_t dst_end_pos = dst_pos; | ||||
size_t dst_pos_counter = 0; | ||||
for(size_t i = dst_pos; i < pathname_len; ++i) { | ||||
if(flow_pathname[i] == '.') { | ||||
dst_pos_counter++; | ||||
printf("%d%s", atoi(hoststr.c_str()), (dst_pos_counter != 4 ? ". | ||||
" : "")); | ||||
hoststr.clear(); | ||||
} else { | ||||
hoststr = hoststr + flow_pathname[i]; | ||||
} | ||||
if(dst_pos_counter == 4) { | ||||
dst_pos = i; | ||||
break; | ||||
} | ||||
} | ||||
dst_end_pos = dst_pos; | ||||
for(;dst_end_pos < pathname_len; ++dst_end_pos) { | ||||
if(flow_pathname[dst_end_pos] == '-') { | ||||
break; | ||||
} | ||||
} | ||||
printf("\",\"dst_port\":%d,\"payload\": [", atoi(flow_pathname.substr(ds | ||||
t_pos + 1, dst_end_pos - dst_pos).c_str())); | ||||
for(size_t i = 0; i < length; ++i) { | ||||
printf("%d%s", data[i], (i != length - 1 ? "," : "]}")); | ||||
} | ||||
} else if (demux.opt.output_strip_nonprint) { | ||||
for(const u_char *cc = data;cc<data+length;cc++){ | ||||
if(isprint(*cc) || (*cc=='\n') || (*cc=='\r')){ | if(isprint(*cc) || (*cc=='\n') || (*cc=='\r')){ | |||
int ret = fputc(*cc,stdout); | int ret = fputc(*cc,stdout); | |||
if(ret==EOF){ | if(ret==EOF){ | |||
std::cerr << "EOF on write to stdout\n"; | std::cerr << "EOF on write to stdout\n"; | |||
exit(1); | exit(1); | |||
} | } | |||
} | } | |||
else fputc('.',stdout); | else fputc('.',stdout); | |||
written += 1; // treat even unprintable characters as "written". It | written += 1; // treat even unprintable characters as "written". It | |||
// really means "processed" | // really means "processed" | |||
} | } | |||
} | } else { | |||
else { | written = fwrite(data,1,length,stdout); | |||
written = fwrite(data,1,length,stdout); | ||||
if(length != written) std::cerr << "\nwrite error to stdout (" << length << "!=" << written << ") \n"; | if(length != written) std::cerr << "\nwrite error to stdout (" << length << "!=" << written << ") \n"; | |||
} | } | |||
last_byte += length; | last_byte += length; | |||
if (demux.opt.use_color) printf("\033[0m"); | if (demux.opt.use_color) printf("\033[0m"); | |||
if (! demux.opt.console_output_nonewline) putchar('\n'); | if (! demux.opt.console_output_nonewline) putchar('\n'); | |||
fflush(stdout); | fflush(stdout); | |||
skipping to change at line 458 | skipping to change at line 532 | |||
isn -= insert_bytes; // it's really earlier | isn -= insert_bytes; // it's really earlier | |||
lseek(fd,(off_t)0,SEEK_SET); // put at the beginning | lseek(fd,(off_t)0,SEEK_SET); // put at the beginning | |||
pos = 0; | pos = 0; | |||
nsn = isn+1; | nsn = isn+1; | |||
out_of_order_count++; | out_of_order_count++; | |||
DEBUG(25)("%s: insert(0,%d); lseek(%d,0,SEEK_SET) out_of_order_count=%" P RId64, | DEBUG(25)("%s: insert(0,%d); lseek(%d,0,SEEK_SET) out_of_order_count=%" P RId64, | |||
flow_pathname.c_str(), insert_bytes, | flow_pathname.c_str(), insert_bytes, | |||
fd,out_of_order_count); | fd,out_of_order_count); | |||
/* TK: If we have seen packets, everything in the recon set needs to be shifted as well.*/ | /* TK: If we have seen packets, everything in the recon set needs to be shifted as well.*/ | |||
if(seen){ | delete seen; | |||
delete seen; | seen = 0; | |||
seen = 0; | ||||
} | ||||
} | } | |||
/* if we're not at the correct point in the file, seek there */ | /* if we're not at the correct point in the file, seek there */ | |||
if (offset != pos) { | if (offset != pos) { | |||
/* Check for a keepalive */ | /* Check for a keepalive */ | |||
if(delta == -1 && length == 1) { | if(delta == -1 && length == 1) { | |||
DEBUG(25)("%s: RFC1122 keepalive detected and ignored",flow_pathname .c_str()); | DEBUG(25)("%s: RFC1122 keepalive detected and ignored",flow_pathname .c_str()); | |||
return; | return; | |||
} | } | |||
End of changes. 7 change blocks. | ||||
17 lines changed or deleted | 93 lines changed or added |