00001 // $Id: TLVParser.cci 1742 2010-11-04 14:51:56Z g0dil $ 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 //#include "TLVParser.ih" 00027 00028 // Custom includes 00029 00030 #define prefix_ inline 00031 //-///////////////////////////////////////////////////////////////////////////////////////////////// 00032 00033 //-///////////////////////////////////////////////////////////////////////////////////////////////// 00034 // MIHTLVLengthParser 00035 00036 prefix_ senf::MIHTLVLengthParser::size_type senf::MIHTLVLengthParser::bytes() const 00037 { 00038 return 1 + ( length_field()<=128 ? 0 : fixed_length_field()); 00039 } 00040 00041 //-///////////////////////////////////////////////////////////////////////////////////////////////// 00042 // MIHFIdTLVParser::binaryNAIDecoder 00043 00044 prefix_ senf::MIHFIdTLVParser::binaryNAIDecoder::binaryNAIDecoder() 00045 : readNextByte_( true) 00046 {} 00047 00048 prefix_ bool senf::MIHFIdTLVParser::binaryNAIDecoder::operator()(boost::uint8_t v) 00049 { 00050 readNextByte_ = readNextByte_ ? false : true; 00051 return readNextByte_; 00052 } 00053 00054 //-///////////////////////////////////////////////////////////////////////////////////////////////// 00055 // MIHFIdTLVParser 00056 00057 prefix_ senf::PacketParserBase::data_iterator senf::MIHFIdTLVParser::valueBegin() 00058 const 00059 { 00060 return i( idValue_offset()); 00061 } 00062 00063 prefix_ senf::PacketParserBase::data_iterator senf::MIHFIdTLVParser::valueEnd() 00064 const 00065 { 00066 return i( idValue_offset() + idLength()); 00067 } 00068 00069 prefix_ std::string senf::MIHFIdTLVParser::valueAsString() 00070 const 00071 { 00072 return std::string( valueBegin(), valueEnd()); 00073 } 00074 00075 prefix_ senf::MACAddress senf::MIHFIdTLVParser::valueAsMACAddress() 00076 const 00077 { 00078 if (idLength() != 6*2) throw WrapException<std::bad_cast>(std::bad_cast()) 00079 << "length of MIHF_ID does not match for a MAC address"; 00080 return MACAddress::from_data( getNAIDecodedIterator( valueBegin(), valueEnd())); 00081 } 00082 00083 prefix_ senf::INet4Address senf::MIHFIdTLVParser::valueAsINet4Address() 00084 const 00085 { 00086 if (idLength() != 4*2) throw WrapException<std::bad_cast>(std::bad_cast()) 00087 << "length of MIHF_ID does not match for a INet4 address"; 00088 return INet4Address::from_data( getNAIDecodedIterator( valueBegin(), valueEnd())); 00089 } 00090 00091 prefix_ senf::INet6Address senf::MIHFIdTLVParser::valueAsINet6Address() 00092 const 00093 { 00094 if (idLength() != 16*2) throw WrapException<std::bad_cast>(std::bad_cast()) 00095 << "length of MIHF_ID does not match for a INet6 address"; 00096 return INet6Address::from_data( getNAIDecodedIterator( valueBegin(), valueEnd())); 00097 } 00098 00099 prefix_ senf::EUI64 senf::MIHFIdTLVParser::valueAsEUI64() 00100 const 00101 { 00102 if (idLength() != 8*2) throw WrapException<std::bad_cast>(std::bad_cast()) 00103 << "length of MIHF_ID does not match for a EUI64 address"; 00104 return EUI64::from_data( getNAIDecodedIterator( valueBegin(), valueEnd())); 00105 } 00106 00107 prefix_ bool senf::MIHFIdTLVParser::valueEquals( std::string const &id) 00108 const 00109 { 00110 return id == valueAsString(); 00111 } 00112 00113 prefix_ bool senf::MIHFIdTLVParser::valueEquals( senf::MACAddress const & addr) 00114 const 00115 { 00116 return idLength()==12 && addr==valueAsMACAddress(); 00117 } 00118 00119 prefix_ bool senf::MIHFIdTLVParser::valueEquals( senf::INet4Address const & addr) 00120 const 00121 { 00122 return idLength()==8 && addr==valueAsINet4Address(); 00123 } 00124 00125 prefix_ bool senf::MIHFIdTLVParser::valueEquals( senf::INet6Address const & addr) 00126 const 00127 { 00128 return idLength()==32 && addr==valueAsINet6Address(); 00129 } 00130 00131 prefix_ bool senf::MIHFIdTLVParser::valueEquals( senf::EUI64 const & addr) 00132 const 00133 { 00134 return idLength()==16 && addr==valueAsEUI64(); 00135 } 00136 00137 prefix_ bool senf::MIHFIdTLVParser::valueEquals( MIHFId const & id) 00138 const 00139 { 00140 return boost::apply_visitor( ValueEqualsVisitor(*this), id); 00141 } 00142 00143 00144 //-///////////////////////////////////////////////////////////////////////////////////////////////// 00145 // MIHBaseTLVParser 00146 00147 prefix_ void senf::MIHBaseTLVParser::maxLength(MIHTLVLengthParser::value_type maxl) 00148 const 00149 { 00150 protect(), length_().capacity(maxl); 00151 } 00152 00153 prefix_ void senf::MIHBaseTLVParser::finalize() 00154 { 00155 protect(), length_().finalize(); 00156 }; 00157 00158 00159 //-///////////////////////////////////////////////////////////////////////////////////////////////// 00160 #undef prefix_ 00161 00162 00163 // Local Variables: 00164 // mode: c++ 00165 // fill-column: 100 00166 // c-file-style: "senf" 00167 // indent-tabs-mode: nil 00168 // ispell-local-dictionary: "american" 00169 // compile-command: "scons -u test" 00170 // comment-column: 40 00171 // End: