-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipv4range.go
42 lines (36 loc) · 943 Bytes
/
ipv4range.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
package ipsearch
// IPv4Range represents an IPv4 CIDR range.
type IPv4Range struct {
firstSeg uint8
start, end uint32
cidr string
}
// NewIPv4Range creates a new IPv4 CIDR range
func NewIPv4Range(cidr string) *IPv4Range {
start, end := IPv4CIDRRange(cidr)
return &IPv4Range{
firstSeg: GetIPv4Segment(cidr, 1),
start: start,
end: end,
cidr: cidr,
}
}
// NewIPv4RangeSlice creates a new slice of IPv4Range.
func NewIPv4RangeSlice(lines []string) []*IPv4Range {
ranges := make([]*IPv4Range, len(lines))
for i, line := range lines {
ranges[i] = NewIPv4Range(line)
}
return ranges
}
func (ip *IPv4Range) String() string {
return ip.cidr
}
// Range returns the range of the IPv4 address in string format
func (ip *IPv4Range) Range() string {
return IPv4IntToStr(ip.start) + " - " + IPv4IntToStr(ip.end)
}
// CIDR returns the CIDR of the IP range
func (ip *IPv4Range) CIDR() string {
return ip.cidr
}