C++实现发送和接收自定义类型的链路层数据帧——不绑定实战

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/chengqiuming/article/details/89601248

一 实战前准备

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("sll_halen:%d\n", sa->sll_halen);  
    printf("sll_addr:"); print_str16(sa->sll_addr, sa->sll_halen);  
}  
int main()  
{  
    int             result = 0, fd, n, count = 0;  
    char    buf[LEN];  
    struct sockaddr_ll   sa_recv;  
    struct ifreq    ifr;  
    socklen_t       sa_len = 0;  
    //char    if_name[] = "eno16777736";  
    struct ethhdr *eth; //定义以太网头结构体指针
  
    //创建套接字,自定义协议0x8902
    fd = socket(PF_PACKET, SOCK_RAW, htons(0x8902));  
    if (fd < 0) {  
        perror("socket error\n");  
        return errno;  
    }  

    // 开始等待接收数据  
    while (1) {  
        memset(buf, 0, sizeof(buf));  
        // 第5个和第6个参数没有用NULL,这样可以获得对端(发送端)的物理地址sockaddr_ll的内容
         n = recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&sa_recv, &sa_len);  
        //n = recvfrom(fd, buf, sizeof(buf), 0, NULL, NULL);    //如果不需要打印sa_recv内容,用NULL也可以
        if (n < 0) {  
            printf("sendto error, %d\n", errno);  
            return errno;  
        }  
        printf("******************* recvfrom msg %d ****************\n", ++count);  
        print_str16((unsigned char*)buf, n);     //打印数据帧的内容
        
        eth = (struct ethhdr*)buf;
        //从eth里提取目的mac、源mac、协议号
        printf("proto=0x%04x,dst mac addr:%02x:%02x:%02x:%02x:%02x:%02x\n", ntohs(eth->h_proto), eth->h_dest[0], eth->h_dest[1], eth->h_dest[2], eth->h_dest[3], eth->h_dest[4], eth->h_dest[5]);
        printf("proto=0x%04x,src mac addr:%02x:%02x:%02x:%02x:%02x:%02x\n", ntohs(eth->h_proto), eth->h_source[0], eth->h_source[1], eth->h_source[2], eth->h_source[3], eth->h_source[4], eth->h_source[5]);
        
        // 打印sockaddr_ll的内容
        print_sockaddr_ll(&sa_recv);  
        printf("sa_len:%d\n", sa_len);  
    }  
    return 0;  
}

2 编译并运行

[root@localhost test]# g++ recv.cpp -o recv
[root@localhost test]# ./recv

三 发送端

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
  
#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");  
}  
int main()  
{  
    int             result = 0;  
    int             fd, n, count = 3, nsend = 0;  //count表示发送3个数据包
    char    buf[LEN];  
    struct sockaddr_ll      sa;  
    struct ifreq    ifr;  
    char    if_name[] = "enp0s3";  //本机要发送数据的网卡名称
    //对应enp0s3 ,192.168.0.110的网卡
    char    dst_mac[6] = { 0x08,0x00,0x27,0x60,0x7b,0x7f };
    char    src_mac[6];  
    short   type = htons(0x8902);  
  
    memset(&sa, 0, sizeof(struct sockaddr_ll));  
    memset(buf, 0, sizeof(buf));  
  
    //创建套接字  
    fd = socket(PF_PACKET, SOCK_RAW, htons(0x8902));  
    if (fd < 0) {  
        printf("socket error, %d\n", errno);  
        return errno;  
    }  
  
    //获得网卡索引号  
    strcpy(ifr.ifr_name, if_name);  
    result = ioctl(fd, SIOCGIFINDEX, &ifr);  
    if (result != 0) {  
        printf("get mac index error, %d\n", errno);  
        return errno;  
    }  
    sa.sll_ifindex = ifr.ifr_ifindex;  //赋值给物理层地址
  
    //得到源MAC地址,即本机要发送数据的网卡MAC地址
    result = ioctl(fd, SIOCGIFHWADDR, &ifr);  
    if (result != 0) {  
        printf("get mac addr error, %d\n", errno);  
        return errno;  
    }  
    
    memcpy(src_mac, ifr.ifr_hwaddr.sa_data, 6);  
  
    //设置数据给以太网帧头  
    memcpy(buf, dst_mac, 6);  
    memcpy(buf + 6, src_mac, 6);  
    memcpy(buf + 12, &type, 2);  
  
    print_str16((unsigned char*)buf, sizeof(buf));   //打印发要送的数据帧
    //准备发送数据
    while (count-- > 0) {  
        n = sendto(fd, buf, sizeof(buf), 0, (struct sockaddr *)&sa, sizeof(struct sockaddr_ll));  
        if (n < 0) {  
            printf("sendto error, %d\n", errno);  
            return errno;  
        }  
        printf("sendto msg %d, len %d\n", ++nsend, n);  
    }  
    return 0;  
}

2 编译并运行

[root@localhost test]# ./send
080027607b7f0800270c3bd3890200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
sendto msg 1, len 60
sendto msg 2, len 60
sendto msg 3, len 60

此时接收端收到结果如下:

[root@localhost test]# ./recv
******************* recvfrom msg 1 ****************
080027607b7f0800270c3bd3890200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
proto=0x8902,dst mac addr:08:00:27:60:7b:7f
proto=0x8902,src mac addr:08:00:27:0c:3b:d3
sll_family:0
sll_protocol:0
sll_ifindex:0
sll_hatype:0
sll_pkttype:0
sll_halen:0
sll_addr:sa_len:18
******************* recvfrom msg 2 ****************
080027607b7f0800270c3bd3890200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
proto=0x8902,dst mac addr:08:00:27:60:7b:7f
proto=0x8902,src mac addr:08:00:27:0c:3b:d3
sll_family:17
sll_protocol:0x8902
sll_ifindex:0x2
sll_hatype:1
sll_pkttype:0
sll_halen:6
sll_addr:0800270c3bd3
sa_len:18
******************* recvfrom msg 3 ****************
080027607b7f0800270c3bd3890200000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
proto=0x8902,dst mac addr:08:00:27:60:7b:7f
proto=0x8902,src mac addr:08:00:27:0c:3b:d3
sll_family:17
sll_protocol:0x8902
sll_ifindex:0x2
sll_hatype:1
sll_pkttype:0
sll_halen:6
sll_addr:0800270c3bd3
sa_len:18

四 说明

默认情况下,从任何接口收到符合指定协议(包括自定义协议号)的所有数据报文都会被传送到原始PACKET套接口,而使用bind系统调用并以一个sockaddr_ll结构体对象将PACKET套接口与某个网络接口相绑定,可以使我们的PACKET原始套接字只接收指定接口的数据报文。

猜你喜欢

转载自blog.csdn.net/chengqiuming/article/details/89601248