socat(1)

NAME
socat - Multipurpose relay (SOcket CAT)

SYNOPSIS
socat [options] <address> <address>
socat -V
socat -?[?]
filan
procan

DESCRIPTION

Socat is a command line based utility that establishes two bidirectional byte streams and transfers data between them. Because the streams can be constructed from a large set of different types of data sinks and sources (see address types), and because lots of address options may be applied to the streams, socat can be used for many different purposes. It might be one of the tools that one `has already needed'.

Filan is a utility that prints information about its active file descriptors to stdout. It has been written for debugging socat, but might be useful for other purposes too.

Procan is a utility that prints information about process related system parameters to stdout. It has been written to better understand some UNIX process properties and for debugging socat, but might be useful for other purposes too.

The lifecycle of a socat instance typically consists of four phases.

In the init phase, the command line options are parsed and logging is initialized.

During the open phase, socat opens the first address and afterwards the second address. These steps are usually blocking; thus, for complex address types like socks, connection requests or authentication dialogs must be completed before the next step is started.

In the transfer phase, socat watches both streams' read file descriptors via select(), and, when data is available, reads it in portions of at most 8192 bytes (change with option -b), performs newline character conversions if required, and writes the data to the other stream.

When one of the streams effectively reaches EOF, the closing phase begins. Socat tries to transfer the EOF condition to the other stream, giving it a chance to terminate gracefully.

OPTIONS

Socat provides some command line options that modify the behaviour of the program. They have nothing to do with so called address options that are used as parts of address specifications.

-V  Print version and available feature information to stdout, and exit.

-?  Print a help text to stdout describing command line options and available address types, and exit.

-?? Like -?, plus a list of all available address options. Some options are platform dependent, so this output is helpful for checking the particular implementation.

-d  Without this option, only fatal and error messages are generated; applying this option also prints warning messages. See DIAGNOSTICS for more information.

-d -d
    Prints fatal, error, warning, and notice messages.

-d -d -d
    Prints fatal, error, warning, notice, and info messages.

-d -d -d -d
    Prints fatal, error, warning, notice, info, and debug messages.

-D  Logs information about file descriptors before starting the transfer phase.

-ly[<facility>]
    Writes messages to syslog instead of stderr; severity as defined with -d option. With optional <facility>, the syslog type can be selected, default is "daemon".

-lf <logfile>
    Writes messages to <logfile> [filename] instead of stderr.

-ls Writes messages to stderr (this is the default).

-lm[<facility>]
    Mixed log mode. During startup messages are printed to stderr; when socat starts the transfer phase loop or daemon mode (i.e. after opening all streams and before starting data transfer, or, with listening sockets with fork option, before the first accept call), it switches logging to syslog. With optional <facility>, the syslog type can be selected, default is "daemon".

-v  Writes the transferred data not only to their target streams, but also to stderr. The output format is text with some conversions for readability, and prefixed with "> " or "< " indicating flow directions.

-x  Writes the transferred data not only to their target streams, but also to stderr. The output format is hexadecimal, prefixed with "> " or "< " indicating flow directions. Can not be combined with -v.

-b<size>
    Sets the data transfer block <size> [size_t]. At most <size> bytes are transferred per step. Default is 8192 bytes.

-s  By default, socat terminates when an error occurred to prevent the process from running even if some security option could not be applied. With this option, socat is sloppy with errors and tries to continue (but still exits on fatals).

-t<timeout>
    If one channel has reached EOF, the write part of the other channel is shut down. Then, socat waits <timeout> [timeval] seconds before terminating. Default is 0.5 seconds.

-u  Uses unidirectional mode. The first address is only used for reading, and the second address is only used for writing.

-U  Uses unidirectional mode in reverse direction. The first address is only used for writing, and the second address is only used for reading.

-g  During address option parsing, don't check option group membership.

ADDRESS SPECIFICATIONS

With the address command line arguments, the user gives socat instructions and the necessary information for establishing the byte streams.

An address specification usually consists of an address type keyword, zero or more address parameters separated by ':' from the keyword and from each other, and zero or more address options separated by ','.

The keyword specifies the address type (e.g., TCP4, OPEN, EXEC). For some keywords there exist synonyms (- for STDIO, TCP for TCP4). Keywords are case insensitive. For a few special address types, the keyword may be omitted: Address specifications starting with a number are assumed to be FD addresses; if a '/' is found before the first ':' or ',', GOPEN is assumed.

The required number and type of address parameters depend on the address type. E.g., TCP4 requires a server name or server address, and a port or service specification.

Zero or more address options may be given with each address. Options are members of option groups. With each address type there is a set of option groups defined. Only options belonging to one of these address groups may be used (except with option -g). Options consist of an option keyword or an option keyword and a value, separated by '='. Option keywords are case insensitive.

Address specifications following the above schema are also called single address specifications. Two single addresses can be combined with "!!" to form a dual type address for one channel. Here, the first address is used by socat for reading data, and the second address for writing data. There is no way to specify an option only once for being applied to both single addresses.

Usually, addresses are opened in read/write mode. When an address is part of a dual address specification, or when option -u is used, an address might be used only for reading or for writing. Considering this is important with some address types.

ADDRESS TYPES

This section describes the available address types with their keywords, parameters, and semantics.

CREATE:<filename>
    Opens <filename> with creat() and uses the file descriptor for writing. This address type requires write-only context. <filename> must be a valid existing or not existing path. If <filename> is a named pipe, creat() might block; if <filename> refers to a socket, this is an error.
    Option groups: FD,REG,NAMED
    Useful options: mode, user, group, unlink-early, unlink-late, append
    See also: OPEN, GOPEN

EXEC:<command-line>
    Invokes the program with execvp(). <command-line> is a simple command with arguments separated by single spaces. If the program name contains a '/', the part behind the last '/' is taken as ARGV[0]. If the program name is a relative path, the execvp() semantics for finding the program via $PATH apply. After successful program start, socat writes data to stdin of the process and reads from its stdout using a UNIX domain socket generated by socketpair() per default.
    Option groups: FD,FIFO,SOCKET,EXEC,FORK,TERMIOS,UNIX
    Useful options: path, fdin, fdout, chroot, su, su-d, pty, stderr, ctty, setsid, pipes, login
    See also: SYSTEM

FD:<fdnum>
    Uses the file descriptor <fdnum>. It must already exist as valid UN*X file descriptor.
    Option groups: FD (FIFO,TERMIOS,BLK,REG,SOCKET,UNIX)
    See also: STDIO, STDIN, STDOUT, STDERR

GOPEN:<filename>
    (Generic open) This address type tries to handle any file system entry except directories usefully. <filename> may be a relative or absolute path. If it already exists, its type is checked. In case of a UNIX domain socket, socat connects; if connecting fails, socat assumes a datagram socket and uses sendto() calls. If the entry is not a socket, socat opens it applying the O_APPEND flag. If it does not exist, it is opened with flag O_CREAT as a regular file.
    Option groups: FD,REG,SOCKET,NAMED,OPEN,UNIX
    See also: OPEN, CREATE, UNIX-CONNECT

IP4:<host>:<protocol>
    Opens a raw IPv4 socket with <protocol>, sends packets to <host> [IPv4 address] and receives packets from host. Protocol 255 uses the raw socket with the IP header being part of the data.
    Option groups: FD,SOCKET,IP4
    Useful options: ttl, broadcast
    See also: IP6, UDP4, UDP4-LISTEN

IP6:<host>:<protocol>
    Opens a raw IPv6 socket with <protocol>, sends packets to <host> [IPv6 address] and receives packets from host.
    Option groups: FD,SOCKET,IP6
    Useful options: ttl, broadcast
    See also: IP4, UDP6, UDP6-LISTEN

OPEN:<filename>
    Opens <filename> using the open() system call. This operation fails on UNIX domain sockets.
    Note: This address type is rarly useful in bidirectional mode.
    Option groups: FD,REG,NAMED,OPEN
    Useful options: creat, excl, nofollow, append, rdonly, wronly, lock, ignoreeof
    See also: CREATE, GOPEN, UNIX-CONNECT

OPENSSL:<host>:<port>
    Tries to establish a SSL connection to <port> [TCP service] on <host> [IPv4 address] using TCP/IPv4.
    Note: this is currently only an experimental integration of openssl! (it does not provide any trust between the peers because is does not check certificates!) Option groups: FD,SOCKET,SOCK_IP,IP_TCP,OPENSSL
    Useful options: cipherlist, method, verify
    See also: TCP4

PIPE:<filename>
    If <filename> already exists, it is opened. If is does not exist, a named pipe is created and opened.
    Note: When a pipe is used for both reading and writing, it works as echo service.
    Option groups: FD,FIFO,NAMED,OPEN
    Useful options: rdonly, nonblock, group, user, mode, unlink-early
    See also: unnamed pipe

PIPE
    Creates an unnamed pipe and uses it for reading and writing. It works as an echo, because everything written to it appeares immediately as read data.
    Option groups: FD,FIFO
    See also: named pipe

PTY Generates a pseudo terminal (pty) and uses its master side. Another process may open the pty's slave side using it like a serial line or terminal.
    Option groups: FD,PTY,TERMIOS
    Useful options: link, openpty, ptmx
    See also: UNIX-LISTEN, PIPE, EXEC, SYSTEM

READLINE
    Uses GNU readline and history on stdio to allow editing and reusing input lines. This requires the GNU readline and history libraries. Note that stdio should be a (pseudo) terminal device, otherwise readline does not seem to work.
    Option groups: FD,READLINE,TERMIOS
    Useful options: history
    See also: STDIO

SOCKS4:<socks-server>:<host>:<port>
    Connects via <socks-server> [IPv4 address] to <host> [IPv4 address] on <port> [TCP service], using socks version 4 protocol.
    Option groups: FD,SOCKET,IP4,TCP,SOCKS4
    Useful options: socksuser, socksport, sourceport
    See also: SOCKS4A, TCP4

SOCKS4A:<socks-server>:<host>:<port>
    Connects via <socks-server> [IPv4 address] to <host> [IPv4 address] on <port> [TCP service]. This address uses version 4a of the socks protocol, thus it sends the destination host name unresolved in the socks request.
    Option groups: FD,SOCKET,IP4,TCP,SOCKS4
    Useful options: socksuser, socksport, sourceport
    See also: SOCKS4, TCP4

STDERR
    Uses file descriptor 2.
    Option groups: FD (FIFO,TERMIOS,BLK,REG,SOCKET,UNIX)
    See also: FD

STDIN
    Uses file descriptor 0.
    Option groups: FD (FIFO,TERMIOS,BLK,REG,SOCKET,UNIX)
    See also: FD

STDIO
    Uses file descriptor 0 for reading, and 1 for writing.
    Option groups: FD (FIFO,TERMIOS,BLK,REG,SOCKET,UNIX)
    See also: FD

STDOUT
    Uses file descriptor 1.
    Option groups: FD (FIFO,TERMIOS,BLK,REG,SOCKET,UNIX)
    See also: FD

SYSTEM:<shell-command>
    Invokes the program with system(). Please note that <shell-command> [string] must not contain ',' or "!!", and that shell meta characters may have to be protected. After successful program start, socat writes data to stdin of the process and reads from its stdout.
    Option groups: FD,FIFO,SOCKET,EXEC,FORK,TERMIOS,UNIX
    Useful options: path, fdin, fdout, chroot, su, su-d, pty, stderr, ctty, setsid, pipes
    See also: EXEC

TCP4:<host>:<port>
    Connects to <port> [TCP service] on <host> [IPv4 address] using TCP/IPv4.
    Option groups: FD,SOCKET,IP4,TCP
    Useful options: crnl, bind, tos, mtudiscover, mss, nodelay, nonblock, sourceport
    See also: TCP4-LISTEN, UDP4, TCP6, UNIX-CONNECT

TCP4-LISTEN:<port>
    Listens on <port> [TCP service] and accepts a TCP/IPv4 connection. Note that opening this address usually blocks until a client connects.
    Option groups: FD,SOCKET,LISTEN,CHILD,RANGE,IP4,TCP
    Useful options: crnl, fork, bind, range, backlog, mss, su, reuseaddr
    See also: TCP4, UDP4-LISTEN, TCP6-LISTEN, UNIX-LISTEN

TCP6:<host>:<port>
    Connects to <port> [TCP service] on <host> [IPv6 address] using TCP/IPv6.
    Option groups: FD,SOCKET,IP6,TCP
    Useful options: crnl, bind, tos, nodelay, nonblock
    See also: TCP6-LISTEN, UDP6, TCP4

TCP6-LISTEN:<port>
    Listens on <port> TCP service] and accepts a TCP/IPv6 connection. Note that opening this address usually blocks until a client connects.
    Option groups: FD,SOCKET,LISTEN,CHILD,RANGE,IP6,TCP
    Useful options: crnl, fork, bind, range, backlog, reuseaddr
    See also: TCP6, UDP6-LISTEN, TCP4-LISTEN

UDP4:<host>:<port>
    Connects to <port> [UDP service] on <host> [IPv4 address] using UDP/IPv4. Please note that, due to UDP protocol properties, no real connection is established; data has to be sent for `connecting' to the server, and no end-of-file condition can be transported.
    Option groups: FD,SOCKET,IP4,UDP
    Useful options: ttl, tos, bind, sourceport
    See also: UDP4-LISTEN, TCP4, UDP6

