blob: 5950087e022f7d3f964ada08b8999c0a7cc868d2 [file] [log] [blame]
Matteo Scandoloa4285862020-12-01 18:10:10 -08001/*
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 "errors"
21 "net"
22 "reflect"
23 "syscall"
24)
25
26// IPNetEqual checks if the two input IPNets are representing the same subnet.
27// For example,
28// 10.0.0.1/24 and 10.0.0.0/24 are the same subnet.
29// 10.0.0.1/24 and 10.0.0.0/25 are not the same subnet.
30func IPNetEqual(ipnet1, ipnet2 *net.IPNet) bool {
31 if ipnet1 == nil || ipnet2 == nil {
32 return false
33 }
34 if reflect.DeepEqual(ipnet1.Mask, ipnet2.Mask) && ipnet1.Contains(ipnet2.IP) && ipnet2.Contains(ipnet1.IP) {
35 return true
36 }
37 return false
38}
39
40// Returns if the given err is "connection reset by peer" error.
41func IsConnectionReset(err error) bool {
42 var errno syscall.Errno
43 if errors.As(err, &errno) {
44 return errno == syscall.ECONNRESET
45 }
46 return false
47}
48
49// Returns if the given err is "connection refused" error
50func IsConnectionRefused(err error) bool {
51 var errno syscall.Errno
52 if errors.As(err, &errno) {
53 return errno == syscall.ECONNREFUSED
54 }
55 return false
56}