我想实现这样一个功能用pcap抓包在回调函数中用可以处理底层数据的socket套接字发出去。现在发送IP包没问题但是发送MAC包貌似死循环发送了 部分代码如下:
这是我发送数据包的代码
int sockfd_MAC = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); // 创建Socket
void SendMACPacket(int sockfd, void* msg, size_t len)
{
struct sockaddr_ll addr;
memset( &addr, 0, sizeof(addr) );
addr.sll_family = AF_PACKET;
struct ifreq ifstruct;
strcpy(ifstruct.ifr_name, "eth0");
ioctl(sockfd, SIOCGIFINDEX, &ifstruct); //??I/O??
addr.sll_ifindex = ifstruct.ifr_ifindex;
addr.sll_protocol = htons(ETH_P_ALL);
memcpy(addr.sll_addr,((struct eth_hdr*)msg)->h_source,6);
sendto(sockfd, msg, len, 0, &addr, sizeof(struct sockaddr_ll));
}
pcap_loop(handle,packetCount,GenPcapPacket,NULL);
回调函数在这
int GenPcapPacket(u_char argument,const struct pcap_pkthdr pack,const u_char *content)
{
const u_char *buf;
buf = content;
SendMACPacket(sockfd_MAC, (void*)buf, pack->len);
return 0;
}