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>
34 #include <senf/Scheduler/ClockService.hh>
36 //#include "Telnet.mpp"
37 ///////////////////////////////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 static unsigned const DEFAULT_REQUEST_TIMEOUT_MS = 500u;
55 typedef ClientSocketHandle<senf::MakeSocketPolicy<
56 ConnectedCommunicationPolicy,
59 WriteablePolicy>::policy> Handle;
61 typedef unsigned char option_type;
65 void write(std::string const & s);
77 void sendOptionParameters(option_type option, std::string const & data);
79 void requestLocalOption(option_type option, bool enabled = true);
80 void acceptLocalOption(option_type option, bool enabled = true);
82 void requestPeerOption(option_type option, bool enabled = true);
83 void acceptPeerOption(option_type option, bool enabled = true);
86 explicit BaseTelnetProtocol(Handle handle);
88 virtual ~BaseTelnetProtocol();
90 template <class Handler>
91 void registerHandler(Handler * h, bool request=true);
93 void incrementRequestCounter();
94 void decrementRequestCounter();
95 bool requestsPending();
100 virtual void v_setupComplete() = 0;
101 virtual void v_charReceived(char c) = 0;
102 virtual void v_eof() = 0;
104 virtual void v_handleNOP();
105 virtual void v_handleBRK();
106 virtual void v_handleIP();
107 virtual void v_handleAO();
108 virtual void v_handleAYT();
109 virtual void v_handleEC();
110 virtual void v_handleEL();
111 virtual void v_handleGA();
116 void handleChar(char c);
117 void handleNormalChar(char c);
118 void handleCommand(char c);
119 void handleOption(char c);
120 void handleCR(char c);
121 void handleSBOption(char c);
122 void handleSBData(char c);
123 void handleSBIAC(char c);
125 void processCommand();
126 void transmit(char c);
128 void sendWILL(char option);
129 void sendWONT(char option);
130 void sendDO(char option);
131 void sendDONT(char option);
133 void readHandler(int state);
134 void writeHandler(int state);
159 enum WantState { WANTED, ACCEPTED, DISABLED };
160 enum OptionState { NONE, REQUEST_SENT, ACKNOWLEDGED };
163 OptInfo(bool local, option_type option);
165 ///////////////////////////////////////////////////////////////
168 option_type const option;
171 OptionState optionState;
176 OptInfo & getOption(bool local, option_type option);
177 void request(OptInfo & info, bool enabled);
178 void response(OptInfo & info, bool enabled);
180 typedef std::map<std::pair<bool, option_type>, OptInfo> OptionsMap;
183 typedef std::map<option_type, TelnetHandler*> OptionHandlerMap;
184 OptionHandlerMap handlers_;
188 typedef std::vector<char> SendQueue;
189 SendQueue sendQueue_;
191 enum CharState { NORMAL, IAC_SEEN, EXPECT_OPTION, CR_SEEN,
192 SB_OPTION, SB_DATA, SB_IAC_SEEN };
193 CharState charState_;
199 senf::scheduler::FdEvent inputEvent_;
200 senf::scheduler::FdEvent outputEvent_;
202 unsigned pendingRequests_;
204 ClockService::clock_type requestTimeout_;
205 scheduler::TimerEvent timeout_;
207 friend class TelnetHandler;
210 struct BaseTelnetProtocol::TelnetHandler
211 : public virtual BaseTelnetProtocol
213 virtual ~TelnetHandler();
214 virtual void v_init() = 0;
215 virtual void v_handleOptionParameters(std::string const & data) = 0;
218 // See http://www.iana.org/assignments/telnet-options for a list of options
219 namespace telnetopt { BaseTelnetProtocol::option_type const ECHO = 1u; }
220 namespace telnetopt { BaseTelnetProtocol::option_type const TRANSMIT_BINARY = 0u; }
221 namespace telnetopt { BaseTelnetProtocol::option_type const SUPPRESS_GO_AHEAD = 3u; }
222 namespace telnetopt { BaseTelnetProtocol::option_type const TERMINAL_TYPE = 24u; }
223 namespace telnetopt { BaseTelnetProtocol::option_type const NAWS = 31u; }
224 namespace telnetopt { BaseTelnetProtocol::option_type const LINEMODE = 34u; }
226 namespace telnethandler {
229 : public BaseTelnetProtocol::TelnetHandler
232 static option_type const OPTION_CODE = telnetopt::TERMINAL_TYPE;
234 void nextTerminalType();
235 std::string const & terminalType() const;
241 virtual void v_init();
242 virtual void v_handleOptionParameters(std::string const & data);
248 : public BaseTelnetProtocol::TelnetHandler
251 static option_type const OPTION_CODE = telnetopt::NAWS;
253 unsigned width() const;
254 unsigned height() const;
260 virtual void v_windowSizeChanged() = 0;
262 virtual void v_init();
263 virtual void v_handleOptionParameters(std::string const & data);
273 ///////////////////////////////hh.e////////////////////////////////////////
274 #include "Telnet.cci"
276 //#include "Telnet.ct"
277 #include "Telnet.cti"
284 // comment-column: 40
285 // c-file-style: "senf"
286 // indent-tabs-mode: nil
287 // ispell-local-dictionary: "american"
288 // compile-command: "scons -u test"