From cc3d753a5339443aa2ae7b8fd2fbc19234e8f249 Mon Sep 17 00:00:00 2001 From: Josh Bleecher Snyder Date: Tue, 27 Apr 2021 12:56:28 -0700 Subject: [PATCH] wgengine/magicsock: remove context arg from listenPacket It was set to context.Background by all callers, for the same reasons. Set it locally instead, to simplify call sites. Signed-off-by: Josh Bleecher Snyder --- wgengine/magicsock/magicsock.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/wgengine/magicsock/magicsock.go b/wgengine/magicsock/magicsock.go index d7230e651..4e49d0531 100644 --- a/wgengine/magicsock/magicsock.go +++ b/wgengine/magicsock/magicsock.go @@ -2599,7 +2599,9 @@ func (c *Conn) initialBind() error { return nil } -func (c *Conn) listenPacket(ctx context.Context, network, host string, port uint16) (net.PacketConn, error) { +// listenPacket opens a listener. Host must be "" or an IP address. +func (c *Conn) listenPacket(network, host string, port uint16) (net.PacketConn, error) { + ctx := context.Background() // unused without DNS name to resolve addr := net.JoinHostPort(host, fmt.Sprint(port)) if c.packetListener != nil { return c.packetListener.ListenPacket(ctx, network, addr) @@ -2617,15 +2619,14 @@ func (c *Conn) bind1(ruc **RebindingUDPConn, which string) error { } var pc net.PacketConn var err error - listenCtx := context.Background() // unused without DNS name to resolve if c.port == 0 && DefaultPort != 0 { - pc, err = c.listenPacket(listenCtx, which, host, DefaultPort) + pc, err = c.listenPacket(which, host, DefaultPort) if err != nil { c.logf("magicsock: bind: default port %s/%v unavailable; picking random", which, DefaultPort) } } if pc == nil { - pc, err = c.listenPacket(listenCtx, which, host, c.port) + pc, err = c.listenPacket(which, host, c.port) } if err != nil { c.logf("magicsock: bind(%s/%v): %v", which, c.port, err) @@ -2645,7 +2646,6 @@ func (c *Conn) Rebind() { if inTest() && !c.simulatedNetwork { host = "127.0.0.1" } - listenCtx := context.Background() // unused without DNS name to resolve if c.port != 0 { c.pconn4.mu.Lock() @@ -2653,10 +2653,10 @@ func (c *Conn) Rebind() { if err := c.pconn4.pconn.Close(); err != nil { c.logf("magicsock: link change close failed: %v", err) } - packetConn, err := c.listenPacket(listenCtx, "udp4", host, c.port) + packetConn, err := c.listenPacket("udp4", host, c.port) if err != nil { c.logf("magicsock: link change unable to bind fixed port %d: %v, falling back to random port", c.port, err) - packetConn, err = c.listenPacket(listenCtx, "udp4", host, 0) + packetConn, err = c.listenPacket("udp4", host, 0) if err != nil { c.logf("magicsock: link change failed to bind random port: %v", err) c.pconn4.mu.Unlock() @@ -2671,7 +2671,7 @@ func (c *Conn) Rebind() { c.pconn4.mu.Unlock() } else { c.logf("magicsock: link change, binding new port") - packetConn, err := c.listenPacket(listenCtx, "udp4", host, 0) + packetConn, err := c.listenPacket("udp4", host, 0) if err != nil { c.logf("magicsock: link change failed to bind new port: %v", err) return