UDP4-LISTEN:<port>
    Waits for a UDP/IPv4 packet arriving on <port> [UDP service] and `connects' back to sender. Please note that, due to UDP protocol properties, no real connection is established; data has to arrive from the peer first, and no end-of-file condition can be transported. Note that opening this address usually blocks until a client connects.
    Option groups: FD,SOCKET,LISTEN,CHILD,RANGE,IP4,UDP
    Useful options: fork, bind, range
    See also: UDP4, TCP4-LISTEN, UDP6-LISTEN

UDP6:<host>:<port>
    Connects to <port> [UDP service] on <host> [IPv6 address] using UDP/IPv6. Please note that, due to UDP protocol properties, no real connection is established; data has to be sent for `connecting' to the server, and no end-of-file condition can be transported.
    Option groups: FD,SOCKET,IP6,UDP
    Useful options: ttl, tos
    bind, sourceport, See also: UDP6-LISTEN, TCP6, UDP4

UDP6-LISTEN:<port>
    Waits for a UDP/IPv6 packet arriving on <port> [UDP service] and `connects' back to sender. Please note that, due to UDP protocol properties, no real connection is established; data has to arrive from the peer first, and no end-of-file condition can be transported. Note that opening this address usually blocks until a client connects.
    Option groups: FD,SOCKET,LISTEN,CHILD,RANGE,IP6,UDP
    Useful options: fork, bind, range
    See also: UDP6, TCP6-LISTEN, UDP4-LISTEN

UNIX-CONNECT:<filename>
    Connects to <filename> assuming it is a UNIX domain socket. If <filename> does not exist, this is an error; if <filename> is not a UNIX domain socket, this is an error; if <filename> is a UNIX domain socket, but no process is listening, this is an error.
    Option groups: FD,SOCKET,NAMED,UNIX
    See also: UNIX-LISTEN, TCP4

UNIX-LISTEN:<filename>
    Listens on <filename> using a UNIX domain stream socket and accepts a connection. If <filename> exists and is not a socket, this is an error. If <filename> exists and is a UNIX domain socket, binding to the address fails (use option unlink-early!). Note that opening this address usually blocks until a client connects.
    Option groups: FD,SOCKET,NAMED,LISTEN,CHILD,UNIX
    Useful options: fork, umask, mode, user, group, unlink-early
    See also: UNIX-CONNECT, TCP4-LISTEN

ADDRESS OPTIONS

Address options can be applied to address specifications to influence the process of opening the addresses and the properties of the resulting data channels.

For technical reasons not every option can be applied to every address type; e.g., applying a socket option to a regular file will fail. To catch most useless combinations as early as in the open phase, the concept of option groups was introduced. Each option belongs to one or more option groups. Options can be used only with address types that support at least one of their option groups (but see option -g).

Address options have data types that their values must conform to. Every address option consists of just a keyword or a keyword followed by "=value", where value must conform to the options type. Some address options manipulate parameters of system calls; e.g., option sync sets the O_SYNC flag with the open() call. Other options cause a system or library call; e.g., with option `ttl=value' the setsockopt(fd, SOL_IP, IP_TTL, value, sizeof(int)) call is applied. Other options set internal socat variables that are used during data transfer; e.g., `crnl' causes explicit character conversions. A few options have more complex implementations; e.g., su-d (substuser-delayed) inquires some user and group infos, stores them, and applies them later after a possible chroot() call.

