I made a simple network service with xinetd which reads a string from tcp socket and outputs its encoded view. Original binary(qrencode) just reading stdin.
It works fine when I use it with netcat
as echo string | nc <ip> <port>
But it doesn't answer when I try to use it via bash tcp redirection.
exec 7<>/dev/tcp/<ip>/<port>
echo string >&7
cat <&7
It waits forever. I tried echoing \004, cat /dev/null to this fd and no luck.
How can I make it working?
Best Answer
That
inetd
service or yours is probably waiting for EOF before exiting (and then closing the connection).To do that, you'd need the client to shutdown the sending side of the socket while still keeping the receive side open. That's what
nc
does when it detects EOF on its stdin.The
/dev/tcp/host/port
virtual interface ofbash
(copied from ksh) doesn't allow that. Evenzsh
'sztcp
more flexible alternative doesn't have the ability to asymmetrically shutting down connections.You could rely on a timeout instead, but that would be unreliable. So best it to keep relying on dedicated utilities like
nc
orsocat
(possibly with a coproc to have a similar interface), or use a programming language with a proper network API (with an interface toshutdown()
).The 0x04 character (
^D
) only means EOF for terminal devices when the line discipline is inicanon
mode (implements a crude line editor). So for that to work you'd have to insert a pseudo-terminal in between the socket created byxinetd
and your service. For instance, by starting it with:instead of
Then you'd need to send:
Or:
for your-service to see the end of input.
Because of that line editor,
your-service
will only see input when you send newline or^D
characters (we've disabled all the other editing characters likewerase
,erase
,kill
above and the^C
,^Z
... handling is also disabled as part ofraw
).So you might as well insert an input pre-processor instead that quits upon that 0x4 character like:
(note that that
awk
can't bemawk
asmawk
insists in not processing its input as long as it's not received a full buffer or eof).