diff --git a/p2p/nat/natupnp.go b/p2p/nat/natupnp.go index f1bb955892..673a0b36d6 100644 --- a/p2p/nat/natupnp.go +++ b/p2p/nat/natupnp.go @@ -86,7 +86,6 @@ func (n *upnp) AddMapping(protocol string, extport, intport int, desc string, li } protocol = strings.ToUpper(protocol) lifetimeS := uint32(lifetime / time.Second) - n.DeleteMapping(protocol, extport, intport) err = n.withRateLimit(func() error { return n.client.AddPortMapping("", uint16(extport), protocol, uint16(intport), ip.String(), true, desc, lifetimeS) @@ -110,12 +109,15 @@ func (n *upnp) addAnyPortMapping(protocol string, extport, intport int, ip net.I } // It will retry with a random port number if the client does // not support AddAnyPortMapping. - extport = n.randomPort() - err := n.client.AddPortMapping("", uint16(extport), protocol, uint16(intport), ip.String(), true, desc, lifetimeS) - if err != nil { - return 0, err + var err error + for i := 0; i < 3; i++ { + extport = n.randomPort() + err := n.client.AddPortMapping("", uint16(extport), protocol, uint16(intport), ip.String(), true, desc, lifetimeS) + if err == nil { + return uint16(extport), nil + } } - return uint16(extport), nil + return 0, err } func (n *upnp) randomPort() int {