#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

void main(void)
{
	int pega2(char *);


	FILE *fp,*ht;
	char *aa;
	float tot_conexao=0.0, cont_ftp=0.0, cont_telnet=0.0, cont_smtp=0.0, cont_http=0.0,
	    cont_pop2=0.0, cont_pop3=0.0, cont_dns=0.0, cont_roteamento=0.0, cont_login=0.0,
	    cont_impressora=0.0,cont_outros=0.0;
	char tot1[300]="snmpwalk -v 1 ";
	char dado[30];
	strcpy(dado,"penta.ufrgs.br");
	strcat(tot1,dado);
	strcat(tot1," public tcp > ");
	chdir("/home/uel/cce/dcop/mestrado/cmu/cmu-snmp2/bin");
	aa=tempnam("","aqui");
	strcat(tot1,aa);   
	system(tot1);
	fp=fopen(aa,"r");
	
	do{
		fgets(tot1,300,fp);             
		if (feof(fp))
		{  
		   rewind(fp);
		   fgets(tot1,300,fp);
		   while(!feof(fp))
		   {
		       puts(tot1);
		       fgets(tot1,300,fp);
		   }                 
		   printf("\n</pre>\n");
		   goto fim;
		}
		if((strstr(tot1,"tcpConnState"))!=NULL)
		{
		    if(((strstr(tot1,"closeWait"))!=NULL)
		    || ((strstr(tot1,"established"))!=NULL))

		    {
			tot_conexao++;
			 switch(pega2(tot1))
			 {
			 case 20    :     
			 case 21    : cont_ftp++;
				      break;
			 case 23    : cont_telnet++;
				      break;
			 case 25    : cont_smtp++;
				      break;
			 case 80    : cont_http++;
				      break;
			 case 79    : cont_pop2++;
				      break;
			 case 110   : cont_pop3++;
				      break;
			 case 42    : 
			 case 53    : cont_dns++;
				      break;
			 case 520   : cont_roteamento++;
				      break;
			 case 513   : cont_login++;
				      break;
			 case 515   : cont_impressora++;
				      break;
			 default    : cont_outros++;
			 }
		    }
		}
	}while((strstr(tot1,"tcpConnLocalAddress"))==NULL);

	if ((int)tot_conexao<1)             
	{
	    ht=fopen("/home/uel/cce/dcop/especializacao/tironi/public_html/marinw34.dat","r");
	    fgets(tot1,300,ht);
	    do
	    {
	    puts(tot1);
	    fgets(tot1,300,ht);
	    }while(!feof(ht));
	    
	    goto fim;
	}

	    ht=fopen("/home/uel/cce/dcop/especializacao/tironi/public_html/marinw31.dat","r");
	    fgets(tot1,300,ht);

	    do
	    {
	      puts(tot1);
	      if((strstr(tot1,"///"))==NULL)
	       fgets(tot1,300,ht);
	    
	    }while((strstr(tot1,"///"))==NULL);

	    
	    printf("%3.3f",tot_conexao);
	      
	    fgets(tot1,300,ht);
	    do
	    {
	      puts(tot1);
	      if((strstr(tot1,"///"))==NULL)
	      fgets(tot1,300,ht);
	    }while((strstr(tot1,"///"))==NULL);



	printf("\n<tr><th>FTP</th><th>%3.3f</th><th>%3.3f</th></tr>",cont_ftp,cont_ftp*100/tot_conexao);
	printf("\n<tr><th>TELNET</th><th>%3.3f</th><th>%3.3f</th></tr>",cont_telnet,cont_telnet*100/tot_conexao);
	printf("\n<tr><th>SMTP</th><th>%3.3f</th><th>%3.3f</th></tr>",cont_smtp,cont_smtp*100/tot_conexao);
	printf("\n<tr><th>HTTP</th><th>%3.3f</th><th>%3.3f</th></tr>",cont_http,cont_http*100/tot_conexao);
	printf("\n<tr><th>POP2</th><th>%3.3f</th><th>%3.3f</th></tr>",cont_pop2,cont_pop2*100/tot_conexao);
	printf("\n<tr><th>POP3</th><th>%3.3f</th><th>%3.3f</th></tr>",cont_pop3,cont_pop3*100/tot_conexao);
	printf("\n<tr><th>DNS</th><th>%3.3f</th><th>%3.3f</th></tr>",cont_dns,cont_dns*100/tot_conexao);
	printf("\n<tr><th>ROTEAMENTO</th><th>%3.3f</th><th>%3.3f</th></tr>",cont_roteamento,cont_roteamento*100/tot_conexao);
	printf("\n<tr><th>LOGIN</th><th>%3.3f</th><th>%3.3f</th></tr>",cont_login,cont_login*100/tot_conexao);
	printf("\n<tr><th>IMPRESSORA</th><th>%3.3f</th><th>%3.3f</th></tr>",cont_impressora,cont_impressora*100/tot_conexao);
	printf("\n<tr><th>OUTROS</th><th>%3.3f</th><th>%3.3f</th></tr>",cont_outros,cont_outros*100/tot_conexao);


	fgets(tot1,300,ht);
	do
	{
	  puts(tot1);
	  fgets(tot1,300,ht);
	}while(!feof(ht));



	fim:

	fclose(fp);
	fclose(ht);
	
	strcpy(tot1,"rm ");
	strcat(tot1,aa);
	system(tot1);     
}


int pega2(char *tot1)
{
	char ss[100];
	char *sss;
	char *ssss;
	int i;
	sss=tot1;
	for(i=0;i < 8;i++)
	{
	   while(*tot1++!='.');
	}
	strcpy(ss,tot1);
	ssss=ss;
	while(*++ssss!='.');
	*ssss='\0';
	
	tot1=sss;  
	return(atoi(ss));
}




