[Source] TCP List

Started by Dark_Side, 12 de October , 2006, 06:37:22 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Dark_Side

Hi,
Um simples programa que fiz para não ficar de vagaba na internet...

Segue o source:
/* Exemplo:
     - Mostra as conexões TCP abertas com as seguintes informações:
              * IP Local
              * Porta Local
              * IP Remoto
              * Porta Remota
              * Estado de conexão

 Compile o programa com as opções no linker (sem aspas): "-l ws2_32 -l iphlpapi
*/

#include <stdio.h> // Standard IO -> usar os printf()'s e puts()'s da vida.
#include <string.h> // snprintf()
#include <winsock2.h> // Header -> WINSOCK 2
#include <iphlpapi.h> // Header que contém funções para o manipulamento de conexões

int main()
{
 PMIB_TCPTABLE TcpTable;  // Estrutura TCPTABLE -> obter lista de conexões
 DWORD size = 0; // Variável auxiliar -> tamanho do buffer
 
   char *addr = NULL; // Obter IP LOCAL e REMOTO
   int i; // Lolz para auxiliar loop
   
   // Variáveis que armazenam os IPs
   char  IP_LOCAL[0x11];
   char  IP_REMOTO[0x11];
   
   // Porta local, remota e estado de conexão
   unsigned short PORTA_LOCAL,PORTA_REMOTA,estado;
   
   // Verifica se o tamanho necessário para o buffer
 if (GetTcpTable(NULL, &size, 0) == ERROR_INSUFFICIENT_BUFFER)
    TcpTable = (MIB_TCPTABLE *) malloc (size); //  Aloca o espaço no buffer;


 if (GetTcpTable(TcpTable, &size, 0) != NO_ERROR){ // Tenta obter lista de conexões
    printf("Buffer insuficiente.");
    return 1;}

   
   if (TcpTable->dwNumEntries < 1) {  // Verifica se há conexões
    printf("Nenhuma conexão encontrada.");
    return 1;
}

// Só uma frescura
puts("IP Local\tPorta Local\tIP Remoto\tPorta Remota\tEstado");
puts("------------------------------------------------------------------------------\n");

    for (i=0; i<TcpTable->dwNumEntries; i++) { // Percorre o array de conexões
     
     memset(IP_LOCAL,0x0,0x11); // Limpa IP_LOCAL
     memset(IP_REMOTO,0x0,0x11); // Limpa IP_REMOTO
     
     addr = (char *)&TcpTable->table[i].dwLocalAddr; // Obtém IP Local
     snprintf(IP_LOCAL,0x11,"%s",inet_ntoa(*(struct in_addr *)addr)); // Converte para string

     addr = (char *)&TcpTable->table[i].dwRemoteAddr; // Obtém IP Remoto
     snprintf(IP_REMOTO,0x11,"%s",inet_ntoa(*(struct in_addr *)addr));  // Converte para string

     PORTA_LOCAL = htons((unsigned short)TcpTable->table[i].dwLocalPort); // Obtém Porta Local
     PORTA_REMOTA = htons((unsigned short)TcpTable->table[i].dwRemotePort); // Obtém Porta Remota
     
     estado = (unsigned short)TcpTable->table[i].dwState; // Estado de conexão
   
   
     // Verifica se os IPS não são 0.0.0.0 -> inválido
    if(strcmp(IP_LOCAL,"0.0.0.0")!=0 && strcmp(IP_REMOTO,"0.0.0.0") !=0){

    printf("%s\t",IP_LOCAL); // Mostra IP LOCAL
    printf("   %ld\t",PORTA_LOCAL); // Mostra PORTA LOCAL
    printf("\t%s\t",IP_REMOTO); // Mostra IP REMOTO
    printf("   %ld\t",PORTA_REMOTA); // Mostra PORTA REMOTA
   
    switch(estado) // Verifica estado de conexão
    {
                  case MIB_TCP_STATE_CLOSED: // Fechada
                       printf("\tFechado");
                       break;
                 
                  case MIB_TCP_STATE_LISTEN: // Aguardando
                       printf("\tAguardando");
                       break;
                 
                 
                  case MIB_TCP_STATE_SYN_SENT: // Flag SYN Enviada
                       printf("\tSYN enviado");
                       break;
                 
                  case MIB_TCP_STATE_SYN_RCVD: // Flag SYN Recebida
                       printf("\tSYN recebido.");
                       break;
                                         
                  case MIB_TCP_STATE_ESTAB: // Estabelecida
                       printf("\tEstabelecida");
                       break;

                  case MIB_TCP_STATE_FIN_WAIT1: // Aguardando Flag FIN1
                       printf("\tEsperando FIN1");
                       break;
                       
                  case MIB_TCP_STATE_FIN_WAIT2: // Aguardando Flag FIN2
                       printf("\tEsperando FIN2");
                       break;
   
                  case MIB_TCP_STATE_CLOSING: // Fechando
                       printf("\tFechando");
                       break;
                                       
                  case MIB_TCP_STATE_LAST_ACK: // Última Flag ACK
                       printf("\tUltimo Flag ACK");
                       break;

                  case MIB_TCP_STATE_TIME_WAIT: // Esperando
                       printf("\tEsperando");
                       break;
                 
                  }

printf("\n");
             }
     }

free(TcpTable); // Libera o buffer alocado
 return 0;
}

Bye.