General
SockOpts is a program I wrote for setting some defaults for all sockets you open.
It all started as a simple program that would help you bind to a specific address for all connections by default. It is useful especially for outgoing connections where you can choose one of the many ip addresses an interface can have, or route all packets through a certain interface.
Technical information
I will add more technical information later.
The program is written in C. At the tarball you can also find a Makefile.
Usage
$ export LD_PRELOAD=/path/to/sockopts.so
$ export SOCKOPTS_BIND_ADDR=10.0.0.1
$ SOCKOPTS_OPT_KEEPALIVE=1 nc www.google.com 80
You must first use the environment LD_PRELOAD to instruct the runtime linker to preload your library. Then you can set any of the following environment variables to bind to an address/interface or to set any socket options you want.
The descriptions for the socket options are taken from the socket(7) man page.
- SOCKOPTS_BIND_ADDR
Bind to a specific IP address.
- SOCKOPTS_BIND_IF
Bind to an interface. Note that this is not what you need!!! When binding to an interface all the default routing rules apply. However, packets with source or destination interface other the one that you binded are discarded.
- SOCKOPTS_OPT_DEBUG
Enable socket debugging. Only allowed for processes with the CAP_NET_ADMIN capability or an effective user ID of 0.
- SOCKOPTS_OPT_KEEPALIVE
Enable sending of keep-alive messages on connection-oriented sockets. Expects an integer boolean flag.
- SOCKOPTS_OPT_OOBINLINE
If this option is enabled, out-of-band data is directly placed into the receive data stream. Otherwise out-of-band data is only passed when the MSG_OOB flag is set during receiving.
- SOCKOPTS_OPT_RCVBUF
Sets or gets the maximum socket receive buffer in bytes. The kernel doubles this value (to allow space for bookkeeping overhead) when it is set using setsockopt(2), and this doubled value is returned by getsockopt(2). The default value is set by the /proc/sys/net/core/rmem_default file, and the maximum allowed value is set by the /proc/sys/net/core/rmem_max file. The minimum (doubled) value for this option is 256.
- SOCKOPTS_OPT_SNDBUF
Sets or gets the maximum socket send buffer in bytes. The kernel doubles this value (to allow space for bookkeeping overhead) when it is set using setsockopt(2), and this doubled value is returned by getsockopt(2). The default value is set by the /proc/sys/net/core/wmem_default file and the maximum allowed value is set by the /proc/sys/net/core/wmem_max file. The minimum (doubled) value for this option is 2048.
Download
The program is available for download here.
License
The program is licensed under the terms of the MIT license.