16#include "ns3/assert.h"
45 static constexpr std::array<uint8_t, 16> staticDict = {0x16,
61 std::copy(staticDict.begin(), staticDict.end(), dict + 32);
67 if ((
byte & 0x80) == 0)
72 else if ((
byte & 0xF0) == 0x80)
77 else if (
byte == 0x90)
82 else if ((
byte & 0xE0) == 0xA0)
87 else if ((
byte & 0xC0) == 0xC0)
102 const uint8_t* compressed,
108 NS_LOG_FUNCTION(srcAddr << dstAddr << compressedLen << outputMaxLen << useStopCode);
113 std::vector<uint8_t> buffer(bufferSize);
122 bool stopCodeFound =
false;
124 while (inputPos < compressedLen && !stopCodeFound)
126 uint8_t codeByte = compressed[inputPos++];
142 NS_LOG_WARN(
"GHC: Literal count k=" << k <<
" is reserved");
145 if (inputPos + k > compressedLen)
147 NS_LOG_WARN(
"GHC: Literal overruns compressed data");
150 if (outputPos + k > bufferSize)
156 std::memcpy(buffer.data() + outputPos, compressed + inputPos, k);
166 if (outputPos + n > bufferSize)
168 NS_LOG_WARN(
"GHC: Zero insert would exceed buffer");
172 std::memset(buffer.data() + outputPos, 0, n);
181 NS_LOG_WARN(
"GHC: Unexpected stop code in non-extension context");
185 stopCodeFound =
true;
195 uint32_t nBit = (codeByte >> 4) & 0x01;
208 uint32_t nnn = (codeByte >> 3) & 0x07;
212 uint32_t offset = kkk + sa + copyLen;
218 if (offset > outputPos)
220 NS_LOG_WARN(
"GHC: Backref offset " << offset <<
" exceeds buffer position "
224 if (outputPos + copyLen > bufferSize)
231 uint32_t srcPos = outputPos - offset;
232 for (
uint32_t i = 0; i < copyLen; i++)
234 buffer[outputPos + i] = buffer[srcPos + i];
236 outputPos += copyLen;
241 NS_LOG_WARN(
"GHC: Reserved bytecode 0x" << std::hex << +codeByte);
252 NS_LOG_DEBUG(
"GHC: Decompressed " << compressedLen <<
" bytes to " << decompressedLen
255 return decompressedLen;
262 while (count < remaining && input[count] == 0)
276 uint32_t kBase = matchOffset - matchLength;
278 uint32_t na = (nBase > 7) ? ((nBase - 7 + 7) / 8) * 8 : 0;
279 uint32_t sa = (kBase > 7) ? ((kBase - 7 + 7) / 8) * 8 : 0;
283 while (sa > 0 || na > 0)
286 sa -= std::min<uint32_t>(sa, 15 * 8);
287 na -= std::min<uint32_t>(na, 8);
295 const uint8_t* input,
315 for (
uint32_t back = 2; back <= bufLen; back++)
320 while (len < maxMatchLen && buffer[searchPos + len] == input[len])
327 len = std::min(len, back);
329 if (len < 2 || len <= matchLength)
344 if (matchLength == maxMatchLen)
350 return (matchLength >= 2);
390 na = ((na + 7) / 8) * 8;
397 sa = ((sa + 7) / 8) * 8;
404 if (nnn > 7 || kkk > 7)
406 NS_LOG_WARN(
"GHC: Cannot encode backref len=" << n <<
" offset=" << s);
412 if (outPos + extArgBytes + 1 > outputMaxLen)
421 while (remainingSa > 0 || remainingNa > 0)
425 uint32_t ssss = std::min<uint32_t>(remainingSa / 8, 15);
426 uint32_t nBit = (remainingNa >= 8) ? 1 : 0;
428 uint8_t extByte = 0xA0 | (nBit << 4) | (ssss & 0x0F);
429 output[outPos++] = extByte;
431 remainingSa -= ssss * 8;
439 uint8_t backrefByte = 0xC0 | ((nnn & 0x07) << 3) | (kkk & 0x07);
440 output[outPos++] = backrefByte;
448 const uint8_t* input,
454 NS_LOG_FUNCTION(srcAddr << dstAddr << inputLen << outputMaxLen << emitStopCode);
471 std::vector<uint8_t> literalBuf;
474 auto flushLiterals = [&]() ->
bool {
475 while (!literalBuf.empty())
480 if (outPos + 1 + chunk > outputMaxLen)
485 output[outPos++] =
static_cast<uint8_t
>(chunk);
486 std::memcpy(output + outPos, literalBuf.data(), chunk);
489 literalBuf.erase(literalBuf.begin(), literalBuf.begin() + chunk);
494 while (inPos < inputLen)
519 const bool preferBackref = haveMatch && (zeros < 2 || matchLength > zeros);
523 if (!flushLiterals())
529 if (
EmitBackref(output, outPos, outputMaxLen, matchLength, matchOffset))
532 std::memcpy(buffer.data() + bufPos, input + inPos, matchLength);
533 bufPos += matchLength;
534 inPos += matchLength;
539 outPos = savedOutPos;
544 if (!flushLiterals())
553 if (outPos + 1 > outputMaxLen)
559 output[outPos++] = 0x80 | ((emit - 2) & 0x0F);
562 std::memset(buffer.data() + bufPos, 0, emit);
571 literalBuf.push_back(input[inPos]);
572 buffer[bufPos++] = input[inPos];
577 if (!flushLiterals())
585 if (outPos + 1 > outputMaxLen)
589 output[outPos++] = 0x90;
593 uint32_t overhead = emitStopCode ? 1 : 0;
594 if (outPos >= inputLen + overhead)
596 NS_LOG_DEBUG(
"GHC: No compression benefit (" << outPos <<
" >= " << inputLen <<
")");
600 NS_LOG_DEBUG(
"GHC: Compressed " << inputLen <<
" bytes to " << outPos <<
" bytes (ratio "
601 <<
static_cast<float>(inputLen) / outPos <<
"x)");
623 static TypeId tid =
TypeId(
"ns3::SixLowPanGhcExtension")
625 .SetGroupName(
"SixLowPan")
679 while (start.GetRemainingSize() > 0 &&
m_blobLength < 255)
681 uint8_t
byte = start.ReadU8();
690 if ((
byte & 0x80) == 0)
757 std::memcpy(
m_blob, blob, size);
764 std::memcpy(blob,
m_blob, copyLen);
800 .SetGroupName(
"SixLowPan")
895 uint8_t packed = start.ReadU8();
896 m_srcPort = 0xF0B0 | ((packed >> 4) & 0x0F);
1007 .SetGroupName(
"SixLowPan")
1040 NS_FATAL_ERROR(
"The two-iterator Deserialize must be used for this variable-sized header");
1047 uint8_t nhc = start.ReadU8();
1053 uint32_t blobLen = start.GetDistanceFrom(end);
1072 std::memcpy(
m_blob, blob, size);
1079 std::memcpy(blob,
m_blob, copyLen);
1112 .SetGroupName(
"SixLowPan")
1150 uint8_t type = start.ReadU8();
1152 uint8_t len = start.ReadU8();
1153 m_flags = start.ReadNtohU32();
1156 uint32_t remaining = (len * 8) - 6;
1157 for (
uint32_t i = 0; i < remaining; i++)
iterator in a Buffer instance
Describes an IPv6 address.
void GetBytes(uint8_t buf[ADDRESS_LENGTH]) const
Get the bytes corresponding to the address.
6LoWPAN Capability Indication Option (6CIO) - RFC 7400 Section 6.
static constexpr uint8_t SIXLOWPAN_6CIO_TYPE
ND Option Type.
uint32_t GetSerializedSize() const override
void Serialize(Buffer::Iterator start) const override
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
static TypeId GetTypeId()
Get the type ID.
void Print(std::ostream &os) const override
static constexpr uint8_t SIXLOWPAN_6CIO_LEN
Length in 8-octet units.
void SetGhcCapable(bool ghcCapable)
Set the GHC capability flag.
bool GetGhcCapable() const
Get the GHC capability flag.
uint32_t Deserialize(Buffer::Iterator start) override
static constexpr uint32_t GHC_FLAG_BIT
G bit position.
uint32_t m_flags
Flags field (variable, up to 48 bits per RFC).
NhcDispatch_e
Dispatch values for Next Header compression.
static uint32_t Decompress(const Ipv6Address &srcAddr, const Ipv6Address &dstAddr, const uint8_t *compressed, uint32_t compressedLen, uint8_t *output, uint32_t outputMaxLen, bool useStopCode=false)
Decompress a GHC-compressed byte stream.
static uint32_t CountBackrefExtArgBytes(uint32_t matchLength, uint32_t matchOffset)
Count the extended-args bytes a backreference encoding needs.
static GhcBytecodeType ClassifyBytecode(uint8_t byte)
Classify a GHC bytecode instruction.
static uint32_t CountZeros(const uint8_t *input, uint32_t remaining)
Count consecutive zero bytes in input.
static void InitDictionary(uint8_t *dict, const Ipv6Address &srcAddr, const Ipv6Address &dstAddr)
Initialize the 48-byte decompression dictionary.
static uint32_t Compress(const Ipv6Address &srcAddr, const Ipv6Address &dstAddr, const uint8_t *input, uint32_t inputLen, uint8_t *output, uint32_t outputMaxLen, bool emitStopCode=false)
Compress a byte stream using the GHC LZ77 algorithm.
static constexpr uint32_t MAX_LITERAL_RUN
Maximum bytes copied by one 0kkkkkkk literal bytecode.
static constexpr uint32_t MAX_BACKREF_LEN
Maximum copy length of one 11nnnkkk backreference.
static bool EmitBackref(uint8_t *output, uint32_t &outPos, uint32_t outputMaxLen, uint32_t matchLength, uint32_t matchOffset)
Emit a backreference bytecode (with optional extended args).
static constexpr uint32_t MAX_ZERO_RUN
Maximum zeros inserted by one 1000nnnn bytecode: n+2 with n <= 15.
static constexpr uint32_t DICTIONARY_SIZE
Predefined dictionary size (48 bytes).
static bool FindLongestMatch(const uint8_t *buffer, uint32_t bufLen, const uint8_t *input, uint32_t inputRemaining, uint32_t &matchOffset, uint32_t &matchLength)
Find the longest match in the buffer for LZ77 compression.
GHC NHC Extension Header - see RFC 7400 Section 5.
uint8_t m_nhcByte
NHC header byte: 1011EID[NH].
void Serialize(Buffer::Iterator start) const override
static constexpr uint32_t MAX_BLOB_SIZE
Maximum blob size: the blob length field is 8 bits.
static constexpr uint8_t GHC_EXT_BASE
Base NHC byte pattern for GHC extension: 10110000 = 0xB0.
uint8_t m_blobLength
Length of GHC compressed data.
Eid_e GetEid() const
Get the Extension Header Type.
static TypeId GetTypeId()
Get the type ID.
bool GetNh() const
Get the NH field.
uint8_t GetNextHeader() const
Get the Next Header field value.
uint32_t GetSerializedSize() const override
void SetNh(bool nhField)
Set the NH field (next header compressed flag).
static constexpr uint8_t EID_MASK
Mask for EID field extraction (bits 1-3).
uint8_t m_blob[MAX_BLOB_SIZE+1]
GHC compressed bytecodes.
Eid_e
Extension Header ID (same encoding as RFC 6282).
uint32_t CopyBlob(uint8_t *blob, uint32_t size) const
Copy the GHC-compressed bytecode blob.
void Print(std::ostream &os) const override
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
SixLowPanDispatch::NhcDispatch_e GetNhcDispatchType() const
Get the NhcDispatch type.
void SetBlob(const uint8_t *blob, uint32_t size)
Set the GHC-compressed bytecode blob.
uint32_t Deserialize(Buffer::Iterator start) override
void SetEid(Eid_e eid)
Set the Extension Header Type.
uint8_t m_nextHeader
Next Header value (if NH=0).
void SetNextHeader(uint8_t nextHeader)
Set the Next Header field value.
static constexpr uint8_t NH_MASK
Mask for NH flag extraction (bit 0).
uint32_t GetBlobLength() const
Get the blob length.
GHC ICMPv6 NHC Header - see RFC 7400 Section 5.
uint32_t GetBlobLength() const
Get the blob length.
SixLowPanDispatch::NhcDispatch_e GetNhcDispatchType() const
Get the NhcDispatch type.
uint32_t CopyBlob(uint8_t *blob, uint32_t size) const
Copy the GHC-compressed bytecode blob.
uint32_t Deserialize(Buffer::Iterator start) override
uint8_t m_blobLength
Length of GHC compressed ICMPv6 body.
void Print(std::ostream &os) const override
uint8_t m_blob[MAX_BLOB_SIZE+1]
GHC compressed bytecodes.
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
void SetBlob(const uint8_t *blob, uint32_t size)
Set the GHC-compressed bytecode blob for the ICMPv6 body.
static constexpr uint8_t GHC_ICMPV6_NHC
Fixed: 11011111.
void Serialize(Buffer::Iterator start) const override
static constexpr uint32_t MAX_BLOB_SIZE
Maximum blob size: the blob length field is 8 bits.
uint32_t GetSerializedSize() const override
static TypeId GetTypeId()
Get the type ID.
GHC UDP NHC Header - see RFC 7400 Section 5.
void SetSrcPort(uint16_t port)
Set the source port.
static constexpr uint8_t GHC_UDP_BASE
11010000
uint32_t Deserialize(Buffer::Iterator start) override
static constexpr uint8_t C_MASK
Checksum flag mask.
uint8_t m_baseFormat
NHC byte: 11010CPP.
uint16_t m_dstPort
Destination port.
void SetPorts(Ports_e port)
Set the port compression mode.
static constexpr uint8_t P_MASK
Port compression mask.
void Print(std::ostream &os) const override
TypeId GetInstanceTypeId() const override
Get the most derived TypeId for this Object.
uint16_t GetDstPort() const
Get the destination port.
void SetChecksum(uint16_t checksum)
Set the UDP checksum.
void SetDstPort(uint16_t port)
Set the destination port.
uint16_t GetChecksum() const
Get the UDP checksum.
static TypeId GetTypeId()
Get the type ID.
uint16_t m_checksum
UDP checksum.
void SetC(bool cField)
Set the checksum elision flag (C bit).
Ports_e
Port compression modes (same as RFC 6282 UDP NHC).
@ PORTS_LAST_SRC_LAST_DST
void Serialize(Buffer::Iterator start) const override
SixLowPanDispatch::NhcDispatch_e GetNhcDispatchType() const
Get the NhcDispatch type.
uint16_t m_srcPort
Source port.
Ports_e GetPorts() const
Get the port compression mode.
uint32_t GetSerializedSize() const override
bool GetC() const
Get the checksum elision flag (C bit).
uint16_t GetSrcPort() const
Get the source port.
a unique identifier for an interface.
TypeId SetParent(TypeId tid)
Set the parent TypeId.
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
GhcBytecodeType
GHC bytecode instruction types per RFC 7400 Section 3.
@ STOP_CODE
10010000: terminate decompression
@ RESERVED
Reserved/invalid pattern.
@ BACKREF
11nnnkkk: backreference copy
@ LITERAL
0kkkkkkk: copy k literal bytes
@ ZERO_INSERT
1000nnnn: insert (n+2) zero bytes
@ EXTENDED_ARGS
101nssss: extend backreference
Every class exported by the ns3 library is enclosed in the ns3 namespace.
std::ostream & operator<<(std::ostream &os, const Angles &a)