Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: prefer NIR over RIR #341

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 22 additions & 10 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 @@ -148,16 +148,28 @@ 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()
Find the responsible RIR/NIR for a prefix, from self.rirstats previously
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