Find the answer to your Linux question:
Results 1 to 2 of 2
Hi , I have problems with pcap.h. I have installed the libpcap ... but the problems are still there. Can anyone help me thank you Here is my code : ...
  1. #1
    eba
    eba is offline
    Just Joined!
    Join Date
    Oct 2010
    Posts
    1

    Ubuntu 10.10 Pcap

    Hi ,
    I have problems with pcap.h. I have installed the libpcap ... but the problems are still there.
    Can anyone help me thank you
    Here is my code :
    Code:
    #define _BSD_SOURCE 1
    
    #include <pcap.h>
    
    #include <stdio.h>
    
    #include <ctype.h>
    
    #include <netinet/in.h>
    
    #include <arpa/inet.h>
    
    #include <net/if.h>
    
    #include <netinet/if_ether.h>
    
    #include "/home/moi/headers.h"
    
    #define SNAP_LEN 1518
    
    #define NB_PAQUETS 5
    
    void callback(u_char *args, const struct _pkthdr *header,const u_char *packet)
    
    {
    
    static int count = 1;
    
    const struct ethernet *ethernet; /* entete ethernet */
    
    const struct ip *ip; /* entete IP */
    
    const struct tcp *tcp; /* entete TCP */
    
    const char *payload; /* payload */
    
    int size_ethernet = 14;
    
    int size_ip;
    
    int size_tcp;
    
    int size_payload;
    
    printf("\nNombre de paquets captures [%d]\n", count);
    
    count++;
    
    ethernet = (struct ethernet*) (packet) ;
    
    //Parametres de l'entete IP
    
    ip = (struct ip*) (packet + size_ethernet);
    
    size_ip = ip->ip_hlen*4;
    
    printf ("IP source: [%s] --> IP Destination :[%s] ",inet_ntoa(ip->ip_source),inet_ntoa(ip->ip_dest));
    
    //Parametres de l'entete TCP
    
    tcp = (struct tcp*) (packet + size_ethernet + size_ip);
    
    size_tcp = tcp->tcp_hlen*4;
    
    printf("Port Source: [%d] --> Port Destination: [%d]\n", ntohs(tcp-> tcp_sourceport), ntohs(tcp->tcp_destport));
    
    printf ("Drapaux : [FIN=%d] [SYN=%d] [RST=%d] [PSH=%d] [ACK=%d] [URG=%d] \n" , tcp->
    
    tcp_fin,tcp->tcp_syn, tcp->tcp_rst, tcp->tcp_psh,tcp->tcp_ack,tcp->tcp_urg);
    
    //Affichage de la partie donnee d'un paquet
    
    payload = (u_char *) (packet + size_ethernet + size_ip + size_tcp);
    
    size_payload = ntohs(ip->ip_total_length) - (size_ip + size_tcp);
    
    printf ("Payload [%d bytes] [%s] ", size_payload, payload);
    
    }
    
    int main ()
    
    {
    
    char *dev = NULL; /* interface reseau */
    
    char errbuf[PCAP_ERRBUF_SIZE]; /* buffer d'erreurs */
    
    pcap_t *descr; /* descripteur de paquets */
    
    struct bpf_program fp;
    
    //char *packet=NULL;
    
    bpf_u_int32 maskp; /* masque reseau */
    
    bpf_u_int32 netp; /* adresse ip */
    
    char filter_app[]= "tcp"; /* filtre * /
    
    
    
    
    
    /* Etape 1 : detecter l'interface reseau */
    
    dev = "wlan0"; //pcap_lookupdev(errbuf);
    
    
    
    /* Etape 2 : recuperer les informations reseau */
    
    pcap_lookupnet(dev, &netp, &maskp, errbuf);
    
    
    
    /* Etape 3 : obtenir un descripteur de capture de paquets */
    
    descr = pcap_open_live(dev, SNAP_LEN, 1, 0, errbuf);
    
    
    
    /* Etape 4: compiler la configuration de capture choisie */
    
    pcap_compile(descr, &fp, filter_app, 0, maskp);
    
    
    
    /* Etape 5 : appliquer Ie filtre */
    
    pcap_setfilter(descr, &fp);
    
    
    
    /*Etape 6 : lancer la capture de paquets --> declenchement de la fonction a chaque
    
    paquet recu */
    
    pcap_loop(descr, NB_PAQUETS,callback, NULL);
    
    
    
    /* Etape 7 : fermeture du descripteur de paquets */
    
    pcap_close(descr) ;
    
    return 0;
    
    }

    and here are the problems when i compile
    Code:
    main.c:12: warning: 'struct _pkthdr' declared inside parameter list
    main.c: In function 'main':
    main.c:70: warning: passing argument 3 of 'pcap_loop' from incompatible pointer type
    /usr/include/pcap/pcap.h:297: note: expected 'pcap_handler' but argument is of type 'void (*)(u_char *, const struct _pkthdr *, const u_char *)'
    /tmp/ccZcy8p7.o: In function `main':
    main.c:(.text+0x225): undefined reference to `pcap_lookupnet'
    main.c:(.text+0x251): undefined reference to `pcap_open_live'
    main.c:(.text+0x281): undefined reference to `pcap_compile'
    main.c:(.text+0x295): undefined reference to `pcap_setfilter'
    main.c:(.text+0x2ba): undefined reference to `pcap_loop'
    main.c:(.text+0x2c6): undefined reference to `pcap_close'
    collect2: ld returned 1 exit status
    }
    Thank you very much for your help.
    I'm waiting for you

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,977
    Most of your errors are linkage errors. The rest are pretty much self-explanatory.

    In your compilation output:
    1. First line is a warning - you haven't defined or included the definition/declaration of "struck _pkthdr" before using it as an argument to a function definition.
    2. Line 3 is a warning also - your use of pcap_loop() is inconsistent with the definition of the function in /usr/include/pcap.h.

    Since these are only warnings, you might be able to build a functional program, but you haven't told your Makefile where to find the libraries and which libraries contain the implementation of pcap_lookupnet() et al.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...