0

For some reason, I want to create a server for the standard Telnet application as client to connect to. I wrote a basic TCP server in C++, its purpose is to serve an interactive menu, so I have to set ECHO on my side.

When the client connects, I send IAC WILL ECHO to it, and I expected to get IAC DO ECHO. To my surprise, what I get is IAC DO ECHO, IAC WON'T ECHO, IAC DON'T ECHO. Three messages one by one. And the client is still echoing by itself.

What am I doing wrong? What do those three messages mean?

int main() {
    TcpListener tcpListener(10001);  // custom tcp helper library
    auto stream = tcpListener.acceptClient();
    vector<uint8_t> msg = {255, 251, 1}; // send IAC WILL ECHO on start
    stream.writeBytes(msg);
    auto message = stream.readBytes(100);
    while (!message.empty()) {
        for (auto c : message) cout << (int)c << endl; // to see what i'm getting from client
        cout << "length: " << message.size() << "\n";
        stream.writeBytes(message); // echo back
        message = stream.readBytes(100);
    }
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
banasraf
  • 11
  • 3
  • Which telnet client did you use? Which version of it? – Some programmer dude Apr 19 '18 at 15:08
  • Oh, and welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Apr 19 '18 at 15:10
  • I'm using telnet client which came with ubuntu, Don't know how to check version. It has no -v option. – banasraf Apr 19 '18 at 15:13
  • Okay, that should pretty standards compliant. If it was Windows it could be a different matter. Now for your problem, since you don't show your code it's hard to say if you implement the telnet state machine correctly. If you don't want to show your code, then can you at least go to the telnet programs own command prompt and enable some debug options? You go to the command prompt by pressing `Ctrl-^`. Then enter the commands `set options` to enable option-negotiating debugging, and `set prettydump` and optionally `set debug`. You might need to use `Ctrl-^` in between the commands. – Some programmer dude Apr 19 '18 at 15:21
  • Well. Thanks for help. 'set options' was very helpful there. And it all has just shown I'm an idiot. My problem was I was resending all messages to client. Option negotating too. Thanks once again. – banasraf Apr 19 '18 at 15:30
  • I kind of guessed. :) Anyway, those `set` commands are very useful. Always enable them while developing the state machine. :) – Some programmer dude Apr 19 '18 at 15:37
  • 1
    @banasraf • Don't sell yourself short. This stuff is hard. – Eljay Apr 19 '18 at 15:38

1 Answers1

1

I think my mistake can be now seen in my code, but I worked on some more complicated version of it. The problem is I echoed all messages from client, so when I sent IAC WILL ECHO it responded with IAC DO ECHO. When I echoed it client said it won't do it and then sent IAC DON'T ECHO (to clear the situation I think). So of course to avoid such errors first thing in telnet server is to properly handle negotation.

banasraf
  • 11
  • 3
  • 1
    Yup, don't blindly echo back everything the client sends. Never echo IAC commands, handle them privately, and only echo back non-IAC data if the other party asked you to echo. I suggest you read [RFC 857 TELNET ECHO OPTION](https://tools.ietf.org/html/rfc857) for more details. – Remy Lebeau Apr 19 '18 at 21:12