sockopts

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 8, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

README

Release Reference DeepWiki Test

Insights Insights

go-sockopts

Use network socket options easily on Go.

Features

  • Simple
  • Easy to use
  • Apply multiple socket options
  • Control function for net.Listener
  • Custom option
  • Multi platform support

Usages

Basic usage of single option

With this library, socket options can be easily set.

With the library:

// Single socket option with this library.
lc := &net.ListenConfig{
    Control: sockopts.SO_REUSEPORT.SetFunc(true).Control,
}

Without library:

// Single socket option without this library.
// Codes depends on platforms because the example uses `unix` package.
lc := &net.ListenConfig{
    Control: func(network, address string, c syscall.RawConn) error {
        var inner, outer error
        outer = c.Control(func(fd uintptr) {
            inner = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
        })
        return cmp.Or(inner, outer)
    },
}
Basic usage of multiple options

With the library:

// Multiple socket options with this library.
lc := &net.ListenConfig{
    Control: sockopts.Control(
        sockopts.SO_REUSEADDR.SetFunc(true),
        sockopts.SO_REUSEPORT.SetFunc(true),
    ),
}

Without library:

// Multiple socket options without this library.
// Codes depends on platforms because the example uses `unix` package.
lc := &net.ListenConfig{
    Control: func(network, address string, c syscall.RawConn) error {
        var inner1, inner2, outer error
        outer = c.Control(func(fd uintptr) {
            inner1 = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)
            inner2 = unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
        })
        return cmp.Or(inner1, inner2, outer)
    },
}
Run a server with SO_REUSEPORT

SO_REUSEPORT is one of the popular socket option to sahre the same port from multiple applications.

lc := &net.ListenConfig{
    Control: sockopts.SO_REUSEPORT.SetFunc(true).Control,
}

ln, err := lc.Listen(context.Background(), "tcp", ":8080")
if err != nil {
    panic(err)
}

svr := &http.Server{
    Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.Write([]byte("Hello Gopher !!"))
    }),
}

if err := svr.Serve(ln); err != nil && err != http.ErrServerClosed {
    panic(err)
}
Custom option

Custom options can be easily defined. Choose appropriate data type and level/opt.

The Name field is used in error messages.

var SO_KEEPALIVE = sockopts.Bool{
    Level: unix.SOL_SOCKET,
    Opt:   unix.SO_KEEPALIVE,
    Name:  "SO_KEEPALIVE",
}

Docs & Examples

References

Documentation

Overview

***** DO NOT EDIT. ***** Code generated by the github.com/aileron-projects/go-sockopts/cmd/generator/generate.go

Index

Constants

View Source
const (
	SOL_SOCKET   = unix.SOL_SOCKET
	IPPROTO_IP   = unix.IPPROTO_IP
	IPPROTO_IPV6 = unix.IPPROTO_IPV6
	IPPROTO_TCP  = unix.IPPROTO_TCP
	IPPROTO_UDP  = unix.IPPROTO_UDP
)

Variables

