message.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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, AsyncDecodable, AsyncEncodable, SerialDecodable, SerialEncodable,
  20. };
  21. use url::Url;
  22. pub(in crate::net) const MAGIC_BYTES: [u8; 4] = [0xd9, 0xef, 0xb6, 0x7d];
  23. /// Generic message template.
  24. pub trait Message: 'static + Send + Sync + AsyncDecodable + AsyncEncodable {
  25. const NAME: &'static str;
  26. }
  27. #[macro_export]
  28. macro_rules! impl_p2p_message {
  29. ($st:ty, $nm:expr) => {
  30. impl Message for $st {
  31. const NAME: &'static str = $nm;
  32. }
  33. };
  34. }
  35. /// Outbound keepalive message.
  36. #[derive(Debug, Copy, Clone, SerialEncodable, SerialDecodable)]
  37. pub struct PingMessage {
  38. pub nonce: u16,
  39. }
  40. impl_p2p_message!(PingMessage, "ping");
  41. /// Inbound keepalive message.
  42. #[derive(Debug, Copy, Clone, SerialEncodable, SerialDecodable)]
  43. pub struct PongMessage {
  44. pub nonce: u16,
  45. }
  46. impl_p2p_message!(PongMessage, "pong");
  47. /// Requests address of outbound connecction.
  48. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  49. pub struct GetAddrsMessage {
  50. /// Maximum number of addresses with preferred
  51. /// transports to receive. Response vector will
  52. /// also containg addresses without the preferred
  53. /// transports, so its size will be 2 * max.
  54. pub max: u32,
  55. /// Preferred addresses transports
  56. pub transports: Vec<String>,
  57. }
  58. impl_p2p_message!(GetAddrsMessage, "getaddr");
  59. /// Sends address information to inbound connection.
  60. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  61. pub struct AddrsMessage {
  62. pub addrs: Vec<(Url, u64)>,
  63. }
  64. impl_p2p_message!(AddrsMessage, "addr");
  65. /// Requests version information of outbound connection.
  66. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  67. pub struct VersionMessage {
  68. /// Only used for debugging. Compromises privacy when set.
  69. pub node_id: String,
  70. /// Identifies protocol version being used by the node
  71. pub version: semver::Version,
  72. /// UNIX timestamp of when the VersionMessage was created.
  73. pub timestamp: u64,
  74. /// Network address of the node receiving this message (before
  75. /// resolving).
  76. pub connect_recv_addr: Url,
  77. /// Network address of the node receiving this message (after
  78. /// resolving). Optional because only used by outbound connections.
  79. pub resolve_recv_addr: Option<Url>,
  80. /// External address of the sender node, if it exists (empty
  81. /// otherwise).
  82. pub ext_send_addr: Vec<Url>,
  83. /// List of features consisting of a tuple of (services, version)
  84. /// to be enabled for this connection
  85. pub features: Vec<(String, u32)>,
  86. }
  87. impl_p2p_message!(VersionMessage, "version");
  88. /// Sends version information to inbound connection.
  89. /// Response to `VersionMessage`.
  90. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  91. pub struct VerackMessage {
  92. /// App version
  93. pub app_version: semver::Version,
  94. }
  95. impl_p2p_message!(VerackMessage, "verack");