package module
v1.4.8 Latest Latest

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

Go to latest
Published: Jul 12, 2024 License: Apache-2.0 Imports: 11 Imported by: 26


GitHub release CI Go Reference Go Report Card

Add-ons for networking

This package overrides a package of the same name.


  • Functions for IPv4/v6 address calculations.
  • Functions to identify network errors.
  • Functions for TLS constants.
  • Utilities for TCP keep-alive.
  • Interface to half-close connections.
  • functions.


Read the documentation.


netutil is licensed under the Apache License, Version 2.0.



Package netutil provides a few more network utilities over

This package overrides netutil namespace since it provides all functions in



This section is empty.


View Source
var (
	// ErrIPv6 is an error that a function does not support IPv6.
	ErrIPv6 = errors.New("only IPv4 address is supported")


func CipherSuiteString added in v1.1.0

func CipherSuiteString(suite uint16) string

CipherSuiteString returns string ID for the given cipher suite defined in crypto/tls.

func DetectMTU added in v1.4.0

func DetectMTU() (int, error)

DetectMTU returns the right MTU value for communications to the Internet. This may return zero if it fails to detect MTU.

func EqualIP added in v1.3.0

func EqualIP(ip net.IP) types.GomegaMatcher

EqualIP is a custom mather of Gomega to assert IP equivalence

func HostsFunc deprecated

func HostsFunc(n *net.IPNet) (func() net.IP, error)

HostsFunc returns a function to generate all IP addresses in a network. The network address and the broadcast address of the network will be excluded.

The returned function will finally generate nil to tell the end.

The network must be an IPv4 network.

Deprecated: this cannot be used for IPv6 networks.

func IP4ToInt deprecated

func IP4ToInt(ip net.IP) uint32

IP4ToInt returns uint32 value for an IPv4 address. If ip is not an IPv4 address, this returns 0.

Deprecated: use IPAdd and IPDiff.

func IPAdd added in v1.3.0

func IPAdd(ip net.IP, val int64) net.IP

IPAdd adds `val` to `ip`. If `ip` is IPv4 address, the value returned is IPv4, or nil when over/underflowed. If `ip` is IPv6 address, the value returned is IPv6, or nil when over/underflowed.

func IPDiff added in v1.3.0

func IPDiff(ip1, ip2 net.IP) int64

IPDiff calculates the numeric difference between two IP addresses. Intuitively, the calculation is done as `ip2 - ip1`. `ip1` and `ip2` must be the same IP version (4 or 6).

func IntToIP4 deprecated

func IntToIP4(n uint32) net.IP

IntToIP4 does the reverse of IP4ToInt.

Deprecated: use IPAdd and IPDiff.

func IsConnectionRefused added in v1.0.1

func IsConnectionRefused(err error) bool

IsConnectionRefused returns true if err indicates ECONNREFUSED errno.

func IsNetworkUnreachable added in v1.0.1

func IsNetworkUnreachable(err error) bool

IsNetworkUnreachable returns true if err indicates ENETUNREACH errno.

func IsNoRouteToHost added in v1.0.1

func IsNoRouteToHost(err error) bool

IsNoRouteToHost returns true if err indicates EHOSTUNREACH errno.

func KeepAliveListener

func KeepAliveListener(l net.Listener) net.Listener

KeepAliveListener returns TCPKeepAliveListener if l is a *net.TCPListener. Otherwise, l is returned without change.

func LimitListener

func LimitListener(l net.Listener, n int) net.Listener

LimitListener is the same as

func SetKeepAlive

func SetKeepAlive(c net.Conn)

SetKeepAlive enables TCP keep-alive if c is a *net.TCPConn.

func TLSVersionString added in v1.1.0

func TLSVersionString(ver uint16) string

TLSVersionString returns string for the given SSL/TLS version.


type HalfCloser

type HalfCloser interface {
	CloseRead() error
	CloseWrite() error

HalfCloser is an interface for connections that can be half-closed. TCPConn and UNIXConn implement this.

type TCPKeepAliveListener

type TCPKeepAliveListener struct {

TCPKeepAliveListener wraps *net.TCPListener.

func (TCPKeepAliveListener) Accept

func (l TCPKeepAliveListener) Accept() (c net.Conn, err error)

Accept returns a TCP keep-alive enabled connection.

Jump to

Keyboard shortcuts

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