2

I am using my open source Serial-to-IP converter (Serial Network Interface, SNI) to communicate with headless Slackware server using ttyS0 console. SNI during connection sends back to Telnet command set ff fb 01 ff fb 03. This turns Telnet from Line mode to Char mode. But i notice that Telnet replaces (adds) my CR press (0x0d) to two chars 0x0d 0x00. The Slackware's TTY itself is immune to this, but some commands i run under it, at least cat and mcedit, are not. So i try to do the following:

  1. Insert filter in my SNI so it just throw away NULLs at direction IP->Serial. It works, but it quite ugly, because no binary transfer will be allowed anymore.
  2. Modify Telnet's source code to remove addind NULL to CR:

    inetutils-1.9.4/telnet/telnet.c  from line 2294:
            case '\r':
              if (!crlf)
            {
              NETADD ('\r'); <-- added
    //        NET2ADD ('\r', '\0'); <-- removed
            }
              else
            {
              NET2ADD ('\r', '\n');
            }
              bol = flushline = 1;
              break;
    

It also work, so i ensure i am on the right way catching the problem. But it is also ugly due to multiple reasons, include that it is quite not portable, and obligate me to have my own non-standard telnet binary.

So the question is: It is possible to command to Telnet not to modify my CRs? (looking at this source code piece, it is not possible at all with original code, but i am sure i miss something, and this should be possible without modify source code). Note: Replace 0x0d to 0x0d, 0x0a (this is present in code) will not work with Linux TTY: it interprets this as two CRs.

And sub-question is: Where is 0x0d+0x00 used at all? I do not know any hardware device, teletype machine, etc., and any TTY, where NULL after CR used, at all. Thanks.

jpka
  • 131
  • 8

2 Answers2

1

So i take some investigation to see how this should be solved correctly, in terms of not to abuse the software, and to find the correct tools, or clearly show there are no such tools currently.

First of all, i show why my setup is exactly as it is, and why it uses (or not uses) standard tools.

My goal is very common and essential to Linux world (as i think before). Because Linux is network operating system, Linux box should be fully configurable via network (so can/should be fully useful when headless, i.e. without display monitor). But at time when Lilo/Grub starts, there is no network. Only serial ports are able at this time (and Lilo/Grub supports it). Why it is important to remote control at this time? Just because you can (remotely) compile new custom kernel for your Slackware box, and want to test it, adding as 2nd option in Lilo list, and want to return to original kernel even when remote machine stuck when booting, so no way to remotely edit/agjust Lilo options.

But serial console is really much more powerful tool for Linux machine. It shows boot messages and shut down messages which impossible to see via ssh, due to network is not initialized at these moments. (And remember, we do not have display). It lets you to (suddenly or intentionally) drop all network interfaces without a fear to lost your machine, when it is many miles from you.

Note: Serial console will not work out-of-box, but can be configured in quite well known standard ways, and described in many places. Example is http://docs.slackware.com/howtos:general_admin:serial_console

Note: There is problem that serial port should be BIOS-recognized, i.e., fully onboard. USB and PCIE devices will never work at stage when Lilo/Grub boots. Fortunately there is a good news. Hardware serial ports now (slowly) return to motherboards. I test new modern Asus J1900I-C for my server, it have two rs232 ports, and it all work fine.

To use serial console benefits remotely, some sort of SNI, Serial Network Interface, should be used (and it is the only way, AFAIK). SNI typically contains (simplest possible) TCP listening server, and raw IP-to-serial duplex translator. I start study and using SNIs when Lantronix X-Port was invented. It was in 2006, and was working fine with Slackware box. (I can't remember if there was \r\0 problems or not with XPort, but let's continue). XPort was too expensive, and was replaced with my own open source SNI immediately when Wiznet W5100 was issued. (Really, there is other, essential problem, why i need to replace Lantronix. It can't press Reset or Power buttons on motherboard, while my device can, and now server is absolutely under full remote control even after kernel panic; but it is offtopic here). Also nowadays there are many cheap no-named SNIs at online shops; i do not test these.

All these times i was using telnet to connect to SNI. The main reason was that XPort docs have examples of that. And, most times it working; i can't say that \r\0 problem stops my work. Most times it is not noticeable at all (say, mc commander is resistent to NULL 's). But mcedit fear the NULLs. And recently i start to catch the problem, so this question arrives as an result. (now please re-read from begin of thread).

