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 <boost/algorithm/string/case_conv.hpp>
31 #include <senf/Utils/membind.hh>
32 #include <senf/Utils/Logger/SenfLog.hh>
34 //#include "Telnet.mpp"
36 ///////////////////////////////cc.p////////////////////////////////////////
38 prefix_ senf::term::BaseTelnetProtocol::BaseTelnetProtocol(Handle handle)
39 : handle_ (handle), charState_ (NORMAL), command_ (CMD_NONE), option_ (0),
40 inputEvent_ ("senf::term::BaseTelnetProtocol::input",
41 senf::membind(&BaseTelnetProtocol::readHandler, this), handle,
42 senf::scheduler::FdEvent::EV_READ),
43 outputEvent_ ("senf::term::BaseTelnetProtocol::output",
44 senf::membind(&BaseTelnetProtocol::writeHandler, this), handle,
45 senf::scheduler::FdEvent::EV_WRITE, false),
46 pendingRequests_ (0u),
47 requestTimeout_ (ClockService::milliseconds(DEFAULT_REQUEST_TIMEOUT_MS)),
48 timeout_ ("senf::term::BaseTelnetProtocol::configTimeout",
49 senf::membind(&BaseTelnetProtocol::timeout, this))
52 prefix_ senf::term::BaseTelnetProtocol::BaseTelnetProtocol()
53 : handle_ (), charState_ (NORMAL), command_ (CMD_NONE), option_ (0),
54 inputEvent_ ("senf::term::BaseTelnetProtocol::input", 0),
55 outputEvent_ ("senf::term::BaseTelnetProtocol::output", 0),
56 pendingRequests_ (0u),
57 requestTimeout_ (ClockService::milliseconds(DEFAULT_REQUEST_TIMEOUT_MS)),
58 timeout_ ("senf::term::BaseTelnetProtocol::timeout", 0)
63 prefix_ void senf::term::BaseTelnetProtocol::write(std::string const & s)
65 for (std::string::const_iterator i (s.begin()); i != s.end(); ++i)
69 prefix_ void senf::term::BaseTelnetProtocol::write(char c)
91 senf::term::BaseTelnetProtocol::sendOptionParameters(option_type option,
92 std::string const & data)
97 for (std::string::const_iterator i (data.begin()); i != data.end(); ++i)
108 prefix_ void senf::term::BaseTelnetProtocol::v_handleNOP()
111 prefix_ void senf::term::BaseTelnetProtocol::v_handleBRK()
114 prefix_ void senf::term::BaseTelnetProtocol::v_handleIP()
117 prefix_ void senf::term::BaseTelnetProtocol::v_handleAO()
120 prefix_ void senf::term::BaseTelnetProtocol::v_handleAYT()
123 prefix_ void senf::term::BaseTelnetProtocol::v_handleEC()
126 prefix_ void senf::term::BaseTelnetProtocol::v_handleEL()
129 prefix_ void senf::term::BaseTelnetProtocol::v_handleGA()
132 prefix_ void senf::term::BaseTelnetProtocol::handleChar(char c)
134 switch (charState_) {
139 handleCommand(static_cast<unsigned char>(c));
159 prefix_ void senf::term::BaseTelnetProtocol::handleNormalChar(char c)
163 charState_ = CR_SEEN;
166 charState_ = IAC_SEEN;
174 prefix_ void senf::term::BaseTelnetProtocol::handleCommand(char c)
178 // Ignore spurious SE commands .. they should only occur while in subnegotiation mode
190 command_ = Command(static_cast<unsigned char>(c));
196 charState_ = SB_OPTION;
202 command_ = Command(static_cast<unsigned char>(c));
203 charState_ = EXPECT_OPTION;
217 prefix_ void senf::term::BaseTelnetProtocol::handleOption(char c)
224 prefix_ void senf::term::BaseTelnetProtocol::handleCR(char c)
243 prefix_ void senf::term::BaseTelnetProtocol::handleSBOption(char c)
246 charState_ = SB_DATA;
250 prefix_ void senf::term::BaseTelnetProtocol::handleSBData(char c)
253 charState_ = SB_IAC_SEEN;
258 prefix_ void senf::term::BaseTelnetProtocol::handleSBIAC(char c)
263 charState_ = SB_DATA;
270 charState_ = IAC_SEEN;
276 prefix_ void senf::term::BaseTelnetProtocol::processCommand()
310 OptionHandlerMap::const_iterator i (handlers_.find(option_));
311 if (i != handlers_.end())
312 i->second->v_handleOptionParameters(data_);
317 response(getOption(false, option_), command_ == CMD_WILL);
321 response(getOption(true, option_), command_ == CMD_DO);
326 prefix_ void senf::term::BaseTelnetProtocol::transmit(char c)
328 sendQueue_.push_back(c);
329 outputEvent_.enable();
332 prefix_ void senf::term::BaseTelnetProtocol::readHandler(int state)
334 if (state != senf::scheduler::FdEvent::EV_READ || handle_.eof()) {
335 inputEvent_.disable();
340 handle_.read(data, 0u);
341 for (std::string::const_iterator i (data.begin()); i != data.end(); ++i)
345 prefix_ void senf::term::BaseTelnetProtocol::writeHandler(int state)
347 if (state != senf::scheduler::FdEvent::EV_WRITE) {
348 outputEvent_.disable();
349 inputEvent_.disable();
353 sendQueue_.erase(sendQueue_.begin(),
354 handle_.write(boost::make_iterator_range(
355 sendQueue_.begin(), sendQueue_.end())));
356 if (sendQueue_.empty())
357 outputEvent_.disable();
360 prefix_ void senf::term::BaseTelnetProtocol::timeout()
362 if (pendingRequests_ > 0u) {
363 pendingRequests_ = 0u;
368 prefix_ senf::term::BaseTelnetProtocol::OptInfo &
369 senf::term::BaseTelnetProtocol::getOption(bool local, option_type option)
371 OptionsMap::iterator i (options_.find(std::make_pair(local, option)));
372 if (i == options_.end())
373 i = options_.insert(std::make_pair(std::make_pair(local, option),
374 OptInfo(local, option))).first;
378 prefix_ void senf::term::BaseTelnetProtocol::request(OptInfo & info, bool enabled)
380 info.wantState = enabled ? OptInfo::WANTED : OptInfo::DISABLED;
381 if (enabled != info.enabled) {
383 transmit((info.local ? CMD_WILL : CMD_DO) + (enabled ? 0 : 1));
384 transmit(info.option);
385 info.optionState = OptInfo::REQUEST_SENT;
386 incrementRequestCounter();
390 prefix_ void senf::term::BaseTelnetProtocol::response(OptInfo & info, bool enabled)
392 bool decrementCount (false);
394 // If this is a response, we need to unconditionally accept it. If this is a remote
395 // configuration request, we accept it if wantState is wither WANTED or ACCEPTED. If this is a
396 // response, we never send out a reply. If it is a remote request we send out a reply only if
397 // either a) we reject the request or b) we accept it AND we have changed our own mode.
398 if (info.optionState == OptInfo::REQUEST_SENT) {
399 // This is a response
400 info.optionState = OptInfo::ACKNOWLEDGED;
401 info.enabled = enabled;
402 decrementCount = true;
404 else if (enabled != info.enabled) {
405 // Request to change the current state
407 (enabled && (info.wantState == OptInfo::WANTED || info.wantState == OptInfo::ACCEPTED))) {
408 // accept the request
409 info.optionState = OptInfo::ACKNOWLEDGED;
410 info.enabled = enabled;
411 } // else reject the request
413 transmit((info.local ? CMD_WILL : CMD_DO) + (info.enabled ? 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 // and v_setupComplete() might be called prematurely.
426 decrementRequestCounter();
429 prefix_ void senf::term::BaseTelnetProtocol::decrementRequestCounter()
431 if (pendingRequests_ > 0u) {
433 if (pendingRequests_ == 0u) {
440 ///////////////////////////////////////////////////////////////////////////
441 // senf::term::telnethandler::TerminalType
443 prefix_ senf::term::telnethandler::TerminalType::TerminalType()
445 registerHandler(this);
448 prefix_ void senf::term::telnethandler::TerminalType::nextTerminalType()
450 sendOptionParameters(telnetopt::TERMINAL_TYPE, "\x01");
453 prefix_ void senf::term::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 boost::algorithm::to_lower(type_);
461 decrementRequestCounter();
465 prefix_ void senf::term::telnethandler::TerminalType::v_init()
468 incrementRequestCounter();
471 ///////////////////////////////////////////////////////////////////////////
472 // senf::term::telnethandler::NAWS
474 prefix_ senf::term::telnethandler::NAWS::NAWS()
475 : width_ (0u), height_ (0u)
477 registerHandler(this);
480 prefix_ void senf::term::telnethandler::NAWS::v_init()
482 incrementRequestCounter();
486 senf::term::telnethandler::NAWS::v_handleOptionParameters(std::string const & data)
488 if (data.size() != 4)
490 width_ = (static_cast<unsigned char>(data[0])<<8)+static_cast<unsigned char>(data[1]);
491 height_ = (static_cast<unsigned char>(data[2])<<8)+static_cast<unsigned char>(data[3]);
492 if (! requestsPending())
493 v_windowSizeChanged();
494 decrementRequestCounter();
497 ///////////////////////////////cc.e////////////////////////////////////////
499 //#include "Telnet.mpp"
505 // comment-column: 40
506 // c-file-style: "senf"
507 // indent-tabs-mode: nil
508 // ispell-local-dictionary: "american"
509 // compile-command: "scons -u test"