Commit 32d30efd authored by 's avatar
Browse files

changed tracke_list to a hash table


git-svn-id: file:///home/svn/mapi/trunk@396 8d5bb341-7cf1-0310-8cf6-ba355fef3186
parent f041236e
......@@ -49,6 +49,8 @@ struct list{
struct filters *tail;
};
#define HASHTABLESIZE 101
char *dc_strings[42]={"$MyNick","$Lock","$Direction","$Key","$ConnectToMe","$Get","$Send|","$HubName","$ValidateNick",
"$ValidateDenide","$GetPass","$MyPass","$LogedIn","$BadPass","$Hello","$MyINFO $ALL","$GetINFO","$GetListLen",
"$ListLen","$MaxedOut","$Error","$FileLength","$Canceled","$SR","$Ping","$Hello","$Version","$GetNickList",
......@@ -60,7 +62,7 @@ int isDc(mapidflib_function_instance_t *, unsigned char *, int );
struct mapid_dc {
int *shift[42];
int *skip[42];
struct list *dclist;
struct list **dclist;
};
static int dc_init(mapidflib_function_instance_t *instance, MAPI_UNUSED int fd)
......@@ -68,10 +70,13 @@ static int dc_init(mapidflib_function_instance_t *instance, MAPI_UNUSED int fd)
int i=0;
instance->internal_data = malloc(sizeof(struct mapid_dc));
((struct mapid_dc*)instance->internal_data)->dclist = (struct list*)malloc(sizeof(struct list));
((struct mapid_dc*)instance->internal_data)->dclist->head = NULL;
((struct mapid_dc*)instance->internal_data)->dclist->head = NULL;
((struct mapid_dc*)instance->internal_data)->dclist = (struct list**)malloc(sizeof(struct list*)*HASHTABLESIZE);
memset(((struct mapid_dc*)instance->internal_data)->dclist, 0, (sizeof(struct list*)*HASHTABLESIZE));
for(i = 0; i < HASHTABLESIZE; i++) {
((struct mapid_dc*)instance->internal_data)->dclist[i] = (struct list*)malloc(sizeof(struct list));
((struct mapid_dc*)instance->internal_data)->dclist[i]->head = NULL;
((struct mapid_dc*)instance->internal_data)->dclist[i]->tail = NULL;
}
for(i=0;i<42;i++) {
((struct mapid_dc*)instance->internal_data)->shift[i] = make_shift(dc_strings[i],strlen(dc_strings[i]));
((struct mapid_dc*)instance->internal_data)->skip[i] = make_skip(dc_strings[i], strlen(dc_strings[i]));
......@@ -106,7 +111,7 @@ static int dc_process(mapidflib_function_instance_t *instance,
unsigned char *p = NULL;
struct timeval ts;
struct list *dclist = ((struct mapid_dc*)instance->internal_data)->dclist;
struct list **dclist = ((struct mapid_dc*)instance->internal_data)->dclist;
uint16_t ethertype;
struct ether_header *ep = NULL;
struct iphdr *iph = NULL;
......@@ -116,6 +121,8 @@ static int dc_process(mapidflib_function_instance_t *instance,
unsigned int saddr, daddr;
uint16_t sp, dp;
unsigned int hashval = 0;
p = pkt;
......@@ -140,6 +147,8 @@ static int dc_process(mapidflib_function_instance_t *instance,
p += iph->ihl * 4;
hashval = saddr % HASHTABLESIZE;
if(iph->protocol == 6) // TCP
{
tcph = (struct tcphdr *)p;
......@@ -161,7 +170,7 @@ static int dc_process(mapidflib_function_instance_t *instance,
gettimeofday(&ts, NULL);
for(temp = dclist->head, prev = dclist->head; temp != NULL; prev = temp, temp = temp->next)
for(temp = dclist[hashval]->head, prev = dclist[hashval]->head; temp != NULL; prev = temp, temp = temp->next)
{
if(temp->protocol == iph->protocol &&
(
......@@ -173,8 +182,8 @@ static int dc_process(mapidflib_function_instance_t *instance,
gettimeofday(&(temp->ts), NULL);
if(iph->protocol == 6 && tcph->fin) {
if(temp == dclist->head){
dclist->head = temp->next;
if(temp == dclist[hashval]->head){
dclist[hashval]->head = temp->next;
}
else {
prev->next = temp->next;
......@@ -186,8 +195,8 @@ static int dc_process(mapidflib_function_instance_t *instance,
return 1;
}
else if(ts.tv_sec - temp->ts.tv_sec > 1) {
if(temp == dclist->head){
dclist->head = temp->next;
if(temp == dclist[hashval]->head){
dclist[hashval]->head = temp->next;
}
else {
prev->next = temp->next;
......@@ -238,7 +247,9 @@ static int dc_process(mapidflib_function_instance_t *instance,
new->sp = sp;
new->dp = dp;
for(temp = dclist->head; temp != NULL; temp = temp->next)
hashval = new->saddr % HASHTABLESIZE;
for(temp = dclist[hashval]->head; temp != NULL; temp = temp->next)
{
if(new->protocol == temp->protocol && (
(new->saddr == temp->saddr && new->daddr == temp->daddr && new->sp == temp->sp && new->dp == temp->dp)
......@@ -251,8 +262,8 @@ static int dc_process(mapidflib_function_instance_t *instance,
}
}
new->next = dclist->head;
dclist->head = new;
new->next = dclist[hashval]->head;
dclist[hashval]->head = new;
return 1;
}
......@@ -263,16 +274,18 @@ static int dc_process(mapidflib_function_instance_t *instance,
static int dc_cleanup(mapidflib_function_instance_t *instance)
{
struct filters *temp = NULL, *tmp = NULL;
if(instance->internal_data != NULL){
temp = ((struct mapid_dc*)instance->internal_data)->dclist->head;
int i = 0;
while(temp != NULL)
{
tmp = temp;
temp = temp->next;
free(tmp);
}
if(instance->internal_data != NULL){
for(i = 0; i < HASHTABLESIZE; i++) {
temp = ((struct mapid_dc*)instance->internal_data)->dclist[i]->head;
while(temp != NULL) {
tmp = temp;
temp = temp->next;
free(tmp);
}
}
free(((struct mapid_dc*)instance->internal_data)->dclist);
free(instance->internal_data);
}
......
......@@ -44,13 +44,13 @@ struct list{
struct filters *tail;
};
#define HASHTABLESIZE 101
int isEdonkey(int, char);
unsigned int getSize(char *);
struct mapid_edonkey {
// int *shift[42];
// int *skip[42];
struct list *edonkeylist;
struct list **edonkeylist;
};
struct edonkey_header
......@@ -62,10 +62,16 @@ struct edonkey_header
static int edonkey_init(mapidflib_function_instance_t *instance, MAPI_UNUSED int fd)
{
int i = 0;
instance->internal_data = malloc(sizeof(struct mapid_edonkey));
((struct mapid_edonkey*)instance->internal_data)->edonkeylist = (struct list*)malloc(sizeof(struct list));
((struct mapid_edonkey*)instance->internal_data)->edonkeylist->head = NULL;
((struct mapid_edonkey*)instance->internal_data)->edonkeylist->head = NULL;
((struct mapid_edonkey*)instance->internal_data)->edonkeylist = (struct list**)malloc(sizeof(struct list*)*HASHTABLESIZE);
memset(((struct mapid_edonkey*)instance->internal_data)->edonkeylist, 0, (sizeof(struct list*)*HASHTABLESIZE));
for(i = 0; i < HASHTABLESIZE; i++) {
((struct mapid_edonkey*)instance->internal_data)->edonkeylist[i] = (struct list*)malloc(sizeof(struct list));
((struct mapid_edonkey*)instance->internal_data)->edonkeylist[i]->head = NULL;
((struct mapid_edonkey*)instance->internal_data)->edonkeylist[i]->tail = NULL;
}
return 0;
}
......@@ -230,7 +236,7 @@ static int edonkey_process(mapidflib_function_instance_t *instance,
struct filters *temp = NULL, *prev = NULL, *new = NULL;
unsigned char *p = NULL;
struct list *edonkeylist = ((struct mapid_edonkey*)instance->internal_data)->edonkeylist;
struct list **edonkeylist = ((struct mapid_edonkey*)instance->internal_data)->edonkeylist;
uint16_t ethertype;
struct ether_header *ep = NULL;
struct iphdr *iph = NULL;
......@@ -245,6 +251,8 @@ static int edonkey_process(mapidflib_function_instance_t *instance,
uint16_t sp, dp;
unsigned int hashval = 0;
p = pkt;
// lay the Ethernet header struct over the packet data
......@@ -269,6 +277,8 @@ static int edonkey_process(mapidflib_function_instance_t *instance,
p += iph->ihl * 4;
hashval = saddr % HASHTABLESIZE;
if(iph->protocol == 6) // TCP
{
......@@ -293,7 +303,7 @@ static int edonkey_process(mapidflib_function_instance_t *instance,
gettimeofday(&ts, NULL);
for(temp = edonkeylist->head, prev = edonkeylist->head; temp != NULL; prev = temp, temp = temp->next)
for(temp = edonkeylist[hashval]->head, prev = edonkeylist[hashval]->head; temp != NULL; prev = temp, temp = temp->next)
{
if(temp->protocol == iph->protocol &&
(
......@@ -305,8 +315,8 @@ static int edonkey_process(mapidflib_function_instance_t *instance,
gettimeofday(&(temp->last_timestamp), NULL);
if(iph->protocol == 6 && tcph->fin) {
if(temp == edonkeylist->head) {
edonkeylist->head = temp->next;
if(temp == edonkeylist[hashval]->head) {
edonkeylist[hashval]->head = temp->next;
}
else {
prev->next = temp->next;
......@@ -318,8 +328,8 @@ static int edonkey_process(mapidflib_function_instance_t *instance,
return 1;
}
else if(ts.tv_sec - temp->last_timestamp.tv_sec > 1) {
if(temp == edonkeylist->head) {
edonkeylist->head = temp->next;
if(temp == edonkeylist[hashval]->head) {
edonkeylist[hashval]->head = temp->next;
}
else {
prev->next = temp->next;
......@@ -383,8 +393,9 @@ static int edonkey_process(mapidflib_function_instance_t *instance,
new->dp = dp;
gettimeofday(&(new->last_timestamp), NULL);
/*
for(temp = edonkeylist->head; temp != NULL; temp = temp->next)
hashval = new->saddr % HASHTABLESIZE;
for(temp = edonkeylist[hashval]->head; temp != NULL; temp = temp->next)
{
if(new->protocol == temp->protocol && (
(new->saddr == temp->saddr && new->daddr == temp->daddr && new->sp == temp->sp && new->dp == temp->dp)
......@@ -396,9 +407,9 @@ static int edonkey_process(mapidflib_function_instance_t *instance,
return 1;
}
}
*/
new->next = edonkeylist->head;
edonkeylist->head = new;
new->next = edonkeylist[hashval]->head;
edonkeylist[hashval]->head = new;
return 1;
}
......@@ -406,18 +417,22 @@ static int edonkey_process(mapidflib_function_instance_t *instance,
static int edonkey_cleanup(mapidflib_function_instance_t *instance)
{
struct filters *temp = NULL, *tmp = NULL;
int i = 0;
if(instance->internal_data != NULL){
temp = ((struct mapid_edonkey*)instance->internal_data)->edonkeylist->head;
while(temp != NULL)
{
tmp = temp;
temp = temp->next;
free(tmp);
}
free(((struct mapid_edonkey*)instance->internal_data)->edonkeylist);
free(instance->internal_data);
for(i = 0; i < HASHTABLESIZE; i++) {
temp = ((struct mapid_edonkey*)instance->internal_data)->edonkeylist[i]->head;
while(temp != NULL) {
tmp = temp;
temp = temp->next;
free(tmp);
}
}
free(((struct mapid_edonkey*)instance->internal_data)->edonkeylist);
free(instance->internal_data);
}
return 0;
}
......
......@@ -57,7 +57,6 @@ struct mapid_gnutella {
int *shift[7];
int *skip[7];
struct list **gnulist;
struct list *top_ten;
};
int isGnutella(mapidflib_function_instance_t *, unsigned char *, int );
......@@ -74,10 +73,6 @@ static int gnutella_init(mapidflib_function_instance_t *instance, MAPI_UNUSED in
((struct mapid_gnutella*)instance->internal_data)->gnulist[i]->head = NULL;
((struct mapid_gnutella*)instance->internal_data)->gnulist[i]->tail = NULL;
}
((struct mapid_gnutella*)instance->internal_data)->top_ten = (struct list*)malloc(sizeof(struct list));
((struct mapid_gnutella*)instance->internal_data)->top_ten->head = NULL;
((struct mapid_gnutella*)instance->internal_data)->top_ten->tail = NULL;
for(i=0;i<7;i++) {
((struct mapid_gnutella*)instance->internal_data)->shift[i] = make_shift(gnutella_strings[i],strlen(gnutella_strings[i]));
((struct mapid_gnutella*)instance->internal_data)->skip[i] = make_skip(gnutella_strings[i], strlen(gnutella_strings[i]));
......@@ -260,16 +255,7 @@ static int gnutella_cleanup(mapidflib_function_instance_t *instance)
}
}
temp = ((struct mapid_gnutella*)instance->internal_data)->top_ten->head;
while(temp != NULL) {
tmp = temp;
temp = temp->next;
free(tmp);
}
free(((struct mapid_gnutella*)instance->internal_data)->gnulist);
free(((struct mapid_gnutella*)instance->internal_data)->top_ten);
free(instance->internal_data);
}
return 0;
......
......@@ -49,6 +49,8 @@ struct list{
struct filters *tail;
};
#define HASHTABLESIZE 101
char *torrent_strings[3]={"BitTorrent protocol","GET /scrape?info_hash=","GET /announce?info_hash="};
int isTorrent(mapidflib_function_instance_t *, unsigned char *, int );
......@@ -56,7 +58,7 @@ int isTorrent(mapidflib_function_instance_t *, unsigned char *, int );
struct mapid_torrent {
int *shift[3];
int *skip[3];
struct list *torlist;
struct list **torlist;
};
static int torrent_init(mapidflib_function_instance_t *instance, MAPI_UNUSED int fd)
......@@ -64,10 +66,12 @@ static int torrent_init(mapidflib_function_instance_t *instance, MAPI_UNUSED int
int i=0;
instance->internal_data = malloc(sizeof(struct mapid_torrent));
((struct mapid_torrent*)instance->internal_data)->torlist = (struct list*)malloc(sizeof(struct list));
((struct mapid_torrent*)instance->internal_data)->torlist->head = NULL;
((struct mapid_torrent*)instance->internal_data)->torlist->head = NULL;
((struct mapid_torrent*)instance->internal_data)->torlist = (struct list**)malloc(sizeof(struct list*)*HASHTABLESIZE);
for(i = 0; i < HASHTABLESIZE; i++) {
((struct mapid_torrent*)instance->internal_data)->torlist[i] = (struct list*)malloc(sizeof(struct list));
((struct mapid_torrent*)instance->internal_data)->torlist[i]->head = NULL;
((struct mapid_torrent*)instance->internal_data)->torlist[i]->tail = NULL;
}
for(i=0;i<3;i++) {
((struct mapid_torrent*)instance->internal_data)->shift[i] = make_shift(torrent_strings[i],strlen(torrent_strings[i]));
((struct mapid_torrent*)instance->internal_data)->skip[i] = make_skip(torrent_strings[i], strlen(torrent_strings[i]));
......@@ -103,7 +107,7 @@ static int torrent_process(mapidflib_function_instance_t *instance,
unsigned char *p = NULL;
struct timeval ts;
struct list *torlist = ((struct mapid_torrent*)instance->internal_data)->torlist;
struct list **torlist = ((struct mapid_torrent*)instance->internal_data)->torlist;
uint16_t ethertype;
struct ether_header *ep = NULL;
struct iphdr *iph = NULL;
......@@ -114,6 +118,8 @@ static int torrent_process(mapidflib_function_instance_t *instance,
uint16_t sp, dp;
unsigned int hashval = 0;
p = pkt;
// lay the Ethernet header struct over the packet data
......@@ -136,7 +142,8 @@ static int torrent_process(mapidflib_function_instance_t *instance,
p += iph->ihl * 4;
hashval = saddr % HASHTABLESIZE;
if(iph->protocol == 6) // TCP
{
tcph = (struct tcphdr *)p;
......@@ -160,7 +167,7 @@ static int torrent_process(mapidflib_function_instance_t *instance,
gettimeofday(&ts, NULL);
for(temp = torlist->head, prev = torlist->head; temp != NULL; prev = temp, temp = temp->next)
for(temp = torlist[hashval]->head, prev = torlist[hashval]->head; temp != NULL; prev = temp, temp = temp->next)
{
if(temp->protocol == iph->protocol &&
(
......@@ -172,8 +179,8 @@ static int torrent_process(mapidflib_function_instance_t *instance,
gettimeofday(&(temp->ts), NULL);
if(iph->protocol == 6 && tcph->fin) {
if(temp == torlist->head) {
torlist->head = temp->next;
if(temp == torlist[hashval]->head) {
torlist[hashval]->head = temp->next;
}
else {
prev->next = temp->next;
......@@ -185,8 +192,8 @@ static int torrent_process(mapidflib_function_instance_t *instance,
return 1;
}
else if(ts.tv_sec - temp->ts.tv_sec > 1) {
if(temp == torlist->head) {
torlist->head = temp->next;
if(temp == torlist[hashval]->head) {
torlist[hashval]->head = temp->next;
}
else {
prev->next = temp->next;
......@@ -215,7 +222,9 @@ static int torrent_process(mapidflib_function_instance_t *instance,
new->sp = sp;
new->dp = dp;
for(temp = torlist->head; temp != NULL; temp = temp->next)
hashval = new->saddr % HASHTABLESIZE;
for(temp = torlist[hashval]->head; temp != NULL; temp = temp->next)
{
if(new->protocol == temp->protocol && (
(new->saddr == temp->saddr && new->daddr == temp->daddr && new->sp == temp->sp && new->dp == temp->dp)
......@@ -228,8 +237,8 @@ static int torrent_process(mapidflib_function_instance_t *instance,
}
}
new->next = torlist->head;
torlist->head = new;
new->next = torlist[hashval]->head;
torlist[hashval]->head = new;
return 1;
}
......@@ -242,16 +251,18 @@ static int torrent_process(mapidflib_function_instance_t *instance,
static int torrent_cleanup(mapidflib_function_instance_t *instance)
{
struct filters *temp = NULL, *tmp = NULL;
int i = 0;
if(instance->internal_data != NULL){
temp = ((struct mapid_torrent*)instance->internal_data)->torlist->head;
while(temp != NULL)
{
tmp = temp;
temp = temp->next;
free(tmp);
}
for(i = 0; i < HASHTABLESIZE; i++) {
temp = ((struct mapid_torrent*)instance->internal_data)->torlist[i]->head;
while(temp != NULL) {
tmp = temp;
temp = temp->next;
free(tmp);
}
}
free(((struct mapid_torrent*)instance->internal_data)->torlist);
free(instance->internal_data);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment