Commit abbfa919 authored by 's avatar
Browse files

Eliminate memory leaks from function top and update testing file of this function

git-svn-id: file:///home/svn/mapi/trunk@919 8d5bb341-7cf1-0310-8cf6-ba355fef3186
parent 02bb2ba7
......@@ -17,6 +17,8 @@ mapidflib_functionlist_t functions[4];
extern mapidflib_function_def_t * cooking_get_funct_info();
extern mapidflib_function_def_t * regexp_get_funct_info();
extern mapidflib_function_def_t * topx_get_funct_info();
extern mapidflib_function_def_t * exprflow_get_funct_info();
//extern mapidflib_function_def_t * inject_get_funct_info();
mapidflib_functionlist_t* mapidflib_get_function_list()
......
......@@ -29,41 +29,6 @@ struct topx_field {
unsigned short value; /* used when cannot pass pointer directly into packet data */
};
static int topx_reset(mapidflib_function_instance_t *instance)
{
struct topx_list_node *tmp,*next;
struct topx_data *data;
struct topx_hash_node **htable;
struct topx_hash_node *htmp,*hnext;
int i=0;
data=(struct topx_data *)(instance->internal_data);
tmp=data->list_head;
while(tmp) {
next=tmp->next;
free(tmp);
tmp=next;
}
data->list_head = data->list_tail=NULL;
data->list_size=0;
htable=data->hashtable;
for(i=0;i<TOPX_HASH_SIZE;i++) {
htmp=htable[i];
while(htmp) {
hnext=htmp->next;
free(htmp);
htmp=hnext;
}
}
memset(htable,0,TOPX_HASH_SIZE*sizeof(struct topx_hash_node *));
return 0;
}
void extract_field(struct topx_field *field, unsigned char *ip_pkt, size_t length, int protocol, int pfield);
struct topx_hash_node *hash_lookup(struct topx_val *value, struct topx_data *data);
void check_for_shift(struct topx_data *data,struct topx_list_node *node);
......@@ -452,6 +417,44 @@ void add_field_to_list(struct topx_field *field,struct topx_data *data,unsigned
}
}
static int topx_cleanup(mapidflib_function_instance_t *instance){
struct topx_list_node *tmp,*next;
struct topx_data *data;
struct topx_hash_node **htable;
struct topx_hash_node *htmp,*hnext;
int i=0;
data=(struct topx_data *)(instance->internal_data);
tmp=data->list_head;
while(tmp) {
next=tmp->next;
free(tmp);
tmp=next;
}
data->list_head = data->list_tail=NULL;
data->list_size=0;
htable=data->hashtable;
for(i=0;i<TOPX_HASH_SIZE;i++) {
htmp=htable[i];
while(htmp) {
hnext=htmp->next;
free(htmp);
htmp=hnext;
}
}
free(data);
memset(htable,0,TOPX_HASH_SIZE*sizeof(struct topx_hash_node *));
return 0;
}
int pktcnt=0;
static int topx_process(mapidflib_function_instance_t *instance,
......@@ -510,7 +513,7 @@ static int topx_process(mapidflib_function_instance_t *instance,
data->previous_reset = cur_secs;
}
else if(cur_secs - data->previous_reset > data->reset_interval) {
topx_reset(instance);
topx_cleanup(instance);
data->previous_reset = cur_secs;
gettimeofday(&tv, NULL);
data->last_rst = tv.tv_sec * 1000000 + tv.tv_usec;
......@@ -654,8 +657,8 @@ static mapidflib_function_def_t topfinfo={
topx_init, //init
topx_process,
NULL, //get_result,
topx_reset,
NULL, //cleanup
NULL, // reset
topx_cleanup, //cleanup
NULL, //client_init
NULL, //client_read_result
NULL //client_cleanup
......@@ -666,6 +669,3 @@ mapidflib_function_def_t* topx_get_funct_info();
mapidflib_function_def_t* topx_get_funct_info() {
return &topfinfo;
};
/*******************************************
/*************************************************
*
* Testing PKT_COUNTER
* Testing TOP - Extra MAPI Function Library
*
*******************************************/
************************************************/
#include <stdio.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include "mapi.h"
#include "mapi/topx.h"
#include "../test.h"
void terminate();
int fd;
int err_no = 0;
char error[512];
int main(int argc, char *argv[]){
int main(MAPI_UNUSED int argc, char *argv[])
{
int fd;
int fid;
int flag = 0;
unsigned int i;
mapi_results_t *cnt;
struct topx_result *tmp;
if(!argv[1])
{
printf("\nWrong arguments\n");
signal(SIGINT, terminate);
signal(SIGQUIT, terminate);
signal(SIGTERM, terminate);
if(argc != 2){
printf("Usage: %s <interface> \n", argv[0]);
return -1;
}
/*
* Offline tests
*/
if( (fd = mapi_create_offline_flow("../tracefile", MFF_PCAP)) < 0){
fprintf(stderr, "Could not create flow using tracefile\n");
mapi_read_error(&err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
DOT;
if( (fid = mapi_apply_function(fd, "TOP", 10, TOPX_TCP, TOPX_TCP_DSTPORT)) == -1){
fprintf(stderr, "Could not apply function TOP to flow %d\n", fd);
mapi_read_error(&err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
DOT;
if(mapi_connect(fd) < 0){
fprintf(stderr, "Could not connect to flow %d\n", fd);
mapi_read_error(&err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
DOT;
sleep(2);
cnt = mapi_read_results(fd, fid);
printf("\nresults: %d\n", *((int*)cnt->res));
tmp = (struct topx_result *)(((int*)cnt->res) + 1);
for(i = 0; i < (*((unsigned int*)cnt->res)); i++){
printf("\n\tPort: %u ", tmp->value);
printf("\n\tPackets: %u ", tmp->count);
printf("\n\tBytes: %llu\n", tmp->bytecount);
tmp++;
}
DOT;
if(mapi_close_flow(fd) < 0){
fprintf(stderr, "Close flow failed\n");
mapi_read_error(&err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
/*
* Offline error checking
*/
fd = fid = 0;
//fd=mapi_create_offline_flow(argv[1],MFF_PCAP);
if( (fd = mapi_create_offline_flow("../tracefile", MFF_PCAP)) < 0){
fprintf(stderr, "Could not create flow using tracefile\n");
mapi_read_error(&err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
DOT;
if( (fid = mapi_apply_function(fd, "TOP", 1001, TOPX_TCP, TOPX_TCP_DSTPORT)) == -1){ // maximum value of first argument is 1000
mapi_read_error(&err_no, error);
printf("\nTesting error case1: Errorcode :%d description: %s \n", err_no, error);
if(err_no != 7001){
fprintf(stderr, "\nWARNING: mapi_apply_function returned wrong errorcode\n");
flag = 1;
}
}
DOT;
if( (fid = mapi_apply_function(fd, "TOP", 10, 4, TOPX_TCP_DSTPORT)) == -1){ // protocol: TOPX_IP = 1, TOPX_TCP = 2, TOPX_UDP = 3
mapi_read_error(&err_no, error);
printf("Testing error case2: Errorcode :%d description: %s \n", err_no, error);
if(err_no != 7002){
fprintf(stderr, "\nWARNING: mapi_apply_function returned wrong errorcode\n");
flag = 1;
}
}
DOT;
if( (fid = mapi_apply_function(fd, "TOP", 10, TOPX_IP, TOPX_TCP_DSTPORT)) == -1){ // different protocol and field
mapi_read_error(&err_no, error);
printf("Testing error case3: Errorcode :%d description: %s \n", err_no, error);
if(err_no != 7003){
fprintf(stderr, "\nWARNING: mapi_apply_function returned wrong errorcode\n");
flag = 1;
}
}
DOT;
if(mapi_close_flow(fd) < 0){
fprintf(stderr, "Close flow failed\n");
mapi_read_error(&err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
DOT;
if (!flag)
printf("\n\nTOP Offline Error Checking OK\n\n");
else{
fprintf(stderr,"\n\nTOP Offline Error Checking :FAILED:\n\n");
exit(EXIT_FAILURE);
}
fd=mapi_create_flow(argv[1]);
fd = fid = flag = 0;
fid=mapi_apply_function(fd,"TOP",10,TOPX_TCP,TOPX_TCP_DSTPORT);
/*
* Online error checking
*/
if ((fd = mapi_create_flow(argv[1])) < 0) {
fprintf(stderr, "Could not create flow using '%s'\n", argv[1]);
mapi_read_error(&err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
DOT;
if( (fid = mapi_apply_function(fd, "TOP", 1001, TOPX_TCP, TOPX_TCP_DSTPORT)) == -1){ // maximum value of first argument is 1000
mapi_read_error(&err_no, error);
printf("Testing error case1: Errorcode :%d description: %s \n", err_no, error);
if(err_no != 7001){
fprintf(stderr, "\nWARNING: mapi_apply_function returned wrong errorcode\n");
flag = 1;
}
}
DOT;
if( (fid = mapi_apply_function(fd, "TOP", 10, 4, TOPX_TCP_DSTPORT)) == -1){ // protocol: TOPX_IP = 1, TOPX_TCP = 2, TOPX_UDP = 3
mapi_read_error(&err_no, error);
printf("Testing error case2: Errorcode :%d description: %s \n", err_no, error);
if(err_no != 7002){
fprintf(stderr, "\nWARNING: mapi_apply_function returned wrong errorcode\n");
flag = 1;
}
}
DOT;
if( (fid = mapi_apply_function(fd, "TOP", 10, TOPX_IP, TOPX_TCP_DSTPORT)) == -1){ // different protocol and field
mapi_read_error(&err_no, error);
printf("Testing error case3: Errorcode :%d description: %s \n", err_no, error);
if(err_no != 7003){
fprintf(stderr, "\nWARNING: mapi_apply_function returned wrong errorcode\n");
flag = 1;
}
}
DOT;
if(mapi_close_flow(fd) < 0){
fprintf(stderr, "Close flow failed\n");
mapi_read_error( &err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n" ,err_no, error);
return -1;
}
DOT;
if (!flag)
printf("\n\nTOP Error Checking OK\n\n");
else{
fprintf(stderr,"\n\nTOP Error Checking :FAILED:\n\n");
exit(EXIT_FAILURE);
}
DOT;
fd = fid = 0;
mapi_connect(fd);
/*
* Online tests
*/
if( (fd = mapi_create_flow(argv[1])) < 0){
fprintf(stderr, "Could not create flow using '%s'\n", argv[1]);
mapi_read_error(&err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
DOT;
if( (fid = mapi_apply_function(fd, "TOP", 10, TOPX_TCP, TOPX_TCP_DSTPORT)) == -1){
fprintf(stderr, "Could not apply function TOP to flow %d\n", fd);
mapi_read_error(&err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
DOT;
if(mapi_connect(fd) < 0){
fprintf(stderr, "Could not connect to flow %d\n", fd);
mapi_read_error(&err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
return -1;
}
DOT;
while(1){ /* infinite loop */
while(1)
{
sleep(1);
cnt=mapi_read_results(fd,fid);
printf("results: %d\n",*((int*)cnt->res));
cnt = mapi_read_results(fd, fid);
printf("\nresults: %d\n", *((int*)cnt->res));
//counter,{ res1 },{ res2 },...,{ res_counter }
tmp=(struct topx_result *)(((int*)cnt->res)+1);
tmp = (struct topx_result *)(((int*)cnt->res) + 1);
for(i=0;i<(*((unsigned int*)cnt->res));i++) {
printf("\tPort: %u ",tmp->value);
printf("Packets: %u ",tmp->count);
printf("Bytes: %llu\n",tmp->bytecount);
for(i = 0; i < (*((unsigned int*)cnt->res)); i++){
printf("\n\tPort: %u ", tmp->value);
printf("\n\tPackets: %u ", tmp->count);
printf("\n\tBytes: %llu\n", tmp->bytecount);
tmp++;
}
}
mapi_close_flow(fd);
return 0;
}
printf("\nTOP OK\n");
void terminate(){
return 0;
printf("\nTesting TOP Terminated\n");
if(mapi_close_flow(fd) < 0){
fprintf(stderr, "Close flow failed\n");
mapi_read_error(&err_no, error);
fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
exit(EXIT_FAILURE);
}
printf("TOP OK\n");
exit(EXIT_SUCCESS);
}
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