Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

TelnetParser.h

00001 #ifndef _STR_TelnetParser_H_  // -*-c++-*-
00002 
00003 /*
00004  * Copyright 2001-2002 by Eric M. Hopper <hopper@omnifarious.org>
00005  * 
00006  *     This program is free software; you can redistribute it and/or modify it
00007  *     under the terms of the GNU Lesser General Public License as published
00008  *     by the Free Software Foundation; either version 2 of the License, or
00009  *     (at your option) any later version.
00010  * 
00011  *     This program is distributed in the hope that it will be useful, but
00012  *     WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  *     Lesser General Public License for more details.
00015  * 
00016  *     You should have received a copy of the GNU Lesser General Public
00017  *     License along with this program; if not, write to the Free Software
00018  *     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00019  */
00020 
00021 #ifdef __GNUG__
00022 #  pragma interface
00023 #endif
00024 
00025 /* $Header: /home/hopper/src/cvs/C++/StrMod/StrMod/TelnetParser.h,v 1.11 2002/11/25 05:40:05 hopper Exp $ */
00026 
00027 // For a log, see ../ChangeLog
00028 
00029 #include <cstddef>
00030 #include <LCore/Protocol.h>
00031 #include <LCore/HopClTypes.h>
00032 #include <StrMod/TelnetChars.h>
00033 #include <StrMod/STR_ClassIdent.h>
00034 
00035 #define _STR_TelnetParser_H_
00036 
00037 namespace strmod {
00038 namespace strmod {
00039 
00040 class TelnetChunkBuilder;
00041 template <unsigned int n> class PreAllocBuffer;
00042 
00043 /** \class TelnetParser TelnetParser.h StrMod/TelnetParser.h
00044  * Class for parsing out a stream of characters into telnet protocol
00045  * elements using the TelnetChunkBuilder class.
00046  */
00047 class TelnetParser : virtual public lcore::Protocol
00048 {
00049  public:
00050    static const STR_ClassIdent identifier;
00051 
00052    //! Construct a parser in the 'start' state.
00053    TelnetParser();
00054    //! Destroy a parser.
00055    virtual ~TelnetParser();
00056 
00057    inline virtual int AreYouA(const lcore::ClassIdent &cid) const;
00058 
00059    //! Process a buffer, calling the builder, and advancing the state.
00060    void processData(const void *data, size_t len,
00061                             TelnetChunkBuilder &builder);
00062 
00063    //! Stuff any pending data chunks into the builder.
00064    void endOfChunk(TelnetChunkBuilder &builder);
00065 
00066    /** \brief Return number of bytes to the beginning of the current region that
00067     * TelnetParser is not finished parsing yet.
00068    */
00069    size_t getRegionBegin() const                        { return regionbegin_; }
00070 
00071    /** \brief Move region as if <code>bytes</code> bytes had been chopped off
00072     * the front.
00073     */
00074    inline void dropBytes(size_t bytes);
00075 
00076    //! Reset the parser back to the 'start' state.
00077    void reset(TelnetChunkBuilder &builder);
00078 
00079  protected:
00080    /** Describes the current state of the parser.
00081     *
00082     * This state is completely determined by the characters fed into
00083     * processChar().
00084     */
00085    enum ParserState {
00086       PS_Normal,  //!< Starting state.  Normal data.
00087       PS_Escape,  //!< Saw an IAC character in normal data
00088       PS_SubNeg,  //!< Saw an IAC {WILL,WONT,DO,DONT}, expecting option number
00089       PS_SuboptNum, //!< Saw an IAC SE and expecting an option number
00090       PS_Subopt,  //!< Between IAC SE &lt;num> and IAC SE
00091       PS_SuboptEscape  //!< Saw an IAC while in PS_Subopt
00092    };
00093 
00094    virtual const lcore::ClassIdent *i_GetIdent() const  { return &identifier; }
00095 
00096  private:
00097    typedef lcore::U1Byte U1Byte;
00098    TelnetChars::OptionNegotiations negtype_;
00099    U1Byte subopt_type_;
00100    size_t curpos_;
00101    ParserState state_;
00102    size_t regionbegin_;
00103    size_t regionend_;
00104    U1Byte *cookedbuf_;
00105    size_t cooked_total_;
00106    size_t cooked_used_;
00107    PreAllocBuffer<48> *cooked_;
00108 
00109    inline void stateNormal(ParserState &state, const U1Byte ch);
00110    inline void stateEscape(ParserState &state, const U1Byte ch, size_t i,
00111                            TelnetChunkBuilder &builder);
00112    inline void stateSubNeg(ParserState &state, const U1Byte ch, size_t i,
00113                            TelnetChunkBuilder &builder);
00114    inline void stateSuboptNum(ParserState &state, const U1Byte ch, size_t i,
00115                               TelnetChunkBuilder &builder);
00116    inline void stateSubopt(ParserState &state, U1Byte ch);
00117    inline void stateSuboptEscape(ParserState &state, U1Byte ch, size_t i,
00118                                  TelnetChunkBuilder &builder);
00119 };
00120 
00121 //-----------------------------inline functions--------------------------------
00122 
00123 inline int TelnetParser::AreYouA(const lcore::ClassIdent &cid) const
00124 {
00125    return((identifier == cid) || Protocol::AreYouA(cid));
00126 }
00127 
00128 inline void TelnetParser::dropBytes(size_t bytes)
00129 {
00130    if (bytes <= regionbegin_)
00131    {
00132       regionbegin_ -= bytes;
00133       curpos_ -= bytes;
00134    }
00135 }
00136 
00137 };  // namespace strmod
00138 };  // namespace strmod
00139 
00140 #endif

Generated on Wed Jan 29 00:32:45 2003 for libNet by doxygen1.3-rc1