pkarr

Struct SignedPacket

Source
pub struct SignedPacket { /* private fields */ }
Expand description

Signed DNS packet

Implementations§

Source§

impl SignedPacket

Source

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
Source

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.

Source

pub fn from_relay_payload( public_key: &PublicKey, payload: &Bytes, ) -> Result<SignedPacket>

Creates a SignedPacket from a PublicKey and the relays payload.

§Errors
Source

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
Source

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>

Source

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.

Source

pub fn public_key(&self) -> PublicKey

Returns the PublicKey of the signer of this SignedPacket

Source

pub fn signature(&self) -> Signature

Returns the Signature of the the bencoded sequence number concatenated with the encoded and compressed packet, as defined in BEP_0044

Source

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.

Source

pub fn encoded_packet(&self) -> Bytes

Returns the DNS Packet compressed and encoded.

Source

pub fn packet(&self) -> &Packet<'_>

Return the DNS Packet.

Source

pub fn last_seen(&self) -> &u64

Unix last_seen time in microseconds

Source

pub fn set_last_seen(&mut self, last_seen: &u64)

Set the Self::last_seen property

Source

pub fn refresh(&mut self)

Set the Self::last_seen to the current system time

Source

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.

Source

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

Source

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.

Source

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.

Source

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

Source

pub fn ttl(&self, min: u32, max: u32) -> u32

Returns the smallest ttl in the Self::packet resource records, calmped with min and max.

§Panics

Panics if min < max

Trait Implementations§

Source§

impl AsRef<[u8]> for SignedPacket

Source§

fn as_ref(&self) -> &[u8]

Returns the SignedPacket as a bytes slice with the format: <public_key><signature><6 bytes timestamp in microseconds><compressed dns packet>

Source§

impl Clone for SignedPacket

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SignedPacket

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for SignedPacket

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&SignedPacket> for MutableItem

Source§

fn from(s: &SignedPacket) -> Self

Converts to this type from the input type.
Source§

impl TryFrom<&MutableItem> for SignedPacket

Source§

type Error = Error

The type returned in the event of a conversion error.
Source§

fn try_from(i: &MutableItem) -> Result<Self>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more