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);
79 void sendOptionParameters(option_type option, std::string const & data);
81 void requestLocalOption(option_type option, bool enabled = true);
82 void acceptLocalOption(option_type option, bool enabled = true);
84 void requestPeerOption(option_type option, bool enabled = true);
85 void acceptPeerOption(option_type option, bool enabled = true);
87 bool localOption(option_type option);
88 bool peerOption(option_type option);
91 explicit BaseTelnetProtocol(Handle handle);
93 virtual ~BaseTelnetProtocol();
95 template <class Handler>
96 void registerHandler(Handler * h, bool request=true);
98 void incrementRequestCounter();
99 void decrementRequestCounter();
100 bool requestsPending();
105 virtual void v_setupComplete() = 0;
106 virtual void v_charReceived(char c) = 0;
107 virtual void v_eof() = 0;
109 virtual void v_handleNOP();
110 virtual void v_handleBRK();
111 virtual void v_handleIP();
112 virtual void v_handleAO();
113 virtual void v_handleAYT();
114 virtual void v_handleEC();
115 virtual void v_handleEL();
116 virtual void v_handleGA();
121 void handleChar(char c);
122 void handleNormalChar(char c);
123 void handleCommand(char c);
124 void handleOption(char c);
125 void handleCR(char c);
126 void handleSBOption(char c);
127 void handleSBData(char c);
128 void handleSBIAC(char c);
130 void processCommand();
131 void transmit(char c);
133 void sendWILL(char option);
134 void sendWONT(char option);
135 void sendDO(char option);
136 void sendDONT(char option);
138 void readHandler(int state);
139 void writeHandler(int state);
164 enum WantState { WANTED, ACCEPTED, DISABLED };
165 enum OptionState { NONE, REQUEST_SENT, ACKNOWLEDGED };
168 OptInfo(bool local, option_type option);
170 ///////////////////////////////////////////////////////////////
173 option_type const option;
176 OptionState optionState;
181 OptInfo & getOption(bool local, option_type option);
182 void request(OptInfo & info, bool enabled);
183 void response(OptInfo & info, bool enabled);
185 typedef std::map<std::pair<bool, option_type>, OptInfo> OptionsMap;
188 typedef std::map<option_type, TelnetHandler*> OptionHandlerMap;
189 OptionHandlerMap handlers_;
193 typedef std::vector<char> SendQueue;
194 SendQueue sendQueue_;
196 enum CharState { NORMAL, IAC_SEEN, EXPECT_OPTION, CR_SEEN,
197 SB_OPTION, SB_DATA, SB_IAC_SEEN };
198 CharState charState_;
204 senf::scheduler::FdEvent inputEvent_;
205 senf::scheduler::FdEvent outputEvent_;
207 unsigned pendingRequests_;
209 ClockService::clock_type requestTimeout_;
210 scheduler::TimerEvent timeout_;
212 friend class TelnetHandler;
215 struct BaseTelnetProtocol::TelnetHandler
216 : public virtual BaseTelnetProtocol
218 virtual ~TelnetHandler();
219 virtual void v_init() = 0;
220 virtual void v_handleOptionParameters(std::string const & data) = 0;
223 // See http://www.iana.org/assignments/telnet-options for a list of options
224 namespace telnetopt { BaseTelnetProtocol::option_type const ECHO = 1u; }
225 namespace telnetopt { BaseTelnetProtocol::option_type const TRANSMIT_BINARY = 0u; }
226 namespace telnetopt { BaseTelnetProtocol::option_type const SUPPRESS_GO_AHEAD = 3u; }
227 namespace telnetopt { BaseTelnetProtocol::option_type const TERMINAL_TYPE = 24u; }
228 namespace telnetopt { BaseTelnetProtocol::option_type const NAWS = 31u; }
229 namespace telnetopt { BaseTelnetProtocol::option_type const LINEMODE = 34u; }
231 namespace telnethandler {
234 : public BaseTelnetProtocol::TelnetHandler
237 static option_type const OPTION_CODE = telnetopt::TERMINAL_TYPE;
239 void nextTerminalType();
240 std::string const & terminalType() const;
246 virtual void v_init();
247 virtual void v_handleOptionParameters(std::string const & data);
253 : public BaseTelnetProtocol::TelnetHandler
256 static option_type const OPTION_CODE = telnetopt::NAWS;
258 unsigned width() const;
259 unsigned height() const;
265 virtual void v_windowSizeChanged() = 0;
267 virtual void v_init();
268 virtual void v_handleOptionParameters(std::string const & data);
278 ///////////////////////////////hh.e////////////////////////////////////////
279 #include "Telnet.cci"
281 //#include "Telnet.ct"
282 #include "Telnet.cti"
289 // comment-column: 40
290 // c-file-style: "senf"
291 // indent-tabs-mode: nil
292 // ispell-local-dictionary: "american"
293 // compile-command: "scons -u test"