View Source
var (
	SO_BINDTOIFINDEX        = Int{Level: unix.SOL_SOCKET, Opt: unix.SO_BINDTOIFINDEX, Name: "SO_BINDTOIFINDEX"}
	SO_BINDTODEVICE         = String{Level: unix.SOL_SOCKET, Opt: unix.SO_BINDTODEVICE, Name: "SO_BINDTODEVICE"}
	SO_DEBUG                = Bool{Level: unix.SOL_SOCKET, Opt: unix.SO_DEBUG, Name: "SO_DEBUG"}
	SO_KEEPALIVE            = Bool{Level: unix.SOL_SOCKET, Opt: unix.SO_KEEPALIVE, Name: "SO_KEEPALIVE"}
	SO_LINGER               = Linger{Level: unix.SOL_SOCKET, Opt: unix.SO_LINGER, Name: "SO_LINGER"}
	SO_MARK                 = Int{Level: unix.SOL_SOCKET, Opt: unix.SO_MARK, Name: "SO_MARK"}
	SO_RCVBUF               = Int{Level: unix.SOL_SOCKET, Opt: unix.SO_RCVBUF, Name: "SO_RCVBUF"}
	SO_RCVBUFFORCE          = Int{Level: unix.SOL_SOCKET, Opt: unix.SO_RCVBUFFORCE, Name: "SO_RCVBUFFORCE"}
	SO_SNDBUF               = Int{Level: unix.SOL_SOCKET, Opt: unix.SO_SNDBUF, Name: "SO_SNDBUF"}
	SO_SNDBUFFORCE          = Int{Level: unix.SOL_SOCKET, Opt: unix.SO_SNDBUFFORCE, Name: "SO_SNDBUFFORCE"}
	SO_SNDTIMEO             = Timeval{Level: unix.SOL_SOCKET, Opt: unix.SO_SNDTIMEO, Name: "SO_SNDTIMEO"}
	SO_RCVTIMEO             = Timeval{Level: unix.SOL_SOCKET, Opt: unix.SO_RCVTIMEO, Name: "SO_RCVTIMEO"}
	SO_REUSEADDR            = Bool{Level: unix.SOL_SOCKET, Opt: unix.SO_REUSEADDR, Name: "SO_REUSEADDR"}
	SO_REUSEPORT            = Bool{Level: unix.SOL_SOCKET, Opt: unix.SO_REUSEPORT, Name: "SO_REUSEPORT"}
	IP_BIND_ADDRESS_NO_PORT = Bool{Level: unix.IPPROTO_IP, Opt: unix.IP_BIND_ADDRESS_NO_PORT, Name: "IP_BIND_ADDRESS_NO_PORT"}
	IP_FREEBIND             = Bool{Level: unix.IPPROTO_IP, Opt: unix.IP_FREEBIND, Name: "IP_FREEBIND"}
	IP_LOCAL_PORT_RANGE     = Int{Level: unix.IPPROTO_IP, Opt: unix.IP_LOCAL_PORT_RANGE, Name: "IP_LOCAL_PORT_RANGE"}
	IP_TRANSPARENT          = Bool{Level: unix.IPPROTO_IP, Opt: unix.IP_TRANSPARENT, Name: "IP_TRANSPARENT"}
	IP_TTL                  = Int{Level: unix.IPPROTO_IP, Opt: unix.IP_TTL, Name: "IP_TTL"}
	IPV6_V6ONLY             = Bool{Level: unix.IPPROTO_IPV6, Opt: unix.IPV6_V6ONLY, Name: "IPV6_V6ONLY"}
	TCP_CORK                = Bool{Level: unix.IPPROTO_TCP, Opt: unix.TCP_CORK, Name: "TCP_CORK"}
	TCP_DEFER_ACCEPT        = Int{Level: unix.IPPROTO_TCP, Opt: unix.TCP_DEFER_ACCEPT, Name: "TCP_DEFER_ACCEPT"}
	TCP_KEEPCNT             = Int{Level: unix.IPPROTO_TCP, Opt: unix.TCP_KEEPCNT, Name: "TCP_KEEPCNT"}
	TCP_KEEPIDLE            = Int{Level: unix.IPPROTO_TCP, Opt: unix.TCP_KEEPIDLE, Name: "TCP_KEEPIDLE"}
	TCP_KEEPINTVL           = Int{Level: unix.IPPROTO_TCP, Opt: unix.TCP_KEEPINTVL, Name: "TCP_KEEPINTVL"}
	TCP_LINGER2             = Linger{Level: unix.IPPROTO_TCP, Opt: unix.TCP_LINGER2, Name: "TCP_LINGER2"}
	TCP_MAXSEG              = Int{Level: unix.IPPROTO_TCP, Opt: unix.TCP_MAXSEG, Name: "TCP_MAXSEG"}
	TCP_NODELAY             = Bool{Level: unix.IPPROTO_TCP, Opt: unix.TCP_NODELAY, Name: "TCP_NODELAY"}
	TCP_QUICKACK            = Bool{Level: unix.IPPROTO_TCP, Opt: unix.TCP_QUICKACK, Name: "TCP_QUICKACK"}
	TCP_SYNCNT              = Int{Level: unix.IPPROTO_TCP, Opt: unix.TCP_SYNCNT, Name: "TCP_SYNCNT"}
	TCP_USER_TIMEOUT        = Int{Level: unix.IPPROTO_TCP, Opt: unix.TCP_USER_TIMEOUT, Name: "TCP_USER_TIMEOUT"}
	TCP_WINDOW_CLAMP        = Int{Level: unix.IPPROTO_TCP, Opt: unix.TCP_WINDOW_CLAMP, Name: "TCP_WINDOW_CLAMP"}
	TCP_FASTOPEN            = Bool{Level: unix.IPPROTO_TCP, Opt: unix.TCP_FASTOPEN, Name: "TCP_FASTOPEN"}
	TCP_FASTOPEN_CONNECT    = Bool{Level: unix.IPPROTO_TCP, Opt: unix.TCP_FASTOPEN_CONNECT, Name: "TCP_FASTOPEN_CONNECT"}
	UDP_CORK                = Bool{Level: unix.IPPROTO_UDP, Opt: unix.UDP_CORK, Name: "UDP_CORK"}
	UDP_SEGMENT             = Int{Level: unix.IPPROTO_UDP, Opt: unix.UDP_SEGMENT, Name: "UDP_SEGMENT"}
	UDP_GRO                 = Bool{Level: unix.IPPROTO_UDP, Opt: unix.UDP_GRO, Name: "UDP_GRO"}
)

