blob: 8344d10c83ae9b0924889d262639d8047df510d9 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
2Copyright 2016 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package net
18
19import (
20 "net"
21 "net/url"
22 "os"
23 "reflect"
24 "syscall"
25)
26
27// IPNetEqual checks if the two input IPNets are representing the same subnet.
28// For example,
29// 10.0.0.1/24 and 10.0.0.0/24 are the same subnet.
30// 10.0.0.1/24 and 10.0.0.0/25 are not the same subnet.
31func IPNetEqual(ipnet1, ipnet2 *net.IPNet) bool {
32 if ipnet1 == nil || ipnet2 == nil {
33 return false
34 }
35 if reflect.DeepEqual(ipnet1.Mask, ipnet2.Mask) && ipnet1.Contains(ipnet2.IP) && ipnet2.Contains(ipnet1.IP) {
36 return true
37 }
38 return false
39}
40
41// Returns if the given err is "connection reset by peer" error.
42func IsConnectionReset(err error) bool {
43 if urlErr, ok := err.(*url.Error); ok {
44 err = urlErr.Err
45 }
46 if opErr, ok := err.(*net.OpError); ok {
47 err = opErr.Err
48 }
49 if osErr, ok := err.(*os.SyscallError); ok {
50 err = osErr.Err
51 }
52 if errno, ok := err.(syscall.Errno); ok && errno == syscall.ECONNRESET {
53 return true
54 }
55 return false
56}