# Named Pipe

> Short explanation of named pipes (FIFOs) on Linux and how they differ from regular pipes, plus a small netcat example that turns them into a simple bidirectional TCP proxy.

Author: Elnur Badalov
Published: 2024-07-12
Updated: 2024-07-12
Tags: linux
Source: https://elnurbda.com/posts/12-named-pipe/
License: CC BY 4.0, please cite the source URL.

---

# What is a Named Pipe?

Named pipes, also known as FIFOs (First In, First Out), are a powerful feature in Unix-like operating systems that allow for unidirectional or bidirectional communication between processes. They function similarly to regular pipes, but with the key difference that they exist as a file in the filesystem, making them accessible by unrelated processes. A command for creating named pipes: `mkfifo`.

Simple example with `cat`:

<video controls autoplay muted>
  <source src="/assets/12-named-pipe/mkfifo.mp4" type="video/mp4">
  <source src="/assets/12-named-pipe/mkfifo.mp4" type="video/ogg">
Your browser does not support the video tag.
</video>

## Using `netcat` as a Proxy

A practical example of using named pipes can be demonstrated with `netcat`.

### Standard `netcat` Proxy

One useful behavior is using `netcat` as a proxy to redirect both ports and hosts.

```bash
nc -lvnp 12345 | nc -v some.website 80
```

In this command:

- `nc -l 12345` starts a `netcat` server listening on port 12345.
- Any connection to this port is piped (`|`) to `nc some.website 80`, which connects to `some.website` on port 80.

When a web browser makes a request to the `netcat` server on port 12345, the request is forwarded to `some.website:80`. However, the response from the Website is not sent back to the web browser because standard pipes are unidirectional.

### Working Around with Named Pipes

This limitation can be overcome using a named pipe to redirect both input and output.

```bash
mkfifo backpipe
nc -l 12345 0<backpipe | nc some.website 80 1>backpipe
```

In this setup:

- `mkfifo backpipe` creates a named pipe called `backpipe`.
- `nc -l 12345 0<backpipe` starts a `netcat` server on port 12345, reading its input from `backpipe`.
- `| nc some.website 80 1>backpipe` forwards the request to `some.website:80` and writes the response back to `backpipe`.

This configuration allows bidirectional communication through the named pipe, ensuring that the response from the Website is sent back to the web browser that made the request.

### Visual Representation

#### Normal Pipe

<video controls autoplay muted>
  <source src="/assets/12-named-pipe/normal_pipe.mp4" type="video/mp4">
  <source src="/assets/12-named-pipe/normal_pipe.mp4" type="video/ogg">
Your browser does not support the video tag.
</video>

#### Named Pipe

<video controls autoplay muted>
  <source src="/assets/12-named-pipe/named_pipe.mp4" type="video/mp4">
  <source src="/assets/12-named-pipe/named_pipe.mp4" type="video/ogg">
Your browser does not support the video tag.
</video>

By using named pipes, you can effectively manage bidirectional data flow between processes, making it a valuable tool for more complex networking tasks and process communication in Unix-like systems.

# References

- <https://en.wikipedia.org/wiki/Netcat>
- <https://en.wikipedia.org/wiki/Named_pipe>