libshevek
src/telnet.hh
00001 /* telnet.hh - implementation of the telnet protocol
00002  * Copyright 2003 Bas Wijnen <wijnen@debian.org>
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License as published by
00006  * the Free Software Foundation, either version 3 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #ifndef SHEVEK_TELNET_HH
00019 #define SHEVEK_TELNET_HH
00020 
00021 #include "socket.hh"
00022 
00023 namespace shevek
00024 {
00026   class telnet : public socket
00027   {
00028     // These are ideal for an enum, in a sense, but the values are really char constants, and C++ doesn't allow that for enums. So the choice is to cast the enum value to a char, or to not use an enum. I don't like casts.
00029     // Command constants
00030     static char const SE = '\xf0';
00031     static char const NOP = '\xf1';
00032     static char const MARK = '\xf2';
00033     static char const BREAK = '\xf3';
00034     static char const IP = '\xf4';
00035     static char const AO = '\xf5';
00036     static char const AYT = '\xf6';
00037     static char const EC = '\xf7';
00038     static char const EL = '\xf8';
00039     static char const GA = '\xf9';
00040     static char const SB = '\xfa';
00041     static char const WILL = '\xfb';
00042     static char const WONT = '\xfc';
00043     static char const DO = '\xfd';
00044     static char const DONT = '\xfe';
00045     static char const IAC = '\xff';
00046     // Option constants.
00047     static char const BINARY = '\x00';
00048     static char const ECHO = '\x01';
00049     static char const SUPPRESS_GA = '\x03';
00050     static char const STATUS = '\x05';
00051     static char const TIMING_MARK = '\x06';
00052     static char const EXOPL = '\xff';
00053     enum option_idx {
00054       BINARY_IDX = 0,
00055       ECHO_IDX = 1,
00056       SUPPRESS_GA_IDX = 2,
00057       STATUS_IDX = 3,
00058       TIMING_MARK_IDX = 4,
00059       EXOPL_IDX = 5
00060     };      
00061     struct option_t;
00062     typedef void (telnet::*action)(option_t *opt);
00063     struct option_t
00064     {
00065       char type;
00066       action will, wont, doo, dont;
00067       bool here, there, not_both;
00068     };
00069     static option_t options[6];
00070     option_t nop_option;
00071     option_t *s_find (char opt);
00072     void nop (option_t *);
00073     void nopwill (option_t *opt);
00074     void nopdo (option_t *opt);
00075     void l_will (option_t *opt);
00076     void l_wont (option_t *opt);
00077     void l_do (option_t *opt);
00078     void l_dont (option_t *opt);
00079     void will_check (option_t *opt);
00080     void do_check (option_t *opt);
00081     void wont_check (option_t *opt);
00082     void dont_check (option_t *opt);
00083     void l_do_sub (std::string const &data, std::string::size_type &pos);
00084     bool m_ignore;
00085     void l_in_filter (std::string &data);
00086     void l_out_filter (std::string &data);
00087     std::string m_am_here, m_inbuffer;
00088   protected:
00090     telnet (Glib::RefPtr <Glib::MainContext> main);
00091   public:
00093     static Glib::RefPtr <telnet> create (Glib::RefPtr <Glib::MainContext> main = Glib::MainContext::get_default () );
00095 
00097     std::string &you_there ();
00098   };
00099 }
00100 
00101 #endif