1.套接字创建及解读

 socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));

        链路层原始套接字调用socket()函数创建。
        第一个参数指定协议族类型为PF_PACKET,第二个参数type可以设置为SOCK_RAW或SOCK_DGRAM,第三个参数是协议类型(该参数只对报文接收有意义)。
参数type设置为SOCK_RAW时,套接字接收和发送的数据都是从MAC首部开始的。ETH_P_ALL 报收本机收到的所有二层报文。

2.MAC层通讯数据帧头格式

//2.根据各种协议首部格式构建发送数据报
	unsigned char send_msg[1024] = {
		//--------------组MAC--------14------
		0x00, 0x30, 0x1B, 0xBA, 0x02, 0xDB, //dst
		0x36, 0x72, 0xC3, 0x0A, 0xFE, 0xB3, //src
		0x08, 0x00,                         //类型:0x0800 IP协议
	/*	//--------------组IP---------20------
		0x45, 0x00, 0x00, 0x00,             //版本号:4, 首部长度:20字节, TOS:0, --总长度--:
		0x00, 0x00, 0x00, 0x00,				//16位标识、3位标志、13位片偏移都设置0
		0x80, 17,   0x00, 0x00,				//TTL:128、协议:UDP(17)、16位首部校验和
		10,  221,   20,  11,				//src_ip: 10.221.20.11
		10,  221,   20,  10,				//dst_ip: 10.221.20.10
		//--------------组UDP--------8+78=86------
		0x1f, 0x90, 0x1f, 0x90,             //src_port:0x1f90(8080), dst_port:0x1f90(8080)
		0x00, 0x00, 0x00, 0x00,               //#--16位UDP长度--30个字节、#16位校验和*/
	};

3.sockaddr_II结构体解读

struct sockaddr_ll {
unsigned short sll_family; /* Always AF_PACKET /
unsigned short sll_protocol; / Physical-layer protocol /
int sll_ifindex; / Interface number /
unsigned short sll_hatype; / ARP hardware type /
unsigned char sll_pkttype; / Packet type /
unsigned char sll_halen; / Length of address /
unsigned char sll_addr[8]; / Physical-layer address */
};
sll_protocol : 标准以太网协议类型,按网络字节顺序。定义在中。
sll_ifindex: interface索引,0 匹配所有的网络接口卡;
sll_hatype: ARP 硬件地址类型(hardware address type) 定义在中,常用 ARPHRD_ETHER
sll_pkttype: 包含了packet类型。
PACK_HOST 包地址为本地主机地址。
PACK_BROADCAST 物理层广播包。
PACK_MULTICAST 发送到物理层多播地址的包。
PACK_OTHERHOST 发往其它在混杂模式下被设备捕获的主机的包。
PACK_OUTGOING 本地回环包;
sll_addr 和 ssl_halen 包含了物理层地址和其长度

4.参考代码及解析

发送端