Functions

This section is empty.

Types

type Bool

type Bool Option

func (Bool) SetFunc

func (opt Bool) SetFunc(value bool) SetFunc

type Byte

type Byte Option

func (Byte) SetFunc

func (opt Byte) SetFunc(value byte) SetFunc

type ControlFunc

type ControlFunc func(network, address string, conn syscall.RawConn) error

ControlFunc controls connections. Socket options are configures through conn.

func Control

func Control(fs ...SetFunc) ControlFunc

Control returns a ControlFunc from given functions. Control is the alias for SetFuncs(fs).Control.

type Error

type Error struct {
	Inner error  // Inner is the inner error.
	Level string // Level is the socket level. e.g. "SOL_SOCKET"
	Opt   string // Opt is the option name. e.g. "SO_BINDTOIFINDEX"
	Msg   string // Msg is the error message.
}

Error is the socket option error.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Is

func (e *Error) Is(target error) bool

func (*Error) Unwrap

func (e *Error) Unwrap() error

type Int

type Int Option

func (Int) SetFunc

func (opt Int) SetFunc(value int) SetFunc

type Linger

type Linger Option

func (Linger) SetFunc

func (opt Linger) SetFunc(value int32) SetFunc

type Option

type Option struct {
	Level int    // Level is the protocol level.
	Opt   int    // Opt is the option.
	Name  string // Name is the option name. Used for logging.
}

Option is the socket option.

type SetFunc

type SetFunc func(fd uintptr) error

SetFunc is the function that set socket option to fd. SetFuncs provides Control method that can be used for net.ListenConfig.Control.

func (SetFunc) Control

func (f SetFunc) Control(network, address string, conn syscall.RawConn) (err error)

Control sets socket option to conn.

type SetFuncs

type SetFuncs []SetFunc

SetFuncs is the collection of SetFunc. SetFuncs provides Control method that can be used for net.ListenConfig.Control.

func (SetFuncs) Control

func (fs SetFuncs) Control(network, address string, conn syscall.RawConn) (err error)

Control sets socket options to conn.

type String

type String Option

func (String) SetFunc

func (opt String) SetFunc(value string) SetFunc

type Timeval

type Timeval Option

Directories

Path Synopsis
cmd
generator command
custom command
multiple_opts command
reuseaddr command
reuseport command
single_opt command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL