socat

What is socat?

socat stands for SOcket CAT. It is a utility for data transfer between two addresses.

Usage

The way I learn CLI tools is by first learning the usage of the tool, followed by committing a few simple commands to muscle memory. Usually, I can get by with those just all right. If I need to do something a little more involved, I can look at the man page, or failing that, I can Google it.

socat -d -d - TCP4:www.example.com:80
A typical socat invocation

Addresses

In order to understand socat it’s important to understand what addresses are and how they work.

~: socat2018/09/22 19:12:30 socat[15505] E exactly 2 addresses required (there are 0); use option "-h" for help
  • zero or more required address parameters separated by :
  • zero or more address options separated by ,
The anatomy of an address

Type

The type is used to specify the kind of address we need. Popular options are TCP4, CREATE, EXEC, GOPEN, STDIN, STDOUT, PIPE, PTY, UDP4 etc, where the names are pretty self-explanatory.

socat -d -d - TCP4:www.example.com:80

Parameters

Immediately after the type comes zero or more required address parameters separated by :

Address Options

Address parameters can be further enhanced with options, which govern how the opening of the address is done or what the properties of the resulting bytestreams will be.

TCP:www.example.com:80,retry=5
TCP4-LISTEN:www.example.com:80,fork
Adress options have a many to 1 mapping with option groups.

Addresses, redux

Now that we have a slightly better understanding of what addresses are, let’s see how data is transferred between the two addresses.

socat -u STDIN STDOUT
$ socat -u STDIN STDOUT2018/10/14 14:18:15 socat[22736] N using stdin for reading2018/10/14 14:18:15 socat[22736] N using stdout for writing
...
$ socat STDIN STDOUT2018/10/14 14:19:48 socat[22739] N using stdin for reading and writing2018/10/14 14:19:48 socat[22739] N using stdout for reading and writing

Single Address Specification and Dual Addresses

An address of the sort we’ve seen till now that conforms to the aforementioned format is known as a single address specification.

socat -d -d - TCP4:www.example.com:80
Two Single Address Specifications
socat -d -d   READLINE\!\!OPEN:file.txt,creat,trunc   SYSTEM:'read stdin; echo $stdin'

Address options versus socat options

It’s important to understand that the options that apply to an address is different from invoking socat itself with specific options, which govern how the socat tool behaves (as opposed to shaping how the address behaves).

The Lifecycle of a socat instance

A socat process goes through four phases. The first phase comprises of parsing the command line options and initializing logging. The second phase comprises of opening the first address followed by the second address. Opening of the addresses is usually a blocking operation, and per the man page, “especially for complex address types like SOCKS, connection requests or authentication dialogs must be completed before the next step is started.”

Examples

The man page for socat has a good list of examples, though I found understanding how addresses are constructed to be a prerequisite to understanding the examples.

Why use socat when you can use netcat

Netcat is a fantastic tool for network debugging and exploration, but it’s mostly limited to TCP and UDP connections. socat, in comparison, supports a very wide variety of address types.

A War Story

I first learned about socat while watching an ex-colleague fix what was a minor outage. At a previous job, we had a public facing API service A that spoke to another service B for a very, very, very small fraction of requests.

socat -d -d TCP-LISTEN:8004 TCP:10.2.7.21:8004

Conclusion

While I might’ve learned about socat for the first time while watching an SRE troubleshoot and fix a production issue, I personally use socat not so much as a production debugging tool than for troubleshooting local development issues (especially when developing network services).

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cindy Sridharan

Cindy Sridharan

@copyconstruct on Twitter. views expressed on this blog are solely mine, not those of present or past employers.