If multiple options are given to an address, their sequence in the address specification has (almost) no effect on the sequence of their execution/application. Instead, socat has built in an option phase model that tries to bring the options in a useful order. Some options exist in different forms (e.g., unlink, unlink-early, unlink-late) to control the time of their execution.

If the same option is specified more than once within one address specification, with equal or different values, the effect depends on the kind of option. Options resulting in function calls like setsockopt() cause multiple invokations. With options that set parameters for a required call like open() or set internal flags, the value of the last option occurrence is effective.

The existence or semantics of many options are system dependent. Socat usually does NOT try to emulate missing libc or kernel features, it just provides an interface to the underlying system. So, if an operating system lacks a feature, the related option is simply not available on this platform.

The following paragraphs introduce just the more common address options. For a comprehensive reference and to find information about canonical option names, alias names, option phases, and platforms see file xio.help.

FD option group

This option group contains options that are applied to a UN*X style file descriptor, no matter how it was generated. Because all current socat address types are file descriptor based, these options may be applied to any address.
Note: Some of these options are also member of another option group, that provides an other, non-fd based mechanism. For these options, it depends on the actual address type and its option groups which mechanism is used. The second, non-fd based mechanism is prioritized.

cloexec=<bool>
    Sets the FD_CLOEXEC flag with the fcntl() system call to value <bool>. If set, the file descriptor is closed on exec() family function calls. Socat internally handles this flag for the fds it controls, so in most cases there will be no need to apply this option.

