高分相求---关于通过libpcap函数库访问链路层程序的运行问题

wangbangjie 2004-05-08 09:47:54
#include <pcap.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#define BUFSIZE 1000
#define PCAP_ERRBUF_SIZE 200
void Display(const u_char *packet,const size_t length);

/*list all packets be filtered*/
void my_callback(u_char *none,const struct pcap_pkthdr *pkthdr,const u_char *packet)
{
Display((u_char *)packet,(size_t)(pkthdr->caplen));
return;
}

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;
struct ether_header *eptr;
struct bpf_program fp;
bpf_u_int32 maskp;
bpf_u_int32 netp;
}

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

/*grab a device*/
dev=pcap_lookupdev(errbuf);
if(dev==NULL){
fprintf(stderr,"%s\n",errbuf);
exit(1);
}

/*ask pcap for the network address and mask of the device*/
pcap_lookupnet(dev,&netp,&maskp,errbuf);

/*open device for reading this time and set it in promiscuous mode*/
descr=pcap_open_live(dev,BUFSIZ,1,-1,errbuf);
if(descr==NULL){
printf("pcap_open_live():%s\n",errbuf);
exit(1);
}

/*try and compile the program ..non-optimized*/
if(pcap_compile(descr,&fp,argv[1],0,netp)==-1){
fprintf(stderr,"Error calling pcap_compile\n");
exit(1);
}

/*set the compiled program as the filter*/
if(pcap_setfilter(descr,&fp)==-1){
fprintf(stderr,"Error setting filter\n");
exit(1);
}

/*capture packets*/
pcap_loop(descr,-1,my_callback,NULL);

return 0;
}

void Display(const u_char *packet,const size_t length)
{
u_long offset;
int i,j,k;
printf("packet[%lu bytes]:\n",(long unsigned int)length);
if(length<=0){
ruturn;
}

i=0;
offset=0;
for(k=length/16;k>0,k--,offset+=16){
printf("%08X",(unsigned int)offset);
for(j=0;j<16;j++,i++){
if(j==8){
printf("-%02X",packet[i]);
}
else printf("%02X",packet[i]);}
printf(" ");
i-=16;
for(j=0;j<16;j++,i++){
if((packet[i]>='')&&(packet[i]<=255))
{
printf("%c",packet[i]);
}
else printf(".");
}
printf("\n");
}
k=length-i;
if(k<=0){
return;
}

printf("%08X",(unsigned int)offset);
for(j=0;j<k;j++,i++){
if(j==8){
printf("-%02X",packet[i]);
}
else printf("%02X",packe[i]);
}
i-=k;
for(j=16-k;j>0;j--){
printf(" ");
}
printf(" ");
for(j=0;j<k;j++,i++){
if((packet[i]>=' ')&&(packet[i]<=255)){
printf("%c",packet[i]);
}
else {
printf(".");
}
}
printf("\n");
return;
}
在linux7.3下运行出现syntax error near unexpected token 'my callback(u'
'void my_callback(u.char *none,const struct--)的错误,我是这方面的新手,望高高手不吝赐教!
...全文
99 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangbangjie 2004-05-13
  • 打赏
  • 举报
回复
我再请问一下,在VC下,我编译时找不到pcap.h,winpcap怎么用啊?
wangbangjie 2004-05-13
  • 打赏
  • 举报
回复
我已将所需的头文件加入了啊.编译也已通过了啊.
lm98725 2004-05-13
  • 打赏
  • 举报
回复
这应该是头文件没被引用吧
wangbangjie 2004-05-13
  • 打赏
  • 举报
回复
此程序在linux下,用kdevelop运行时,有1.o: In function `main':
/root/capture/capture/1.c:55: undefined reference to `pcap_lookupdev'
/root/capture/capture/1.c:62: undefined reference to `pcap_lookupnet'
/root/capture/capture/1.c:66: undefined reference to `pcap_open_live'
/root/capture/capture/1.c:73: undefined reference to `pcap_compile'
/root/capture/capture/1.c:79: undefined reference to `pcap_setfilter'
/root/capture/capture/1.c:85: undefined reference to `pcap_loop'
collect2: ld returned 1 exit status
的问题,怎么调试啊?
lm98725 2004-05-13
  • 打赏
  • 举报
回复
把pcap.h等头文件拷到VC的include子目录下
lm98725 2004-05-12
  • 打赏
  • 举报
回复
奇怪,我在VC下可以编译正确
wlpwind 2004-05-08
  • 打赏
  • 举报
回复
是编译错误还是运行错误?
wlpwind 2004-05-08
  • 打赏
  • 举报
回复
是编译错误吧?
wangbangjie 2004-05-08
  • 打赏
  • 举报
回复
在编译时就发生错误,根据提示应该是语法错误,但我看不出来。我急用,望各位高人指点一二!

4,356

社区成员

发帖
与我相关
我的任务
社区描述
通信技术相关讨论
社区管理员
  • 网络通信
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