Address

Struct Address 

Source
pub struct Address {
    pub addr_type: AddrType,
    pub type_int: u8,
    pub value: Option<String>,
    pub value_bin: Vec<u8>,
}
Expand description

A relay address with type information.

Represents an address in the Tor protocol, supporting IPv4, IPv6, and other address types. Addresses are encoded with a type byte, length byte, and variable-length value.

§Wire Format

+----------+--------+------------------+
| Type (1) | Len (1)| Value (Len bytes)|
+----------+--------+------------------+

§Example

use stem_rs::client::datatype::{Address, AddrType};

// Create from string
let addr = Address::new("127.0.0.1").unwrap();
assert_eq!(addr.addr_type, AddrType::IPv4);
assert_eq!(addr.value, Some("127.0.0.1".to_string()));

// Pack and unpack
let packed = addr.pack();
let unpacked = Address::unpack(&packed).unwrap();
assert_eq!(addr, unpacked);

§IPv6 Handling

IPv6 addresses are normalized to their fully expanded form:

use stem_rs::client::datatype::Address;

let addr = Address::new("::1").unwrap();
assert_eq!(addr.value, Some("0000:0000:0000:0000:0000:0000:0000:0001".to_string()));

Fields§

§addr_type: AddrType

The type of this address.

§type_int: u8

The raw type byte (preserved for unknown types).

§value: Option<String>

The human-readable address string (if applicable).

This is None for error types and unknown address types.

§value_bin: Vec<u8>

The raw binary representation of the address.

Implementations§

Source§

impl Address

Source

pub fn new(value: &str) -> Result<Self, Error>

Creates a new Address from an IP address string.

Automatically detects whether the address is IPv4 or IPv6.

§Arguments
  • value - An IPv4 or IPv6 address string
§Errors

Returns Error::Protocol if the string is not a valid IPv4 or IPv6 address.

§Example
use stem_rs::client::datatype::{Address, AddrType};

let ipv4 = Address::new("192.168.1.1").unwrap();
assert_eq!(ipv4.addr_type, AddrType::IPv4);

let ipv6 = Address::new("2001:db8::1").unwrap();
assert_eq!(ipv6.addr_type, AddrType::IPv6);

// Invalid addresses return an error
assert!(Address::new("not-an-address").is_err());
Source

pub fn with_type(value: &[u8], addr_type: u8) -> Result<Self, Error>

Creates an Address from raw bytes with a specified type.

This is used when parsing addresses from the wire format where the type is already known.

§Arguments
  • value - The raw address bytes
  • addr_type - The address type byte
§Errors

Returns Error::Protocol if:

  • addr_type is 4 (IPv4) but value is not 4 bytes
  • addr_type is 6 (IPv6) but value is not 16 bytes
§Example
use stem_rs::client::datatype::{Address, AddrType};

let addr = Address::with_type(&[127, 0, 0, 1], 4).unwrap();
assert_eq!(addr.value, Some("127.0.0.1".to_string()));

// Unknown types are accepted
let unknown = Address::with_type(b"data", 99).unwrap();
assert_eq!(unknown.addr_type, AddrType::Unknown);
Source

pub fn pack(&self) -> Vec<u8>

Packs the address into its wire format.

§Returns

A Vec<u8> containing: [type, length, value...]

§Example
use stem_rs::client::datatype::Address;

let addr = Address::new("127.0.0.1").unwrap();
let packed = addr.pack();
assert_eq!(packed, vec![0x04, 0x04, 127, 0, 0, 1]);
Source

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

Unpacks an address from its wire format.

§Arguments
  • data - The packed address bytes (must be exactly the right size)
§Errors

Returns Error::Protocol if:

  • The data is too short
  • There are extra bytes after the address
  • The address type/length combination is invalid
Source

pub fn pop(content: &[u8]) -> Result<(Self, &[u8]), Error>

Unpacks an address from the start of a byte slice, returning the remainder.

§Arguments
  • content - The byte slice to read from
§Returns

A tuple of (Address, remainder).

§Errors

Returns Error::Protocol if the data is malformed.

Trait Implementations§

Source§

impl Clone for Address

Source§

fn clone(&self) -> Address

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 Address

Source§

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

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

impl PartialEq for Address

Source§

fn eq(&self, other: &Address) -> 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 Eq for Address

Source§

impl StructuralPartialEq for Address

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> 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 = 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.