...

Source file src/github.com/linkerd/linkerd2/pkg/util/portrange.go

Documentation: github.com/linkerd/linkerd2/pkg/util

     1  package util
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  // PortRange defines the upper- and lower-bounds for a range of ports.
    10  type PortRange struct {
    11  	LowerBound int
    12  	UpperBound int
    13  }
    14  
    15  // ParsePort parses and verifies the validity of the port candidate.
    16  func ParsePort(port string) (int, error) {
    17  	i, err := strconv.Atoi(port)
    18  	if err != nil || !isPort(i) {
    19  		return -1, fmt.Errorf("\"%s\" is not a valid TCP port", port)
    20  	}
    21  	return i, nil
    22  }
    23  
    24  // ParsePortRange parses and checks the provided range candidate to ensure it is
    25  // a valid TCP port range.
    26  func ParsePortRange(portRange string) (PortRange, error) {
    27  	bounds := strings.Split(portRange, "-")
    28  	if len(bounds) > 2 {
    29  		return PortRange{}, fmt.Errorf("ranges expected as <lower>-<upper>")
    30  	}
    31  	if len(bounds) == 1 {
    32  		// If only provided a single value, treat as both lower- and upper-bounds
    33  		bounds = append(bounds, bounds[0])
    34  	}
    35  	lower, err := strconv.Atoi(bounds[0])
    36  	if err != nil || !isPort(lower) {
    37  		return PortRange{}, fmt.Errorf("\"%s\" is not a valid lower-bound", bounds[0])
    38  	}
    39  	upper, err := strconv.Atoi(bounds[1])
    40  	if err != nil || !isPort(upper) {
    41  		return PortRange{}, fmt.Errorf("\"%s\" is not a valid upper-bound", bounds[1])
    42  	}
    43  	if upper < lower {
    44  		return PortRange{}, fmt.Errorf("\"%s\": upper-bound must be greater than or equal to lower-bound", portRange)
    45  	}
    46  	return PortRange{LowerBound: lower, UpperBound: upper}, nil
    47  }
    48  
    49  // isPort checks the provided to determine whether or not the port
    50  // candidate is a valid TCP port number. Valid TCP ports range from 0 to 65535.
    51  func isPort(port int) bool {
    52  	return 0 <= port && port <= 65535
    53  }
    54  
    55  // Ports returns an array of all the ports contained by this range.
    56  func (pr PortRange) Ports() []uint16 {
    57  	var ports []uint16
    58  	for i := pr.LowerBound; i <= pr.UpperBound; i++ {
    59  		ports = append(ports, uint16(i))
    60  	}
    61  	return ports
    62  }
    63  
    64  func (pr PortRange) ToString() string {
    65  	if pr.LowerBound == pr.UpperBound {
    66  		return strconv.Itoa(pr.LowerBound)
    67  	}
    68  
    69  	return fmt.Sprintf("%d-%d", pr.LowerBound, pr.UpperBound)
    70  }
    71  

View as plain text