linux下c语言使用mac地址进行socket通信
server端:
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include<memory.h>
#include <sys/ioctl.h>
#include<stdlib.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#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];
unsigned char send_msg[1024] = {
//--------------组MAC--------14------
0x00, 0x0c, 0x29, 0x0d, 0xe8, 0x83,
0x00, 0x0c, 0x29, 0x56, 0xd5, 0xc7, //dst_mac: E8-6A-64-17-CB-5C目的地址
//src_mac: 8c:ec:4b:73:79:b5源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位校验和*/
};
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;
}
int num = n-14;
memcpy(sendbuffer, buffer, n);
char data[1024];
data[24]='\0';
memcpy(data,sendbuffer+14,num);
if(strcmp(data,"test")==0)
{
int len = sprintf(send_msg+14, "%s", "shou");
struct sockaddr_ll sll; //原始套接字地址结构
struct ifreq req; //网络接口地址
strncpy(req.ifr_name, "ens33", IFNAMSIZ); //指定网卡名称
if(-1 == ioctl(sock, SIOCGIFINDEX, &req)) //获取网络接口
{
perror("ioctl");
close(sock);
exit(-1);
}
/*将网络接口赋值给原始套接字地址结构*/
bzero(&sll, sizeof(sll));
sll.sll_ifindex = req.ifr_ifindex;
len = sendto(sock, send_msg, 14+len, 0 , (struct sockaddr *)&sll, sizeof(sll));
if(len == -1)
{
perror("sendto");
}
}
printf("%s\n",data);
}
}
socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))
链路层原始套接字调用socket()函数创建。第一个参数指定协议族类型为PF_PACKET,第二个参数type可以设置为SOCK_RAW或SOCK_DGRAM,第三个参数是协议类型(该参数只对报文接收有意义)。参数type设置为SOCK_RAW时,套接字接收和发送的数据都是从MAC首部开始的。ETH_P_ALL 报收本机收到的所有二层报文。
数据链路层的头信息通常定义在 sockaddr_ll 的结构体中,protocol是按照网络字节顺序(network byte order),大部分定义在头文件中,设置协议时,例如 htons(ETH_P_ALL)来接收所有的数据包;
如果要获取从指定以太网接口卡上的数据包时,在 struct sockaddr_ll中指定网络接口卡,绑定(bind)数据包到该interface上。只有sll_protocol和 sll_ifindex这两个地址字段是用来bind的。
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 包含了物理层地址和其长度;
当发送数据包时,指定 sll_family, sll_addr, sll_halen, sll_ifindex, sll_protocol 就足够了。其它字段设置为0; sll_hatype和 sll_pkttype是在接收数据包时使用的; 如果要bind, 只需要使用 sll_protocol和 sll_ifindex;
client端
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/ether.h>
#include <netpacket/packet.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include<unistd.h> //close
int main(int argc, char *argv[])
{
//1.创建通信用的原始套接字
int n;
char buffer[1024];
int sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
uint8_t sendbuffer[1024];
//2.根据各种协议首部格式构建发送数据报
unsigned char send_msg[1024] = {
//--------------组MAC--------14------
0x00, 0x0c, 0x29, 0x56, 0xd5, 0xc7, //dst_mac: E8-6A-64-17-CB-5C目的地址
0x00, 0x0c, 0x29, 0x0d, 0xe8, 0x83, //src_mac: 8c:ec:4b:73:79:b5源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位校验和*/
};
while(1)
{
int len = sprintf(send_msg+14, "%s", "test");
struct sockaddr_ll sll; //原始套接字地址结构
struct ifreq req; //网络接口地址
strncpy(req.ifr_name, "ens33", 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;
len = sendto(sock_raw_fd, send_msg, 14+len, 0 , (struct sockaddr *)&sll, sizeof(sll));
if(len == -1)
{
perror("sendto");
}
/
socklen_t addr_length = sizeof(struct sockaddr_ll);
n = recvfrom(sock_raw_fd, buffer,1024,0, (struct sockaddr *)&sll, &addr_length);
if (n < 14) {
continue;
}
int num = n-14;
memcpy(sendbuffer, buffer, n);
char data[1024];
data[24]='\0';
memcpy(data,sendbuffer+14,num);
printf("%s\n",data);
}
return 0;
}
使用时注意修改网卡名称,目的地址和源地址。