net/netns: unindent, refactor to remove some redunant code

Also:
* always error on Control failing. That's very unexpected.
* pull out sockopt funcs into their own funcs for easier future testing
pull/427/head
Brad Fitzpatrick 5 years ago
parent 45b139d338
commit 7a410f9236

@ -110,25 +110,32 @@ func control(network, address string, c syscall.RawConn) error {
return nil return nil
} }
if ipRuleAvailable() { var sockErr error
var controlErr error
err := c.Control(func(fd uintptr) { err := c.Control(func(fd uintptr) {
controlErr = unix.SetsockoptInt(int(fd), if ipRuleAvailable() {
unix.SOL_SOCKET, unix.SO_MARK, sockErr = setBypassMark(fd)
tailscaleBypassMark) } else {
sockErr = bindToDevice(fd)
}
}) })
if (err != nil || controlErr != nil) && ignoreErrors() { if err != nil {
return fmt.Errorf("RawConn.Control on %T: %w", c, err)
}
if sockErr != nil && ignoreErrors() {
// TODO(bradfitz): maybe log once? probably too spammy for e.g. CLI tools like tailscale netcheck.
return nil return nil
} }
if err != nil { return sockErr
return fmt.Errorf("setting socket mark1: %w", err)
} }
if controlErr != nil {
return fmt.Errorf("setting socket mark2: %w", controlErr) func setBypassMark(fd uintptr) error {
if err := unix.SetsockoptInt(int(fd), unix.SOL_SOCKET, unix.SO_MARK, tailscaleBypassMark); err != nil {
return fmt.Errorf("setting SO_MARK bypass: %w", err)
} }
} else { return nil
var controlErr error }
err := c.Control(func(fd uintptr) {
func bindToDevice(fd uintptr) error {
ifc, err := defaultRouteInterface() ifc, err := defaultRouteInterface()
if err != nil { if err != nil {
// Make sure we bind to *some* interface, // Make sure we bind to *some* interface,
@ -137,18 +144,8 @@ func control(network, address string, c syscall.RawConn) error {
// a default route anyway, it doesn't matter. // a default route anyway, it doesn't matter.
ifc = "lo" ifc = "lo"
} }
controlErr = unix.SetsockoptString(int(fd), if err := unix.SetsockoptString(int(fd), unix.SOL_SOCKET, unix.SO_BINDTODEVICE, ifc); err != nil {
unix.SOL_SOCKET, unix.SO_BINDTODEVICE, ifc) return fmt.Errorf("setting SO_BINDTODEVICE: %w", err)
})
if (err != nil || controlErr != nil) && ignoreErrors() {
return nil
}
if err != nil {
return fmt.Errorf("setting SO_BINDTODEVICE 1: %w", err)
}
if controlErr != nil {
return fmt.Errorf("setting SO_BINDTODEVICE 2: %w", controlErr)
}
} }
return nil return nil
} }

Loading…
Cancel
Save