mfontanini / libtins

High-level, multiplatform C++ network packet sniffing and crafting library.
http://libtins.github.io/
BSD 2-Clause "Simplified" License
1.91k stars 375 forks source link

Converting RawPDU to specific PDU does not retain reference to parent PDUs #350

Closed m-peko closed 5 years ago

m-peko commented 5 years ago

Hi there,

I would like to ask if, when converting from RawPDU object to another specific PDU (let's say FooPDU), it is possible to retain reference to parent PDU.

Following function doesn't do that

template<typename T>
T to() const {
    return T(&payload_[0], static_cast<uint32_t>(payload_.size()));
}

Is this a bug or wanted behaviour?

The reason why I am asking this is because I have a special situation with SCTP protocol when SCTP packet is let's say fragmented like this:

Thank you and regards, Marin

mfontanini commented 5 years ago

That's the intended behavior. This is basically to reinterpret that one layer as something else. If it retained it, then you could have 2 different PDUs having the same parent, which would lead to confusing behavior.

You can call inner_pdu to attach the re-interpreted one to its parent so you replace the RawPDU with whatever you converted it to.

m-peko commented 5 years ago

Yes, your suggestion might work but I think it's far from pretty. It would look like this:

auto raw_pdu = sctp_pdu.rfind_pdu<Tins::RawPDU>();
auto foo_pdu = raw_pdu.to<Tins::FooProtocol>();
sctp_pdu.inner_pdu(foo_pdu);

which is too much error prone because the one that uses the library and is not so familiar with it could easily miss one of the above steps.

But, anyway, I will go with your suggestion.