Procházet zdrojové kódy

channel: expand ChannelInfo to include resolve_addr and connect_addr

This paves the way for DEP 0001: https://darkrenaissance.github.io/darkfi/dep/0001.html

We retain the same method for returning the channel's address, channel.address() but modify it so that it adapts depending on whether this channel is inbound or outbound. The usage remains the same and the return value is equivalent.
draoi před 2 roky
rodič
revize
b9edcc6077

+ 1 - 1
src/net/acceptor.rs

@@ -134,7 +134,7 @@ impl Acceptor {
 
                     // Create the new Channel.
                     let session = self.session.clone();
-                    let channel = Channel::new(stream, url, session).await;
+                    let channel = Channel::new(stream, None, url, session).await;
 
                     // Increment the connection counter
                     self.conn_count.fetch_add(1, SeqCst);

+ 29 - 8
src/net/channel.rs

@@ -57,13 +57,14 @@ pub type ChannelPtr = Arc<Channel>;
 /// Channel debug info
 #[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
 pub struct ChannelInfo {
-    pub addr: Url,
+    pub resolve_addr: Option<Url>,
+    pub connect_addr: Url,
     pub id: u32,
 }
 
 impl ChannelInfo {
-    fn new(addr: Url) -> Self {
-        Self { addr, id: OsRng.gen() }
+    fn new(resolve_addr: Option<Url>, connect_addr: Url) -> Self {
+        Self { resolve_addr, connect_addr, id: OsRng.gen() }
     }
 }
 
@@ -91,7 +92,12 @@ impl Channel {
     /// Sets up a new channel. Creates a reader and writer [`PtStream`] and
     /// the message subscriber subsystem. Performs a network handshake on the
     /// subsystem dispatchers.
-    pub async fn new(stream: Box<dyn PtStream>, addr: Url, session: SessionWeakPtr) -> Arc<Self> {
+    pub async fn new(
+        stream: Box<dyn PtStream>,
+        resolve_addr: Option<Url>,
+        connect_addr: Url,
+        session: SessionWeakPtr,
+    ) -> Arc<Self> {
         let (reader, writer) = io::split(stream);
         let reader = Mutex::new(reader);
         let writer = Mutex::new(writer);
@@ -99,7 +105,7 @@ impl Channel {
         let message_subsystem = MessageSubsystem::new();
         Self::setup_dispatchers(&message_subsystem).await;
 
-        let info = ChannelInfo::new(addr.clone());
+        let info = ChannelInfo::new(resolve_addr, connect_addr.clone());
 
         Arc::new(Self {
             reader,
@@ -320,9 +326,24 @@ impl Channel {
         debug!(target: "net::channel::ban()", "STOP {:?}", self);
     }
 
-    /// Returns the local socket address
+    /// Returns the relevant socket address for this connection.  If this is
+    /// an outbound connection, the transport-processed resolve_addr will
+    /// be returned.  Otherwise for inbound connections it will default
+    /// to connect_addr.
     pub fn address(&self) -> &Url {
-        &self.info.addr
+        if self.info.resolve_addr.is_some() {
+            self.resolve_addr()
+        } else {
+            self.connect_addr()
+        }
+    }
+
+    fn resolve_addr(&self) -> &Url {
+        &self.info.resolve_addr.as_ref().unwrap()
+    }
+
+    fn connect_addr(&self) -> &Url {
+        &self.info.connect_addr
     }
 
     /// Returns the inner [`MessageSubsystem`] reference
@@ -353,6 +374,6 @@ impl Channel {
 
 impl fmt::Debug for Channel {
     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
-        write!(f, "<Channel addr='{}' id={}>", self.info.addr, self.info.id)
+        write!(f, "<Channel addr='{}' id={}>", self.address(), self.info.id)
     }
 }

+ 2 - 1
src/net/connector.rs

@@ -80,7 +80,8 @@ impl Connector {
         let timeout = Duration::from_secs(self.settings.outbound_connect_timeout);
         let ptstream = dialer.dial(Some(timeout)).await?;
 
-        let channel = Channel::new(ptstream, endpoint.clone(), self.session.clone()).await;
+        let channel =
+            Channel::new(ptstream, Some(endpoint.clone()), url.clone(), self.session.clone()).await;
         Ok((endpoint, channel))
     }
 }

+ 2 - 2
src/net/session/inbound_session.rs

@@ -189,7 +189,7 @@ impl InboundSession {
         );
 
         dnetev!(self, InboundConnected, {
-            addr: channel.info.addr.clone(),
+            addr: channel.info.connect_addr.clone(),
             channel_id: channel.info.id,
         });
 
@@ -207,7 +207,7 @@ impl InboundSession {
         );
 
         dnetev!(self, InboundDisconnected, {
-            addr: channel.info.addr.clone(),
+            addr: channel.info.connect_addr.clone(),
             channel_id: channel.info.id,
         });
 

+ 4 - 1
src/rpc/from_impl.rs

@@ -25,7 +25,10 @@ use crate::event_graph;
 #[cfg(feature = "net")]
 impl From<net::channel::ChannelInfo> for JsonValue {
     fn from(info: net::channel::ChannelInfo) -> JsonValue {
-        json_map([("addr", JsonStr(info.addr.to_string())), ("id", JsonNum(info.id.into()))])
+        json_map([
+            ("addr", JsonStr(info.connect_addr.to_string())),
+            ("id", JsonNum(info.id.into())),
+        ])
     }
 }