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
Родитель fcf8e85a35
Коммит 0dcc970432
2 изменённых файлов: 19 добавлений и 18 удалений

Просмотреть файл

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

Просмотреть файл

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