Browse Source

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

GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/2979
Audrius Butkevicius 9 years ago
parent
commit
e61f424ade
2 changed files with 6 additions and 2 deletions
  1. 3 2
      lib/nat/service.go
  2. 3 0
      lib/pmp/pmp.go

+ 3 - 2
lib/nat/service.go

@@ -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

+ 3 - 0
lib/pmp/pmp.go

@@ -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)