conn: use local ipvN vars in StdNetBind.Open

This makes it clearer that they are fresh on each attempt,
and avoids the bookkeeping required to clearing them on failure.

Also, remove an unnecessary err != nil.

Signed-off-by: Josh Bleecher Snyder <josharian@gmail.com>
This commit is contained in:
Josh Bleecher Snyder 2021-03-29 13:21:06 -07:00
parent 822f5a6d70
commit 204140016a

View file

@ -91,30 +91,28 @@ func (bind *StdNetBind) Open(uport uint16) (uint16, error) {
again: again:
port := int(uport) port := int(uport)
var ipv4, ipv6 *net.UDPConn
bind.ipv4, port, err = listenNet("udp4", port) ipv4, port, err = listenNet("udp4", port)
if err != nil && !errors.Is(err, syscall.EAFNOSUPPORT) { if err != nil && !errors.Is(err, syscall.EAFNOSUPPORT) {
bind.ipv4 = nil
return 0, err return 0, err
} }
bind.ipv6, port, err = listenNet("udp6", port) ipv6, port, err = listenNet("udp6", port)
if uport == 0 && err != nil && errors.Is(err, syscall.EADDRINUSE) && tries < 100 { if uport == 0 && errors.Is(err, syscall.EADDRINUSE) && tries < 100 {
bind.ipv4.Close() ipv4.Close()
bind.ipv4 = nil
bind.ipv6 = nil
tries++ tries++
goto again goto again
} }
if err != nil && !errors.Is(err, syscall.EAFNOSUPPORT) { if err != nil && !errors.Is(err, syscall.EAFNOSUPPORT) {
bind.ipv4.Close() ipv4.Close()
bind.ipv4 = nil
bind.ipv6 = nil
return 0, err return 0, err
} }
if bind.ipv4 == nil && bind.ipv6 == nil { if ipv4 == nil && ipv6 == nil {
return 0, syscall.EAFNOSUPPORT return 0, syscall.EAFNOSUPPORT
} }
bind.ipv4 = ipv4
bind.ipv6 = ipv6
return uint16(port), nil return uint16(port), nil
} }