Utils: Fix hexump() of nevative values
[senf.git] / Utils / Termlib / Telnet.hh
1 // $Id$
2 //
3 // Copyright (C) 2008 
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
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.
12 //
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.
17 //
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.
22
23 /** \file
24     \brief Telnet public header */
25
26 #ifndef HH_SENF_Scheduler_Console_Telnet_
27 #define HH_SENF_Scheduler_Console_Telnet_ 1
28
29 // Custom includes
30 #include <vector>
31 #include <map>
32 #include <senf/Socket.hh>
33 #include <senf/Scheduler/Scheduler.hh>
34 #include <senf/Scheduler/ClockService.hh>
35
36 //#include "Telnet.mpp"
37 ///////////////////////////////hh.p////////////////////////////////////////
38
39 namespace senf {
40 namespace term {
41
42     /** \brief  Telnet server
43
44         \see 
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
47
48         \todo SYNCH handling
49      */
50     class BaseTelnetProtocol
51     {
52     public:
53         static unsigned const DEFAULT_REQUEST_TIMEOUT_MS = 500u;
54
55         typedef ClientSocketHandle<senf::MakeSocketPolicy<
56             ConnectedCommunicationPolicy, 
57             StreamFramingPolicy, 
58             ReadablePolicy,
59             WriteablePolicy>::policy> Handle;
60
61         typedef unsigned char option_type;
62
63         struct TelnetHandler;
64
65         void write(std::string const & s);
66         void write(char c);
67
68         void sendNOP();
69         void sendBRK();
70         void sendIP();
71         void sendAO();
72         void sendAYT();
73         void sendEC();
74         void sendEL();
75         void sendGA();
76         
77         void sendOptionParameters(option_type option, std::string const & data);
78
79         void requestLocalOption(option_type option, bool enabled = true);
80         void acceptLocalOption(option_type option, bool enabled = true);
81
82         void requestPeerOption(option_type option, bool enabled = true);
83         void acceptPeerOption(option_type option, bool enabled = true);
84         
85     protected:
86         explicit BaseTelnetProtocol(Handle handle);
87         BaseTelnetProtocol();
88         virtual ~BaseTelnetProtocol();
89
90         template <class Handler>
91         void registerHandler(Handler * h, bool request=true);
92
93         void incrementRequestCounter();
94         void decrementRequestCounter();
95         bool requestsPending();
96
97 #ifndef DOXYGEN
98     private:
99 #endif
100         virtual void v_setupComplete() = 0;
101         virtual void v_charReceived(char c) = 0;
102         virtual void v_eof() = 0;
103
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();
112         
113 #ifdef DOXYGEN
114     private:
115 #endif
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);
124         void emit(char c);
125         void processCommand();
126         void transmit(char c);
127
128         void sendWILL(char option);
129         void sendWONT(char option);
130         void sendDO(char option);
131         void sendDONT(char option);
132
133         void readHandler(int state);
134         void writeHandler(int state);
135         void timeout();
136
137         enum Command { 
138             CMD_NONE = 0,
139             CMD_SE = 240,
140             CMD_NOP = 241,
141             CMD_DM = 242,
142             CMD_BRK = 243,
143             CMD_IP = 244,
144             CMD_AO = 245,
145             CMD_AYT = 246,
146             CMD_EC = 247,
147             CMD_EL = 248,
148             CMD_GA = 249,
149             CMD_SB = 250,
150             CMD_WILL = 251,
151             CMD_WONT = 252,
152             CMD_DO = 253,
153             CMD_DONT = 254,
154             CMD_IAC = 255,
155         };
156
157         struct OptInfo
158         {
159             enum WantState { WANTED, ACCEPTED, DISABLED };
160             enum OptionState { NONE, REQUEST_SENT, ACKNOWLEDGED };
161
162             OptInfo();
163             OptInfo(bool local, option_type option);
164
165             ///////////////////////////////////////////////////////////////
166
167             bool const local;
168             option_type const option;
169
170             WantState wantState;
171             OptionState optionState;
172             bool enabled;
173             
174         };
175
176         OptInfo & getOption(bool local, option_type option);
177         void request(OptInfo & info, bool enabled);
178         void response(OptInfo & info, bool enabled);
179
180         typedef std::map<std::pair<bool, option_type>, OptInfo> OptionsMap;
181         OptionsMap options_;
182
183         typedef std::map<option_type, TelnetHandler*> OptionHandlerMap;
184         OptionHandlerMap handlers_;
185
186         Handle handle_;
187
188         typedef std::vector<char> SendQueue;
189         SendQueue sendQueue_;
190
191         enum CharState { NORMAL, IAC_SEEN, EXPECT_OPTION, CR_SEEN, 
192                          SB_OPTION, SB_DATA, SB_IAC_SEEN };
193         CharState charState_;
194
195         Command command_;
196         option_type option_;
197         std::string data_;
198
199         senf::scheduler::FdEvent inputEvent_;
200         senf::scheduler::FdEvent outputEvent_;
201
202         unsigned pendingRequests_;
203
204         ClockService::clock_type requestTimeout_;
205         scheduler::TimerEvent timeout_;
206
207         friend class TelnetHandler;
208     };
209
210     struct BaseTelnetProtocol::TelnetHandler
211         : public virtual BaseTelnetProtocol
212     {
213         virtual ~TelnetHandler();
214         virtual void v_init() = 0;
215         virtual void v_handleOptionParameters(std::string const & data) = 0;
216     };
217
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; }
225
226 namespace telnethandler {
227
228     class TerminalType
229         : public BaseTelnetProtocol::TelnetHandler
230     {
231     public:
232         static option_type const OPTION_CODE = telnetopt::TERMINAL_TYPE;
233
234         void nextTerminalType();
235         std::string const & terminalType() const;
236         
237     protected:
238         TerminalType();
239
240     private:
241         virtual void v_init();
242         virtual void v_handleOptionParameters(std::string const & data);
243
244         std::string type_;
245     };
246
247     class NAWS 
248         : public BaseTelnetProtocol::TelnetHandler
249     {
250     public:
251         static option_type const OPTION_CODE = telnetopt::NAWS;
252
253         unsigned width() const;
254         unsigned height() const;
255
256     protected:
257         NAWS();
258         
259     private:
260         virtual void v_windowSizeChanged() = 0;
261
262         virtual void v_init();
263         virtual void v_handleOptionParameters(std::string const & data);
264
265         unsigned width_;
266         unsigned height_;
267     };
268     
269 }
270
271 }}
272
273 ///////////////////////////////hh.e////////////////////////////////////////
274 #include "Telnet.cci"
275
276 //#include "Telnet.ct"
277 #include "Telnet.cti"
278 #endif
279
280 \f
281 // Local Variables:
282 // mode: c++
283 // fill-column: 100
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"
289 // End: