gpt4 book ai didi

c - 在 pcap_creat 和 pcap_set_rfmon 之后使用 pcap 库,pcap_loop 中的处理程序不起作用

转载 作者:行者123 更新时间:2023-11-30 19:47:43 25 4
gpt4 key购买 nike

#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;
}

我需要做的就是将我的卡接口(interface)转换为监视模式,以嗅探信标帧以达到某些良好的目的,但是当在 pcap_creat 和 pcap_set_rfmon 之后使用 pcap 库时,pcap_loop 中的处理程序不起作用,所以任何人都可以帮助我知道什么问题是??

最佳答案

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

关于c - 在 pcap_creat 和 pcap_set_rfmon 之后使用 pcap 库,pcap_loop 中的处理程序不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20702918/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com