pub struct NetworkAddress(/* private fields */);
Expand description

§Overview

Diem NetworkAddress is a compact, efficient, self-describing and future-proof network address represented as a stack of protocols. Essentially libp2p’s multiaddr but using [bcs] to describe the binary format.

Most validators will advertise a network address like:

/dns/example.com/tcp/6180/ln-noise-ik/<x25519-pubkey>/ln-handshake/1

Unpacking, the above effectively means:

  1. Resolve the DNS name “example.com” to an ip address, addr.
  2. Open a TCP connection to (addr, 6180).
  3. Perform a Noise IK handshake and assume the peer’s static pubkey is <x25519-pubkey>. After this step, we will have a secure, authenticated connection with the peer.
  4. Perform a DiemNet version negotiation handshake (version 1).

§Self-describing, Upgradable

One key concept behind NetworkAddress is that it is fully self-describing, which allows us to easily “pre-negotiate” protocols while also allowing for future upgrades. For example, it is generally unsafe to negotiate a secure transport in-band. Instead, with NetworkAddress we can advertise (via discovery) the specific secure transport protocol and public key that we support (and even advertise multiple incompatible versions). When a peer wishes to establish a connection with us, they already know which secure transport protocol to use; in this sense, the secure transport protocol is “pre-negotiated” by the dialier selecting which advertised protocol to use.

Each network address is encoded with the length of the encoded NetworkAddress and then the serialized protocol slices to allow for transparent upgradeability. For example, if the current software cannot decode a NetworkAddress within a Vec<NetworkAddress> it can still decode the underlying Vec<u8> and retrieve the remaining Vec<NetworkAddress>.

§Transport

In addition, NetworkAddress is integrated with the DiemNet concept of a Transport, which takes a NetworkAddress when dialing and peels off Protocols to establish a connection and perform initial handshakes. Similarly, the Transport takes NetworkAddress to listen on, which tells it what protocols to expect on the socket.

§Example

An example of a serialized NetworkAddress:

// human-readable format:
//
//   "/ip4/10.0.0.16/tcp/80"
//
// serialized NetworkAddress:
//
//      [ 09 02 00 0a 00 00 10 05 80 00 ]
//          \  \  \  \           \  \
//           \  \  \  \           \  '-- u16 tcp port
//            \  \  \  \           '-- uvarint protocol id for /tcp
//             \  \  \  '-- u32 ipv4 address
//              \  \  '-- uvarint protocol id for /ip4
//               \  '-- uvarint number of protocols
//                '-- length of encoded network address

use bcs;
use diem_types::network_address::NetworkAddress;
use std::{convert::TryFrom, str::FromStr};

let addr = NetworkAddress::from_str("/ip4/10.0.0.16/tcp/80").unwrap();
let actual_ser_addr = bcs::to_bytes(&addr).unwrap();

let expected_ser_addr: Vec<u8> = [9, 2, 0, 10, 0, 0, 16, 5, 80, 0].to_vec();

assert_eq!(expected_ser_addr, actual_ser_addr);

Implementations§

source§

impl NetworkAddress

source

pub fn as_slice(&self) -> &[Protocol]

source

pub fn push(self, proto: Protocol) -> Self

source

pub fn extend_from_slice(self, protos: &[Protocol]) -> Self

source

pub fn encrypt( self, shared_val_netaddr_key: &Key, key_version: KeyVersion, account: &AccountAddress, seq_num: u64, addr_idx: u32 ) -> Result<EncNetworkAddress, ParseError>

source

pub fn append_prod_protos( self, network_pubkey: PublicKey, handshake_version: u8 ) -> Self

Given a base NetworkAddress, append production protocols and return the modified NetworkAddress.

§Example
use diem_crypto::{traits::ValidCryptoMaterialStringExt, x25519};
use diem_types::network_address::NetworkAddress;
use std::str::FromStr;

let pubkey_str = "080e287879c918794170e258bfaddd75acac5b3e350419044655e4983a487120";
let pubkey = x25519::PublicKey::from_encoded_string(pubkey_str).unwrap();
let addr = NetworkAddress::from_str("/dns/example.com/tcp/6180").unwrap();
let addr = addr.append_prod_protos(pubkey, 0);
assert_eq!(
    addr.to_string(),
    "/dns/example.com/tcp/6180/ln-noise-ik/080e287879c918794170e258bfaddd75acac5b3e350419044655e4983a487120/ln-handshake/0",
);
source

pub fn is_diemnet_addr(&self) -> bool

Check that a NetworkAddress looks like a typical DiemNet address with associated protocols.

