|
@@ -80,8 +80,39 @@ func (t *trie) insert(ipAddr string, cc string) {
|
|
|
current.ends = true
|
|
|
}
|
|
|
|
|
|
+func isReservedIP(ip string) bool {
|
|
|
+ parsedIP := net.ParseIP(ip)
|
|
|
+ if parsedIP == nil {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ // Check if the IP address is a loopback address
|
|
|
+ if parsedIP.IsLoopback() {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ // Check if the IP address is in the link-local address range
|
|
|
+ if parsedIP.IsLinkLocalUnicast() || parsedIP.IsLinkLocalMulticast() {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ // Check if the IP address is in the private address ranges
|
|
|
+ privateRanges := []*net.IPNet{
|
|
|
+ {IP: net.ParseIP("10.0.0.0"), Mask: net.CIDRMask(8, 32)},
|
|
|
+ {IP: net.ParseIP("172.16.0.0"), Mask: net.CIDRMask(12, 32)},
|
|
|
+ {IP: net.ParseIP("192.168.0.0"), Mask: net.CIDRMask(16, 32)},
|
|
|
+ }
|
|
|
+ for _, r := range privateRanges {
|
|
|
+ if r.Contains(parsedIP) {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // If the IP address is not a reserved address, return false
|
|
|
+ return false
|
|
|
+}
|
|
|
+
|
|
|
// Initializing the search for word in node
|
|
|
func (t *trie) search(ipAddr string) string {
|
|
|
+ if isReservedIP(ipAddr) {
|
|
|
+ return ""
|
|
|
+ }
|
|
|
word := ipToBitString(ipAddr)
|
|
|
current := t.root
|
|
|
for _, wr := range word {
|
|
@@ -91,5 +122,10 @@ func (t *trie) search(ipAddr string) string {
|
|
|
}
|
|
|
current = current.childrens[index]
|
|
|
}
|
|
|
- return current.cc
|
|
|
+ if current.ends {
|
|
|
+ return current.cc
|
|
|
+ }
|
|
|
+
|
|
|
+ //Not found
|
|
|
+ return ""
|
|
|
}
|