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 non-inline non-template implementation */
27 //#include "Telnet.ih"
30 #include "../membind.hh"
31 #include "../Logger/SenfLog.hh"
33 //#include "Telnet.mpp"
35 ///////////////////////////////cc.p////////////////////////////////////////
37 prefix_ senf::console::detail::BaseTelnetProtocol::BaseTelnetProtocol(Handle handle)
38 : handle_ (handle), charState_ (NORMAL), command_ (CMD_NONE), option_ (0),
39 inputEvent_ ("senf::console::detail::BaseTelnetProtocol::input",
40 senf::membind(&BaseTelnetProtocol::readHandler, this), handle,
41 senf::scheduler::FdEvent::EV_READ),
42 outputEvent_ ("senf::console::detail::BaseTelnetProtocol::output",
43 senf::membind(&BaseTelnetProtocol::writeHandler, this), handle,
44 senf::scheduler::FdEvent::EV_WRITE, false),
45 pendingRequests_ (0u),
46 requestTimeout_ (ClockService::milliseconds(DEFAULT_REQUEST_TIMEOUT_MS)),
47 timeout_ ("senf::console::detail::BaseTelnetProtocol::timeout",
48 senf::membind(&BaseTelnetProtocol::timeout, this))
51 prefix_ senf::console::detail::BaseTelnetProtocol::BaseTelnetProtocol()
52 : handle_ (), charState_ (NORMAL), command_ (CMD_NONE), option_ (0),
53 inputEvent_ ("senf::console::detail::BaseTelnetProtocol::input", 0),
54 outputEvent_ ("senf::console::detail::BaseTelnetProtocol::output", 0),
55 pendingRequests_ (0u),
56 requestTimeout_ (ClockService::milliseconds(DEFAULT_REQUEST_TIMEOUT_MS)),
57 timeout_ ("senf::console::detail::BaseTelnetProtocol::timeout", 0)
60 prefix_ void senf::console::detail::BaseTelnetProtocol::write(std::string const & s)
62 for (std::string::const_iterator i (s.begin()); i != s.end(); ++i)
66 prefix_ void senf::console::detail::BaseTelnetProtocol::write(char c)
88 senf::console::detail::BaseTelnetProtocol::sendOptionParameters(option_type option,
89 std::string const & data)
94 for (std::string::const_iterator i (data.begin()); i != data.end(); ++i)
105 prefix_ void senf::console::detail::BaseTelnetProtocol::v_handleNOP()
108 prefix_ void senf::console::detail::BaseTelnetProtocol::v_handleBRK()
111 prefix_ void senf::console::detail::BaseTelnetProtocol::v_handleIP()
114 prefix_ void senf::console::detail::BaseTelnetProtocol::v_handleAO()
117 prefix_ void senf::console::detail::BaseTelnetProtocol::v_handleAYT()
120 prefix_ void senf::console::detail::BaseTelnetProtocol::v_handleEC()
123 prefix_ void senf::console::detail::BaseTelnetProtocol::v_handleEL()
126 prefix_ void senf::console::detail::BaseTelnetProtocol::v_handleGA()
129 prefix_ void senf::console::detail::BaseTelnetProtocol::handleChar(char c)
131 switch (charState_) {
136 handleCommand(static_cast<unsigned char>(c));
156 prefix_ void senf::console::detail::BaseTelnetProtocol::handleNormalChar(char c)
160 charState_ = CR_SEEN;
163 charState_ = IAC_SEEN;
171 prefix_ void senf::console::detail::BaseTelnetProtocol::handleCommand(char c)
175 // Ignore spurious SE commands .. they should only occur while in subnegotiation mode
187 command_ = Command(static_cast<unsigned char>(c));
193 charState_ = SB_OPTION;
199 command_ = Command(static_cast<unsigned char>(c));
200 charState_ = EXPECT_OPTION;
214 prefix_ void senf::console::detail::BaseTelnetProtocol::handleOption(char c)
221 prefix_ void senf::console::detail::BaseTelnetProtocol::handleCR(char c)
240 prefix_ void senf::console::detail::BaseTelnetProtocol::handleSBOption(char c)
243 charState_ = SB_DATA;
247 prefix_ void senf::console::detail::BaseTelnetProtocol::handleSBData(char c)
250 charState_ = SB_IAC_SEEN;
255 prefix_ void senf::console::detail::BaseTelnetProtocol::handleSBIAC(char c)
260 charState_ = SB_DATA;
267 charState_ = IAC_SEEN;
273 prefix_ void senf::console::detail::BaseTelnetProtocol::processCommand()
307 OptionHandlerMap::const_iterator i (handlers_.find(option_));
308 if (i != handlers_.end())
309 i->second->v_handleOptionParameters(data_);
314 response(getOption(false, option_), command_ == CMD_WILL);
318 response(getOption(true, option_), command_ == CMD_DO);
323 prefix_ void senf::console::detail::BaseTelnetProtocol::transmit(char c)
325 sendQueue_.push_back(c);
326 outputEvent_.enable();
329 prefix_ void senf::console::detail::BaseTelnetProtocol::readHandler(int state)
331 if (state != senf::scheduler::FdEvent::EV_READ || handle_.eof()) {
332 inputEvent_.disable();
337 handle_.read(data, 0u);
338 for (std::string::const_iterator i (data.begin()); i != data.end(); ++i)
342 prefix_ void senf::console::detail::BaseTelnetProtocol::writeHandler(int state)
344 if (state != senf::scheduler::FdEvent::EV_WRITE) {
345 outputEvent_.disable();
346 inputEvent_.disable();
350 sendQueue_.erase(sendQueue_.begin(),
351 handle_.write(std::make_pair(sendQueue_.begin(), sendQueue_.end())));
352 if (sendQueue_.empty())
353 outputEvent_.disable();
356 prefix_ void senf::console::detail::BaseTelnetProtocol::timeout()
358 if (pendingRequests_ > 0u) {
359 pendingRequests_ = 0u;
364 prefix_ senf::console::detail::BaseTelnetProtocol::OptInfo &
365 senf::console::detail::BaseTelnetProtocol::getOption(bool local, option_type option)
367 OptionsMap::iterator i (options_.find(std::make_pair(local, option)));
368 if (i == options_.end())
369 i = options_.insert(std::make_pair(std::make_pair(local, option),
370 OptInfo(local, option))).first;
374 prefix_ void senf::console::detail::BaseTelnetProtocol::request(OptInfo & info, bool enabled)
376 info.wantState = enabled ? OptInfo::WANTED : OptInfo::DISABLED;
377 if (enabled != info.enabled) {
379 transmit((info.local ? CMD_WILL : CMD_DO) + (enabled ? 0 : 1));
380 transmit(info.option);
381 info.optionState = OptInfo::REQUEST_SENT;
382 incrementRequestCounter();
386 prefix_ void senf::console::detail::BaseTelnetProtocol::response(OptInfo & info, bool enabled)
388 bool decrementCount (false);
390 // If this is a response, we need to unconditionally accept it. If this is a remote
391 // configuration request, we accept it if wantState is wither WANTED or ACCEPTED. If this is a
392 // response, we never send out a reply. If it is a remote request we send out a reply only if
393 // either a) we reject the request or b) we accept it AND we have changed our own mode.
394 if (info.optionState == OptInfo::REQUEST_SENT) {
395 // This is a response
396 info.optionState = OptInfo::ACKNOWLEDGED;
397 info.enabled = enabled;
398 decrementCount = true;
400 else if (enabled != info.enabled) {
401 // Request to change the current state
402 bool accept (enabled);
404 enabled && (info.wantState == OptInfo::WANTED || info.wantState == OptInfo::ACCEPTED)) {
405 // Accept the request
406 info.optionState = OptInfo::ACKNOWLEDGED;
407 info.enabled = enabled;
410 // Reject the request
411 accept = info.enabled;
413 transmit((info.local ? CMD_WILL : CMD_DO) + (accept ? 0 : 1));
414 transmit(info.option);
419 OptionHandlerMap::const_iterator i (handlers_.find(info.option));
420 if (i != handlers_.end())
424 // This call must be AFTER calling v_init since v_init might increment the request count
425 // again. Otherwise v_setupComplete might be called prematurely
426 decrementRequestCounter();
429 prefix_ void senf::console::detail::BaseTelnetProtocol::decrementRequestCounter()
431 if (pendingRequests_ > 0u) {
433 if (pendingRequests_ == 0u) {
440 ///////////////////////////////////////////////////////////////////////////
441 // senf::console::detail::telnethandler::TerminalType
443 prefix_ senf::console::detail::telnethandler::TerminalType::TerminalType()
445 registerHandler(this);
448 prefix_ void senf::console::detail::telnethandler::TerminalType::nextTerminalType()
450 sendOptionParameters(telnetopt::TERMINAL_TYPE, "\x01");
453 prefix_ void senf::console::detail::telnethandler::TerminalType::
454 v_handleOptionParameters(std::string const & data)
456 if (data.size() <= 0)
458 if (data[0] == '\x00') {
459 type_ = data.substr(1);
460 decrementRequestCounter();
464 prefix_ void senf::console::detail::telnethandler::TerminalType::v_init()
467 incrementRequestCounter();
470 ///////////////////////////////////////////////////////////////////////////
471 // senf::console::detail::telnethandler::NAWS
473 prefix_ senf::console::detail::telnethandler::NAWS::NAWS()
474 : width_ (0u), height_ (0u)
476 registerHandler(this);
479 prefix_ void senf::console::detail::telnethandler::NAWS::v_init()
481 incrementRequestCounter();
485 senf::console::detail::telnethandler::NAWS::v_handleOptionParameters(std::string const & data)
487 if (data.size() != 4)
489 width_ = (static_cast<unsigned char>(data[0])<<8)+static_cast<unsigned char>(data[1]);
490 height_ = (static_cast<unsigned char>(data[2])<<8)+static_cast<unsigned char>(data[3]);
491 if (! requestsPending())
492 v_windowSizeChanged();
493 decrementRequestCounter();
496 ///////////////////////////////cc.e////////////////////////////////////////
498 //#include "Telnet.mpp"
504 // comment-column: 40
505 // c-file-style: "senf"
506 // indent-tabs-mode: nil
507 // ispell-local-dictionary: "american"
508 // compile-command: "scons -u test"