lib/{nat,pmp}: Fix shadowing and nil IPs

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/2979
This commit is contained in:
Audrius Butkevicius 2016-04-16 16:48:07 +00:00 committed by Jakob Borg
parent fa1cfd94d0
commit e61f424ade
2 changed files with 6 additions and 2 deletions

View File

@ -254,6 +254,7 @@ func (s *Service) acquireNewMappings(mapping *Mapping, nats map[string]Device) (
// the given external port. If external port is 0, picks a pseudo-random port.
func (s *Service) tryNATDevice(natd Device, intPort, extPort int, leaseTime time.Duration) (Address, error) {
var err error
var port int
// Generate a predictable random which is based on device ID + local port
// number so that the ports we'd try to acquire for the mapping would always
@ -263,7 +264,7 @@ func (s *Service) tryNATDevice(natd Device, intPort, extPort int, leaseTime time
if extPort != 0 {
// First try renewing our existing mapping, if we have one.
name := fmt.Sprintf("syncthing-%d", extPort)
port, err := natd.AddPortMapping(TCP, intPort, extPort, name, leaseTime)
port, err = natd.AddPortMapping(TCP, intPort, extPort, name, leaseTime)
if err == nil {
extPort = port
goto findIP
@ -275,7 +276,7 @@ func (s *Service) tryNATDevice(natd Device, intPort, extPort int, leaseTime time
// Then try up to ten random ports.
extPort = 1024 + predictableRand.Intn(65535-1024)
name := fmt.Sprintf("syncthing-%d", extPort)
port, err := natd.AddPortMapping(TCP, intPort, extPort, name, leaseTime)
port, err = natd.AddPortMapping(TCP, intPort, extPort, name, leaseTime)
if err == nil {
extPort = port
goto findIP

View File

@ -27,6 +27,9 @@ func Discover(renewal, timeout time.Duration) []nat.Device {
l.Debugln("Failed to discover gateway", err)
return nil
}
if ip == nil || ip.IsUnspecified() {
return nil
}
l.Debugln("Discovered gateway at", ip)