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::term::BaseTelnetProtocol::BaseTelnetProtocol(Handle handle)
38 : handle_ (handle), charState_ (NORMAL), command_ (CMD_NONE), option_ (0),
39 inputEvent_ ("senf::term::BaseTelnetProtocol::input",
40 senf::membind(&BaseTelnetProtocol::readHandler, this), handle,
41 senf::scheduler::FdEvent::EV_READ),
42 outputEvent_ ("senf::term::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::term::BaseTelnetProtocol::configTimeout",
48 senf::membind(&BaseTelnetProtocol::timeout, this))
51 prefix_ senf::term::BaseTelnetProtocol::BaseTelnetProtocol()
52 : handle_ (), charState_ (NORMAL), command_ (CMD_NONE), option_ (0),
53 inputEvent_ ("senf::term::BaseTelnetProtocol::input", 0),
54 outputEvent_ ("senf::term::BaseTelnetProtocol::output", 0),
55 pendingRequests_ (0u),
56 requestTimeout_ (ClockService::milliseconds(DEFAULT_REQUEST_TIMEOUT_MS)),
57 timeout_ ("senf::term::BaseTelnetProtocol::timeout", 0)
62 prefix_ void senf::term::BaseTelnetProtocol::write(std::string const & s)
64 for (std::string::const_iterator i (s.begin()); i != s.end(); ++i)
68 prefix_ void senf::term::BaseTelnetProtocol::write(char c)
90 senf::term::BaseTelnetProtocol::sendOptionParameters(option_type option,
91 std::string const & data)
96 for (std::string::const_iterator i (data.begin()); i != data.end(); ++i)
107 prefix_ void senf::term::BaseTelnetProtocol::v_handleNOP()
110 prefix_ void senf::term::BaseTelnetProtocol::v_handleBRK()
113 prefix_ void senf::term::BaseTelnetProtocol::v_handleIP()
116 prefix_ void senf::term::BaseTelnetProtocol::v_handleAO()
119 prefix_ void senf::term::BaseTelnetProtocol::v_handleAYT()
122 prefix_ void senf::term::BaseTelnetProtocol::v_handleEC()
125 prefix_ void senf::term::BaseTelnetProtocol::v_handleEL()
128 prefix_ void senf::term::BaseTelnetProtocol::v_handleGA()
131 prefix_ void senf::term::BaseTelnetProtocol::handleChar(char c)
133 switch (charState_) {
138 handleCommand(static_cast<unsigned char>(c));
158 prefix_ void senf::term::BaseTelnetProtocol::handleNormalChar(char c)
162 charState_ = CR_SEEN;
165 charState_ = IAC_SEEN;
173 prefix_ void senf::term::BaseTelnetProtocol::handleCommand(char c)
177 // Ignore spurious SE commands .. they should only occur while in subnegotiation mode
189 command_ = Command(static_cast<unsigned char>(c));
195 charState_ = SB_OPTION;
201 command_ = Command(static_cast<unsigned char>(c));
202 charState_ = EXPECT_OPTION;
216 prefix_ void senf::term::BaseTelnetProtocol::handleOption(char c)
223 prefix_ void senf::term::BaseTelnetProtocol::handleCR(char c)
242 prefix_ void senf::term::BaseTelnetProtocol::handleSBOption(char c)
245 charState_ = SB_DATA;
249 prefix_ void senf::term::BaseTelnetProtocol::handleSBData(char c)
252 charState_ = SB_IAC_SEEN;
257 prefix_ void senf::term::BaseTelnetProtocol::handleSBIAC(char c)
262 charState_ = SB_DATA;
269 charState_ = IAC_SEEN;
275 prefix_ void senf::term::BaseTelnetProtocol::processCommand()
309 OptionHandlerMap::const_iterator i (handlers_.find(option_));
310 if (i != handlers_.end())
311 i->second->v_handleOptionParameters(data_);
316 response(getOption(false, option_), command_ == CMD_WILL);
320 response(getOption(true, option_), command_ == CMD_DO);
325 prefix_ void senf::term::BaseTelnetProtocol::transmit(char c)
327 sendQueue_.push_back(c);
328 outputEvent_.enable();
331 prefix_ void senf::term::BaseTelnetProtocol::readHandler(int state)
333 if (state != senf::scheduler::FdEvent::EV_READ || handle_.eof()) {
334 inputEvent_.disable();
339 handle_.read(data, 0u);
340 for (std::string::const_iterator i (data.begin()); i != data.end(); ++i)
344 prefix_ void senf::term::BaseTelnetProtocol::writeHandler(int state)
346 if (state != senf::scheduler::FdEvent::EV_WRITE) {
347 outputEvent_.disable();
348 inputEvent_.disable();
352 sendQueue_.erase(sendQueue_.begin(),
353 handle_.write(boost::make_iterator_range(
354 sendQueue_.begin(), sendQueue_.end())));
355 if (sendQueue_.empty())
356 outputEvent_.disable();
359 prefix_ void senf::term::BaseTelnetProtocol::timeout()
361 if (pendingRequests_ > 0u) {
362 pendingRequests_ = 0u;
367 prefix_ senf::term::BaseTelnetProtocol::OptInfo &
368 senf::term::BaseTelnetProtocol::getOption(bool local, option_type option)
370 OptionsMap::iterator i (options_.find(std::make_pair(local, option)));
371 if (i == options_.end())
372 i = options_.insert(std::make_pair(std::make_pair(local, option),
373 OptInfo(local, option))).first;
377 prefix_ void senf::term::BaseTelnetProtocol::request(OptInfo & info, bool enabled)
379 info.wantState = enabled ? OptInfo::WANTED : OptInfo::DISABLED;
380 if (enabled != info.enabled) {
382 transmit((info.local ? CMD_WILL : CMD_DO) + (enabled ? 0 : 1));
383 transmit(info.option);
384 info.optionState = OptInfo::REQUEST_SENT;
385 incrementRequestCounter();
389 prefix_ void senf::term::BaseTelnetProtocol::response(OptInfo & info, bool enabled)
391 bool decrementCount (false);
393 // If this is a response, we need to unconditionally accept it. If this is a remote
394 // configuration request, we accept it if wantState is wither WANTED or ACCEPTED. If this is a
395 // response, we never send out a reply. If it is a remote request we send out a reply only if
396 // either a) we reject the request or b) we accept it AND we have changed our own mode.
397 if (info.optionState == OptInfo::REQUEST_SENT) {
398 // This is a response
399 info.optionState = OptInfo::ACKNOWLEDGED;
400 info.enabled = enabled;
401 decrementCount = true;
403 else if (enabled != info.enabled) {
404 // Request to change the current state
406 (enabled && (info.wantState == OptInfo::WANTED || info.wantState == OptInfo::ACCEPTED))) {
407 // accept the request
408 info.optionState = OptInfo::ACKNOWLEDGED;
409 info.enabled = enabled;
410 } // else reject the request
412 transmit((info.local ? CMD_WILL : CMD_DO) + (info.enabled ? 0 : 1));
413 transmit(info.option);
418 OptionHandlerMap::const_iterator i (handlers_.find(info.option));
419 if (i != handlers_.end())
423 // This call must be AFTER calling v_init since v_init might increment the request count
424 // and v_setupComplete() might be called prematurely.
425 decrementRequestCounter();
428 prefix_ void senf::term::BaseTelnetProtocol::decrementRequestCounter()
430 if (pendingRequests_ > 0u) {
432 if (pendingRequests_ == 0u) {
439 ///////////////////////////////////////////////////////////////////////////
440 // senf::term::telnethandler::TerminalType
442 prefix_ senf::term::telnethandler::TerminalType::TerminalType()
444 registerHandler(this);
447 prefix_ void senf::term::telnethandler::TerminalType::nextTerminalType()
449 sendOptionParameters(telnetopt::TERMINAL_TYPE, "\x01");
452 prefix_ void senf::term::telnethandler::TerminalType::
453 v_handleOptionParameters(std::string const & data)
455 if (data.size() <= 0)
457 if (data[0] == '\x00') {
458 type_ = data.substr(1);
459 decrementRequestCounter();
463 prefix_ void senf::term::telnethandler::TerminalType::v_init()
466 incrementRequestCounter();
469 ///////////////////////////////////////////////////////////////////////////
470 // senf::term::telnethandler::NAWS
472 prefix_ senf::term::telnethandler::NAWS::NAWS()
473 : width_ (0u), height_ (0u)
475 registerHandler(this);
478 prefix_ void senf::term::telnethandler::NAWS::v_init()
480 incrementRequestCounter();
484 senf::term::telnethandler::NAWS::v_handleOptionParameters(std::string const & data)
486 if (data.size() != 4)
488 width_ = (static_cast<unsigned char>(data[0])<<8)+static_cast<unsigned char>(data[1]);
489 height_ = (static_cast<unsigned char>(data[2])<<8)+static_cast<unsigned char>(data[3]);
490 if (! requestsPending())
491 v_windowSizeChanged();
492 decrementRequestCounter();
495 ///////////////////////////////cc.e////////////////////////////////////////
497 //#include "Telnet.mpp"
503 // comment-column: 40
504 // c-file-style: "senf"
505 // indent-tabs-mode: nil
506 // ispell-local-dictionary: "american"
507 // compile-command: "scons -u test"