port_range.go 2.8 KB

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