pub struct SignedPacket { /* private fields */ }Expand description
Signed DNS packet
Implementations§
Source§impl SignedPacket
impl SignedPacket
Sourcepub const MAX_BYTES: u64 = 1104
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).
Sourcepub fn builder() -> SignedPacketBuilder
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();Sourcepub fn new(
keypair: &Keypair,
answers: &[ResourceRecord<'_>],
timestamp: Timestamp,
) -> Result<SignedPacket, SignedPacketBuildError>
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.
Sourcepub fn from_relay_payload(
public_key: &PublicKey,
payload: &Bytes,
) -> Result<SignedPacket, SignedPacketVerifyError>
pub fn from_relay_payload( public_key: &PublicKey, payload: &Bytes, ) -> Result<SignedPacket, SignedPacketVerifyError>
Creates a SignedPacket from a PublicKey and the relays payload.
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 serialize(&self) -> Vec<u8> ⓘ
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.
Sourcepub fn deserialize(bytes: &[u8]) -> Result<Self, SimpleDnsError>
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.
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) -> Timestamp
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.
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: &Timestamp)
pub fn set_last_seen(&mut self, last_seen: &Timestamp)
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.
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>.
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 all_resource_records(&self) -> impl Iterator<Item = &ResourceRecord<'_>>
pub fn all_resource_records(&self) -> impl Iterator<Item = &ResourceRecord<'_>>
Returns all resource records in this packet
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
Sourcepub fn ttl(&self, min: u32, max: u32) -> u32
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
Sourcepub fn is_expired(&self, min: u32, max: u32) -> bool
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.
Sourcepub fn elapsed(&self) -> u32
pub fn elapsed(&self) -> u32
Time since the Self::last_seen in seconds
Trait Implementations§
Source§impl AsRef<[u8]> for SignedPacket
impl AsRef<[u8]> for SignedPacket
Source§impl<'a> BytesDecode<'a> for SignedPacket
impl<'a> BytesDecode<'a> for SignedPacket
Source§type DItem = SignedPacket
type DItem = SignedPacket
Source§fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError>
fn bytes_decode(bytes: &'a [u8]) -> Result<Self::DItem, BoxedError>
DItem.Source§impl<'a> BytesEncode<'a> for SignedPacket
impl<'a> BytesEncode<'a> for SignedPacket
Source§type EItem = SignedPacket
type EItem = SignedPacket
Source§fn bytes_encode(
signed_packet: &Self::EItem,
) -> Result<Cow<'a, [u8]>, BoxedError>
fn bytes_encode( signed_packet: &Self::EItem, ) -> Result<Cow<'a, [u8]>, BoxedError>
Source§impl Clone for SignedPacket
impl Clone for SignedPacket
Source§impl Debug for SignedPacket
impl Debug for SignedPacket
Source§impl<'de> Deserialize<'de> for SignedPacket
impl<'de> Deserialize<'de> for SignedPacket
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Deserialize a SignedPacket from persistent storage.
Source§impl Display for SignedPacket
impl Display for SignedPacket
Source§impl From<&SignedPacket> for MutableItem
Available on dht only.
impl From<&SignedPacket> for MutableItem
dht only.Source§fn from(s: &SignedPacket) -> Self
fn from(s: &SignedPacket) -> Self
Source§impl PartialEq for SignedPacket
impl PartialEq for SignedPacket
Source§impl Serialize for SignedPacket
impl Serialize for SignedPacket
Source§fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>where
S: Serializer,
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.
impl TryFrom<&MutableItem> for SignedPacket
dht only.Source§type Error = SignedPacketVerifyError
type Error = SignedPacketVerifyError
Source§fn try_from(i: &MutableItem) -> Result<Self, SignedPacketVerifyError>
fn try_from(i: &MutableItem) -> Result<Self, SignedPacketVerifyError>
Source§impl TryFrom<MutableItem> for SignedPacket
Available on dht only.
impl TryFrom<MutableItem> for SignedPacket
dht only.Source§type Error = SignedPacketVerifyError
type Error = SignedPacketVerifyError
Source§fn try_from(i: MutableItem) -> Result<Self, SignedPacketVerifyError>
fn try_from(i: MutableItem) -> Result<Self, SignedPacketVerifyError>
impl Eq for SignedPacket
impl StructuralPartialEq 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 UnsafeUnpin 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§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> ToStringFallible for Twhere
T: Display,
impl<T> ToStringFallible for Twhere
T: Display,
Source§fn try_to_string(&self) -> Result<String, TryReserveError>
fn try_to_string(&self) -> Result<String, TryReserveError>
ToString::to_string, but without panic on OOM.