//发数据报
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <net/if.h>				//struct ifreq
#include <sys/ioctl.h>			//ioctl、SIOCGIFADDR
#include <sys/socket.h>
#include <netinet/ether.h>		//ETH_P_ALL
#include <netpacket/packet.h>	//struct sockaddr_ll
#include <arpa/inet.h>
#include <sys/types.h>
#include<unistd.h>               //close
unsigned short checksum(unsigned short *buf, int nword);//校验和函数
int main(int argc, char *argv[])
{
	//1.创建通信用的原始套接字
	int sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
	/*
    0x00, 0x30, 0x1B, 0xBA, 0x02, 0xDB, //dst 注意使用正确的mac地址
	0x36, 0x72, 0xC3, 0x0A, 0xFE, 0xB3, //src_mac 
    */
	//2.根据各种协议首部格式构建发送数据报
	unsigned char send_msg[1024] = {
		//--------------组MAC--------14------
		0x00, 0x30, 0x1B, 0xBA, 0x02, 0xDB, //dst_mac
		0x36, 0x72, 0xC3, 0x0A, 0xFE, 0xB3, //src_mac
		0x08, 0x00,                         //类型:0x0800 IP协议
	/*	//--------------组IP---------20------
		0x45, 0x00, 0x00, 0x00,             //版本号:4, 首部长度:20字节, TOS:0, --总长度--:
		0x00, 0x00, 0x00, 0x00,				//16位标识、3位标志、13位片偏移都设置0
		0x80, 17,   0x00, 0x00,				//TTL:128、协议:UDP(17)、16位首部校验和
		10,  221,   20,  11,				//src_ip: 10.221.20.11
		10,  221,   20,  10,				//dst_ip: 10.221.20.10
		//--------------组UDP--------8+78=86------
		0x1f, 0x90, 0x1f, 0x90,             //src_port:0x1f90(8080), dst_port:0x1f90(8080)
		0x00, 0x00, 0x00, 0x00,               //#--16位UDP长度--30个字节、#16位校验和*/
	};
	
	int len = sprintf(send_msg+14, "%s", "this is for the udp test");
/*	if(len % 2 == 1)//判断len是否为奇数
	{
		len++;//如果是奇数,len就应该加1(因为UDP的数据部分如果不为偶数需要用0填补)
	}
	
	*((unsigned short *)&send_msg[16]) = htons(20+8+len);//IP总长度 = 20 + 8 + len
	*((unsigned short *)&send_msg[14+20+4]) = htons(8+len);//udp总长度 = 8 + len
	//3.UDP伪头部
	unsigned char pseudo_head[1024] = {
		//------------UDP伪头部--------12--
		192,  168,   0,  1,				//src_ip: 10.221.20.11
		192,  168,   0,  255,				//dst_ip: 10.221.20.10
		0x00, 17,   0x00, 0x00,             	//0,17,#--16位UDP长度--20个字节
	};
	
	*((unsigned short *)&pseudo_head[10]) = htons(8 + len);//为头部中的udp长度(和真实udp长度是同一个值)
	//4.构建udp校验和需要的数据报 = udp伪头部 + udp数据报
	memcpy(pseudo_head+12, send_msg+34, 8+len);//--计算udp校验和时需要加上伪头部--
	//5.对IP首部进行校验
	*((unsigned short *)&send_msg[24]) = htons(checksum((unsigned short *)(send_msg+14),20/2));
	//6.--对UDP数据进行校验--
	*((unsigned short *)&send_msg[40]) = htons(checksum((unsigned short *)pseudo_head,(12+8+len)/2));
	
	*/
	//发送数据
	struct sockaddr_ll sll;					//原始套接字地址结构
	struct ifreq req;					//网络接口地址
	
	strncpy(req.ifr_name, "eth0", IFNAMSIZ);			//指定网卡名称
	if(-1 == ioctl(sock_raw_fd, SIOCGIFINDEX, &req))	//获取网络接口
	{
		perror("ioctl");
		close(sock_raw_fd);
		exit(-1);
	}
	
	/*将网络接口赋值给原始套接字地址结构*/
	bzero(&sll, sizeof(sll));
	sll.sll_ifindex = req.ifr_ifindex;

    while(1){
        len = sendto(sock_raw_fd, send_msg, 14+len, 0 , (struct sockaddr *)&sll, sizeof(sll));
        if(len == -1)
        {
            perror("sendto");
        }
        sleep(1);
    }
	return 0;
}
 

接收端

//捕获二层包,并把数据输出
#include<unistd.h>
#include<stdio.h>
#include<sys/socket.h>
#include<netinet/ip.h>
#include<netinet/udp.h>
#include<memory.h>
#include<stdlib.h>
#include<linux/if_ether.h>
#include<linux/if_packet.h> // sockaddr_ll
#include<arpa/inet.h>
#include<netinet/if_ether.h>

#include<errno.h>
#include<netinet/ether.h>
#include<net/if.h>
#include<string.h>


int main(int argc, char **argv) {
	int sock, n;
	char buffer[1024];
 
    //获取所有数据
	if ((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0)
	{
		perror("socket");
		exit(1);
	}
 
	struct sockaddr_ll client;
	socklen_t addr_length = sizeof(struct sockaddr_ll);
	uint8_t sendbuffer[1024];
 
	while (1) {
		n = recvfrom(sock, buffer,1024,0, (struct sockaddr *)&client, &addr_length);
		if (n < 14) {
			continue;
		}

		printf("收到消息的bytes%d个\n",n-14);
		int num = n-14;
		memcpy(sendbuffer, buffer, n);
		char data[1024];
        data[24]='\0';
        memcpy(data,sendbuffer+14,num);
		printf("%s\n",data);
 
        sleep(0.5);
	}
 
}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