comparison src/plugins/plugin_misc_nat-port.py @ 2488:78c7992a26ed

plugin NAT-port: raise MappingError if something get wrong during "addportmapping" + log unexpected errors (i.e. everything else than MappingError)
author Goffi <goffi@goffi.org>
date Thu, 01 Feb 2018 07:24:34 +0100
parents 0046283a285d
children
comparison
equal deleted inserted replaced
2487:ed1d71b91b29 2488:78c7992a26ed
148 if starting_port is None: 148 if starting_port is None:
149 # XXX: we cache the first successfuly found external port 149 # XXX: we cache the first successfuly found external port
150 # to avoid testing again the first series the next time 150 # to avoid testing again the first series the next time
151 self._starting_port_cache = ext_port 151 self._starting_port_cache = ext_port
152 152
153 mapping = self._upnp.addportmapping( 153 try:
154 # the last parameter is remoteHost, we don't use it 154 mapping = self._upnp.addportmapping(
155 ext_port, protocol, self._upnp.lanaddr, int_port, desc, '') 155 # the last parameter is remoteHost, we don't use it
156 ext_port, protocol, self._upnp.lanaddr, int_port, desc, '')
157 except Exception as e:
158 log.error(_(u"addportmapping error: {msg}").format(msg=e))
159 raise failure.Failure(MappingError())
156 160
157 if not mapping: 161 if not mapping:
158 raise MappingError 162 raise failure.Failure(MappingError())
159 else: 163 else:
160 self._to_unmap.append((ext_port, protocol)) 164 self._to_unmap.append((ext_port, protocol))
161 finally: 165 finally:
162 self._mutex.release() 166 self._mutex.release()
163 167
180 protocol = protocol, 184 protocol = protocol,
181 int_port = int_port, 185 int_port = int_port,
182 ext_port = ext_port, 186 ext_port = ext_port,
183 )) 187 ))
184 return ext_port 188 return ext_port
185 def mappingEb(failure): 189 def mappingEb(failure_):
186 failure.trap(MappingError) 190 failure_.trap(MappingError)
187 log.warning(u"Can't map internal {int_port}".format(int_port=int_port)) 191 log.warning(u"Can't map internal {int_port}".format(int_port=int_port))
192 def mappingUnknownEb(failure_):
193 log.error(_(u"error while trying to map ports: {msg}").format(msg=failure_))
188 d = threads.deferToThread(self._mapPortBlocking, int_port, ext_port, protocol, desc) 194 d = threads.deferToThread(self._mapPortBlocking, int_port, ext_port, protocol, desc)
189 d.addCallbacks(mappingCb, mappingEb) 195 d.addCallbacks(mappingCb, mappingEb)
196 d.addErrback(mappingUnknownEb)
190 return d 197 return d