Browse code

Merge pull request #7100 from discordianfish/fix-port-allocation

Fix masked err in portmapper

unclejack authored on 2014/07/31 05:51:21
Showing 1 changed files
... ...
@@ -37,24 +37,16 @@ func SetIptablesChain(c *iptables.Chain) {
37 37
 	chain = c
38 38
 }
39 39
 
40
-func Map(container net.Addr, hostIP net.IP, hostPort int) (net.Addr, error) {
40
+func Map(container net.Addr, hostIP net.IP, hostPort int) (host net.Addr, err error) {
41 41
 	lock.Lock()
42 42
 	defer lock.Unlock()
43 43
 
44 44
 	var (
45 45
 		m                 *mapping
46
-		err               error
47 46
 		proto             string
48 47
 		allocatedHostPort int
49 48
 	)
50 49
 
51
-	// release the port on any error during return.
52
-	defer func() {
53
-		if err != nil {
54
-			portallocator.ReleasePort(hostIP, proto, allocatedHostPort)
55
-		}
56
-	}()
57
-
58 50
 	switch container.(type) {
59 51
 	case *net.TCPAddr:
60 52
 		proto = "tcp"
... ...
@@ -77,14 +69,19 @@ func Map(container net.Addr, hostIP net.IP, hostPort int) (net.Addr, error) {
77 77
 			container: container,
78 78
 		}
79 79
 	default:
80
-		err = ErrUnknownBackendAddressType
81
-		return nil, err
80
+		return nil, ErrUnknownBackendAddressType
82 81
 	}
83 82
 
83
+	// release the allocated port on any further error during return.
84
+	defer func() {
85
+		if err != nil {
86
+			portallocator.ReleasePort(hostIP, proto, allocatedHostPort)
87
+		}
88
+	}()
89
+
84 90
 	key := getKey(m.host)
85 91
 	if _, exists := currentMappings[key]; exists {
86
-		err = ErrPortMappedForIP
87
-		return nil, err
92
+		return nil, ErrPortMappedForIP
88 93
 	}
89 94
 
90 95
 	containerIP, containerPort := getIPAndPort(m.container)