pub struct Id { /* private fields */ }
Expand description
Represents the id of a Node or a BitTorrent info-hash. Basically, it’s a 20-byte identifier.
Implementations§
Source§impl Id
impl Id
Sourcepub fn from_bytes<T: AsRef<[u8]>>(bytes: T) -> Result<Id, RustyDHTError>
pub fn from_bytes<T: AsRef<[u8]>>(bytes: T) -> Result<Id, RustyDHTError>
Create a new Id from some bytes. Returns Err if bytes
is not of length
ID_SIZE.
Sourcepub fn from_ip(ip: &IpAddr) -> Id
pub fn from_ip(ip: &IpAddr) -> Id
Generates a random Id for a mainline DHT node with the provided IP address. The generated Id will be valid with respect to BEP0042.
Sourcepub fn from_random<T: RngCore>(rng: &mut T) -> Id
pub fn from_random<T: RngCore>(rng: &mut T) -> Id
Generates a completely random Id. The returned Id is not guaranteed to be valid with respect to BEP0042.
Sourcepub fn to_vec(&self) -> Vec<u8> ⓘ
pub fn to_vec(&self) -> Vec<u8> ⓘ
Copies the byes that make up the Id and returns them in a Vec
Sourcepub fn is_valid_for_ip(&self, ip: &IpAddr) -> bool
pub fn is_valid_for_ip(&self, ip: &IpAddr) -> bool
Evaluates the Id and decides if it’s a valid Id for a DHT node with the provided IP address (based on BEP0042). Note: the current implementation does not handle non-globally-routable address space properly. It will likely return false for non-routable IPv4 address space (against the spec).
Sourcepub fn matching_prefix_bits(&self, other: &Self) -> usize
pub fn matching_prefix_bits(&self, other: &Self) -> usize
Returns the number of bits of prefix that the two ids have in common.
Consider two Ids with binary values 10100000
and 10100100
. This function
would return 5
because the Ids share the common 5-bit prefix 10100
.
Sourcepub fn from_hex(h: &str) -> Result<Id, RustyDHTError>
pub fn from_hex(h: &str) -> Result<Id, RustyDHTError>
Creates an Id from a hex string.
For example: let id = Id::from_hex("88ffb73943354a00dc2dadd14c54d28020a513c8").unwrap();
Sourcepub fn xor(&self, other: &Id) -> Id
pub fn xor(&self, other: &Id) -> Id
Computes the exclusive or (XOR) of this Id with another. The BitTorrent DHT uses XOR as its distance metric.
Example: let distance_between_nodes = id.xor(other_id);
Sourcepub fn make_mutant(&self, identical_bytes: usize) -> Result<Id, RustyDHTError>
pub fn make_mutant(&self, identical_bytes: usize) -> Result<Id, RustyDHTError>
Makes a new id that’s similar to this one.
identical_bytes
specifies how many bytes of the resulting Id should be the same as this
.
identical_bytes
must be in the range (0, ID_SIZE) otherwise Err
is returned.