And now i try to answer to my own question. All these times i was misuse the software. Telnet was not written for human communication; instead, Telnet (suddenly) use Telnet protocol, not Raw protocol; and \r\0s are probably part of protocol. (probably, because RFC says nothing about requirement of NULLs anywhere). While Telnet and Raw protocols may looks quite close, they are not match, so sometimes should work but sometimes not. Using bruteforce and recompiling binary, i show that Telnet can be very easily turned to Raw mode; but it is non-standard software after that, and there is no chance that it can be pushed to worldwide repos.

So i search for Raw utilites. netcat and ncat are not have Char mode, only Line mode, so only pure console possible, no ANSI colors, mc, passwords, etc. Putty is too complex and uses GUI. And... No more utilites i found! This was quite strange and annoying... Then i try to use initially character-based terminals, minicom and gtkterm. They are both do not allow feed ip:port structure istead of ttyS* name. But there is data translators exist, i try socat. It connects to SNI server and creates virtual serial port; then character terminal software connects to that port.

And that, finally, work. Whoa. But very many disadvantages in this chain; complex, hard to remember commands, can't be written in one line; when SNI drops TCP connection, it is impossible to see/catch that; a LOT of garbage issued by socat into virtual port at connection time (457 packets i count!). Here are the commands for brave people who may find my work useful.

sudo socat pty,link=/dev/ttyMYPORT,raw tcp:10.1.1.11:10001 &
then
sudo chmod a+rw /dev/ttyMYPORT; gtkterm -p /dev/ttyMYPORT
or 
sudo chmod a+rw /dev/ttyMYPORT; minicom -o --color=on -D /dev/ttyMYPORT

And conclusion, i do not found any simple software nowadays, that can be directly used to communicate in human raw character mode with serial port via network bridge. (Please suggest something). Thanks.

jpka
  • 131
  • 8
0

I believe the solution you are looking for is a few lines higher:

if (!crlf)

If we set crlf = true, then we would get \r\n instead of \r\0. That's probably going to work for cat and friends.

Looking through the telnet source, it appears crlf is some sort of "toggle" option. Looking an man telnet and a bit of Googling, it appears you can run something like this:

$ telnet
telnet> toggle crlf

...and you'll get \r\n sent from then on.

Community
  • 1
  • 1
Brian Malehorn
  • 2,627
  • 14
  • 15
  • 1
    Yes, `toggle crlf` works as you describe, but unf. it is not helpful for me: as i mention before, linux TTY interpret `\r\n` as two CRs (and now i test `cat` command, its behaviour is same). I am still wonder where `\r\0` used, and should Telnet app be fixed worldwide or not, and what happens then. Thank you for answer. – jpka Nov 25 '15 at 22:15
  • I'm sure the telnet folks have a good reason for sending `\r\0` and `\r\n`, and it probably shouldn't be "fixed" to a `\r`. I think the real problem may be outside of telnet: why are you sending `\r\n` when you press Enter? Your problems would be fixed if you only sent `\n`, which I think is what telnet expects. Maybe you can fix this in some terminal setting or Windows setting. – Brian Malehorn Nov 25 '15 at 23:35
  • 1
    My current setup with Telnet (Ubuntu, English UTF8 console) sends to telnet only `\r` code. Telnet currently for unknown reason can't just transfer `\r` unchanged, but replaces to `\r\0` or `\r\n`. So i am not sending `\r\n`, but Telnet does it. Using `\n` as CR is good idea, but looks like far not easy to obtain this, because i even can't imagine software level where to search, driver, console, etc. (I am using only Linux boxes). And that is far not standard for rs232 consoles, at least because key has code 0x0d, not 0x0a, worldwide and for at least half of century, AFAIK... – jpka Nov 26 '15 at 08:32