一、逐个元素组包
unsigned char msg[]={
0x70,0x5A,0x0F,0x63,0xF5,0x9D,
0x00,0x00,0x00,0x00,0x00,0x00,
0x08,0x06,
0x00,0x01,
0x08,0x00,
6,
4,
0x00,0x02,
0x00,0x00,0x00,0x00,0x00,0x00,
192,168,0,111,
0x70,0x5A,0x0F,0x63,0xF5,0x9D,
192,168,0,110
};
二、利用结构体方式组包
以太网头部 ARP头部 使用的时候需要在/usr/include/net/if_arp.h 中将arphdr结构体中的#if 0改为1
unsigned char dst_mac[6]={0x70,0x5A,0x0F,0x63,0xF5,0x9D,};
unsigned char src_mac[6]={0x00};
unsigned char src_ip[4]={192,168,0,111};
unsigned char dst_ip[4]={192,168,0,110};
unsigned char msg[1024]="";
struct ether_header *eth_addr = (struct ether_header *)msg;
memcpy(eth_addr->ether_dhost, dst_mac, 6);
memcpy(eth_addr->ether_shost, src_mac, 6);
eth_addr->ether_type = htons(0x0806);
struct arphdr *arp_head = (struct arphdr *)(msg+14);
arp_head->ar_hrd = htons(1);
arp_head->ar_pro = htons(0x0800);
arp_head->ar_hln = 6;
arp_head->ar_pln = 4;
arp_head->ar_op = htons(2);
memcpy(arp_head->__ar_sha, src_mac, 6);
memcpy(arp_head->__ar_sip, src_ip, 4);
memcpy(arp_head->__ar_tha, dst_mac, 6);
memcpy(arp_head->__ar_tip, dst_ip, 4);
|