Exemplo n.º 1
0
void track_who(void)
{
	int pkts = 0, bytes = 0;
	mapi_results_t *res = NULL;
	who.bytes = 0;
	who.pkts = 0;
	pthread_mutex_init(&who.lock, NULL);
	
	
	if(offline)
	{
		who.fd = mapi_create_offline_flow(readfile, MFF_PCAP);
	}
	else
	{
		who.fd = mapi_create_flow(DEVICE);
	}

	who.filter = mapi_apply_function(who.fd, "BPF_FILTER", "udp port 513");

	who.pkt_counter = mapi_apply_function(who.fd, "PKT_COUNTER");

	who.byte_counter = mapi_apply_function(who.fd, "BYTE_COUNTER");

	mapi_connect(who.fd);

	
	while(1)
	{
		sleep(2);


		res = mapi_read_results(who.fd, who.pkt_counter);


		if(res)
		{
			pkts = *((int*)res->res);
			res = NULL;
		}
		

		res = mapi_read_results(who.fd, who.byte_counter);


		if(res)
		{
			bytes = *((int*)res->res);
			res = NULL;
		}
		
		pthread_mutex_lock(&who.lock);
		
		who.pkts = pkts;
		who.bytes = bytes;

		pthread_mutex_unlock(&who.lock);
	}

}
int main(MAPI_UNUSED int argc, char *argv[])
{
	int fd;
	mapi_flow_info_t info;
	int err_no;
	char error[512];

	if((fd = mapi_create_offline_flow(argv[1], MFF_PCAP)) < 0) {
		fprintf(stderr,"Could not create offline flow\n");
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
	DOT;
	if(mapi_apply_function(fd, "ANONYMIZE", "IP,SRC_IP,RANDOM") < 0) {
		printf("Could not apply ANONYMIZE IP,SRC_IP,RANDOM \n");
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
	DOT;
	if(mapi_apply_function(fd, "TO_FILE", MFF_PCAP, argv[2], 0) < 0) {
		fprintf(stderr, "Could not apply TO_FILE 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;

	do {
		if(mapi_get_flow_info(fd, &info) < 0) {
			fprintf(stderr, "Could not apply PKT_COUNTER 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(info.status != FLOW_FINISHED);

	if (mapi_close_flow(fd) < 0) {
		fprintf(stderr, "Could not close flow %d\n", fd);
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
	DOT;

	return 0;
}
Exemplo n.º 3
0
void track_ssdp(void)
{
    int pkts = 0, bytes = 0;
    mapi_results_t *res = NULL;
    ssdp.bytes = 0;
    ssdp.pkts = 0;
    pthread_mutex_init(&ssdp.lock, NULL);

    if(offline)
    {
        ssdp.fd = mapi_create_offline_flow(readfile, MFF_PCAP);
    }
    else
    {
        ssdp.fd = mapi_create_flow(DEVICE);
    }

    ssdp.filter = mapi_apply_function(ssdp.fd, "BPF_FILTER", "port 1900");

    ssdp.pkt_counter = mapi_apply_function(ssdp.fd, "PKT_COUNTER");

    ssdp.byte_counter = mapi_apply_function(ssdp.fd, "BYTE_COUNTER");

    mapi_connect(ssdp.fd);

    while(1)
    {
        sleep(2);

        res = mapi_read_results(ssdp.fd, ssdp.pkt_counter);

        if(res)
        {
            pkts = *((int*)res->res);
            res = NULL;
        }

        res = mapi_read_results(ssdp.fd, ssdp.byte_counter);

        if(res)
        {
            bytes = *((int*)res->res);
            res = NULL;
        }

        pthread_mutex_lock(&ssdp.lock);

        ssdp.pkts = pkts;
        ssdp.bytes = bytes;

        pthread_mutex_unlock(&ssdp.lock);
    }

}
Exemplo n.º 4
0
void createFlows(char *dev,int ifindex,char *start, char *stop, char *path) {
  int byte,pkt,stats1[INTERVALS],stats2[INTERVALS];
  int i;
  char str[256],*space;
  char fids[128],types[128];
  char intervals[4][20]={"1s","100ms","10ms"};

  for(i=0;i<INTERVALS;i++) {
    nextfd[i]=mapi_create_flow(dev);
    mapi_apply_function(nextfd[i],"INTERFACE",ifindex);
    mapi_apply_function(nextfd[i],"STARTSTOP",start,stop);
    byte=mapi_apply_function(nextfd[i],"BYTE_COUNTER");
    pkt=mapi_apply_function(nextfd[i],"PKT_COUNTER");
    stats1[i]=mapi_apply_function(nextfd[i],"STATS",nextfd[i],byte,intervals[i]);
    stats2[i]=mapi_apply_function(nextfd[i],"STATS",nextfd[i],pkt,intervals[i]);
    mapi_connect(nextfd[i]);
  }

  nextfd[INTERVALS]=mapi_create_flow(dev);
  mapi_apply_function(nextfd[INTERVALS],"INTERFACE",ifindex);
  mapi_apply_function(nextfd[INTERVALS],"STARTSTOP",start,stop);
  byte=mapi_apply_function(nextfd[INTERVALS],"BYTE_COUNTER");
  pkt=mapi_apply_function(nextfd[INTERVALS],"PKT_COUNTER");
  mapi_connect(nextfd[INTERVALS]);

  nextfd[INTERVALS+1]=mapi_create_flow(dev);
  mapi_apply_function(nextfd[INTERVALS+1],"INTERFACE",ifindex);
  mapi_apply_function(nextfd[INTERVALS+1],"STARTSTOP",start,"+400s");
 snprintf(fids,128,"%d@%d,%d@%d,%d@%d,%d@%d,%d@%d,%d@%d,%d@%d,%d@%d",
	  byte,nextfd[INTERVALS],pkt,nextfd[INTERVALS],
	  stats1[0],nextfd[0],stats2[0],nextfd[0],stats1[1],nextfd[1],stats2[1],nextfd[1],
	  stats1[2],nextfd[2],stats2[2],nextfd[2]);
 snprintf(types,128,"%d,%d,%d,%d,%d,%d,%d,%d",R2F_ULLSTR,R2F_ULLSTR,R2F_STATS,R2F_STATS,R2F_STATS,R2F_STATS,R2F_STATS,R2F_STATS);
  sprintf(str,"%s/tmp_%s.txt",path,start);
  space=strchr(str,' ');
  *space='_';

  mapi_apply_function(nextfd[INTERVALS+1],"RES2FILE",types,fids,"bytes1s packets1s bytes100ms packets100ms bytes10ms packets10ms",str,"300s",1);
  mapi_connect(nextfd[INTERVALS+1]);

}
Exemplo n.º 5
0
int
main(int argc, char **argv)
{
	Mapi dbh;
	MapiHdl hdl = NULL;
	int i, port, n = 20000;
	char buf[40];
	int lang = 1;
	char *l = "sql";

	/* char *line; */

	if (argc != 2 && argc != 3) {
		printf("usage:smack00 <port> [<language>]\n");
		exit(-1);
	}
	if (argc == 3) {
		l = argv[2];
		if (strcmp(argv[2], "sql") == 0) 
			lang = 1;
		else if (strcmp(argv[2], "mal") == 0)
			lang = 3;
	}

	port = atol(argv[1]);
	dbh = mapi_connect("localhost", port, "monetdb", "monetdb", l, NULL);
	if (dbh == NULL || mapi_error(dbh))
		die(dbh, hdl);

	/* switch of autocommit */
	if (lang==1 && (mapi_setAutocommit(dbh, 0) != MOK || mapi_error(dbh)))
		die(dbh,NULL);

	for (i = 0; i < n; i++) {
		if (lang==1)
			snprintf(buf, 40, "select %d;", i);
		else
			snprintf(buf, 40, "io.print(%d);", i);
		if ((hdl = mapi_query(dbh, buf)) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		while ( (/*line= */ mapi_fetch_line(hdl)) != NULL) {
			/*printf("%s \n", line); */
		}
		if (mapi_error(dbh))
			die(dbh, hdl);
		if (mapi_close_handle(hdl) != MOK)
			die(dbh, hdl);
	}
	mapi_destroy(dbh);

	return 0;
}
Exemplo n.º 6
0
CAMLprim value mapi_connect_stub_bc(value *argv, int argn)
{
    assert(argn == 6);
    const char *host_str = String_val(argv[0]);
    const int port_i = Int_val(argv[1]);
    assert(port_i > 0 && port_i < 65536);
    const char *user_str = String_val(argv[2]);
    const char *passwd_str = String_val(argv[3]);
    const char *lang_str = String_val(argv[4]);
    const char *db_str = String_val(argv[5]);
    Mapi conn = mapi_connect(host_str, port_i, user_str, passwd_str, lang_str, db_str);
    if (conn) {
	return Val_some((value) conn);
    } else {
	return Val_none;
    }
}
Exemplo n.º 7
0
CAMLprim value mapi_connect_stub_native(value host, value port, value user, 
					value passwd, value lang, value db)
{
    const char *host_str = String_val(host);
    const int port_i = Int_val(port);
    assert(port_i > 0 && port_i < 65536);
    const char *user_str = String_val(user);
    const char *passwd_str = String_val(passwd);
    const char *lang_str = String_val(lang);
    const char *db_str = String_val(db);
    Mapi conn = mapi_connect(host_str, port_i, user_str, passwd_str, lang_str, db_str);
    if (conn) {
	return Val_some((value) conn);
    } else {
	return Val_none;
    }
}
Exemplo n.º 8
0
int main(MAPI_UNUSED int argc, char *argv[])
{
	int fd;
	int fid,fid2,fid3,fid4;
	int *cnt,*cnt2,*cnt3,*cnt4;

	if(!argv[1])
	{
		printf("\nWrong arguments\n");
		
		return -1;
	}

	fd=mapi_create_offline_flow(argv[1],MFF_PCAP);

	mapi_apply_function(fd, "BPF_FILTER", "tcp or udp");
	fid=mapi_apply_function(fd,"PKT_COUNTER");
	fid2=mapi_apply_function(fd,"BYTE_COUNTER");
//	mapi_apply_function(fd,"TRACK_FTP");
//	mapi_apply_function(fd,"TRACK_GNUTELLA");
//	mapi_apply_function(fd,"TRACK_TORRENT");
//	mapi_apply_function(fd,"TRACK_DC");
	mapi_apply_function(fd, "TRACK_EDONKEY");
	fid3=mapi_apply_function(fd,"PKT_COUNTER");
	fid4=mapi_apply_function(fd,"BYTE_COUNTER");

	mapi_connect(fd);

	cnt=mapi_read_results(fd,fid,MAPI_REF);
	cnt2=mapi_read_results(fd,fid2,MAPI_REF);
	cnt3=mapi_read_results(fd,fid3,MAPI_REF);
	cnt4=mapi_read_results(fd,fid4,MAPI_REF);
	
	while(1)
	{
		sleep(1);
		printf("Total packets: %d Total bytes: %d FTP packets: %d FTP bytes: %d\n",*cnt,*cnt2,*cnt3,*cnt4);
	}

	mapi_close_flow(fd);

	printf("\nPKT_COUNTER OK\n");

	return 0;
}
int main(int argc, char *argv[])
{
	int fd;
	int fid;
	mapi_results_t *cnt;
	int err_no =0 , flag=0;
	char error[512];

	if(argc!=2)
	{
		printf("\nWrong arguments\n");
		
		return -1;
	}

	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, "PKT_COUNTER"))<0){
	  	fprintf(stderr, "Could not apply PKT_COUNTER 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, "Connecting to flow failed %d\n", fd);
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;
	sleep(3);
	
	if( (cnt = mapi_read_results(fd, fid))==NULL ){
		fprintf(stderr, "Mapi read results failed\n");
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}

	/*
	 * Sanity check. Assuming that we have presence of network traffic
	 */
	if( *((int*)cnt->res)<=0)
		fprintf(stderr, "WARNING: No packets captured. Maybe no traffic on line..\n");
	else 
		printf("\nRead_results : %d\n", *((int*)cnt->res));
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;
	/*
	 * Error checking 
	 */
	fd = fid = 0;
	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, "PKT_COUNTER"))<0){
	  	fprintf(stderr, "Could not apply PKT_COUNTER 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, "Connecting to flow failed %d\n", fd);
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;	
	if(mapi_read_results(123,fid) ==NULL || mapi_read_results(fd,-1244655) ==NULL){
		mapi_read_error( &err_no, error);
		printf("Testing error case1: Errorcode :%d description: %s \n" ,err_no, error);
		if(err_no != 6145){
			printf("          Wrong ERRORCODE returned\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;

	/*
	 * Offline tests
	 */
	if ((fd = mapi_create_offline_flow("./tracefile" , MFF_PCAP))<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, "PKT_COUNTER"))<0){
	  	fprintf(stderr, "Could not apply PKT_COUNTER 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, "Connecting to flow failed %d\n", fd);
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;
	
	if( (cnt = mapi_read_results(fd, fid))==NULL ){
		fprintf(stderr, "Mapi read results failed\n");
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}

	/*
	 * Sanity check. Assuming that we have presence of network traffic
	 */

	sleep(10);
	if( *((int*)cnt->res)!=893)
		fprintf(stderr, "WARNING: No packets captured. Maybe no traffic on line.. %d\n" ,*((int*)cnt->res) );
	else 
		printf("\nRead_results : %d\n", *((int*)cnt->res));
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;
	/*
	 * Error checking 
	 */
	fd = fid = 0;
	if ((fd = mapi_create_offline_flow("./tracefile",MFF_PCAP))<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, "PKT_COUNTER"))<0){
	  	fprintf(stderr, "Could not apply PKT_COUNTER 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, "Connecting to flow failed %d\n", fd);
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;	
	if(mapi_read_results(123,fid) ==NULL || mapi_read_results(fd,-1244655) ==NULL){
		mapi_read_error( &err_no, error);
		printf("Testing error case2: Errorcode :%d description: %s \n" ,err_no, error);
		if(err_no != 6145){
			printf("          Wrong ERRORCODE returned\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;
	printf("\nmapi_read_results OK\n");
	if(!flag)
		printf("\nMAPI read results Error Checking OK\n");
	else 
		printf("\nMAPI read results Error Checking :FAILED:\n");

return 0;
}
Exemplo n.º 10
0
int main(int argc, char *argv[])
{
	int fd, fd2;
	int err;
	int fid_bpf2;
	int fid_cnt2;
	int fid_to_buffer;
	mapi_results_t *cnt;
	struct mapipkt *pkt;
	int err_no =0 , flag=0 , ret;
	char error[512];
	
	if(argc!=2)	{
		printf("\nWrong arguments\n");
		return -1;
	}
	
	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_to_buffer = mapi_apply_function(fd, "TO_BUFFER", 0))<0){
	  	fprintf(stderr, "Could not apply TO_BUFFER 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 ((fd2 = 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_bpf2 = mapi_apply_function(fd2, "BPF_FILTER", "ip src net 195.113.0.0/16 and ip dst net 147.251.0.0/16"))<0){
	  	fprintf(stderr, "Could not apply BPF_FILTER 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((fid_cnt2 = mapi_apply_function(fd2, "PKT_COUNTER"))<0){
	  	fprintf(stderr, "Could not apply PKT_COUNTER 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((err=mapi_connect(fd))<0){
	 	fprintf(stderr, "Connecting to flow failed %d\n", fd);
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}

DOT;	
	if((err=mapi_connect(fd2))<0){
	 	fprintf(stderr, "Connecting to flow failed %d\n", fd);
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}

DOT;	

	if(err == 0)
	{
		printf("\nConnected\n");
	}
	else if(err == -2)
	{
		printf("\nauthorization error\n");
		return -1;
	}
	else if(err == -1)
	{
		printf("\nACK error\n");
		return -1;
	}
	else 
	{
		printf("\nerror at mapi_connect\n");
		return -1;
	}

	if ((pkt=mapi_get_next_pkt(fd, fid_to_buffer)) != NULL)
                  printf("Got packet\n");
      else
			printf("Error in mapi_get_next_packet\n");
                
      cnt = mapi_read_results(fd2, fid_cnt2);
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(mapi_close_flow(fd2)<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;
	printf("\nmapi_connect OK\n");
	/*
	 * Error checking
	 */

	fd = 0;
	
	if( (ret = mapi_connect(13244)) == -1){
		mapi_read_error( &err_no, error);
		printf("\nTesting error case1: Errorcode :%d description: %s \n" ,err_no, error);
		if(err_no != 6145){
			printf("          Wrong ERRORCODE returned\n");
			flag = 1;	
		}
	}
DOT;
	fd = mapi_create_flow(argv[1]);
	
	if( (ret = mapi_connect(fd)) == -1){
		mapi_read_error( &err_no, error);
		printf("\nERROR: Errorcode :%d description: %s \n" ,err_no, error);
		flag=1;
	}
DOT;	
	if( (ret = mapi_connect(fd)) == -1){
		mapi_read_error( &err_no, error);
		printf("\nTesting error case2: Errorcode :%d description: %s \n" ,err_no, error);
		if(err_no != 3081){
			printf("          Wrong ERRORCODE returned\n");
			flag = 1;	
		}
	}
DOT;

/*
 * Offline testing
 */
	if ((fd = mapi_create_offline_flow("./tracefile", MFF_PCAP))<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_to_buffer = mapi_apply_function(fd, "TO_BUFFER", 0))<0){
	  	fprintf(stderr, "Could not apply TO_BUFFER 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 ((fd2 = 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_bpf2 = mapi_apply_function(fd2, "BPF_FILTER", "ip src net 195.113.0.0/16 and ip dst net 147.251.0.0/16"))<0){
	  	fprintf(stderr, "Could not apply BPF_FILTER 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((fid_cnt2 = mapi_apply_function(fd2, "PKT_COUNTER"))<0){
	  	fprintf(stderr, "Could not apply PKT_COUNTER 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((err=mapi_connect(fd))<0){
	 	fprintf(stderr, "Connecting to flow failed %d\n", fd);
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}

DOT;	
	if((err=mapi_connect(fd2))<0){
	 	fprintf(stderr, "Connecting to flow failed %d\n", fd);
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}

DOT;	

	if(err == 0){
		printf("\nConnected\n");
	}
	else if(err == -2){
		printf("\nauthorization error\n");
		return -1;
	}
	else if(err == -1){
		printf("\nACK error\n");
		return -1;
	}
	else {
		printf("\nerror at mapi_connect\n");
		return -1;
	}

	if ((pkt=mapi_get_next_pkt(fd, fid_to_buffer)) != NULL)
                  printf("Got packet\n");
      else
			printf("Error in mapi_get_next_packet\n");
                
      cnt = mapi_read_results(fd2, fid_cnt2);
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(mapi_close_flow(fd2)<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;
	printf("\nmapi_connect OK\n");
	/*
	 * Error checking
	 */

	fd = 0;
	
	if( (ret = mapi_connect(13244)) == -1){
		mapi_read_error( &err_no, error);
		printf("\nTesting error case1: Errorcode :%d description: %s \n" ,err_no, error);
		if(err_no != 6145){
			printf("          Wrong ERRORCODE returned\n");
			flag = 1;	
		}
	}
DOT;
	fd = mapi_create_offline_flow("./tracefile" , MFF_PCAP);
	
	if( (ret = mapi_connect(fd)) == -1){
		mapi_read_error( &err_no, error);
		printf("\nERROR: Errorcode :%d description: %s \n" ,err_no, error);
		flag=1;
	}
DOT;	
	if( (ret = mapi_connect(fd)) == -1){
		mapi_read_error( &err_no, error);
		printf("\nTesting error case2: Errorcode :%d description: %s \n" ,err_no, error);
		if(err_no != 3081){
			printf("          Wrong ERRORCODE returned\n");
			flag = 1;	
		}
	}
DOT;


	if(!flag)
		printf("\nMAPI Connect Error Checking OK\n");
	else 
		printf("\nMAPI Connect Error Checking :FAILED:\n");


	return 0;
}
Exemplo n.º 11
0
int main(MAPI_UNUSED int argc, char *argv[])
{
  int size;
  int fd;
  int fid;
  mapi_flow_info_t info;
  mapi_results_t* res;
  stats_t *stats;
  int err_no =0 , flag=0;
  char error[512];
  
  if(!argv[1])
    {
      printf("\nWrong arguments\n");
      
      return -1;
    }
  
	if(argc!=2){ // just for uniformity reasons 
		  fprintf(stderr, "Wrong Arguments\n");
		  return -1;
	}
		  
	if ((fd = mapi_create_offline_flow("./tracefile" , MFF_PCAP))<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((size=mapi_apply_function(fd,"PKTINFO",PKT_SIZE))<0){
	  	fprintf(stderr, "Could not apply PKTINFO 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((fid=mapi_apply_function(fd,"STATS",fd,size,"0"))<0){
	  	fprintf(stderr, "Could not apply STATS 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, "Connecting to flow failed %d\n", fd);
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;
  
  do {
    sleep(1);
    mapi_get_flow_info(fd,&info);
  } while(info.status!=FLOW_FINISHED);
  
  res = mapi_read_results(fd,fid);
  stats = (stats_t*)res->res;
/*
 * Sanity Checks
 */
  if(stats->count!=893 || stats->sum!=283464.0 ||
     stats->sum2!=309175572.0 || stats->min!=42.0 ||
     stats->max!=1514) {
    printf("Wrong values\n");
    return -1;
  }
 	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;	
 
  
  printf("Offline STATS OK\n");
/*
 * Error checking test postponed till correcting a
 */
	if ((fd = mapi_create_offline_flow("./tracefile" , MFF_PCAP))<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((size=mapi_apply_function(fd,"PKTINFO",PKT_SIZE))<0){
	  	fprintf(stderr, "Could not apply PKTINFO to flow %d\n", fd);
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}

DOT;	

  fid=mapi_apply_function(fd,"STATS",1234343,size,"0");
  if (fid<0){
  	mapi_read_error( &err_no, error);
	printf("Testing error case1: Errorcode :%d description: %s \n" ,err_no, error);
		if(err_no != 7001){
			printf("          Wrong ERRORCODE returned\n");
			flag = 1;	
		}
  }
DOT;  
  fid=mapi_apply_function(fd,"STATS",fd,-12352,"0");
  if (fid<0){
  	mapi_read_error( &err_no, error);
	printf("Testing error case2: Errorcode :%d description: %s \n" ,err_no, error);
		if(err_no != 7001){
			printf("          Wrong ERRORCODE returned\n");
			flag = 1;	
		}
  }
DOT;
  if(!flag)
		printf("\nTest offline stats Error Checking OK\n");
  else 
		printf("\nTest offline stats Error Checking :FAILED:\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);
	return -1;
  }
DOT;	

return 0;
}
Exemplo n.º 12
0
int
main(int argc, char **argv)
{
	int port = 0;
	char *user = NULL;
	char *passwd = NULL;
	char *host = NULL;
	char *dbname = NULL;
	int trace = 0;
	int describe = 0;
	int functions = 0;
	int useinserts = 0;
	int c;
	Mapi mid;
	int quiet = 0;
	stream *out;
	char user_set_as_flag = 0;
	char *table = NULL;
	static struct option long_options[] = {
		{"host", 1, 0, 'h'},
		{"port", 1, 0, 'p'},
		{"database", 1, 0, 'd'},
		{"describe", 0, 0, 'D'},
		{"functions", 0, 0, 'f'},
		{"table", 1, 0, 't'},
		{"inserts", 0, 0, 'N'},
		{"Xdebug", 0, 0, 'X'},
		{"user", 1, 0, 'u'},
		{"quiet", 0, 0, 'q'},
		{"help", 0, 0, '?'},
		{0, 0, 0, 0}
	};

	parse_dotmonetdb(&user, &passwd, NULL, NULL, NULL, NULL);

	while ((c = getopt_long(argc, argv, "h:p:d:Dft:NXu:q?", long_options, NULL)) != -1) {
		switch (c) {
		case 'u':
			if (user)
				free(user);
			user = strdup(optarg);
			user_set_as_flag = 1;
			break;
		case 'h':
			host = optarg;
			break;
		case 'p':
			assert(optarg != NULL);
			port = atoi(optarg);
			break;
		case 'd':
			dbname = optarg;
			break;
		case 'D':
			describe = 1;
			break;
		case 'N':
			useinserts = 1;
			break;
		case 'f':
			if (table)
				usage(argv[0], -1);
			functions = 1;
			break;
		case 't':
			if (table || functions)
				usage(argv[0], -1);
			table = optarg;
			break;
		case 'q':
			quiet = 1;
			break;
		case 'X':
			trace = MAPI_TRACE;
			break;
		case '?':
			/* a bit of a hack: look at the option that the
			   current `c' is based on and see if we recognize
			   it: if -? or --help, exit with 0, else with -1 */
			usage(argv[0], strcmp(argv[optind - 1], "-?") == 0 || strcmp(argv[optind - 1], "--help") == 0 ? 0 : -1);
		default:
			usage(argv[0], -1);
		}
	}

	if (optind == argc - 1)
		dbname = argv[optind];
	else if (optind != argc)
		usage(argv[0], -1);

	/* when config file would provide defaults */
	if (user_set_as_flag)
		passwd = NULL;

	if (user == NULL)
		user = simple_prompt("user", BUFSIZ, 1, prompt_getlogin());
	if (passwd == NULL)
		passwd = simple_prompt("password", BUFSIZ, 0, NULL);

	mid = mapi_connect(host, port, user, passwd, "sql", dbname);
	if (user)
		free(user);
	if (passwd)
		free(passwd);
	if (mid == NULL) {
		fprintf(stderr, "failed to allocate Mapi structure\n");
		exit(2);
	}
	if (mapi_error(mid)) {
		mapi_explain(mid, stderr);
		exit(2);
	}
	if (!quiet) {
		char *motd = mapi_get_motd(mid);

		if (motd)
			fprintf(stderr, "%s", motd);
	}
	mapi_trace(mid, trace);
	mapi_cache_limit(mid, 10000);

	out = file_wastream(stdout, "stdout");
	if (out == NULL) {
		fprintf(stderr, "failed to allocate stream\n");
		exit(2);
	}
	if (!quiet) {
		char buf[27];
		time_t t = time(0);
		char *p;

#ifdef HAVE_CTIME_R3
		ctime_r(&t, buf, sizeof(buf));
#else
#ifdef HAVE_CTIME_R
		ctime_r(&t, buf);
#else
		strncpy(buf, ctime(&t), sizeof(buf));
#endif
#endif
		if ((p = strrchr(buf, '\n')) != NULL)
			*p = 0;
		mnstr_printf(out, "-- msqldump %s %s%s %s\n",
			     describe ? "describe" : "dump",
			     functions ? "functions" : table ? "table " : "database",
			     table ? table : "", buf);
		dump_version(mid, out, "--");
	}
	if (functions)
		c = dump_functions(mid, out, NULL, NULL);
	else if (table)
		c = dump_table(mid, NULL, table, out, describe, 1, useinserts);
	else
		c = dump_database(mid, out, describe, useinserts);
	mnstr_flush(out);

	mapi_destroy(mid);
	if (mnstr_errnr(out)) {
		fprintf(stderr, "%s: %s", argv[0], mnstr_error(out));
		return 1;
	}

	mnstr_destroy(out);
	return c;
}
Exemplo n.º 13
0
int main(MAPI_UNUSED int argc, char *argv[])
{
	int fd, ok;
	mapi_flow_info_t info;
	int err_no =0 , flag=0;
	char error[512];

	if(!argv[1])
	{
		printf("\nwrong arguments\n");

		return -1;
	}
	
	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((ok = mapi_apply_function(fd, "PKT_COUNTER"))<0){
		fprintf(stderr,"Could not apply function PKT_COUNTER\n");  
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;	
	if((ok = mapi_apply_function(fd, "BYTE_COUNTER"))<0){
		fprintf(stderr,"Could not apply function BYTE_COUNTER\n");  
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;
	if((ok = mapi_apply_function(fd, "PKT_COUNTER"))<0){
		fprintf(stderr,"Could not apply function PKT_COUNTER\n");  
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;
	if((ok = mapi_apply_function(fd, "BYTE_COUNTER"))<0){
		fprintf(stderr,"Could not apply function BYTE_COUNTER\n");  
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;
	if((ok = mapi_apply_function(fd, "PKT_COUNTER"))<0){
		fprintf(stderr,"Could not apply function PKT_COUNTER\n");  
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;
	if((ok = mapi_apply_function(fd, "BYTE_COUNTER"))<0){
		fprintf(stderr,"Could not apply function BYTE_COUNTER\n");  
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;	
	if((ok = mapi_apply_function(fd, "PKT_COUNTER"))<0){
		fprintf(stderr,"Could not apply function PKT_COUNTER\n");  
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;
	if((ok = mapi_apply_function(fd, "BYTE_COUNTER"))<0){
		fprintf(stderr,"Could not apply function BYTE_COUNTER\n");  
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;
	if((ok = mapi_apply_function(fd, "PKT_COUNTER"))<0){
		fprintf(stderr,"Could not apply function PKT_COUNTER\n");  
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;
	if((ok = mapi_apply_function(fd, "BYTE_COUNTER"))<0){
		fprintf(stderr,"Could not apply function BYTE_COUNTER\n");  
		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, "Connecting to flow failed %d\n", fd);
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}

DOT;	
	if((ok = mapi_get_flow_info(fd, &info))<0){
	 	fprintf(stderr, "Getting flow info failed on fd:%d\n", fd);
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;
	printf("\nflow info : ");
	printf("\n\t uid = %d", info.uid);
	printf("\n\t device = %s", info.device);
	printf("\n\t num_functions: %u", info.num_functions);
	printf("\n\t start = %lu", info.start);
	printf("\n\t end = %lu", info.end);
	
	printf("\n");
/*
 * Sanity checks
 */
   if(info.num_functions != 10){
   	fprintf(stderr, "WARNING: Sanity check failed %d functions found\n" , info.num_functions);
   }
	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;
	/*
	 * Wrong argument error checking 
	 */
	
	fd =0;
	if(mapi_get_flow_info(fd, &info)<0){
		mapi_read_error( &err_no, error);
		printf("Testing error case0. Errorcode :%d description: %s \n" ,err_no, error);
		if(err_no != 6145){
			printf("          Wrong ERRORCODE returned\n");
			flag = 1;	
		}
	}
DOT;

	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(mapi_get_flow_info(fd, NULL)<0){
		mapi_read_error( &err_no, error);
		printf("Testing error case1. Errorcode :%d description: %s \n" ,err_no, error);
		if(err_no != 7002){
			printf("          Wrong ERRORCODE returned\n");
			flag = 1;	
		}
	}
DOT;
	if(mapi_get_flow_info(12345, &info)<0){
		mapi_read_error( &err_no, error);
		printf("Testing error case2. Errorcode :%d description: %s \n" ,err_no, error);
		if(err_no != 6145){
			printf("          Wrong ERRORCODE returned\n");
			flag = 1;	
		}
	}
DOT;
	if ((fd = mapi_create_offline_flow("./tracefile" , MFF_PCAP))<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((ok = mapi_apply_function(fd, "PKT_COUNTER"))<0){
		fprintf(stderr,"Could not apply function PKT_COUNTER\n");  
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;	
	if((ok = mapi_apply_function(fd, "BYTE_COUNTER"))<0){
		fprintf(stderr,"Could not apply function BYTE_COUNTER\n");  
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;
	if((ok = mapi_apply_function(fd, "PKT_COUNTER"))<0){
		fprintf(stderr,"Could not apply function PKT_COUNTER\n");  
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;
	if((ok = mapi_apply_function(fd, "BYTE_COUNTER"))<0){
		fprintf(stderr,"Could not apply function BYTE_COUNTER\n");  
		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, "Connecting to flow failed %d\n", fd);
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
info.num_functions=0;

DOT;	
	if((ok = mapi_get_flow_info(fd, &info))<0){
	 	fprintf(stderr, "Getting flow info failed on fd:%d\n", fd);
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;
/*
 * Sanity checks
 */
   if(info.num_functions != 4){
   	fprintf(stderr, "WARNING: Sanity check failed %d functions found\n" , info.num_functions);
   }
	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("\nGet Flow Info Error Checking OK\n");
	else 
		printf("\nGet Flow Info Error Checking :FAILED:\n");

	return 0;
	
}	
Exemplo n.º 14
0
int main(int argc, char *argv[])
{ 
	configuration_t configuration;
	int i, j;
	struct timeval tm;
	struct timezone tz;
	measurement_t *measurement;
	struct timeval next, wait;
	int subject_id, flow_id;
	unsigned long long packets, bytes;
	double mbps;
	char command[MAX_COMMAND+1];
	char hostname_interface[MAX_HOSTNAME_INTERFACE+1];	/* DiMAPI connect string
															   as "hostname:interface,..." */

	struct timeval tv_start, tv_stop;	/* to measure how fast mapi_read_result()
														responds */
	int tv_diff_pkt, tv_diff_byte;		/* time used by mapi_read_results() */
	int tv_diff_threshold;		/* 1 if threshold was reached */
	mapi_results_t *pkt_counter_res;
	mapi_results_t *byte_counter_res;
	unsigned long long pkt_counter;
	unsigned long long byte_counter;
	int scope_size;
	double pkt_sec;	/* seconds from previous packet result */
	double byte_sec;	/* seconds from previous byte result */
   mapi_flow_info_t info;
   mapi_device_info_t dinfo;

	openlog("abw", LOG_PID, LOG_LOCAL0);
	syslog(LOG_DEBUG, "starting abw");

	memset((void *)&configuration, 0, (size_t)(sizeof(configuration)));

	/* Create global configuration */
	if ((configuration.global=malloc(sizeof(global_t)))==NULL) {
		fprintf(stderr, "%s: malloc() failed\n", __func__);
		return -1;
	}
	memset(configuration.global, 0, sizeof(global_t));

	/* Create first subject, scope, parameters and measurement so that they
      can be filled-in by command-line options */

	/* if ((configuration.subject=new_subject())==NULL) {
      fprintf(stderr, "%s: new_subject() failed\n", __func__);
      return -1;
	}
	if ((configuration.scope=new_scope())==NULL) {
      fprintf(stderr, "%s: new_subject() failed\n", __func__);
      return -1;
	}
	if ((configuration.parameters=new_parameters())==NULL) {
      fprintf(stderr, "%s: new_parameters() failed\n", __func__);
      return -1;
	}

	if ((configuration.measurement=new_measurement())==NULL) {
      fprintf(stderr, "%s: new_measurement() failed\n", __func__);
      return -1;
	} */

	/* Read command line */

	if (read_command_line(argc, argv, &configuration)<0) {
		fprintf(stderr, "%s: read_command_line() failed\n", __func__);
		return -1;
	}

	/* Read configuration file */

	if (configuration.global->conf_filename) {
		if (read_conf_file(&configuration)<0) {
			fprintf(stderr, "%s: read_conf_file() failed\n", __func__);
			return -1;
		}
	}

	/* Fill-in local hostname */

	if (get_local_hostname(&(configuration.global->hostname))<0) {
		fprintf(stderr, "%s: get_local_hostname() failed\n", __func__);
		return -1;
	}

	/* Check if specified values are within acceptable limits */

	if (check_conf(&configuration)<0) {
      fprintf(stderr, "%s: check_conf() failed\n", __func__);
      exit(-1);
	}

	/* Print configuration */

	if (debug)
		print_conf(&configuration);

	if (daemonize) {
		printf("Switching to daemon\n");
		if (continue_as_daemon()<0) {
			fprintf(stderr, "%s: continue_as_daemon() failed\n", __func__);
			return -1;
		}
		printf("Continuing as daemon\n");
	}

	/* 
	 * Create RRD files 
	 */

	/* Go over all measurements */

	measurement=configuration.measurement;
   while (measurement) {

		int parameters_id;
		char *filename;

		parameters_id = measurement->parameters_id;

		/* Go over all protocols */

		j=0;
		while (protocols[j].protocol) {
			if ((filename=
				abw_rrd_create_filename(measurement->scope, 
					parameters_id, protocols[j].protocol))==NULL) {
				fprintf(stderr, "%s: rrd_create_filename() failed\n", 
					__func__);
				return -1;
			}

			if (abw_rrd_create_file(filename)<0) {
				fprintf(stderr, "%s: abw_rrd_create_file() failed\n", __func__);
				return -1;
			}

			j++;
		} /* Go over all protocols */

		/* Go over all tracked protocols */

		j=0;
		while (tracked_protocols[j].protocol) {
			if ((filename=
				abw_rrd_create_filename(measurement->scope, 
					parameters_id, tracked_protocols[j].protocol))==NULL) {
				fprintf(stderr, "%s: rrd_create_filename() failed\n", 
					__func__);
				return -1;
			}

			if (abw_rrd_create_file(filename)<0) {
				fprintf(stderr, "%s: abw_rrd_create_file() failed\n", __func__);
				return -1;
			}

			j++;
		} /* Go over all tracked protocols */

		/* Create RRD file for "all" protocol (all traffic together) */

		if ((filename=
			abw_rrd_create_filename(measurement->scope, 
				parameters_id, "all"))==NULL) {
				fprintf(stderr, "%s: rrd_create_filename() failed\n", 
					__func__);
				return -1;
		}

		if (abw_rrd_create_file(filename)<0) {
			fprintf(stderr, "%s: abw_rrd_create_file() failed\n", __func__);
			return -1;
		}

		measurement=measurement->next;

   } /* while (measurement) */

	/* 
	 * Create MAPI flows 
	 */

	flow_id=0;

	/* Go over all measurements */

	measurement=configuration.measurement;
   while (measurement) {

		/* Go over all monitored protocols */

		i=0;
		while (measurement->protocols_array[i] && i<MAX_PROTOCOLS) {

			int parameters_id;
			char *protocol;

			/* Create data structure to maintain MAPI information */

			if (flow_id>=MAX_FLOWS) {
				fprintf(stderr, "%s: more than %d flows requested\n", __func__,
					MAX_FLOWS);
				return -1;
			}

			if ((flow[flow_id]=new_flow())==NULL) {
  				fprintf(stderr, "%s: new_flow() failed\n", __func__);
  				return -1;
  			}
			flow[flow_id]->measurement=measurement;
			flow[flow_id]->protocol=measurement->protocols_array[i];

			parameters_id = measurement->parameters_id;
			protocol = measurement->protocols_array[i];
				
			if ((flow[flow_id]->rrd_filename=
				abw_rrd_create_filename(measurement->scope, 
					parameters_id, protocol))==NULL) {
				fprintf(stderr, "%s: rrd_create_filename() failed\n", 
					__func__);
				return -1;
			}

			/* 
			 * If scope has only one subject and if hostname is "localhost" or
			 * equal to local hostname, then use MAPI connect string (not DiMAPI)
			 */

			if (!(measurement->scope->subject[1]) && 
				 (!strcmp(measurement->scope->subject[0]->hostname, "localhost") ||
				  !strcmp(measurement->scope->subject[0]->hostname, 
				  		configuration.global->hostname)))

				strcpy(hostname_interface, 
					measurement->scope->subject[0]->interface);

			/* 
			 * Prepare DiMAPI connect string as hostname:interface, ... 
			 */

			else {
				
				j=0; hostname_interface[0]='\0';
      		while (measurement->scope->subject[j] && j<MAX_SUBJECTS) {

					/* Append comma "," */

					if (hostname_interface[0]) {
						if (strlen(hostname_interface)+1>=MAX_HOSTNAME_INTERFACE) {
							fprintf(stderr, "%s: DiMAPI connect string is longer than %d characters\n", __func__, MAX_HOSTNAME_INTERFACE);
							return -1;
						}
						strcat(hostname_interface, ",");
					}

					/* Append next hostname:interface */
					if (strlen(hostname_interface) +
						 strlen(measurement->scope->subject[j]->hostname) +
				 		strlen(measurement->scope->subject[j]->interface) 
							>= MAX_HOSTNAME_INTERFACE) {
         			fprintf(stderr, "%s: DiMAPI connect string is longer than %d characters\n", __func__, MAX_HOSTNAME_INTERFACE);
            		return -1;
         		}
					sprintf(hostname_interface + strlen(hostname_interface), "%s:%s",
						measurement->scope->subject[j]->hostname,
						measurement->scope->subject[j]->interface);
			
					j++;
				} /* while (measurement->scope->subject[j] && j<MAX_SUBJECTS) */

			} /* Creating DiMAPI connect string */

			/* Create a new MAPI flow */

			if (debug)
				printf("%s: mapi_create_flow(%s)\n", __func__, hostname_interface);

  			if ((flow[flow_id]->fd=mapi_create_flow(hostname_interface))<0) {
				fprintf(stderr, "%s: mapi_create_flow(%s) failed\n", __func__,
					hostname_interface);
				fprintf(stderr, "%s: Do you run mapid daemon on the machine where you connect to?\n", __func__);
				fprintf(stderr, "%s: Do you run mapicommd daemon on the machine where you connect to? (if you are connecting to a non-local machine or to multiple machines)\n", __func__);
					return -1;
			}

         /* If this is a MAPI flow (not DiMAPI flow), then set MPLS and VLAN 
				flags according to mapi.conf. Otherwise the flags were set in
				abw.conf */

			if (!strchr(hostname_interface, ':')) {

				if (debug)
					printf("%s: MAPI flow on \"%s\", setting MPLS and VLAN flags from mapi.conf\n", __func__, hostname_interface);

         	if ((mapi_get_flow_info(flow[flow_id]->fd, &info)) < 0){
            	fprintf(stderr, "%s: mapi_get_flow_info() failed\n", __func__);
            	return -1;
         	}

         	if ((mapi_get_device_info(info.devid, &dinfo)) < 0) {
            	fprintf(stderr, "%s: mapi_get_device_info() failed\n", __func__);
            	return -1;
         	}

         	measurement->scope->mpls = dinfo.mpls;
         	measurement->scope->vlan = dinfo.vlan;

			}
			else
				if (debug)
               printf("%s: DiMAPI flow on \"%s\", setting MPLS and VLAN flags from abw.conf\n", __func__, hostname_interface);

			/* Prepare header filter for this protocol */

			if ((flow[flow_id]->tracked_protocol=
				protocol_filter(measurement->parameters->header_filter, 
					flow[flow_id]->protocol, measurement->scope->mpls, 
					measurement->scope->vlan,
					&(flow[flow_id]->header_filter)))<0) {
				fprintf(stderr, "%s: protocol_filter() failed\n", __func__);
				return -1;
			}

			if (debug)
				printf("measurement->parameters->header_filter: %s, flow[flow_id]->protocol: %s, flow[flow_id]->header_filter: %s, track_function: %s\n", (measurement->parameters->header_filter)?measurement->parameters->header_filter:"NULL", flow[flow_id]->protocol, (flow[flow_id]->header_filter)?flow[flow_id]->header_filter:"NULL", (flow[flow_id]->tracked_protocol)?tracked_protocols[flow[flow_id]->tracked_protocol-1].track_function:"none");

			/* Filter based on input port, we can use port number in the first 
				subject of the scope, because all subjects in a scope must have
				the same port number */

			if (measurement->scope->subject[0]->port >= 0) {
				if ((flow[flow_id]->interface_fid=mapi_apply_function(flow[flow_id]->fd, "INTERFACE", measurement->scope->subject[0]->port))<0) {
					fprintf(stderr, "%s: INTERFACE failed\n", __func__);
               return -1;
            }
			}

			/* Note that BPF_FILTER uses compiled header filter that
				selects packets of the given protocol */

			/* BPF_FILTER is applied if a) header_filter was specified in
				[parameters] section or b) protocol other than "all" and other than
				some that requires tracking was specified in [parameters] section or
				c) MPLS is used on links in this [scope] */

			if (flow[flow_id]->header_filter) {
				if (debug)
					printf("%s: mapi_apply_function(%d, BPF_FILTER, \"%s\")\n",
						__func__, flow[flow_id]->fd, flow[flow_id]->header_filter);
				if ((flow[flow_id]->bpf_filter_fid=
					mapi_apply_function(flow[flow_id]->fd, "BPF_FILTER", 
						flow[flow_id]->header_filter))<0) {
						fprintf(stderr, "%s: BPF_FILTER (\"%s\") failed\n", 
							__func__, flow[flow_id]->header_filter);
						return -1;
				}
			}

			/* Track application protocol, BPF_FILTER could have been applied 
				before */

			if (flow[flow_id]->tracked_protocol) {
				if (debug)
					printf("%s: mapi_apply_function(%d, %s)\n", __func__, 
						flow[flow_id]->fd,
						tracked_protocols[flow[flow_id]->tracked_protocol-1].
							track_function);
				if ((flow[flow_id]->track_function_fid=
					mapi_apply_function(flow[flow_id]->fd, 
						tracked_protocols[flow[flow_id]->tracked_protocol-1].
							track_function))<0) {
					fprintf(stderr, "%s: tracking (%s) failed\n", __func__, 
						tracked_protocols[flow[flow_id]->tracked_protocol-1].
							track_function);
					return -1;
				}
			}

			/* Sampling */

			if (measurement->parameters->sau_mode == 'd' && 
				 (unsigned int)(measurement->parameters->sau_threshold) != 1) {
				if ((flow[flow_id]->sample_fid=
					mapi_apply_function(flow[flow_id]->fd, "SAMPLE", 
						measurement->parameters->sau_threshold, PERIODIC))<0) {
					fprintf(stderr, "%s: SAMPLE (PERIODIC, %.02f) failed\n",
						__func__, measurement->parameters->sau_threshold);
					return -1;
				}
			}
			else if (measurement->parameters->sau_mode == 'p' && 
						(unsigned int)(measurement->parameters->sau_threshold) != 1) {
				if ((flow[flow_id]->sample_fid=
        			mapi_apply_function(flow[flow_id]->fd, "SAMPLE", 
						(measurement->parameters->sau_threshold)*100,
						PROBABILISTIC))<0) {
        			fprintf(stderr, "%s: SAMPLE (PROBABILISTIC, %.02f) failed\n", 
						__func__, (measurement->parameters->sau_threshold)*100);
        			return -1;
      		}
			}
	
			/* Payload searching */
	
			if (measurement->parameters->payload_strings[0]) {
				if ((flow[flow_id]->str_search_fid=
        			mapi_apply_function(flow[flow_id]->fd, "STR_SEARCH", 
					measurement->parameters->payload_strings[0], 0, 0))<0) {
           			fprintf(stderr, "%s: STR_SEARCH (%s) failed\n", 
						__func__, measurement->parameters->payload_strings[0]);
        	   		return -1;
       		}
			}

			/* Counting packets and bytes */

			if ((flow[flow_id]->pkt_counter_fid=
        		mapi_apply_function(flow[flow_id]->fd, "PKT_COUNTER"))<0) {
           		fprintf(stderr, "%s: PKT_COUNTER failed\n", __func__);
        		return -1;
  			}

			/* Simultaneous use of PKT_COUNTER and BYTE_COUNTER does not
				work with DAG4.3GE. Temporary hack: always use stflib version */

			if ((flow[flow_id]->byte_counter_fid=
        		mapi_apply_function(flow[flow_id]->fd, "stdflib:BYTE_COUNTER"))<0) {
           		fprintf(stderr, "%s: BYTE_COUNTER failed\n", 
				__func__);
        		return -1;
  			}

			/* Connect to flow */

			if (!configuration.global->no_measure) {
				if (mapi_connect(flow[flow_id]->fd)<0) {
					fprintf(stderr, "%s: mapi_connect() (%s) failed\n", 
						__func__, hostname_interface);
					return -1;
				}

				if ((scope_size=mapi_get_scope_size(flow[flow_id]->fd)) != 
					flow[flow_id]->measurement->scope->subject_no) {
					fprintf(stderr, "%s: mapi_get_scope_size() returned %d for %d subjects\n", __func__, scope_size, flow[flow_id]->measurement->scope->subject_no);
					return -1;
				}
			}

			i++;
			flow_id++;

		} /* while (measurement->protocols_array[i] && i<MAX_PROTOCOLS) */

		measurement=measurement->next;

	} /* while (measurement) */

	if (configuration.global->no_measure || !configuration.measurement)
		return 0;

	/* Periodically get results from MAPI flows */

	while (1) {
		if (gettimeofday(&tm, &tz)<0) {
			fprintf(stderr, "%s: gettimeofday() failed\n", __func__);
			return -1;
		}

		flow_id=0;
		while (flow[flow_id] && flow_id<MAX_FLOWS) {

			int scope_packets, scope_bytes;
	
			if (!configuration.global->no_stdout) {
				printf("%d %u.%u", flow[flow_id]->measurement->scope->id, 
					(unsigned int)(tm.tv_sec), 
					(unsigned int)(tm.tv_usec));
				if (!configuration.global->stdout_simple)
					printf(" %s\n", flow[flow_id]->protocol);
			}

			gettimeofday(&tv_start, NULL);
			if ((pkt_counter_res=mapi_read_results(flow[flow_id]->fd, 
				flow[flow_id]->pkt_counter_fid))==NULL) {
					fprintf(stderr, "%s: mapi_read_results() for flow %d failed\n",
						__func__, flow_id);
			 	return -1;
			}

			gettimeofday(&tv_stop, NULL);
			tv_diff_pkt=timestamp_diff(&tv_start, &tv_stop);

			gettimeofday(&tv_start, NULL);
			if ((byte_counter_res=mapi_read_results(flow[flow_id]->fd, 
				flow[flow_id]->byte_counter_fid))==NULL) {
        			fprintf(stderr, "%s: mapi_read_results() for flow %d failed\n",
             		__func__, flow_id);
          		return -1;
     		}
			gettimeofday(&tv_stop, NULL);
			tv_diff_byte=timestamp_diff(&tv_start, &tv_stop);

			if (tv_diff_pkt>=TV_DIFF_THRESHOLD ||
				 tv_diff_byte>=TV_DIFF_THRESHOLD)
				tv_diff_threshold=1;
			else
				tv_diff_threshold=0;

			if (tv_diff_pkt>=TV_DIFF_THRESHOLD)
				syslog(LOG_DEBUG, "mapi_read_result() for PKT_COUNTER takes %d us for measurement ID %d and protocol %s (threshold %d us reached)", tv_diff_pkt, flow[flow_id]->measurement->id, flow[flow_id]->protocol, TV_DIFF_THRESHOLD);
			if (tv_diff_byte>=TV_DIFF_THRESHOLD)
				syslog(LOG_DEBUG, "mapi_read_result() for BYTE_COUNTER takes %d us for measurement ID %d and protocol %s (threshold %d us reached)", tv_diff_byte, flow[flow_id]->measurement->id, flow[flow_id]->protocol, TV_DIFF_THRESHOLD);

			scope_size = flow[flow_id]->measurement->scope->subject_no;

			scope_packets=0;
			scope_bytes=0;

			for (subject_id=0; subject_id<scope_size; subject_id++) {
	
				pkt_counter=
					*((unsigned long long*)(pkt_counter_res[subject_id].res));
				byte_counter=
					*((unsigned long long*)(byte_counter_res[subject_id].res));

				packets=pkt_counter - flow[flow_id]->pkt_counter[subject_id];
				bytes=byte_counter - flow[flow_id]->byte_counter[subject_id];
				mbps=(double)bytes*8/1000000;

				flow[flow_id]->pkt_counter[subject_id]=pkt_counter;
				flow[flow_id]->byte_counter[subject_id]=byte_counter;

				/* Determine seconds from previous result */

				if (flow[flow_id]->pkt_ts[subject_id])
					pkt_sec=(double)(pkt_counter_res[subject_id].ts -
								flow[flow_id]->pkt_ts[subject_id])/1000000;
				else
					pkt_sec=
						flow[flow_id]->measurement->parameters->interval.tv_sec +
	               (double)(flow[flow_id]->measurement->parameters->interval.tv_usec)/1000000;
		
				if (flow[flow_id]->byte_ts[subject_id])
					byte_sec=(double)(byte_counter_res[subject_id].ts -
						flow[flow_id]->byte_ts[subject_id])/1000000;
				else
					byte_sec=
						flow[flow_id]->measurement->parameters->interval.tv_sec +
	               (double)(flow[flow_id]->measurement->parameters->interval.tv_usec)/1000000;

				scope_packets+=(packets/pkt_sec);
				scope_bytes+=(bytes/byte_sec);

				flow[flow_id]->pkt_ts[subject_id]=
					pkt_counter_res[subject_id].ts;
				flow[flow_id]->byte_ts[subject_id]=
					byte_counter_res[subject_id].ts;

				if (tv_diff_threshold) {
					syslog(LOG_DEBUG, "%s:%s: %.02f seconds from previous result",
						flow[flow_id]->measurement->scope->subject[subject_id]->hostname,
						flow[flow_id]->measurement->scope->subject[subject_id]->interface,
						byte_sec);
				}

				/* Print result */

				if (!configuration.global->no_stdout) {
					if (configuration.global->stdout_simple)
						printf(" %0.2f %0.2f %0.2f", packets/pkt_sec, 
							bytes/byte_sec, mbps/byte_sec);
					else
							printf(" %0.2f packets/s, %0.2f bytes/s, %0.2f Mb/s, time %uus/%uus, interval %0.2fs/%0.2fs\n", 
								packets/pkt_sec, bytes/byte_sec, mbps/byte_sec, 
								tv_diff_pkt, tv_diff_byte, pkt_sec, byte_sec);
				}

			} /* for (subject_id=0; subject_id++; subject_id<scope_size) */

			if (!configuration.global->no_stdout)
				printf("\n");

			/* If interval is at least 1 second, then store results 
			   to RRD file */
				
			if (flow[flow_id]->measurement->parameters->interval.tv_sec) {
				sprintf(command, "rrdtool update %s %u:%lu:%lu:%.6f", 
					 flow[flow_id]->rrd_filename, 
					 (unsigned int)(tm.tv_sec), 
					 (unsigned long)(scope_packets), 
					 (unsigned long)(scope_bytes), 
					 (double)scope_bytes*8/1000000);

				if (configuration.global->debug > 1)
					syslog(LOG_DEBUG, "system(%s)", command);

				if (tm.tv_sec == flow[flow_id]->rrd_ts)
					syslog(LOG_ERR, "duplicate RRD timestamp %u for scope %d\n", (unsigned int)(tm.tv_sec), flow[flow_id]->measurement->scope->id);
				else
					flow[flow_id]->rrd_ts=tm.tv_sec;

				if (debug)
					printf("%s: system(%s)\n", __func__, command);

				if (system(command)<0) {
					fprintf(stderr, "%s: command(%s) failed\n", __func__,
						command);
					return -1;
				}
			}

			flow_id++;

		} /* while (flow[flow_id] && flow_id<MAX_FLOWS) */

		abw_next_timestamp(&(configuration.measurement->parameters->interval), 
			&next, &wait);

		if (!configuration.global->no_stdout && 
			 !configuration.global->stdout_simple) {
     		printf("next.tv_sec: %d, next.tv_usec: %d, wait.tv_sec: %d, wait.tv_usec: %d\n", (int)(next.tv_sec), (int)(next.tv_usec), (int)(wait.tv_sec), (int)(wait.tv_usec));
			printf("===============================================================================\n");
		}

     	usleep(wait.tv_sec * 1000000 + wait.tv_usec);
		 
	} /* while (1) */

	return 0;
} /* main() */
Exemplo n.º 15
0
int
main(int argc, char **argv)
{
	Mapi dbh;
	MapiHdl hdl = NULL;
	mapi_int64 rows, i;
	char *parm[] = { "peter", 0 };
	char *parm2[] = { "25", 0 };
	int j;

	if (argc != 4) {
		printf("usage:%s <host> <port> <language>\n", argv[0]);
		exit(-1);
	}

	dbh = mapi_connect(argv[1], atoi(argv[2]), "monetdb", "monetdb", argv[3], NULL);
	if (dbh == NULL || mapi_error(dbh))
		die(dbh, hdl);

	/* mapi_trace(dbh, 1); */
	if (strcmp(argv[3], "sql") == 0) {
		/* switch of autocommit */
		if (mapi_setAutocommit(dbh, 0) != MOK || mapi_error(dbh))
			die(dbh,NULL);
		if ((hdl = mapi_query(dbh, "create table emp(name varchar(20), age int)")) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if (mapi_close_handle(hdl) != MOK)
			die(dbh, hdl);
		if ((hdl = mapi_query(dbh, "insert into emp values('John', 23)")) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if (mapi_close_handle(hdl) != MOK)
			die(dbh, hdl);
		if ((hdl = mapi_query(dbh, "insert into emp values('Mary', 22)")) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if (mapi_close_handle(hdl) != MOK)
			die(dbh, hdl);
		if ((hdl = mapi_query(dbh, "select * from emp")) == NULL || mapi_error(dbh))
			die(dbh, hdl);
	} else if (strcmp(argv[3], "mal") == 0) {
		if ((hdl = mapi_query(dbh, "emp := bat.new(:oid,:str);")) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if ((hdl = mapi_query(dbh, "age := bat.new(:oid,:int);")) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if (mapi_close_handle(hdl) != MOK)
			die(dbh, hdl);
		if ((hdl = mapi_query_array(dbh, "bat.append(emp,\"?\");", parm)) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if ((hdl = mapi_query_array(dbh, "bat.append(age,?);", parm2)) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if (mapi_close_handle(hdl) != MOK)
			die(dbh, hdl);
		if ((hdl = mapi_query(dbh, "io.print(emp,age);")) == NULL || mapi_error(dbh))
			die(dbh, hdl);
	} else {
		fprintf(stderr, "%s: unknown language, only mal and sql supported\n", argv[0]);
		exit(1);
	}

	/* Retrieve all tuples in the client cache first */
	rows = mapi_fetch_all_rows(hdl);
	if (mapi_error(dbh))
		die(dbh, hdl);
	printf("rows received " LLFMT " with %d fields\n", rows, mapi_get_field_count(hdl));

	/* Interpret the cache as a two-dimensional array */
	for (i = 0; i < rows; i++) {
		if (mapi_seek_row(hdl, i, MAPI_SEEK_SET) || mapi_fetch_row(hdl) == 0)
			break;
		for (j = 0; j < mapi_get_field_count(hdl); j++) {
			printf("%s=%s ", mapi_get_name(hdl, j), mapi_fetch_field(hdl, j));
		}
		printf("\n");
	}
	if (mapi_error(dbh))
		die(dbh, hdl);
	if (mapi_close_handle(hdl) != MOK)
		die(dbh, hdl);
	mapi_destroy(dbh);

	return 0;
}
Exemplo n.º 16
0
int
main(int argc, char **argv)
{
	/* a parameter binding test */
	char *nme = 0;
	int age = 0;
	char *parm[] = { "peter", 0 };
	char *parm2[] = { "25", 0 };
	char *parm3[] = { "peter", "25", 0 };
	Mapi dbh= NULL;
	MapiHdl hdl = NULL;

	if (argc != 4) {
		printf("usage:%s <host> <port> <language>\n", argv[0]);
		exit(-1);
	}

	dbh = mapi_connect(argv[1], atoi(argv[2]), "monetdb", "monetdb", argv[3], NULL);
	if (dbh == NULL || mapi_error(dbh))
		die(dbh, hdl);

	/* mapi_trace(dbh,1); */
	if (strcmp(argv[3], "sql") == 0) {
		/* switch of autocommit */
		if (mapi_setAutocommit(dbh, 0) != MOK || mapi_error(dbh))
			die(dbh,NULL);
		if ((hdl = mapi_query(dbh, "create table emp(name varchar(20), age int)")) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if (mapi_close_handle(hdl) != MOK)
			die(dbh, hdl);
		if ((hdl = mapi_query_array(dbh, "insert into emp values('?', ?)", parm3)) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if (mapi_close_handle(hdl) != MOK)
			die(dbh, hdl);
		if ((hdl = mapi_query(dbh, "select * from emp")) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if (mapi_bind(hdl, 0, &nme))
			die(dbh, hdl);
		if (mapi_bind_var(hdl, 1, MAPI_INT, &age))
			die(dbh, hdl);
		while (mapi_fetch_row(hdl)) {
			printf("%s is %d\n", nme, age);
		}
	} else if (strcmp(argv[3], "mal") == 0) {
		if ((hdl = mapi_query(dbh, "emp := bat.new(:oid,:str);")) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if ((hdl = mapi_query(dbh, "age := bat.new(:oid,:int);")) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if (mapi_close_handle(hdl) != MOK)
			die(dbh, hdl);
		if ((hdl = mapi_query_array(dbh, "bat.append(emp,\"?\");", parm)) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if ((hdl = mapi_query_array(dbh, "bat.append(age,?);", parm2)) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if (mapi_close_handle(hdl) != MOK)
			die(dbh, hdl);
		if ((hdl = mapi_query(dbh, "io.print(emp,age);")) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if (mapi_bind(hdl, 1, &nme))
			die(dbh, hdl);
		if (mapi_bind_var(hdl, 2, MAPI_INT, &age))
			die(dbh, hdl);
		while (mapi_fetch_row(hdl)) {
			printf("%s is %d\n", nme, age);
		}
	} else {
		fprintf(stderr, "%s: unknown language, only mal and sql supported\n", argv[0]);
		exit(1);
	}

	if (mapi_error(dbh))
		die(dbh, hdl);
	if (mapi_close_handle(hdl) != MOK)
		die(dbh, hdl);
	mapi_destroy(dbh);

	return 0;
}
Exemplo n.º 17
0
int main(int argc, char *argv[]) {

    int fd;
    int fid, fidc, fids;
    mapi_results_t *cntc, *cnts;
    int err_no =0, flag=0;
    char error[512];
    mapi_flow_info_t info;

    if(argc != 2) {
        printf("Usage: %s <interface> \n", argv[0]);
        return -1;
    }

    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( (fidc = mapi_apply_function(fd, "PKT_COUNTER")) < 0) {
        fprintf(stderr, "Could not apply PKT_COUNTER 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( (fid = mapi_apply_function(fd, "REGEXP", "HTTP.*OK")) < 0) {
        fprintf(stderr, "Could not apply REGEXP 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( (fids = mapi_apply_function(fd, "PKT_COUNTER")) < 0) {
        fprintf(stderr, "Could not apply PKT_COUNTER 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, "Connecting to flow failed %d\n", fd);
        mapi_read_error(&err_no, error);
        fprintf(stderr, "Errorcode :%d description: %s \n", err_no, error);
        return -1;
    }
    DOT;

    /*
     * Sanity checks
     */
    sleep(5);
    cntc = mapi_read_results(fd, fidc);
    DOT;
    cnts = mapi_read_results(fd, fids);
    DOT;
    printf("\n\nPackets read: %d\t Packets matched : %d", *((int*)cntc->res), *((int*)cnts->res));

    if (*((int*)cntc->res) <= *((int*)cnts->res))
        fprintf(stderr,"\nWARNING: REGXEP failed\n");
    else
        printf("\nREGEXP OK\n\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);
        return -1;
    }
    DOT;

    /*
     *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, "REGEXP", "*)!@#@!##############@$#@@@@@@@@@@@@@@@@^^^^^^^^^^^^^^^^^^^#$$^^^^^^^^^^^^ \
							$#!!!!!!!!!!!!!!!!!!!!!!#$^^^^^^^^^^^^^^%%%%%%%%&&&&&&&&&&&&&&%%%%%%%%%%%%%% \
							*&&&&&&&&&&&&&&&&&&&&&&&&&&&@@@@@@@@@@@@@@@@@@%%%%%%%%%%%%%%%%^^^^^^^^^^^^^^^ \
							@@@@@@@@@@@@@")) < 0) {		/* invalid pattern - nothing to repeat */
        mapi_read_error(&err_no, error);
        printf("Testing error case1: Errorcode :%d description: %s \n", err_no, error);
        if(err_no != 7000) {
            printf("\t\tWrong ERRORCODE returned\n");
            flag = 1;
        }
    }
Exemplo n.º 18
0
int
main(int argc, char **argv)
{
	Mapi dbh;
	MapiHdl hdl = NULL;
	mapi_int64 rows;

	if (argc != 4) {
		printf("usage:%s <host> <port> <language>\n", argv[0]);
		exit(-1);
	}

	dbh = mapi_connect(argv[1], atoi(argv[2]), "monetdb", "monetdb", argv[3], NULL);
	if (dbh == NULL || mapi_error(dbh))
		die(dbh, hdl);

	mapi_cache_limit(dbh, 2);
	/* mapi_trace_log(dbh, "/tmp/mapilog"); */
	/* mapi_trace(dbh, 1); */
	if (strcmp(argv[3], "sql") == 0) {
		/* switch of autocommit */
		if (mapi_setAutocommit(dbh, 0) != MOK || mapi_error(dbh))
			die(dbh,NULL);
		if ((hdl = mapi_query(dbh, "create table emp(name varchar(20), age int)")) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if (mapi_close_handle(hdl) != MOK)
			die(dbh, hdl);
		if ((hdl = mapi_query(dbh, "insert into emp values('John', 23)")) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if (mapi_close_handle(hdl) != MOK)
			die(dbh, hdl);
		if ((hdl = mapi_query(dbh, "insert into emp values('Mary', 22)")) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if (mapi_close_handle(hdl) != MOK)
			die(dbh, hdl);
		if ((hdl = mapi_query(dbh, "select * from emp")) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		rows = mapi_fetch_all_rows(hdl);
		if (mapi_error(dbh))
			die(dbh, hdl);
		printf("rows received " LLFMT "\n", rows);
		while (mapi_fetch_row(hdl)) {
			char *nme = mapi_fetch_field(hdl, 0);
			char *age = mapi_fetch_field(hdl, 1);

			printf("%s is %s\n", nme, age);
		}
	} else if (strcmp(argv[3], "mal") == 0) {
		if ((hdl = mapi_query(dbh, "emp := bat.new(:oid,:str);")) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if ((hdl = mapi_query(dbh, "age := bat.new(:oid,:int);")) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if (mapi_close_handle(hdl) != MOK)
			die(dbh, hdl);
		if ((hdl = mapi_query(dbh, "bat.append(emp, \"John\");")) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if ((hdl = mapi_query(dbh, "bat.append(age, 23);")) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if (mapi_close_handle(hdl) != MOK)
			die(dbh, hdl);
		if ((hdl = mapi_query(dbh, "bat.append(emp, \"Mary\");")) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if ((hdl = mapi_query(dbh, "bat.append(age, 22);")) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		if (mapi_close_handle(hdl) != MOK)
			die(dbh, hdl);
		if ((hdl = mapi_query(dbh, "io.print(emp,age);")) == NULL || mapi_error(dbh))
			die(dbh, hdl);
		rows = mapi_fetch_all_rows(hdl);
		if (mapi_error(dbh))
			die(dbh, hdl);
		printf("rows received " LLFMT "\n", rows);
		while (mapi_fetch_row(hdl)) {
			char *nme = mapi_fetch_field(hdl, 1);
			char *age = mapi_fetch_field(hdl, 2);

			printf("%s is %s\n", nme, age);
		}
	} else {
		fprintf(stderr, "%s: unknown language, only mal and sql supported\n", argv[0]);
		exit(1);
	}

	if (mapi_error(dbh))
		die(dbh, hdl);
	/* mapi_stat(dbh);
	   printf("mapi_ping %d\n",mapi_ping(dbh)); */
	if (mapi_close_handle(hdl) != MOK)
		die(dbh, hdl);
	mapi_destroy(dbh);

	return 0;
}
int main(int argc, char *argv[])
{
	int fd;
	int fid;
	int err_no =0 , flag=0;
	char error[512];
	int ok;
	mapi_flow_info_t info;
	
	if(argc!=2){
		printf("\nWrong arguments\n");
		
		return -1;
	}
	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, "PKT_COUNTER"))<0){
	  	fprintf(stderr, "Could not apply PKT_COUNTER 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, "Connecting to flow failed %d\n", fd);
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;
	if((ok = mapi_get_flow_info(fd, &info))<0){
	 	fprintf(stderr, "Getting flow info failed on fd:%d\n", fd);
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
	/*
	 *Sanity check
	 */
	if(info.num_functions!=1)
		  fprintf(stderr,"WARNING: Apply function sanity check failed\n");
	else 
		  printf("Mapi_apply_function OK\n");
	
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;
	/*
	 * Error checking
	 */

	fd = fid = 0;
	
	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, "IMAGINARY FUNCTION")) == -1){
		mapi_read_error( &err_no, error);
		printf("\nTesting error case1: Errorcode :%d description: %s \n" ,err_no, error);
		if(err_no != 6144){
			fprintf(stderr,"WARNING:Wrong ERRORCODE returned\n");
			flag = 1;	
		}
	}
DOT;	
	if( (fid = mapi_apply_function(-12314, "TO_BUFFER")) == -1){
		mapi_read_error( &err_no, error);
		printf("\nTesting error case2: Errorcode :%d description: %s \n" ,err_no, error);
		if(err_no != 6147){
			fprintf(stderr,"WARNING:Wrong ERRORCODE returned\n");
			flag = 1;	
		}
	}
DOT;	
	if(mapi_connect(fd)<0){
	 	fprintf(stderr, "Connecting to flow failed %d\n", fd);
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -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;

	/*
	 *Offline checking
	 */
	if ((fd = mapi_create_offline_flow("./tracefile" , MFF_PCAP))<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, "PKT_COUNTER"))<0){
	  	fprintf(stderr, "Could not apply PKT_COUNTER 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, "Connecting to flow failed %d\n", fd);
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
DOT;
	if((ok = mapi_get_flow_info(fd, &info))<0){
	 	fprintf(stderr, "Getting flow info failed on fd:%d\n", fd);
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -1;
	}
	/*
	 *Sanity check
	 */
	if(info.num_functions!=1)
		  fprintf(stderr,"WARNING: Apply function sanity check failed\n");
	else 
		  printf("Mapi_apply_function OK\n");
	
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;
	/*
	 * Error checking
	 */

	fd = fid = 0;
	
	if ((fd = mapi_create_offline_flow("./tracefile" , MFF_PCAP))<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, "IMAGINARY FUNCTION")) == -1){
		mapi_read_error( &err_no, error);
		printf("\nTesting error case3: Errorcode :%d description: %s \n" ,err_no, error);
		if(err_no != 6144){
			fprintf(stderr,"WARNING:Wrong ERRORCODE returned\n");
			flag = 1;	
		}
	}
DOT;	
	if( (fid = mapi_apply_function(-12314, "TO_BUFFER")) == -1){
		mapi_read_error( &err_no, error);
		printf("\nTesting error case4: Errorcode :%d description: %s \n" ,err_no, error);
		if(err_no != 6147){
			fprintf(stderr,"WARNING:Wrong ERRORCODE returned\n");
			flag = 1;	
		}
	}
DOT;	
	if(mapi_connect(fd)<0){
	 	fprintf(stderr, "Connecting to flow failed %d\n", fd);
		mapi_read_error( &err_no, error);
		fprintf(stderr,"Errorcode :%d description: %s \n" ,err_no, error);
		return -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("\nMAPI apply function Error Checking OK\n");
	else 
		printf("\nMAPI apply function Error Checking :FAILED:\n");


	return 0;
}