Set permanent UPnP lease when required (fixes #1831)

This commit is contained in:
Lode Hoste
2015-05-21 09:55:55 +02:00
parent 1df6589533
commit 5042248260
2 changed files with 44 additions and 4 deletions

View File

@@ -527,6 +527,11 @@ type getExternalIPAddressResponse struct {
NewExternalIPAddress string `xml:"NewExternalIPAddress"`
}
type soapErrorResponse struct {
ErrorCode int `xml:"Body>Fault>detail>UPnPError>errorCode"`
ErrorDescription string `xml:"Body>Fault>detail>UPnPError>errorDescription"`
}
// AddPortMapping adds a port mapping to the specified IGD service.
func (s *IGDService) AddPortMapping(localIPAddress string, protocol Protocol, externalPort, internalPort int, description string, timeout int) error {
tpl := `<u:AddPortMapping xmlns:u="%s">
@@ -541,12 +546,20 @@ func (s *IGDService) AddPortMapping(localIPAddress string, protocol Protocol, ex
</u:AddPortMapping>`
body := fmt.Sprintf(tpl, s.serviceURN, externalPort, protocol, internalPort, localIPAddress, description, timeout)
_, err := soapRequest(s.serviceURL, s.serviceURN, "AddPortMapping", body)
if err != nil {
return err
response, err := soapRequest(s.serviceURL, s.serviceURN, "AddPortMapping", body)
if err != nil && timeout > 0 {
// Try to repair error code 725 - OnlyPermanentLeasesSupported
envelope := &soapErrorResponse{}
err = xml.Unmarshal(response, envelope)
if err != nil {
return err
}
if envelope.ErrorCode == 725 {
return s.AddPortMapping(localIPAddress, protocol, externalPort, internalPort, description, 0)
}
}
return nil
return err
}
// DeletePortMapping deletes a port mapping from the specified IGD service.