1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00
moby--moby/libnetwork/portallocator/portallocator_freebsd.go
Tomoya Tabuchi 047f7c0793 Add support for FreeBSD in portallocator
Signed-off-by: Tomoya Tabuchi <t@tomoyat1.com>
2017-05-15 11:37:29 +09:00

42 lines
1.7 KiB
Go

package portallocator
import (
"bytes"
"fmt"
"os/exec"
)
func getDynamicPortRange() (start int, end int, err error) {
portRangeKernelSysctl := []string{"net.inet.ip.portrange.hifirst", "net.ip.portrange.hilast"}
portRangeFallback := fmt.Sprintf("using fallback port range %d-%d", DefaultPortRangeStart, DefaultPortRangeEnd)
portRangeLowCmd := exec.Command("/sbin/sysctl", portRangeKernelSysctl[0])
var portRangeLowOut bytes.Buffer
portRangeLowCmd.Stdout = &portRangeLowOut
cmdErr := portRangeLowCmd.Run()
if cmdErr != nil {
return 0, 0, fmt.Errorf("port allocator - sysctl net.inet.ip.portrange.hifirst failed - %s: %v", portRangeFallback, err)
}
n, err := fmt.Sscanf(portRangeLowOut.String(), "%d", &start)
if n != 1 || err != nil {
if err == nil {
err = fmt.Errorf("unexpected count of parsed numbers (%d)", n)
}
return 0, 0, fmt.Errorf("port allocator - failed to parse system ephemeral port range start from %s - %s: %v", portRangeLowOut.String(), portRangeFallback, err)
}
portRangeHighCmd := exec.Command("/sbin/sysctl", portRangeKernelSysctl[1])
var portRangeHighOut bytes.Buffer
portRangeHighCmd.Stdout = &portRangeHighOut
cmdErr = portRangeHighCmd.Run()
if cmdErr != nil {
return 0, 0, fmt.Errorf("port allocator - sysctl net.inet.ip.portrange.hilast failed - %s: %v", portRangeFallback, err)
}
n, err = fmt.Sscanf(portRangeHighOut.String(), "%d", &end)
if n != 1 || err != nil {
if err == nil {
err = fmt.Errorf("unexpected count of parsed numbers (%d)", n)
}
return 0, 0, fmt.Errorf("port allocator - failed to parse system ephemeral port range end from %s - %s: %v", portRangeHighOut.String(), portRangeFallback, err)
}
return start, end, nil
}