📄 telnet.cxx
字号:
PDebugError << "queued."; opt.ourState = OptionInfo::WantYesQueued; break; case OptionInfo::WantYesQueued : PDebugError << "already queued." << endl; opt.ourState = OptionInfo::IsYes; return FALSE; } PDebugError << endl; return TRUE;}BOOL PTelnetSocket::SendSubOption(BYTE code, const BYTE * info, PINDEX len, int subCode){ if (!StartSend("SendSubOption", code)) return FALSE; PDebugError << "with " << len << " bytes." << endl; PBYTEArray buffer(len + 6); buffer[0] = IAC; buffer[1] = SB; buffer[2] = code; PINDEX i = 3; if (subCode >= 0) buffer[i++] = (BYTE)subCode; while (len-- > 0) { if (*info == IAC) buffer[i++] = IAC; buffer[i++] = *info++; } buffer[i++] = IAC; buffer[i++] = SE; return PTCPSocket::Write((const BYTE *)buffer, i);}void PTelnetSocket::SetTerminalType(const PString & newType){ terminalType = newType;}void PTelnetSocket::SetWindowSize(WORD width, WORD height){ windowWidth = width; windowHeight = height; if (IsOurOption(WindowSize)) { BYTE buffer[4]; buffer[0] = (BYTE)(width >> 8); buffer[1] = (BYTE)width; buffer[2] = (BYTE)(height >> 8); buffer[3] = (BYTE)height; SendSubOption(WindowSize, buffer, 4); } else { SetOurOption(WindowSize); SendWill(WindowSize); }}void PTelnetSocket::GetWindowSize(WORD & width, WORD & height) const{ width = windowWidth; height = windowHeight;}BOOL PTelnetSocket::Read(void * data, PINDEX bytesToRead){ PBYTEArray buffer(bytesToRead); PINDEX charsLeft = bytesToRead; BYTE * dst = (BYTE *)data; while (charsLeft > 0) { BYTE * src = buffer.GetPointer(charsLeft); if (!PTCPSocket::Read(src, charsLeft)) { lastReadCount = bytesToRead - charsLeft; return lastReadCount > 0; } while (lastReadCount > 0) { BYTE currentByte = *src++; lastReadCount--; switch (state) { case StateCarriageReturn : state = StateNormal; if (currentByte == '\0') break; // Ignore \0 after CR // Else, fall through for normal processing case StateNormal : if (currentByte == IAC) state = StateIAC; else { if (currentByte == '\r' && !IsTheirOption(TransmitBinary)) state = StateCarriageReturn; *dst++ = currentByte; charsLeft--; } break; case StateIAC : switch (currentByte) { case IAC : state = StateNormal; *dst++ = IAC; charsLeft--; break; case DO : state = StateDo; break; case DONT : state = StateDont; break; case WILL : state = StateWill; break; case WONT : state = StateWont; break; case DataMark : // data stream portion of a Synch /* We may have missed an urgent notification, so make sure we flush whatever is in the buffer currently. */ PTelnetError << "received DataMark" << endl; if (synchronising > 0) synchronising--; break; case SB : // subnegotiation start state = StateSubNegotiations; subOption.SetSize(0); break; default: if (OnCommand(currentByte)) state = StateNormal; break; } break; case StateDo : OnDo(currentByte); state = StateNormal; break; case StateDont : OnDont(currentByte); state = StateNormal; break; case StateWill : OnWill(currentByte); state = StateNormal; break; case StateWont : OnWont(currentByte); state = StateNormal; break; case StateSubNegotiations : if (currentByte == IAC) state = StateEndNegotiations; else subOption[subOption.GetSize()] = currentByte; break; case StateEndNegotiations : if (currentByte == SE) state = StateNormal; else if (currentByte != IAC) { /* This is an error. We only expect to get "IAC IAC" or "IAC SE". Several things may have happend. An IAC was not doubled, the IAC SE was left off, or another option got inserted into the suboption are all possibilities. If we assume that the IAC was not doubled, and really the IAC SE was left off, we could get into an infinate loop here. So, instead, we terminate the suboption, and process the partial suboption if we can. */ state = StateIAC; src--; // Go back to character for IAC ccommand } else { subOption[subOption.GetSize()] = currentByte; state = StateSubNegotiations; break; // Was IAC IAC, subnegotiation not over yet. } if (subOption.GetSize() > 1 && IsOurOption(subOption[0])) OnSubOption(subOption[0], ((const BYTE*)subOption)+1, subOption.GetSize()-1); break; default : PTelnetError << "illegal state: " << (int)state << endl; state = StateNormal; } if (synchronising > 0) { charsLeft = bytesToRead; // Flush data being received. dst = (BYTE *)data; } } } lastReadCount = bytesToRead; return TRUE;}void PTelnetSocket::OnDo(BYTE code){ PTelnetError << "OnDo " << GetTELNETOptionName(code) << ' '; OptionInfo & opt = option[code]; switch (opt.ourState) { case OptionInfo::IsNo : if (opt.weCan) { PDebugError << "WILL."; SendCommand(WILL, code); opt.ourState = OptionInfo::IsYes; } else { PDebugError << "WONT."; SendCommand(WONT, code); } break; case OptionInfo::IsYes : PDebugError << "ignored."; break; case OptionInfo::WantNo : PDebugError << "is answer to WONT."; opt.ourState = OptionInfo::IsNo; break; case OptionInfo::WantNoQueued : PDebugError << "impossible answer."; opt.ourState = OptionInfo::IsYes; break; case OptionInfo::WantYes : PDebugError << "accepted."; opt.ourState = OptionInfo::IsYes; break; case OptionInfo::WantYesQueued : PDebugError << "refused."; opt.ourState = OptionInfo::WantNo; SendCommand(WONT, code); break; } PDebugError << endl; if (IsOurOption(code)) { switch (code) { case TerminalSpeed : { static BYTE defSpeed[] = "38400,38400"; SendSubOption(TerminalSpeed,defSpeed,sizeof(defSpeed)-1,SubOptionIs); } break; case TerminalType : SendSubOption(TerminalType, terminalType, terminalType.GetLength(), SubOptionIs); break; case WindowSize : SetWindowSize(windowWidth, windowHeight); break; } }}void PTelnetSocket::OnDont(BYTE code){ PTelnetError << "OnDont " << GetTELNETOptionName(code) << ' '; OptionInfo & opt = option[code]; switch (opt.ourState) { case OptionInfo::IsNo : PDebugError << "ignored."; break; case OptionInfo::IsYes : PDebugError << "WONT."; opt.ourState = OptionInfo::IsNo; SendCommand(WONT, code); break; case OptionInfo::WantNo : PDebugError << "disabled."; opt.ourState = OptionInfo::IsNo; break; case OptionInfo::WantNoQueued : PDebugError << "accepting."; opt.ourState = OptionInfo::WantYes; SendCommand(DO, code); break; case OptionInfo::WantYes : PDebugError << "queued disable."; opt.ourState = OptionInfo::IsNo; break; case OptionInfo::WantYesQueued : PDebugError << "refused."; opt.ourState = OptionInfo::IsNo; break; } PDebugError << endl;}void PTelnetSocket::OnWill(BYTE code){ PTelnetError << "OnWill " << GetTELNETOptionName(code) << ' '; OptionInfo & opt = option[code]; switch (opt.theirState) { case OptionInfo::IsNo : if (opt.theyShould) { PDebugError << "DO."; SendCommand(DO, code); opt.theirState = OptionInfo::IsYes; } else { PDebugError << "DONT."; SendCommand(DONT, code); } break; case OptionInfo::IsYes : PDebugError << "ignored."; break; case OptionInfo::WantNo : PDebugError << "is answer to DONT."; opt.theirState = OptionInfo::IsNo; break; case OptionInfo::WantNoQueued : PDebugError << "impossible answer."; opt.theirState = OptionInfo::IsYes; break; case OptionInfo::WantYes : PDebugError << "accepted."; opt.theirState = OptionInfo::IsYes; break; case OptionInfo::WantYesQueued : PDebugError << "refused."; opt.theirState = OptionInfo::WantNo; SendCommand(DONT, code); break; } PDebugError << endl;}void PTelnetSocket::OnWont(BYTE code){ PTelnetError << "OnWont " << GetTELNETOptionName(code) << ' '; OptionInfo & opt = option[code]; switch (opt.theirState) { case OptionInfo::IsNo : PDebugError << "ignored."; break; case OptionInfo::IsYes : PDebugError << "DONT."; opt.theirState = OptionInfo::IsNo; SendCommand(DONT, code); break; case OptionInfo::WantNo : PDebugError << "disabled."; opt.theirState = OptionInfo::IsNo; break; case OptionInfo::WantNoQueued : PDebugError << "accepting."; opt.theirState = OptionInfo::WantYes; SendCommand(DO, code); break; case OptionInfo::WantYes : PDebugError << "refused."; opt.theirState = OptionInfo::IsNo; break; case OptionInfo::WantYesQueued : PDebugError << "queued refusal."; opt.theirState = OptionInfo::IsNo; break; } PDebugError << endl;}void PTelnetSocket::OnSubOption(BYTE code, const BYTE * info, PINDEX len){ PTelnetError << "OnSubOption " << GetTELNETOptionName(code) << " of " << len << " bytes." << endl; switch (code) { case TerminalType : if (*info == SubOptionSend) SendSubOption(TerminalType, terminalType, terminalType.GetLength(), SubOptionIs); break; case TerminalSpeed : if (*info == SubOptionSend) { static BYTE defSpeed[] = "38400,38400"; SendSubOption(TerminalSpeed,defSpeed,sizeof(defSpeed)-1,SubOptionIs); } break; }}BOOL PTelnetSocket::OnCommand(BYTE code){ PTelnetError << "unknown command " << (int)code << endl; return TRUE;}void PTelnetSocket::OnOutOfBand(const void *, PINDEX length){ PTelnetError << "out of band data received of length " << length << endl; synchronising++;}// End Of File ///////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -