一 实战前准备
1 准备两台虚拟机
A机配置
网卡 |
IP |
MAC |
enp0s3 |
192.168.0.110 |
08:00:27:60:7b:7f |
enp0s8 |
192.168.0.104 |
08:00:27:87:45:35 |
B机配置
网卡 |
IP |
MAC |
enp0s3 |
192.168.0.120 |
08:00:27:0c:3b:d3 |
二 接收端
1 代码
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netpacket/packet.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <sys/ioctl.h>
#include <arpa/inet.h> //for htons
#include <netinet/if_ether.h> //for ethhdr
#define LEN 60
void print_str16(unsigned char buf[], size_t len)
{
int i;
unsigned char c;
if (buf == NULL || len <= 0)
return;
for (i = 0; i < len; i++) {
c = buf[i];
printf("%02x", c);
}
printf("\n");
}
void print_sockaddr_ll(struct sockaddr_ll *sa)
{
if (sa == NULL)
return;
printf("sll_family:%d\n", sa->sll_family);
printf("sll_protocol:%#x\n", ntohs(sa->sll_protocol));
printf("sll_ifindex:%#x\n", sa->sll_ifindex);
printf("sll_hatype:%d\n", sa->sll_hatype);
printf("sll_pkttype:%d\n", sa->sll_pkttype);
printf(