pub struct SignedPacket { /* private fields */ }Expand description
Signed DNS packet
Implementations§
Source§impl SignedPacket
impl SignedPacket
Sourcepub fn from_bytes(bytes: &Bytes) -> Result<SignedPacket>
pub fn from_bytes(bytes: &Bytes) -> Result<SignedPacket>
Creates a Self from the serialized representation:
<32 bytes public_key><64 bytes signature><8 bytes big-endian timestamp in microseconds><encoded DNS packet>
Performs the following validations:
- Bytes minimum length
- Validates the PublicKey
- Verifies the Signature
- Validates the DNS packet encoding
You can skip all these validations by using Self::from_bytes_unchecked instead.
You can use Self::from_relay_payload instead if you are receiving a response from an HTTP relay.
§Errors
- Returns crate::Error::InvalidSignedPacketBytesLength if
bytes.len()is smaller than 104 bytes - Returns crate::Error::PacketTooLarge if
bytes.len()is bigger than 1104 bytes - Returns crate::Error::InvalidEd25519PublicKey if the first 32 bytes are invalid
ed25519public key - Returns crate::Error::InvalidEd25519Signature if the following 64 bytes are invalid
ed25519signature - Returns crate::Error::DnsError if it failed to parse the DNS Packet after the first 104 bytes
Sourcepub fn from_bytes_unchecked(bytes: &Bytes, last_seen: u64) -> SignedPacket
pub fn from_bytes_unchecked(bytes: &Bytes, last_seen: u64) -> SignedPacket
Useful for cloning a SignedPacket, or cerating one from a previously checked bytes, like ones stored on disk or in a database.
Sourcepub fn from_relay_payload(
public_key: &PublicKey,
payload: &Bytes,
) -> Result<SignedPacket>
pub fn from_relay_payload( public_key: &PublicKey, payload: &Bytes, ) -> Result<SignedPacket>
Creates a SignedPacket from a PublicKey and the relays payload.
§Errors
- Returns crate::Error::InvalidSignedPacketBytesLength if
payloadis too small - Returns crate::Error::PacketTooLarge if the payload is too large.
- Returns crate::Error::InvalidEd25519Signature if the signature in the payload is invalid
- Returns crate::Error::DnsError if it failed to parse the DNS Packet
Sourcepub fn from_packet(
keypair: &Keypair,
packet: &Packet<'_>,
) -> Result<SignedPacket>
pub fn from_packet( keypair: &Keypair, packet: &Packet<'_>, ) -> Result<SignedPacket>
Creates a new SignedPacket from a Keypair and a DNS Packet.
It will also normalize the names of the ResourceRecords to be relative to the origin, which would be the z-base32 encoded PublicKey of the Keypair used to sign the Packet.
§Errors
- Returns crate::Error::DnsError if the packet is invalid or it failed to compress or encode it.
Sourcepub fn as_bytes(&self) -> &Bytes
pub fn as_bytes(&self) -> &Bytes
Returns the serialized signed packet:
<32 bytes public_key><64 bytes signature><8 bytes big-endian timestamp in microseconds><encoded DNS packet>
Sourcepub fn to_relay_payload(&self) -> Bytes
pub fn to_relay_payload(&self) -> Bytes
Returns a slice of the serialized SignedPacket omitting the leading public_key, to be sent as a request/response body to or from relays.
Sourcepub fn public_key(&self) -> PublicKey
pub fn public_key(&self) -> PublicKey
Returns the PublicKey of the signer of this SignedPacket
Sourcepub fn timestamp(&self) -> u64
pub fn timestamp(&self) -> u64
Returns the timestamp in microseconds since the UNIX_EPOCH.
This timestamp is authored by the controller of the keypair, and it is trusted as a way to order which packets where authored after which, but it shouldn’t be used for caching for example, instead, use Self::last_seen which is set when you create a new packet.
Sourcepub fn encoded_packet(&self) -> Bytes
pub fn encoded_packet(&self) -> Bytes
Returns the DNS Packet compressed and encoded.
Sourcepub fn set_last_seen(&mut self, last_seen: &u64)
pub fn set_last_seen(&mut self, last_seen: &u64)
Set the Self::last_seen property
Sourcepub fn refresh(&mut self)
pub fn refresh(&mut self)
Set the Self::last_seen to the current system time
Sourcepub fn more_recent_than(&self, other: &SignedPacket) -> bool
pub fn more_recent_than(&self, other: &SignedPacket) -> bool
Return whether this SignedPacket is more recent than the given one. If the timestamps are erqual, the one with the largest value is considered more recent. Usefel for determining which packet contains the latest information from the Dht. Assumes that both packets have the same PublicKey, you shouldn’t compare packets from different keys.
Sourcepub fn is_same_as(&self, other: &SignedPacket) -> bool
pub fn is_same_as(&self, other: &SignedPacket) -> bool
Returns true if both packets have the same timestamp and packet, and only differ in Self::last_seen
Sourcepub fn resource_records(
&self,
name: &str,
) -> impl Iterator<Item = &ResourceRecord<'_>>
pub fn resource_records( &self, name: &str, ) -> impl Iterator<Item = &ResourceRecord<'_>>
Return and iterator over the ResourceRecords in the Answers section of the DNS Packet that matches the given name. The name will be normalized to the origin TLD of this packet.
Sourcepub fn fresh_resource_records(
&self,
name: &str,
) -> impl Iterator<Item = &ResourceRecord<'_>>
pub fn fresh_resource_records( &self, name: &str, ) -> impl Iterator<Item = &ResourceRecord<'_>>
Similar to resource_records, but filters out
expired records, according the the Self::last_seen value and each record’s ttl.
Sourcepub fn expires_in(&self, min: u32, max: u32) -> u32
pub fn expires_in(&self, min: u32, max: u32) -> u32
calculates the remaining seconds by comparing the Self::ttl (clamped by min and max)
to the Self::last_seen.
§Panics
Panics if min < max
Trait Implementations§
Source§impl AsRef<[u8]> for SignedPacket
impl AsRef<[u8]> for SignedPacket
Source§impl Clone for SignedPacket
impl Clone for SignedPacket
Source§impl Debug for SignedPacket
impl Debug for SignedPacket
Source§impl Display for SignedPacket
impl Display for SignedPacket
Source§impl From<&SignedPacket> for MutableItem
impl From<&SignedPacket> for MutableItem
Source§fn from(s: &SignedPacket) -> Self
fn from(s: &SignedPacket) -> Self
Source§impl TryFrom<&MutableItem> for SignedPacket
impl TryFrom<&MutableItem> for SignedPacket
Auto Trait Implementations§
impl Freeze for SignedPacket
impl RefUnwindSafe for SignedPacket
impl Send for SignedPacket
impl Sync for SignedPacket
impl Unpin for SignedPacket
impl UnwindSafe for SignedPacket
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit)