ipnet.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. "fmt"
  16. "net"
  17. "strings"
  18. )
  19. // IPNetSet maps string to net.IPNet.
  20. type IPNetSet map[string]*net.IPNet
  21. // ParseIPNets parses string slice to IPNetSet.
  22. func ParseIPNets(specs ...string) (IPNetSet, error) {
  23. ipnetset := make(IPNetSet)
  24. for _, spec := range specs {
  25. spec = strings.TrimSpace(spec)
  26. _, ipnet, err := ParseCIDRSloppy(spec)
  27. if err != nil {
  28. return nil, err
  29. }
  30. k := ipnet.String() // In case of normalization
  31. ipnetset[k] = ipnet
  32. }
  33. return ipnetset, nil
  34. }
  35. // Insert adds items to the set.
  36. func (s IPNetSet) Insert(items ...*net.IPNet) {
  37. for _, item := range items {
  38. s[item.String()] = item
  39. }
  40. }
  41. // Delete removes all items from the set.
  42. func (s IPNetSet) Delete(items ...*net.IPNet) {
  43. for _, item := range items {
  44. delete(s, item.String())
  45. }
  46. }
  47. // Has returns true if and only if item is contained in the set.
  48. func (s IPNetSet) Has(item *net.IPNet) bool {
  49. _, contained := s[item.String()]
  50. return contained
  51. }
  52. // HasAll returns true if and only if all items are contained in the set.
  53. func (s IPNetSet) HasAll(items ...*net.IPNet) bool {
  54. for _, item := range items {
  55. if !s.Has(item) {
  56. return false
  57. }
  58. }
  59. return true
  60. }
  61. // Difference returns a set of objects that are not in s2
  62. // For example:
  63. // s1 = {a1, a2, a3}
  64. // s2 = {a1, a2, a4, a5}
  65. // s1.Difference(s2) = {a3}
  66. // s2.Difference(s1) = {a4, a5}
  67. func (s IPNetSet) Difference(s2 IPNetSet) IPNetSet {
  68. result := make(IPNetSet)
  69. for k, i := range s {
  70. _, found := s2[k]
  71. if found {
  72. continue
  73. }
  74. result[k] = i
  75. }
  76. return result
  77. }
  78. // StringSlice returns a []string with the String representation of each element in the set.
  79. // Order is undefined.
  80. func (s IPNetSet) StringSlice() []string {
  81. a := make([]string, 0, len(s))
  82. for k := range s {
  83. a = append(a, k)
  84. }
  85. return a
  86. }
  87. // IsSuperset returns true if and only if s1 is a superset of s2.
  88. func (s IPNetSet) IsSuperset(s2 IPNetSet) bool {
  89. for k := range s2 {
  90. _, found := s[k]
  91. if !found {
  92. return false
  93. }
  94. }
  95. return true
  96. }
  97. // Equal returns true if and only if s1 is equal (as a set) to s2.
  98. // Two sets are equal if their membership is identical.
  99. // (In practice, this means same elements, order doesn't matter)
  100. func (s IPNetSet) Equal(s2 IPNetSet) bool {
  101. return len(s) == len(s2) && s.IsSuperset(s2)
  102. }
  103. // Len returns the size of the set.
  104. func (s IPNetSet) Len() int {
  105. return len(s)
  106. }
  107. // IPSet maps string to net.IP
  108. type IPSet map[string]net.IP
  109. // ParseIPSet parses string slice to IPSet
  110. func ParseIPSet(items ...string) (IPSet, error) {
  111. ipset := make(IPSet)
  112. for _, item := range items {
  113. ip := ParseIPSloppy(strings.TrimSpace(item))
  114. if ip == nil {
  115. return nil, fmt.Errorf("error parsing IP %q", item)
  116. }
  117. ipset[ip.String()] = ip
  118. }
  119. return ipset, nil
  120. }
  121. // Insert adds items to the set.
  122. func (s IPSet) Insert(items ...net.IP) {
  123. for _, item := range items {
  124. s[item.String()] = item
  125. }
  126. }
  127. // Delete removes all items from the set.
  128. func (s IPSet) Delete(items ...net.IP) {
  129. for _, item := range items {
  130. delete(s, item.String())
  131. }
  132. }
  133. // Has returns true if and only if item is contained in the set.
  134. func (s IPSet) Has(item net.IP) bool {
  135. _, contained := s[item.String()]
  136. return contained
  137. }
  138. // HasAll returns true if and only if all items are contained in the set.
  139. func (s IPSet) HasAll(items ...net.IP) bool {
  140. for _, item := range items {
  141. if !s.Has(item) {
  142. return false
  143. }
  144. }
  145. return true
  146. }
  147. // Difference returns a set of objects that are not in s2
  148. // For example:
  149. // s1 = {a1, a2, a3}
  150. // s2 = {a1, a2, a4, a5}
  151. // s1.Difference(s2) = {a3}
  152. // s2.Difference(s1) = {a4, a5}
  153. func (s IPSet) Difference(s2 IPSet) IPSet {
  154. result := make(IPSet)
  155. for k, i := range s {
  156. _, found := s2[k]
  157. if found {
  158. continue
  159. }
  160. result[k] = i
  161. }
  162. return result
  163. }
  164. // StringSlice returns a []string with the String representation of each element in the set.
  165. // Order is undefined.
  166. func (s IPSet) StringSlice() []string {
  167. a := make([]string, 0, len(s))
  168. for k := range s {
  169. a = append(a, k)
  170. }
  171. return a
  172. }
  173. // IsSuperset returns true if and only if s1 is a superset of s2.
  174. func (s IPSet) IsSuperset(s2 IPSet) bool {
  175. for k := range s2 {
  176. _, found := s[k]
  177. if !found {
  178. return false
  179. }
  180. }
  181. return true
  182. }
  183. // Equal returns true if and only if s1 is equal (as a set) to s2.
  184. // Two sets are equal if their membership is identical.
  185. // (In practice, this means same elements, order doesn't matter)
  186. func (s IPSet) Equal(s2 IPSet) bool {
  187. return len(s) == len(s2) && s.IsSuperset(s2)
  188. }
  189. // Len returns the size of the set.
  190. func (s IPSet) Len() int {
  191. return len(s)
  192. }