tcpdump mailing list archives

a problem in code... please help


From: Drona Nagarajan <drona89 () live com>
Date: Sun, 6 Sep 2009 18:16:29 +0530


i made this sniffer using the latest libpcap library but it is giving an error (not an error actually but its not 
running)....
please help asap as i'll show my sniffer as my this semester's project....
the code....

/*
   sniff.c
   a simple packet sniffer that counts the number of packets passed through the command line
   usage: a.out <number of packets>
   this is just a test sniffer.... the real one is under "construction"
*/

#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h>

/* callback function that is passed to pcap_loop(..) and called each time 
 * a packet is recieved                                                    */
void my_callback(u_char *useless,const struct pcap_pkthdr* pkthdr,const u_char*
        packet)
{
    static int count = 1;
    fprintf(stdout,"%d, ",count);
    if(count == 4)
        fprintf(stdout,"counting......");
    if(count == 7)
        fprintf(stdout,"counted ");
    fflush(stdout);
    count++;
}

int main(int argc,char **argv)
{ 
    int i;
    char *dev; 
    char errbuf[PCAP_ERRBUF_SIZE];
    pcap_t* descr;
    const u_char *packet;
    struct pcap_pkthdr hdr;     /* pcap.h */
    struct ether_header *eptr;  /* net/ethernet.h */

    if(argc != 2){ fprintf(stdout,"Usage: %s numpackets\n",argv[0]);return 0;}

    /* grab a device to peak into... */
    dev = pcap_lookupdev(errbuf);
    if(dev == NULL)
    { printf("%s\n",errbuf); exit(1); }
    /* open device for reading */
    descr = pcap_open_live(dev,BUFSIZ,0,-1,errbuf);
    if(descr == NULL)
    { printf("pcap_open_live(): %s\n",errbuf); exit(1); }

    /* allright here we call pcap_loop(..) and pass in our callback function */
    /* int pcap_loop(pcap_t *p, int cnt, pcap_handler callback, u_char *user)*/
    /* If you are wondering what the user argument is all about, so am I!!   */
    pcap_loop(descr,atoi(argv[1]),my_callback,NULL);

    fprintf(stdout,"\nDone processing packets... wheew!\n");
    return 0;
}

the file is saved as sniff.c
now on the terminal when i gcc the code i get

the problem....

/tmp/ccGTnIuI.o: In function `main':
sniff.c:(.text+0x107): undefined reference to `pcap_lookupdev'
sniff.c:(.text+0x160): undefined reference to `pcap_open_live'
sniff.c:(.text+0x1c6): undefined reference to `pcap_loop'
collect2: ld returned 1 exit status

is it because my libpcap was not installed properly???? (cant be possible as i directly downloaded it from the packet 
manager)
i use linux ubuntu 9.04 release and the code was compiled using gcc (as you might have guessed)

hoping for a reply soon...
yours sincerely,

Drona.


_________________________________________________________________
We all see it as it is. But on MSN India, the difference lies in perspective.
http://in.msn.com-
This is the tcpdump-workers list.
Visit https://cod.sandelman.ca/ to unsubscribe.


Current thread: