From 204140016af494ef8e0dd2749d4ae6c4a0828a9e Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Mon, 29 Mar 2021 13:21:06 -0700 Subject: 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 --- conn/bind_std.go | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/conn/bind_std.go b/conn/bind_std.go index 73946b4..a511341 100644 --- a/conn/bind_std.go +++ b/conn/bind_std.go @@ -91,30 +91,28 @@ func (bind *StdNetBind) Open(uport uint16) (uint16, error) { again: 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) { - bind.ipv4 = nil return 0, err } - bind.ipv6, port, err = listenNet("udp6", port) - if uport == 0 && err != nil && errors.Is(err, syscall.EADDRINUSE) && tries < 100 { - bind.ipv4.Close() - bind.ipv4 = nil - bind.ipv6 = nil + ipv6, port, err = listenNet("udp6", port) + if uport == 0 && errors.Is(err, syscall.EADDRINUSE) && tries < 100 { + ipv4.Close() tries++ goto again } if err != nil && !errors.Is(err, syscall.EAFNOSUPPORT) { - bind.ipv4.Close() - bind.ipv4 = nil - bind.ipv6 = nil + ipv4.Close() return 0, err } - if bind.ipv4 == nil && bind.ipv6 == nil { + if ipv4 == nil && ipv6 == nil { return 0, syscall.EAFNOSUPPORT } + bind.ipv4 = ipv4 + bind.ipv6 = ipv6 return uint16(port), nil } -- cgit v1.2.3