appliedsec/pygeoip

Add GeoIP.last_netmask property

palant opened this issue · 1 comments

MaxMind's Python API has a GeoIP.last_netmask property starting with version 1.2.4: https://github.com/maxmind/geoip-api-python/blob/master/ChangeLog. This allows determining which IP range a particular IP address belongs to and for example iterating over the entire IP space (start with 1.0.0.0, look up, increase IP address according to last_netmask, repeat). This attribute can be set in the _seek_country() method with a single additional line:

             for depth in range(seek_depth, -1, -1):
+                self.last_netmask = seek_depth - depth + 1
                 if self._flags & const.MEMORY_CACHE:

That's a hack of course, you would normally only set that attribute when returning and also declare that property somewhere - but that hack already works.

Thanks for the idea. I've implemented it as a function to mock the Maxmind Python and C API. Would be nice to add GeoIP.range_by_ip() now when we have GeoIP.last_netmask().