setlk
    Tries to set a discretionary lock to the whole file using the fcntl(fd, F_SETLK, ...) system call. If the file is already locked, this call results in an error.

setlkw
    Tries to set a discretionary waiting lock to the whole file using the fcntl(fd, F_SETLKW, ...) system call. If the file is already locked, this call blocks.

flock-ex
    Tries to set a blocking exclusive advisory lock to the file using the flock(fd, LOCK_EX) system call. Socat hangs in this call if the file is locked by another process.

flock-ex-nb
    Tries to set a nonblocking exclusive advisory lock to the file using the flock(fd, LOCK_EX) system call. If the file is already locked, this option results in an error.

flock-sh
    Tries to set a blocking shared advisory lock to the file using the flock(fd, LOCK_SH) system call. Socat hangs in this call if the file is locked by another process.

flock-sh-nb
    Tries to set a nonblocking shared advisory lock to the file using the flock(fd, LOCK_SH|LOCK_NB) system call. If the file is already locked, this option results in an error.

lock
    Sets a blocking lock on the file. Uses the setlk or flock mechanism depending on availability on the particular platform. If both are available, the POSIX variant (setlkw) is selected.

user=<user>
    Sets the <user> (owner) of the stream. If the address is member of the NAMED option group, socat uses the chown() system call after opening the file or binding to the UNIX domain socket (race condition!). Otherwise (without filesystem entry), socat sets the user of the stream using the fchown() system call. These calls might require root privilege.

user-late=<user>
    Sets the owner of the fd to <user> with the fchown() system call after opening or connecting the channel. This is useful only on file system entries.

group=<group>
    Sets the <group> of the stream. If the address is member of the NAMED option group, socat uses the chown() system call after opening the file or binding to the UNIX domain socket (race condition!). Otherwise (without filesystem entry), socat sets the group of the stream with the fchown() system call. These calls might require group membership or root privilege.

group-late=<group>
    Sets the group of the fd to <group> with the fchown() system call after opening or connecting the channel. This is useful only on file system entries.

mode=<mode>
    Sets the <mode> [mode_t] (permissions) of the stream. If the address is member of the NAMED option group and uses the open() or creat() call, the mode is applied with these. If the address is member of the NAMED option group without using these system calls, socat uses the chmod() system call after opening the filesystem entry or binding to the UNIX domain socket (race condition!). Otherwise, socat sets the mode of the stream using fchmod(). These calls might require ownership or root privilege.

perm-late=<mode>
    Sets the permissions of the fd to value <mode> [mode_t] using the fchmod() system call after opening or connecting the channel. This is useful only on file system entries.

append=<bool>
    Always writes data to the actual end of file. If the address is member of the OPEN option group, socat uses the O_APPEND flag with the open() system call. Otherwise, socat applies the fcntl(fd, F_SETFL, O_APPEND) call.

nonblock=<bool>
    Tries to open or use file in nonblocking mode. Its only effects are that the connect() call of TCP addresses does not block, and that opening a named pipe for reading does not block. If the address is member of the OPEN option group, socat uses the O_NONBLOCK flag with the open() system call. Otherwise, socat applies the fcntl(fd, F_SETFL, O_NONBLOCK) call.

NAMED option group

These options work on file system entries.
See also options user, group, and mode.

user-early=<user>
    Changes the <user> (owner) of the file system entry before accessing it, using the chown() system call. This call might require root privilege.

group-early=<group>
    Changes the <group> of the file system entry before accessing it, using the chown() system call. This call might require group membership or root privilege.

perm-early=<mode>
    Changes the <mode> [mode_t] of the file system entry before accessing it, using the chmod() system call. This call might require ownership or root privilege.

umask=<mode>
    Sets the umask of the process to <mode> [mode_t] before accessing the file system entry (useful with UNIX domain sockets!). This call might affect all further operations of the socat process!

unlink-early
    Unlinks (removes) the file before opening it and even before applying user-early etc.

unlink
    Unlinks (removes) the file before accessing it, but after user-early etc.

unlink-late
    Unlinks (removes) the file after opening it to make it inaccessible for other processes after a short race condition.

OPEN option group

