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 <josharian@gmail.com>
This commit is contained in:
Josh Bleecher Snyder 2021-04-27 12:56:28 -07:00
parent 3c9dea85e6
commit cc3d753a53

View File

@ -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