Skip to content

Commit

Permalink
chore: prefer NIR over RIR
Browse files Browse the repository at this point in the history
  • Loading branch information
kiraum committed May 12, 2024
1 parent ab995b3 commit 45e5d81
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
30 changes: 21 additions & 9 deletions irrexplorer/api/collectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
6 changes: 6 additions & 0 deletions irrexplorer/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 45e5d81

Please sign in to comment.