The OPEN group options allow to set flags with the open() system call. E.g., option `creat' sets the O_CREAT flag.
See also options append and nonblock.

creat=<bool>
    Creates the file if it does not exist.

dsync=<bool>
    Blocks write() calls until metainfo is physically written to media.

excl=<bool>
    With option creat, if file exists this is an error.

largefile=<bool>
    On 32 bit systems, allows a file larger than 2^31 bytes.

noctty=<bool>
    Does not make this file the controlling terminal.

nofollow=<bool>
    Does not follow symbolic links.

nshare=<bool>
    Does not allow to share this file with other processes.

rshare=<bool>
    Does not allow other processes to open this file for writing.

rsync=<bool>
    Blocks write() until metainfo is physically written to media.

sync=<bool>
    Blocks write() until data is physically written to media.

rdonly=<bool>
    Opens the file for reading only.

wronly=<bool>
    Opens the file for writing only.

trunc
    Truncates the file to size 0 during opening it.

REG and BLK option group

These options are usually applied to a UN*X file descriptor, but their semantics make sense only on a file supporting random access.

seek=<offset>
    Applies the lseek(fd, <offset>, SEEK_SET) system call, thus positioning the file pointer absolutely to <offset> [long].

seek-cur=<offset>
    Applies the lseek(fd, <offset>, SEEK_CUR) system call, thus positioning the file pointer <offset> [long] bytes relatively to its current position (which is usually 0).

seek-end=<offset>
    Applies the lseek(fd, <offset>, SEEK_END) system call, thus positioning the file pointer <offset> [long] bytes relatively to the files current end.

ftruncate=<offset>
    Applies the ftruncate(fd, <offset>) system call, thus truncating the file at the position <offset> [long].

PROCESS option group

Options of this group change the process properties instead of just affecting one data channel. For EXEC and SYSTEM addresses and for LISTEN type addresses with option FORK, these options apply to the child processes instead of the main socat process.

chroot=<directory>
    Performs a chroot() operation to <directory> after processing the address. This call might require root privilege.

chroot-early=<directory>
    Performs a chroot() operation to <directory> before opening the address. This call might require root privilege.

setgid=<group>
    Changes the primary <group> of the process after processing the address. This call might require root privilege.

setgid-early=<group>
    Changes the primary <group> of the process before opening the address. This call might require root privilege.

setuid=<user>
    Changes the <user> (owner) of the process after processing the address. This call might require root privilege.

setuid-early=<user>
    Changes the <user> (owner) of the process before opening the address. This call might require root privilege.

su=<user>
    Changes the <user> (owner) and groups of the process after processing the address. This call might require root privilege.

su-d=<user>
    Short name for substuser-delayed. Changes the <user> (owner) and groups of the process after processing the address. The user and his groups are retrieved before a possible chroot(). This call might require root privilege.

setpgid=<pid_t>
    Makes the process a member of the specified process group <pid_t>. If no value is given, or if the value is 0 or 1, the process becomes leader of a new process group.

setsid
    Makes the process the leader of a new session.

READLINE option group

These options apply to the readline address type.

history=<filename>
    Reads and writes history from/to <filename>.

APPLICATION option group

This group contains options that work at data level.

cr  Converts the default line termination character NL ('\n', 0x0a) to/from CR ('\r', 0x0d) when writing or reading on this channel.

crnl
    Converts the default line termination character NL ('\n', 0x0a) to/from CRNL ("\r\n", 0x0d0a) when writing or reading on this channel.

ignoreeof
    When EOF occurs on this channel, socat ignores it and tries to read more data (like "tail -f").

SOCKET option group

These options are intended for all kinds of sockets, e.g. IP or UNIX domain. Most are applied with a setsockopt() call.

bind=<sockname>
    Binds the socket to the given socket address using the bind() system call. The form of <sockname> is socket domain dependent: IP4 and IP6 allow the form [hostname|hostaddress][:[service|port]], UNIX domain sockets require <filename>.

interface=<interface>
    Binds the socket to the given <interface>. With Linux, this is a string like "eth0". This option might require root privilege.

broadcast
    For datagram sockets, allows sending to broadcast addresses and receiving packets addressed to broadcast addresses.

bsdcompat
    Emulates some (old?) bugs of the BSD socket implementation.

debug
    Enables socket debugging.

dontroute
    Only communicates with directly connected peers, does not use routers.

keepalive
    Enables sending keepalives on the socket.

linger=<seconds>
    Blocks shutdown() or close() until data transfers have finished or the given timeout [int] expired.

oobinline
    Places out-of-band data in the input data stream.

priority=<priority>
    Sets the protocol defined <priority> [<int>] for outgoing packets.

rcvbuf=<bytes>
    Sets the size of the receive buffer after the socket() call to <bytes> [int]. With TCP sockets, this value correspondends to the socket's maximal window size.

rcvbuf-late=<bytes>
    Sets the size of the receive buffer when the socket is already connected to <bytes> [int]. With TCP sockets, this value correspondends with the socket's maximal window size.

rcvlowat=<bytes>
    Specifies the minimum number of received bytes [int] until the socket layer will pass the buffered data to socat.

rcvtimeo=<seconds>
    Sets the receive timeout [timeval].

reuseaddr
    Allows other sockets to bind to an address even if parts of it (e.g. the local port) are already in use by socat.

sndbuf=<bytes>
    Sets the size of the send buffer after the socket() call to <bytes> [int].

sndbuf-late=<bytes>
    Sets the size of the send buffer when the socket is connected to <bytes> [int].

sndlowat=<bytes>
    Specifies the minimum number of bytes in the send buffer until the socket layer will send the data to <bytes> [int].

sndtimeo=<seconds>
    Sets the send timeout to seconds [timeval].

type=<type>
    Sets the type of the socket, usually as argument to the socket() or socketpair() call, to <type> [int]. Under Linux, 1 means stream oriented socket, 2 means datagram socket, and 3 means raw socket.

IP option group

These options can be used with IPv4 and IPv6 based sockets.

tos=<tos>
    Sets the TOS (type of service) field of outgoing packets to <tos> [byte] (see RFC 791).

ttl=<ttl>
    Sets the TTL (time to live) field of outgoing packets to <ttl> [byte].

ipoptions=<data>
    Sets IP options like source routing. Must be given in binary form, recommended format is a leading "x" followed by an even number of hex digits. This option may be used multiple times, data are appended. E.g., to connect to host 10.0.0.1 via some gateway using a loose source route, use the gateway as address parameter and set a loose source route using the option ipoptions=x8307040a000001.
    IP options are defined in RFC 791.

mtudiscover=<0|1|2>
    Takes 0, 1, 2 to never, want, or always use path MTU discover on this socket.

TCP option group

These options may be applied to TCP sockets. They work by invoking setsockopt() with the appropriate parameters.

cork
    Doesn't send packets smaller than MSS (maximal segment size).

defer-accept
    While listening, accepts connections only when data from the peer arrived.

keepcnt=<count>
    Sets the number of keepalives before shutting down the socket to <count> [int].

keepidle=<seconds>
    Sets the idle time before sending the first keepalive to <seconds> [int].

keepintvl=<seconds>
    Sets the intervall between two keepalives to <seconds> [int].

linger2=<seconds>
    Sets the time to keep the socket in FIN-WAIT-2 state to <seconds> [int].

mss=<bytes>
    Sets the MSS (maximum segment size) after the socket() call to <bytes> [int]. This value is then proposed to the peer with the SYN or SYN/ACK packet.

mss-late=<bytes>
    Sets the MSS of the socket after connection has been established to <bytes> [int].

nodelay
    Turns off the Nagle algorithm for measuring the RTT (round trip time).

rfc1323
    Enables RFC1323 TCP options: TCP window scale, round-trip time measurement (RTTM), and protect against wrapped sequence numbers (PAWS).

stdurg
    Enables RFC1122 compliant urgent pointer handling.

syncnt=<count>
    Sets the maximal number of SYN retransmits during connect to <count> [int].

UDP and TCP option groups

Here we find options that are related to the network port mechanism and that thus can be used with UDP and TCP addresses.

sourceport=<port>
    For outgoing TCP and UDP connections, it sets the source <port> using an extra bind() call. With TCP or UDP listen addresses, immediately shuts down the connection if the client does not use this sourceport.

SOCKS option group

When using SOCKS type addresses, some socks specific options can be set.

socksport=<port>
    Overrides the default "socks" service or port 1080 for the socks server port with <port>.

socksuser=<user>
    Sends the <user> [string] in the username field to the socks server. Default is the actual user name ($LOGNAME or $USER).

RANGE option group

The range option can be applied to listening network sockets.

range=<address-range>
    After accepting a connection, tests if the peer is within range. This option is currently only implemented for IPv4 addresses. Address range has the form ww.xx.yy.zz/bits, e.g. 10.0.0.0/8. If the client address does not match, socat issues an error aborting the program or keeps listening (see option -s).

LISTEN option group

Options specific to listening sockets.

backlog=<count>
    Sets the backlog value passed with the listen() system call to <count> [int]. Default is 5.

CHILD option group

Options for listen addresses with child processes.

fork
    After accepting a connection, handles its channel in a child process and keeps the parent process listening.

EXEC option group

Options for addresses that invoke a program.

path=<string>
    Overrides the PATH environment variable for searching the program with <string>. This $PATH value is effective in the child process too.

login
    Prefixes argv[0] for the execvp() call with '-', thus making a shell behave as login shell.

FORK option group

EXEC or SYSTEM addresses invoke a program using a child process and transfer data between socat and the program. The interprocess communication mechanism can be influenced with the following options. Per default, a socketpair() is created and assigned to stdin and stdout of the child process, while stderr is inherited from the socat process, and the child process uses file descriptors 0 and 1 for communicating with the main socat process.

pipes
    Creates a pair of unnamed pipes for interprocess communication instead of a socket pair.

openpty
    Establishes communication with the sub process using a pseudo terminal created with openpty() instead of a socket pair.

ptmx
    Establishes communication with the sub process using a pseudo terminal created by opening /dev/ptmx or /dev/pts instead of a socket pair.

pty Establishes communication with the sub process using a pseudo terminal instead of a socket pair. Creates the pty with an available mechanism. If openpty and ptmx are both available, it uses ptmx because this is POSIX compliant.

ctty
    Makes the pty the controlling tty of the sub process.

stderr
    Directs stderr of the sub process to its output channel by making stderr a dup() of stdout.

fdin=<fdnum>
    Assigns the sub processes input channel to its file descriptor <fdnum> instead of stdin (0). The program started from the subprocess has to use this fd for reading data from socat.

fdout=<fdnum>
    Assigns the sub processes output channel to its file descriptor <fdnum> instead of stdout (1). The program started from the subprocess has to use this fd for writing data to socat.

TERMIOS option group

For addresses that work on a tty (e.g., stdio, file:/dev/tty, exec:...,pty), the terminal parameters defined in the UN*X termios mechanism are made available as address option parameters. Please note that changes of the parameters of your interactive terminal remain effective after socat's termination, so you might have to enter "reset" or "stty sane" in your shell afterwards. For EXEC and SYSTEM addresses with option PTY, these options apply to the pty by the child processes.

Only a subset of the termios options is listed here; see the file xio.help of the socat documentation, or "man termios" for a comprehensive description.

echo=<bool>
    Enables or disables local echo.

icanon=<bool>
    Sets or clears canonical mode, enabling line buffering and some special characters.

raw Sets raw mode, thus passing input and output almost unprocessed.

ignbrk=<bool>
    Ignores or interpretes the BREAK character (e.g., ^C)

    .LP .nf cr0 cr1 cr2 cr3 .fi .IP Sets the carriage return delay to 0, 1, 2, or 3, respectively. 0 means no delay, the other values are terminal dependent.

    .LP .nf cs5 cs6 cs7 cs8 .fi .IP Sets the character size to 5, 6, 7, or 8 bits, respectively.

cstopb=<bool>
    Sets two stop bits, rather than one.

echoctl=<bool>
    Echos control characters in hat notation (e.g. ^A)

nl0 Sets the newline delay to 0.

opost=<bool>
    Enables or disables output processing; e.g., converts NL to CR-NL.

parenb=<bool>
    Enable parity generation on output and parity checking for input.

sane
    Brings the terminal to something like a useful default state.

PTY option group

These options are intended for use with the pty address type.

link=<filename>
    Generates a symbolic link that points to the actual pseudo terminal (pty). This might help to solve the problem that ptys are generated with more or less unpredictable names, making it difficult to directly access the socat generated pty automatically. With this option, the user can specify a "fix" point in the file hierarchy that helps him to access the actual pty.

OPENSSL option group

These options apply to the openssl address type.

cipherlist=<cipherlist>
    Selects the list of ciphers that may be used for the connection. See the man page of ciphers, section CIPHER LIST FORMAT, for detailed information about syntax, values, and default of <cipherlist>.
    Several cipher strings may be given, separated by ':'. Some simple cipher strings:

3DES
    Uses a cipher suite with triple DES.

MD5 Uses a cipher suite with MD5.

aNULL
    Uses a cipher suite without authentication.

NULL
    Does not use encryption.

HIGH
    Uses a cipher suite with "high" encryption. Note that the peer must support the selected property, or the negotiation will fail.

method=<ssl-method>
    Sets the protocol version to be used. Valid strings (not case sensitive) are:

SSLv2
    Select SSL protocol version 2.

SSLv3
    Select SSL protocol version 3.

SSLv23
    Select SSL protocol version 2 or 3. This is the default when this option is not provided.

TLSv1
    Select TLS protocol version 1.

verify=<bool>
    Forces check of the peer's certificate. Currently there is not certificate check implemented, therefore applying this option lets the connection fails.

DATA VALUES

This section explains the different data types that address parameters and address options can take.

address-range
    Is currently only implemented for IPv4. See address-option `range'

