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: AddrTypeThe type of this address.
type_int: u8The 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
impl Address
Sourcepub fn new(value: &str) -> Result<Self, Error>
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());Sourcepub fn with_type(value: &[u8], addr_type: u8) -> Result<Self, Error>
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 bytesaddr_type- The address type byte
§Errors
Returns Error::Protocol if:
addr_typeis 4 (IPv4) butvalueis not 4 bytesaddr_typeis 6 (IPv6) butvalueis 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);Sourcepub fn unpack(data: &[u8]) -> Result<Self, Error>
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