Skip to content

Instantly share code, notes, and snippets.

@silverjam
Last active April 12, 2019 07:12
Show Gist options
  • Save silverjam/485cc01d20e6f72215e554ffd1b3b9a6 to your computer and use it in GitHub Desktop.
Save silverjam/485cc01d20e6f72215e554ffd1b3b9a6 to your computer and use it in GitHub Desktop.
socat experiments
# Create input FIFO
rm -f /tmp/input.fifo
mkfifo /tmp/input.fifo
# Bind to FD in the shell to keep the FIFO alive
exec 10<>/tmp/input.fifo
# Create output FIFO
rm -f /tmp/output.fifo
mkfifo /tmp/output.fifo
# Bind to FD in the shell to keep the FIFO alive
exec 11<>/tmp/output.fifo
# Configure the FIFOs to have a 4096 byte buffer (the minimum)
# 1031 is F_SETPIPE_SZ, see http://man7.org/linux/man-pages/man2/fcntl.2.html
python -c 'import fcntl; fifo = open("/tmp/input.fifo", "wb"); print(fcntl.fcntl(fifo.fileno(), 1031, 4096))'
python -c 'import fcntl; fifo = open("/tmp/output.fifo", "rb"); print(fcntl.fcntl(fifo.fileno(), 1031, 4096))'
# Generate a 4k test file
python -c "import sys; sys.stdout.write(bytes.join(b'', [chr(X) for X in range(256)] * 16))" >4k.bin
# Launch server
socat -d -d -d tcp-listen:2101,reuseaddr,fork - >/tmp/output.fifo </tmp/input.fifo &
# Write more data to the server than the pipes can hold
dd if=4k.bin bs=4096 count=4 | socat - tcp:localhost:2101
# Check how much data is in the pipe:
cat /tmp/output.fifo | xxd
# Last line of output should look like this (showing that there's only 4k of data):
# > 00000ff0: f0f1 f2f3 f4f5 f6f7 f8f9 fafb fcfd feff ................
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment