Skip to content
Merged
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
30 changes: 30 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,29 @@ impl Record {
}
}

pub fn new_from_addrv2_source(
addr: AddrV2,
port: u16,
services: ServiceFlags,
source: &AddrV2,
) -> Self {
let source = match source {
AddrV2::Ipv4(ip) => IpAddr::V4(*ip).source_id(),
AddrV2::Ipv6(ip) => IpAddr::V6(*ip).source_id(),
// All the mixnets go in the same source,
_ => SourceId([1u8; 8]),
};
Self {
addr,
port,
source,
services,
failed_attempts: 0,
last_connection: None,
last_attempt: None,
}
}

/// Build a new record from deserialization
pub fn deserialize<R: Read>(reader: &mut R) -> Result<Self, std::io::Error> {
let mut size_buf = [0u8; 1];
Expand Down Expand Up @@ -150,6 +173,11 @@ impl Record {
self.services
}

/// Update the most recent service flag information.
pub fn update_service_flags(&mut self, flags: ServiceFlags) {
self.services = flags;
}

/// Serialize a record into bytes.
pub fn serialize(self) -> Vec<u8> {
let len = self.compute_size();
Expand Down Expand Up @@ -397,6 +425,7 @@ impl<const S: usize> Bucket<S> {

fn successful_connection(&mut self, record: &Record) {
let slot = Self::derive_slot(record);
let new_flags = record.services;
if let Some(record) = &mut self.records[slot] {
record.last_attempt = Some(
SystemTime::now()
Expand All @@ -409,6 +438,7 @@ impl<const S: usize> Bucket<S> {
.expect("time went backwards"),
);
record.failed_attempts = 0;
record.services = new_flags;
}
}

Expand Down