github.com/slackhq/nebula@v1.9.0/udp/udp_darwin.go (about)

     1  //go:build !e2e_testing
     2  // +build !e2e_testing
     3  
     4  package udp
     5  
     6  // Darwin support is primarily implemented in udp_generic, besides NewListenConfig
     7  
     8  import (
     9  	"fmt"
    10  	"net"
    11  	"syscall"
    12  
    13  	"github.com/sirupsen/logrus"
    14  	"golang.org/x/sys/unix"
    15  )
    16  
    17  func NewListener(l *logrus.Logger, ip net.IP, port int, multi bool, batch int) (Conn, error) {
    18  	return NewGenericListener(l, ip, port, multi, batch)
    19  }
    20  
    21  func NewListenConfig(multi bool) net.ListenConfig {
    22  	return net.ListenConfig{
    23  		Control: func(network, address string, c syscall.RawConn) error {
    24  			if multi {
    25  				var controlErr error
    26  				err := c.Control(func(fd uintptr) {
    27  					if err := syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, unix.SO_REUSEPORT, 1); err != nil {
    28  						controlErr = fmt.Errorf("SO_REUSEPORT failed: %v", err)
    29  						return
    30  					}
    31  				})
    32  				if err != nil {
    33  					return err
    34  				}
    35  				if controlErr != nil {
    36  					return controlErr
    37  				}
    38  			}
    39  
    40  			return nil
    41  		},
    42  	}
    43  }
    44  
    45  func (u *GenericConn) Rebind() error {
    46  	rc, err := u.UDPConn.SyscallConn()
    47  	if err != nil {
    48  		return err
    49  	}
    50  
    51  	return rc.Control(func(fd uintptr) {
    52  		err := syscall.SetsockoptInt(int(fd), unix.IPPROTO_IPV6, unix.IPV6_BOUND_IF, 0)
    53  		if err != nil {
    54  			u.l.WithError(err).Error("Failed to rebind udp socket")
    55  		}
    56  	})
    57  }