github.com/chwjbn/xclash@v0.2.0/component/dialer/reuse_unix.go (about)

     1  //go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
     2  // +build darwin dragonfly freebsd linux netbsd openbsd solaris
     3  
     4  package dialer
     5  
     6  import (
     7  	"net"
     8  	"syscall"
     9  
    10  	"golang.org/x/sys/unix"
    11  )
    12  
    13  func addrReuseToListenConfig(lc *net.ListenConfig) {
    14  	chain := lc.Control
    15  
    16  	lc.Control = func(network, address string, c syscall.RawConn) (err error) {
    17  		defer func() {
    18  			if err == nil && chain != nil {
    19  				err = chain(network, address, c)
    20  			}
    21  		}()
    22  
    23  		return c.Control(func(fd uintptr) {
    24  			unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEADDR, 1)
    25  			unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_REUSEPORT, 1)
    26  		})
    27  	}
    28  }