Skip to content

Commit

Permalink
Fixed wrong value returned by CIDRToIPv6 function
Browse files Browse the repository at this point in the history
  • Loading branch information
ip2location committed Nov 14, 2023
1 parent 18b7aca commit 081dace
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 22 deletions.
2 changes: 1 addition & 1 deletion ip2location.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ var district_position = [27]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
var asn_position = [27]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24}
var as_position = [27]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25}

const api_version string = "9.6.0"
const api_version string = "9.6.1"

var max_ipv4_range = uint128.From64(4294967295)
var max_ipv6_range = uint128.From64(0)
Expand Down
54 changes: 33 additions & 21 deletions iptools.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,31 +448,43 @@ func (t *IPTools) CIDRToIPv6(CIDR string) ([]string, error) {
return nil, errors.New("Not a valid CIDR.")
}

hexstartaddress, _ := t.ExpandIPv6(ip)
hexstartaddress = strings.ReplaceAll(hexstartaddress, ":", "")
hexendaddress := hexstartaddress

bits := 128 - prefix
pos := 31

for bits > 0 {
values := []int{4, bits}
min, _ := t.minMax(values)
x, _ := strconv.ParseInt(string(hexendaddress[pos]), 16, 64)
y := fmt.Sprintf("%x", (x | int64(math.Pow(2, float64(min))-1)))
expand, _ := t.ExpandIPv6(ip)
parts := strings.Split(expand, ":")

bitStart := strings.Repeat("1", prefix) + strings.Repeat("0", 128-prefix)
bitEnd := strings.Repeat("0", prefix) + strings.Repeat("1", 128-prefix)

n := 16 // split string into 16-char parts
floors := []string{}
for i := 0; i < len(bitStart); i += n {
end := i + n
if end > len(bitStart) {
end = len(bitStart)
}
floors = append(floors, bitStart[i:end])
}
ceilings := []string{}
for i := 0; i < len(bitEnd); i += n {
end := i + n
if end > len(bitEnd) {
end = len(bitEnd)
}
ceilings = append(ceilings, bitEnd[i:end])
}

hexendaddress = hexendaddress[:pos] + y + hexendaddress[pos+1:]
start := []string{}
end := []string{}

bits = bits - 4
pos = pos - 1
for i := 0; i < 8; i += 1 {
p, _ := strconv.ParseUint(parts[i], 16, 64)
f, _ := strconv.ParseUint(floors[i], 2, 64)
c, _ := strconv.ParseUint(ceilings[i], 2, 64)
start = append(start, strconv.FormatUint(p&f, 16))
end = append(end, strconv.FormatUint(p|c, 16))
}

re2 := regexp.MustCompile(`(.{4})`)
hexstartaddress = re2.ReplaceAllString(hexstartaddress, "$1:")
hexstartaddress = strings.TrimSuffix(hexstartaddress, ":")
hexendaddress = re2.ReplaceAllString(hexendaddress, "$1:")
hexendaddress = strings.TrimSuffix(hexendaddress, ":")

hexstartaddress, _ := t.ExpandIPv6(strings.Join(start, ":"))
hexendaddress, _ := t.ExpandIPv6(strings.Join(end, ":"))
result := []string{hexstartaddress, hexendaddress}

return result, nil
Expand Down

0 comments on commit 081dace

Please sign in to comment.