|
@@ -1,6 +1,7 @@
|
|
|
package geodb
|
|
|
|
|
|
import (
|
|
|
+ "math"
|
|
|
"net"
|
|
|
)
|
|
|
|
|
@@ -14,7 +15,7 @@ type trie struct {
|
|
|
root *trie_Node
|
|
|
}
|
|
|
|
|
|
-func ipToInt64(ip string) int64 {
|
|
|
+func ipToBytes(ip string) []byte {
|
|
|
// Parse the IP address string into a net.IP object
|
|
|
parsedIP := net.ParseIP(ip)
|
|
|
|
|
@@ -25,15 +26,7 @@ func ipToInt64(ip string) int64 {
|
|
|
ipBytes = parsedIP.To16()
|
|
|
}
|
|
|
|
|
|
- // Convert each byte in the IP address to its 8-bit binary representation
|
|
|
- var ipInt64 int64
|
|
|
- for _, b := range ipBytes {
|
|
|
- ipInt64 <<= 8
|
|
|
- ipInt64 |= int64(b)
|
|
|
- }
|
|
|
-
|
|
|
- // Join the binary representation of each byte with dots to form the final bit string
|
|
|
- return ipInt64
|
|
|
+ return ipBytes
|
|
|
}
|
|
|
|
|
|
// inititlaizing a new trie
|
|
@@ -45,18 +38,39 @@ func newTrie() *trie {
|
|
|
|
|
|
// Passing words to trie
|
|
|
func (t *trie) insert(ipAddr string, cc string) {
|
|
|
- ipInt64 := ipToInt64(ipAddr)
|
|
|
+ ipBytes := ipToBytes(ipAddr)
|
|
|
current := t.root
|
|
|
- for i := 63; i >= 0; i-- {
|
|
|
- bit := (ipInt64 >> uint(i)) & 1
|
|
|
- if current.childrens[bit] == nil {
|
|
|
- current.childrens[bit] = &trie_Node{
|
|
|
- childrens: [2]*trie_Node{},
|
|
|
- cc: cc,
|
|
|
+ for _, b := range ipBytes {
|
|
|
+ //For each byte in the ip address
|
|
|
+ //each byte is 8 bit
|
|
|
+ for j := 0; j < 8; j++ {
|
|
|
+ bitwise := (b&uint8(math.Pow(float64(2), float64(j))) > 0)
|
|
|
+ bit := 0b0000
|
|
|
+ if bitwise {
|
|
|
+ bit = 0b0001
|
|
|
}
|
|
|
+ if current.childrens[bit] == nil {
|
|
|
+ current.childrens[bit] = &trie_Node{
|
|
|
+ childrens: [2]*trie_Node{},
|
|
|
+ cc: cc,
|
|
|
+ }
|
|
|
+ }
|
|
|
+ current = current.childrens[bit]
|
|
|
}
|
|
|
- current = current.childrens[bit]
|
|
|
}
|
|
|
+
|
|
|
+ /*
|
|
|
+ for i := 63; i >= 0; i-- {
|
|
|
+ bit := (ipInt64 >> uint(i)) & 1
|
|
|
+ if current.childrens[bit] == nil {
|
|
|
+ current.childrens[bit] = &trie_Node{
|
|
|
+ childrens: [2]*trie_Node{},
|
|
|
+ cc: cc,
|
|
|
+ }
|
|
|
+ }
|
|
|
+ current = current.childrens[bit]
|
|
|
+ }
|
|
|
+ */
|
|
|
}
|
|
|
|
|
|
func isReservedIP(ip string) bool {
|
|
@@ -87,15 +101,32 @@ func (t *trie) search(ipAddr string) string {
|
|
|
return ""
|
|
|
}
|
|
|
|
|
|
- ipInt64 := ipToInt64(ipAddr)
|
|
|
+ ipBytes := ipToBytes(ipAddr)
|
|
|
current := t.root
|
|
|
- for i := 63; i >= 0; i-- {
|
|
|
- bit := (ipInt64 >> uint(i)) & 1
|
|
|
- if current.childrens[bit] == nil {
|
|
|
- return current.cc
|
|
|
+ for _, b := range ipBytes {
|
|
|
+ //For each byte in the ip address
|
|
|
+ //each byte is 8 bit
|
|
|
+ for j := 0; j < 8; j++ {
|
|
|
+ bitwise := (b&uint8(math.Pow(float64(2), float64(j))) > 0)
|
|
|
+ bit := 0b0000
|
|
|
+ if bitwise {
|
|
|
+ bit = 0b0001
|
|
|
+ }
|
|
|
+ if current.childrens[bit] == nil {
|
|
|
+ return current.cc
|
|
|
+ }
|
|
|
+ current = current.childrens[bit]
|
|
|
}
|
|
|
- current = current.childrens[bit]
|
|
|
}
|
|
|
+ /*
|
|
|
+ for i := 63; i >= 0; i-- {
|
|
|
+ bit := (ipInt64 >> uint(i)) & 1
|
|
|
+ if current.childrens[bit] == nil {
|
|
|
+ return current.cc
|
|
|
+ }
|
|
|
+ current = current.childrens[bit]
|
|
|
+ }
|
|
|
+ */
|
|
|
if len(current.childrens) == 0 {
|
|
|
return current.cc
|
|
|
}
|