[C/C++] Analizator pakietów

0

Witam,

Mam do napisania program który analizuje pakiety sieciowe i wyświetla ich diagram. ( Taki wireshark własnej produkcji)
Szczerze to za bardzo nie wie jak to napisać ... Wiem że muszę użyć bibliotek winpcap (musi być pod Windowsa ...) tak jak w wiresharku.
Moduł wyboru karty sieciowej już mam (manual winpcapa ^^)
Nie wiem tylko jak pobrać dane o pakietach i jak je przełożyć na "diagram", po rozmowie z wykładowczynią uznała że może być to w konsoli a diagram to lista "typów" a obok wartości procentowe udziału w sieci.
Czyli z tego co zrozumiałem na to wyjść mniej więcej tak:

Typ           
--------------
typ1     10%
typ2     32%
...        ...

i na bieżąco aktualizuje dane.

Bardzo będę wdzięczny za wszelką pomoc i jakiekolwiek rady i wskazówki.

0

Polecam lekturę dokumentacji winpcap. Znalezienie fragmentu dotyczącego pobierania pakietów nie jest problemem.

0

Może ktoś mi powiedzieć dlaczego mi biblioteka pcap nie działa?

Piszę w Dev-C++.
Wszedłem w Projekt->Opcje projektu. W zakładce Pliki/katalogi w polu "Katalog bibliotek" dodałem C:\WpdPack\Lib, a w polu "Katalog plików nagłówkowych" dodałem C:\WpdPack\Include

Zainstalowałem Winpcap domyślnie i ściągnąłem z ich strony dodatkowo WpdPack

i w teraz w momencie kompilacji wyrzuca mi błędy i nie wiem dlaczego :(

0

kod:

#include <stdlib.h>
#include <stdio.h>
#include <pcap.h>

void usage();

void dispatcher_handler(u_char *, const struct pcap_pkthdr *, const u_char *);


void main(int argc, char **argv)
{
pcap_t *fp;
char errbuf[PCAP_ERRBUF_SIZE];
struct timeval st_ts;
u_int netmask;
struct bpf_program fcode;
  
    /* Check the validity of the command line */
    if (argc != 2)
    {
        usage();
        return 1;
    }
        
    /* Open the output adapter */
    if ( (fp= pcap_open(argv[1], 100, PCAP_OPENFLAG_PROMISCUOUS, 1000, NULL, errbuf) ) == NULL)
    {
        fprintf(stderr,"\nUnable to open adapter %s.\n", errbuf);
        return 1;
    }

    /* Don't care about netmask, it won't be used for this filter */
    netmask=0xffffff; 

    //compile the filter
    if (pcap_compile(fp, &fcode, "tcp", 1, netmask) <0 )
    {
        fprintf(stderr,"\nUnable to compile the packet filter. Check the syntax.\n");
        /* Free the device list */
        return 1;
    }
    
    //set the filter
    if (pcap_setfilter(fp, &fcode)<0)
    {
        fprintf(stderr,"\nError setting the filter.\n");
        pcap_close(fp);
        /* Free the device list */
        return 1;
    }

    /* Put the interface in statstics mode */
    if (pcap_setmode(fp, MODE_STAT)<0)
    {
        fprintf(stderr,"\nError setting the mode.\n");
        pcap_close(fp);
        /* Free the device list */
        return 1;
    }


    printf("TCP traffic summary:\n");

    /* Start the main loop */
    pcap_loop(fp, 0, dispatcher_handler, (PUCHAR)&st_ts);

    pcap_close(fp);
    return 1;
}

void dispatcher_handler(u_char *state, const struct pcap_pkthdr *header, const u_char *pkt_data)
{
    struct timeval *old_ts = (struct timeval *)state;
    u_int delay;
    LARGE_INTEGER Bps,Pps;
    struct tm ltime;
    char timestr[16];
    time_t local_tv_sec;

    /* Calculate the delay in microseconds from the last sample. */
    /* This value is obtained from the timestamp that the associated with the sample. */
    delay=(header->ts.tv_sec - old_ts->tv_sec) * 1000000 - old_ts->tv_usec + header->ts.tv_usec;
    /* Get the number of Bits per second */
    Bps.QuadPart=(((*(LONGLONG*)(pkt_data + 8)) * 8 * 1000000) / (delay));
    /*                                            ^      ^
                                                  |      |
                                                  |      | 
                                                  |      |
                         converts bytes in bits --       |
                                                         |
                    delay is expressed in microseconds --
    */

    /* Get the number of Packets per second */
    Pps.QuadPart=(((*(LONGLONG*)(pkt_data)) * 1000000) / (delay));

    /* Convert the timestamp to readable format */
    local_tv_sec = header->ts.tv_sec;
    localtime_s(&ltime, &local_tv_sec);
    strftime( timestr, sizeof timestr, "%H:%M:%S", &ltime);

    /* Print timestamp*/
    printf("%s ", timestr);

    /* Print the samples */
    printf("BPS=%I64u ", Bps.QuadPart);
    printf("PPS=%I64u\n", Pps.QuadPart);

    //store current timestamp
    old_ts->tv_sec=header->ts.tv_sec;
    old_ts->tv_usec=header->ts.tv_usec;
}


void usage()
{
    
    printf("\nShows the TCP traffic load, in bits per second and packets per second.\nCopyright (C) 2002 Loris Degioanni.\n");
    printf("\nUsage:\n");
    printf("\t tcptop adapter\n");
    printf("\t You can use \"WinDump -D\" if you don't know the name of your adapters.\n");

    exit(0);
}

I wywala takie błędy:
26 C:\Dev-Cpp\main.cpp PCAP_OPENFLAG_PROMISCUOUS' undeclared (first use this function) 26 C:\Dev-Cpp\main.cpp pcap_open' undeclared (first use this function)
99 C:\Dev-Cpp\main.cpp `localtime_s' undeclared (first use this function)

0

Nie dolaczasz jakiejs biblioteki, w dokumentacji znajdziesz wszystko co i jak.

Minelo 4 dni od Twojego poprzedniego posta i jeszcze nie pomyslales, ze undeclared oznacza ze brakuje deklaracji. Przez te kilka dni spokojnie byc znalazl co tam trzeba dolaczyc.

0

akurat wypadek losowy że nie miałem dostępu do internetu, poza tym egzaminy się zaczęły i praca i tak jakoś wyszło... a wiecie może jakiej brakuje? a może źle pcap podłączyłem? bo tak ciut na czuja szedłem ale zawsze działało :(

1 użytkowników online, w tym zalogowanych: 0, gości: 1