github.com/database64128/tfo-go/v2@v2.2.0/tfo_listen_generic.go (about)

     1  //go:build freebsd || linux || windows
     2  
     3  package tfo
     4  
     5  import (
     6  	"context"
     7  	"errors"
     8  	"net"
     9  	"syscall"
    10  )
    11  
    12  func (lc *ListenConfig) listenTFO(ctx context.Context, network, address string) (net.Listener, error) {
    13  	ctrlFn := lc.Control
    14  	backlog := lc.Backlog
    15  	llc := *lc
    16  	llc.Control = func(network, address string, c syscall.RawConn) (err error) {
    17  		if ctrlFn != nil {
    18  			if err = ctrlFn(network, address, c); err != nil {
    19  				return err
    20  			}
    21  		}
    22  
    23  		if cerr := c.Control(func(fd uintptr) {
    24  			err = setTFOListenerWithBacklog(fd, backlog)
    25  		}); cerr != nil {
    26  			return cerr
    27  		}
    28  
    29  		if err != nil {
    30  			if !lc.Fallback || !errors.Is(err, errors.ErrUnsupported) {
    31  				return wrapSyscallError("setsockopt(TCP_FASTOPEN)", err)
    32  			}
    33  			runtimeListenNoTFO.Store(true)
    34  		}
    35  		return nil
    36  	}
    37  	return llc.ListenConfig.Listen(ctx, network, address)
    38  }