util.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. "net/url"
  17. "os"
  18. "reflect"
  19. "syscall"
  20. )
  21. // IPNetEqual checks if the two input IPNets are representing the same subnet.
  22. // For example,
  23. // 10.0.0.1/24 and 10.0.0.0/24 are the same subnet.
  24. // 10.0.0.1/24 and 10.0.0.0/25 are not the same subnet.
  25. func IPNetEqual(ipnet1, ipnet2 *net.IPNet) bool {
  26. if ipnet1 == nil || ipnet2 == nil {
  27. return false
  28. }
  29. if reflect.DeepEqual(ipnet1.Mask, ipnet2.Mask) && ipnet1.Contains(ipnet2.IP) && ipnet2.Contains(ipnet1.IP) {
  30. return true
  31. }
  32. return false
  33. }
  34. // Returns if the given err is "connection reset by peer" error.
  35. func IsConnectionReset(err error) bool {
  36. if urlErr, ok := err.(*url.Error); ok {
  37. err = urlErr.Err
  38. }
  39. if opErr, ok := err.(*net.OpError); ok {
  40. err = opErr.Err
  41. }
  42. if osErr, ok := err.(*os.SyscallError); ok {
  43. err = osErr.Err
  44. }
  45. if errno, ok := err.(syscall.Errno); ok && errno == syscall.ECONNRESET {
  46. return true
  47. }
  48. return false
  49. }