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

一 实战前准备

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原始套接字只接收指定接口的数据报文。

 

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
数据预处理中的自定义转换是指将数据集中的原始数据按照一定的规则进行处理,以便于后续的模型训练和应用。C++作为一门强类型语言,提供了丰富的数据类型和操作函数,可以非常方便地实现数据预处理中的自定义转换。下面以一个简单的案例来演示如何使用C++实现数据预处理中的自定义转换。 假设我们有一个包含学生信息的数据集,其中每个学生的信息包括姓名、年龄、性别与成绩四个属性。现在我们要对这个数据集进行处理,将每个学生的成绩按照以下规则转换为一个0~5的整数: - 小于60分的成绩转换为0 - 60~69分的成绩转换为1 - 70~79分的成绩转换为2 - 80~89分的成绩转换为3 - 90~99分的成绩转换为4 - 100分的成绩转换为5 下面是一个使用C++实现的解决方案: ```c++ #include <iostream> #include <vector> #include <string> using namespace std; // 定义一个结构体,表示学生信息 struct Student { string name; int age; char gender; int score; }; // 自定义转换函数,将成绩转换为0~5的整数 int score_transform(int score) { if (score < 60) { return 0; } else if (score < 70) { return 1; } else if (score < 80) { return 2; } else if (score < 90) { return 3; } else if (score < 100) { return 4; } else { return 5; } } int main() { // 定义一个学生信息列表 vector<Student> students = { {"张三", 18, 'M', 75}, {"李四", 19, 'F', 68}, {"王五", 20, 'M', 92}, {"赵六", 21, 'F', 85} }; // 遍历学生信息列表,将成绩转换为整数 for (auto& student : students) { student.score = score_transform(student.score); } // 输出转换后的学生信息列表 for (auto& student : students) { cout << student.name << " " << student.age << " " << student.gender << " " << student.score << endl; } return 0; } ``` 在上面的代码中,我们首先定义了一个包含学生信息的结构体`Student`,并且定义了一个自定义转换函数`score_transform`,用于将成绩转换为整数。然后我们定义了一个学生信息列表`students`,并且遍历该列表,将每个学生的成绩按照自定义转换函数进行转换。最后我们输出转换后的学生信息列表。 需要注意的是,在实际应用中,我们可能需要进行更加复杂的自定义转换,例如对数据进行归一化、标准化、降维等处理。C++提供了丰富的函数库和工具,可以帮助我们实现这些复杂的自定义转换。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值