4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 /** \mainpage The SENF Packet Library
25 The SENF Packet library provides facilities to analyze, manipulate and create structured packet
26 oriented data (e.g. network packets).
31 \section packet_intro_arch Introduction
32 \seechapter \ref packet_arch
34 The Packet library consists of several components:
36 \li The \ref packet_module manages the packet data and provides the framework for handling the
37 chain of packet headers. The visible interface is provided by the Packet class.
38 \li \ref packetparser provides the framework for interpreting packet data. It handles
39 parsing the packet information into meaningful values.
40 \li The \ref protocolbundles provide concrete implementations for interpreting packets of
41 some protocol. The Protocol Bundles are built on top of the basic packet library.
43 All these components work together to provide a hopefully simple and intuitive interface to
44 packet parsing and creation.
47 \section packet_intro_usage Tutorial
48 \seechapter \ref packet_usage
50 This chapter discusses the usage of the packet library from a high level view.
53 \section packet_intro_api The packet API
55 The packet library API is divided into three areas
57 \li the \ref senf::PacketData API for accessing the raw data container
58 \li the packet interpreter chain providing \ref packet_module
59 \li and \ref packetparser which provides access to protocol specific packet fields.
62 \section protocolbundles Supported packet types (protocols)
64 Each protocol bundle provides a collection of related concrete packet classes for a group of
67 \li \ref protocolbundle_default : Some basic default protocols: Ethernet, Ip, TCP, UDP
68 \li \ref protocolbundle_mpegdvb : MPEG and DVB protocols
69 \li \ref protocolbundle_80211 : 802.11 protocols
70 \li \ref protocolbundle_80221 : 802.21 protocols
72 There are two ways to link with a bundle
74 \li If you only work with known packets which you explicitly reference you may just link with
75 the corresponding library.
76 \li If you need to parse unknown packets and want those to be parsed as complete as possible
77 without explicitly referencing the packet type, you will need to link against the combined
78 object file built for every bundle. This way, all packets defined in the bundle will be
79 included whether they are explicitly referenced or not (and they will all automatically be
83 \section packet_intro_new Defining new packet types
84 \seechapter \ref packet_new
86 The packet library provides the framework which allows to define arbitrary packet types. There
87 is quite some information needed to completely specify a specific type of packet.
91 /** \page packet_arch Overall Packet library Architecture
93 The packet library handles network packets of a large number of protocols. We work with a packet
99 \section packet_arch_handle The Packet handle
101 Whenever we are using a Packet, we are talking about a senf::Packet (or a
102 senf::ConcretePacket). This class is a \e handle referencing an internally managed packet data
103 structure. So even though we pass senf::Packet instances around by value, they work like
104 references. The packet library automatically manages all required memory resources using
107 Different Packet handles may really internally share one Packet data structure if they both
108 point to the same packet.
111 \section packet_arch_data The Packet as a 'bunch of bytes'
113 From the outside, a packet is just a bunch of bytes just as it is read from (or will be
114 written to) the wire. At this low-level view, we can access the data in it's raw form but
115 have no further information about what kind of packet we have.
117 The packet library provides a consistent container interface for this representation.
122 // Change first byte of packet to 1
125 // Copy packet data into a vector
126 std::vector<char> data (p.data().size());
127 std::copy(p.data().begin(), p.data().end(), data.begin());
130 This type of access is primarily needed when reading or writing packets (e.g. to/from the
133 \see senf::Packet::data() \n
137 \section packet_arch_chain The Interpreter Chain
139 On the next level, the packet is divided into a nested list of sub-packets (or headers) called
140 interpreters. Each senf::Packet handle internally points to an interpreter or header. This
141 allows us to access one and the same packet in different ways.
143 Consider an Ethernet Packet with an IP payload holding a UDP packet. We may reference either the
144 Ethernet packet as a whole or we may reference the IP or UDP interpreters (sub-packets or
145 headers). All handles really refer to the \e same data structure but provide access to a
146 different (sub-)range of the data in the packet.
148 We can navigate around this chained structure using appropriate members:
151 // eth, ip and udp all reference the same internal packet data albeit at different data ranges
153 Packet ip = eth.next();
154 Packet udp = ip.next();
156 eth.next() == ip // true
157 eth.next().is<IPv4Packet>() // true
158 eth.next().next() == udp // true
159 eth.next().is<UDPPacket>() // false
160 eth.find<UDPPacket>() == udp // true
162 udp.find<EthernetPacket>() // throws InvalidPacketChainException
163 udp.find<EthernetPacket>(senf::nothrow) // An in-valid() senf::Packet which tests as 'false'
164 udp.find<UDPPacket> == udp // true
165 udp.first<IPv4Packet>() // throws InvalidPacketChainException
167 udp.prev() == ip // true
168 udp.prev<EthernetPacket>() // throws InvalidPacketChainException
171 \see \ref packet_module
174 \section packet_arch_parser Parsing specific Protocols
176 On the next level, the packet library allows us to parse the individual protocols. This gives us
177 access to the protocol specific data members of a packet and allows us to access or manipulate a
178 packet in a protocol specific way.
180 To access this information, we need to use a protocol specific handle, the senf::ConcretePacket
181 which takes as a template argument the specific type of packet to be interpreted. This allows us
182 to easily interpret or create packets. Here an example on how to create a new Ethernet / IP / UDP
183 / Payload packet interpreter chain:
186 // EthernetPacket, IPv4Packet, UDPPacket and DataPacket are typedefs for corresponding
187 // ConcretePacket instantiations
188 senf::EthernetPacket eth (senf::EthernetPacket::create());
189 senf::IPv4Packet ip (senf::IPv4Packet ::createAfter(eth));
190 senf::UDPPacket udp (senf::UDPPacket ::createAfter(ip));
191 senf::DataPacket payload (senf::DataPacket ::createAfter(udp,
192 std::string("Hello, world!")));
194 udp->source() = 2000u;
195 udp->destination() = 2001u;
197 ip->source() = senf::INet4Address::from_string("192.168.0.1");
198 ip->destination() = senf::INet4Address::from_string("192.168.0.2");
199 eth->source() = senf::MACAddress::from_string("00:11:22:33:44:55");
200 eth->destination() = senf::MACAddress::from_string("00:11:22:33:44:66");
205 Again, realize, that \a eth, \a ip, \a udp and \a payload share the same internal packet
206 data structure (the respective \c data() members all provide access to the same underlying
207 container however at different byte ranges): The complete packet can be accessed at
208 <tt>eth.data()</tt> whereas <tt>payload.data()</tt> only holds UDP payload (in this case the
209 string "Hello, world!").
211 \see \ref packetparser \n
215 /** \page packet_usage Using the packet library
219 \section packet_usage_intro Includes
221 To use the library, you need to include the appropriate header files. This will probably happen
222 automatically when including the specific protocol headers. If needed, you may explicitly use
225 #include "Packets.hh"
230 \warning Never include any other Packets library header directly, only include \c
231 Packets.hh or one (or several) protocol headers from the protocol bundles.
233 Most every use of the packet library starts with some concrete packet typedef. Some fundamental
234 packet types are provided by \ref protocolbundle_default.
237 \section packet_usage_create Creating a new packet
239 Building on those packet types, this example will build a complex packet: This will be an
240 Ethernet packet containing an IPv4 UDP packet. We begin by building the raw packet skeleton:
243 #include "Packets/DefaultBundle/EthernetPacket.hh"
244 #include "Packets/DefaultBundle/IPv4Packet.hh"
245 #include "Packets/DefaultBundle/UDPPacket.hh"
247 senf::EthernetPacket eth (senf::EthernetPacket::create());
248 senf::IPv4Packet ip (senf::IPv4Packet ::createAfter(eth));
249 senf::UDPPacket udp (senf::UDPPacket ::createAfter(ip));
250 senf::DataPacket payload (senf::DataPacket ::createAfter(udp,
251 std::string("Hello, world!")));
254 These commands create what is called an interpreter chain. This chain consists of four
255 interpreters. All interpreters reference the same data storage. This data storage is a random
256 access sequence which contains the data bytes of the packet.
258 \note The data structures allocated are automatically managed using reference counting. In this
259 example we have four packet references each referencing the same underlying data
260 structure. This data structure will be freed when the last reference to it goes out of
263 The packet created above already has the correct UDP payload (The string "Hello, world!")
264 however all protocol fields are empty. We need to set those protocol fields:
267 udp->source() = 2000u;
268 udp->destination() = 2001u;
270 ip->source() = senf::INet4Address::from_string("192.168.0.1");
271 ip->destination() = senf::INet4Address::from_string("192.168.0.2");
272 eth->source() = senf::MACAddress::from_string("00:11:22:33:44:55");
273 eth->destination() = senf::MACAddress::from_string("00:11:22:33:44:66");
278 As seen above, packet fields are accessed using the <tt>-></tt> operator whereas other packet
279 facilities (like \c finalizeAll()) are directly accessed using the member operator. The field
280 values are simply set using appropriately named accessors. As a last step, the \c finalizeAll()
281 call will update all calculated fields (fields like next-protocol, header or payload length,
282 checksums etc). Now the packet is ready. We may now send it out using a packet socket
285 senf::PacketSocketHandle sock ("eth0");
286 sock.write(eth.data());
290 \section packet_usage_read Reading and parsing packets
292 The chain navigation functions are also used to parse a packet. Let's read an Ethernet packet
293 from a packet socket handle:
296 senf::PacketSocketHandle sock ("eth0");
297 senf::EthernetPacket packet (senf::EthernetPacket::create(senf::noinit));
298 sock.read(packet.data(),0u);
301 This first creates an uninitialized Ethernet packet and then reads into this packet. We can now
302 parse this packet. Let's find out, whether this is a UDP packet destined to port 2001:
306 senf::UDPPacket udp (packet.find<UDPPacket>());
307 if (udp->destination() == 2001u) {
310 } catch (senf::TruncatedPacketException &) {
311 std::cerr << "Ooops !! Broken packet received\n";
312 } catch (senf::InvalidPacketChainException &) {
313 std::cerr << "Not a udp packet\n";
317 TruncatedPacketException is thrown by <tt>udp->destination()</tt> if that field cannot be
318 accessed (that is it would be beyond the data read which means we have read a truncated
319 packet). More generally, whenever a field cannot be accessed because it would be out of bounds
320 of the data read, this exception is generated.
323 \section packet_usage_container The raw data container
325 Every packet is based internally on a raw data container holding the packet data. This container
326 is accessed via senf::Packet::data() member.
328 This container is a random access container. It can be used like an ordinary STL container and
329 supports all the standard container members.
334 // Insert 5 0x01 bytes
335 p.data().insert(p.data().begin()+5, 5, 0x01);
337 // Insert data from another container
338 p.data().insert(p.data().end(), other.begin(), other.end());
340 // Erase a single byte
341 p.data().erase(p.data().begin()+5);
343 // XOR byte 5 with 0xAA
347 A packet consists of a list of interpreters (packet headers or protocols) which all reference
348 the same data container at different byte ranges. Each packet consists of the protocol header \e
349 plus the packets payload. This means, that the data container ranges of successive packets from
350 a single interpreter chain are nested.
352 Example: The packet created above (the Ethernet-IP-UDP packet with payload "Hello, world!") has
353 4 Interpreters: Ethernet, IPv4, UDP and the UDP payload data. The nested data containers lead to
354 the following structure
357 // The ethernet header has a size of 14 bytes
358 eth.data().begin() + 14 == ip.data().begin()
359 eth.data().end() == ip.data().end()
361 // The IP header has a size of 20 bytes and therefore
362 ip.data().begin() + 20 == udp.data().begin()
363 ip.data().end() == udp.data().end()
365 // The UDP header has a size of 8 bytes and thus
366 udp.data().begin() + 8 == payload.data().begin()
367 udp.data().end() == payload.data().end()
370 This nesting will (and must) always hold: The data range of a subsequent packet will always be
371 within the range of it's preceding packet.
373 \warning It is forbidden to change the data of a subsequent packet interpreter from the
374 preceding packet even if the data container includes this data. If you do so, you may
375 corrupt the data structure (especially when changing it's size).
377 Every operation on a packet is considered to be \e within this packet and \e without and
378 following packet. When inserting or erasing data, the data ranges are all adjusted
379 accordingly. So the following are \e not the same even though \c eth.end(), \c ip.end() and \c
380 udp.end() are identical.
383 eth.data().insert(eth.data().end(), 5, 0x01);
384 assert( eth.data().end() == ip.data().end() + 5
385 && ip.data().end() == udp.data().end() );
387 // Or alternatively: (You could even use eth.data().end() here ... it's the same)
388 ip.data().insert(ip.data().end(), 5, 0x01);
389 assert( eth.data().end() == ip.data().end()
390 && ip.data().end() == udp.data().end() + 5 );
393 \warning When accessing the packet data via the container interface, you may easily build
394 invalid packets since the packet will not be validated against it's protocol.
397 \section packet_usage_fields Field access
399 When working with concrete protocols, the packet library provides direct access to all the
400 protocol information.
403 udp->source() = 2000u;
404 udp->destination() = 2001u;
406 ip->source() = senf::INet4Address::from_string("192.168.0.1");
407 ip->destination() = senf::INet4Address::from_string("192.168.0.2");
408 eth->source() = senf::MACAddress::from_string("00:11:22:33:44:55");
409 eth->destination() = senf::MACAddress::from_string("00:11:22:33:44:66");
412 The protocol field members above do \e not return references, they return parser instances.
413 Protocol fields are accessed via parsers. A parser is a very lightweight class which points into
414 the raw packet data and converts between raw data bytes and it's interpreted value: For example
415 a senf::UInt16Parser accesses 2 bytes (in network byte order) and converts them to or from a 16
416 bit integer. There are a few properties about parsers which need to be understood:
418 \li Parsers are created only temporarily when needed. They are created when accessing a protocol
419 field and are returned by value.
421 \li A parser never contains a value itself, it just references a packets data container.
423 \li Parsers can be built using other parsers and may have members which return further parsers.
425 The top-level interface to a packets protocol fields is provided by a protocol parser. This
426 protocol parser is a composite parser which has members to access the protocol fields (compare
427 with the example code above). Some protocol fields may be more complex than a simple value. In
428 this case, those accessors may return other composite parsers or collection parsers. Ultimately,
429 a value parser will be returned.
431 The simple value parsers which return plain values (integer numbers, network addresses etc) can
432 be used like those values and can also be assigned corresponding values. More complex parsers
433 don't allow simple assignment. However, they can always be copied from another parser <em>of the
434 same type</em> using the generalized parser assignment. This type of assignment also works for
435 simple parsers and is then identical to a normal assignment.
438 // Copy the complete udp parser from udp packet 2 to packet 1
439 udp1.parser() << udp2.parser();
442 Additionally, the parsers have a parser specific API which allows to manipulate or query the
445 This is a very abstract description of the parser structure. For a more concrete description, we
446 need to differentiate between the different parser types
448 \subsection packet_usage_fields_value Simple fields (Value parsers)
450 We have already seen value parsers: These are the lowest level building blocks witch parse
451 numbers, addresses etc. They return some type of value and can be assigned such a value. More
452 formally, they have a \c value_type typedef member which gives the type of value they accept and
453 they have an overloaded \c value() member which is used to read or set the value. Some parsers
454 have additional functionality: The numeric parser for Example provide conversion and arithmetic
455 operators so they can be used like a numeric value.
457 If you have a value parser \c valueParser with type \c ValueParser, the following will always be
460 // You can read the value and assign it to a variable of the corresponding value_type
461 ValueParser::value_type v (valueParser.value());
463 // You can assign that value to the parser
464 valueParser.value(v);
466 // The assignment can also be done using the generic parser assignment
471 \subsection packet_usage_fields_composite Composite and protocol parsers
473 A composite parser is a parser which just combines several other parsers into a structure: For
474 example, the senf::EthernetPacketParser has members \c destination(), \c source() and \c
475 type_length(). Those members return parsers again (in this case value parsers) to access the
478 Composite parsers can be nested; A composite parser may be returned by another composite
479 parser. The protocol parser is a composite parser which defines the field for a specific
480 protocol header like Ethernet.
482 \subsection packet_usage_fields_collection Collection parsers
484 Besides simple composites, the packet library has support for more complex collections.
486 \li The senf::ArrayParser allows to repeat an arbitrary parser a fixed number of times.
487 \li senf::VectorParser and senf::ListParser are two different types of lists with variable
489 \li The senf::VariantParser is a discriminated union: It will select one of several parsers
490 depending on the value of a discriminant.
493 \subsubsection packet_usage_collection_vector Vector and List Parsers
495 Remember, that a parser does \e not contain any data: It only points into the raw data
496 container. This is also true for the collection parsers. VectorParser and ListParser provide an
497 interface which looks like an STL container to access a sequence of elements.
499 We will use an \c MLDv2QueryPacket as an example (see <a
500 href="http://tools.ietf.org/html/rfc3810#section-5">RFC 3810</a>). Here an excerpt of the
503 <table class="fields">
504 <tr><td>nrOfSources</td><td>Integer</td><td>Number of multicast sources in this packet</td></tr>
505 <tr><td>sources</td><td>Vector of IPv6 Addresses</td><td>Multicast sources</td></tr>
508 To demonstrate nested collections, we use the \c MLDv2ReportPacket as an example. The relevant
509 fields of this packet are;
511 <table class="fields">
512 <tr><td>nrOfRecords</td><td>Integer</td><td>Number of multicast address records</td></tr>
513 <tr><td>records</td><td>List of Records</td><td>List of multicast groups and sources</td></tr>
516 Each Record is a composite with the following relevant fields:
518 <table class="fields">
519 <tr><td>nrOfSources</td><td>Integer</td><td>Number of sources in this record</td></tr>
520 <tr><td>sources</td><td>Vector of IPv6 Addresses</td><td>Multicast sources</td></tr>
523 The first example will iterate over the sources in a \c MLDv2QueryPacket:
526 MLDv2QueryPacket mld = ...;
528 // Instantiate a collection wrapper for the source list
529 MLDv2QueryPacket::Parser::sources_t::container sources (mld->sources());
531 // Iterate over all the addresses in that list
532 for (MLDv2QueryPacket::Parser::sources_t::container::iterator i (sources.begin());
533 i != sources.end(); ++i)
534 std::cout << *i << std::endl;
537 Beside other fields, the MLDv2Query consists of a list of source addresses. The \c sources()
538 member returns a VectorParser for these addresses. The collection parsers can only be accessed
539 completely using a container wrapper. The container wrapper type is available as the \c
540 container member of the collection parser, here it is \c
541 MLDv2QueryPacket::Parser::sources_t::container.
543 Using this wrapper, we can not only read the data, we can also manipulate the source list. Here
544 we copy a list of addresses from an \c std::vector into the packet:
547 std::vector<senf::INet6Address> addrs (...);
549 sources.resize(addrs.size());
550 std::copy(addrs.begin(), addrs.end(), sources.begin())
553 Collection parsers may be nested. To access a nested collection parser, a container wrapper must
554 be allocated for each level. An MLD Report (which is a composite parser) includes a list of
555 multicast address records called \c records(). Each record is again a composite which contains a
556 list of sources called \c sources():
559 MLDv2ReportPacket report = ...;
561 // Instantiate a collection wrapper for the list of records:
562 MLDv2ReportPacket::Parser::records_t::container records (report->records());
564 // Iterate over the multicast address records
565 for (MLDv2ReportPacket::Parser::records_t::container::iterator i (records.begin());
566 i != records.end(); ++i) {
567 // Allocate a collection wrapper for the multicast address record
568 typedef MLDv2ReportPacket::Parser::records_t::value_type::sources_t Sources;
569 Sources::container sources (i->sources());
571 // Iterate over the sources in this record
572 for (Sources::container::iterator i (sources.begin());
573 i != sources.end(); ++i)
574 std::cout << *i << std::endl;
578 In this example we also see how to find the type of a parser or container wrapper.
579 \li Composite parsers have typedefs for each their fields with a \c _t postfix
580 \li The vector or list parsers have a \c value_type typedef which gives the type of the
583 By traversing this hierarchical structure, the types of all the fields can be found.
585 The container wrapper is only temporary (even though it has a longer lifetime than a
586 parser). Any change made to the packet not via the collection wrapper has the potential to
587 invalidate the wrapper if it changes the packets size.
590 senf::VectorParser / senf::VectorParser_Container Interface of the vector parser \n
591 senf::ListParser / senf::ListParser_Container Interface of the list parser
594 \subsubsection packet_usage_collection_variant The Variant Parser
596 The senf::VariantParser is a discriminated union of parsers. It is also used for optional fields
597 (using senf::VoidPacketParser as one possible variant which is a parser parsing nothing). A
598 senf::VariantParser is not really a collection in the strict sense: It only ever contains one
599 element, the \e type of which is determined by the discriminant.
601 For Example, we look at the DTCP HELLO Packet as defined in the UDLR Protocol (see <a
602 href="http://tools.ietf.org/html/rfc3077">RFC 3077</a>)
605 DTCPHelloPacket hello (...);
607 if (hello->ipVersion() == 4) {
608 typedef DTCPHelloPacket::Parser::v4fbipList_t FBIPList;
609 FBIPList::container fbips (hello->v4fbipList());
610 for (FBIPList::container::iterator i (fbips.begin()); i != fbips.end(); ++i)
611 std::cout << *i << std::endl;
613 else { // if (hello->ipVersion() == 6)
614 typedef DTCPHelloPacket::Parser::v6fbipList_t FBIPList;
615 FBIPList::container fbips (hello->v6fbipList());
616 for (FBIPList::container::iterator i (fbips.begin()); i != fbips.end(); ++i)
617 std::cout << *i << std::endl;
621 This packet has a field \c ipVersion() which has a value of 4 or 6. Depending on the version,
622 the packet contains a list of IPv4 or IPv6 addresses. Only one of the fields \c v4fbipList() and
623 \c v6fbipList() is available at a time. Which one is decided by the value of \c
624 ipVersion(). Trying to access the wrong one will provoke undefined behavior.
626 Here we have used the variants discriminant (the \c ipVersion() field) to select, which field to
627 parse. More generically, every variant field should have a corresponding member to test for it's
630 if (hello->has_v4fbipList()) {
633 else { // if (hello->has_v6fbipList())
638 A variant can have more than 2 possible types and you can be sure, that exactly one type will be
639 accessible at any time.
641 It is not possible to change a variant by simply changing the discriminant:
644 hello->ipVersion() = 6;
646 Instead, for each variant field there is a special member which switches the variant to that
647 type. After switching the type, the field will be in it's initialized (that is mostly zero)
650 std::vector<senf::INet6Address> addrs (...);
652 // Initialize the IPv6 list
653 hello->init_v6fbipList();
655 // Copy values into that list
656 DTCPHelloPacket::Parser::v6fbipList_t::container fbips (hello->v6fbipList());
657 fbips.resize(addrs.size());
658 std::copy(addrs.begin(), addrs.end(), fbips.begin());
661 \note Here we have documented the default variant interface as it is preferred. It is possible
662 to define variants in a different way giving other names to the special members (\c has_\e
663 name or \c init_\e name etc.). This must be documented with the composite or protocol parser
664 which defines the variant.
666 \section packet_usage_annotation Annotations
668 Sometimes we need to store additional data with a packet. Data, which is not part of the packet
669 itself but gives us some information about the packet: A timestamp, the interface the packet was
670 received on or other processing related information.
672 This type of information can be stored using the annotation interface. The following example
673 will read packet data and will store the read timestamp as a packet annotation.
677 senf::ClockService::clock_t value;
680 std::ostream & operator<<(std::ostream & os, Timestamp const & tstamp) {
681 os << tstamp.value; return os;
684 senf::EthernetPacket packet (senf::EthernetPacket::create(senf::noinit));
685 sock.read(packet.data(), 0u);
686 packet.annotation<Timestamp>().value = senf::ClockService::now();
689 In the same way, the annotation can be used later
692 if (senf::ClockService::now() - packet.annotation<Timestamp>().value
693 > senf::ClockService::seconds(1)) {
694 // this packet is to old
699 It is very important to define a specific structure (or class or enum) type for each type of
700 annotation. \e Never directly store a fundamental type as an annotation: The name of the type is
701 used to look up the annotation, so you can store only one annotation for each built-in type. \c
702 typedef does not help since \c typedef does not introduce new type names, it only defines an
705 The annotation type must support the output \c operator<< for description purposes
706 (e.g. for the \ref senf::Packet::dump() "Packet::dump()" member).
708 Of course, the annotation structure can be arbitrary. However, one very important caveat: If the
709 annotation is not a POD type, it needs to inherit from senf::ComplexAnnotation. A type is POD,
710 if it is really just a bunch of bytes: No (non-static) members, no constructor or destructor and
711 no base classes and all it's members must be POD too. So the following annotation is complex
712 since \c std::string is not POD
715 struct ReadInfo : senf::ComplexAnnotation
717 std::string interface;
718 senf::ClockService::clock_t timestamp;
723 packet.annotation<ReadInfo>().interface = "eth0";
724 packet.annotation<ReadInfo>().timestamp = senf::ClockService::now();
726 // Or store a reference to the annotation for easier access
728 ReadInfo & info (packet.annotation<ReadInfo>());
730 if (info.interface == "eth0") {
735 Every annotation is automatically default-initialized, there is no way to query, whether a
736 packet holds a specific annotation -- every packet conceptually always holds all annotations.
738 You should use annotations economically: Every annotation type used in your program will
739 allocate an annotation slot in \e all packet data structures. So don't use hundreds of different
740 annotation types if this is not really necessary: Reuse annotation types where possible or
741 aggregate data into larger annotation structures. The best solution is to use annotations only
742 for a small number of packet specific informations. If you really need to manage a train-load of
743 data together with the packet consider some other way (e.g. place the packet into another class
744 which holds that data).
746 \see senf::Packet::annotation()
749 /** \page packet_new Defining new Packet types
751 Each packet is specified by the following two components:
753 \li A protocol parser which defines the protocol specific fields
754 \li A packet type class which is a policy class defining the packet
758 \see <a href="../../../HowTos/NewPacket/doc/html/index.html">NewPacket HowTo</a>
760 \section packet_new_parser The protocol parser
762 The protocol parser is simply a composite parser. It defines all the protocol
763 fields. Additionally, the protocol parser may have additional members which will then be
764 accessible via the \c -> operator of the packet. Possibilities here are e.g. checksum
765 calculation and validation, packet validation as a whole and so on.
767 Defining a protocol parser is quite simple:
769 struct EthernetPacketParser : public PacketParserBase
771 # include SENF_FIXED_PARSER()
773 SENF_PARSER_FIELD( destination, MACAddressParser );
774 SENF_PARSER_FIELD( source, MACAddressParser );
775 SENF_PARSER_FIELD( type_length, UInt16Parser );
777 SENF_PARSER_FINALIZE(EthernetPacketParser);
781 There are a lot of other possibilities to define fields. See \ref packetparsermacros for a
782 detailed description of the macro language which is used to define composite parsers.
785 \ref packetparsermacros
787 \section packet_new_type The packet type policy class
789 This is a class which provides all the information needed to integrate the new packet type into
792 \li It provides the type of the protocol parser to use
793 \li It provides information on how the next protocol can be found and where the payload resides
795 \li It provides methods to initialize a new packet and get information about the packet size
797 All this information is provided via static or typedef members.
800 struct EthernetPacketType
801 : public PacketTypeBase,
802 public PacketTypeMixin<EthernetPacketType, EtherTypes>
804 typedef PacketTypeMixin<EthernetPacketType, EtherTypes> mixin;
805 typedef ConcretePacket<EthernetPacketType> packet;
806 typedef EthernetPacketParser parser;
808 using mixin::nextPacketRange;
809 using mixin::initSize;
812 static factory_t nextPacketType(packet p);
813 static void dump(packet p, std::ostream & os);
814 static void finalize(packet p);
817 typedef EthernetPacketType::packet EthernetPacket;
820 The definition of senf::EthernetPacket is quite straight forward. This template works for most
823 \see \ref senf::PacketTypeMixin \n
824 \ref senf::PacketTypeBase \n
825 \ref senf::PacketRegistry
832 // c-file-style: "senf"
833 // indent-tabs-mode: nil
834 // ispell-local-dictionary: "american"
836 // compile-command: "scons -u doc"