29 #include <ndn-cxx/lp/fields.hpp>
35 static_assert(lp::tlv::LpPacket < 253,
"LpPacket TLV-TYPE must fit in 1 octet");
36 static_assert(lp::tlv::Sequence < 253,
"Sequence TLV-TYPE must fit in 1 octet");
37 static_assert(lp::tlv::FragIndex < 253,
"FragIndex TLV-TYPE must fit in 1 octet");
38 static_assert(lp::tlv::FragCount < 253,
"FragCount TLV-TYPE must fit in 1 octet");
39 static_assert(lp::tlv::Fragment < 253,
"Fragment TLV-TYPE must fit in 1 octet");
61 , m_linkService(linkService)
65 std::tuple<bool, std::vector<lp::Packet>>
68 BOOST_ASSERT(packet.has<lp::FragmentField>());
69 BOOST_ASSERT(!packet.has<lp::FragIndexField>());
70 BOOST_ASSERT(!packet.has<lp::FragCountField>());
76 return {
true, {packet}};
79 auto [netPktBegin, netPktEnd] = packet.get<lp::FragmentField>();
80 size_t netPktSize = std::distance(netPktBegin, netPktEnd);
83 size_t firstHeaderSize = 0;
84 const auto& packetWire = packet.wireEncode();
85 if (packetWire.type() == lp::tlv::LpPacket) {
86 for (
const auto& element : packetWire.elements()) {
87 if (element.type() != lp::tlv::Fragment) {
88 firstHeaderSize += element.size();
98 size_t firstPayloadSize = std::min(netPktSize, mtu - firstHeaderSize -
MAX_FRAG_OVERHEAD);
100 size_t fragCount = 1 + ((netPktSize - firstPayloadSize) / payloadSize) +
101 ((netPktSize - firstPayloadSize) % payloadSize != 0);
110 std::vector<lp::Packet> frags(fragCount);
111 frags.front() = packet;
112 size_t fragIndex = 0;
113 auto fragBegin = netPktBegin,
114 fragEnd = fragBegin + firstPayloadSize;
115 while (fragBegin < netPktEnd) {
116 lp::Packet& frag = frags[fragIndex];
117 frag.add<lp::FragIndexField>(fragIndex);
118 frag.add<lp::FragCountField>(fragCount);
119 frag.set<lp::FragmentField>({fragBegin, fragEnd});
120 BOOST_ASSERT(frag.wireEncode().size() <= mtu);
124 fragEnd = std::min(netPktEnd, fragBegin + payloadSize);
126 BOOST_ASSERT(fragIndex == fragCount);
128 return {
true, frags};
134 if (flh.
obj.getLinkService() ==
nullptr) {
135 os <<
"[id=0,local=unknown,remote=unknown] ";
138 os << FaceLogHelper<LinkService>(*flh.
obj.getLinkService());
For internal use by FaceLogging macros.
The upper half of a Face.
LpFragmenter(const Options &options, const LinkService *linkService=nullptr)
std::tuple< bool, std::vector< lp::Packet > > fragmentPacket(const lp::Packet &packet, size_t mtu)
Fragments a network-layer packet into link-layer packets.
#define NFD_LOG_FACE_WARN(msg)
Log a message at WARN level.
#define NFD_LOG_INIT(name)
std::ostream & operator<<(std::ostream &os, const FaceLogHelper< Face > &flh)
constexpr size_t MAX_SINGLE_FRAG_OVERHEAD
Maximum overhead on a single fragment, not counting other NDNLPv2 headers.
constexpr size_t MAX_FRAG_OVERHEAD
Maximum overhead of adding fragmentation to payload, not counting other NDNLPv2 headers.
Options that control the behavior of LpFragmenter.
size_t nMaxFragments
Maximum number of fragments in a packet.