bool
    "0" or "1"; if value is omitted, "1" is taken.

byte
    An unsigned int number, read with strtoul(), lower or equal to UCHAR_MAX.

command-line
    A string specifying a program name and its arguments, separated by single spaces.

data
    A raw data specification following dalan syntax. The only documented form is a string starting with 'x' followed by an even number of hex digits.

directory
    A string with usual UN*X directory name semantics.

facility
    The name of a syslog facility in lower case characters.

fdnum
    An unsigned int type, read with strtoul(), specifying a UN*X file descriptor.

filename
    A string with usual UN*X filename semantics.

group
    If the first character is a decimal digit, the value is read with strtoul() as unsigned integer specifying a group id. Otherwise, it must be an existing group name.

int A number following the rules of the strtol() function with base "0", i.e. decimal number, octal number with leading "0", or hexadecimal number with leading "0x". The value must fit into a C int.

interface
    A string specifying the device name of a network interface.

IPv4 address
    A hostname that is resolved by gethostbyname(), or an address in numbers-and-dots notation.
    Examples: www.dest-unreach.org, dns1, 127.0.0.1

IPv6 address
    A hostname that is resolved by gethostbyname(), or an address in hexnumbers-and-colons notation.
    Examples: ip6name.domain.org, ::1, 1234:5678:9abc:def0:1234:5678:9abc:def0

