mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-12 23:13:45 +00:00
p2p/nat: retry UPnP mapping several times
Changing ENR is a bit disruptive, so we better retry a few times. Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
parent
d4a32c82de
commit
54d124ba46
1 changed files with 18 additions and 10 deletions
|
|
@ -35,6 +35,9 @@ import (
|
||||||
const (
|
const (
|
||||||
soapRequestTimeout = 3 * time.Second
|
soapRequestTimeout = 3 * time.Second
|
||||||
rateLimit = 200 * time.Millisecond
|
rateLimit = 200 * time.Millisecond
|
||||||
|
retryInterval = 1 * time.Second // time to wait between retries
|
||||||
|
retryCount = 3 // number of retries after a failed AddPortMapping
|
||||||
|
randomCount = 3 // number of random ports to try
|
||||||
)
|
)
|
||||||
|
|
||||||
type upnp struct {
|
type upnp struct {
|
||||||
|
|
@ -115,21 +118,26 @@ func (n *upnp) addAnyPortMapping(protocol string, extport, intport int, ip net.I
|
||||||
return client.AddAnyPortMapping("", uint16(extport), protocol, uint16(intport), ip.String(), true, desc, lifetimeS)
|
return client.AddAnyPortMapping("", uint16(extport), protocol, uint16(intport), ip.String(), true, desc, lifetimeS)
|
||||||
}
|
}
|
||||||
// For IGDv1 and v1 services we should first try to add with extport.
|
// For IGDv1 and v1 services we should first try to add with extport.
|
||||||
err := n.client.AddPortMapping("", uint16(extport), protocol, uint16(intport), ip.String(), true, desc, lifetimeS)
|
for i := 0; i < retryCount+1; i++ {
|
||||||
if err == nil {
|
|
||||||
return uint16(extport), nil
|
|
||||||
}
|
|
||||||
log.Trace("Failed to add port mapping", "protocol", protocol, "extport", extport, "intport", intport, "err", err)
|
|
||||||
|
|
||||||
// If above fails, we retry with a random port.
|
|
||||||
// We retry several times because of possible port conflicts.
|
|
||||||
for i := 0; i < 3; i++ {
|
|
||||||
extport = n.randomPort()
|
|
||||||
err := n.client.AddPortMapping("", uint16(extport), protocol, uint16(intport), ip.String(), true, desc, lifetimeS)
|
err := n.client.AddPortMapping("", uint16(extport), protocol, uint16(intport), ip.String(), true, desc, lifetimeS)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return uint16(extport), nil
|
return uint16(extport), nil
|
||||||
}
|
}
|
||||||
|
log.Trace("Failed to add port mapping", "protocol", protocol, "extport", extport, "intport", intport, "err", err)
|
||||||
|
time.Sleep(retryInterval)
|
||||||
|
}
|
||||||
|
|
||||||
|
// If above fails, we retry with a random port.
|
||||||
|
// We retry several times because of possible port conflicts.
|
||||||
|
var err error
|
||||||
|
for i := 0; i < randomCount; 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
|
||||||
|
}
|
||||||
log.Trace("Failed to add random port mapping", "protocol", protocol, "extport", extport, "intport", intport, "err", err)
|
log.Trace("Failed to add random port mapping", "protocol", protocol, "extport", extport, "intport", intport, "err", err)
|
||||||
|
time.Sleep(retryInterval)
|
||||||
}
|
}
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue