测试MAC帧数据包使用的传输协议
1 #include <stdio.h>
2 #include <sys/socket.h>
3 #include <stdlib.h>
4 #include <arpa/inet.h>
5 #include <unistd.h>
6 #include <netinet/if_ether.h>
7
8 int main(int argc, const char *argv[])
9 {
10 //创建原始套接字
11 int sockfd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
12 if(sockfd < 0)
13 {
14 perror("fail to socket");
15 exit(1);
16 }
17 printf("sockfd = %d\n", sockfd);
18
19 //接收链路层数据
20 int ret;
21 unsigned short mac_type;
22 while(1)
23 {
24 unsigned char buf[1600] = {};
25 char src_mac[18] = {};
26 char dest_mac[18] = {};
27 //buf将存放完整的帧数据 (例如:MAC头部+IP头+tcp/udp头+应用数据)
28 ret = recvfrom(sockfd, buf, sizeof(buf), 0, NULL