简体   繁体   English

使用 libtins 重新创建 IP 数据包更改其 header

[英]Using libtins to recreate IP packet change its header

I have a uint8_t* buffer which I get by doing buf.data() .我有一个uint8_t*缓冲区,我通过执行buf.data()获得。 It's a buffer of an IP packet.它是 IP 数据包的缓冲区。

I want to change the source address and recalculate the checksum, so I recreated the packet in libtins by doing what you see below:我想更改源地址并重新计算校验和,所以我通过执行您在下面看到的操作在 libtins 中重新创建了数据包:

//prints original packet  
for (size_t i = 0; i < buf.size(); i++)
{
    printf("%x ", buf.data()[i]);
}
printf("\n");

Tins::IP pa = Tins::IP(buf.data(), buf.size());
pa.src_addr(newIpv4address);
uint8_t *packetData = pa.serialize().data();

//prints libtins packet created from original packet
for (size_t i = 0; i < buf.size(); i++)
{
    printf("%x ", packetData[i]);
}
printf("\n");

The problem is that the packet printed from libtins appears with a different header:问题是从 libtins 打印的数据包出现了不同的 header:

45 0 0 34 0 0 40 0 40 6 6b 53 c0 a8 45 1 ac d9 1c ee 0 20 0 50 0 0 0 0 0 0 0 0 80 2 fd e8 a5 4d 0 0 2 4 5 b4 3 3 0 4 2 0 0 0 

60 b 0 34 62 76 0 0 d0 8 0 28 62 76 0 0 ac d9 1c ee 0 20 0 50 0 0 0 0 0 0 0 0 80 2 fd e8 eb 47 0 0 2 4 5 b4 3 3 0 4 2 0 0 0

I then tried to change the buf with the new packet even though it looks different:然后我尝试用新数据包更改buf ,即使它看起来不同:

uint8_t* b = buf.data();
uint8_t* o = pa.serialize().data();

for (int i = 0; i < buf.size(); i++)
{
    b[i] = o[i];
}

auto p = Tins::IP(buf.data(), buf.size());

but on the line auto p = Tins::IP(buf.data(), buf.size());但是就行auto p = Tins::IP(buf.data(), buf.size()); I get:我得到:

terminate called after throwing an instance of 'Tins::malformed_packet'
  what():  Malformed packet

which is strange since it's literally a packet generated from libtins.这很奇怪,因为它实际上是从 libtins 生成的数据包。

What am I doing wrong?我究竟做错了什么? According to libtins documentation I can create a libtins packet from a buffer and a size.根据libtins 文档,我可以从缓冲区和大小创建一个 libtins 数据包。

UPDATE:更新:

#include <iostream>
#include <memory>
#include <tins/ip.h>

int main()
{
    const int packet_size = 52;
    uint8_t ip_packet[] = {
        69, 0, 0, 52, 0, 0, 64, 0, 64, 6, 107,
        83, 192, 168, 69, 1, 172, 217, 28, 238,
        0, 112, 0, 80, 0, 0, 0, 0, 0, 0, 0, 0, 128,
        2, 253, 232, 164, 253, 0, 0, 2, 4, 5, 180,
        3, 3, 0, 4, 2, 0, 0, 0};

    std::cout << "packet BEFORE " << std::endl;
    for (size_t i = 0; i < packet_size; i++)
    {
        printf("%x ", ip_packet[i]);
    }

    uint8_t b[52];
    for (int i = 0; i < packet_size; i++)
    {
        b[i] = ip_packet[i];
    }
    printf("\n");

    Tins::IP pa = Tins::IP(b, packet_size);
    pa.src_addr("172.78.123.76");
    uint8_t *packetData = pa.serialize().data();
    std::cout << "TINS packet: " << std::endl;
    for (size_t i = 0; i < packet_size; i++)
    {
        printf("%x ", packetData[i]);
    }
    printf("\n");

    return 0;
}

OUTPUT: OUTPUT:

packet BEFORE 
45 0 0 34 0 0 40 0 40 6 6b 53 c0 a8 45 1 ac d9 1c ee 0 70 0 50 0 0 0 0 0 0 0 0 80 2 fd e8 a4 fd 0 0 2 4 5 b4 3 3 0 4 2 0 0 0 
TINS packet: 
0 0 0 0 0 0 0 0 10 90 68 1 0 0 0 0 ac d9 1c ee 0 70 0 50 0 0 0 0 0 0 0 0 80 2 fd e8 83 c 0 0 2 4 5 b4 3 3 0 4 2 0 0 0 

With the sample IP packet you have provided, I've done the following:使用您提供的示例 IP 数据包,我完成了以下操作:

uint8_t ip_packet[] = {
    69,  0,   0,   52,  0,   0,   64, 0, 
    64,  6,   107, 83,  192, 168, 69, 1,
    172, 217, 28,  238, 0,   112, 0,  80,
    0,   0,   0,   0,   0,   0,   0,  0,
    128, 2,   253, 232, 164, 253, 0,  0,
    2,   4,   5,   180, 3,   3,   0,  4,
    2,   0,   0,   0
};

Tins::IP ip(ip_packet, sizeof(ip_packet));
for (std::size_t i = 0; i < sizeof(ip_packet); i++)
    std::cout << +ip_packet[i] << " ";

std::cout << std::endl;

ip.src_addr("192.155.32.10");

auto data = ip.serialize();
for (std::size_t i = 0; i < data.size(); i++)
    std::cout << +data[i] << " ";

std::cout << std::endl;

and I get the following output:我得到以下 output:

69 0 0 52 0 0 64 0 64 6 107 83 192 168 69 1 172 217 28 238 0 112 0 80 0 0 0 0 0 0 0 0 128 2 253 232 164 253 0 0 2 4 5 180 3 3 0 4 2 0 0 0
69 0 0 52 0 0 64 0 64 6 144 87 192 155 32 10 172 217 28 238 0 112 0 80 0 0 0 0 0 0 0 0 128 2 253 232 202 1 0 0 2 4 5 180 3 3 0 4 2 0 0 0

so it works as expected.所以它按预期工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM