Skip to content

Commit 1bd70ba

Browse files
gazzuafjl
andauthored
p2p/nat: improve AddMapping code (ethereum#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>
1 parent bc36f2d commit 1bd70ba

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

p2p/nat/natupnp.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func (n *upnp) ExternalIP() (addr net.IP, err error) {
8282
func (n *upnp) AddMapping(protocol string, extport, intport int, desc string, lifetime time.Duration) (uint16, error) {
8383
ip, err := n.internalAddress()
8484
if err != nil {
85-
return 0, nil // TODO: Shouldn't we return the error?
85+
return 0, err
8686
}
8787
protocol = strings.ToUpper(protocol)
8888
lifetimeS := uint32(lifetime / time.Second)
@@ -94,14 +94,15 @@ func (n *upnp) AddMapping(protocol string, extport, intport int, desc string, li
9494
if err == nil {
9595
return uint16(extport), nil
9696
}
97-
98-
return uint16(extport), n.withRateLimit(func() error {
97+
// Try addAnyPortMapping if mapping specified port didn't work.
98+
err = n.withRateLimit(func() error {
9999
p, err := n.addAnyPortMapping(protocol, extport, intport, ip, desc, lifetimeS)
100100
if err == nil {
101101
extport = int(p)
102102
}
103103
return err
104104
})
105+
return uint16(extport), err
105106
}
106107

107108
func (n *upnp) addAnyPortMapping(protocol string, extport, intport int, ip net.IP, desc string, lifetimeS uint32) (uint16, error) {

0 commit comments

Comments
 (0)