long
    A number read with strtol(). The value must fit into a C long.

mode_t
    An unsigned integer, read with strtoul(), specifying mode (permission) bits.

pid_t
    A number, read with strtol(), specifying a process id.

port
    A byte2_t (16 bit) unsigned number specifying a TCP or UDP port, read with strtoul().

protocol
    An unsigned 8 bit number, read with strtoul().

size_t
    An unsigned int with size_t limitations, read with strtoul.

sockname
    A socket address. See address-option `bind'

string
    A sequence of characters, not containing '\0' and, depending on the position within the command line, ':', ',', or "!!". Note that you might have to escape shell meta characters in the command line.

TCP service
    A service name, not starting with a digit, that is resolved by getservbyname(), or an unsigned int 16 bit number read with strtoul().

timeval
    A double float specifying seconds; the number is mapped into a struct timeval, consisting of seconds and microseconds.

UDP service
    A service name, not starting with a digit, that is resolved by getservbyname(), or an unsigned int 16 bit number read with strtoul().

unsigned int
    A number read with strtoul(). The value must fit into a C unsigned int.

user
    If the first character is a decimal digit, the value is read with strtoul() as unsigned integer specifying a user id. Otherwise, it must be an existing user name.

EXAMPLES

socat - TCP4:www.domain.org:80

    Transfers data between STDIO (-) and a TCP4 connection to port 80 of host www.domain.org. This example results in an interactive connection similar to telnet or netcat. The stdin terminal parameters are not changed, so you may close the relay with ^D or abort it with ^C.

    .LP .nf socat -d -d READLINE,history=$HOME/.http_history \ TCP4:www.domain.org:www,crnl .fi

    This is similar to the previous example, but you can edit the current line in a bash like manner (READLINE) and use the history file .http_history; socat prints messages about progress (-d -d). The port is specified by service name (www), and correct network line termination characters (crnl) instead of NL are used.

socat TCP4-LISTEN:www TCP4:www.domain.org:www

    Installs a simple TCP port forwarder. With TCP4-LISTEN it listens on local port "www" until a connection comes in, accepts it, then connects to the remote host (TCP4) and starts data transfer. It will not accept a second connection.

    .LP .nf socat -d -d -lmlocal2 \ TCP4-LISTEN:80,bind=myaddr1,su=nobody,fork,range=10.0.0.0/8,reuseaddr \ TCP4:www.domain.org:80,bind=myaddr2 .fi

    TCP port forwarder, each side bound to another local IP address (bind). This example handles an almost arbitrary number of parallel or consecutive connections by fork'ing a new process after each accept(). It provides a little security by su'ing to user nobody after forking; it only permits connections from the private 10 network (range); due to reuseaddr, it allows immediate restart after master process's termination, even if some child sockets are not completely shut down. With -lmlocal2, socat logs to stderr until successfully reaching the accept loop. Further logging is directed to syslog with facility local2.

    .LP .nf socat TCP4-LISTEN:5555,fork \ EXEC:/bin/myscript,chroot=/home/sandbox,su-d=sandbox,pty,stderr .fi

    A simple server that accepts connections (TCP4-LISTEN) and fork's a new child process for each connection; every child acts as single relay. For EXEC'uting the program, it chroot's to /home/sandbox, su's to user sandbox, and then starts the program /home/sandbox/bin/myscript. Socat and myscript communicate via a pseudo tty (pty); myscript's stderr is redirected to stdout, so its error messages are transferred via socat to the connected client.

    .LP .nf socat EXEC:"mail.sh target@domain.com",fdin=3,fdout=4 \ TCP4:mail.relay.org:25,crnl,bind=alias1.server.org,mss=512 .fi

    mail.sh is a shell script, distributed with socat, that implements a simple SMTP client. It is programmed to "speak" SMTP on its FDs 3 (in) and 4 (out). The fdin and fdout options tell socat to use these FDs for communication with the program. Because mail.sh inherits stdin and stdout while socat does not use them, the script can read a mail body from stdin. Socat makes alias1 your local source address (bind), cares for correct network line termination (crnl) and sends at most 512 data bytes per packet (mss).

socat - /dev/ttyS0,raw,echo=0,crnl

    Opens an interactive connection via the serial line, e.g. for talking with a modem. raw and echo set ttyS0's terminal parameters to practicable values, crnl converts to correct newline characters. Consider using READLINE instead of `-'.

    .LP .nf socat UNIX-LISTEN:/tmp/.X11-unix/X1,fork \ SOCKS4:host.victim.org:127.0.0.1:6000,socksuser=nobody,sourceport=20 .fi

    With UNIX-LISTEN, socat opens a listening UNIX domain socket /tmp/.X11-unix/X1. This path correspondents with local XWindow display :1 on your machine, so XWindow client connections to DISPLAY=:1 are accepted. Socat then speaks with the SOCKS4 server host.victim.org that might permit sourceport 20 based connections due to an FTP related weakness in its static IP filters. Socat pretends to be invoked by socksuser nobody, and requests to be connected to loopback port 6000 (only weak sockd configurations will allow this). So we get a connection to the victims XWindow server and, if it does not require MIT cookies or Kerberos authentication, we can start work. Please note that there can only be one connection at a time, because TCP can establish only one session with a given set of addresses and ports.

