4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief Telnet public header */
26 #ifndef HH_SENF_Scheduler_Console_Telnet_
27 #define HH_SENF_Scheduler_Console_Telnet_ 1
32 #include <senf/Socket.hh>
33 #include <senf/Scheduler/Scheduler.hh>
35 //#include "Telnet.mpp"
36 ///////////////////////////////hh.p////////////////////////////////////////
42 /** \brief Telnet server
45 <a href="http://tools.ietf.org/html/rfc854>RFC 854</a> The Telnet protocol \n
46 <a href="http://tools.ietf.org/html/rfc854>RFC 855</a> Telnet option specifications
50 class BaseTelnetProtocol
53 typedef ClientSocketHandle<senf::MakeSocketPolicy<
54 ConnectedCommunicationPolicy,
57 WriteablePolicy>::policy> Handle;
59 typedef unsigned char option_type;
63 void write(std::string const & s);
75 void sendOptionParameters(option_type option, std::string const & data);
77 void requestLocalOption(option_type option, bool enabled = true);
78 void acceptLocalOption(option_type option, bool enabled = true);
80 void requestPeerOption(option_type option, bool enabled = true);
81 void acceptPeerOption(option_type option, bool enabled = true);
84 explicit BaseTelnetProtocol(Handle handle);
86 virtual ~BaseTelnetProtocol();
88 template <class Handler>
89 void registerHandler(Handler * h, bool request=true);
96 virtual void v_charReceived(char c) = 0;
97 virtual void v_eof() = 0;
99 virtual void v_handleNOP();
100 virtual void v_handleBRK();
101 virtual void v_handleIP();
102 virtual void v_handleAO();
103 virtual void v_handleAYT();
104 virtual void v_handleEC();
105 virtual void v_handleEL();
106 virtual void v_handleGA();
111 void handleChar(char c);
112 void handleNormalChar(char c);
113 void handleCommand(char c);
114 void handleOption(char c);
115 void handleCR(char c);
116 void handleSBOption(char c);
117 void handleSBData(char c);
118 void handleSBIAC(char c);
120 void processCommand();
121 void transmit(char c);
123 void sendWILL(char option);
124 void sendWONT(char option);
125 void sendDO(char option);
126 void sendDONT(char option);
128 void readHandler(int state);
129 void writeHandler(int state);
153 enum WantState { WANTED, ACCEPTED, DISABLED };
154 enum OptionState { NONE, REQUEST_SENT, ACKNOWLEDGED };
157 OptInfo(bool local, option_type option);
159 ///////////////////////////////////////////////////////////////
162 option_type const option;
165 OptionState optionState;
170 OptInfo & getOption(bool local, option_type option);
171 void request(OptInfo & info, bool enabled);
172 void response(OptInfo & info, bool enabled);
174 typedef std::map<std::pair<bool, option_type>, OptInfo> OptionsMap;
177 typedef std::map<option_type, TelnetHandler*> OptionHandlerMap;
178 OptionHandlerMap handlers_;
182 typedef std::vector<char> SendQueue;
183 SendQueue sendQueue_;
185 enum CharState { NORMAL, IAC_SEEN, EXPECT_OPTION, CR_SEEN,
186 SB_OPTION, SB_DATA, SB_IAC_SEEN };
187 CharState charState_;
193 senf::scheduler::FdEvent inputEvent_;
194 senf::scheduler::FdEvent outputEvent_;
196 friend class TelnetHandler;
199 struct BaseTelnetProtocol::TelnetHandler
200 : public virtual BaseTelnetProtocol
202 virtual ~TelnetHandler();
203 virtual void v_init() = 0;
204 virtual void v_handleOptionParameters(std::string const & data) = 0;
207 // See http://www.iana.org/assignments/telnet-options for a list of options
208 namespace telnetopt { BaseTelnetProtocol::option_type const ECHO = 1u; }
209 namespace telnetopt { BaseTelnetProtocol::option_type const TRANSMIT_BINARY = 0u; }
210 namespace telnetopt { BaseTelnetProtocol::option_type const SUPPRESS_GO_AHEAD = 3u; }
211 namespace telnetopt { BaseTelnetProtocol::option_type const TERMINAL_TYPE = 24u; }
212 namespace telnetopt { BaseTelnetProtocol::option_type const NAWS = 31u; }
213 namespace telnetopt { BaseTelnetProtocol::option_type const LINEMODE = 34u; }
215 namespace telnethandler {
218 : public BaseTelnetProtocol::TelnetHandler
221 static option_type const OPTION_CODE = telnetopt::TERMINAL_TYPE;
223 void nextTerminalType();
229 virtual void v_handleTerminalType(std::string const & type) = 0;
231 virtual void v_init();
232 virtual void v_handleOptionParameters(std::string const & data);
236 : public BaseTelnetProtocol::TelnetHandler
239 static option_type const OPTION_CODE = telnetopt::NAWS;
245 virtual void v_handleWindowSize(unsigned width, unsigned height) = 0;
247 virtual void v_init();
248 virtual void v_handleOptionParameters(std::string const & data);
255 ///////////////////////////////hh.e////////////////////////////////////////
256 #include "Telnet.cci"
258 //#include "Telnet.ct"
259 #include "Telnet.cti"
266 // comment-column: 40
267 // c-file-style: "senf"
268 // indent-tabs-mode: nil
269 // ispell-local-dictionary: "american"
270 // compile-command: "scons -u test"