1
0
Fork 0
forked from forks/go-ethereum

p2p/nat: improve AddMapping code (#31486)

It introduces a new variable to store the external port returned by the
addAnyPortMapping function and ensures that the correct external port is
returned even in case of an error.

---------

Co-authored-by: Felix Lange <fjl@twurst.com>
This commit is contained in:
John 2025-04-01 21:07:47 +09:00 committed by GitHub
parent bc36f2de83
commit 1bd70ba57a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -82,7 +82,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) {
ip, err := n.internalAddress()
if err != nil {
return 0, nil // TODO: Shouldn't we return the error?
return 0, err
}
protocol = strings.ToUpper(protocol)
lifetimeS := uint32(lifetime / time.Second)
@ -94,14 +94,15 @@ func (n *upnp) AddMapping(protocol string, extport, intport int, desc string, li
if err == nil {
return uint16(extport), nil
}
return uint16(extport), n.withRateLimit(func() error {
// Try addAnyPortMapping if mapping specified port didn't work.
err = n.withRateLimit(func() error {
p, err := n.addAnyPortMapping(protocol, extport, intport, ip, desc, lifetimeS)
if err == nil {
extport = int(p)
}
return err
})
return uint16(extport), err
}
func (n *upnp) addAnyPortMapping(protocol string, extport, intport int, ip net.IP, desc string, lifetimeS uint32) (uint16, error) {