Search:

SENF Extensible Network Framework

  • Home
  • Download
  • Wiki
  • BerliOS
  • ChangeLog
  • Browse SVN
  • Bug Tracker
  • Overview
  • Examples
  • HowTos
  • Glossary
  • PPI
  • Packets
  • Scheduler
  • Socket
  • Utils
  • Console
  • Daemon
  • Logger
  • Termlib
  • Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

80221Bundle/TLVParser.hh

Go to the documentation of this file.
00001 // $Id: TLVParser.hh 1789 2011-05-19 09:31:33Z tho $
00002 //
00003 // Copyright (C) 2007
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Thorsten Horstmann <tho@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00026 #ifndef HH_SENF_Packets_80221Bundle_TLVParser_
00027 #define HH_SENF_Packets_80221Bundle_TLVParser_ 1
00028 
00029 // Custom includes
00030 #include <senf/Packets/Packets.hh>
00031 #include "MIHTypes.hh"
00032 
00033 //#include "TLVParser.mpp"
00034 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00035 
00036 namespace senf {
00037 
00038     class MIHTLVLengthParser
00039         : public detail::packet::IntParserOps<MIHTLVLengthParser, boost::uint32_t>,
00040           public PacketParserBase
00041     {
00042     public:
00043         MIHTLVLengthParser(data_iterator i, state_type s) : PacketParserBase(i,s) {}
00044 
00045         typedef boost::uint32_t value_type;
00046         static const size_type init_bytes = 1;
00047         static value_type const min_value = 0;
00048         static value_type const max_value = 4294967295u;
00049 
00050         value_type value() const;
00051         void value(value_type const & v);
00052 
00053         MIHTLVLengthParser const & operator= (value_type other);
00054         size_type bytes() const;
00055         void init() const;
00056 
00057 #       include SENF_PARSER()
00058         SENF_PARSER_PRIVATE_FIELD ( length_field, UInt8Parser );
00059         SENF_PARSER_GOTO( length_field );
00060         SENF_PARSER_PRIVATE_BITFIELD ( extended_length_flag, 1,  bool     );
00061         SENF_PARSER_PRIVATE_BITFIELD ( underflow_flag,       1,  bool     );
00062         SENF_PARSER_PRIVATE_BITFIELD ( fixed_length_field,   6,  unsigned );
00063 
00064         void finalize();
00065         void capacity(value_type v);
00066         value_type capacity() const;
00067 
00068     private:
00069         void resize_(size_type size);
00070     };
00071 
00072 
00099     class MIHBaseTLVParser : public PacketParserBase
00100     {
00101     public:
00102 #       include SENF_PARSER()
00103         SENF_PARSER_FIELD    ( type,   UInt8Parser        );
00104         SENF_PARSER_FIELD_RO ( length, MIHTLVLengthParser );
00105         SENF_PARSER_FINALIZE ( MIHBaseTLVParser           );
00106 
00112         void finalize();
00113 
00114         typedef GenericTLVParserRegistry<MIHBaseTLVParser> Registry;
00115 
00116     protected:
00122         void maxLength(MIHTLVLengthParser::value_type maxl) const;
00123 
00124         void validateType(boost::uint8_t type) const;
00125         void validateTypeLength(boost::uint8_t type, MIHTLVLengthParser::value_type length) const;
00126     };
00127 
00128 
00129 
00132     struct MIHGenericTLVParser
00133         : public GenericTLVParserBase<MIHBaseTLVParser>
00134     {
00135         typedef senf::GenericTLVParserBase<MIHBaseTLVParser> base;
00136         MIHGenericTLVParser(data_iterator i, state_type s) : base(i,s) {}
00137 
00138         void init() const {
00139             defaultInit();
00140             maxLength( MIHTLVLengthParser::max_value);
00141         }
00142 
00143         using base::init;
00144         using base::maxLength;
00145     };
00146 
00147 
00150     struct MIHBaseListTLVParser
00151         : public MIHBaseTLVParser
00152     {
00153     #   include SENF_PARSER()
00154         SENF_PARSER_INHERIT  ( MIHBaseTLVParser );
00155         SENF_PARSER_FIELD_RO ( listSize, MIHTLVLengthParser );
00156         SENF_PARSER_FINALIZE ( MIHBaseListTLVParser );
00157 
00158         void maxListSize(MIHTLVLengthParser::value_type maxl) const;
00159     };
00160 
00161     template <class Self>
00162     struct MIHListTLVParserMixin
00163     {
00164         void finalize();
00165     };
00166 
00167 
00179     class MIHFIdTLVParser : public MIHBaseTLVParser
00180     {
00181     #   include SENF_PARSER()
00182         SENF_PARSER_INHERIT  ( MIHBaseTLVParser );
00183         SENF_PARSER_FIELD_RO ( idLength, MIHTLVLengthParser );
00184         SENF_PARSER_LABEL    ( idValue          );
00185         SENF_PARSER_SKIP     ( idLength(), 0    );
00186         SENF_PARSER_FINALIZE ( MIHFIdTLVParser  );
00187 
00188     public:
00190         //\{
00191         void value( MIHFId const & id);
00192 
00193         void value( std::string        const & id  );
00194         void value( senf::MACAddress   const & addr);
00195         void value( senf::INet4Address const & addr);
00196         void value( senf::INet6Address const & addr);
00197         void value( senf::EUI64        const & addr);
00198         //\}
00199 
00201         //\{
00202         MIHFId valueAs( MIHFId::Type type) const;
00203 
00204         std::string        valueAsString()       const;
00205         senf::MACAddress   valueAsMACAddress()   const;
00206         senf::INet4Address valueAsINet4Address() const;
00207         senf::INet6Address valueAsINet6Address() const;
00208         senf::EUI64        valueAsEUI64()        const;
00209         //\}
00210 
00212         //\{
00213         bool valueEquals( MIHFId const & id) const;
00214 
00215         bool valueEquals( std::string        const & id  ) const;
00216         bool valueEquals( senf::MACAddress   const & addr) const;
00217         bool valueEquals( senf::INet4Address const & addr) const;
00218         bool valueEquals( senf::INet6Address const & addr) const;
00219         bool valueEquals( senf::EUI64        const & addr) const;
00220         //\}
00221 
00222         void dump(std::ostream & os) const;
00223         void maxIdLength(boost::uint8_t maxl) const;
00224         void finalize();
00225 
00226     private:
00228         senf::safe_data_iterator resizeValueField(MIHTLVLengthParser::value_type size);
00229 
00230         data_iterator valueBegin() const;
00231         data_iterator valueEnd() const;
00232 
00233         template <class OutputIterator>
00234         struct binaryNAIEncoder {
00235             binaryNAIEncoder(OutputIterator & i);
00236             void operator()(boost::uint8_t v);
00237             OutputIterator & i_;
00238         };
00239 
00240         template <class OutputIterator>
00241         static boost::function_output_iterator<binaryNAIEncoder<OutputIterator> >
00242         getNAIEncodedOutputIterator(OutputIterator i);
00243 
00244         struct binaryNAIDecoder {
00245             binaryNAIDecoder();
00246             bool operator()(boost::uint8_t v);
00247             bool readNextByte_;
00248         };
00249 
00250         template <class Iterator>
00251         static boost::filter_iterator<binaryNAIDecoder, Iterator>
00252         getNAIDecodedIterator(Iterator begin, Iterator end);
00253 
00254         struct ValueSetterVisitor : public boost::static_visitor<> {
00255             MIHFIdTLVParser & parser;
00256             ValueSetterVisitor( MIHFIdTLVParser & p) : parser(p) {}
00257             void operator()( boost::blank ) const {
00258                 parser.value( std::string());
00259             }
00260             template <typename MIHFIdType>
00261             void operator()( MIHFIdType const & id ) const {
00262                 parser.value( id);
00263             }
00264         };
00265 
00266         struct ValueEqualsVisitor : public boost::static_visitor<bool> {
00267             MIHFIdTLVParser const & parser;
00268             ValueEqualsVisitor( MIHFIdTLVParser const & p) : parser(p) {}
00269             bool operator()( boost::blank ) const {
00270                 return parser.idLength() == 0;
00271             }
00272             template <typename MIHFIdType>
00273             bool operator()( MIHFIdType const & id ) const {
00274                 return parser.valueEquals( id);
00275             }
00276         };
00277     };
00278 
00281     struct MIHFSrcIdTLVParser : public MIHFIdTLVParser
00282     {
00283         MIHFSrcIdTLVParser(data_iterator i, state_type s) : MIHFIdTLVParser(i,s) {}
00284 
00285         void init() const {
00286             defaultInit();
00287             type() << typeId+0;
00288         }
00289         static type_t::value_type const typeId = 1;
00290         void dump(std::ostream & os) const;
00291         void validate() const;
00292     };
00293 
00296     struct MIHFDstIdTLVParser : public MIHFIdTLVParser
00297     {
00298         MIHFDstIdTLVParser(data_iterator i, state_type s) : MIHFIdTLVParser(i,s) {}
00299 
00300         void init() const {
00301             defaultInit();
00302             type() << typeId+0;
00303         }
00304         static type_t::value_type const typeId = 2;
00305         void dump(std::ostream & os) const;
00306         void validate() const;
00307     };
00308 
00311     struct MIHStatusTLVParser : public MIHBaseTLVParser
00312     {
00313     #   include SENF_PARSER()
00314         SENF_PARSER_INHERIT ( MIHBaseTLVParser   );
00315         SENF_PARSER_FIELD   ( value, UInt8Parser );
00316         SENF_PARSER_FINALIZE( MIHStatusTLVParser );
00317 
00318         SENF_PARSER_INIT() {
00319             defaultInit();
00320             type() << typeId+0;
00321             length_() = 1;
00322         }
00323         static type_t::value_type const typeId = 3;
00324         void dump(std::ostream & os) const; 
00325         void validate() const;
00326 
00327         enum StatusCode {
00328             Success, UnspecifiedFailure, Rejected, AuthorizationFailure, NetworkError };
00329     };
00330 
00331     struct MIHRegisterReqCodeTLVParser : public MIHBaseTLVParser
00332     {
00333     #   include SENF_PARSER()
00334         SENF_PARSER_INHERIT  ( MIHBaseTLVParser );
00335         SENF_PARSER_FIELD    ( value, UInt8Parser );
00336         SENF_PARSER_FINALIZE ( MIHRegisterReqCodeTLVParser );
00337 
00338         SENF_PARSER_INIT() {
00339             defaultInit();
00340             type() = typeId+0;
00341             length_() = 1;
00342         }
00343         static type_t::value_type const typeId = 11;
00344         void dump(std::ostream & os) const; 
00345         void validate() const;
00346 
00347         enum RequestCode { Registration, ReRegistration };
00348     };
00349 
00350     struct MIHValidTimeIntervalTLVParser : public MIHBaseTLVParser
00351     {
00352     #   include SENF_PARSER()
00353         SENF_PARSER_INHERIT  ( MIHBaseTLVParser );
00354         SENF_PARSER_FIELD    ( value, UInt32Parser );
00355         SENF_PARSER_FINALIZE ( MIHValidTimeIntervalTLVParser );
00356 
00357         SENF_PARSER_INIT() {
00358             defaultInit();
00359             type() = typeId+0;
00360             length_() = 4;
00361         }
00362         static type_t::value_type const typeId = 12;
00363         void dump(std::ostream & os) const; 
00364         void validate() const;
00365     };
00366 
00367 }
00368 
00369 
00370 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00371 #include "TLVParser.cci"
00372 #include "TLVParser.ct"
00373 #include "TLVParser.cti"
00374 #endif
00375 
00376 
00377 // Local Variables:
00378 // mode: c++
00379 // fill-column: 100
00380 // c-file-style: "senf"
00381 // indent-tabs-mode: nil
00382 // ispell-local-dictionary: "american"
00383 // compile-command: "scons -u test"
00384 // comment-column: 40
00385 // End:

Contact: senf-dev@lists.berlios.de | © 2006-2010 Fraunhofer Institute for Open Communication Systems, Network Research