-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipv4range_maplist.go
58 lines (50 loc) · 1.58 KB
/
ipv4range_maplist.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
package ipsearch
// IPv4RangeMapList is a map of lists of IPv4 CIDR ranges
type IPv4RangeMapList map[uint8]*IPv4RangeList
// NewIPv4RangeMapList creates a new map of lists of IPv4 CIDR ranges
func NewIPv4RangeMapList() IPv4RangeMapList {
m := make(IPv4RangeMapList)
return m
}
// AppendBatch adds a list of IPv4 CIDR ranges to the map of lists of IPv4 CIDR ranges
func (m IPv4RangeMapList) AppendBatch(ipRanges []*IPv4Range) {
for _, ip := range ipRanges {
m.Append(ip)
}
}
// Append adds an IPv4 CIDR range to the map of lists of IPv4 CIDR ranges
func (m IPv4RangeMapList) Append(ipRange *IPv4Range) {
ip1 := ipRange.firstSeg
if _, ok := m[ip1]; !ok {
m[ip1] = &IPv4RangeList{}
}
(*m[ip1]).Append(ipRange)
}
// Sort sorts the map of lists of IPv4 CIDR ranges
func (m IPv4RangeMapList) Sort() {
for _, lst := range m {
lst.Sort()
}
}
// InsertSorted inserts a IPv4 CIDR range to the map of lists of IPv4 CIDR ranges, keeping the lists sorted
func (m IPv4RangeMapList) InsertSorted(ipRange *IPv4Range) {
ip1 := ipRange.firstSeg
if _, ok := m[ip1]; !ok {
m[ip1] = &IPv4RangeList{}
}
(*m[ip1]).InsertSorted(ipRange)
}
// InsertSortedCIDRs inserts a list of IPv4 CIDR ranges to the map of IPv4 CIDR ranges lists, keeping the lists sorted
func (m IPv4RangeMapList) InsertSortedCIDRs(ipRanges []*IPv4Range) {
for _, ip := range ipRanges {
m.InsertSorted(ip)
}
}
// Search searches if an IPv4 address is in the map of lists
func (m IPv4RangeMapList) Search(ipStr string) *IPv4Range {
ip1 := GetIPv4Segment(ipStr, 1)
if _, ok := m[ip1]; !ok {
return nil
}
return (*m[ip1]).Search(ipStr)
}