@ -2599,7 +2599,9 @@ func (c *Conn) initialBind() error {
return nil
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 ) )
addr := net . JoinHostPort ( host , fmt . Sprint ( port ) )
if c . packetListener != nil {
if c . packetListener != nil {
return c . packetListener . ListenPacket ( ctx , network , addr )
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 pc net . PacketConn
var err error
var err error
listenCtx := context . Background ( ) // unused without DNS name to resolve
if c . port == 0 && DefaultPort != 0 {
if c . port == 0 && DefaultPort != 0 {
pc , err = c . listenPacket ( listenCtx, which, host , DefaultPort )
pc , err = c . listenPacket ( which, host , DefaultPort )
if err != nil {
if err != nil {
c . logf ( "magicsock: bind: default port %s/%v unavailable; picking random" , which , DefaultPort )
c . logf ( "magicsock: bind: default port %s/%v unavailable; picking random" , which , DefaultPort )
}
}
}
}
if pc == nil {
if pc == nil {
pc , err = c . listenPacket ( listenCtx, which, host , c . port )
pc , err = c . listenPacket ( which, host , c . port )
}
}
if err != nil {
if err != nil {
c . logf ( "magicsock: bind(%s/%v): %v" , which , c . port , err )
c . logf ( "magicsock: bind(%s/%v): %v" , which , c . port , err )
@ -2645,7 +2646,6 @@ func (c *Conn) Rebind() {
if inTest ( ) && ! c . simulatedNetwork {
if inTest ( ) && ! c . simulatedNetwork {
host = "127.0.0.1"
host = "127.0.0.1"
}
}
listenCtx := context . Background ( ) // unused without DNS name to resolve
if c . port != 0 {
if c . port != 0 {
c . pconn4 . mu . Lock ( )
c . pconn4 . mu . Lock ( )
@ -2653,10 +2653,10 @@ func (c *Conn) Rebind() {
if err := c . pconn4 . pconn . Close ( ) ; err != nil {
if err := c . pconn4 . pconn . Close ( ) ; err != nil {
c . logf ( "magicsock: link change close failed: %v" , err )
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 {
if err != nil {
c . logf ( "magicsock: link change unable to bind fixed port %d: %v, falling back to random port" , c . port , err )
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 {
if err != nil {
c . logf ( "magicsock: link change failed to bind random port: %v" , err )
c . logf ( "magicsock: link change failed to bind random port: %v" , err )
c . pconn4 . mu . Unlock ( )
c . pconn4 . mu . Unlock ( )
@ -2671,7 +2671,7 @@ func (c *Conn) Rebind() {
c . pconn4 . mu . Unlock ( )
c . pconn4 . mu . Unlock ( )
} else {
} else {
c . logf ( "magicsock: link change, binding new port" )
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 {
if err != nil {
c . logf ( "magicsock: link change failed to bind new port: %v" , err )
c . logf ( "magicsock: link change failed to bind new port: %v" , err )
return
return