ipnet.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. Copyright 2016 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. "net"
  16. "strings"
  17. )
  18. // IPNetSet maps string to net.IPNet.
  19. type IPNetSet map[string]*net.IPNet
  20. // ParseIPNets parses string slice to IPNetSet.
  21. func ParseIPNets(specs ...string) (IPNetSet, error) {
  22. ipnetset := make(IPNetSet)
  23. for _, spec := range specs {
  24. spec = strings.TrimSpace(spec)
  25. _, ipnet, err := net.ParseCIDR(spec)
  26. if err != nil {
  27. return nil, err
  28. }
  29. k := ipnet.String() // In case of normalization
  30. ipnetset[k] = ipnet
  31. }
  32. return ipnetset, nil
  33. }
  34. // Insert adds items to the set.
  35. func (s IPNetSet) Insert(items ...*net.IPNet) {
  36. for _, item := range items {
  37. s[item.String()] = item
  38. }
  39. }
  40. // Delete removes all items from the set.
  41. func (s IPNetSet) Delete(items ...*net.IPNet) {
  42. for _, item := range items {
  43. delete(s, item.String())
  44. }
  45. }
  46. // Has returns true if and only if item is contained in the set.
  47. func (s IPNetSet) Has(item *net.IPNet) bool {
  48. _, contained := s[item.String()]
  49. return contained
  50. }
  51. // HasAll returns true if and only if all items are contained in the set.
  52. func (s IPNetSet) HasAll(items ...*net.IPNet) bool {
  53. for _, item := range items {
  54. if !s.Has(item) {
  55. return false
  56. }
  57. }
  58. return true
  59. }
  60. // Difference returns a set of objects that are not in s2
  61. // For example:
  62. // s1 = {a1, a2, a3}
  63. // s2 = {a1, a2, a4, a5}
  64. // s1.Difference(s2) = {a3}
  65. // s2.Difference(s1) = {a4, a5}
  66. func (s IPNetSet) Difference(s2 IPNetSet) IPNetSet {
  67. result := make(IPNetSet)
  68. for k, i := range s {
  69. _, found := s2[k]
  70. if found {
  71. continue
  72. }
  73. result[k] = i
  74. }
  75. return result
  76. }
  77. // StringSlice returns a []string with the String representation of each element in the set.
  78. // Order is undefined.
  79. func (s IPNetSet) StringSlice() []string {
  80. a := make([]string, 0, len(s))
  81. for k := range s {
  82. a = append(a, k)
  83. }
  84. return a
  85. }
  86. // IsSuperset returns true if and only if s1 is a superset of s2.
  87. func (s IPNetSet) IsSuperset(s2 IPNetSet) bool {
  88. for k := range s2 {
  89. _, found := s[k]
  90. if !found {
  91. return false
  92. }
  93. }
  94. return true
  95. }
  96. // Equal returns true if and only if s1 is equal (as a set) to s2.
  97. // Two sets are equal if their membership is identical.
  98. // (In practice, this means same elements, order doesn't matter)
  99. func (s IPNetSet) Equal(s2 IPNetSet) bool {
  100. return len(s) == len(s2) && s.IsSuperset(s2)
  101. }
  102. // Len returns the size of the set.
  103. func (s IPNetSet) Len() int {
  104. return len(s)
  105. }