Reputation: 21
I want telnet client to pass interrupts/signals as their normal ASCII values in message to server.
I send following telnet command from server to client in my code.
IAC SB LINEMODE TRAPSIG 0 IAC SE
sprintf(msg, "%c%c%c%c%c%c%c", 255 /*IAC*/, 250 /*SB*/, 34 /*LINEMODE*/,
2 /*TRAPSIG*/, 0 /*mask*/, 255 /*IAC*/, 240 /*SE*/);
But on pressing Ctrl+c on telnet prompt, client still pass telnet commands rather then ASCII values.
Message received by server: IAC IP IAC DO 6 (timing mark)
Instead of "IAC IP" I want to receive ctrl+C (3).
Any idea how I can make client to send across ASCII values rather than telnet commands while server receives whole line from telnet client not character by character.
Any help would be highly appreciated!
Upvotes: 0
Views: 561
Reputation: 4135
It appears that your way to negotiate LINEMODE
is not correct.
You cannot send an IAC
SB
out of the blue for an option that hasn't been negotiated first with a WILL
or DO
or that hasn't been replied positively with a DO
or WILL
, respectively.
For line mode in particular, according to RFC 1184, the server side should only ever initiate IAC
DO
LINEMODE
(not a WILL
).
It would then wait for the client to reply with either IAC
WILL
LINEMODE
or IAC
WONT
LINEMODE
. In the latter case, there's nothing you can do, as the client isn't willing to support line mode at this point in time and subsequently sending a sub-negotiation will be pointless (a conforming client will then just ignore it).
If the client answered your request with IAC
WILL
LINEMODE
, that means further sub-negotiation of line mode will take place, but it will usually be initiated by the client, not by you (the server).
That means you should now wait for the client to send send IAC
SB
LINEMODE
something IAC
SE
and in reaction to that you should request a MODE
and a FORWARDMASK
as suboptions.
In particular, you want to request EDIT
be turned on in the client, as that's the actual mode where the client sends only complete lines, and more importantly you want to request TRAPSIG
be turned off in the client, as trapping the signals means the client intercepts the key presses in question and translates them into Telnet commands (such as IAC
IP
), which you don't want. That is:
IAC
SB
LINEMODE
MODE
1 IAC
SE
(1 means EDIT
on and TRAPSIG
off.)
And you also want to negotiate a FORWARDMASK
where all bits associated with the control characters you wish to receive are set, otherwise you will receive those characters only when the client sends the completed line or even not at all.
For Ctrl-C (ASCII 3) the minimum forward mask request would be:
IAC
SB
LINEMODE
DO
FORWARDMASK
16 IAC
SE
(The 16 is decimal. Note that the bits are counted starting from the MSB, not the LSB, which can be confusing if you're not aware of it.)
Then you will have to wait for the client to acknowledge both the mode as well as the forward mask. The client is required to honor the server's request for the EDIT
and TRAPSIG
settings if it previously agreed to negotiate line mode at all (however, this is no guarantee that it will pass the special key presses on to you), but it is free to refuse the forward mask.
In the end, if you want the client to successfully negotiate the desired options with you, you will need to make sure you stick the protocol as close as possible. I suggest you at least read and understand the sample communication in section 5.10 of the RFC.
Upvotes: 1