1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Restructured port range loader

And renamed `GetPortRange` to `PortRange`.

Signed-off-by: Michal Minar <miminar@redhat.com>
This commit is contained in:
Michal Minar 2015-03-10 09:56:42 +01:00
parent fcf8e85a35
commit 0dcc970432
2 changed files with 19 additions and 18 deletions

View file

@ -71,23 +71,25 @@ func NewErrPortAlreadyAllocated(ip string, port int) ErrPortAlreadyAllocated {
func init() {
const param = "/proc/sys/net/ipv4/ip_local_port_range"
if line, err := ioutil.ReadFile(param); err != nil {
line, err := ioutil.ReadFile(param)
if err != nil {
log.Errorf("Failed to read %s kernel parameter: %s", param, err.Error())
} else {
var start, end int
if n, err := fmt.Fscanf(strings.NewReader(string(line)), "%d\t%d", &start, &end); n != 2 || err != nil {
if err == nil {
err = fmt.Errorf("unexpected count of parsed numbers (%d)", n)
}
log.Errorf("Failed to parse port range from %s: %v", param, err)
} else {
beginPortRange = start
endPortRange = end
}
return
}
var start, end int
n, err := fmt.Fscanf(strings.NewReader(string(line)), "%d\t%d", &start, &end)
if n != 2 || err != nil {
if err == nil {
err = fmt.Errorf("unexpected count of parsed numbers (%d)", n)
}
log.Errorf("Failed to parse port range from %s: %v", param, err)
return
}
beginPortRange = start
endPortRange = end
}
func GetPortRange() (int, int) {
func PortRange() (int, int) {
return beginPortRange, endPortRange
}
@ -169,11 +171,10 @@ func ReleaseAll() error {
func (pm *portMap) findPort() (int, error) {
port := pm.last
start, end := GetPortRange()
for i := 0; i <= end-start; i++ {
for i := 0; i <= endPortRange-beginPortRange; i++ {
port++
if port > end {
port = start
if port > endPortRange {
port = beginPortRange
}
if _, ok := pm.p[port]; !ok {

View file

@ -129,7 +129,7 @@ func TestMapAllPortsSingleInterface(t *testing.T) {
}()
for i := 0; i < 10; i++ {
start, end := portallocator.GetPortRange()
start, end := portallocator.PortRange()
for i := start; i < end; i++ {
if host, err = Map(srcAddr1, dstIp1, 0); err != nil {
t.Fatal(err)