A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
sixlowpan-ghc.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 SRM Institute of Science and Technology
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Usham Roy <ushamroy80@gmail.com>
7 *
8 * 6LoWPAN-GHC: Generic Header Compression for IPv6 over
9 * Low-Power Wireless Personal Area Networks (6LoWPANs) - RFC 7400
10 *
11 * Source: original implementation following RFC 7400 (Bormann, November 2014).
12 */
13
14#ifndef SIXLOWPAN_GHC_H
15#define SIXLOWPAN_GHC_H
16
17#include "sixlowpan-header.h"
18
19#include "ns3/buffer.h"
20#include "ns3/header.h"
21#include "ns3/ipv6-address.h"
22
23#include <cstdint>
24#include <vector>
25
26namespace ns3
27{
28
29/**
30 * @ingroup sixlowpan
31 * @brief GHC bytecode instruction types per RFC 7400 Section 3.
32 *
33 * The decompressor processes a stream of bytecode instructions:
34 * - Literal: 0kkkkkkk - Copy k literal bytes from stream
35 * - ZeroInsert: 1000nnnn - Insert (n+2) zero bytes
36 * - StopCode: 10010000 - Terminate extension header decompression
37 * - ExtendedArgs: 101nssss - Extend next backreference range
38 * - Backref: 11nnnkkk - Copy n bytes from s bytes back
39 */
40enum class GhcBytecodeType : uint8_t
41{
42 LITERAL = 0, //!< 0kkkkkkk: copy k literal bytes
43 ZERO_INSERT, //!< 1000nnnn: insert (n+2) zero bytes
44 STOP_CODE, //!< 10010000: terminate decompression
45 EXTENDED_ARGS, //!< 101nssss: extend backreference
46 BACKREF, //!< 11nnnkkk: backreference copy
47 RESERVED //!< Reserved/invalid pattern
48};
49
50// ============================================================================
51// GHC Compression/Decompression Engine
52// ============================================================================
53
54/**
55 * @ingroup sixlowpan
56 * @brief Core GHC compression and decompression engine per RFC 7400.
57 *
58 * This class implements the LZ77-style bytecode compression defined in
59 * RFC 7400 Section 3. The engine operates on raw byte buffers and is
60 * used by the GHC header classes for actual data transformation.
61 *
62 * The decompression buffer is pre-initialized with a 48-byte dictionary:
63 * Bytes 0-15: Source IPv6 address from the packet
64 * Bytes 16-31: Destination IPv6 address from the packet
65 * Bytes 32-47: Static dictionary (16 bytes from RFC 7400 Section 3.1)
66 *
67 * The static dictionary contains:
68 * 0x16 0xfe 0xfd 0x17 0xfe 0xfd 0x00 0x01
69 * 0x00 0x00 0x00 0x00 0x00 0x01 0x00 0x00
70 */
72{
73 public:
74 /**
75 * @brief Predefined dictionary size (48 bytes).
76 *
77 * 16 bytes source address + 16 bytes destination address + 16 bytes static.
78 */
79 static constexpr uint32_t DICTIONARY_SIZE = 48;
80
81 /**
82 * @brief Maximum bytes copied by one 0kkkkkkk literal bytecode.
83 *
84 * k is in [1, 95]; values 96-127 are reserved (RFC 7400 Section 3).
85 */
86 static constexpr uint32_t MAX_LITERAL_RUN = 95;
87
88 /**
89 * @brief Maximum zeros inserted by one 1000nnnn bytecode: n+2 with n <= 15.
90 */
91 static constexpr uint32_t MAX_ZERO_RUN = 17;
92
93 /**
94 * @brief Maximum copy length of one 11nnnkkk backreference.
95 *
96 * n = nnn (max 7) + na (max 8 from one extended-args byte) + 2 = 17.
97 * Longer repeats become consecutive backreferences.
98 */
99 static constexpr uint32_t MAX_BACKREF_LEN = 17;
100
101 /**
102 * @brief Decompress a GHC-compressed byte stream.
103 *
104 * @param [in] srcAddr Source IPv6 address for dictionary initialization.
105 * @param [in] dstAddr Destination IPv6 address for dictionary initialization.
106 * @param [in] compressed Pointer to compressed bytecode data.
107 * @param [in] compressedLen Length of compressed data.
108 * @param [out] output Buffer to receive decompressed output.
109 * @param [in] outputMaxLen Maximum output buffer size.
110 * @param [in] useStopCode If true, decompression terminates on Stop Code
111 * (used for extension headers where Length is elided).
112 * @return Number of decompressed bytes, or 0 on error.
113 */
114 static uint32_t Decompress(const Ipv6Address& srcAddr,
115 const Ipv6Address& dstAddr,
116 const uint8_t* compressed,
117 uint32_t compressedLen,
118 uint8_t* output,
119 uint32_t outputMaxLen,
120 bool useStopCode = false);
121
122 /**
123 * @brief Compress a byte stream using the GHC LZ77 algorithm.
124 *
125 * The compressor scans the dictionary and previously output data for
126 * the longest match, then emits backreference bytecodes. This is a
127 * full LZ77 compressor implementation.
128 *
129 * @param [in] srcAddr Source IPv6 address for dictionary initialization.
130 * @param [in] dstAddr Destination IPv6 address for dictionary initialization.
131 * @param [in] input Pointer to uncompressed data.
132 * @param [in] inputLen Length of uncompressed data.
133 * @param [out] output Buffer to receive compressed bytecodes.
134 * @param [in] outputMaxLen Maximum output buffer size.
135 * @param [in] emitStopCode If true, append Stop Code at end (for extension headers).
136 * @return Number of compressed bytes, or 0 on error/no compression benefit.
137 */
138 static uint32_t Compress(const Ipv6Address& srcAddr,
139 const Ipv6Address& dstAddr,
140 const uint8_t* input,
141 uint32_t inputLen,
142 uint8_t* output,
143 uint32_t outputMaxLen,
144 bool emitStopCode = false);
145
146 /**
147 * @brief Classify a GHC bytecode instruction.
148 *
149 * @param [in] byte The bytecode byte to classify.
150 * @return The bytecode instruction type.
151 */
152 static GhcBytecodeType ClassifyBytecode(uint8_t byte);
153
154 private:
155 /**
156 * @brief Initialize the 48-byte decompression dictionary.
157 *
158 * @param [out] dict Buffer to receive dictionary (must be >= 48 bytes).
159 * @param [in] srcAddr Source IPv6 address.
160 * @param [in] dstAddr Destination IPv6 address.
161 */
162 static void InitDictionary(uint8_t* dict,
163 const Ipv6Address& srcAddr,
164 const Ipv6Address& dstAddr);
165
166 /**
167 * @brief Count the extended-args bytes a backreference encoding needs.
168 *
169 * Each 101nssss extended-args byte carries up to 15*8 = 120 of the
170 * start-adjust and 8 of the number-adjust accumulator (RFC 7400
171 * Section 3.2).
172 *
173 * @param [in] matchLength Number of bytes to copy (>= 2).
174 * @param [in] matchOffset Offset from current position (>= matchLength).
175 * @return Number of 101nssss bytes needed before the 11nnnkkk bytecode.
176 */
177 static uint32_t CountBackrefExtArgBytes(uint32_t matchLength, uint32_t matchOffset);
178
179 /**
180 * @brief Find the longest match in the buffer for LZ77 compression.
181 *
182 * Searches backward from the current position for the longest
183 * substring match, respecting GHC backreference encoding limits.
184 *
185 * @param [in] buffer Full buffer (dictionary + output so far).
186 * @param [in] bufLen Current buffer length.
187 * @param [in] input Input data at current position.
188 * @param [in] inputRemaining Remaining input bytes.
189 * @param [out] matchOffset Offset of best match (bytes back from bufLen).
190 * @param [out] matchLength Length of best match.
191 * @return true if a useful match was found (length >= 2).
192 */
193 static bool FindLongestMatch(const uint8_t* buffer,
194 uint32_t bufLen,
195 const uint8_t* input,
196 uint32_t inputRemaining,
197 uint32_t& matchOffset,
198 uint32_t& matchLength);
199
200 /**
201 * @brief Emit a backreference bytecode (with optional extended args).
202 *
203 * @param [out] output Output bytecode buffer.
204 * @param [in,out] outPos Current output position (advanced on write).
205 * @param [in] outputMaxLen Maximum output buffer size.
206 * @param [in] matchLength Number of bytes to copy (>= 2).
207 * @param [in] matchOffset Offset from current position (>= matchLength).
208 * @return true on success, false if output buffer would overflow.
209 */
210 static bool EmitBackref(uint8_t* output,
211 uint32_t& outPos,
212 uint32_t outputMaxLen,
213 uint32_t matchLength,
214 uint32_t matchOffset);
215
216 /**
217 * @brief Count consecutive zero bytes in input.
218 *
219 * @param [in] input Pointer to current input position.
220 * @param [in] remaining Remaining bytes in input.
221 * @return Number of consecutive zero bytes (0 to remaining).
222 */
223 static uint32_t CountZeros(const uint8_t* input, uint32_t remaining);
224};
225
226// ============================================================================
227// GHC NHC Header Classes
228// ============================================================================
229
230/**
231 * @ingroup sixlowpan
232 * @brief GHC NHC Extension Header - see RFC 7400 Section 5.
233 *
234 * Replaces the RFC 6282 extension header NHC byte pattern (1110EID[NH])
235 * with the GHC pattern (1011EID[NH]).
236 *
237 * @verbatim
238 * 0 1 2 3 4 5 6 7
239 * +---+---+---+---+---+---+---+---+
240 * | 1 | 0 | 1 | 1 | EID |NH |
241 * +---+---+---+---+---+---+---+---+
242 * @endverbatim
243 *
244 * The compressed payload following this header contains GHC bytecodes.
245 * Decompression terminates upon encountering the Stop Code (0x90).
246 * The Length field of the original extension header is elided.
247 */
249{
250 public:
251 /**
252 * @brief Extension Header ID (same encoding as RFC 6282).
253 */
263
265
266 /**
267 * @brief Get the type ID.
268 * @return The object TypeId.
269 */
270 static TypeId GetTypeId();
271
272 TypeId GetInstanceTypeId() const override;
273 void Print(std::ostream& os) const override;
274 uint32_t GetSerializedSize() const override;
275 void Serialize(Buffer::Iterator start) const override;
276 uint32_t Deserialize(Buffer::Iterator start) override;
277
278 /**
279 * @brief Get the NhcDispatch type.
280 * @return The NhcDispatch type (LOWPAN_GHC_EXT).
281 */
283
284 /**
285 * @brief Set the Extension Header Type.
286 * @param [in] eid The Extension Header Type.
287 */
288 void SetEid(Eid_e eid);
289
290 /**
291 * @brief Get the Extension Header Type.
292 * @return The Extension Header Type.
293 */
294 Eid_e GetEid() const;
295
296 /**
297 * @brief Set the Next Header field value.
298 * @param [in] nextHeader The Next Header value.
299 */
300 void SetNextHeader(uint8_t nextHeader);
301
302 /**
303 * @brief Get the Next Header field value.
304 * @return The Next Header value.
305 */
306 uint8_t GetNextHeader() const;
307
308 /**
309 * @brief Set the NH field (next header compressed flag).
310 * @param [in] nhField True if next header is compressed via NHC/GHC.
311 */
312 void SetNh(bool nhField);
313
314 /**
315 * @brief Get the NH field.
316 * @return True if next header is compressed.
317 */
318 bool GetNh() const;
319
320 /**
321 * @brief Maximum blob size: the blob length field is 8 bits.
322 */
323 static constexpr uint32_t MAX_BLOB_SIZE = 255;
324
325 /**
326 * @brief Set the GHC-compressed bytecode blob.
327 * @param [in] blob Buffer holding compressed bytecodes.
328 * @param [in] size Blob length.
329 */
330 void SetBlob(const uint8_t* blob, uint32_t size);
331
332 /**
333 * @brief Copy the GHC-compressed bytecode blob.
334 * @param [out] blob Buffer to copy blob into.
335 * @param [in] size Buffer size.
336 * @return Bytes copied.
337 */
338 uint32_t CopyBlob(uint8_t* blob, uint32_t size) const;
339
340 /**
341 * @brief Get the blob length.
342 * @return Length of the compressed bytecode blob.
343 */
344 uint32_t GetBlobLength() const;
345
346 private:
347 uint8_t m_nhcByte; //!< NHC header byte: 1011EID[NH]
348 uint8_t m_nextHeader; //!< Next Header value (if NH=0)
349 uint8_t m_blobLength; //!< Length of GHC compressed data
350 uint8_t m_blob[MAX_BLOB_SIZE + 1]; //!< GHC compressed bytecodes
351
352 /**
353 * @brief Base NHC byte pattern for GHC extension: 10110000 = 0xB0.
354 */
355 static constexpr uint8_t GHC_EXT_BASE = 0xB0;
356
357 /**
358 * @brief Mask for EID field extraction (bits 1-3).
359 */
360 static constexpr uint8_t EID_MASK = 0x0E;
361
362 /**
363 * @brief Mask for NH flag extraction (bit 0).
364 */
365 static constexpr uint8_t NH_MASK = 0x01;
366};
367
368/**
369 * @brief Stream insertion operator.
370 * @param [in,out] os The reference to the output stream.
371 * @param [in] header The SixLowPanGhcExtension header.
372 * @return The reference to the output stream.
373 */
374std::ostream& operator<<(std::ostream& os, const SixLowPanGhcExtension& header);
375
376// ============================================================================
377
378/**
379 * @ingroup sixlowpan
380 * @brief GHC UDP NHC Header - see RFC 7400 Section 5.
381 *
382 * Replaces the RFC 6282 UDP NHC byte pattern (11110CPP)
383 * with the GHC pattern (11010CPP). Port compression uses
384 * the same encoding as RFC 6282. GHC bytecodes compress
385 * the UDP payload following the header.
386 *
387 * @verbatim
388 * 0 1 2 3 4 5 6 7
389 * +---+---+---+---+---+---+---+---+
390 * | 1 | 1 | 0 | 1 | 0 | C | P |
391 * +---+---+---+---+---+---+---+---+
392 * @endverbatim
393 */
395{
396 public:
397 /**
398 * @brief Port compression modes (same as RFC 6282 UDP NHC).
399 */
407
409
410 /**
411 * @brief Get the type ID.
412 * @return The object TypeId.
413 */
414 static TypeId GetTypeId();
415
416 TypeId GetInstanceTypeId() const override;
417 void Print(std::ostream& os) const override;
418 uint32_t GetSerializedSize() const override;
419 void Serialize(Buffer::Iterator start) const override;
420 uint32_t Deserialize(Buffer::Iterator start) override;
421
422 /**
423 * @brief Get the NhcDispatch type.
424 * @return The NhcDispatch type (LOWPAN_GHC_UDP).
425 */
427
428 /**
429 * @brief Set the port compression mode.
430 * @param [in] port The port compression mode.
431 */
432 void SetPorts(Ports_e port);
433
434 /**
435 * @brief Get the port compression mode.
436 * @return The port compression mode.
437 */
438 Ports_e GetPorts() const;
439
440 /**
441 * @brief Set the source port.
442 * @param [in] port The source port.
443 */
444 void SetSrcPort(uint16_t port);
445
446 /**
447 * @brief Get the source port.
448 * @return The source port.
449 */
450 uint16_t GetSrcPort() const;
451
452 /**
453 * @brief Set the destination port.
454 * @param [in] port The destination port.
455 */
456 void SetDstPort(uint16_t port);
457
458 /**
459 * @brief Get the destination port.
460 * @return The destination port.
461 */
462 uint16_t GetDstPort() const;
463
464 /**
465 * @brief Set the checksum elision flag (C bit).
466 * @param [in] cField True if checksum is elided.
467 */
468 void SetC(bool cField);
469
470 /**
471 * @brief Get the checksum elision flag (C bit).
472 * @return True if checksum is elided.
473 */
474 bool GetC() const;
475
476 /**
477 * @brief Set the UDP checksum.
478 * @param [in] checksum The UDP checksum value.
479 */
480 void SetChecksum(uint16_t checksum);
481
482 /**
483 * @brief Get the UDP checksum.
484 * @return The UDP checksum value.
485 */
486 uint16_t GetChecksum() const;
487
488 private:
489 uint8_t m_baseFormat; //!< NHC byte: 11010CPP
490 uint16_t m_checksum; //!< UDP checksum
491 uint16_t m_srcPort; //!< Source port
492 uint16_t m_dstPort; //!< Destination port
493
494 static constexpr uint8_t GHC_UDP_BASE = 0xD0; //!< 11010000
495 static constexpr uint8_t C_MASK = 0x04; //!< Checksum flag mask
496 static constexpr uint8_t P_MASK = 0x03; //!< Port compression mask
497};
498
499/**
500 * @brief Stream insertion operator.
501 * @param [in,out] os The reference to the output stream.
502 * @param [in] header The SixLowPanGhcUdp header.
503 * @return The reference to the output stream.
504 */
505std::ostream& operator<<(std::ostream& os, const SixLowPanGhcUdp& header);
506
507// ============================================================================
508
509/**
510 * @ingroup sixlowpan
511 * @brief GHC ICMPv6 NHC Header - see RFC 7400 Section 5.
512 *
513 * Fixed NHC byte value 0xDF (11011111). This is the key GHC addition
514 * that fills the gap left by RFC 6282 which defines no ICMPv6 NHC.
515 * The entire ICMPv6 message body is compressed using GHC bytecodes.
516 *
517 * @verbatim
518 * 0 1 2 3 4 5 6 7
519 * +---+---+---+---+---+---+---+---+
520 * | 1 | 1 | 0 | 1 | 1 | 1 | 1 | 1 |
521 * +---+---+---+---+---+---+---+---+
522 * @endverbatim
523 *
524 * This header enables compression of:
525 * - RPL control messages (DIO, DAO, DIS, DAO-ACK)
526 * - Neighbor Discovery messages (NS, NA, RS, RA)
527 * - Echo Request/Reply
528 * - Any ICMPv6 message
529 */
531{
532 public:
534
535 /**
536 * @brief Get the type ID.
537 * @return The object TypeId.
538 */
539 static TypeId GetTypeId();
540
541 TypeId GetInstanceTypeId() const override;
542 void Print(std::ostream& os) const override;
543 uint32_t GetSerializedSize() const override;
544 void Serialize(Buffer::Iterator start) const override;
545 uint32_t Deserialize(Buffer::Iterator start) override;
546
547 /**
548 * @brief Deserialize the header from the given buffer region.
549 *
550 * The GHC ICMPv6 wire format has no length field: the bytecode blob
551 * extends to the end of the packet (RFC 7400 Section 3). This
552 * variable-size variant must be used, through
553 * Packet::RemoveHeader(header, size).
554 *
555 * @param [in] start Start of the header.
556 * @param [in] end End of the blob (end of the packet).
557 * @return Bytes consumed.
558 */
560
561 /**
562 * @brief Get the NhcDispatch type.
563 * @return The NhcDispatch type (LOWPAN_GHC_ICMPV6).
564 */
566
567 /**
568 * @brief Maximum blob size: the blob length field is 8 bits.
569 */
570 static constexpr uint32_t MAX_BLOB_SIZE = 255;
571
572 /**
573 * @brief Set the GHC-compressed bytecode blob for the ICMPv6 body.
574 * @param [in] blob Buffer holding compressed bytecodes.
575 * @param [in] size Blob length.
576 */
577 void SetBlob(const uint8_t* blob, uint32_t size);
578
579 /**
580 * @brief Copy the GHC-compressed bytecode blob.
581 * @param [out] blob Buffer to copy blob into.
582 * @param [in] size Buffer size.
583 * @return Bytes copied.
584 */
585 uint32_t CopyBlob(uint8_t* blob, uint32_t size) const;
586
587 /**
588 * @brief Get the blob length.
589 * @return Compressed bytecode length.
590 */
591 uint32_t GetBlobLength() const;
592
593 private:
594 static constexpr uint8_t GHC_ICMPV6_NHC = 0xDF; //!< Fixed: 11011111
595 uint8_t m_blobLength; //!< Length of GHC compressed ICMPv6 body
596 uint8_t m_blob[MAX_BLOB_SIZE + 1]; //!< GHC compressed bytecodes
597};
598
599/**
600 * @brief Stream insertion operator.
601 * @param [in,out] os The reference to the output stream.
602 * @param [in] header The SixLowPanGhcIcmpv6 header.
603 * @return The reference to the output stream.
604 */
605std::ostream& operator<<(std::ostream& os, const SixLowPanGhcIcmpv6& header);
606
607// ============================================================================
608// 6CIO - 6LoWPAN Capability Indication Option
609// ============================================================================
610
611/**
612 * @ingroup sixlowpan
613 * @brief 6LoWPAN Capability Indication Option (6CIO) - RFC 7400 Section 6.
614 *
615 * Carried in Neighbor Discovery messages (RS, RA, NS, NA) to advertise
616 * GHC capability. The G bit (bit 15 of the flags field) indicates
617 * that the node supports GHC compression.
618 *
619 * @verbatim
620 * +--------+--------+--------+--------+--------+--------+
621 * | Type=36| Length | Flags (variable, 16+ bits) |
622 * +--------+--------+--------+--------+--------+--------+
623 *
624 * Flags[15] = G bit: GHC Capable (1 = supports GHC)
625 * @endverbatim
626 */
627class SixLowPan6Cio : public Header
628{
629 public:
631
632 /**
633 * @brief Get the type ID.
634 * @return The object TypeId.
635 */
636 static TypeId GetTypeId();
637 TypeId GetInstanceTypeId() const override;
638 void Print(std::ostream& os) const override;
639 uint32_t GetSerializedSize() const override;
640 void Serialize(Buffer::Iterator start) const override;
641 uint32_t Deserialize(Buffer::Iterator start) override;
642
643 /**
644 * @brief Set the GHC capability flag.
645 * @param [in] ghcCapable True if this node supports GHC.
646 */
647 void SetGhcCapable(bool ghcCapable);
648
649 /**
650 * @brief Get the GHC capability flag.
651 * @return True if GHC is supported.
652 */
653 bool GetGhcCapable() const;
654
655 private:
656 static constexpr uint8_t SIXLOWPAN_6CIO_TYPE = 36; //!< ND Option Type
657 static constexpr uint8_t SIXLOWPAN_6CIO_LEN = 1; //!< Length in 8-octet units
658 static constexpr uint32_t GHC_FLAG_BIT = (1u << 15); //!< G bit position
659
660 uint32_t m_flags; //!< Flags field (variable, up to 48 bits per RFC)
661};
662
663/**
664 * @brief Stream insertion operator.
665 * @param [in,out] os The reference to the output stream.
666 * @param [in] header The SixLowPan6Cio header.
667 * @return The reference to the output stream.
668 */
669std::ostream& operator<<(std::ostream& os, const SixLowPan6Cio& header);
670
671} // namespace ns3
672
673#endif /* SIXLOWPAN_GHC_H */
iterator in a Buffer instance
Definition buffer.h:109
Protocol header serialization and deserialization.
Definition header.h:36
Describes an IPv6 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.
Core GHC compression and decompression engine per RFC 7400.
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).
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.
Definition type-id.h:50
uint16_t port
Definition dsdv-manet.cc:33
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)
Definition angles.cc:148