Packets/80221Bundle: renamed MIHTLVLengthParser to MIHLengthParser
[senf.git] / senf / Packets / 80221Bundle / TLVParser.cci
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Thorsten Horstmann <tho@berlios.de>
27
28 /** \file
29     \brief TLVParser inline non-template implementation */
30
31 //#include "TLVParser.ih"
32
33 // Custom includes
34
35 #define prefix_ inline
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
37
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39 // MIHLengthParser
40
41 prefix_ senf::MIHLengthParser::size_type senf::MIHLengthParser::bytes() const
42 {
43     return 1 + ( length_field()<=128 ? 0 : fixed_length_field());
44 }
45
46 //-/////////////////////////////////////////////////////////////////////////////////////////////////
47 // MIHFIdTLVParser::binaryNAIDecoder
48
49 prefix_ senf::MIHFIdTLVParser::binaryNAIDecoder::binaryNAIDecoder()
50     : readNextByte_( true)
51 {}
52
53 prefix_ bool senf::MIHFIdTLVParser::binaryNAIDecoder::operator()(boost::uint8_t v)
54 {
55     readNextByte_ = readNextByte_ ? false : true;
56     return readNextByte_;
57 }
58
59 //-/////////////////////////////////////////////////////////////////////////////////////////////////
60 // MIHFIdTLVParser
61
62 prefix_ senf::PacketParserBase::data_iterator senf::MIHFIdTLVParser::valueBegin()
63     const
64 {
65     return i( idValue_offset());
66 }
67
68 prefix_ senf::PacketParserBase::data_iterator senf::MIHFIdTLVParser::valueEnd()
69     const
70 {
71     return i( idValue_offset() + idLength());
72 }
73
74 prefix_ std::string senf::MIHFIdTLVParser::valueAsString()
75     const
76 {
77     return std::string( valueBegin(), valueEnd());
78 }
79
80 prefix_ senf::MACAddress senf::MIHFIdTLVParser::valueAsMACAddress()
81     const
82 {
83     if (idLength() != 6*2) throw WrapException<std::bad_cast>(std::bad_cast())
84         << "length of MIHF_ID does not match for a MAC address";
85     return MACAddress::from_data( getNAIDecodedIterator( valueBegin(), valueEnd()));
86 }
87
88 prefix_ senf::INet4Address senf::MIHFIdTLVParser::valueAsINet4Address()
89     const
90 {
91     if (idLength() != 4*2) throw WrapException<std::bad_cast>(std::bad_cast())
92         << "length of MIHF_ID does not match for a INet4 address";
93     return INet4Address::from_data( getNAIDecodedIterator( valueBegin(), valueEnd()));
94 }
95
96 prefix_ senf::INet6Address senf::MIHFIdTLVParser::valueAsINet6Address()
97     const
98 {
99     if (idLength() != 16*2) throw WrapException<std::bad_cast>(std::bad_cast())
100         << "length of MIHF_ID does not match for a INet6 address";
101     return INet6Address::from_data( getNAIDecodedIterator( valueBegin(), valueEnd()));
102 }
103
104 prefix_ senf::EUI64 senf::MIHFIdTLVParser::valueAsEUI64()
105     const
106 {
107     if (idLength() != 8*2) throw WrapException<std::bad_cast>(std::bad_cast())
108         << "length of MIHF_ID does not match for a EUI64 address";
109     return EUI64::from_data( getNAIDecodedIterator( valueBegin(), valueEnd()));
110 }
111
112 prefix_ bool senf::MIHFIdTLVParser::valueEquals( std::string const &id)
113     const
114 {
115     return id == valueAsString();
116 }
117
118 prefix_ bool senf::MIHFIdTLVParser::valueEquals( senf::MACAddress const & addr)
119     const
120 {
121     return idLength()==12 && addr==valueAsMACAddress();
122 }
123
124 prefix_ bool senf::MIHFIdTLVParser::valueEquals( senf::INet4Address const & addr)
125     const
126 {
127     return idLength()==8 && addr==valueAsINet4Address();
128 }
129
130 prefix_ bool senf::MIHFIdTLVParser::valueEquals( senf::INet6Address const & addr)
131     const
132 {
133     return idLength()==32 && addr==valueAsINet6Address();
134 }
135
136 prefix_ bool senf::MIHFIdTLVParser::valueEquals( senf::EUI64 const & addr)
137     const
138 {
139     return idLength()==16 && addr==valueAsEUI64();
140 }
141
142 prefix_ bool senf::MIHFIdTLVParser::valueEquals( MIHFId const & id)
143     const
144 {
145     return boost::apply_visitor( ValueEqualsVisitor(*this), id);
146 }
147
148
149 //-/////////////////////////////////////////////////////////////////////////////////////////////////
150 // MIHBaseTLVParser
151
152 prefix_ void senf::MIHBaseTLVParser::maxLength(MIHLengthParser::value_type maxl)
153     const
154 {
155     protect(), length_().capacity(maxl);
156 }
157
158 prefix_ void senf::MIHBaseTLVParser::finalize()
159 {
160     protect(), length_().finalize();
161 };
162
163
164 //-/////////////////////////////////////////////////////////////////////////////////////////////////
165 #undef prefix_
166
167 \f
168 // Local Variables:
169 // mode: c++
170 // fill-column: 100
171 // c-file-style: "senf"
172 // indent-tabs-mode: nil
173 // ispell-local-dictionary: "american"
174 // compile-command: "scons -u test"
175 // comment-column: 40
176 // End: