reuseport

package module
v0.4.5 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2023 License: ISC Imports: 6 Imported by: 1

README

reuseport Go

Fork of go-reuseport

This package enables listening and dialing from the same TCP or UDP port. This means that the following sockopts may be set:

SO_REUSEADDR
SO_REUSEPORT

This is a simple package to help with address reuse. This is particularly important when attempting to do TCP NAT holepunching, which requires a process to both Listen and Dial on the same TCP port. This package provides some utilities around enabling this behaviour on various OS.

Examples

// listen on the same port. oh yeah.
l1, _ := reuse.Listen("tcp", "127.0.0.1:1234")
l2, _ := reuse.Listen("tcp", "127.0.0.1:1234")
// dial from the same port. oh yeah.
l1, _ := reuse.Listen("tcp", "127.0.0.1:1234")
l2, _ := reuse.Listen("tcp", "127.0.0.1:1235")
c, _ := reuse.Dial("tcp", "127.0.0.1:1234", "127.0.0.1:1235")

Note: cant dial self because tcp/ip stacks use 4-tuples to identify connections, and doing so would clash.

Additional helper function Available() returns whether or not SO_REUSEADDR and SO_REUSEPORT are available in the OS.

Documentation

Overview

Package reuseport provides Listen and Dial functions that set socket options in order to be able to reuse ports. You should only use this package if you know what SO_REUSEADDR and SO_REUSEPORT are.

For example:

// listen on the same port.
l1, _ := reuse.Listen("tcp", "127.0.0.1:1234")
l2, _ := reuse.Listen("tcp", "127.0.0.1:1234")

// dial from the same port.
l1, _ := reuse.Listen("tcp", "127.0.0.1:1234")
l2, _ := reuse.Listen("tcp", "127.0.0.1:1235")
c, _ := reuse.Dial("tcp", "127.0.0.1:1234", "127.0.0.1:1235")

Note: cant dial self because tcp/ip stacks use 4-tuples to identify connections, and doing so would clash.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Available

func Available() (reuseaddr, reuseport bool)

Available returns whether or not SO_REUSEADDR and SO_REUSEPORT are available in the OS.

func Control

func Control(network, address string, c syscall.RawConn) (err error)

func Dial

func Dial(network, laddr, raddr string) (net.Conn, error)

Dial dials the given network and address. see net.Dial Returns a net.Conn created from a file descriptor for a socket with SO_REUSEPORT and SO_REUSEADDR option set.

func DialTimeout

func DialTimeout(network, laddr, raddr string, timeout time.Duration) (net.Conn, error)

Dial dials the given network and address, with the given timeout. see net.DialTimeout Returns a net.Conn created from a file descriptor for a socket with SO_REUSEPORT and SO_REUSEADDR option set.

func Listen

func Listen(network, address string) (net.Listener, error)

Listen listens at the given network and address. see net.Listen Returns a net.Listener created from a file descriptor for a socket with SO_REUSEPORT and SO_REUSEADDR option set.

func ListenPacket

func ListenPacket(network, address string) (net.PacketConn, error)

ListenPacket listens at the given network and address. see net.ListenPacket Returns a net.Listener created from a file descriptor for a socket with SO_REUSEPORT and SO_REUSEADDR option set.

func ResolveAddr

func ResolveAddr(network, address string) (net.Addr, error)

Types

This section is empty.

Jump to

Keyboard shortcuts

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