socat -u /tmp/readdata,seek-end=0,ignoreeof -

    This is an example for unidirectional data transfer (-u). Socat transfers data from file /tmp/readdata (implicit address GOPEN), starting at its end (seek-end=0 lets socat start reading at current end of file; use seek=0 or no seek* options at all to work through existing file data) in a "tail -f" like mode (ignoreeof). The "file" might also be a listening UNIX domain socket (do not use seek-* then).

    .LP .nf (echo PASSWORD; sleep 5; echo ls; sleep 1) | socat - EXEC:'ssh -l user server',pty,setsid,ctty .fi

    EXEC'utes an ssh session to server. Uses a pty for communication between socat and ssh, makes it ssh's controlling tty (ctty), and makes this pty the owner of a new process group (setsid), so ssh accepts the password from socat.

    .LP .nf socat -u TCP4-LISTEN:3334,reuseaddr,fork \ OPEN:/tmp/in.log,creat,append .fi

    Implements a simple network based message collector. For each client connecting to port 3334, a new child process is generated (option fork). All data sent by the clients are append'ed to the file /tmp/in.log. If the file does not exist, socat creat's it. Option reuseaddr allows immediate restart of the server process.

socat READLINE EXEC:'ftp ftp.server.com',pty

    Wraps a command line history (READLINE) around the EXEC'uted ftp client utility. This allows editing and reuse of FTP commands for relatively comfortable browsing through the ftp directory hierarchy. The password is read directly from the terminal and therefore not echoed. pty is required to have ftp issue a prompt. Nevertheless, there may occur some confusion with the password and FTP prompts.

    On server with modem:

socat TCP4-LISTEN:54321 /dev/ttyS0

    On client: mkdir $HOME/dev

socat PTY,link=$HOME/dev/vmodem0 TCP4:modemserver.us.org:54321

    Installs a TCP4 service on a modemserver and generates a pseudo terminal device (PTY) on the client that can be reached under the symbolic link $HOME/dev/vmodem0. Now an application on the client that expects a serial line or modem can be configured to use $HOME/dev/vmodem0; its traffic will be directed to /dev/ttyS0 on the modem server.

DIAGNOSTICS

Socat uses a logging mechanism that allows to filter messages by severity. The severities provided are more or less compatible to the appropriate syslog priority. With one or up to four occurrences of the -d command line option, the lowest priority of messages that are issued can be selected.

FATAL:
    Conditions that require unconditional and immediate program termination.

ERROR:
    Conditions that prevent proper program processing. Usually the program is terminated (see option -s).

WARNING:
    Something did not function correctly or is in a state where correct further processing cannot be guaranteed, but might be possible.

NOTICE:
    Interesting actions of the program, e.g. for supervising socat in some kind of server mode.

INFO:
    Description of what the program does, and maybe why it happens.

DEBUG:
    Description of how the program works, all system or library calls and their results.

Log messages can be written to stderr, to a file, or to syslog.

On exit, socat gives status 0 if it terminated due to EOF or inactivity timeout, with a positive value on error, and with a negative value on fatal error.

FILES

/usr/local/bin/socat
/usr/local/bin/filan
/usr/local/bin/procan

CREDITS

The work of the following groups and organizations was invaluable for this project:

The FSF (GNU, http://www.fsf.org/ project with their free and portable development software and lots of other useful tools and libraries.

The Linux developers community (http://www.linux.org/) for providing a free, open source operating system.

Sourceforge (http://www.sourceforge.net/) for providing a compile farm with Solaris, FreeBSD, and MacOS X machines, making these ports possible.

The Open Group (http://www.unix-systems.org/) for making their standard specifications available on the Internet for free.

VERSION

This man page describes version 1.2.0 of socat.

BUGS

Addresses cannot be nested, so one socat instance cannot, e.g., drive ssl over socks.

In address specifications, characters with special meaning (':', ',', '!!') cannot be escaped, so it is hardly possible to directly invoke a second socat instance from an exec type addresses.

IPv6 address specification does not allow '[' and ']'.

Address option ftruncate without value uses default 1 instead of 0.

Send bug reports to <socat@dest-unreach.org>

SEE ALSO

nc(1), netcat6(1), sock(1), rinetd(8), cage(1), socks.conf(5), openssl(1), stunnel(1)

Socat home page http://www.dest-unreach.org/socat/

AUTHOR

Gerhard Rieger <rieger@dest-unreach.org>