|
|
|
|
@ -1487,16 +1487,20 @@ func (c *Conn) initialBind() error { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (c *Conn) bind1(ruc **RebindingUDPConn, which string) error { |
|
|
|
|
host := "" |
|
|
|
|
if v, _ := strconv.ParseBool(os.Getenv("IN_TS_TEST")); v { |
|
|
|
|
host = "127.0.0.1" |
|
|
|
|
} |
|
|
|
|
var pc net.PacketConn |
|
|
|
|
var err error |
|
|
|
|
if c.pconnPort == 0 && DefaultPort != 0 { |
|
|
|
|
pc, err = net.ListenPacket(which, fmt.Sprintf(":%d", DefaultPort)) |
|
|
|
|
pc, err = net.ListenPacket(which, fmt.Sprintf("%s:%d", host, DefaultPort)) |
|
|
|
|
if err != nil { |
|
|
|
|
c.logf("magicsock: bind: default port %s/%v unavailable; picking random", which, DefaultPort) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
if pc == nil { |
|
|
|
|
pc, err = net.ListenPacket(which, fmt.Sprintf(":%d", c.pconnPort)) |
|
|
|
|
pc, err = net.ListenPacket(which, fmt.Sprintf("%s:%d", host, c.pconnPort)) |
|
|
|
|
} |
|
|
|
|
if err != nil { |
|
|
|
|
c.logf("magicsock: bind(%s/%v): %v", which, c.pconnPort, err) |
|
|
|
|
@ -1512,12 +1516,16 @@ func (c *Conn) bind1(ruc **RebindingUDPConn, which string) error { |
|
|
|
|
// Rebind closes and re-binds the UDP sockets.
|
|
|
|
|
// It should be followed by a call to ReSTUN.
|
|
|
|
|
func (c *Conn) Rebind() { |
|
|
|
|
host := "" |
|
|
|
|
if v, _ := strconv.ParseBool(os.Getenv("IN_TS_TEST")); v { |
|
|
|
|
host = "127.0.0.1" |
|
|
|
|
} |
|
|
|
|
if c.pconnPort != 0 { |
|
|
|
|
c.pconn4.mu.Lock() |
|
|
|
|
if err := c.pconn4.pconn.Close(); err != nil { |
|
|
|
|
c.logf("magicsock: link change close failed: %v", err) |
|
|
|
|
} |
|
|
|
|
packetConn, err := net.ListenPacket("udp4", fmt.Sprintf(":%d", c.pconnPort)) |
|
|
|
|
packetConn, err := net.ListenPacket("udp4", fmt.Sprintf("%s:%d", host, c.pconnPort)) |
|
|
|
|
if err == nil { |
|
|
|
|
c.logf("magicsock: link change rebound port: %d", c.pconnPort) |
|
|
|
|
c.pconn4.pconn = packetConn.(*net.UDPConn) |
|
|
|
|
@ -1528,7 +1536,7 @@ func (c *Conn) Rebind() { |
|
|
|
|
c.pconn4.mu.Unlock() |
|
|
|
|
} |
|
|
|
|
c.logf("magicsock: link change, binding new port") |
|
|
|
|
packetConn, err := net.ListenPacket("udp4", ":0") |
|
|
|
|
packetConn, err := net.ListenPacket("udp4", host+":0") |
|
|
|
|
if err != nil { |
|
|
|
|
c.logf("magicsock: link change failed to bind new port: %v", err) |
|
|
|
|
return |
|
|
|
|
|