Skip to content

Commit

Permalink
Tracked down and fixed the memleak in connection cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
gamelinux committed Dec 23, 2011
1 parent 4bc02cd commit 912b9dc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
21 changes: 14 additions & 7 deletions src/passivedns.c
Expand Up @@ -435,8 +435,8 @@ connection *cxt_new(packetinfo *pi)

cxt->af = pi->af;
if(pi->tcph) cxt->s_tcpFlags |= pi->tcph->t_flags;
cxt->s_total_bytes = pi->packet_bytes;
cxt->s_total_pkts = 1;
//cxt->s_total_bytes = pi->packet_bytes;
//cxt->s_total_pkts = 1;
cxt->start_time = pi->pheader->ts.tv_sec;
cxt->last_pkt_time = pi->pheader->ts.tv_sec;

Expand All @@ -458,6 +458,7 @@ connection *cxt_new(packetinfo *pi)
cxt->c_asset = NULL;
cxt->s_asset = NULL;
cxt->reversed = 0;
config.curcxt++;

return cxt;
}
Expand Down Expand Up @@ -504,10 +505,12 @@ void end_all_sessions()
{
connection *cxt;
int cxkey;
config.llcxt = 0;

for (cxkey = 0; cxkey < BUCKET_SIZE; cxkey++) {
cxt = bucket[cxkey];
while (cxt != NULL) {
config.llcxt++;
if (cxt->prev)
cxt->prev->next = cxt->next;
if (cxt->next)
Expand All @@ -521,22 +524,25 @@ void end_all_sessions()
}
}
}
dlog("Current CXT: %10u\n", config.curcxt);
dlog("CXT in List: %10u\n", config.llcxt);
}

void end_sessions()
{
connection *cxt;
time_t check_time;
check_time = config.tstamp;
//time(&check_time);
int ended, expired = 0;
uint32_t curcxt = 0;
config.llcxt = 0;

int iter;
for (iter = 0; iter < BUCKET_SIZE; iter++) {
cxt = bucket[iter];
while (cxt != NULL) {
ended = 0;
curcxt++;
config.llcxt++;
/* TCP */
if (cxt->proto == IP_PROTO_TCP) {
/* * FIN from both sides */
Expand Down Expand Up @@ -581,18 +587,18 @@ void end_sessions()
ended = expired = 0;

cxt = cxt->next;
//cxt = cxt->prev;

del_connection(tmp, &bucket[iter]);
if (cxt == NULL) {
if (cxt->prev == NULL && cxt->next == NULL && cxt == NULL) {
bucket[iter] = NULL;
}
} else {
cxt = cxt->next;
//cxt = cxt->prev;
}
}
}
dlog("Current CXT: %10u\n", config.curcxt);
dlog("CXT in List: %10u\n", config.llcxt);
}

void del_connection(connection * cxt, connection ** bucket_ptr)
Expand All @@ -618,6 +624,7 @@ void del_connection(connection * cxt, connection ** bucket_ptr)
// Free and set to NULL
free(cxt);
cxt = NULL;
config.curcxt--;
}

const char *u_ntop_src(packetinfo *pi, char *dest)
Expand Down
2 changes: 2 additions & 0 deletions src/passivedns.h
Expand Up @@ -477,6 +477,8 @@ typedef struct _globalconfig {
uint8_t ctf; /* Flags for TCP checks, SYN,RST,FIN.... */
uint8_t cof; /* Flags for other; icmp,udp,other,.... */
uint32_t payload; /* dump how much of the payload ? */
uint32_t curcxt;
uint32_t llcxt;
uint64_t mem_limit_max; /* Try soft limit memory use */
uint64_t mem_limit_size; /* Current memory size */
uint32_t dns_records; /* total number of DNS records in memory */
Expand Down

0 comments on commit 912b9dc

Please sign in to comment.