#![deny(missing_docs)]
#![deny(unsafe_code)]
use std::fmt;
use ipnet::{IpNet, Ipv4Net, Ipv6Net};
use prefix_trie::rkyv::ArchivedPrefixMap;
use prefix_trie::{AsView, PrefixMap, TrieView};
use rkyv::rancor::Error as RkyvError;
pub use ipnet;
pub use prefix_trie;
pub use rkyv;
use rkyv::util::AlignedVec;
use std::io::Write;
pub mod header;
pub mod journal;
pub mod profile;
pub use header::{
Entry, Header, StreamRef, ENTRY_LEN, FLAG_STREAM, FLAG_WINDOW, HEADER_LEN, ID_V4, ID_V6,
KIND_PAYLOAD, KIND_V4, KIND_V6,
};
pub type HighSer<'a> =
rkyv::api::high::HighSerializer<AlignedVec, rkyv::ser::allocator::ArenaHandle<'a>, RkyvError>;
#[doc = include_str!("../docs/bgpkit-snap-developer-guide.md")]
mod guide {}
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Io(std::io::Error),
BadMagic,
BadVersion(u16),
BadHeader(&'static str),
BadHeaderCrc,
Truncated {
need: usize,
have: usize,
},
BadSectionTable(&'static str),
SectionOrder,
UnknownRequiredSection(u16),
BadSectionKind(u16),
TypeMismatch {
expected: String,
found: String,
},
ContentId,
Locked,
BadOpts(&'static str),
BadSectionId(u16),
BadPrefix(String),
BadArchive(u16),
ConfigMismatch(String),
BadValue,
BadRecord(&'static str),
BaseLost(String),
SequenceGap {
expected: u64,
found: u64,
},
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Io(e) => write!(f, "io: {e}"),
Error::BadMagic => write!(f, "not a snap object"),
Error::BadVersion(v) => write!(f, "unsupported format version {v}"),
Error::BadHeader(why) => write!(f, "bad header: {why}"),
Error::BadHeaderCrc => write!(f, "header crc mismatch"),
Error::Truncated { need, have } => write!(f, "truncated: need {need}, have {have}"),
Error::BadSectionTable(why) => write!(f, "bad section table: {why}"),
Error::SectionOrder => write!(f, "sections are not in ascending id order"),
Error::UnknownRequiredSection(id) => write!(
f,
"section {id} is required but this reader does not know it; \
open the object with a validator that implements it"
),
Error::BadSectionKind(id) => write!(f, "section {id} has an unexpected kind tag"),
Error::TypeMismatch { expected, found } => {
write!(
f,
"value type mismatch: expected {expected}, found {found:?}"
)
}
Error::ContentId => write!(f, "content id mismatch"),
Error::Locked => write!(f, "another writer holds this journal"),
Error::BadOpts(what) => write!(f, "bad writer option: {what}"),
Error::BadSectionId(id) => write!(f, "section id {id} cannot be used here"),
Error::BadPrefix(p) => write!(f, "prefix {p} has host bits set"),
Error::BadArchive(id) => {
write!(f, "section {id} is not an archive of this value type")
}
Error::ConfigMismatch(why) => write!(f, "{why}"),
Error::BadValue => write!(f, "value could not be decoded"),
Error::BadRecord(what) => write!(f, "journal record: {what}"),
Error::BaseLost(what) => write!(f, "journal base: {what}"),
Error::SequenceGap { expected, found } => {
write!(
f,
"journal sequence gap: expected {expected}, found {found}"
)
}
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Error::Io(e)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Count {
NoEntries,
All,
Exact(u128),
}
impl Count {
pub fn exact(self) -> Option<u128> {
match self {
Count::NoEntries => Some(0),
Count::Exact(n) => Some(n),
Count::All => None,
}
}
}
impl std::fmt::Display for Count {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Count::NoEntries => f.write_str("0"),
Count::Exact(n) => write!(f, "{n}"),
Count::All => f.write_str("all"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AddressCounts {
pub v4: Count,
pub v6: Count,
}
#[cfg(feature = "derive")]
pub use bgpkit_snap_derive::snap_value;
#[cfg(feature = "derive")]
pub use bgpkit_snap_derive::SnapValue;
#[diagnostic::on_unimplemented(
message = "`{Self}` cannot be stored in a snapshot",
label = "not a value type",
note = "a value type needs `Clone`, rkyv's `Archive` and `Serialize`, and `SnapValue` for its id:",
note = "#[derive(Clone, rkyv::Archive, rkyv::Serialize, rkyv::Deserialize, bgpkit_snap::SnapValue)]",
note = "#[snap(type_id = \"your.type:v1\")]"
)]
pub trait SnapValue: rkyv::Archive + Clone + for<'a> rkyv::Serialize<HighSer<'a>> {
const TYPE_ID: &'static str;
fn canonicalize(&mut self) {}
}
impl SnapValue for u8 {
const TYPE_ID: &str = "snap.u8:v1";
}
impl SnapValue for u16 {
const TYPE_ID: &str = "snap.u16:v1";
}
impl SnapValue for u32 {
const TYPE_ID: &str = "snap.u32:v1";
}
impl SnapValue for u64 {
const TYPE_ID: &str = "snap.u64:v1";
}
impl SnapValue for u128 {
const TYPE_ID: &str = "snap.u128:v1";
}
pub trait Codec: SnapValue + Sized {
fn encode(value: &Self) -> Result<Vec<u8>, Error>;
fn decode(bytes: &[u8]) -> Result<Self, Error>;
}
impl<T> Codec for T
where
T: SnapValue,
T::Archived: for<'a> rkyv::bytecheck::CheckBytes<rkyv::api::high::HighValidator<'a, RkyvError>>
+ rkyv::Deserialize<T, rkyv::api::high::HighDeserializer<RkyvError>>,
{
fn encode(value: &Self) -> Result<Vec<u8>, Error> {
rkyv::to_bytes::<RkyvError>(value)
.map(|b| b.to_vec())
.map_err(|_| Error::BadValue)
}
fn decode(bytes: &[u8]) -> Result<Self, Error> {
rkyv::from_bytes::<T, RkyvError>(bytes).map_err(|_| Error::BadValue)
}
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
pub struct TreeId([u8; 16]);
impl TreeId {
pub const fn from_bytes(bytes: [u8; 16]) -> Self {
Self(bytes)
}
pub fn from_name(name: &str) -> Self {
let mut out = [0u8; 16];
out.copy_from_slice(&blake3::hash(name.as_bytes()).as_bytes()[..16]);
Self(out)
}
pub const fn as_bytes(&self) -> &[u8; 16] {
&self.0
}
}
impl From<[u8; 16]> for TreeId {
fn from(bytes: [u8; 16]) -> Self {
Self(bytes)
}
}
impl std::fmt::Display for TreeId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for b in self.0 {
write!(f, "{b:02x}")?;
}
Ok(())
}
}
#[derive(Clone, Copy)]
pub struct ExtraSection<'a> {
pub id: u16,
pub kind: [u8; 4],
pub required: bool,
pub bytes: &'a [u8],
}
pub struct Opts<'a> {
pub created_at: i64,
pub stream: Option<StreamRef>,
pub window: Option<(i64, i64)>,
pub extras: &'a [ExtraSection<'a>],
}
impl Opts<'_> {
pub fn now() -> Self {
Self {
created_at: chrono::Utc::now().timestamp(),
stream: None,
window: None,
extras: &[],
}
}
}
pub struct Writer<T: SnapValue> {
v4: PrefixMap<Ipv4Net, T>,
v6: PrefixMap<Ipv6Net, T>,
}
impl<T: SnapValue> Default for Writer<T> {
fn default() -> Self {
Self::new()
}
}
impl<T: SnapValue> Writer<T> {
pub fn new() -> Self {
Self {
v4: PrefixMap::new(),
v6: PrefixMap::new(),
}
}
pub fn insert(&mut self, prefix: IpNet, mut value: T) -> Result<(), Error> {
if prefix.addr() != prefix.network() {
return Err(Error::BadPrefix(prefix.to_string()));
}
value.canonicalize();
match prefix {
IpNet::V4(p) => {
self.v4.insert(p, value);
}
IpNet::V6(p) => {
self.v6.insert(p, value);
}
}
Ok(())
}
pub fn insert_many(
&mut self,
entries: impl IntoIterator<Item = (IpNet, T)>,
) -> Result<(), Error> {
for (prefix, value) in entries {
self.insert(prefix, value)?;
}
Ok(())
}
pub fn len(&self) -> usize {
self.v4.len() + self.v6.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn finish(self, opts: &Opts<'_>) -> Result<Vec<u8>, Error> {
let created_at = opts.created_at;
let v4_bytes = rkyv::to_bytes::<RkyvError>(&self.v4).map_err(|_| Error::BadValue)?;
let v6_bytes = rkyv::to_bytes::<RkyvError>(&self.v6).map_err(|_| Error::BadValue)?;
let mut sections: Vec<(Entry, &[u8])> = Vec::new();
sections.push((
Entry {
id: ID_V4,
kind: KIND_V4,
required: false,
len: v4_bytes.len() as u64,
},
&v4_bytes,
));
sections.push((
Entry {
id: ID_V6,
kind: KIND_V6,
required: false,
len: v6_bytes.len() as u64,
},
&v6_bytes,
));
let counts = header::Counts {
entries: self.len() as u64,
v4: self.v4.len() as u64,
v6: self.v6.len() as u64,
};
let counts_bytes = counts.encode();
sections.push((
Entry {
id: header::ID_STATS,
kind: header::KIND_STATS,
required: false,
len: header::STATS_LEN as u64,
},
&counts_bytes,
));
let mut extras: Vec<&ExtraSection<'_>> = opts.extras.iter().collect();
extras.sort_by_key(|e| e.id);
for extra in extras {
if extra.id < header::FIRST_PROFILE_ID {
return Err(Error::BadHeader("extra section id is below 16"));
}
if extra.kind == KIND_V4 || extra.kind == KIND_V6 {
return Err(Error::BadHeader("extra section uses a family kind"));
}
if sections.iter().any(|(e, _)| e.id == extra.id) {
return Err(Error::BadHeader("duplicate section id"));
}
sections.push((
Entry {
id: extra.id,
kind: extra.kind,
required: extra.required,
len: extra.bytes.len() as u64,
},
extra.bytes,
));
}
sections.sort_by_key(|(e, _)| e.id);
let table_len = sections.len() * ENTRY_LEN;
let mut body: Vec<u8> = Vec::new();
let mut table: Vec<u8> = Vec::with_capacity(table_len);
for (entry, bytes) in §ions {
while !(HEADER_LEN + table_len + body.len()).is_multiple_of(16) {
body.push(0);
}
table.extend_from_slice(&entry.encode());
body.extend_from_slice(bytes);
}
let file_len = (HEADER_LEN + table_len + body.len()) as u64;
let mut type_id = [0u8; 32];
let id_bytes = T::TYPE_ID.as_bytes();
if id_bytes.is_empty() {
return Err(Error::BadHeader("type id is empty"));
}
if id_bytes.len() > 31 {
return Err(Error::BadHeader("type id is longer than 31 bytes"));
}
type_id[..id_bytes.len()].copy_from_slice(id_bytes);
let (flags, tree_id, generation, base_seq) = match opts.stream {
Some(s) => (FLAG_STREAM, s.tree_id, s.generation, s.base_seq),
None => (0, [0u8; 16], [0u8; 16], 0),
};
let (flags, window) = match opts.window {
Some((from, to)) => (flags | FLAG_WINDOW, [from, to]),
None => (flags, [0, 0]),
};
let mut head = Header {
flags,
tree_id,
generation,
base_seq,
type_id,
file_len,
section_count: sections.len() as u32,
section_table_len: table_len as u32,
window,
created_at,
content_id: [0u8; 32],
};
let mut prefix = head.encode();
let mut h = blake3::Hasher::new();
h.update(b"bgpkit-snap-v4\0");
h.update(&prefix[..header::HASH_END]);
h.update(&table);
for (_, bytes) in §ions {
h.update(bytes);
}
head.content_id = *h.finalize().as_bytes();
let mut out = Vec::with_capacity(file_len as usize);
prefix = head.encode();
Header::parse(&prefix)?;
out.extend_from_slice(&prefix);
out.extend_from_slice(&table);
out.extend_from_slice(&body);
debug_assert_eq!(out.len() as u64, file_len);
Ok(out)
}
pub fn finish_to(
self,
path: impl AsRef<std::path::Path>,
opts: &Opts<'_>,
) -> Result<u64, Error> {
let bytes = self.finish(opts)?;
let path = path.as_ref();
static NEXT_TEMP: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
let n = NEXT_TEMP.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
let tmp = path.with_extension(format!("snap.{}.{n}.tmp", std::process::id()));
let written = (|| -> std::io::Result<()> {
let mut f = std::fs::File::create(&tmp)?;
f.write_all(&bytes)?;
f.sync_all()
})();
if let Err(e) = written {
let _ = std::fs::remove_file(&tmp);
return Err(Error::Io(e));
}
if let Err(e) = std::fs::rename(&tmp, path) {
let _ = std::fs::remove_file(&tmp);
return Err(Error::Io(e));
}
if let Some(dir) = path.parent() {
let _ = std::fs::File::open(dir).and_then(|d| d.sync_all());
}
Ok(bytes.len() as u64)
}
}
pub fn recorded_counts(raw: &[u8]) -> Result<Option<header::Counts>, Error> {
let header = Header::parse(raw)?;
header.check_layout(raw.len())?;
let table_end = HEADER_LEN + header.section_table_len as usize;
let sections = parse_sections(raw, &header, table_end)?;
Ok(sections
.iter()
.find(|s| s.entry.id == header::ID_STATS)
.and_then(|s| header::Counts::parse(&raw[s.offset..s.offset + s.len])))
}
fn parse_sections(raw: &[u8], header: &Header, table_end: usize) -> Result<Vec<SectionRef>, Error> {
let table = &raw[HEADER_LEN..table_end];
let mut sections: Vec<SectionRef> = Vec::new();
let mut cursor = table_end;
let mut prev_id = 0u16;
for i in 0..header.section_count as usize {
let entry = Entry::parse(&table[i * ENTRY_LEN..(i + 1) * ENTRY_LEN])?;
if entry.id <= prev_id && i > 0 {
return Err(Error::SectionOrder);
}
while !cursor.is_multiple_of(16) {
let Some(byte) = raw.get(cursor) else {
return Err(Error::Truncated {
need: cursor + 1,
have: raw.len(),
});
};
if *byte != 0 {
return Err(Error::BadSectionTable("padding is not zero"));
}
cursor += 1;
}
let len = entry.len as usize;
let Some(end) = cursor.checked_add(len) else {
return Err(Error::BadSectionTable("section length overflows"));
};
if end > raw.len() {
return Err(Error::Truncated {
need: end,
have: raw.len(),
});
}
sections.push(SectionRef {
entry,
offset: cursor,
len,
});
prev_id = entry.id;
cursor = end;
}
if cursor != raw.len() {
return Err(Error::BadSectionTable("sections do not end the file"));
}
for SectionRef { entry, len, .. } in §ions {
if entry.id == header::ID_STATS && *len != header::STATS_LEN {
return Err(Error::BadSectionTable(
"count section is not the fixed 32 bytes",
));
}
}
for SectionRef { entry, .. } in §ions {
match entry.id {
ID_V4 if entry.kind != KIND_V4 => return Err(Error::BadSectionKind(entry.id)),
ID_V6 if entry.kind != KIND_V6 => return Err(Error::BadSectionKind(entry.id)),
header::ID_STATS if entry.kind != header::KIND_STATS => {
return Err(Error::BadSectionKind(entry.id))
}
_ => {}
}
match entry.kind {
KIND_V4 if entry.id != ID_V4 => return Err(Error::BadSectionKind(entry.id)),
KIND_V6 if entry.id != ID_V6 => return Err(Error::BadSectionKind(entry.id)),
header::KIND_STATS if entry.id != header::ID_STATS => {
return Err(Error::BadSectionKind(entry.id))
}
_ => {}
}
}
Ok(sections)
}
impl<T: SnapValue> Snapshot<T> {
pub fn merge_last_wins(inputs: &[&Snapshot<T>], opts: &Opts<'_>) -> Result<Vec<u8>, Error>
where
T::Archived: rkyv::Deserialize<T, rkyv::api::high::HighDeserializer<RkyvError>>,
{
Self::merge_with(inputs, opts, |_, _, later| Some(later))
}
pub fn merge_with(
inputs: &[&Snapshot<T>],
opts: &Opts<'_>,
mut combine: impl FnMut(IpNet, T, T) -> Option<T>,
) -> Result<Vec<u8>, Error>
where
T::Archived: rkyv::Deserialize<T, rkyv::api::high::HighDeserializer<RkyvError>>,
{
if !opts.extras.is_empty() {
return Err(Error::BadOpts(
"a merge cannot carry a caller section: merge refuses inputs that hold \
sections of their own, so there is nothing to keep it consistent with",
));
}
for snap in inputs {
for entry in snap.entries() {
if entry.id >= crate::header::FIRST_PROFILE_ID {
return Err(Error::BadOpts(
"a merge cannot take an object that carries its own section: the \
values may point into it, and the merged object would not have it",
));
}
}
}
let mut merged: std::collections::HashMap<IpNet, T> = std::collections::HashMap::new();
for snap in inputs {
for (prefix, value) in snap.iter() {
let value =
rkyv::deserialize::<T, RkyvError>(value).map_err(|_| Error::BadValue)?;
match merged.remove(&prefix) {
None => {
merged.insert(prefix, value);
}
Some(earlier) => {
if let Some(next) = combine(prefix, earlier, value) {
merged.insert(prefix, next);
}
}
}
}
}
let window = match opts.window {
Some(w) => Some(w),
None => Self::covers_one_span(inputs),
};
let mut writer = Writer::<T>::new();
writer.insert_many(merged)?;
let opts = Opts {
created_at: opts.created_at,
stream: None,
window,
extras: &[],
};
writer.finish(&opts)
}
fn covers_one_span(inputs: &[&Snapshot<T>]) -> Option<(i64, i64)> {
if inputs.is_empty() {
return None;
}
let mut spans = Vec::with_capacity(inputs.len());
for snap in inputs {
spans.push(snap.header().window()?);
}
spans.sort_unstable();
let (mut from, mut to) = spans[0];
for (a, b) in spans.into_iter().skip(1) {
if a > to {
return None;
}
to = to.max(b);
from = from.min(a);
}
Some((from, to))
}
}
pub struct Owned<'a, T: SnapValue> {
snap: &'a Snapshot<T>,
}
impl<T: SnapValue> Snapshot<T> {
pub fn owned(&self) -> Owned<'_, T> {
Owned { snap: self }
}
}
impl<T: SnapValue> Owned<'_, T>
where
T::Archived: rkyv::Deserialize<T, rkyv::api::high::HighDeserializer<RkyvError>>,
{
fn decode(value: &T::Archived) -> Result<T, Error> {
rkyv::deserialize::<T, RkyvError>(value).map_err(|_| Error::BadValue)
}
pub fn len(&self) -> usize {
self.snap.len()
}
pub fn is_empty(&self) -> bool {
self.snap.is_empty()
}
pub fn get_exact(&self, prefix: IpNet) -> Result<Option<T>, Error> {
match self.snap.get_exact(prefix) {
Some(value) => Ok(Some(Self::decode(value)?)),
None => Ok(None),
}
}
pub fn search(&self, addr: std::net::IpAddr) -> Result<Option<(IpNet, T)>, Error> {
match self.snap.search(addr) {
Some((prefix, value)) => Ok(Some((prefix, Self::decode(value)?))),
None => Ok(None),
}
}
pub fn lpm(&self, prefix: IpNet) -> Result<Option<(IpNet, T)>, Error> {
match self.snap.lpm(prefix) {
Some((prefix, value)) => Ok(Some((prefix, Self::decode(value)?))),
None => Ok(None),
}
}
pub fn spm(&self, prefix: IpNet) -> Result<Option<(IpNet, T)>, Error> {
match self.snap.spm(prefix) {
Some((prefix, value)) => Ok(Some((prefix, Self::decode(value)?))),
None => Ok(None),
}
}
pub fn cover(&self, prefix: IpNet) -> Result<Vec<(IpNet, T)>, Error> {
self.snap
.cover(prefix)
.into_iter()
.map(|(p, v)| Ok((p, Self::decode(v)?)))
.collect()
}
pub fn children(&self, prefix: IpNet) -> Result<Vec<(IpNet, T)>, Error> {
self.snap
.children(prefix)
.into_iter()
.map(|(p, v)| Ok((p, Self::decode(v)?)))
.collect()
}
pub fn iter(&self) -> Result<Vec<(IpNet, T)>, Error> {
self.snap
.iter()
.map(|(p, v)| Ok((p, Self::decode(v)?)))
.collect()
}
pub fn values(&self) -> Result<Vec<T>, Error> {
self.snap.values().map(|v| Self::decode(v)).collect()
}
pub fn iter_from(&self, prefix: IpNet, inclusive: bool) -> Result<Vec<(IpNet, T)>, Error> {
self.snap
.iter_from(prefix, inclusive)
.map(|(p, v)| Ok((p, Self::decode(v)?)))
.collect()
}
}
#[derive(Debug, Clone, Copy)]
pub struct SectionRef {
pub entry: Entry,
pub offset: usize,
pub len: usize,
}
pub struct SnapshotView<'a> {
pub header: &'a Header,
pub sections: &'a [SectionRef],
bytes: &'a [u8],
table: &'a [u8],
}
impl SnapshotView<'_> {
pub fn section_bytes(&self, id: u16) -> Option<&[u8]> {
self.sections
.iter()
.find(|s| s.entry.id == id)
.map(|s| &self.bytes[s.offset..s.offset + s.len])
}
pub fn entry(&self, id: u16) -> Option<&Entry> {
self.sections
.iter()
.find(|s| s.entry.id == id)
.map(|s| &s.entry)
}
pub fn table(&self) -> &[u8] {
self.table
}
}
pub fn verify_bytes(raw: &[u8]) -> Result<Header, Error> {
let header = Header::parse(raw)?;
header.check_layout(raw.len())?;
let table_start = HEADER_LEN;
let table_end = table_start + header.section_table_len as usize;
let table = &raw[table_start..table_end];
let mut cursor = table_end;
let mut prev_id = 0u16;
for i in 0..header.section_count as usize {
let entry = Entry::parse(&table[i * ENTRY_LEN..(i + 1) * ENTRY_LEN])?;
if entry.id <= prev_id && i > 0 {
return Err(Error::SectionOrder);
}
match entry.id {
ID_V4 if entry.kind != KIND_V4 => return Err(Error::BadSectionKind(entry.id)),
ID_V6 if entry.kind != KIND_V6 => return Err(Error::BadSectionKind(entry.id)),
_ => {}
}
match entry.kind {
KIND_V4 if entry.id != ID_V4 => return Err(Error::BadSectionKind(entry.id)),
KIND_V6 if entry.id != ID_V6 => return Err(Error::BadSectionKind(entry.id)),
_ => {}
}
while !cursor.is_multiple_of(16) {
let Some(byte) = raw.get(cursor) else {
return Err(Error::Truncated {
need: cursor + 1,
have: raw.len(),
});
};
if *byte != 0 {
return Err(Error::BadSectionTable("padding is not zero"));
}
cursor += 1;
}
let Some(end) = cursor.checked_add(entry.len as usize) else {
return Err(Error::BadSectionTable("section length overflows"));
};
if end > raw.len() {
return Err(Error::Truncated {
need: end,
have: raw.len(),
});
}
cursor = end;
prev_id = entry.id;
}
if cursor != raw.len() {
return Err(Error::BadSectionTable("sections do not end the file"));
}
let mut h = blake3::Hasher::new();
h.update(b"bgpkit-snap-v4\0");
h.update(&raw[..header::HASH_END]);
h.update(table);
let mut cursor = table_end;
for i in 0..header.section_count as usize {
let entry = Entry::parse(&table[i * ENTRY_LEN..(i + 1) * ENTRY_LEN])?;
while !cursor.is_multiple_of(16) {
cursor += 1;
}
if cursor + entry.len as usize > raw.len() {
return Err(Error::Truncated {
need: cursor + entry.len as usize,
have: raw.len(),
});
}
h.update(&raw[cursor..cursor + entry.len as usize]);
cursor += entry.len as usize;
}
if h.finalize().as_bytes() != &header.content_id {
return Err(Error::ContentId);
}
Ok(header)
}
pub trait Validator {
fn knows(&self, _entry: &Entry) -> bool {
false
}
fn validate(&self, _view: &SnapshotView<'_>) -> Result<(), Error> {
Ok(())
}
}
struct NoValidator;
impl Validator for NoValidator {}
pub struct Snapshot<T: SnapValue> {
bytes: AlignedVec,
header: Header,
sections: Vec<SectionRef>,
table_range: (usize, usize),
_t: std::marker::PhantomData<T>,
}
impl<T: SnapValue> fmt::Debug for Snapshot<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Snapshot")
.field("content_id", &self.header.content_id_hex())
.field("sections", &self.sections.len())
.finish()
}
}
impl<T: SnapValue> Snapshot<T> {
pub fn open(path: impl AsRef<std::path::Path>) -> Result<Self, Error>
where
T::Archived:
for<'a> rkyv::bytecheck::CheckBytes<rkyv::api::high::HighValidator<'a, RkyvError>>,
{
Self::from_bytes(&std::fs::read(path.as_ref())?)
}
pub fn open_with(
path: impl AsRef<std::path::Path>,
validator: &dyn Validator,
) -> Result<Self, Error>
where
T::Archived:
for<'a> rkyv::bytecheck::CheckBytes<rkyv::api::high::HighValidator<'a, RkyvError>>,
{
Self::from_bytes_with(&std::fs::read(path.as_ref())?, validator)
}
pub fn from_bytes(raw: &[u8]) -> Result<Self, Error>
where
T::Archived:
for<'a> rkyv::bytecheck::CheckBytes<rkyv::api::high::HighValidator<'a, RkyvError>>,
{
Self::from_bytes_with(raw, &NoValidator)
}
pub fn from_bytes_with(raw: &[u8], validator: &dyn Validator) -> Result<Self, Error>
where
T::Archived:
for<'a> rkyv::bytecheck::CheckBytes<rkyv::api::high::HighValidator<'a, RkyvError>>,
{
let header = Header::parse(raw)?;
header.check_layout(raw.len())?;
let mut aligned = AlignedVec::with_capacity(raw.len());
aligned.extend_from_slice(raw);
let table_start = HEADER_LEN;
let table_end = table_start + header.section_table_len as usize;
let table = &aligned[table_start..table_end];
let sections = parse_sections(&aligned, &header, table_end)?;
let mut h = blake3::Hasher::new();
h.update(b"bgpkit-snap-v4\0");
h.update(&aligned[..header::HASH_END]);
h.update(table);
for SectionRef {
offset: off, len, ..
} in §ions
{
h.update(&aligned[*off..*off + *len]);
}
if *h.finalize().as_bytes() != header.content_id {
return Err(Error::ContentId);
}
let declared = header.type_id_str();
if declared != T::TYPE_ID {
return Err(Error::TypeMismatch {
expected: T::TYPE_ID.to_string(),
found: declared,
});
}
let snap = Snapshot {
bytes: aligned,
header,
sections,
table_range: (table_start, table_end),
_t: std::marker::PhantomData,
};
let view = snap.view();
for SectionRef { entry, .. } in &snap.sections {
let known = matches!(entry.id, ID_V4 | ID_V6 | header::ID_STATS);
if entry.required && !known && !validator.knows(entry) {
return Err(Error::UnknownRequiredSection(entry.id));
}
}
snap.validate_archives()?;
validator.validate(&view)?;
Ok(snap)
}
pub fn table_bytes(&self) -> &[u8] {
&self.bytes[self.table_range.0..self.table_range.1]
}
pub fn entries(&self) -> Vec<Entry> {
self.sections.iter().map(|s| s.entry).collect()
}
pub fn header(&self) -> &Header {
&self.header
}
pub fn content_id(&self) -> String {
self.header.content_id_hex()
}
pub fn section_bytes(&self, id: u16) -> Option<&[u8]> {
self.sections
.iter()
.find(|s| s.entry.id == id)
.map(|s| &self.bytes[s.offset..s.offset + s.len])
}
pub fn view(&self) -> SnapshotView<'_> {
SnapshotView {
header: &self.header,
sections: &self.sections,
bytes: &self.bytes,
table: &self.bytes[self.table_range.0..self.table_range.1],
}
}
fn validate_archives(&self) -> Result<(), Error>
where
T::Archived:
for<'a> rkyv::bytecheck::CheckBytes<rkyv::api::high::HighValidator<'a, RkyvError>>,
{
for SectionRef {
entry,
offset: off,
len,
} in &self.sections
{
let slice = &self.bytes[*off..*off + *len];
match entry.id {
ID_V4 => {
rkyv::access::<ArchivedPrefixMap<Ipv4Net, T>, RkyvError>(slice)
.map_err(|_| Error::BadArchive(ID_V4))?;
}
ID_V6 => {
rkyv::access::<ArchivedPrefixMap<Ipv6Net, T>, RkyvError>(slice)
.map_err(|_| Error::BadArchive(ID_V6))?;
}
_ => {}
}
}
Ok(())
}
#[allow(unsafe_code)]
pub fn v4(&self) -> Option<&ArchivedPrefixMap<Ipv4Net, T>> {
self.section_bytes(ID_V4)
.map(|s| unsafe { rkyv::access_unchecked::<ArchivedPrefixMap<Ipv4Net, T>>(s) })
}
#[allow(unsafe_code)]
pub fn v6(&self) -> Option<&ArchivedPrefixMap<Ipv6Net, T>> {
self.section_bytes(ID_V6)
.map(|s| unsafe { rkyv::access_unchecked::<ArchivedPrefixMap<Ipv6Net, T>>(s) })
}
pub fn len(&self) -> usize {
self.v4().map(|m| m.len()).unwrap_or(0) + self.v6().map(|m| m.len()).unwrap_or(0)
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn search(&self, addr: std::net::IpAddr) -> Option<(IpNet, &T::Archived)> {
match addr {
std::net::IpAddr::V4(a) => {
let p = Ipv4Net::new(a, 32).ok()?;
let map = self.v4()?;
let (lpm, value) = map.get_lpm(&p)?;
Some((IpNet::V4(lpm), value))
}
std::net::IpAddr::V6(a) => {
let p = Ipv6Net::new(a, 128).ok()?;
let map = self.v6()?;
let (lpm, value) = map.get_lpm(&p)?;
Some((IpNet::V6(lpm), value))
}
}
}
pub fn lpm(&self, prefix: IpNet) -> Option<(IpNet, &T::Archived)> {
match prefix {
IpNet::V4(p) => {
let map = self.v4()?;
let (lpm, value) = map.get_lpm(&p)?;
Some((IpNet::V4(lpm), value))
}
IpNet::V6(p) => {
let map = self.v6()?;
let (lpm, value) = map.get_lpm(&p)?;
Some((IpNet::V6(lpm), value))
}
}
}
pub fn get_exact(&self, prefix: IpNet) -> Option<&T::Archived> {
match prefix {
IpNet::V4(p) => self.v4().and_then(|m| m.get(&p)),
IpNet::V6(p) => self.v6().and_then(|m| m.get(&p)),
}
}
pub fn contains_key(&self, prefix: IpNet) -> bool {
match prefix {
IpNet::V4(p) => self.v4().is_some_and(|m| m.contains_key(&p)),
IpNet::V6(p) => self.v6().is_some_and(|m| m.contains_key(&p)),
}
}
pub fn spm(&self, prefix: IpNet) -> Option<(IpNet, &T::Archived)> {
match prefix {
IpNet::V4(p) => self
.v4()
.and_then(|m| m.get_spm(&p))
.map(|(k, v)| (IpNet::V4(k), v)),
IpNet::V6(p) => self
.v6()
.and_then(|m| m.get_spm(&p))
.map(|(k, v)| (IpNet::V6(k), v)),
}
}
pub fn is_covered(&self, prefix: IpNet) -> bool {
match prefix {
IpNet::V4(p) => self.v4().is_some_and(|m| m.is_covered(&p)),
IpNet::V6(p) => self.v6().is_some_and(|m| m.is_covered(&p)),
}
}
pub fn is_covered_in_aggregate(&self, prefix: IpNet) -> bool {
match prefix {
IpNet::V4(p) => self.v4().is_some_and(|m| m.is_covered_in_aggregate(&p)),
IpNet::V6(p) => self.v6().is_some_and(|m| m.is_covered_in_aggregate(&p)),
}
}
pub fn iter(&self) -> Box<dyn Iterator<Item = (IpNet, &T::Archived)> + '_> {
let v4 = self
.v4()
.into_iter()
.flat_map(|m| m.iter())
.map(|(p, v)| (IpNet::V4(p), v));
let v6 = self
.v6()
.into_iter()
.flat_map(|m| m.iter())
.map(|(p, v)| (IpNet::V6(p), v));
Box::new(v4.chain(v6))
}
pub fn keys(&self) -> Box<dyn Iterator<Item = IpNet> + '_> {
Box::new(self.iter().map(|(p, _)| p))
}
pub fn iter_from(
&self,
prefix: IpNet,
inclusive: bool,
) -> Box<dyn Iterator<Item = (IpNet, &T::Archived)> + '_> {
match prefix {
IpNet::V4(p) => {
let it = self
.v4()
.into_iter()
.flat_map(move |m| m.iter_from(&p, inclusive))
.map(|(k, v)| (IpNet::V4(k), v));
Box::new(it)
}
IpNet::V6(p) => {
let it = self
.v6()
.into_iter()
.flat_map(move |m| m.iter_from(&p, inclusive))
.map(|(k, v)| (IpNet::V6(k), v));
Box::new(it)
}
}
}
pub fn values(&self) -> Box<dyn Iterator<Item = &T::Archived> + '_> {
let v4 = self.v4().into_iter().flat_map(|m| m.values());
let v6 = self.v6().into_iter().flat_map(|m| m.values());
Box::new(v4.chain(v6))
}
pub fn address_counts(&self) -> AddressCounts {
let count = |m: Option<Option<u128>>| match m {
None => Count::NoEntries,
Some(None) => Count::All,
Some(Some(n)) => Count::Exact(n),
};
AddressCounts {
v4: count(self.v4().map(|m| m.address_count().map(u128::from))),
v6: count(self.v6().map(|m| m.address_count())),
}
}
pub fn cover(&self, prefix: IpNet) -> Vec<(IpNet, &T::Archived)> {
match prefix {
IpNet::V4(p) => self
.v4()
.map(|m| m.cover(&p).map(|(k, v)| (IpNet::V4(k), v)).collect())
.unwrap_or_default(),
IpNet::V6(p) => self
.v6()
.map(|m| m.cover(&p).map(|(k, v)| (IpNet::V6(k), v)).collect())
.unwrap_or_default(),
}
}
pub fn children(&self, prefix: IpNet) -> Vec<(IpNet, &T::Archived)> {
let mut out: Vec<(IpNet, &T::Archived)> = Vec::new();
match prefix {
IpNet::V4(p) => {
let p = p.trunc();
let Some(map) = self.v4() else { return out };
if let Some((k, v)) = map.get_key_value(&p) {
out.push((IpNet::V4(k), v));
}
if let Some(view) = map.view_at(&p) {
for (k, v) in view.iter() {
if k != p {
out.push((IpNet::V4(k), v));
}
}
}
}
IpNet::V6(p) => {
let p = p.trunc();
let Some(map) = self.v6() else { return out };
if let Some((k, v)) = map.get_key_value(&p) {
out.push((IpNet::V6(k), v));
}
if let Some(view) = map.view_at(&p) {
for (k, v) in view.iter() {
if k != p {
out.push((IpNet::V6(k), v));
}
}
}
}
}
out
}
}