Search code examples
tcppipestdinfreezesocat

Socat hangs when piping stdin to tcp server


I have an embedded linux system running a tcp server. Telnet can connect to and use the tcp server interactively just fine.

I am trying to use socat to make the same connection, by linking stdin to the tcp server, like so:

socat - tcp:localhost:8088

I get the initial response from the server, and I can type, but as soon as I press enter, the terminal hangs (I can get out of this with Ctrl + C as long as I am not using the raw socat option.)

If I do the same thing but with readline, everything mostly works as expected:

socat readline tcp:localhost:8088

However, with readline, the terminal echoes my input, and then the response from the server also echoes it. This is more than just an annoyance, since things like passwords (which should not even be echoed once) are now visible.

Ideally, I would just use stdin instead of readline, so that I could send each character as soon as I type it, and turn off stdin echo. That way, when I type a character, the tcp server receives it and echoes it immediately, but only if it is supposed to.

But socat is hanging with the stdin option for reasons I don't understand.

I'm looking for an explanation as to why stdin is locking up the thread, or a workaround possibly using readline or similar.

EDIT: Here is the dddd debug output from the socat command when I use stdin:

sh-5.0# socat -d -d -d -d -lu - tcp:localhost:8088

2022/02/04 01:34:58.805536 socat[1074] D getpid()

2022/02/04 01:34:58.807631 socat[1074] D getpid() -> 1074

2022/02/04 01:34:58.807667 socat[1074] D setenv("SOCAT_PID", "1074", 1)

2022/02/04 01:34:58.807687 socat[1074] D setenv() -> 0

2022/02/04 01:34:58.807704 socat[1074] D setenv("SOCAT_PPID", "1074", 1)

2022/02/04 01:34:58.807724 socat[1074] D setenv() -> 0

2022/02/04 01:34:58.807737 socat[1074] I socat by Gerhard Rieger and contributors - see www.dest-unreach.org

