conn: use errors.Is for unwrapping

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2021-02-09 19:46:57 +01:00
parent da32fe328b
commit 5cdb862f15

View File

@ -8,8 +8,8 @@
package conn package conn
import ( import (
"errors"
"net" "net"
"os"
"syscall" "syscall"
) )
@ -84,18 +84,6 @@ func listenNet(network string, port int) (*net.UDPConn, int, error) {
return conn, uaddr.Port, nil return conn, uaddr.Port, nil
} }
func extractErrno(err error) error {
opErr, ok := err.(*net.OpError)
if !ok {
return nil
}
syscallErr, ok := opErr.Err.(*os.SyscallError)
if !ok {
return nil
}
return syscallErr.Err
}
func createBind(uport uint16) (Bind, uint16, error) { func createBind(uport uint16) (Bind, uint16, error) {
var err error var err error
var bind nativeBind var bind nativeBind
@ -105,16 +93,16 @@ again:
port := int(uport) port := int(uport)
bind.ipv4, port, err = listenNet("udp4", port) bind.ipv4, port, err = listenNet("udp4", port)
if err != nil && extractErrno(err) != syscall.EAFNOSUPPORT { if err != nil && !errors.Is(err, syscall.EAFNOSUPPORT) {
return nil, 0, err return nil, 0, err
} }
bind.ipv6, port, err = listenNet("udp6", port) bind.ipv6, port, err = listenNet("udp6", port)
if uport == 0 && err != nil && extractErrno(err) == syscall.EADDRINUSE && tries < 100 { if uport == 0 && err != nil && errors.Is(err, syscall.EADDRINUSE) && tries < 100 {
tries++ tries++
goto again goto again
} }
if err != nil && extractErrno(err) != syscall.EAFNOSUPPORT { if err != nil && !errors.Is(err, syscall.EAFNOSUPPORT) {
bind.ipv4.Close() bind.ipv4.Close()
bind.ipv4 = nil bind.ipv4 = nil
return nil, 0, err return nil, 0, err