00001 // $Id: PacketParser.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 // Stefan Bund <g0dil@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 // Custom includes 00027 #include <iterator> 00028 #include "PacketData.hh" 00029 00030 #define prefix_ inline 00031 //-///////////////////////////////////////////////////////////////////////////////////////////////// 00032 00033 //-///////////////////////////////////////////////////////////////////////////////////////////////// 00034 // senf::PacketParserBase 00035 00036 // public members 00037 00038 prefix_ senf::PacketParserBase::data_iterator senf::PacketParserBase::i() 00039 const 00040 { 00041 return i_; 00042 } 00043 00044 // private members 00045 00046 prefix_ senf::PacketParserBase::data_iterator senf::PacketParserBase::end() 00047 const 00048 { 00049 return data_->end(); 00050 } 00051 00052 prefix_ senf::PacketParserBase::ParserProtector::ParserProtector(PacketParserBase const * p) 00053 : safe_i_( *p), parser_(p) 00054 {} 00055 00056 prefix_ senf::PacketParserBase::ParserProtector::ParserProtector(ParserProtector const & other_) 00057 : safe_i_( *other_.parser_), parser_(other_.parser_) 00058 { 00059 other_.parser_ = 0; 00060 } 00061 00062 prefix_ senf::PacketParserBase::ParserProtector::~ParserProtector() 00063 { 00064 if (parser_) const_cast<PacketParserBase *>(parser_)->i_ = safe_i_; 00065 } 00066 00067 // protected members 00068 00069 prefix_ senf::PacketParserBase::ParserProtector senf::PacketParserBase::protect() 00070 const 00071 { 00072 return ParserProtector(this); 00073 } 00074 00075 prefix_ bool senf::PacketParserBase::check(size_type size) 00076 const 00077 { 00078 return size <= size_type(std::distance(i(),end())); 00079 } 00080 00081 prefix_ void senf::PacketParserBase::validate(size_type size) 00082 const 00083 { 00084 if (! check(size)) 00085 throw TruncatedPacketException(); 00086 } 00087 00088 prefix_ senf::PacketParserBase::PacketParserBase(data_iterator i, state_type s) 00089 : i_ (i), data_ (s) 00090 {} 00091 00092 prefix_ senf::PacketParserBase::PacketParserBase(data_iterator i, state_type s, 00093 size_type size) 00094 : i_ (i), data_ (s) 00095 { 00096 validate(size); 00097 } 00098 00099 // public members 00100 00101 prefix_ senf::PacketParserBase::data_iterator senf::PacketParserBase::i(size_type offset) 00102 const 00103 { 00104 validate(offset); 00105 return boost::next(i_, offset); 00106 } 00107 00108 prefix_ senf::PacketParserBase::state_type senf::PacketParserBase::state() 00109 const 00110 { 00111 return data_; 00112 } 00113 00114 prefix_ senf::PacketData & senf::PacketParserBase::data() 00115 const 00116 { 00117 return * data_; 00118 } 00119 00120 prefix_ void senf::PacketParserBase::init() 00121 const 00122 {} 00123 00124 prefix_ void senf::PacketParserBase::defaultInit() 00125 const 00126 {} 00127 00128 prefix_ senf::Packet senf::PacketParserBase::packet() 00129 const 00130 { 00131 // OUCH ... I hate this but for some awkward packet types, access to the packet 00132 // from the parser is really needed (e.g. UDP when building the pseudo-header 00133 // for calculating the checksum). 00134 return Packet(PacketInterpreterBase::ptr(static_cast<PacketInterpreterBase*>(&data()))); 00135 } 00136 00137 //-///////////////////////////////////////////////////////////////////////////////////////////////// 00138 #undef prefix_ 00139 00140 00141 // Local Variables: 00142 // mode: c++ 00143 // fill-column: 100 00144 // c-file-style: "senf" 00145 // indent-tabs-mode: nil 00146 // ispell-local-dictionary: "american" 00147 // compile-command: "scons -u test" 00148 // comment-column: 40 00149 // End: