ee18f427b99ad71f3012890ce8dd1b5a65f45f0d
[senf.git] / senf / Packets / PacketParser.cci
1 // $Id$
2 //
3 // Copyright (C) 2007
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief PacketParser inline non-template implementation */
25
26 // Custom includes
27 #include <iterator>
28 #include "PacketData.hh"
29
30 #define prefix_ inline
31 //-/////////////////////////////////////////////////////////////////////////////////////////////////
32
33 //-/////////////////////////////////////////////////////////////////////////////////////////////////
34 // senf::PacketParserBase
35
36 // public members
37
38 prefix_ senf::PacketParserBase::data_iterator senf::PacketParserBase::i()
39     const
40 {
41     return i_;
42 }
43
44 // private members
45
46 prefix_ senf::PacketParserBase::data_iterator senf::PacketParserBase::end()
47     const
48 {
49     return data_->end();
50 }
51
52 prefix_ senf::PacketParserBase::ParserProtector::ParserProtector(PacketParserBase const * p)
53     : safe_i_( *p), parser_(p)
54 {}
55
56 prefix_ senf::PacketParserBase::ParserProtector::ParserProtector(ParserProtector const & other_)
57     : safe_i_( *other_.parser_), parser_(other_.parser_)
58 {
59     other_.parser_ = 0;
60 }
61
62 prefix_ senf::PacketParserBase::ParserProtector::~ParserProtector()
63 {
64     if (parser_) const_cast<PacketParserBase *>(parser_)->i_ = safe_i_;
65 }
66
67 // protected members
68
69 prefix_ senf::PacketParserBase::ParserProtector senf::PacketParserBase::protect()
70     const
71 {
72     return ParserProtector(this);
73 }
74
75 prefix_ bool senf::PacketParserBase::check(size_type size)
76     const
77 {
78     return size <= size_type(std::distance(i(),end()));
79 }
80
81 prefix_ void senf::PacketParserBase::validate(size_type size)
82    const
83 {
84     if (! check(size))
85         throw TruncatedPacketException();
86 }
87
88 prefix_ senf::PacketParserBase::PacketParserBase(data_iterator i, state_type s)
89     : i_ (i), data_ (s)
90 {}
91
92 prefix_ senf::PacketParserBase::PacketParserBase(data_iterator i, state_type s,
93                                                  size_type size)
94     : i_ (i), data_ (s)
95 {
96     validate(size);
97 }
98
99 // public members
100
101 prefix_ senf::PacketParserBase::data_iterator senf::PacketParserBase::i(size_type offset)
102     const
103 {
104     validate(offset);
105     return boost::next(i_, offset);
106 }
107
108 prefix_ senf::PacketParserBase::state_type senf::PacketParserBase::state()
109     const
110 {
111     return data_;
112 }
113
114 prefix_ senf::PacketData & senf::PacketParserBase::data()
115     const
116 {
117     return * data_;
118 }
119
120 prefix_ void senf::PacketParserBase::init()
121     const
122 {}
123
124 prefix_ void senf::PacketParserBase::defaultInit()
125     const
126 {}
127
128 prefix_ senf::Packet senf::PacketParserBase::packet()
129     const
130 {
131     // OUCH ... I hate this but for some awkward packet types, access to the packet
132     // from the parser is really needed (e.g. UDP when building the pseudo-header
133     // for calculating the checksum).
134     return Packet(PacketInterpreterBase::ptr(static_cast<PacketInterpreterBase*>(&data())));
135 }
136
137 //-/////////////////////////////////////////////////////////////////////////////////////////////////
138 #undef prefix_
139
140 \f
141 // Local Variables:
142 // mode: c++
143 // fill-column: 100
144 // c-file-style: "senf"
145 // indent-tabs-mode: nil
146 // ispell-local-dictionary: "american"
147 // compile-command: "scons -u test"
148 // comment-column: 40
149 // End: