Browse code

Port number 49153(BeginPortRange) would be returned twice, causing duplication and potential errors.

If we first request port 49153 (BeginPortRange) explicitly, and later some time request the next free port (of same ip/proto) by calling RequestPort() with port number 0, we will again get 49153 returned, even if it's currently in use. Because findPort() blindly retured BeginPortRange the first run, without checking if it has already been taken.

Signed-off-by: shuai-z <zs.broccoli@gmail.com>

shuai-z authored on 2014/10/21 13:24:01
Showing 1 changed files
... ...
@@ -15,6 +15,7 @@ type portMap struct {
15 15
 func newPortMap() *portMap {
16 16
 	return &portMap{
17 17
 		p: map[int]struct{}{},
18
+		last: EndPortRange,
18 19
 	}
19 20
 }
20 21
 
... ...
@@ -135,12 +136,6 @@ func ReleaseAll() error {
135 135
 }
136 136
 
137 137
 func (pm *portMap) findPort() (int, error) {
138
-	if pm.last == 0 {
139
-		pm.p[BeginPortRange] = struct{}{}
140
-		pm.last = BeginPortRange
141
-		return BeginPortRange, nil
142
-	}
143
-
144 138
 	for port := pm.last + 1; port != pm.last; port++ {
145 139
 		if port > EndPortRange {
146 140
 			port = BeginPortRange