Debian Codegraph @

protolib/include/protoPktIP.h

raw
1 #ifndef _PROTO_PKT_IP
2 #define _PROTO_PKT_IP
3
4 #include "protoPkt.h"
5 #include "protoAddress.h"
6 #include "protoDebug.h"
7
8 #include <string.h> // for memcpy()
9
10 /**
11 * @class ProtoPktIP
12 *
13 * @brief These classes provide representations of IP packet formats
14 * for IPv4 and IPv6 packets:
15 *
16 * ProtoPktIP - base class with access to/control of "version" field
17 * ProtoPktIPv4 - class for parsing/building IPv4 packets into buffer
18 * ProtoPktIPv6 - class for parsing/building IPv6 packets into buffer
19 * ProtoPktUDP - class for parsing/building UDP datagrams
20 * (hey, we needed to test ProtoPktIP with something)
21 */
22 class ProtoPktIP : public ProtoPkt
23 {
24 public:
25 ProtoPktIP(void* bufferPtr = NULL,
26 unsigned int bufferBytes = 0,
27 bool freeOnDestruct = false);
28 ~ProtoPktIP();
29
30
31 bool InitFromBuffer(UINT16 pktLength,
32 void* bufferPtr = NULL,
33 unsigned int bufferBytes = 0,
34 bool freeOnDestruct = false)
35 {
36 bool result = ProtoPkt::InitFromBuffer(pktLength, bufferPtr, bufferBytes, freeOnDestruct);
37 result &= (pktLength > OFFSET_VERSION);
38 return result;
39 }
40
41 UINT8 GetVersion() const
42 {return ((ProtoPkt::GetLength() > OFFSET_VERSION) ?
43 (GetUINT8(OFFSET_VERSION) >> 4) : 0);}
44
45 unsigned int GetHeaderLength();
46
47 void SetVersion(UINT8 version)
48 {
49 UINT8& byte = AccessUINT8(OFFSET_VERSION);
50 byte &= 0x0f;
51 byte |= (version << 4);
52 }
53
54 bool SetDstAddr(ProtoAddress& dst);
55 bool GetDstAddr(ProtoAddress& dst);
56 bool SetSrcAddr(ProtoAddress& src);
57 bool GetSrcAddr(ProtoAddress& src);
58
59 // This is a partial list at the moment
60 enum Protocol
61 {
62 HOPOPT = 0, // IPv6 hop-by-hop option
63 ICMP = 1, // Internet Control Message Protocol
64 IGMP = 2, // Internet Group Management Protocol
65 IPIP = 4, // IPv4 in IPv4 encapsulation
66 TCP = 6, // Transmission Control Protocol
67 UDP = 17, // User Datagram Protocol
68 IPV6 = 41, // Used for tunneling IPv6 packets over IPv4 or IPv6
69 RTG = 43, // IPv6 routing header
70 FRAG = 44, // IPv6 fragment header
71 GRE = 47, // Generic Router Encapsulation
72 ESP = 50, // Encapsulation security payload header
73 AUTH = 51, // authentication/ESP header
74 MOBILE = 55, // IP Moobility (Min Encap)
75 ICMPv6 = 58, // ICMP for IPv6
76 MLD = 58, // IPv6 Multicast Listener Discovery
77 NONE = 59, // IPPROTO_NONE
78 DSTOPT = 60, // IPv6 destination options header
79 OSPF = 89, // OSPF routing protocol
80 MOBILITY = 135, // IPv6 mobility extension header
81 EXP1 = 253, // for experimental use
82 EXP2 = 254, // for experimental use
83 RESERVED = 255
84 };
85
86 static bool IsExtension(Protocol p)
87 {
88 switch (p)
89 {
90 case HOPOPT: // hop-by-hop options
91 case DSTOPT: // destination options
92 case RTG: // routing header
93 case FRAG: // fragment header
94 case AUTH: // IPSec AH header
95 return true;
96 default:
97 return false;
98 }
99 }
100
101 protected:
102 enum {OFFSET_VERSION = 0}; // 1/2 byte, most sig nybble
103
104 }; // end class ProtoPktIP
105
106 /**
107 * @class ProtoPktIPv4
108 *
109 * @brief Parses IPv4 Packets
110 */
111
112 class ProtoPktIPv4 : public ProtoPktIP
113 {
114 public:
115 ProtoPktIPv4(void* bufferPtr = NULL,
116 unsigned int numBytes = 0,
117 bool initFromBuffer = false,
118 bool freeOnDestruct = false);
119 ProtoPktIPv4(ProtoPktIP& ipPkt);
120 ~ProtoPktIPv4();
121
122 enum {ADDR_LEN = 4};
123
124 enum Flag
125 {
126 FLAG_NONE = 0x00,
127 FLAG_RESERVED = 0x80, // reserved bit
128 FLAG_DF = 0x40, // 0 = may fragment, 1 = don't fragment
129 FLAG_MF = 0x20 // 0 = last fragment, 1 = more fragments
130 };
131
132 /**
133 * @class Option
134 * @brief ProtoPktIPv4 Option Base class
135 */
136 class Option : public ProtoPkt
137 {
138 public:
139 enum Type
140 {
141 EOOL = 0, // End of Options List [RFC791,JBP]
142 NOP = 1, // No Operation [RFC791,JBP]
143 SEC = 130, // Security [RFC1108]
144 LSR = 131, // Loose Source Route [RFC791,JBP]
145 TS = 68, // Time Stamp [RFC791,JBP]
146 ESEC = 133, // Extended Security [RFC1108]
147 CIPSO = 134, // Commercial Security [???]
148 RR = 7, // Record Route [RFC791,JBP]
149 SID = 136, // Stream ID [RFC791,JBP]
150 SSR = 137, // Strict Source Route [RFC791,JBP]
151 ZSU = 10, // Experimental Measurement [ZSu]
152 MTUP = 11, // MTU Probe [RFC1191]*
153 MTUR = 12, // MTU Reply [RFC1191]*
154 FINN = 205, // Experimental Flow Control [Finn]
155 VISA = 142, // Expermental Access Control [Estrin]
156 ENCODE = 15, // ??? [VerSteeg]
157 IMITD = 144, // IMI Traffic Descriptor [Lee]
158 EIP = 145, // Extended Internet Protocol[RFC1385]
159 TR = 82, // Traceroute [RFC1393]
160 ADDEXT = 147, // Address Extension [Ullmann IPv7]
161 RTRALT = 148, // Router Alert [RFC2113]
162 SDB = 149, // Selective Directed Broadcast[Graff]
163 XXX = 150, // Unassigned (Released 18 October 2005)
164 DPS = 151, // Dynamic Packet State [Malis]
165 UMP = 152, // Upstream Multicast Pkt. [Farinacci]
166 QS = 25, // Quick//Start [RFC4782]
167 EXP1 = 30, // RFC3692//style Experiment (**) [RFC4727]
168 EXP2 = 94, // RFC3692//style Experiment (**) [RFC4727]
169 EXP3 = 158, // RFC3692//style Experiment (**) [RFC4727]
170 EXP4 = 222 // RFC3692//style Experiment (**) [RFC4727]
171 };
172
173 Option(void* bufferPtr = NULL,
174 unsigned int numBytes = 0,
175 bool initFromBuffer = true,
176 bool freeOnDestruct = false);
177 ~Option();
178
179 // Use these to build an option
180 bool InitIntoBuffer(Type type,
181 void* bufferPtr = NULL,
182 unsigned int numBytes = 0,
183 bool freeOnDestruct = false);
184
185 bool SetData(const char* data, unsigned int length);
186
187
188 // Use these to parse
189 bool InitFromBuffer(void* bufferPtr = NULL,
190 unsigned int numBytes = 0,
191 bool freeOnDestruct = false);
192
193 Type GetType() const
194 {return (Type)GetUINT8(OFFSET_TYPE);}
195
196 const char* GetData() const
197 {return (const char*)GetBuffer(OffsetData());}
198
199 unsigned int GetDataLength() const
200 {return (GetLength() - ((OffsetData() != OFFSET_LENGTH) ? 2 : 1));}
201
202 // Per RFC4302 spec that cites related specs
203 bool IsMutable() const
204 {return IsMutable(GetType());}
205
206 static bool IsMutable(Type type);
207
208 /**
209 *
210 * @class Iterator
211 * @brief Iterator class
212 */
213 class Iterator
214 {
215 public:
216 Iterator(const ProtoPktIPv4& ip4Pkt);
217 ~Iterator();
218 void Reset() {offset = 20;}
219 bool GetNextOption(Option& option);
220
221 private:
222 const void* pkt_buffer;
223 unsigned int offset;
224 unsigned int offset_end;
225 }; // end class ProtoPktIPv4::Option::Iterator
226
227 protected:
228 static int GetLengthByType(Type type);
229
230 enum
231 {
232 LENGTH_VARIABLE = 0,
233 LENGTH_UNKNOWN = -1
234 };
235 enum
236 {
237 OFFSET_TYPE = 0, // UINT8 offset
238 OFFSET_LENGTH = OFFSET_TYPE + 1 // UINT8 offset
239 };
240
241 unsigned int OffsetData() const
242 {return ((LENGTH_VARIABLE == GetLengthByType(GetType())) ? 2 : 1);}
243
244 }; // end class ProtoPktIPv4::Option
245
246
247 /// Use these to parse a packet
248 bool InitFromBuffer(void* bufferPtr = NULL,
249 unsigned int numBytes = 0,
250 bool freeOnDestruct = false);
251
252 UINT8 GetHeaderLength() const // in bytes
253 {return ((GetUINT8(OFFSET_HDR_LEN) & 0x0f) << 2); }
254 UINT8 GetTOS() const
255 {return GetUINT8(OFFSET_TOS);}
256 UINT16 GetTotalLength() const
257 {return GetWord16(OFFSET_LEN);}
258 UINT16 GetID() const
259 {return GetWord16(OFFSET_ID);}
260 bool FlagIsSet(Flag flag) const
261 {return (0 != (flag & GetUINT8(OFFSET_FLAGS)));}
262 bool HasOptions() const
263 {return (GetHeaderLength() > 20);}
264 UINT16 GetFragmentOffset() const
265 {return (0x1fff & GetWord16(OFFSET_FRAGMENT));}
266 UINT8 GetTTL() const
267 {return GetUINT8(OFFSET_TTL);}
268 Protocol GetProtocol() const
269 {return (Protocol)GetUINT8(OFFSET_PROTOCOL);}
270 UINT16 GetChecksum() const
271 {return GetWord16(OFFSET_CHECKSUM);}
272 void GetSrcAddr(ProtoAddress& addr) const
273 {addr.SetRawHostAddress(ProtoAddress::IPv4, (char*)GetBuffer32(OFFSET_SRC_ADDR), 4);}
274 void GetDstAddr(ProtoAddress& addr) const
275 {addr.SetRawHostAddress(ProtoAddress::IPv4, (char*)GetBuffer32(OFFSET_DST_ADDR), 4);}
276
277 /// Helper method to get pointer to ID portion of IPv4 header
278 const char* GetIDPtr() const
279 {return ((char*)GetBuffer16(OFFSET_ID));}
280 /// Helper methods for UDP checksum calculation, etc
281 const void* GetSrcAddrPtr() const
282 {return GetBuffer32(OFFSET_SRC_ADDR);}
283 const void* GetDstAddrPtr() const
284 {return GetBuffer32(OFFSET_DST_ADDR);}
285
286 // (TBD) provide methods to get any header extensions
287
288 UINT16 GetPayloadLength() const {return (GetTotalLength() - GetHeaderLength());}
289 const void* GetPayload() const
290 {return ((void*)GetBuffer32(GetUINT8(OFFSET_HDR_LEN) & 0x0f));}
291 void* AccessPayload()
292 {return ((void*)AccessBuffer32(GetUINT8(OFFSET_HDR_LEN) & 0x0f));}
293
294 /// Use these to build a packet
295 bool InitIntoBuffer(void* bufferPtr = NULL,
296 unsigned int bufferBytes = 0,
297 bool freeOnDestruct = false);
298 /// (TBD) modify "Set" methods to optionally update checksum
299 void SetHeaderLength(UINT8 hdrBytes)
300 {
301 UINT8& byte = AccessUINT8(OFFSET_HDR_LEN);
302 byte &= 0xf0;
303 byte |= (hdrBytes >> 2);
304 SetTotalLength(hdrBytes);
305 }
306 void SetTotalLength(UINT16 numBytes)
307 {
308 SetWord16(OFFSET_LEN, numBytes);
309 ProtoPkt::SetLength(numBytes);
310 }
311 void SetTOS(UINT8 tos, bool updateChecksum = false);
312 void SetID(UINT16 id, bool updateChecksum = false) ;
313 void SetFlag(Flag flag, bool updateChecksum = false);
314 void ClearFlag(Flag flag, bool updateChecksum = false);
315 void SetFragmentOffset(UINT16 fragmentOffset, bool updateChecksum = false);
316 void SetTTL(UINT8 ttl, bool updateChecksum = false) ;
317 void SetProtocol(Protocol protocol, bool updateChecksum = false);
318 void SetChecksum(UINT16 checksum)
319 {SetWord16(OFFSET_CHECKSUM, checksum);}
320 void SetSrcAddr(const ProtoAddress& addr, bool calculateChecksum = false);
321 void SetDstAddr(const ProtoAddress& addr, bool calculateChecksum = false);
322 /// (TBD) support header extensions for IPv4
323 void SetPayloadLength(UINT16 numBytes, bool calculateChecksum = true);
324 void SetPayload(const char* payload, UINT16 numBytes, bool calculateChecksum = true)
325 {
326 memcpy(AccessPayload(), payload, numBytes);
327 SetPayloadLength(numBytes, calculateChecksum);
328 }
329
330 /** updates IPv4 header checksum when an aligned
331 * (16-bit portion is changed (e.g. packet id)
332 */
333 void UpdateChecksum(UINT16 oldVal, UINT16 newVal)
334 {
335 UINT16 oldSum = GetChecksum();
336 UINT32 sum = oldSum + oldVal + (~newVal & 0xffff);
337 UINT16 newSum = (UINT16)(sum + (sum >> 16));
338 SetChecksum(newSum);
339 }
340
341 /// updates IPv4 header checksum when a byte is changed.
342 void UpdateChecksum(UINT8 oldByte, UINT8 newByte, bool oddOffset)
343 {
344 UINT16 oldVal = (UINT16)oldByte;
345 UINT16 newVal = (UINT16)newByte;
346 if (!oddOffset)
347 {
348 oldVal <<= 8;
349 newVal <<= 8;
350 }
351 UINT16 oldSum = GetChecksum();
352 UINT32 sum = oldSum + oldVal + (~newVal & 0xffff);
353 UINT16 newSum = (UINT16)(sum + (sum >> 16));
354 SetChecksum(newSum);
355 }
356 /// Helper methods for common packet manipulation
357 UINT8 DecrementTTL()
358 {
359 UINT8 ttl = GetTTL();
360 if (ttl > 0)
361 {
362 SetTTL(ttl-1);
363 UINT16 oldSum = GetChecksum();
364 UINT32 sum = oldSum + 0x0100;
365 UINT16 newSum = (UINT16)(sum + (sum >> 16));
366 SetChecksum(newSum);
367 }
368 return ttl;
369 }
370
371 // Return checksum in host byte order
372 UINT16 CalculateChecksum(bool set = true);
373 UINT16 FinalizeChecksum()
374 {return CalculateChecksum(true);}
375
376 private:
377 enum
378 {
379 OFFSET_HDR_LEN = OFFSET_VERSION, // 0.5 bytes (masked)
380 OFFSET_TOS = OFFSET_HDR_LEN + 1, // 1 bytes
381 OFFSET_LEN = (OFFSET_TOS+1)/2, // 1 UINT16 (2 bytes)
382 OFFSET_ID = OFFSET_LEN+1, // 2 UINT16 (4 bytes)
383 OFFSET_FLAGS = (OFFSET_ID+1)*2, // 3 bits
384 OFFSET_FRAGMENT = OFFSET_FLAGS/2, // 13 bits (masked)
385 OFFSET_TTL = (OFFSET_FRAGMENT+1)*2, // 8 bytes
386 OFFSET_PROTOCOL = OFFSET_TTL+1, // 9 bytes
387 OFFSET_CHECKSUM = (OFFSET_PROTOCOL+1)/2, // 5 UINT16 (10 bytes)
388 OFFSET_SRC_ADDR = ((OFFSET_CHECKSUM+1)*2)/4, // 3 UINT32 (12 bytes)
389 OFFSET_DST_ADDR = OFFSET_SRC_ADDR+1, // 4 UINT32 (16 bytes)
390 OFFSET_OPTIONS = (OFFSET_DST_ADDR+1)*4 // 20 bytes (UINT8 offset)
391 };
392 }; // end class ProtoPktIPv4
393
394
395 // Modified version of the IPv4 "Upstream Multicast Packet" option (type 152)
396 // The modification here is use of the 16-bit RESERVED field as a sequence
397 // number for downstream packet loss detection. This is used by the
398 // NRL experimental Elastic Multicast protocol
399 class ProtoPktUMP : public ProtoPktIPv4::Option
400 {
401 public:
402 ProtoPktUMP(void* bufferPtr = NULL,
403 unsigned int numBytes = 0,
404 bool initFromBuffer = true,
405 bool freeOnDestruct = false)
406 {
407 if (NULL != bufferPtr)
408 {
409 if (initFromBuffer)
410 InitFromBuffer(bufferPtr, numBytes, freeOnDestruct);
411 else
412 InitIntoBuffer(bufferPtr, numBytes, freeOnDestruct);
413 }
414 }
415 ~ProtoPktUMP() {}
416
417 static unsigned int GetOptionLength()
418 {return 8;}
419
420 // Use these to build an option
421 bool InitIntoBuffer(void* bufferPtr = NULL,
422 unsigned int numBytes = 0,
423 bool freeOnDestruct = false)
424 {
425 if (ProtoPktIPv4::Option::InitIntoBuffer(ProtoPktIPv4::Option::UMP, bufferPtr, numBytes, freeOnDestruct))
426 {
427 SetSequence(0);
428 SetSrcAddr(PROTO_ADDR_NONE);
429 return true;
430 }
431 else
432 {
433 return false;
434 }
435 }
436 void SetSequence(UINT16 seq)
437 {SetWord16(OFFSET_SEQ, seq);}
438 void SetSrcAddr(const ProtoAddress& addr)
439 {
440 if (addr.IsValid())
441 memcpy(AccessBuffer32(OFFSET_SRC), addr.GetRawHostAddress(), 4);
442 else
443 memset(AccessBuffer32(OFFSET_SRC), 0, 4);
444 }
445
446 // Use these to parse
447 bool InitFromBuffer(void* bufferPtr = NULL,
448 unsigned int numBytes = 0,
449 bool freeOnDestruct = false)
450 {return ProtoPktIPv4::Option::InitFromBuffer(bufferPtr, numBytes, freeOnDestruct);}
451 UINT16 GetSequence() const
452 {return GetWord16(OFFSET_SEQ);}
453 void GetSrcAddr(ProtoAddress& addr)
454 {addr.SetRawHostAddress(ProtoAddress::IPv4, (char*)GetBuffer32(OFFSET_SRC), 4);}
455
456 private:
457 enum
458 {
459 OFFSET_SEQ = (OFFSET_LENGTH+1)/2, // UINT16 offset, 2-byte sequence number
460 OFFSET_SRC = (OFFSET_SEQ+1)/2 // UINT32 offset, upstream src addr
461 };
462
463 }; // end class ProtoPktUMP
464
465 /**
466 * @class ProtoPktIPv6
467 *
468 * @brief Parses IPv6 packets
469 */
470
471 class ProtoPktIPv6 : public ProtoPktIP
472 {
473 public:
474 ProtoPktIPv6(void* bufferPtr = 0,
475 unsigned int numBytes = 0,
476 bool initFromBuffer = false,
477 bool freeOnDestruct = false);
478 ProtoPktIPv6(ProtoPktIP& ipPkt);
479 ~ProtoPktIPv6();
480
481 enum {ADDR_LEN = 16};
482
483 class Extension;
484 /**
485 * @class Option
486 *
487 * @brief IPv6 Option Base class
488 */
489
490 class Option : public ProtoPkt
491 {
492 public:
493 Option(void* bufferPtr = NULL,
494 unsigned int numBytes = 0,
495 bool initFromBuffer = true,
496 bool freeOnDestruct = false);
497 ~Option();
498
499 bool InitFromBuffer(void* bufferPtr = NULL,
500 unsigned int numBytes = 0,
501 bool freeOnDestruct = false);
502
503 enum UnknownPolicy
504 {
505 SKIP = 0x00, /// skip and keep processing packet
506 DISCARD = 0x01, /// silently discard the packet
507 REPORT_ALL = 0x02, /// discard and ICMP to source for any packet
508 REPORT_UNICAST = 0x03 /// discard and ICMP to source for non-multicast packets
509 };
510
511 enum Type
512 {
513 PAD1 = 0,
514 PADN = 1,
515 SMF_DPD = 2 /// SMF duplicate packet detection option (temp value assignment)
516 };
517
518 UnknownPolicy GetUnknownPolicy() const
519 {return (UnknownPolicy)(GetUINT8(OFFSET_TYPE) >> 6);}
520 bool IsMutable() const
521 {return (0 != (FLAG_MUTABLE & GetUINT8(OFFSET_TYPE)));}
522 Type GetType() const
523 {return (Type)(GetUINT8(OFFSET_TYPE) & 0x1f);}
524 UINT8 GetDataLength() const
525 {return ((PAD1 == GetType()) ? 0 : GetUINT8(OFFSET_DATA_LENGTH));}
526 bool HasData() {return (GetDataLength() > 0);}
527 const char* GetData() const
528 {return (char*)GetBuffer(OFFSET_DATA);}
529 unsigned int GetLength() const
530 {
531 return ((0 == GetBufferLength()) ?
532 0 : ((PAD1 == GetType()) ?
533 1 : 2 + GetDataLength()));
534 }
535
536 bool InitIntoBuffer(Type type,
537 void* bufferPtr = NULL,
538 unsigned int numBytes = 0,
539 bool freeOnDestruct = false);
540 void SetUnknownPolicy(UnknownPolicy policy)
541 {
542 UINT8& byte = AccessUINT8(OFFSET_TYPE);
543 byte &= ~((char)(0x03 << 6));
544 byte |= ((char)policy) << 6;
545 }
546 void SetMutable(bool state)
547 {
548 UINT8& byte = AccessUINT8(OFFSET_TYPE);
549 byte = state ? (byte | FLAG_MUTABLE) : (byte & ~FLAG_MUTABLE);
550 }
551 void SetType(Type type)
552 {
553 UINT8& byte = AccessUINT8(OFFSET_TYPE);
554 byte &= ~((char)0x1f);
555 byte |= (char)(type & 0x1f);
556 }
557 bool SetData(char* dataPtr, UINT8 dataLen);
558 bool MakePad(UINT8 numBytes);
559
560 /**
561 * @class Iterator
562 *
563 * @brief Iterator class
564 */
565
566 class Iterator
567 {
568 public:
569 Iterator(const Extension& extension);
570 ~Iterator();
571 void Reset() {offset = 2;}
572 bool GetNextOption(Option& option);
573
574 private:
575 const Extension& hdr_extension;
576 unsigned int offset;
577
578 }; // end class ProtoPktIPv6::Option::Iterator
579
580 protected:
581 void SetDataLength(UINT8 dataLen)
582 {SetUINT8(OFFSET_DATA_LENGTH, dataLen);}
583
584 enum {FLAG_MUTABLE = 0x20};
585 enum
586 {
587 OFFSET_TYPE = 0,
588 OFFSET_DATA_LENGTH = OFFSET_TYPE + 1,
589 OFFSET_DATA = OFFSET_DATA_LENGTH + 1
590 };
591
592 }; // end class ProtoPktIPv6::Option
593
594 /**
595 * @class Extension
596 * @brief IPv6 Extension class
597 */
598 class Extension : public ProtoPkt
599 {
600 public:
601 Extension(Protocol extType = ProtoPktIP::NONE,
602 void* bufferPtr = NULL,
603 unsigned int numBytes = 0,
604 bool initFromBuffer = true,
605 bool freeOnDestruct = false);
606 ~Extension();
607
608 // Must have a buffer attached or allocated before
609 // calling this one.
610 bool Copy(const Extension& ext);
611
612 bool InitIntoBuffer(Protocol extensionType,
613 void* bufferPtr = NULL,
614 unsigned int numBytes = 0,
615 bool freeOnDestruct = false);
616 void SetType(Protocol extensionType)
617 {ext_type = extensionType;}
618 void SetNextHeader(Protocol protocol)
619 {
620 ASSERT(GetBufferLength() > OFFSET_NEXT_HDR);
621 SetUINT8(OFFSET_NEXT_HDR, (UINT8)protocol);
622 }
623 void SetExtensionLength(UINT16 numBytes);
624
625 Option* AddOption(Option::Type optType); // not for all extension types!
626 bool ReplaceOption(Option& oldOpt, Option& newOpt);
627
628
629 bool Pack();
630
631 bool InitFromBuffer(Protocol extType, void* bufferPtr = NULL, unsigned int numBytes = 0, bool freeOnDestruct = false);
632 Protocol GetType() const
633 {return ext_type;}
634 Protocol GetNextHeader() const
635 {return (Protocol)GetUINT8(OFFSET_NEXT_HDR);}
636 /**
637 * This gets the extension's length embedded in the assumed "payload length" field
638 * (Note the FRAGMENT extension doesn't have this field and is of fixed length)
639 */
640 UINT16 GetExtensionLength() const; // _total_ length of extension header in bytes
641
642 bool IsOptionHeader()
643 {
644 switch(ext_type)
645 {
646 case HOPOPT:
647 case DSTOPT:
648 return true;
649 default:
650 return false;
651 }
652 }
653
654 /**
655 * @class Iterator
656 *
657 * @brief Extension iterator class
658 */
659 class Iterator
660 {
661 public:
662 Iterator(ProtoPktIPv6& pkt);
663 ~Iterator();
664 void Reset()
665 {
666 next_hdr = ipv6_pkt.GetNextHeader();
667 offset = 40; // look for first extension after IPv6 base header
668 }
669 bool GetNextExtension(Extension& extension);
670
671 private:
672 ProtoPktIPv6& ipv6_pkt;
673 Protocol next_hdr;
674 UINT16 offset; // current byte offset into IPv6 packet buffer
675 }; // end class ProtoPktIPv6::Extension::Iterator
676
677 protected:
678 void PackOption(); // pack pending option, if needed
679 bool PadOptionHeader(); // pack and pad as needed
680
681 enum
682 {
683 OFFSET_NEXT_HDR = 0,
684 OFFSET_LENGTH = OFFSET_NEXT_HDR+1 // only applies to some extension types
685 };
686
687 Protocol ext_type;
688 Option opt_temp;
689 bool opt_pending;
690 bool opt_packed; // has been padded/packed
691 }; // end class ProtoPktIPv6::Extension
692
693
694 // Use these to build a packet
695 bool InitIntoBuffer(void* bufferPtr = NULL,
696 unsigned int numBytes = 0,
697 bool freeOnDestruct = false);
698
699 void SetTrafficClass(UINT8 trafficClass)
700 {
701 UINT8* ptr = (UINT8*)AccessBuffer(OFFSET_CLASS_MSN);
702 *ptr &= 0xf0;
703 *ptr++ |= (trafficClass >> 4); // post-increment ptr of OFFSET_CLASS_LSN
704 *ptr &= 0x0f;
705 *ptr |= (trafficClass << 4);
706 }
707 void SetFlowLabel(UINT32 flowLabel)
708 {
709 flowLabel = (0xfc00 & GetWord32(OFFSET_LABEL)) | (flowLabel & 0x03ff);
710 SetWord32(OFFSET_LABEL, flowLabel);
711 }
712 void SetPayloadLength(UINT16 numBytes)
713 {
714 SetWord16(OFFSET_LENGTH, numBytes);
715 ProtoPkt::SetLength(40 + numBytes);
716 }
717 void SetNextHeader(Protocol protocol)
718 {SetUINT8(OFFSET_NEXT_HDR, (UINT8)protocol);}
719 void SetHopLimit(UINT8 hopLimit)
720 {SetUINT8(OFFSET_HOP_LIMIT, hopLimit);}
721 void SetSrcAddr(ProtoAddress& addr)
722 {memcpy((char*)AccessBuffer32(OFFSET_SRC_ADDR), addr.GetRawHostAddress(), 16);}
723 void SetDstAddr(ProtoAddress& addr)
724 {memcpy((char*)AccessBuffer32(OFFSET_DST_ADDR), addr.GetRawHostAddress(), 16);}
725
726 // Map extension to end of current IPv6 packet header
727 Extension* AddExtension(Protocol extType);
728 bool PackHeader(Protocol nextHeader = NONE); // finalize pending extension, if applicable
729
730 // Copy extension to beginning of packet's set of header extensions (payload is moved if present)
731 bool PrependExtension(Extension& ext);
732 // Copy extension to end of current IPv6 header (payload is moved if present)
733 bool AppendExtension(Extension& ext);
734 bool ReplaceExtension(Extension& oldExt, Extension& newExt);
735
736 bool SetPayload(Protocol payloadType, const char* dataPtr, UINT16 dataLen);
737
738 // Use these to parse a packet
739 bool InitFromBuffer(void* bufferPtr = NULL,
740 unsigned int numBytes = 0,
741 bool freeOnDestruct = false);
742 UINT8 GetTrafficClass() const
743 {
744 const UINT8* ptr = (UINT8*)GetBuffer(OFFSET_CLASS_MSN);
745 UINT8 trafficClass = (*ptr++ & 0x0f) << 4; // post-increment ptr to OFFSET_CLASS_LSN
746 trafficClass |= (*ptr & 0xf0) >> 4;
747 return trafficClass;
748 }
749 static unsigned int GetHeaderLength()
750 {return BASE_HDR_LENGTH;}
751 UINT32 GetFlowLabel() const
752 {return (GetWord32(OFFSET_LABEL) & 0x03ff);}
753 Protocol GetNextHeader() const
754 {return (Protocol)GetUINT8(OFFSET_NEXT_HDR);}
755 Protocol GetLastHeader(); // returns type of final extension/transport in packet
756 UINT8 GetHopLimit() const
757 {return GetUINT8(OFFSET_HOP_LIMIT);}
758 void GetSrcAddr(ProtoAddress& addr) const
759 {addr.SetRawHostAddress(ProtoAddress::IPv6, (char*)GetBuffer32(OFFSET_SRC_ADDR), 16);}
760 const void* GetSrcAddrPtr() const
761 {return GetBuffer32(OFFSET_SRC_ADDR);}
762 void GetDstAddr(ProtoAddress& addr) const
763 {addr.SetRawHostAddress(ProtoAddress::IPv6, (char*)GetBuffer32(OFFSET_DST_ADDR), 16);}
764 const void* GetDstAddrPtr() const
765 {return GetBuffer32(OFFSET_DST_ADDR);}
766
767 bool HasExtendedHeader() const
768 {return IsExtension(GetNextHeader());}
769
770 const void* GetPayload() const
771 {return (void*)GetBuffer32(BASE_HDR_LENGTH/4);}
772 void* AccessPayload()
773 {return (void*)AccessBuffer32(BASE_HDR_LENGTH/4);}
774 UINT16 GetPayloadLength() const
775 {return GetWord16(OFFSET_LENGTH);}
776
777 private:
778 enum
779 {
780 OFFSET_CLASS_MSN = OFFSET_VERSION, // 1/2 byte traffic class, most sig nybble
781 OFFSET_CLASS_LSN = OFFSET_CLASS_MSN+1, // 1/2 byte traffic class, least sig nybble
782 OFFSET_LABEL = OFFSET_CLASS_LSN/4, // 24-bit flow label at word offset 0, UINT32 offset
783 OFFSET_LENGTH = (OFFSET_LABEL+1)*2, // 2 payload length bytes, UINT16 offset
784 OFFSET_NEXT_HDR = (OFFSET_LENGTH+1)*2, // 1 byte next header type, UINT8 offset
785 OFFSET_HOP_LIMIT = OFFSET_NEXT_HDR+1, // 1 byte hop limit value, UINT8 offset
786 OFFSET_SRC_ADDR = (OFFSET_HOP_LIMIT+1)/4,// 16 bytes of IPv6 address, UINT32 offset
787 OFFSET_DST_ADDR = OFFSET_SRC_ADDR+4, // 16 bytes of IPv6 address, UINT32 offset
788 BASE_HDR_LENGTH = (OFFSET_DST_ADDR+4)*4 // 40 bytes total, UINT8 offset
789 };
790
791 Extension ext_temp;
792 bool ext_pending;
793
794 }; // end class ProtoPktIPv6
795
796 /**
797 * @class ProtoPktFRAG
798 *
799 * @brief Builds IPv6 FRAG extension.
800 */
801
802 class ProtoPktFRAG : public ProtoPktIPv6::Extension
803 {
804 public:
805 ProtoPktFRAG(void* bufferPtr = NULL,
806 unsigned int numBytes = 0,
807 bool initFromBuffer = true,
808 bool freeOnDestruct = false);
809 ~ProtoPktFRAG();
810
811 // Use these to build a FRAG extension
812 bool InitIntoBuffer(void* bufferPtr = NULL,
813 unsigned int numBytes = 0,
814 bool freeOnDestruct = false);
815
816 void SetFragmentOffset(UINT16 offsetValue)
817 {
818 /// mask flags and "or" in new "offsetValue"
819 UINT16 frag = GetWord16(OFFSET_FRAGMENT);
820 frag = (frag & 0x0007) | (offsetValue << 3);
821 SetWord16(OFFSET_FRAGMENT, frag);
822 }
823
824 void SetMF()
825 {AccessUINT8(OFFSET_FLAGS) |= ((UINT8)FLAG_MF);}
826 void ClearMF()
827 {AccessUINT8(OFFSET_FLAGS) &= ~((UINT8)FLAG_MF);}
828
829 void SetID(UINT32 identifier)
830 {SetWord32(OFFSET_ID, identifier);}
831
832 /// Use these to parse a FRAG extension
833 bool InitFromBuffer(void* bufferPtr = NULL,
834 unsigned int numBytes = 0,
835 bool freeOnDestruct = false)
836 {
837 return Extension::InitFromBuffer(ProtoPktIP::FRAG, bufferPtr, numBytes, freeOnDestruct);
838 }
839
840 UINT16 GetFragmentOffset() const
841 {return (GetWord16(OFFSET_FRAGMENT) >> 3);}
842
843 const char* GetFragmentOffsetPtr() const
844 {return (const char*)GetBuffer16(OFFSET_FRAGMENT);}
845
846 bool GetMF() const
847 {return (0 != (GetUINT8(OFFSET_FLAGS) & ((UINT8)FLAG_MF)));}
848
849 UINT32 GetID() const
850 {return GetWord32(OFFSET_ID);}
851
852 private:
853 enum
854 {
855 OFFSET_RESERVED = (OFFSET_LENGTH + 1)*2, // UINT8 offset
856 OFFSET_FRAGMENT = (OFFSET_RESERVED + 1)/2, // UINT16 offset
857 OFFSET_FLAGS = (OFFSET_FRAGMENT*2) + 1, // UINT8 offset
858 OFFSET_ID = (OFFSET_FLAGS + 1)/4 // UINT32 offset
859 };
860 enum Flag {FLAG_MF = 0x01};
861 }; // end class ProtoPktFRAG
862
863 /**
864 * @class ProtoPktAUTH
865 *
866 * @brief IPv6 Authentication Header (AUTH) extension
867 */
868 class ProtoPktAUTH : public ProtoPktIPv6::Extension
869 {
870 public:
871 ProtoPktAUTH(void* bufferPtr = NULL,
872 unsigned int numBytes = 0,
873 bool initFromBuffer = true,
874 bool freeOnDestruct = false);
875 ~ProtoPktAUTH();
876
877 // Use these to build an AUTH extension
878 // (TBD) Should "InitIntoBuffer() make sure there is room for spi & sequence fields?
879 bool InitIntoBuffer(void* bufferPtr = NULL,
880 unsigned int numBytes = 0,
881 bool freeOnDestruct = false);
882 void SetSPI(UINT32 spi)
883 {SetWord32(OFFSET_SPI, spi);}
884
885 void SetSequence(UINT32 sequence)
886 {SetWord32(OFFSET_SEQUENCE, sequence);}
887 // (TBD) add a method to set the ICV field
888
889 /// Use these to parse an AUTH extension
890 bool InitFromBuffer(void* bufferPtr = NULL,
891 unsigned int numBytes = 0,
892 bool freeOnDestruct = false);
893
894 UINT32 GetSPI() const
895 {return GetWord32(OFFSET_SPI);}
896
897 const char* GetSPIPtr() const
898 {return (char*)GetBuffer32(OFFSET_SPI);}
899
900 UINT32 GetSequence() const
901 {return GetWord32(OFFSET_SEQUENCE);}
902
903 const char* GetSequencePtr() const
904 {return (char*)GetBuffer32(OFFSET_SEQUENCE);}
905
906 private:
907 enum
908 {
909 OFFSET_RESERVED = (OFFSET_LENGTH + 1)/2, // UINT16 offset
910 OFFSET_SPI = ((OFFSET_RESERVED+1)*2)/4,// UINT32 offset
911 OFFSET_SEQUENCE = (OFFSET_SPI + 1), // UINT32 offset
912 OFFSET_ICV = (OFFSET_SEQUENCE + 1) // UINT32 offset
913 };
914 }; // end class ProtoPktAUTH
915
916 /**
917 * @class ProtoPktESP
918 *
919 * @brief IPv6 Encapsulating Security Protocol (ESP) header
920 */
921 class ProtoPktESP : public ProtoPkt
922 {
923 public:
924 ProtoPktESP(void* bufferPtr = NULL,
925 unsigned int numBytes = 0,
926 bool freeOnDestruct = false);
927 ~ProtoPktESP();
928
929 // Use these to build an ESP header
930 // (TBD) Should "InitIntoBuffer() make sure there is room for spi & sequence fields?
931 bool InitIntoBuffer(void* bufferPtr = NULL,
932 unsigned int numBytes = 0,
933 bool freeOnDestruct = false);
934 void SetSPI(UINT32 spi)
935 {SetWord32(OFFSET_SPI, spi);}
936
937 void SetSequence(UINT32 sequence)
938 {SetWord32(OFFSET_SEQUENCE, sequence);}
939 // (TBD) add a method to set the ICV field
940
941 // Use these to parse an ESP extension
942 bool InitFromBuffer(UINT16 espLength,
943 void* bufferPtr = NULL,
944 unsigned int numBytes = 0,
945 bool freeOnDestruct = false);
946
947 UINT32 GetSPI() const
948 {return GetWord32(OFFSET_SPI);}
949
950 const char* GetSPIPtr() const
951 {return (char*)GetBuffer32(OFFSET_SPI);}
952
953 UINT32 GetSequence() const
954 {return GetWord32(OFFSET_SEQUENCE);}
955
956 const char* GetSequencePtr() const
957 {return (char*)GetBuffer32(OFFSET_SEQUENCE);}
958
959 private:
960 enum
961 {
962 OFFSET_SPI = 0, // UINT32 offset
963 OFFSET_SEQUENCE = (OFFSET_SPI + 1), // UINT32 offset
964 OFFSET_PAYLOAD = (OFFSET_SEQUENCE + 1) // UINT32 offset
965 };
966 }; // end class ProtoPktESP
967
968
969
970 // This represents the RFC 2004 Minimal Forwarding Header and IP Payload
971 class ProtoPktMobile : public ProtoPkt
972 {
973 public:
974 ProtoPktMobile(void* bufferPtr = NULL,
975 unsigned int numBytes = 0,
976 bool initFromBuffer = false,
977 bool freeOnDestruct = false);
978 ~ProtoPktMobile();
979
980 enum Flag {FLAG_SRC = 0x80};
981
982 // Use these to build an MOBILE Minimal Forwarding Header
983 bool InitIntoBuffer(void* bufferPtr = NULL,
984 unsigned int numBytes = 0,
985 bool freeOnDestruct = false);
986
987 void SetProtocol(ProtoPktIP::Protocol protocol)
988 {SetUINT8(OFFSET_PROTOCOL, (UINT8)protocol);}
989 void SetFlag(Flag flag)
990 {AccessUINT8(OFFSET_FLAGS) |= flag;}
991 void ClearFlag(Flag flag)
992 {AccessUINT8(OFFSET_FLAGS) &= ~flag;}
993 void SetChecksum(UINT16 checksum)
994 {SetWord32(OFFSET_CHECKSUM, checksum);}
995 void SetDstAddr(const ProtoAddress& addr, bool calculateChecksum = false);
996 bool SetSrcAddr(const ProtoAddress& addr, bool calculateChecksum = false);
997 void SetPayload(const char* payload, UINT16 numBytes)
998 {
999 memcpy(AccessBuffer32(OffsetPayload()), payload, numBytes);
1000 if (FlagIsSet(FLAG_SRC))
1001 SetLength(12 + numBytes);
1002 else
1003 SetLength(8 + numBytes);
1004 }
1005 UINT16 CalculateChecksum(bool set = true);
1006
1007 bool InitFromBuffer(void* bufferPtr = NULL,
1008 unsigned int numBytes = 0,
1009 bool freeOnDestruct = false);
1010
1011 ProtoPktIP::Protocol GetProtocol() const
1012 {return (ProtoPktIP::Protocol)GetUINT8(OFFSET_PROTOCOL);}
1013 bool FlagIsSet(Flag flag) const
1014 {return (0 != (flag & GetUINT32(OFFSET_FLAGS)));}
1015 UINT16 GetChecksum() const
1016 {return GetWord16(OFFSET_CHECKSUM);}
1017 void GetDstAddr(ProtoAddress& dst) const
1018 {dst.SetRawHostAddress(ProtoAddress::IPv4, (char*)GetBuffer32(OFFSET_DST_ADDR), 4);}
1019 bool GetSrcAddr(ProtoAddress& src) const;
1020
1021 UINT16 GetPayloadLength() const
1022 {return GetLength() - 4*OffsetPayload();}
1023 const void* GetPayload() const
1024 {return (void*)GetBuffer32(OffsetPayload());}
1025 void* AccessPayload()
1026 {return (void*)AccessBuffer32(OffsetPayload());}
1027
1028 private:
1029 enum
1030 {
1031 OFFSET_PROTOCOL = 0, // UINT8 offset
1032 OFFSET_RESERVED = (OFFSET_PROTOCOL + 1), // UINT8 offset
1033 OFFSET_FLAGS = OFFSET_RESERVED, // UINT8 offset
1034 OFFSET_CHECKSUM = (OFFSET_PROTOCOL/2 + 1), // UINT16 offset
1035 OFFSET_DST_ADDR = (OFFSET_CHECKSUM/2 + 1), // UINT32 offset
1036 OFFSET_SRC_ADDR = (OFFSET_DST_ADDR + 1) // UINT32 offset
1037 };
1038
1039 unsigned int OffsetPayload() const // (UINT32 offset
1040 {return (FlagIsSet(FLAG_SRC) ? OFFSET_SRC_ADDR + 1 : OFFSET_SRC_ADDR);}
1041
1042
1043 }; // end class ProtoPktMobile
1044
1045 /**
1046 * @class ProtoPktDPD
1047 *
1048 * @brief IPv6 Simplified Multicast Forwarding Duplicate Packet Detection (DPD) option
1049 */
1050 class ProtoPktDPD : public ProtoPktIPv6::Option
1051 {
1052 public:
1053 ProtoPktDPD(void* bufferPtr = NULL,
1054 unsigned int numBytes = 0,
1055 bool initFromBuffer = true,
1056 bool freeOnDestruct = false);
1057 ~ProtoPktDPD();
1058
1059 enum TaggerIdType
1060 {
1061 TID_NULL = 0,
1062 TID_DEFAULT = 1,
1063 TID_IPv4_ADDR = 2,
1064 TID_IPv6_ADDR = 3,
1065 TID_EXT = 7
1066 };
1067
1068 // Use these to build a DPD extension (call them in order)
1069 bool InitIntoBuffer(void* bufferPtr = NULL,
1070 unsigned int numBytes = 0,
1071 bool freeOnDestruct = false);
1072
1073 // If SetHashAssistValue() is used, the other "set" methods do not apply
1074 bool SetHAV(const char* hashAssistValue, UINT8 numBytes);
1075
1076 // After init, call one of these first to set taggerId, if applicable
1077 bool SetTaggerId(TaggerIdType type, const char* taggerId, UINT8 taggerIdLength);
1078 bool SetTaggerId(const ProtoAddress& ipAddr);
1079
1080 // Finally, call one of these to set the pktId (sequence number)
1081 bool SetPktId(const char* pktId, UINT8 pktIdLength);
1082 bool SetPktId(UINT8 value)
1083 {return SetPktId((char*)&value, 1);}
1084 bool SetPktId(UINT16 value)
1085 {
1086 value = htons(value);
1087 return SetPktId((char*)&value, 2);
1088 }
1089 bool SetPktId(UINT32 value)
1090 {
1091 value = htonl(value);
1092 return SetPktId((char*)&value, 4);
1093 }
1094
1095
1096 // Use these to parse a DPD extension
1097 bool InitFromBuffer(void* bufferPtr = NULL,
1098 unsigned int numBytes = 0,
1099 bool freeOnDestruct = false);
1100
1101 bool HasHAV() const
1102 {return (0 != (0x80 & GetUINT8(OFFSET_HAV)));}
1103
1104 UINT8 GetHAVLength() const
1105 {return GetDataLength();}
1106
1107 const char* GetHAV() const
1108 {return (HasHAV() ? (char*)GetBuffer(OFFSET_HAV) : NULL);}
1109
1110
1111 TaggerIdType GetTaggerIdType() const
1112 {return (HasHAV() ? TID_NULL : (TaggerIdType)((GetUINT8(OFFSET_TID_TYPE) >> 4) & 0x0f));}
1113
1114 UINT8 GetTaggerIdLength() const
1115 {
1116 UINT8 tidType = HasHAV() ? TID_NULL : GetUINT8(OFFSET_TID_TYPE);
1117 return ((TID_NULL != (TaggerIdType)tidType) ? ((tidType & 0x0f) + 1) : 0);
1118 }
1119
1120 const char* GetTaggerId() const
1121 {return (char*)GetBuffer(OFFSET_TID_VALUE);}
1122 bool GetTaggerId(ProtoAddress& addr) const;
1123
1124 UINT8 GetPktIdLength() const
1125 {return (GetDataLength() - GetTaggerIdLength() - (HasHAV() ? 0 : 1));}
1126
1127 const char* GetPktId() const
1128 {return (char*)GetBuffer(OffsetPktId());}
1129 bool GetPktId(UINT8& value) const;
1130 bool GetPktId(UINT16& value) const;
1131 bool GetPktId(UINT32& value) const;
1132
1133 private:
1134 enum
1135 {
1136 OFFSET_HAV = OFFSET_DATA, // UINT8 offset (note msbit always '1')
1137 OFFSET_TID_TYPE = OFFSET_DATA, // UINT8 offset (upper 4 bits)
1138 OFFSET_TID_LENGTH = OFFSET_TID_TYPE, // UINT8 offset (lower 4 bits)
1139 OFFSET_TID_VALUE = (OFFSET_TID_TYPE + 1)// UINT8 offset
1140 };
1141 unsigned int OffsetPktId() const
1142 {return (OFFSET_TID_VALUE + GetTaggerIdLength() - (HasHAV() ? 1 : 0));}
1143 }; // end class ProtoPktDPD
1144
1145 /**
1146 * @class ProtoPktUDP
1147 *
1148 * @brief Parses UDP Packets
1149 */
1150 class ProtoPktUDP : public ProtoPkt
1151 {
1152 public:
1153 ProtoPktUDP(void* bufferPtr = 0,
1154 unsigned int numBytes = 0,
1155 bool initFromBuffer = true,
1156 bool freeOnDestruct = false);
1157 ~ProtoPktUDP();
1158
1159 // Use these to parse the datagram
1160 bool InitFromBuffer(void* bufferPtr = NULL,
1161 unsigned int numBytes = 0,
1162 bool freeOnDestruct = false);
1163 bool InitFromPacket(ProtoPktIP& pkt);
1164 static unsigned int GetHeaderLength()
1165 {return (OFFSET_PAYLOAD*4);}
1166 UINT16 GetSrcPort() const
1167 {return GetWord16(OFFSET_SRC);}
1168 UINT16 GetDstPort() const
1169 {return GetWord16(OFFSET_DST);}
1170 UINT16 GetChecksum() const
1171 {return GetWord16(OFFSET_CHECKSUM);}
1172 UINT16 GetPayloadLength() const
1173 {return (GetWord16(OFFSET_LENGTH) - 8);}
1174 const void* GetPayload() const
1175 {return (void*)GetBuffer32(OFFSET_PAYLOAD);}
1176 void* AccessPayload()
1177 {return (void*)AccessBuffer32(OFFSET_PAYLOAD);}
1178 UINT16 ComputeChecksum(ProtoPktIP& ipPkt) const;
1179 bool ChecksumIsValid(ProtoPktIP& ipPkt) const
1180 {return (GetChecksum() == ComputeChecksum(ipPkt));}
1181
1182 // Use these to build the datagram
1183 bool InitIntoBuffer(void* bufferPtr = 0,
1184 unsigned int numBytes = 0,
1185 bool freeOnDestruct = false);
1186 void SetSrcPort(UINT16 port)
1187 {SetWord16(OFFSET_SRC, port);}
1188 void SetDstPort(UINT16 port)
1189 {SetWord16(OFFSET_DST, port);}
1190 void SetChecksum(UINT16 checksum)
1191 {SetWord16(OFFSET_CHECKSUM, checksum);}
1192 void SetPayload(const char* payload, UINT16 numBytes)
1193 {
1194 memcpy(AccessBuffer32(OFFSET_PAYLOAD), payload, numBytes);
1195 SetPayloadLength(numBytes);
1196 }
1197 void SetPayloadLength(UINT16 numBytes)
1198 {
1199 numBytes += 8;
1200 SetWord16(OFFSET_LENGTH, numBytes);
1201 ProtoPkt::SetLength(numBytes);
1202 }
1203 void FinalizeChecksum(ProtoPktIP& ipPkt)
1204 {SetChecksum(ComputeChecksum(ipPkt));}
1205
1206 private:
1207 enum
1208 {
1209 OFFSET_SRC = 0, // source port number (UINT16 offset)
1210 OFFSET_DST = OFFSET_SRC + 1, // destination port number (UINT16 offset)
1211 OFFSET_LENGTH = OFFSET_DST + 1, // UDP datagram length (bytes) (UINT16 offset)
1212 OFFSET_CHECKSUM = OFFSET_LENGTH + 1, // (UINT16 offset)
1213 OFFSET_PAYLOAD = (OFFSET_CHECKSUM + 1)/2 // (UINT32 offset0
1214 };
1215 }; // end class ProtoPktUDP
1216
1217
1218 #endif // _PROTO_PKT_IP