Skip to main content

SignedPacket

Struct SignedPacket 

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

Signed DNS packet

Implementations§

Source§

impl SignedPacket

Source

pub const MAX_BYTES: u64 = 1104

Maximum legal size of the binary representation of a SignedPacket.

Calculated from the maximum size of a value in Mainline DHT’s mutable values; 1000 bytes, plus the number of bytes in a Signature (64), and a Timestamp (8) and a public key (32).

Source

pub fn builder() -> SignedPacketBuilder

Create a SignedPacket using a builder.

 use pkarr::{SignedPacket, Keypair, dns::rdata::SVCB};
  
 let keypair = Keypair::random();

 let signed_packet = SignedPacket::builder()
    // A record
    .address(
        "_derp_region.iroh.".try_into().unwrap(),
        "1.1.1.1".parse().unwrap(),
        30,
    )
    // AAAA record
    .address(
        "_derp_region.iroh.".try_into().unwrap(),
        "3002:0bd6:0000:0000:0000:ee00:0033:6778".parse().unwrap(),
        30,
    )
    // CNAME record
    .cname(
        "subdomain.".try_into().unwrap(),
        "example.com".try_into().unwrap(),
        30
    )
    // TXT record
    .txt(
        "_proto".try_into().unwrap(),
        "foo=bar".try_into().unwrap(),
        30
    )
    // HTTPS record
    .https(
        // You can make a record at the Apex (at the same TLD as your public key)
        ".".try_into().unwrap(),
        SVCB::new(0, "https.example.com".try_into().unwrap()),
        3600,
    )
    // SVCB record
    .https(
        ".".try_into().unwrap(),
        SVCB::new(0, "https.example.com".try_into().unwrap()),
        3600,
    )
    .sign(&keypair)
    .unwrap();
Source

pub fn new( keypair: &Keypair, answers: &[ResourceRecord<'_>], timestamp: Timestamp, ) -> Result<SignedPacket, SignedPacketBuildError>

Creates a new SignedPacket from a Keypair and ResourceRecords as the answers section of a DNS Packet, and a Timestamp.

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.

If any name is empty or just a ., it will be normalized to the public key of the keypair.

Source

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

Creates a SignedPacket from a PublicKey and the relays payload.

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 serialize(&self) -> Vec<u8>

Returns a serialized representation of this SignedPacket including the SignedPacket::last_seen timestamp followed by the returned value from SignedPacket::as_bytes.

Source

pub fn deserialize(bytes: &[u8]) -> Result<Self, SimpleDnsError>

Deserialize SignedPacket from a serialized version for persistent storage using SignedPacket::serialize.

If deserializing the SignedPacket::last_seen failed, or is far in the future, it will be unwrapped to default, i.e the UNIX_EPOCH.

That is useful for backwards compatibility if you ever stored the SignedPacket::last_seen as Little Endian in previous versions.

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) -> Timestamp

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 last_seen(&self) -> &Timestamp

Unix last_seen time in microseconds

Source

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

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.

You can use @ to filter the resource records at the Apex (the public key).

Wildcards are also supported, so *.foo.<key> will match bar.foo.<key>.

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 all_resource_records(&self) -> impl Iterator<Item = &ResourceRecord<'_>>

Returns all resource records in this packet

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 resource records, calmped with min and max.

§Panics

Panics if min < max

Source

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

Returns whether or not this packet is considered expired according to a given min and max TTLs, by comparing it to this SignedPacket::ttl.

Source

pub fn elapsed(&self) -> u32

Time since the Self::last_seen in seconds

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<'a> BytesDecode<'a> for SignedPacket

Source§

type DItem = SignedPacket

The type to decode.
Source§

fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError>

Decode the given bytes as DItem.
Source§

impl<'a> BytesEncode<'a> for SignedPacket

Source§

type EItem = SignedPacket

The type to encode.
Source§

fn bytes_encode( signed_packet: &Self::EItem, ) -> Result<Cow<'a, [u8]>, BoxedError>

Encode the given item as bytes.
Source§

impl Clone for SignedPacket

Source§

fn clone(&self) -> Self

Returns a duplicate 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<'de> Deserialize<'de> for SignedPacket

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize a SignedPacket from persistent storage.

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

Available on dht only.
Source§

fn from(s: &SignedPacket) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for SignedPacket

Source§

fn eq(&self, other: &SignedPacket) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for SignedPacket

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize a SignedPacket for persistent storage.

Source§

impl TryFrom<&MutableItem> for SignedPacket

Available on dht only.
Source§

type Error = SignedPacketVerifyError

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

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

Performs the conversion.
Source§

impl TryFrom<MutableItem> for SignedPacket

Available on dht only.
Source§

type Error = SignedPacketVerifyError

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

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

Performs the conversion.
Source§

impl Eq for SignedPacket

Source§

impl StructuralPartialEq for SignedPacket

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, dest: *mut u8)

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

impl<T> CompatExt for T

Source§

fn compat(self) -> Compat<T>

Applies the Compat adapter by value. Read more
Source§

fn compat_ref(&self) -> Compat<&T>

Applies the Compat adapter by shared reference. Read more
Source§

fn compat_mut(&mut self) -> Compat<&mut T>

Applies the Compat adapter by mutable reference. 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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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§

fn to_string(&self) -> String

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

impl<T> ToStringFallible for T
where T: Display,

Source§

fn try_to_string(&self) -> Result<String, TryReserveError>

ToString::to_string, but without panic on OOM.

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<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
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> ErasedDestructor for T
where T: 'static,