2019-09-30

引用文章

  • http://blog.chinaunix.net/uid-30226910-id-5766449.html
#include //
#include 

#include //提供htons()函数
//#include //
#include // 提供常量定义, 包括#define ETH_P_IP    0x0800

#include 
/*设备无关的物理层地址结构,通过setsockopt可以设置网卡的多播或混杂模*/
struct sockaddr_ll {
        unsigned short sll_family; /* AF_PACKET*/
        __be16 sll_protocol;
        int sll_ifindex;
        unsigned short sll_hatype;
        unsigned char sll_pkttype;
        unsigned char sll_halen;
        unsigned char sll_addr[8];
};

int main()
{
int sockfd1 = socket(AF_PACKET, SOCK_RAW, 0);

/////////////////////
struct sockaddr_ll l2addr={
  .sll_family=AF_PACKET;
  .sll_protocol=htons(ETH_P_IP),
  .sll_ifindex=1,
};//不需要填写MAC头

}
//#include 
//static struct sockaddr_xdp addr;
// linux 2.0 以前的版本是用struct sockaddr_pkt 的,
//而且获取二层的socket是通过socket(AF_INET, SOCK_PACKET, protocol)来获取的,
// 并没有AF_PACKET 这个domain ,只有SOCK_PACKET这个socket_type


int sockfd1 = socket(AF_INET, SOCK_PACKET, 0);
struct sockaddr_pkt rawsockaddr={.spkt_device="ens33",};//发裸包需要填写MAC头

char data[100] = {0x00};
unsigned int len = 100;
sendto(sockfd1, data, len, &rawsockaddr);

你可能感兴趣的:(2019-09-30)