github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/util/port_range.go (about)

     1  /*
     2  Copyright 2015 The Kubernetes Authors All rights reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package util
    18  
    19  import (
    20  	"fmt"
    21  	"strconv"
    22  	"strings"
    23  )
    24  
    25  // PortRange represents a range of TCP/UDP ports.  To represent a single port,
    26  // set Size to 1.
    27  type PortRange struct {
    28  	Base int
    29  	Size int
    30  }
    31  
    32  // Contains tests whether a given port falls within the PortRange.
    33  func (pr *PortRange) Contains(p int) bool {
    34  	return (p >= pr.Base) && ((p - pr.Base) < pr.Size)
    35  }
    36  
    37  // String converts the PortRange to a string representation, which can be
    38  // parsed by PortRange.Set or ParsePortRange.
    39  func (pr PortRange) String() string {
    40  	if pr.Size == 0 {
    41  		return ""
    42  	}
    43  	return fmt.Sprintf("%d-%d", pr.Base, pr.Base+pr.Size-1)
    44  }
    45  
    46  // Set parses a string of the form "min-max", inclusive at both ends, and
    47  // sets the PortRange from it.  This is part of the flag.Value and pflag.Value
    48  // interfaces.
    49  func (pr *PortRange) Set(value string) error {
    50  	value = strings.TrimSpace(value)
    51  
    52  	// TODO: Accept "80" syntax
    53  	// TODO: Accept "80+8" syntax
    54  
    55  	if value == "" {
    56  		pr.Base = 0
    57  		pr.Size = 0
    58  		return nil
    59  	}
    60  
    61  	hyphenIndex := strings.Index(value, "-")
    62  	if hyphenIndex == -1 {
    63  		return fmt.Errorf("expected hyphen in port range")
    64  	}
    65  
    66  	var err error
    67  	var low int
    68  	var high int
    69  	low, err = strconv.Atoi(value[:hyphenIndex])
    70  	if err == nil {
    71  		high, err = strconv.Atoi(value[hyphenIndex+1:])
    72  	}
    73  	if err != nil {
    74  		return fmt.Errorf("unable to parse port range: %s", value)
    75  	}
    76  
    77  	if high < low {
    78  		return fmt.Errorf("end port cannot be less than start port: %s", value)
    79  	}
    80  	pr.Base = low
    81  	pr.Size = 1 + high - low
    82  	return nil
    83  }
    84  
    85  // Type returns a descriptive string about this type.  This is part of the
    86  // pflag.Value interface.
    87  func (*PortRange) Type() string {
    88  	return "portRange"
    89  }
    90  
    91  // ParsePortRange parses a string of the form "min-max", inclusive at both
    92  // ends, and initializs a new PortRange from it.
    93  func ParsePortRange(value string) (*PortRange, error) {
    94  	pr := &PortRange{}
    95  	err := pr.Set(value)
    96  	if err != nil {
    97  		return nil, err
    98  	}
    99  	return pr, nil
   100  }