Skip to content

Commit 9582e8d

Browse files
committed
Fix wrong value returned by cidr_to_ipv6 function
1 parent 6f808a3 commit 9582e8d

File tree

2 files changed

+5
-21
lines changed

2 files changed

+5
-21
lines changed

IP2Location/iptools.py

+4-20
Original file line numberDiff line numberDiff line change
@@ -187,31 +187,15 @@ def ipv6_to_cidr(self, from_ip, to_ip):
187187
def cidr_to_ipv4(self, cidr):
188188
if '/' not in cidr:
189189
return
190-
net=ipaddress.ip_network(u(cidr))
190+
net = ipaddress.ip_network(u(cidr))
191191
return({"ip_start": str(net[0]), "ip_end": str(net[-1])})
192192

193193
def cidr_to_ipv6(self, cidr):
194194
if '/' not in cidr:
195195
return
196-
parts = cidr.split('/')
197-
hexstartaddress = binascii.hexlify(socket.inet_pton(socket.AF_INET6, parts[0]))
198-
if (len(hexstartaddress) < 16):
199-
hexstartaddress = hexstartaddress.zfill(16-hexstartaddress.len())
200-
bits = 128 - int(parts[1])
201-
hexlastaddress = hexstartaddress
202-
pos = 31
203-
while (bits > 0):
204-
int_value = int(hexlastaddress[pos:pos+1], 16)
205-
# new = binascii.hexlify((int_value | (pow(2, min(4, bits)) - 1)).to_bytes(1, 'big'))
206-
new = binascii.hexlify((int_value | (pow(2, min(4, bits)) - 1)).to_bytes(1, 'little'))[1:]
207-
hexlastaddresslist = list(u(hexlastaddress))
208-
hexlastaddresslist[pos] = u(new)
209-
string_hexlastaddresslist = [str(int) for int in hexlastaddresslist]
210-
hexlastaddress = ''.join(string_hexlastaddresslist)
211-
bits = bits - 4
212-
pos = pos - 1
213-
binlastaddress = binascii.unhexlify(b(hexlastaddress))
214-
return({"ip_start": self.expand_ipv6(parts[0]), "ip_end": self.expand_ipv6(socket.inet_ntop(socket.AF_INET6, binlastaddress))})
196+
net = ipaddress.ip_network(cidr, False)
197+
# return({"ip_start": net.network_address.exploded, "ip_end": net.broadcast_address.exploded})
198+
return({"ip_start": str(net[0]), "ip_end": str(net[-1])})
215199

216200
def compressed_ipv6(self, ip):
217201
if self.is_ipv6(ip) is False:

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setuptools.setup(
77
name="IP2Location",
8-
version="8.10.0",
8+
version="8.10.1",
99
author="IP2Location",
1010
author_email="[email protected]",
1111
description="This is an IP geolocation library that enables the user to find the country, region, city, latitude and longitude, ZIP code, time zone, ISP, domain name, area code, weather info, mobile info, elevation, usage type, address type and IAB category from an IP address. It supports both IPv4 and IPv6 lookup.",

0 commit comments

Comments
 (0)