@@ -107,30 +107,30 @@ func (n *upnp) addAnyPortMapping(protocol string, extport, intport int, ip net.I
107107 })
108108 }
109109 // For IGDv1 and v1 services we should first try to add with extport.
110+ var lastErr error
110111 for i := 0 ; i < retryCount + 1 ; i ++ {
111- err : = n .withRateLimit (func () error {
112+ lastErr = n .withRateLimit (func () error {
112113 return n .client .AddPortMapping ("" , uint16 (extport ), protocol , uint16 (intport ), ip .String (), true , desc , lifetimeS )
113114 })
114- if err == nil {
115+ if lastErr == nil {
115116 return uint16 (extport ), nil
116117 }
117- log .Debug ("Failed to add port mapping" , "protocol" , protocol , "extport" , extport , "intport" , intport , "err" , err )
118+ log .Debug ("Failed to add port mapping" , "protocol" , protocol , "extport" , extport , "intport" , intport , "err" , lastErr )
118119 }
119120
120121 // If above fails, we retry with a random port.
121122 // We retry several times because of possible port conflicts.
122- var err error
123123 for i := 0 ; i < randomCount ; i ++ {
124124 extport = n .randomPort ()
125- err : = n .withRateLimit (func () error {
125+ lastErr = n .withRateLimit (func () error {
126126 return n .client .AddPortMapping ("" , uint16 (extport ), protocol , uint16 (intport ), ip .String (), true , desc , lifetimeS )
127127 })
128- if err == nil {
128+ if lastErr == nil {
129129 return uint16 (extport ), nil
130130 }
131- log .Debug ("Failed to add random port mapping" , "protocol" , protocol , "extport" , extport , "intport" , intport , "err" , err )
131+ log .Debug ("Failed to add random port mapping" , "protocol" , protocol , "extport" , extport , "intport" , intport , "err" , lastErr )
132132 }
133- return 0 , err
133+ return 0 , lastErr
134134}
135135
136136func (n * upnp ) randomPort () int {
0 commit comments