switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Packets / PacketParser.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 //   Stefan Bund <g0dil@berlios.de>
27
28 /** \file
29     \brief PacketParser inline non-template implementation */
30
31 // Custom includes
32 #include <iterator>
33 #include "PacketData.hh"
34
35 #define prefix_ inline
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
37
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39 // senf::PacketParserBase
40
41 // public members
42
43 prefix_ senf::PacketParserBase::data_iterator senf::PacketParserBase::i()
44     const
45 {
46     return i_;
47 }
48
49 // private members
50
51 prefix_ senf::PacketParserBase::data_iterator senf::PacketParserBase::end()
52     const
53 {
54     return data_->end();
55 }
56
57 prefix_ senf::PacketParserBase::ParserProtector::ParserProtector(PacketParserBase const * p)
58     : safe_i_( *p), parser_(p)
59 {}
60
61 prefix_ senf::PacketParserBase::ParserProtector::ParserProtector(ParserProtector const & other_)
62     : safe_i_( *other_.parser_), parser_(other_.parser_)
63 {
64     other_.parser_ = 0;
65 }
66
67 prefix_ senf::PacketParserBase::ParserProtector::~ParserProtector()
68 {
69     if (parser_) const_cast<PacketParserBase *>(parser_)->i_ = safe_i_;
70 }
71
72 // protected members
73
74 prefix_ senf::PacketParserBase::ParserProtector senf::PacketParserBase::protect()
75     const
76 {
77     return ParserProtector(this);
78 }
79
80 prefix_ bool senf::PacketParserBase::check(size_type size)
81     const
82 {
83     return size <= size_type(std::distance(i(),end()));
84 }
85
86 prefix_ void senf::PacketParserBase::validate(size_type size)
87    const
88 {
89     if (! check(size))
90         throw TruncatedPacketException();
91 }
92
93 prefix_ senf::PacketParserBase::PacketParserBase(data_iterator i, state_type s)
94     : i_ (i), data_ (s)
95 {}
96
97 prefix_ senf::PacketParserBase::PacketParserBase(data_iterator i, state_type s,
98                                                  size_type size)
99     : i_ (i), data_ (s)
100 {
101     validate(size);
102 }
103
104 // public members
105
106 prefix_ senf::PacketParserBase::data_iterator senf::PacketParserBase::i(size_type offset)
107     const
108 {
109     validate(offset);
110     return boost::next(i_, offset);
111 }
112
113 prefix_ senf::PacketParserBase::state_type senf::PacketParserBase::state()
114     const
115 {
116     return data_;
117 }
118
119 prefix_ senf::PacketData & senf::PacketParserBase::data()
120     const
121 {
122     return * data_;
123 }
124
125 prefix_ void senf::PacketParserBase::init()
126     const
127 {}
128
129 prefix_ void senf::PacketParserBase::defaultInit()
130     const
131 {}
132
133 prefix_ senf::Packet senf::PacketParserBase::packet()
134     const
135 {
136     // OUCH ... I hate this but for some awkward packet types, access to the packet
137     // from the parser is really needed (e.g. UDP when building the pseudo-header
138     // for calculating the checksum).
139     return Packet(PacketInterpreterBase::ptr(static_cast<PacketInterpreterBase*>(&data())));
140 }
141
142 //-/////////////////////////////////////////////////////////////////////////////////////////////////
143 #undef prefix_
144
145 \f
146 // Local Variables:
147 // mode: c++
148 // fill-column: 100
149 // c-file-style: "senf"
150 // indent-tabs-mode: nil
151 // ispell-local-dictionary: "american"
152 // compile-command: "scons -u test"
153 // comment-column: 40
154 // End: