diff --git a/irrexplorer/api/collectors.py b/irrexplorer/api/collectors.py index b654e79..f074c38 100644 --- a/irrexplorer/api/collectors.py +++ b/irrexplorer/api/collectors.py @@ -18,7 +18,7 @@ from irrexplorer.backends.irrd import IRRDQuery from irrexplorer.backends.rirstats import RIRStatsQuery from irrexplorer.settings import MINIMUM_PREFIX_SIZE, TESTING -from irrexplorer.state import RIR, IPNetwork, RouteInfo +from irrexplorer.state import RIR, IPNetwork, RouteInfo, NIR SET_SIZE_LIMIT = 1000 @@ -149,15 +149,27 @@ def _collate_per_prefix(self) -> List[PrefixSummary]: def _rir_for_prefix(self, prefix: IPNetwork) -> Optional[RIR]: """ Find the responsible RIR for a prefix, from self.rirstats previously - gathered by _collect() + gathered by _collect(), and prefer NIR over RIR. """ - relevant_rirstats = ( - rirstat for rirstat in self.rirstats if rirstat.prefix.overlaps(prefix) # type: ignore - ) - try: - return next(relevant_rirstats).rir - except StopIteration: - return None + # This will hold the most relevant RIR statistic found + relevant_rirstat = None + # Flag to indicate if a NIR prefix has been found + NIR_PFX = False + + # Iterate through all RIR statistics to find the one that overlaps with the given prefix + for rirstat in self.rirstats: + if rirstat.prefix.overlaps(prefix): + # Check if the current RIR is a NIR + if any(nir.name == rirstat.rir.name for nir in NIR): + # Set the flag if it's an NIR + NIR_PFX = True + # Update the relevant RIR statistic + relevant_rirstat = rirstat + elif not NIR_PFX: + # If no NIR has been found yet, update the relevant RIR + relevant_rirstat = rirstat + # Return the RIR of the relevant statistic if found, otherwise return None + return relevant_rirstat.rir if relevant_rirstat else None async def collect_member_of(target: str) -> MemberOf: diff --git a/irrexplorer/state.py b/irrexplorer/state.py index 7a226ae..db2bcda 100644 --- a/irrexplorer/state.py +++ b/irrexplorer/state.py @@ -20,6 +20,12 @@ class RIR(enum.Enum): APNIC = "APNIC" REGISTROBR = "Registro.BR" +class NIR(enum.Enum): + """ + This enum classifies specific RIR entries that are considered NIRs. + Each entry in the enum represents a recognized NIR with its official designation. + """ + REGISTROBR = "Registro.BR" # Represents the Brazilian Internet Registry class RPKIStatus(enum.Enum): valid = "VALID"