]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
Process LACNIC geofeed as well
authorPeter Müller <peter.mueller@ipfire.org>
Sat, 11 Dec 2021 21:59:22 +0000 (22:59 +0100)
committerMichael Tremer <michael.tremer@ipfire.org>
Mon, 13 Dec 2021 17:46:19 +0000 (17:46 +0000)
This improves country code accurarcy for suballocations within IP space
managed by LACNIC, as the delegated-extended-latest file only provides
country code information at the top level of an allocated network.

Sadly, lacnic.db.gz does not contain descriptions or names of Autonomous
Systems within the space maintained by LACNIC.

Signed-off-by: Peter Müller <peter.mueller@ipfire.org>
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/python/importer.py
src/python/location-importer.in

index de340b49c138ddba3f824bed9e37026043fb5ad7..dee36ed9dfacb2c6403ec4d53d30cc122c784ec1 100644 (file)
@@ -53,7 +53,9 @@ WHOIS_SOURCES = {
                ],
 
        # Latin America and Caribbean Network Information Centre
-       # XXX ???
+       "LACNIC": [
+               "https://ftp.lacnic.net/lacnic/dbase/lacnic.db.gz"
+               ],
 
        # Réseaux IP Européens
        "RIPE": [
index b3e36582055c1d510dd94f7adb38665c49f13337..eff925326469891b244e72b6dd339355defdcfb3 100644 (file)
@@ -681,13 +681,42 @@ class CLI(object):
                                # Strip any excess space
                                start_address, end_address = start_address.rstrip(), end_address.strip()
 
-                               # Convert to IP address
-                               try:
-                                       start_address = ipaddress.ip_address(start_address)
-                                       end_address   = ipaddress.ip_address(end_address)
-                               except ValueError:
-                                       log.warning("Could not parse line: %s" % line)
-                                       return
+                               # Handle "inetnum" formatting in LACNIC DB (e.g. "24.152.8/22" instead of "24.152.8.0/22")
+                               if start_address and not (delim or end_address):
+                                       try:
+                                               start_address = ipaddress.ip_network(start_address, strict=False)
+                                       except ValueError:
+                                               start_address = start_address.split("/")
+                                               ldigits = len(start_address[0].split("."))
+
+                                               # How many octets do we need to add?
+                                               # (LACNIC does not seem to have a /8 or greater assigned, so the following should suffice.)
+                                               if ldigits == 2:
+                                                       start_address = start_address[0] + ".0.0/" + start_address[1]
+                                               elif ldigits == 3:
+                                                       start_address = start_address[0] + ".0/" + start_address[1]
+                                               else:
+                                                       log.warning("Could not recover IPv4 address from line in LACNIC DB format: %s" % line)
+                                                       return
+
+                                               try:
+                                                       start_address = ipaddress.ip_network(start_address, strict=False)
+                                               except ValueError:
+                                                       log.warning("Could not parse line in LACNIC DB format: %s" % line)
+                                                       return
+
+                                       # Enumerate first and last IP address of this network
+                                       end_address = start_address[-1]
+                                       start_address = start_address[0]
+
+                               else:
+                                       # Convert to IP address
+                                       try:
+                                               start_address = ipaddress.ip_address(start_address)
+                                               end_address   = ipaddress.ip_address(end_address)
+                                       except ValueError:
+                                               log.warning("Could not parse line: %s" % line)
+                                               return
 
                                inetnum["inetnum"] = list(ipaddress.summarize_address_range(start_address, end_address))