在pcap_创建和pcap_设置之后使用pcap库,pcap_循环中的处理程序无法工作 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 结构usf { 目的地[目的地];/*目的地地址*/ u_int8_t源[6];/*源以太地址*/ u_int16_t型; u_int8_t tst[6];/*源以太地址*/ }; u_int16_t处理以太网 (u_char*args,const struct pcap_pkthdr*pkthdr,const u_char*packet); 作废我的回调(u_char*args,const struct pcap_pkthdr*pkthdr, 常量u_char*数据包) { printf(“在my_回调函数中\n”); u_int16_t type=处理以太网(args、pkthdr、数据包); if(type==ETHERTYPE\u IP) {/*处理IP数据包*/ }else if(type==ETHERTYPE\u ARP) {/*处理arp数据包*/ } else if(type==ETHERTYPE\u REVARP) {/*处理反向arp数据包*/ } } u_int16_t处理以太网 (u_char*args,const struct pcap_pkthdr*pkthdr, 常量u_char*数据包) { 结构usf*eptr;/*net/ethernet.h*/ /*让我们从乙醚总管开始*/ eptr=(结构usf*)数据包; fprintf(标准输出,“以太网头源:%s\n” ,以太地址((常量结构以太地址*)&eptr->source)); fprintf(标准输出,“目标:%s\n” ,以太地址((常量结构以太地址*)&eptr->dest)); fprintf(标准输出,“标准输出:%s\n” ,以太地址((常数结构以太地址*)&eptr->tst); /*检查我们是否有ip数据包*/ 如果(ntohs(eptr->type)=ETHERTYPE\u IP) { fprintf(stdout,“(IP)”); }否则如果(ntohs(eptr->type)=ETHERTYPE\u ARP) { fprintf(标准输出,“(ARP)”; }else if(ntohs(eptr->type)=ETHERTYPE\u REVARP) { fprintf(stdout,“(RARP)”; }否则{ fprintf(stdout,“(?)”); 出口(1); } 返回eptr->type; } int main(int argc,字符**argv) { char*dev;//接口名称ex:wlan0 字符errbuf[PCAP_errbuf_SIZE]; pcap_t*descr; struct bpf_program fp;/*保存已编译程序*/ bpf___int32 maskp;/*子网掩码*/ bpf____int32 netp;/*ip*/ u_char*args=NULL; 如果(argc2) { if(pcap_compile(descr,&fp,argv[2],0,netp)=-1) {fprintf(stderr,“调用pcap_编译时出错”);退出(1);} 如果(pcap_setfilter(descr,&fp)=-1) {fprintf(stderr,“错误设置筛选器”\n”);退出(1);} } int i=pcap_循环(descr,atoi(argv[1]),my_回调,args); 如果(i==-1) { printf(“pcap_循环中的错误”);退出(1); } else如果(i==0) { printf(“0\n”);退出(1); } else if(i==-2) { printf(“-2\n”);退出(1); } fprintf(标准输出,“\n完成\n”); 返回0; }

在pcap_创建和pcap_设置之后使用pcap库,pcap_循环中的处理程序无法工作 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 结构usf { 目的地[目的地];/*目的地地址*/ u_int8_t源[6];/*源以太地址*/ u_int16_t型; u_int8_t tst[6];/*源以太地址*/ }; u_int16_t处理以太网 (u_char*args,const struct pcap_pkthdr*pkthdr,const u_char*packet); 作废我的回调(u_char*args,const struct pcap_pkthdr*pkthdr, 常量u_char*数据包) { printf(“在my_回调函数中\n”); u_int16_t type=处理以太网(args、pkthdr、数据包); if(type==ETHERTYPE\u IP) {/*处理IP数据包*/ }else if(type==ETHERTYPE\u ARP) {/*处理arp数据包*/ } else if(type==ETHERTYPE\u REVARP) {/*处理反向arp数据包*/ } } u_int16_t处理以太网 (u_char*args,const struct pcap_pkthdr*pkthdr, 常量u_char*数据包) { 结构usf*eptr;/*net/ethernet.h*/ /*让我们从乙醚总管开始*/ eptr=(结构usf*)数据包; fprintf(标准输出,“以太网头源:%s\n” ,以太地址((常量结构以太地址*)&eptr->source)); fprintf(标准输出,“目标:%s\n” ,以太地址((常量结构以太地址*)&eptr->dest)); fprintf(标准输出,“标准输出:%s\n” ,以太地址((常数结构以太地址*)&eptr->tst); /*检查我们是否有ip数据包*/ 如果(ntohs(eptr->type)=ETHERTYPE\u IP) { fprintf(stdout,“(IP)”); }否则如果(ntohs(eptr->type)=ETHERTYPE\u ARP) { fprintf(标准输出,“(ARP)”; }else if(ntohs(eptr->type)=ETHERTYPE\u REVARP) { fprintf(stdout,“(RARP)”; }否则{ fprintf(stdout,“(?)”); 出口(1); } 返回eptr->type; } int main(int argc,字符**argv) { char*dev;//接口名称ex:wlan0 字符errbuf[PCAP_errbuf_SIZE]; pcap_t*descr; struct bpf_program fp;/*保存已编译程序*/ bpf___int32 maskp;/*子网掩码*/ bpf____int32 netp;/*ip*/ u_char*args=NULL; 如果(argc2) { if(pcap_compile(descr,&fp,argv[2],0,netp)=-1) {fprintf(stderr,“调用pcap_编译时出错”);退出(1);} 如果(pcap_setfilter(descr,&fp)=-1) {fprintf(stderr,“错误设置筛选器”\n”);退出(1);} } int i=pcap_循环(descr,atoi(argv[1]),my_回调,args); 如果(i==-1) { printf(“pcap_循环中的错误”);退出(1); } else如果(i==0) { printf(“0\n”);退出(1); } else if(i==-2) { printf(“-2\n”);退出(1); } fprintf(标准输出,“\n完成\n”); 返回0; },c,pcap,C,Pcap,我所需要做的就是将我的卡接口转换为监控模式,以嗅探信标帧,以达到一些良好的目的,但是在pcap_创建和pcap_设置后使用pcap库时,pcap_循环中的处理程序无法工作,因此有人能帮我知道问题出在哪里吗???您可以使用来获取/设置有关无线接口的信息(这由iwconfig命令使用)。不能保证pcap\u activate()成功。您应该检查错误并在失败时报告: #include <pcap.h> #include <stdio.h> #include <stdlib

我所需要做的就是将我的卡接口转换为监控模式,以嗅探信标帧,以达到一些良好的目的,但是在pcap_创建和pcap_设置后使用pcap库时,pcap_循环中的处理程序无法工作,因此有人能帮我知道问题出在哪里吗???

您可以使用来获取/设置有关无线接口的信息(这由
iwconfig
命令使用)。

不能保证
pcap\u activate()
成功。您应该检查错误并在失败时报告:

#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> 
#include <net/ethernet.h>
#include <netinet/ether.h> 

struct usf
 {
  u_int8_t  dest[6];    /* destination eth addr */
  u_int8_t  source[6];  /* source ether addr    */
  u_int16_t type;
  u_int8_t  tst[6]; /* source ether addr    */
 };

 u_int16_t handle_ethernet
      (u_char *args,const struct pcap_pkthdr* pkthdr,const u_char* packet);

 void my_callback(u_char *args,const struct pcap_pkthdr* pkthdr,
                  const u_char* packet)
 {
   printf ("in my_callback function\n");
   u_int16_t type = handle_ethernet(args,pkthdr,packet);
   if(type == ETHERTYPE_IP)
   {/* handle IP packet */
   }else if(type == ETHERTYPE_ARP)
   {/* handle arp packet */
   }
   else if(type == ETHERTYPE_REVARP)
   {/* handle reverse arp packet */
   }
 }

 u_int16_t handle_ethernet
     (u_char *args,const struct pcap_pkthdr* pkthdr,
      const u_char* packet)
 {
   struct usf *eptr;  /* net/ethernet.h */
   /* lets start with the ether header... */
   eptr = (struct usf *) packet;
   fprintf(stdout,"ethernet header source: %s\n"
          ,ether_ntoa((const struct ether_addr *)&eptr->source));
   fprintf(stdout," destination: %s \n"
         ,ether_ntoa((const struct ether_addr *)&eptr->dest));
   fprintf(stdout," tst : %s\n "
         ,ether_ntoa((const struct ether_addr *)&eptr->tst));
   /* check to see if we have an ip packet */
   if (ntohs (eptr->type) == ETHERTYPE_IP)
   {
     fprintf(stdout,"(IP)");
   }else  if (ntohs (eptr->type) == ETHERTYPE_ARP)
   {
     fprintf(stdout,"(ARP)");
    }else  if (ntohs (eptr->type) == ETHERTYPE_REVARP)
    {
      fprintf(stdout,"(RARP)");
    }else {
      fprintf(stdout,"(?)");
      exit(1);
    }
  return eptr->type;
 }

 int main(int argc,char **argv)
 { 
   char *dev; // interface name ex: wlan0
   char errbuf[PCAP_ERRBUF_SIZE];
   pcap_t* descr;
   struct bpf_program fp;      /* hold compiled program     */
   bpf_u_int32 maskp;          /* subnet mask               */
   bpf_u_int32 netp;           /* ip                        */
   u_char* args = NULL;

   if(argc < 2){ 
    fprintf(stdout,"Usage: %s numpackets \"options\"\n",argv[0]);
    return 0;
   }
   /* grab a device to peak into... */
   //    dev = pcap_lookupdev(errbuf);
   dev = "wlan0";
   if(dev == NULL)
   { printf("%s\n",errbuf); exit(1); }
   /* ask pcap for the network address and mask of the device */
   pcap_lookupnet(dev,&netp,&maskp,errbuf);// return the ip of wlan0 and netmask 
   /* open device for reading. NOTE: defaulting to
    * promiscuous mode*/
   descr = pcap_create(dev,errbuf);
   // descr = pcap_open_live(dev,BUFSIZ,1,-1,errbuf);
   if(descr == NULL)
   { printf("pcap_open_live(): %s\n",errbuf); exit(1); }
   if(pcap_set_rfmon(descr,1)!=0 )
   {
     printf("monitor mode enabled: %s\n",errbuf); exit(1); 
   }
   pcap_activate(descr);
   if(argc > 2)
   {
     if(pcap_compile(descr,&fp,argv[2],0,netp) == -1)
     { fprintf(stderr,"Error calling pcap_compile\n"); exit(1); }

     if(pcap_setfilter(descr,&fp) == -1)
     { fprintf(stderr,"Error setting filter\n"); exit(1); }
   }
   int i=    pcap_loop(descr,atoi(argv[1]),my_callback,args);
   if(i==-1) 
   { 
     printf("error in pcap_loop\n"); exit(1); 
   }
   else if (i==0)
   {
     printf("   0    \n"); exit(1); 
   }
   else if (i==-2)
   {
     printf("   -2    \n"); exit(1); 
   }
   fprintf(stdout,"\nfinished\n");
   return 0;
 }
int状态;
...
状态=pcap_激活(描述);
如果(状态<0)
{
fprintf(标准,“pcap_激活失败:%s\n”,pcap_状态为TR(状态));
出口(1);
}

请缩进并格式化您的代码!无法读取。
int status;

    ...

status = pcap_activate(descr);
if (status < 0)
{
  fprintf(stderr, "pcap_activate failed: %s\n", pcap_statustostr(status));
  exit(1);
}