diff --git a/src/lib.rs b/src/lib.rs index d8e2098..9613ca9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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(reader: &mut R) -> Result { let mut size_buf = [0u8; 1]; @@ -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 { let len = self.compute_size(); @@ -397,6 +425,7 @@ impl Bucket { 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() @@ -409,6 +438,7 @@ impl Bucket { .expect("time went backwards"), ); record.failed_attempts = 0; + record.services = new_flags; } }