message.rs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use darkfi_serial::{
  19. async_trait, serialize_async, AsyncDecodable, AsyncEncodable, SerialDecodable, SerialEncodable,
  20. };
  21. use std::net::Ipv6Addr;
  22. use url::{Host, Url};
  23. /// Generic message template.
  24. pub trait Message: 'static + Send + Sync + AsyncDecodable + AsyncEncodable {
  25. const NAME: &'static str;
  26. }
  27. /// Generic serialized message template.
  28. pub struct SerializedMessage {
  29. pub command: String,
  30. pub payload: Vec<u8>,
  31. }
  32. impl SerializedMessage {
  33. pub async fn new<M: Message>(message: &M) -> Self {
  34. Self { command: M::NAME.to_string(), payload: serialize_async(message).await }
  35. }
  36. }
  37. #[macro_export]
  38. macro_rules! impl_p2p_message {
  39. ($st:ty, $nm:expr) => {
  40. impl Message for $st {
  41. const NAME: &'static str = $nm;
  42. }
  43. };
  44. }
  45. /// Outbound keepalive message.
  46. #[derive(Debug, Copy, Clone, SerialEncodable, SerialDecodable)]
  47. pub struct PingMessage {
  48. pub nonce: u16,
  49. }
  50. impl_p2p_message!(PingMessage, "ping");
  51. /// Inbound keepalive message.
  52. #[derive(Debug, Copy, Clone, SerialEncodable, SerialDecodable)]
  53. pub struct PongMessage {
  54. pub nonce: u16,
  55. }
  56. impl_p2p_message!(PongMessage, "pong");
  57. /// Requests address of outbound connecction.
  58. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  59. pub struct GetAddrsMessage {
  60. /// Maximum number of addresses with preferred
  61. /// transports to receive. Response vector will
  62. /// also containg addresses without the preferred
  63. /// transports, so its size will be 2 * max.
  64. pub max: u32,
  65. /// Preferred addresses transports
  66. pub transports: Vec<String>,
  67. }
  68. impl_p2p_message!(GetAddrsMessage, "getaddr");
  69. /// Sends address information to inbound connection.
  70. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  71. pub struct AddrsMessage {
  72. pub addrs: Vec<(Url, u64)>,
  73. }
  74. impl_p2p_message!(AddrsMessage, "addr");
  75. /// Requests version information of outbound connection.
  76. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  77. pub struct VersionMessage {
  78. /// Only used for debugging. Compromises privacy when set.
  79. pub node_id: String,
  80. /// Identifies protocol version being used by the node
  81. pub version: semver::Version,
  82. /// UNIX timestamp of when the VersionMessage was created.
  83. pub timestamp: u64,
  84. /// Network address of the node receiving this message (before
  85. /// resolving).
  86. pub connect_recv_addr: Url,
  87. /// Network address of the node receiving this message (after
  88. /// resolving). Optional because only used by outbound connections.
  89. pub resolve_recv_addr: Option<Url>,
  90. /// External address of the sender node, if it exists (empty
  91. /// otherwise).
  92. pub ext_send_addr: Vec<Url>,
  93. /// List of features consisting of a tuple of (services, version)
  94. /// to be enabled for this connection
  95. pub features: Vec<(String, u32)>,
  96. }
  97. impl_p2p_message!(VersionMessage, "version");
  98. impl VersionMessage {
  99. pub(in crate::net) fn get_ipv6_addr(&self) -> Option<Ipv6Addr> {
  100. let host = self.connect_recv_addr.host()?;
  101. // Check the reported address is Ipv6
  102. match host {
  103. Host::Ipv6(addr) => Some(addr),
  104. _ => None,
  105. }
  106. }
  107. }
  108. /// Sends version information to inbound connection.
  109. /// Response to `VersionMessage`.
  110. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  111. pub struct VerackMessage {
  112. /// App version
  113. pub app_version: semver::Version,
  114. }
  115. impl_p2p_message!(VerackMessage, "verack");