2022/02/04 01:34:58.807752 socat[1074] I This product includes software developed by the OpenSSL Project for use in the OpenSSL Toolkit. (http://www.openssl.org/)

2022/02/04 01:34:58.807767 socat[1074] I This product includes software written by Tim Hudson ([email protected])

2022/02/04 01:34:58.807781 socat[1074] D socat version 1.7.3.3 on May 25 2021 13:47:29

2022/02/04 01:34:58.807795 socat[1074] D setenv("SOCAT_VERSION", "1.7.3.3", 1)

2022/02/04 01:34:58.807811 socat[1074] D setenv() -> 0

2022/02/04 01:34:58.807827 socat[1074] D running on Linux version #1 SMP Mon Jan 31 20:48:32 UTC 2022, release 5.4.0, machine aarch64

2022/02/04 01:34:58.807845 socat[1074] D argv[0]: "socat"

2022/02/04 01:34:58.807857 socat[1074] D argv[1]: "-d"

2022/02/04 01:34:58.807870 socat[1074] D argv[2]: "-d"

2022/02/04 01:34:58.807883 socat[1074] D argv[3]: "-d"

2022/02/04 01:34:58.807895 socat[1074] D argv[4]: "-d"

2022/02/04 01:34:58.807907 socat[1074] D argv[5]: "-lu"

2022/02/04 01:34:58.807920 socat[1074] D argv[6]: "-"

2022/02/04 01:34:58.807932 socat[1074] D argv[7]: "tcp:localhost:8088"

2022/02/04 01:34:58.807945 socat[1074] D sigaction(1, 0xffffc8d63a18, 0x0)

2022/02/04 01:34:58.807960 socat[1074] D sigaction() -> 0

2022/02/04 01:34:58.807973 socat[1074] D sigaction(2, 0xffffc8d63a18, 0x0)

2022/02/04 01:34:58.807987 socat[1074] D sigaction() -> 0

2022/02/04 01:34:58.808000 socat[1074] D sigaction(3, 0xffffc8d63a18, 0x0)

2022/02/04 01:34:58.808014 socat[1074] D sigaction() -> 0

2022/02/04 01:34:58.808026 socat[1074] D sigaction(4, 0xffffc8d63a18, 0x0)

2022/02/04 01:34:58.808040 socat[1074] D sigaction() -> 0

2022/02/04 01:34:58.808053 socat[1074] D sigaction(6, 0xffffc8d63a18, 0x0)

2022/02/04 01:34:58.808067 socat[1074] D sigaction() -> 0

2022/02/04 01:34:58.808079 socat[1074] D sigaction(7, 0xffffc8d63a18, 0x0)

2022/02/04 01:34:58.808092 socat[1074] D sigaction() -> 0

2022/02/04 01:34:58.808105 socat[1074] D sigaction(8, 0xffffc8d63a18, 0x0)

2022/02/04 01:34:58.808119 socat[1074] D sigaction() -> 0

2022/02/04 01:34:58.808132 socat[1074] D sigaction(11, 0xffffc8d63a18, 0x0)

2022/02/04 01:34:58.808146 socat[1074] D sigaction() -> 0

2022/02/04 01:34:58.808158 socat[1074] D sigaction(15, 0xffffc8d63a18, 0x0)

2022/02/04 01:34:58.808172 socat[1074] D sigaction() -> 0

2022/02/04 01:34:58.808185 socat[1074] D signal(13, 0x1)

2022/02/04 01:34:58.808200 socat[1074] D signal() -> 0x0

2022/02/04 01:34:58.808213 socat[1074] D atexit(0xaaaaba73c4a0)

2022/02/04 01:34:58.808226 socat[1074] D atexit() -> 0

2022/02/04 01:34:58.808267 socat[1074] D calloc(1, 848)

2022/02/04 01:34:58.808284 socat[1074] D calloc() -> 0xaaaadd2cc680

2022/02/04 01:34:58.808304 socat[1074] D malloc(1024)

2022/02/04 01:34:58.808318 socat[1074] D malloc() -> 0xaaaadd2cca00

2022/02/04 01:34:58.808333 socat[1074] D calloc(1, 848)

2022/02/04 01:34:58.808350 socat[1074] D calloc() -> 0xaaaadd2cce10

2022/02/04 01:34:58.808364 socat[1074] D calloc(1, 848)

2022/02/04 01:34:58.808377 socat[1074] D calloc() -> 0xaaaadd2cd170

2022/02/04 01:34:58.808391 socat[1074] D isatty(0)

2022/02/04 01:34:58.808411 socat[1074] D isatty() -> 1

2022/02/04 01:34:58.808424 socat[1074] D tcgetattr(0, 0xaaaadd2ccf6c)

2022/02/04 01:34:58.808461 socat[1074] D tcgetattr(, {00005400,00000005,00001cb2,0000083b, 4098,4098, 03,1c,7f,15,04,00,01,00,11,13,1a,00,12,0f,17,16,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00}) -> 0

2022/02/04 01:34:58.808480 socat[1074] D isatty(1)

2022/02/04 01:34:58.808494 socat[1074] D isatty() -> 1

2022/02/04 01:34:58.808507 socat[1074] D tcgetattr(1, 0xaaaadd2cd2cc)

2022/02/04 01:34:58.808542 socat[1074] D tcgetattr(, {00005400,00000005,00001cb2,0000083b, 4098,4098, 03,1c,7f,15,04,00,01,00,11,13,1a,00,12,0f,17,16,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00}) -> 0

2022/02/04 01:34:59.141868 socat[1074] D malloc(128)

2022/02/04 01:34:59.141888 socat[1074] D malloc() -> 0xaaaadd2cd4d0

2022/02/04 01:34:59.141903 socat[1074] D malloc(128)

2022/02/04 01:34:59.141916 socat[1074] D malloc() -> 0xaaaadd2cd560

2022/02/04 01:34:59.141931 socat[1074] N reading from and writing to stdio

2022/02/04 01:34:59.141946 socat[1074] D calloc(1, 848)

2022/02/04 01:34:59.141959 socat[1074] D calloc() -> 0xaaaadd2cd5f0

2022/02/04 01:34:59.141983 socat[1074] D malloc(1024)

2022/02/04 01:34:59.141997 socat[1074] D malloc() -> 0xaaaadd2cd9b0

2022/02/04 01:34:59.142015 socat[1074] D getaddrinfo("localhost", NULL, {1,0,1,6,0,0x0,0x0,0x0}, 0xffffc8d63538)

2022/02/04 01:34:59.142789 socat[1074] D getaddrinfo(,,,{{AF=10 [0000:0000:0000:0000:0000:0000:0000:0001]:0, }) -> 0

2022/02/04 01:34:59.142815 socat[1074] D malloc(128)

2022/02/04 01:34:59.142830 socat[1074] D malloc() -> 0xaaaadd2ce610

2022/02/04 01:34:59.142847 socat[1074] N opening connection to AF=2 127.0.0.1:8088

2022/02/04 01:34:59.142861 socat[1074] I starting connect loop

2022/02/04 01:34:59.142876 socat[1074] D socket(2, 1, 6)

2022/02/04 01:34:59.142900 socat[1074] I socket(2, 1, 6) -> 5

2022/02/04 01:34:59.142919 socat[1074] D fcntl(5, 2, 1)

2022/02/04 01:34:59.142935 socat[1074] D fcntl() -> 0

2022/02/04 01:34:59.142954 socat[1074] D connect(5, {2,AF=2 127.0.0.1:8088}, 16)

2022/02/04 01:34:59.145199 socat[1074] D connect() -> 0

2022/02/04 01:34:59.145246 socat[1074] D getsockname(5, 0xffffc8d635b8, 0xffffc8d6359c{112})

2022/02/04 01:34:59.145269 socat[1074] D getsockname(, {AF=2 127.0.0.1:46294}, {16}) -> 0

2022/02/04 01:34:59.145286 socat[1074] N successfully connected from local address AF=2 127.0.0.1:46294

2022/02/04 01:34:59.145306 socat[1074] I resolved and opened all sock addresses

2022/02/04 01:34:59.145321 socat[1074] D malloc(16385)

2022/02/04 01:34:59.145344 socat[1074] D malloc() -> 0xaaaadd2cf400

2022/02/04 01:34:59.145360 socat[1074] N starting data transfer loop with FDs [0,1] and [5,5]

2022/02/04 01:34:59.145375 socat[1074] D data loop: sock1->eof=0, sock2->eof=0, closing=0, wasaction=1, total_to={0.000000}

2022/02/04 01:34:59.145395 socat[1074] D select(6, &0x21, &0x22, &0x0, NULL/0.000000)

2022/02/04 01:34:59.145426 socat[1074] D select -> (, 0x0, 0x20, 0x0, NULL/0.000000), 1

2022/02/04 01:34:59.145443 socat[1074] D data loop: sock1->eof=0, sock2->eof=0, closing=0, wasaction=1, total_to={0.000000}

2022/02/04 01:34:59.145461 socat[1074] D select(6, &0x21, &0x2, &0x0, NULL/0.000000)

2022/02/04 01:34:59.157132 socat[1074] D select -> (, 0x20, 0x0, 0x0, NULL/0.000000), 1

2022/02/04 01:34:59.157178 socat[1074] D data loop: sock1->eof=0, sock2->eof=0, closing=0, wasaction=1, total_to={0.000000}

2022/02/04 01:34:59.157198 socat[1074] D select(2, &0x1, &0x2, &0x0, NULL/0.000000)

2022/02/04 01:34:59.401692 socat[1074] D select -> (, 0x0, 0x2, 0x0, NULL/0.000000), 1

2022/02/04 01:34:59.401732 socat[1074] D read(5, 0xaaaadd2cf400, 8192)

2022/02/04 01:34:59.401765 socat[1074] D read -> 48

2022/02/04 01:34:59.401782 socat[1074] D write(1, 0xaaaadd2cf400, 48)

ÿûÿû

Connect to IP address 169.254.12.16

2022/02/04 01:34:59.401809 socat[1074] D write -> 48

2022/02/04 01:34:59.401823 socat[1074] I transferred 48 bytes from 5 to 1

2022/02/04 01:34:59.401838 socat[1074] D data loop: sock1->eof=0, sock2->eof=0, closing=0, wasaction=1, total_to={0.000000}

2022/02/04 01:34:59.401856 socat[1074] D select(6, &0x21, &0x2, &0x0, NULL/0.000000)

2022/02/04 01:34:59.462969 socat[1074] D select -> (, 0x0, 0x2, 0x0, NULL/0.000000), 1

2022/02/04 01:34:59.463017 socat[1074] D data loop: sock1->eof=0, sock2->eof=0, closing=0, wasaction=1, total_to={0.000000}

2022/02/04 01:34:59.463036 socat[1074] D select(6, &0x21, &0x0, &0x0, NULL/0.000000)

At this point, I can still type, but no matter what I do, I get nothing in response.


Solution

  • Turns out it was a combination of things.

    1. The version of socat I was using (1.7.3.3) has a bug when using certain options. See https://bugs.launchpad.net/ubuntu/+source/socat/+bug/1883957 for more info.
    2. The tty options in my terminal were atypical somehow. By using "stty sane", and then adding -brkint, I got the entire thing working with stdin.

    My final solution uses socat version 1.7.4.3, and looks like this:

    stty sane
    
    stty -brkint -icanon && socat -,echo=0 tcp:localhost:8088