Skip to content

Commit ee1ff99

Browse files
author
rbucek
committed
add macro for enable / disable sniffer inline functions
add macro for enable workarround for read pcap via tcpreplay debugging tcpreasembly & http - decrease stream delay interval from 30 to 5s - improve debug logs
1 parent 4516ae5 commit ee1ff99

8 files changed

+72
-25
lines changed

http.cpp

+13-5
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ void HttpData::processData(u_int32_t ip_src, u_int32_t ip_dst,
270270
}
271271
}
272272
delete data;
273+
this->cache.cleanup(false);
273274
}
274275

275276
string HttpData::getUri(string &request) {
@@ -393,10 +394,15 @@ string HttpData::getJsonValue(string &data, const char *valueName) {
393394
return("");
394395
}
395396

397+
void HttpData::printContentSummary() {
398+
cout << "HTTP CACHE: " << this->cache.getSize() << endl;
399+
this->cache.cleanup(true);
400+
}
401+
396402

397403
HttpCache::HttpCache() {
398404
this->cleanupCounter = 0;
399-
this->lastAddTimestamp = 0;
405+
this->lastAddTimestamp = 0;
400406
}
401407

402408
HttpDataCache HttpCache::get(u_int32_t ip_src, u_int32_t ip_dst,
@@ -420,15 +426,17 @@ void HttpCache::add(u_int32_t ip_src, u_int32_t ip_dst,
420426
HttpDataCache_id idc(ip_src, ip_dst, port_src, port_dst, http, body, http_master, body_master);
421427
this->cache[idc] = HttpDataCache(id, timestamp);
422428
this->lastAddTimestamp = timestamp;
423-
this->cleanup();
424429
}
425430

426-
void HttpCache::cleanup() {
431+
void HttpCache::cleanup(bool force) {
427432
++this->cleanupCounter;
428-
if(!(this->cleanupCounter % 100)) {
433+
if(force ||
434+
!(this->cleanupCounter % 100)) {
435+
u_int64_t clock = getTimeMS()/1000;
429436
map<HttpDataCache_id, HttpDataCache>::iterator iter;
430437
for(iter = this->cache.begin(); iter != this->cache.end(); ) {
431-
if(iter->second.timestamp < this->lastAddTimestamp - 120) {
438+
if(iter->second.timestamp < this->lastAddTimestamp - 120 ||
439+
iter->second.timestamp_clock < clock - 120) {
432440
this->cache.erase(iter++);
433441
} else {
434442
++iter;

http.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ struct HttpDataCache {
5151
HttpDataCache(uint32_t id = 0, u_int64_t timestamp = 0) {
5252
this->id = id;
5353
this->timestamp = timestamp;
54+
this->timestamp_clock = getTimeMS()/1000;
5455
}
5556
uint32_t id;
5657
u_int64_t timestamp;
58+
u_int64_t timestamp_clock;
5759
};
5860

5961
class HttpCache {
@@ -68,8 +70,11 @@ class HttpCache {
6870
string *http, string *body,
6971
string *http_master, string *body_master,
7072
u_int32_t id, u_int64_t timestamp);
71-
void cleanup();
73+
void cleanup(bool force = false);
7274
void clear();
75+
u_int32_t getSize() {
76+
return(this->cache.size());
77+
}
7378
private:
7479
map<HttpDataCache_id, HttpDataCache> cache;
7580
u_int64_t cleanupCounter;
@@ -89,6 +94,7 @@ class HttpData : public TcpReassemblyProcessData {
8994
string getUriPathValue(string &uri, const char *valueName);
9095
string getTag(string &data, const char *tag);
9196
string getJsonValue(string &data, const char *valueName);
97+
void printContentSummary();
9298
private:
9399
unsigned int counterProcessData;
94100
HttpCache cache;

sniff_inline.cpp

+13-4
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ extern TcpReassembly *tcpReassembly;
3030
extern unsigned int duplicate_counter;
3131

3232

33-
inline iphdr2 *convertHeaderIP_GRE(iphdr2 *header_ip) {
33+
#if SNIFFER_INLINE_FUNCTIONS
34+
inline
35+
#endif
36+
iphdr2 *convertHeaderIP_GRE(iphdr2 *header_ip) {
3437
char gre[8];
3538
uint16_t a, b;
3639
// if anyone know how to make network to hostbyte nicely, redesign this
@@ -65,7 +68,10 @@ inline iphdr2 *convertHeaderIP_GRE(iphdr2 *header_ip) {
6568
return(header_ip);
6669
}
6770

68-
inline int pcapProcess(pcap_pkthdr** header, u_char** packet, bool *destroy,
71+
#if SNIFFER_INLINE_FUNCTIONS
72+
inline
73+
#endif
74+
int pcapProcess(pcap_pkthdr** header, u_char** packet, bool *destroy,
6975
bool enableDefrag, bool enableCalcMD5, bool enableDedup, bool enableDump,
7076
pcapProcessData *ppd, int pcapLinklayerHeaderType, pcap_dumper_t *pcapDumpHandle, const char *interfaceName) {
7177
*destroy = false;
@@ -110,10 +116,13 @@ inline int pcapProcess(pcap_pkthdr** header, u_char** packet, bool *destroy,
110116
}
111117

112118
if(ppd->protocol != 8) {
113-
/* if(ppd->protocol == 0) { // workarround for read via tcpreplay
119+
#if TCPREPLAY_WORKARROUND
120+
if(ppd->protocol == 0) {
114121
ppd->header_ip_offset += 2;
115122
ppd->protocol = 8;
116-
} else */ {
123+
} else
124+
#endif
125+
{
117126
// not ipv4
118127
return(0);
119128
}

sniff_inline.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44

55
#include "pcap_queue.h"
66

7-
7+
#if SNIFFER_INLINE_FUNCTIONS
88
#include "sniff_inline.cpp"
9-
/*
9+
#else
1010
iphdr2 *convertHeaderIP_GRE(iphdr2 *header_ip);
1111
int pcapProcess(pcap_pkthdr** header, u_char** packet, bool *destroy,
1212
bool enableDefrag, bool enableCalcMD5, bool enableDedup, bool enableDump,
1313
pcapProcessData *ppd, int pcapLinklayerHeaderType, pcap_dumper_t *pcapDumpHandle, const char *interfaceName);
14-
*/
14+
#endif
1515

1616

1717
#endif

tcpreassembly.cpp

+30-8
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ bool debug_data = globalDebug && true;
2929
bool debug_check_ok = globalDebug && true;
3030
bool debug_check_ok_process = globalDebug && true;
3131
bool debug_save = globalDebug && true;
32-
bool debug_print_content = globalDebug && true;
32+
bool debug_cleanup = globalDebug && true;
33+
bool debug_print_content_summary = globalDebug && true;
34+
bool debug_print_content = globalDebug && false;
3335
u_int16_t debug_counter = 0;
3436
u_int16_t debug_limit_counter = 0;
3537
u_int16_t debug_port = 0;
@@ -548,7 +550,9 @@ bool TcpReassemblyLink::streamIterator::nextAckByMaxSeqInReverseDirection() {
548550
}
549551

550552
void TcpReassemblyLink::streamIterator::print() {
551-
cout << "iterator";
553+
cout << "iterator "
554+
<< inet_ntostring(htonl(this->link->ip_src)) << " / " << this->link->port_src << " -> "
555+
<< inet_ntostring(htonl(this->link->ip_dst)) << " / " << this->link->port_dst << " ";
552556
if(this->stream) {
553557
cout << " ack: " << this->stream->ack
554558
<< " state: " << this->state;
@@ -976,7 +980,7 @@ void TcpReassemblyLink::cleanup(u_int64_t act_time) {
976980
map<uint32_t, TcpReassemblyStream*>::iterator iter;
977981
for(iter = this->queue_by_ack.begin(); iter != this->queue_by_ack.end(); ) {
978982
if(iter->second->queue.size() > 500) {
979-
if(this->reassembly->isActiveLog()) {
983+
if(this->reassembly->isActiveLog() || debug_cleanup) {
980984
in_addr ip;
981985
ip.s_addr = this->ip_src;
982986
string ip_src = inet_ntoa(ip);
@@ -989,6 +993,9 @@ void TcpReassemblyLink::cleanup(u_int64_t act_time) {
989993
<< setw(15) << ip_src << "/" << setw(6) << this->port_src
990994
<< " -> "
991995
<< setw(15) << ip_dst << "/" << setw(6) << this->port_dst;
996+
if(debug_cleanup) {
997+
cout << outStr.str() << endl;
998+
}
992999
this->reassembly->addLog(outStr.str().c_str());
9931000
}
9941001
delete iter->second;
@@ -1425,7 +1432,10 @@ void TcpReassemblyLink::complete_crazy(bool final, bool eraseCompletedStreams, b
14251432
if(i == 0 || i == countRequest) {
14261433
cout << endl << endl;
14271434
}
1428-
cout << " ack: " << this->ok_streams[skip_offset + i]->ack << endl << endl;
1435+
cout << " ack: " << this->ok_streams[skip_offset + i]->ack << " "
1436+
<< inet_ntostring(htonl(this->ip_src)) << " / " << this->port_src << " -> "
1437+
<< inet_ntostring(htonl(this->ip_dst)) << " / " << this->port_dst << " "
1438+
<< endl << endl;
14291439
cout << data << endl << endl;
14301440
}
14311441
if(i < countRequest) {
@@ -1843,9 +1853,10 @@ void TcpReassembly::cleanup(bool all) {
18431853
}
18441854
this->unlock_links();
18451855

1846-
u_int64_t act_time = this->act_time_from_header + getTimeMS() - this->last_time;
1847-
18481856
while(true) {
1857+
1858+
u_int64_t act_time = this->act_time_from_header + getTimeMS() - this->last_time;
1859+
18491860
TcpReassemblyLink *link = NULL;
18501861
this->lock_links();
18511862
for(iter = this->links.begin(); iter != this->links.end(); iter++) {
@@ -1857,7 +1868,7 @@ void TcpReassembly::cleanup(bool all) {
18571868
}
18581869
}
18591870
if(link && link->queue_by_ack.size() > 500) {
1860-
if(this->isActiveLog()) {
1871+
if(this->isActiveLog() || debug_cleanup) {
18611872
in_addr ip;
18621873
ip.s_addr = link->ip_src;
18631874
string ip_src = inet_ntoa(ip);
@@ -1870,6 +1881,9 @@ void TcpReassembly::cleanup(bool all) {
18701881
<< setw(15) << ip_src << "/" << setw(6) << link->port_src
18711882
<< " -> "
18721883
<< setw(15) << ip_dst << "/" << setw(6) << link->port_dst;
1884+
if(debug_cleanup) {
1885+
cout << outStr.str() << endl;
1886+
}
18731887
this->addLog(outStr.str().c_str());
18741888
}
18751889
link->unlock_queue();
@@ -1886,7 +1900,7 @@ void TcpReassembly::cleanup(bool all) {
18861900
bool final = act_time > link->last_packet_at_from_header + 2 * 60 * 1000;
18871901
if((all || final ||
18881902
(link->last_packet_at_from_header &&
1889-
act_time > link->last_packet_at_from_header + 30 * 1000 &&
1903+
act_time > link->last_packet_at_from_header + 5 * 1000 &&
18901904
link->last_packet_at_from_header > link->last_packet_process_cleanup_at)) &&
18911905
(link->link_is_ok < 2 || opt_tcpreassembly_thread)) {
18921906

@@ -1960,6 +1974,9 @@ void TcpReassembly::cleanup(bool all) {
19601974
}
19611975
this->doPrintContent = false;
19621976
}
1977+
if(debug_print_content_summary) {
1978+
this->printContentSummary();
1979+
}
19631980
}
19641981

19651982
/*
@@ -1977,3 +1994,8 @@ void TcpReassembly::printContent() {
19771994
iter->second->printContent(1);
19781995
}
19791996
}
1997+
1998+
void TcpReassembly::printContentSummary() {
1999+
cout << "LINKS: " << this->links.size() << endl;
2000+
this->dataCallback->printContentSummary();
2001+
}

tcpreassembly.h

+2
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ class TcpReassemblyProcessData {
151151
u_int16_t port_src, u_int16_t port_dst,
152152
TcpReassemblyData *data,
153153
bool debugSave) = 0;
154+
virtual void printContentSummary() {}
154155
};
155156

156157
struct TcpReassemblyLink_id {
@@ -706,6 +707,7 @@ class TcpReassembly {
706707
bool enableStop();
707708
*/
708709
void printContent();
710+
void printContentSummary();
709711
void setDoPrintContent() {
710712
this->doPrintContent = true;
711713
}

voipmonitor.cpp

+2-4
Original file line numberDiff line numberDiff line change
@@ -3916,10 +3916,8 @@ void test_parsepacket() {
39163916
ParsePacket pp;
39173917
pp.setStdParse();
39183918

3919-
char *str = (char*)"REGISTER sip:mx.com SIP/2.0\r\nv: SIP/2.0/UDP 1.2.3.4:5080;branch=asf4aas-5454sadfasfasdf545fsd454asfd46saf;nat=true\r\nv: SIP/2.0/UDP 5.6.7.8:5060;rport=5060;branch=asdf4as54f65as4df5sdaffds\r\nRecord-Route: <sip:1.2.3.4:5080;transport=udp;dest=5.6.7.8-5060;to-tag=6546565654;lr=1>\r\nf: <sip:[email protected]>;tag=6546565654\r\nt: <sip:[email protected]>\r\ni: [email protected]\r\nCSeq: 128752 REGISTER\r\nMax-Forwards: 10\r\nAccept-Encoding: identity\r\nAccept: application/sdp, multipart/mixed\r\nAllow: INVITE,ACK,OPTIONS,CANCEL,BYE,UPDATE,PRACK,INFO,SUBSCRIBE,NOTIFY,REFER,MESSAGE,PUBLISH\r\nAllow-Events: telephone-event,refer,reg\r\nSupported: 100rel,replaces\r\nl: 0\r\n\r\n";
3920-
for(int i = 0; i < 100000; i++) {
3921-
pp.parseData(str, strlen(str), true);
3922-
}
3919+
char *str = (char*)"";
3920+
cout << pp.parseData(str, strlen(str), true) << endl;
39233921

39243922
pp.debugData();
39253923
}

voipmonitor.h

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#define GRAPH_VERSION 4294967294
3535
#define GRAPH_MARK 4294967293
3636

37+
#define SNIFFER_INLINE_FUNCTIONS true
38+
#define TCPREPLAY_WORKARROUND false
3739

3840

3941
/* choose what method wil be used to synchronize threads. NONBLOCK is the fastest. Do not enable both at once */

0 commit comments

Comments
 (0)