“typical” DiemNet addresses begin with a transport protocol:

"/ip4/<addr>/tcp/<port>" or "/ip6/<addr>/tcp/<port>" or "/dns4/<domain>/tcp/<port>" or "/dns6/<domain>/tcp/<port>" or "/dns/<domain>/tcp/<port>" or cfg!(test) "/memory/<port>"

followed by transport upgrade handshake protocols:

"/ln-noise-ik/<pubkey>/ln-handshake/<version>"

§Example
use diem_types::network_address::NetworkAddress;
use std::str::FromStr;

let addr_str = "/ip4/1.2.3.4/tcp/6180/ln-noise-ik/080e287879c918794170e258bfaddd75acac5b3e350419044655e4983a487120/ln-handshake/0";
let addr = NetworkAddress::from_str(addr_str).unwrap();
assert!(addr.is_diemnet_addr());
source

pub fn find_ip_addr(&self) -> Option<IpAddr>

Retrieves the IP address from the network address

source

pub fn find_noise_proto(&self) -> Option<PublicKey>

A temporary, hacky function to parse out the first /ln-noise-ik/<pubkey> from a NetworkAddress. We can remove this soon, when we move to the interim “monolithic” transport model.

source

pub fn rotate_noise_public_key( &mut self, to_replace: &PublicKey, new_public_key: &PublicKey )

A function to rotate public keys for NoiseIK protocols

Trait Implementations§

source§

impl Clone for NetworkAddress

source§

fn clone(&self) -> NetworkAddress

Returns a copy 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 NetworkAddress

source§

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

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

impl<'de> Deserialize<'de> for NetworkAddress

source§

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

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for NetworkAddress

source§

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

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

impl From<Protocol> for NetworkAddress

source§

fn from(proto: Protocol) -> NetworkAddress

Converts to this type from the input type.
source§

impl From<SocketAddr> for NetworkAddress

source§

fn from(sockaddr: SocketAddr) -> NetworkAddress

Converts to this type from the input type.
source§

impl FromStr for NetworkAddress

§

type Err = ParseError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl Hash for NetworkAddress

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl IntoIterator for NetworkAddress

§

type IntoIter = IntoIter<<NetworkAddress as IntoIterator>::Item>

Which kind of iterator are we turning this into?
§

type Item = Protocol

The type of the elements being iterated over.
source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
source§

impl PartialEq for NetworkAddress

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for NetworkAddress

source§

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

Serialize this value into the given Serde serializer. Read more
source§

impl ToSocketAddrs for NetworkAddress

§

type Iter = IntoIter<SocketAddr>

Returned iterator over socket addresses which this type may correspond to.
source§

fn to_socket_addrs(&self) -> Result<Self::Iter, Error>

Converts this object to an iterator of resolved SocketAddrs. Read more
source§

impl TryFrom<Vec<Protocol>> for NetworkAddress

§

type Error = EmptyError

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

fn try_from(value: Vec<Protocol>) -> Result<Self, Self::Error>

Performs the conversion.
source§

impl Eq for NetworkAddress

source§

impl StructuralPartialEq for NetworkAddress

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
§

impl<I> BidiIterator for I

§

fn bidi(self, cond: bool) -> Bidi<Self::IntoIter>

Conditionally reverses the direction of iteration. 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
§

impl<T> Conv for T

§

fn conv<T>(self) -> T
where Self: Into<T>,

Converts self into T using Into<T>. Read more
§

impl<T> FmtForward for T

§

fn fmt_binary(self) -> FmtBinary<Self>
where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
§

fn fmt_display(self) -> FmtDisplay<Self>
where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>
where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>
where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
§

fn fmt_octal(self) -> FmtOctal<Self>
where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
§

fn fmt_pointer(self) -> FmtPointer<Self>
where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>
where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>
where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
§

fn fmt_list(self) -> FmtList<Self>
where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. 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.

§

impl<T> Pipe for T
where T: ?Sized,

§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
§

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T> Serialize for T
where T: Serialize + ?Sized,

source§

fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<Ok, Error>

§

impl<T> Tap for T

§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Calls .tap_borrow() only in debug builds, and is erased in release builds.
§

fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
§

fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Calls .tap_ref() only in debug builds, and is erased in release builds.
§

fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
§

impl<T> TestOnlyHash for T
where T: Serialize + ?Sized,

§

fn test_only_hash(&self) -> HashValue

Generates a hash used only for tests.
source§

impl<T> ToOwned for T
where T: Clone,

§

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§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
§

impl<T> TryConv for T

§

fn try_conv<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

source§

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