Skip to main content

RawPacket

Struct RawPacket 

Source
pub struct RawPacket<B = Vec<u8>> {
    pub flags: PacketFlags,
    pub hops: u8,
    pub transport_id: Option<[u8; 16]>,
    pub destination_hash: [u8; 16],
    pub context: u8,
    pub raw: B,
    pub packet_hash: [u8; 32],
    pub rssi: Option<i16>,
    pub snr: Option<f32>,
}
Expand description

A packet’s header fields and its wire bytes raw, owned (Vec<u8>, the default) or in other storage: a borrowed view (&[u8]) reads a packet without copying it, and shared bytes (PacketBytes) are cloned, not copied.

Fields§

§flags: PacketFlags§hops: u8§transport_id: Option<[u8; 16]>§destination_hash: [u8; 16]§context: u8§raw: B§packet_hash: [u8; 32]§rssi: Option<i16>§snr: Option<f32>

Implementations§

Source§

impl RawPacket

Source

pub const fn data_offset(header_type: u8) -> usize

Offset of the payload in the wire bytes of a packet with this header type.

RawPacket::is_unhashed_link_resource for wire bytes that are not unpacked; false when they are too short to carry a context byte.

Source

pub fn pack( flags: PacketFlags, hops: u8, destination_hash: &[u8; 16], transport_id: Option<&[u8; 16]>, context: u8, data: &[u8], ) -> Result<Self, PacketError>

Pack fields into raw bytes.

Source

pub fn pack_raw_with_hash( flags: PacketFlags, hops: u8, destination_hash: &[u8; 16], transport_id: Option<&[u8; 16]>, context: u8, data: &[u8], ) -> Result<(Vec<u8>, [u8; 32]), PacketError>

Pack fields into raw bytes and packet hash without constructing a full RawPacket.

Source

pub fn pack_with_max_mtu( flags: PacketFlags, hops: u8, destination_hash: &[u8; 16], transport_id: Option<&[u8; 16]>, context: u8, data: &[u8], max_mtu: usize, ) -> Result<Self, PacketError>

Pack fields into raw bytes with a caller-provided MTU limit.

Source

pub fn pack_raw_with_hash_with_max_mtu( flags: PacketFlags, hops: u8, destination_hash: &[u8; 16], transport_id: Option<&[u8; 16]>, context: u8, data: &[u8], max_mtu: usize, ) -> Result<(Vec<u8>, [u8; 32]), PacketError>

Pack fields into raw bytes and packet hash with a caller-provided MTU limit.

Source

pub fn pack_raw_shared_with_hash_with_max_mtu( flags: PacketFlags, hops: u8, destination_hash: &[u8; 16], transport_id: Option<&[u8; 16]>, context: u8, data: &[u8], max_mtu: usize, ) -> Result<(PacketBytes, [u8; 32]), PacketError>

Pack only wire bytes when the caller does not need a packet hash. Validation and encoding match the hash-producing packing methods. RawPacket::pack_raw_with_hash_with_max_mtu into shared bytes.

Source

pub fn packet_hash_of(header_type: u8, raw: &[u8]) -> [u8; 32]

The packet hash of packed wire bytes with the given header type.

Source

pub fn pack_raw_shared_filled_with_max_mtu( flags: PacketFlags, hops: u8, destination_hash: &[u8; 16], transport_id: Option<&[u8; 16]>, context: u8, payload_len: usize, max_mtu: usize, fill: impl FnOnce(&mut [u8]), ) -> Result<PacketBytes, PacketError>

RawPacket::pack_raw_shared_with_max_mtu with a payload of payload_len bytes that fill writes in place, so a payload produced for this packet (a ciphertext, say) needs no buffer of its own. fill receives the payload zeroed.

Source

pub fn pack_raw_shared_with_max_mtu( flags: PacketFlags, hops: u8, destination_hash: &[u8; 16], transport_id: Option<&[u8; 16]>, context: u8, data: &[u8], max_mtu: usize, ) -> Result<PacketBytes, PacketError>

RawPacket::pack_raw_with_max_mtu into shared bytes, allocated once at their final size, for packets that are routed and sent as they are.

Source

pub fn pack_raw_with_max_mtu( flags: PacketFlags, hops: u8, destination_hash: &[u8; 16], transport_id: Option<&[u8; 16]>, context: u8, data: &[u8], max_mtu: usize, ) -> Result<Vec<u8>, PacketError>

