X-Git-Url: http://g0dil.de/git?a=blobdiff_plain;f=Packets%2FDefaultBundle%2FIpV4Packet.hh;h=5b45f698932bda8620c9a12f7f910aa6550c3dba;hb=6a3a31fb7b2d2a5e8ae6d67d50797700274fb34e;hp=e8879a2e1129af6da3afb6cf22e750e36340f969;hpb=64e170521a221effc1ba11a0544f753544d03bac;p=senf.git diff --git a/Packets/DefaultBundle/IpV4Packet.hh b/Packets/DefaultBundle/IpV4Packet.hh index e8879a2..5b45f69 100644 --- a/Packets/DefaultBundle/IpV4Packet.hh +++ b/Packets/DefaultBundle/IpV4Packet.hh @@ -1,4 +1,4 @@ -// $Id: IpV4Packet.hh 307 2007-07-14 21:31:12Z g0dil $ +// $Id$ // // Copyright (C) 2006 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS) @@ -20,74 +20,126 @@ // Free Software Foundation, Inc., // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +/** \file + \brief IpV4Packet public header */ + #ifndef HH_IpV4Packet_ #define HH_IpV4Packet_ 1 // Custom includes -#include "Packets/PacketType.hh" -#include "Packets/ParseInt.hh" -#include "Packets/PacketRegistry.hh" -#include "Packets/PacketParser.hh" +#include "../../Socket/Protocols/INet/INet4Address.hh" +#include "../../Packets/Packets.hh" //#include "IpV4Packet.mpp" ///////////////////////////////hh.p//////////////////////////////////////// namespace senf { - ///\addtogroup protocolbundle_default - ///@{ + /** \brief Parse in IpV4 address - struct Parse_IpV4 : public PacketParserBase + \see INet4Address + */ + struct Parse_INet4Address : public PacketParserBase { - SENF_PACKET_PARSER_NO_INIT(Parse_IpV4); + Parse_INet4Address(data_iterator i, state_type s) : PacketParserBase(i,s,fixed_bytes) {} /////////////////////////////////////////////////////////////////////////// - typedef Parse_UIntField < 0, 4 > Parse_Version; - typedef Parse_UIntField < 4, 8 > Parse_IHL; - typedef Parse_UInt8 Parse_8bit; - typedef Parse_UInt16 Parse_16bit; - typedef Parse_Flag < 0 > Parse_R; - typedef Parse_Flag < 1 > Parse_DF; - typedef Parse_Flag < 2 > Parse_MF; - typedef Parse_UIntField < 3, 16 > Parse_Frag; - typedef Parse_UInt32 Parse_32bit; - - SENF_PACKET_PARSER_DEFINE_FIXED_FIELDS( - ((OverlayField)( version, Parse_Version )) - ((Field )( ihl, Parse_IHL )) - ((Field )( tos, Parse_8bit )) - ((Field )( length, Parse_16bit )) - ((Field )( identifier, Parse_16bit )) - ((OverlayField)( reserved, Parse_R )) - ((OverlayField)( df, Parse_DF )) - ((OverlayField)( mf, Parse_MF )) - ((Field )( frag, Parse_Frag )) - ((Field )( ttl, Parse_8bit )) - ((Field )( protocol, Parse_8bit )) - ((Field )( crc, Parse_16bit )) - ((Field )( source, Parse_32bit )) - ((Field )( destination, Parse_32bit )) ); - - void init() { + typedef INet4Address value_type; + static const size_type fixed_bytes = 4u; + + value_type value() const { return value_type::from_data(i()); } + void value(value_type const & v) { std::copy(v.begin(), v.end(), i()); } + operator value_type() { return value(); } + byte & operator[](size_type index) { return *boost::next(i(),index); } + Parse_INet4Address const & operator= (value_type const & other) + { value(other); return *this; } + }; + + /** \brief Parse an IpV4 packet + + Parser implementing the IpV4 header. The fields implemented are: + + \see IpV4PacketType \n + RFC 791 + + \todo Implement options + */ + struct Parse_IpV4 : public PacketParserBase + { +# include SENF_FIXED_PARSER() + + SENF_PARSER_BITFIELD( version, 4, unsigned ); + SENF_PARSER_BITFIELD( ihl, 4, unsigned ); + + SENF_PARSER_FIELD( tos, Parse_UInt8 ); + SENF_PARSER_FIELD( length, Parse_UInt16 ); + SENF_PARSER_FIELD( identifier, Parse_UInt16 ); + + SENF_PARSER_BITFIELD( reserved, 1, bool ); + SENF_PARSER_BITFIELD( df, 1, bool ); + SENF_PARSER_BITFIELD( mf, 1, bool ); + SENF_PARSER_BITFIELD( frag, 13, unsigned ); + + SENF_PARSER_FIELD( ttl, Parse_UInt8 ); + SENF_PARSER_FIELD( protocol, Parse_UInt8 ); + SENF_PARSER_FIELD( checksum, Parse_UInt16 ); + SENF_PARSER_FIELD( source, Parse_INet4Address ); + SENF_PARSER_FIELD( destination, Parse_INet4Address ); + + SENF_PARSER_INIT() { version() = 4; + // We don't support option headers at the moment ... + ihl() = 5; + } + + SENF_PARSER_FINALIZE(Parse_IpV4); + + boost::uint16_t calcChecksum() const; + + bool validateChecksum() const { + return checksum() == calcChecksum(); } }; + /** \brief IP protocol number registry + + This registeres packets with their IP protocol number. + + \see Protocol numbers \n + PacketRegistry + */ struct IpTypes { - // See http://www.iana.org/assignments/protocol-numbers - // Also used by IPv6 typedef boost::uint16_t key_t; }; + /** \brief IpV4 packet + + \par Packet type (typedef): + \ref IpV4Packet + + \par Fields: + \ref Parse_IpV4 + + \par Associated registries: + \ref IpTypes + + \par Finalize action: + Set \a length from payload size\n + Set \a protocol from type of next packet if found in \ref IpTypes\n + Calculate \a checksum + + \ingroup protocolbundle_default + */ struct IpV4PacketType : public PacketTypeBase, public PacketTypeMixin { +#ifndef DOXYGEN typedef PacketTypeMixin mixin; typedef ConcretePacket packet; typedef Parse_IpV4 parser; - +#endif using mixin::nextPacketRange; using mixin::nextPacketType; using mixin::initSize; @@ -97,15 +149,17 @@ namespace senf { { return p->protocol(); } static void dump(packet p, std::ostream & os); + static void finalize(packet p); }; - typedef IpV4PacketType::packet IpV4Packet; - - ///@} + /** \brief IpV4 packet typedef */ + typedef ConcretePacket IpV4Packet; } ///////////////////////////////hh.e//////////////////////////////////////// +#endif +#ifndef SENF_PACKETS_DECL_ONLY //#include IpV4Packet.cci" //#include "IpV4Packet.ct" //#include "IpV4Packet.cti"