Selaa lähdekoodia

net: DEP-0006: add app identifier to Version and Verack messages and check app identifier is the same during version exchange

oars 1 vuosi sitten
vanhempi
sitoutus
8b7ee9274e
3 muutettua tiedostoa jossa 32 lisäystä ja 8 poistoa
  1. 4 0
      src/net/message.rs
  2. 11 3
      src/net/protocol/protocol_version.rs
  3. 17 5
      src/net/settings.rs

+ 4 - 0
src/net/message.rs

@@ -149,6 +149,8 @@ impl_p2p_message!(AddrsMessage, "addr", ADDRS_MAX_BYTES, 1, ADDRS_METERING_CONFI
 pub struct VersionMessage {
     /// Only used for debugging. Compromises privacy when set.
     pub node_id: String,
+    /// App identifier
+    pub app_name: String,
     /// Identifies protocol version being used by the node.
     pub version: semver::Version,
     /// UNIX timestamp of when the VersionMessage was created.
@@ -201,6 +203,8 @@ impl VersionMessage {
 pub struct VerackMessage {
     /// App version
     pub app_version: semver::Version,
+    /// App identifier
+    pub app_name: String,
 }
 pub const VERACK_METERING_CONFIGURATION: MeteringConfiguration = MeteringConfiguration {
     threshold: 4,

+ 11 - 3
src/net/protocol/protocol_version.rs

@@ -155,12 +155,14 @@ impl ProtocolVersion {
         let settings = self.settings.read().await;
         let node_id = settings.node_id.clone();
         let app_version = settings.app_version.clone();
+        let app_name = settings.app_name.clone();
         drop(settings);
 
         let external_addrs = self.channel.hosts().external_addrs().await;
 
         let version = VersionMessage {
             node_id,
+            app_name: app_name.clone(),
             version: app_version.clone(),
             timestamp: UNIX_EPOCH.elapsed().unwrap().as_secs(),
             connect_recv_addr: self.channel.connect_addr().clone(),
@@ -183,9 +185,10 @@ impl ProtocolVersion {
             verack_msg.app_version,
         );
 
-        // MAJOR and MINOR should be the same.
+        // MAJOR and MINOR should be the same, as well as the app identifier
         if app_version.major != verack_msg.app_version.major ||
-            app_version.minor != verack_msg.app_version.minor
+            app_version.minor != verack_msg.app_version.minor ||
+             app_name != verack_msg.app_name
         {
             error!(
                 target: "net::protocol_version::send_version()",
@@ -229,7 +232,12 @@ impl ProtocolVersion {
         self.channel.set_version(version).await;
 
         // Send verack
-        let verack = VerackMessage { app_version: self.settings.read().await.app_version.clone() };
+        let settings = self.settings.read().await;
+        let app_version = settings.app_version.clone();
+        let app_name = settings.app_name.clone();
+        drop(settings);
+
+        let verack = VerackMessage { app_version, app_name };
         self.channel.send(&verack).await?;
 
         debug!(

+ 17 - 5
src/net/settings.rs

@@ -19,6 +19,8 @@
 use structopt::StructOpt;
 use url::Url;
 
+use crate::error::{Error, Result};
+
 type BlacklistEntry = (String, Vec<String>, Vec<u16>);
 
 /// Ban policies definitions.
@@ -60,6 +62,8 @@ pub struct Settings {
     pub magic_bytes: MagicBytes,
     /// Application version, used for convenient protocol matching
     pub app_version: semver::Version,
+    /// Application Identifier
+    pub app_name: String,
     /// Whitelisted network transports for outbound connections
     pub allowed_transports: Vec<String>,
     /// Transports allowed to be mixed (tcp, tcp+tls, tor, tor+tls)
@@ -135,6 +139,7 @@ impl Default for Settings {
     fn default() -> Self {
         let version = option_env!("CARGO_PKG_VERSION").unwrap_or("0.0.0");
         let app_version = semver::Version::parse(version).unwrap();
+        let app_name = option_env!("CARGO_PKG_NAME").unwrap_or("").to_string();
 
         Self {
             node_id: String::new(),
@@ -144,6 +149,7 @@ impl Default for Settings {
             peers: vec![],
             seeds: vec![],
             app_version,
+            app_name,
             allowed_transports: vec!["tcp+tls".to_string()],
             mixed_transports: vec![],
             tor_socks5_proxy: None,
@@ -344,18 +350,24 @@ pub struct SettingsOpt {
     pub ban_policy: BanPolicy,
 }
 
-impl From<SettingsOpt> for Settings {
-    fn from(opt: SettingsOpt) -> Self {
+impl TryFrom<(&str, &str, SettingsOpt)> for Settings {
+    type Error = Error;
+    fn try_from(st: (&str, &str, SettingsOpt)) -> Result<Self> {
+        let app_name = st.0.to_string();
+        let app_version = semver::Version::parse(&st.1)?;
+        let opt = st.2;
+
         let def = Settings::default();
 
-        Self {
+        Ok(Self {
             node_id: opt.node_id,
             inbound_addrs: opt.inbound,
             external_addrs: opt.external_addrs,
             magic_bytes: opt.magic_bytes,
             peers: opt.peers,
             seeds: opt.seeds,
-            app_version: def.app_version,
+            app_version,
+            app_name,
             allowed_transports: opt.allowed_transports.unwrap_or(def.allowed_transports),
             mixed_transports: opt.mixed_transports.unwrap_or(def.mixed_transports),
             tor_socks5_proxy: opt.tor_socks5_proxy,
@@ -393,6 +405,6 @@ impl From<SettingsOpt> for Settings {
                 .unwrap_or(def.time_with_no_connections),
             blacklist: opt.blacklist,
             ban_policy: opt.ban_policy,
-        }
+        })
     }
 }