#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <netinet/ether.h>
#include <netpacket/packet.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <arpa/inet.h>
int main()
{
unsigned char buf[1024] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
unsigned int buf_len = 16;
struct sockaddr_ll sll;
struct ifreq ethreq;
int sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
if(sock_raw_fd < 0)
{
perror("socket");
return -1;
}
strncpy(ethreq.ifr_name, "eth2", IFNAMSIZ); //指定网卡名称
if(-1 == ioctl(sock_raw_fd, SIOCGIFINDEX, ereq)) //获取网络接口
{
perror("ioctl");
close(sock_raw_fd);
exit(-1);
}
/*将网络接口赋值给原始套接字地址结构*/
bzero(&sll, sizeof(sll));
sll.sll_ifindex = ethreq.ifr_ifindex;
int len = sendto(sock_raw_fd, buf, buf_len, 0 , (struct sockaddr *)&sll, sizeof(sll));
if(len == -1)
{
perror("sendto");
}
return 0;
}
参考链接:https://blog.csdn.net/m0_67401660/article/details/124294613
原始套接字指定网口发包
最新推荐文章于 2024-08-28 14:27:00 发布