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////////////////////////////////////////
43 /** \brief Telnet server
46 <a href="http://tools.ietf.org/html/rfc854>RFC 854</a> The Telnet protocol \n
47 <a href="http://tools.ietf.org/html/rfc854>RFC 855</a> Telnet option specifications
51 class BaseTelnetProtocol
54 static unsigned const DEFAULT_REQUEST_TIMEOUT_MS = 500u;
56 typedef ClientSocketHandle<senf::MakeSocketPolicy<
57 ConnectedCommunicationPolicy,
60 WriteablePolicy>::policy> Handle;
62 typedef unsigned char option_type;
66 void write(std::string const & s);
78 void sendOptionParameters(option_type option, std::string const & data);
80 void requestLocalOption(option_type option, bool enabled = true);
81 void acceptLocalOption(option_type option, bool enabled = true);
83 void requestPeerOption(option_type option, bool enabled = true);
84 void acceptPeerOption(option_type option, bool enabled = true);
87 explicit BaseTelnetProtocol(Handle handle);
89 virtual ~BaseTelnetProtocol();
91 template <class Handler>
92 void registerHandler(Handler * h, bool request=true);
94 void incrementRequestCounter();
95 void decrementRequestCounter();
96 bool requestsPending();
103 virtual void v_setupComplete() = 0;
104 virtual void v_charReceived(char c) = 0;
105 virtual void v_eof() = 0;
107 virtual void v_handleNOP();
108 virtual void v_handleBRK();
109 virtual void v_handleIP();
110 virtual void v_handleAO();
111 virtual void v_handleAYT();
112 virtual void v_handleEC();
113 virtual void v_handleEL();
114 virtual void v_handleGA();
119 void handleChar(char c);
120 void handleNormalChar(char c);
121 void handleCommand(char c);
122 void handleOption(char c);
123 void handleCR(char c);
124 void handleSBOption(char c);
125 void handleSBData(char c);
126 void handleSBIAC(char c);
128 void processCommand();
129 void transmit(char c);
131 void sendWILL(char option);
132 void sendWONT(char option);
133 void sendDO(char option);
134 void sendDONT(char option);
136 void readHandler(int state);
137 void writeHandler(int state);
162 enum WantState { WANTED, ACCEPTED, DISABLED };
163 enum OptionState { NONE, REQUEST_SENT, ACKNOWLEDGED };
166 OptInfo(bool local, option_type option);
168 ///////////////////////////////////////////////////////////////
171 option_type const option;
174 OptionState optionState;
179 OptInfo & getOption(bool local, option_type option);
180 void request(OptInfo & info, bool enabled);
181 void response(OptInfo & info, bool enabled);
183 typedef std::map<std::pair<bool, option_type>, OptInfo> OptionsMap;
186 typedef std::map<option_type, TelnetHandler*> OptionHandlerMap;
187 OptionHandlerMap handlers_;
191 typedef std::vector<char> SendQueue;
192 SendQueue sendQueue_;
194 enum CharState { NORMAL, IAC_SEEN, EXPECT_OPTION, CR_SEEN,
195 SB_OPTION, SB_DATA, SB_IAC_SEEN };
196 CharState charState_;
202 senf::scheduler::FdEvent inputEvent_;
203 senf::scheduler::FdEvent outputEvent_;
205 unsigned pendingRequests_;
207 ClockService::clock_type requestTimeout_;
208 scheduler::TimerEvent timeout_;
210 friend class TelnetHandler;
213 struct BaseTelnetProtocol::TelnetHandler
214 : public virtual BaseTelnetProtocol
216 virtual ~TelnetHandler();
217 virtual void v_init() = 0;
218 virtual void v_handleOptionParameters(std::string const & data) = 0;
221 // See http://www.iana.org/assignments/telnet-options for a list of options
222 namespace telnetopt { BaseTelnetProtocol::option_type const ECHO = 1u; }
223 namespace telnetopt { BaseTelnetProtocol::option_type const TRANSMIT_BINARY = 0u; }
224 namespace telnetopt { BaseTelnetProtocol::option_type const SUPPRESS_GO_AHEAD = 3u; }
225 namespace telnetopt { BaseTelnetProtocol::option_type const TERMINAL_TYPE = 24u; }
226 namespace telnetopt { BaseTelnetProtocol::option_type const NAWS = 31u; }
227 namespace telnetopt { BaseTelnetProtocol::option_type const LINEMODE = 34u; }
229 namespace telnethandler {
232 : public BaseTelnetProtocol::TelnetHandler
235 static option_type const OPTION_CODE = telnetopt::TERMINAL_TYPE;
237 void nextTerminalType();
238 std::string const & terminalType() const;
244 virtual void v_init();
245 virtual void v_handleOptionParameters(std::string const & data);
251 : public BaseTelnetProtocol::TelnetHandler
254 static option_type const OPTION_CODE = telnetopt::NAWS;
256 unsigned width() const;
257 unsigned height() const;
263 virtual void v_windowSizeChanged() = 0;
265 virtual void v_init();
266 virtual void v_handleOptionParameters(std::string const & data);
276 ///////////////////////////////hh.e////////////////////////////////////////
277 #include "Telnet.cci"
279 //#include "Telnet.ct"
280 #include "Telnet.cti"
287 // comment-column: 40
288 // c-file-style: "senf"
289 // indent-tabs-mode: nil
290 // ispell-local-dictionary: "american"
291 // compile-command: "scons -u test"