Source

pub fn unpack(raw: &[u8]) -> Result<Self, PacketError>

Unpack raw bytes into fields.

Source

pub fn unpack_owned(raw: Vec<u8>) -> Result<Self, PacketError>

Unpack wire bytes the caller owns, keeping them as raw instead of copying them.

Source

pub fn unpack_outbound(raw: Vec<u8>) -> Result<Self, PacketError>

Unpack owned wire bytes of a packet this node sends. A link resource part, part request or proof is left unhashed, its packet_hash zeroed, because nothing reads it (see RawPacket::is_unhashed_link_resource).

Source

pub fn unpack_shared( raw: PacketBytes, ) -> Result<RawPacket<PacketBytes>, PacketError>

RawPacket::unpack_owned for shared bytes, kept as they are.

Source

pub fn unpack_outbound_shared( raw: PacketBytes, ) -> Result<RawPacket<PacketBytes>, PacketError>

RawPacket::unpack_outbound for shared bytes, kept as they are.

Source

pub fn unpack_prehashed( raw: &[u8], packet_hash: [u8; 32], ) -> Result<Self, PacketError>

Unpack wire bytes whose packet hash the caller already computed from these exact bytes (for example a delivered packet’s hash), instead of hashing them again. The hash is trusted, not checked.

Source§

impl<B: AsRef<[u8]>> RawPacket<B>

Source

pub fn data(&self) -> &[u8] ⓘ

The payload: the wire bytes after the header.

Whether this packet is link resource traffic whose hash nothing reads: a part, a part request, or a resource proof. The packet filter admits these contexts before deduplication (Transport.packet_filter), no proof or receipt covers them, and their handlers read only the payload (the receiver locates a part by its map hash). Their hashes cannot mark any other packet as a duplicate either, since the context byte is hashed, so they are neither computed nor remembered.

Whether a link peer never proves this packet: link data in a context with its own protocol handling (requests and responses, resource control and parts, keepalives, identification, RTT and close). Only plain link data and channel messages are proven, so a sent packet in one of these contexts has no receipt to wait for.

Source

pub fn get_hashable_part(&self) -> Vec<u8> ⓘ

Get the hashable part of the packet.

From Python Packet.py:354-361:

  • Take raw[0] & 0x0F (mask out upper 4 bits of flags)
  • For HEADER_1: append raw[2:]
  • For HEADER_2: skip transport_id: append raw[18:]
Source

pub fn get_hash(&self) -> [u8; 32]

Full SHA-256 hash of the hashable part.

Source

pub fn get_truncated_hash(&self) -> [u8; 16]

Truncated hash (first 16 bytes) of the cached packet hash.

Packet construction and unpacking compute packet_hash once; routing paths use this prefix instead of hashing the raw packet again.

Source

pub fn as_view(&self) -> RawPacket<&[u8]>

A view of this packet borrowing its bytes.

Source

pub fn to_owned_packet(&self) -> RawPacket

This packet with its bytes copied into an owned buffer.

Source

pub fn map_raw<C>(self, f: impl FnOnce(B) -> C) -> RawPacket<C>

This packet with its bytes converted by f, fields unchanged.

Source§

impl<'a> RawPacket<&'a [u8]>

Source

pub fn view_prehashed( raw: &'a [u8], packet_hash: [u8; 32], ) -> Result<Self, PacketError>

RawPacket::unpack_prehashed as a view of raw, without copying it.

Trait Implementations§

Source§

impl<B: Clone> Clone for RawPacket<B>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<B: Debug> Debug for RawPacket<B>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<B> Forget for RawPacket<B>

§

impl<B> Freeze for RawPacket<B>
where B: Freeze,

§

impl<B> RefUnwindSafe for RawPacket<B>
where B: RefUnwindSafe,

§

impl<B> Send for RawPacket<B>
where B: Send,

§

impl<B> Sync for RawPacket<B>
where B: Sync,

§

impl<B> Unpin for RawPacket<B>
where B: Unpin,

§

impl<B> UnsafeUnpin for RawPacket<B>
where B: UnsafeUnpin,

§

impl<B> UnwindSafe for RawPacket<B>
where B: UnwindSafe,

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.