-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipv4validators.go
80 lines (62 loc) · 1.89 KB
/
ipv4validators.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package networkvalidator
import (
"bytes"
"fmt"
"net"
)
func NewIpV4Data(ipaddress, netmask, ipgateway, ipsubnet, dnsprimary, dnssecondary string) *ipV4Data {
d := &ipV4Data{
IpAddress: net.ParseIP(ipaddress).To4(),
Netmask: net.IPMask(net.ParseIP(netmask).To4()),
IpGateway: net.ParseIP(ipgateway).To4(),
IpSubnet: net.ParseIP(ipsubnet).To4(),
Dnss: []net.IP{net.ParseIP(dnsprimary).To4(), net.ParseIP(dnssecondary).To4()},
}
o, _ := d.Netmask.Size()
_, in, _ := net.ParseCIDR(fmt.Sprintf("%s/%d", d.IpAddress, o))
d.Cidr = in
d.ipRange = getiprange(in, &d.Netmask)
return d
}
type ValidatorForIpv4 struct {
data *ipV4Data
}
func (v *ValidatorForIpv4) Isinrange() bool {
if bytes.Compare([]byte(v.data.IpAddress), []byte(v.data.ipRange.start)) < 0 {
return false
}
return bytes.Compare([]byte(v.data.IpAddress), []byte((v.data.ipRange.end))) <= 0
}
func (v *ValidatorForIpv4) OverlapsWith(ipnet *net.IPNet) bool {
return v.data.Cidr.Contains(ipnet.IP) || ipnet.Contains(v.data.Cidr.IP)
}
func (v *ValidatorForIpv4) SubnetIsCorrect() bool {
return v.data.Cidr.IP.Equal(net.ParseIP(v.data.IpSubnet.String()).To4())
}
func (v *ValidatorForIpv4) RangeNetworkIsCorrect() bool {
return v.data.ipRange != nil
}
func (v *ValidatorForIpv4) AddressIpIsLocalhost() bool {
return v.data.IpAddress.IsLoopback()
}
func (v *ValidatorForIpv4) AddressIpIsMulticast() bool {
return v.data.IpAddress.IsMulticast()
}
func (v *ValidatorForIpv4) AllIpAreCorrect() bool {
return v.data.IpAddress != nil &&
v.data.IpSubnet != nil &&
v.data.Netmask != nil &&
v.data.IpGateway != nil
}
func (v *ValidatorForIpv4) CIDRIsCorrect() bool {
return v.data.Cidr != nil
}
func (v *ValidatorForIpv4) IpGatewayIsinrange() bool {
return v.data.Cidr.Contains(v.data.IpGateway)
}
func NewValidatorForIpv4(ipv4data *ipV4Data) Validators {
v := &ValidatorForIpv4{
data: ipv4data,
}
return v
}