LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-02-2005, 02:05 AM   #1
suchitra
LQ Newbie
 
Registered: May 2005
Location: india,maharashtra
Posts: 1

Rep: Reputation: 0
undefined reference to functions from pcap.h


I am receiving following error after compiling my pgm given next to it.

undefined reference to pcap_lookupdev(errbuf);
undefined reference to pcap_open_live(dev,BUFSIZ,0,-1,errbuf);

Segment of my program containing these functions:


int main()
{
int saddr_size,bytes_recieved,bytes_recieved1,i,count=0,count1=0,m;
struct sockaddr_in saddr;
struct in_addr in;
char errbuf[PCAP_ERRBUF_SIZE];
pcap_t* descr;
struct pcap_pkthdr hdr;
FILE *tcpipf,*tcpf,*udpf,*udpipf;
char buffer[65535],buffer1[65535],*dev,s[30];
dev = pcap_lookupdev(errbuf);

if(dev == NULL)
{
printf("%s\n",errbuf);
exit(1);
}

descr = pcap_open_live(dev,BUFSIZ,0,-1,errbuf);

if(descr == NULL)
{
printf("pcap_open_live(): %s\n",errbuf);
exit(1);
}
 
Old 05-02-2005, 03:44 AM   #2
alred
Member
 
Registered: Mar 2005
Location: singapore
Distribution: puppy and Ubuntu and ... erh ... redhat(sort of) :( ... + the venerable bsd and solaris ^_^
Posts: 658
Blog Entries: 8

Rep: Reputation: 31
i usually compile pcap app with :
gcc 001.c -o 001 -lpcap
 
Old 09-18-2007, 10:44 AM   #3
gauthamk
Member
 
Registered: Jun 2007
Location: Chennai-India
Posts: 39

Rep: Reputation: 15
Thumbs up undefined reference worked for me gcc 001.c -o 001 -lpcap

Hi its worked for me.Thank u
 
Old 12-23-2007, 06:22 AM   #4
ishamael
LQ Newbie
 
Registered: Dec 2007
Posts: 3

Rep: Reputation: 0
Sorry to open an old thread but, umm....its not working for me. I typed

Code:
gcc test1.c -lpcap -o test1
It says cannot find lpcap. Any idea what the problem could be? Isnt this included in the libpcap library?

heres the code( a pretty basic one, coz I'm just starting out in pcap, and dont know what the hell I'm doing):
Code:
#include<stdio.h>
#include<pcap.h>
#include<stdlib.h>

int main()
{	
	char *dev, *error_openoffline, *fname, *gen_error;
	pcap_t *desc;//declaring the decsriptor 
	pcap_dumper_t	*pd;
	struct pcap_pkthdr *header;//declaring packet header
	u_char *sp;//packet data written to savefile	

	dev="eth1";//setting the device as eth1
	fname="/home/lordofdreams/testcap/cap1";//location of saved file
	
	desc=pcap_open_offline(	fname, error_openoffline );
	if( *(desc) == 'Null' )
	{
		printf("The session could not open as %s", error_openoffline );		
		exit(1);
	}

	pd=pcap_dump_open( desc, fname );
	if( *(pd) == 'Null' )
	{	gen_error=pcap_geterr( desc );
		printf( "\nThe dump could not be opened as %s", gen_error );
		exit(1);	
	}
	
	pcap_dump( (u_char *) pd, header, sp);

	printf("\nThe data is %h", sp );

	pcap_dump_close( pd );
	pcap_close( desc );

	return 0;

}
The interesting thing is when I put the if conditions after pcap_open_offline and pcap_dump_open in comments, then only I get the error, else there's an error concerning the if conditions.
 
Old 12-26-2007, 11:24 AM   #5
ishamael
LQ Newbie
 
Registered: Dec 2007
Posts: 3

Rep: Reputation: 0
Could somebody please help? I've been scouring the net, but I'm unable to find anything. Do I need to install lpcap separately? I got a link to a page, which should have the source for lpcap

http://www-nrg.ee.lbl.gov/

but I cant find it on this page . Additionally, the libpcap link does not seem to be working.
 
Old 12-27-2007, 01:37 AM   #6
gauthamk
Member
 
Registered: Jun 2007
Location: Chennai-India
Posts: 39

Rep: Reputation: 15
Smile install pcap library from the package

Here is the sample program contains pcap library and functions .

for compilation gcc j.c -lpcap -o l and run the ./l.

let me know which linux flavor u r using?.
I also tested u r program in my machine .It also has some syntax error fix it.

#include <stdio.h>
#include <stdlib.h>
#include <pcap.h> /* if this gives you an error try pcap/pcap.h */
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netinet/if_ether.h> /* includes net/ethernet.h */

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 */
struct iphdr *ipheader;
u_char *ptr; /* printing out hardware header info */

/* grab a device to peak into... */
while(1)
{
dev = pcap_lookupdev(errbuf);

if(dev == NULL)
{
printf("%s\n",errbuf);
exit(1);
}

printf("DEV: %s\n",dev);

/* open the device for sniffing.

pcap_t *pcap_open_live(char *device,int snaplen, int prmisc,int to_ms,
char *ebuf)

snaplen - maximum size of packets to capture in bytes
promisc - set card in promiscuous mode?
to_ms - time to wait for packets in miliseconds before read
times out
errbuf - if something happens, place error string here

Note if you change "prmisc" param to anything other than zero, you will
get all packets your device sees, whether they are intendeed for you or
not!! Be sure you know the rules of the network you are running on
before you set your card in promiscuous mode!! */

descr = pcap_open_live(dev,BUFSIZ,0,-1,errbuf);

if(descr == NULL)
{
printf("pcap_open_live(): %s\n",errbuf);
exit(1);
}


/*
grab a packet from descr (yay!)
u_char *pcap_next(pcap_t *p,struct pcap_pkthdr *h)
so just pass in the descriptor we got from
our call to pcap_open_live and an allocated
struct pcap_pkthdr */

packet = pcap_next(descr,&hdr);

if(packet == NULL)
{/* dinna work *sob* */
printf("Didn't grab packet\n");
exit(1);
}

/* struct pcap_pkthdr {
struct timeval ts; time stamp
bpf_u_int32 caplen; length of portion present
bpf_u_int32; lebgth this packet (off wire)
}
*/

printf("Grabbed packet of length %d\n",hdr.len);
printf("Recieved at ..... %s\n",ctime((const time_t*)&hdr.ts.tv_sec));
printf("Ethernet address length is %d\n",ETHER_HDR_LEN);

/* lets start with the ether header... */
eptr = (struct ether_header *) packet;

/* Do a couple of checks to see what packet type we have..*/
if (ntohs (eptr->ether_type) == ETHERTYPE_IP)
{
printf("Ethernet type hex:%x dec:%d is an IP packet\n",
ntohs(eptr->ether_type),
ntohs(eptr->ether_type));
}else if (ntohs (eptr->ether_type) == ETHERTYPE_ARP)
{
printf("Ethernet type hex:%x dec:%d is an ARP packet\n",
ntohs(eptr->ether_type),
ntohs(eptr->ether_type));
}else {
printf("Ethernet type %x not IP", ntohs(eptr->ether_type));
exit(1);
}

/* THANK YOU RICHARD STEVENS!!! RIP*/
ptr = eptr->ether_dhost;
i = ETHER_ADDR_LEN;
printf(" Destination Address: ");
do{
printf("%s%x",(i == ETHER_ADDR_LEN) ? " " : ":",*ptr++);
}while(--i>0);
printf("\n");

ptr = eptr->ether_shost;
i = ETHER_ADDR_LEN;
printf(" Source Address: ");
do{
printf("%s%x",(i == ETHER_ADDR_LEN) ? " " : ":",*ptr++);
}while(--i>0);
printf("\n");

}
return 0;
}
 
Old 12-28-2007, 12:53 AM   #7
ishamael
LQ Newbie
 
Registered: Dec 2007
Posts: 3

Rep: Reputation: 0
gauthamk, thanks a lot for the reply . I'm using Debian 4.0. I'm pretty sure I've installed pcap properly, because I'm able to compile other programs by including pcap.h, even if I dont use its functions.However, could you tell me what lpcap is exactly? I've tried all sorts of permutations involving this term in google, but no satisfactory results so far.

I'll test the program you've given me on my machine, and let you know the results as soon as possible. Thanks a lot for the help again

Last edited by ishamael; 12-28-2007 at 12:55 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Undefined reference, why? george_mercury Programming 4 05-07-2009 12:15 AM
g++: inline functions can cause an undefined reference when linking? R00ts Programming 5 06-21-2005 03:13 AM
Undefined Reference ChemicalBurn Programming 2 02-14-2005 03:01 AM
undefined reference mp4-10 Programming 3 01-25-2005 12:38 PM
undefined reference to 'errno' kbridger Programming 2 10-07-2003 05:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration