p2p/nat: add some trace logging to UPnP

Signed-off-by: Csaba Kiraly <csaba.kiraly@gmail.com>
This commit is contained in:
Csaba Kiraly 2025-04-03 10:06:53 +02:00
parent 78930c11fb
commit d4a32c82de
No known key found for this signature in database
GPG key ID: 0FE274EE8C95166E

View file

@ -26,6 +26,7 @@ import (
"sync" "sync"
"time" "time"
"github.com/ethereum/go-ethereum/log"
"github.com/huin/goupnp" "github.com/huin/goupnp"
"github.com/huin/goupnp/dcps/internetgateway1" "github.com/huin/goupnp/dcps/internetgateway1"
"github.com/huin/goupnp/dcps/internetgateway2" "github.com/huin/goupnp/dcps/internetgateway2"
@ -80,6 +81,7 @@ func (n *upnp) ExternalIP() (addr net.IP, err error) {
} }
func (n *upnp) AddMapping(protocol string, extport, intport int, desc string, lifetime time.Duration) (uint16, error) { func (n *upnp) AddMapping(protocol string, extport, intport int, desc string, lifetime time.Duration) (uint16, error) {
ip, err := n.internalAddress() ip, err := n.internalAddress()
if err != nil { if err != nil {
return 0, err return 0, err
@ -91,7 +93,8 @@ func (n *upnp) AddMapping(protocol string, extport, intport int, desc string, li
extport = intport extport = intport
} else { } else {
// Only delete port mapping if the external port was already used by geth. // Only delete port mapping if the external port was already used by geth.
n.DeleteMapping(protocol, extport, intport) err := n.DeleteMapping(protocol, extport, intport)
log.Trace("Deleting port mapping", "protocol", protocol, "extport", extport, "intport", intport, "err", err)
} }
// Try to add port mapping, preferring the specified external port. // Try to add port mapping, preferring the specified external port.
@ -116,6 +119,7 @@ func (n *upnp) addAnyPortMapping(protocol string, extport, intport int, ip net.I
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)
// If above fails, we retry with a random port. // If above fails, we retry with a random port.
// We retry several times because of possible port conflicts. // We retry several times because of possible port conflicts.
@ -125,6 +129,7 @@ func (n *upnp) addAnyPortMapping(protocol string, extport, intport int, ip net.I
if err == nil { if err == nil {
return uint16(extport), nil return uint16(extport), nil
} }
log.Trace("Failed to add random port mapping", "protocol", protocol, "extport", extport, "intport", intport, "err", err)
} }
return 0, err return 0, err
} }