transport/ethernet: rename discovery module to neighbor

Rename the ethernet link-local neighbor-beacon subsystem from the
overloaded "discovery" vocabulary to a neighbor umbrella. The beacon
frame vocabulary is retained (build_beacon/parse_beacon/BEACON_SIZE/
FRAME_TYPE_BEACON/FRAME_TYPE_DATA); only the subsystem and buffer
identifiers change: DiscoveryBuffer becomes NeighborBuffer,
discovery_buffer becomes neighbor_buffer, and DISCOVERY_VERSION becomes
BEACON_VERSION (wire value 0x01 unchanged). Config toggles are left for
a follow-up commit. Behavior-neutral.
This commit is contained in:
Johnathan Corgan
2026-07-11 22:56:05 +00:00
parent 6c9f55ea80
commit e362ab67a6
2 changed files with 31 additions and 31 deletions
+13 -13
View File
@@ -6,8 +6,8 @@
//! 802.11 transparently on Linux). //! 802.11 transparently on Linux).
pub mod addr; pub mod addr;
pub mod discovery;
pub mod io; pub mod io;
pub mod neighbor;
pub mod stats; pub mod stats;
pub use addr::parse_mac_string; pub use addr::parse_mac_string;
@@ -17,8 +17,8 @@ use super::{
TransportId, TransportState, TransportType, TransportId, TransportState, TransportType,
}; };
use crate::config::EthernetConfig; use crate::config::EthernetConfig;
use discovery::{DiscoveryBuffer, FRAME_TYPE_BEACON, FRAME_TYPE_DATA, build_beacon, parse_beacon};
use io::{AsyncPacketSocket, ETHERNET_BROADCAST, PacketSocket}; use io::{AsyncPacketSocket, ETHERNET_BROADCAST, PacketSocket};
use neighbor::{FRAME_TYPE_BEACON, FRAME_TYPE_DATA, NeighborBuffer, build_beacon, parse_beacon};
use stats::EthernetStats; use stats::EthernetStats;
use secp256k1::XOnlyPublicKey; use secp256k1::XOnlyPublicKey;
@@ -56,8 +56,8 @@ pub struct EthernetTransport {
/// (`[type:1][length:2 LE][payload]`). The 2-byte length field is required /// (`[type:1][length:2 LE][payload]`). The 2-byte length field is required
/// to trim NIC minimum-frame padding before AEAD verification. /// to trim NIC minimum-frame padding before AEAD verification.
effective_mtu: u16, effective_mtu: u16,
/// Discovery buffer for discovered peers. /// Neighbor buffer for discovered peers.
discovery_buffer: Arc<DiscoveryBuffer>, neighbor_buffer: Arc<NeighborBuffer>,
/// Transport-level statistics. /// Transport-level statistics.
stats: Arc<EthernetStats>, stats: Arc<EthernetStats>,
/// Node's public key for beacon construction. /// Node's public key for beacon construction.
@@ -73,7 +73,7 @@ impl EthernetTransport {
packet_tx: PacketTx, packet_tx: PacketTx,
) -> Self { ) -> Self {
let interface = config.interface.clone(); let interface = config.interface.clone();
let discovery_buffer = Arc::new(DiscoveryBuffer::new(transport_id)); let neighbor_buffer = Arc::new(NeighborBuffer::new(transport_id));
let stats = Arc::new(EthernetStats::new()); let stats = Arc::new(EthernetStats::new());
Self { Self {
@@ -88,7 +88,7 @@ impl EthernetTransport {
local_mac: None, local_mac: None,
interface, interface,
effective_mtu: 1499, // default, updated on start effective_mtu: 1499, // default, updated on start
discovery_buffer, neighbor_buffer,
stats, stats,
local_pubkey: None, local_pubkey: None,
} }
@@ -164,7 +164,7 @@ impl EthernetTransport {
let packet_tx = self.packet_tx.clone(); let packet_tx = self.packet_tx.clone();
let mtu = self.effective_mtu; let mtu = self.effective_mtu;
let discovery_enabled = self.config.discovery(); let discovery_enabled = self.config.discovery();
let discovery_buffer = self.discovery_buffer.clone(); let neighbor_buffer = self.neighbor_buffer.clone();
let stats = self.stats.clone(); let stats = self.stats.clone();
let recv_socket = socket.clone(); let recv_socket = socket.clone();
@@ -175,7 +175,7 @@ impl EthernetTransport {
packet_tx, packet_tx,
mtu, mtu,
discovery_enabled, discovery_enabled,
discovery_buffer, neighbor_buffer,
stats, stats,
) )
.await; .await;
@@ -368,7 +368,7 @@ impl Transport for EthernetTransport {
} }
fn discover(&self) -> Result<Vec<DiscoveredPeer>, TransportError> { fn discover(&self) -> Result<Vec<DiscoveredPeer>, TransportError> {
Ok(self.discovery_buffer.take()) Ok(self.neighbor_buffer.take())
} }
fn auto_connect(&self) -> bool { fn auto_connect(&self) -> bool {
@@ -391,7 +391,7 @@ async fn ethernet_receive_loop(
packet_tx: PacketTx, packet_tx: PacketTx,
mtu: u16, mtu: u16,
discovery_enabled: bool, discovery_enabled: bool,
discovery_buffer: Arc<DiscoveryBuffer>, neighbor_buffer: Arc<NeighborBuffer>,
stats: Arc<EthernetStats>, stats: Arc<EthernetStats>,
) { ) {
// Buffer with headroom: frame type prefix + MTU + some extra // Buffer with headroom: frame type prefix + MTU + some extra
@@ -449,11 +449,11 @@ async fn ethernet_receive_loop(
stats.record_beacon_recv(); stats.record_beacon_recv();
if discovery_enabled && let Some(pubkey) = parse_beacon(&buf[..len]) { if discovery_enabled && let Some(pubkey) = parse_beacon(&buf[..len]) {
discovery_buffer.add_peer(src_mac, pubkey); neighbor_buffer.add_peer(src_mac, pubkey);
trace!( trace!(
transport_id = %transport_id, transport_id = %transport_id,
remote_mac = %format_mac(&src_mac), remote_mac = %format_mac(&src_mac),
"Discovery beacon received" "Neighbor beacon received"
); );
} }
} }
@@ -735,6 +735,6 @@ mod tests {
#[test] #[test]
fn test_beacon_size() { fn test_beacon_size() {
assert_eq!(discovery::BEACON_SIZE, 34); assert_eq!(neighbor::BEACON_SIZE, 34);
} }
} }
@@ -1,18 +1,18 @@
//! Ethernet LAN discovery via broadcast beacons. //! Ethernet LAN neighbor detection via broadcast beacons.
//! //!
//! Beacon format (34 bytes total): //! Beacon format (34 bytes total):
//! - `0x01` (1 byte): frame type = discovery announcement //! - `0x01` (1 byte): frame type = neighbor beacon
//! - `0x01` (1 byte): discovery protocol version //! - `0x01` (1 byte): beacon protocol version
//! - x-only public key (32 bytes): node's Nostr identity //! - x-only public key (32 bytes): node's Nostr identity
use crate::transport::{DiscoveredPeer, TransportAddr, TransportId}; use crate::transport::{DiscoveredPeer, TransportAddr, TransportId};
use secp256k1::XOnlyPublicKey; use secp256k1::XOnlyPublicKey;
use std::sync::Mutex; use std::sync::Mutex;
/// Discovery protocol version. /// Beacon protocol version.
pub const DISCOVERY_VERSION: u8 = 0x01; pub const BEACON_VERSION: u8 = 0x01;
/// Frame type prefix for discovery announcement beacons. /// Frame type prefix for neighbor beacon frames.
pub const FRAME_TYPE_BEACON: u8 = 0x01; pub const FRAME_TYPE_BEACON: u8 = 0x01;
/// Frame type prefix for FIPS data frames. /// Frame type prefix for FIPS data frames.
@@ -21,16 +21,16 @@ pub const FRAME_TYPE_DATA: u8 = 0x00;
/// Total beacon payload size: type(1) + version(1) + pubkey(32). /// Total beacon payload size: type(1) + version(1) + pubkey(32).
pub const BEACON_SIZE: usize = 34; pub const BEACON_SIZE: usize = 34;
/// Build a discovery announcement beacon payload. /// Build a neighbor beacon payload.
pub fn build_beacon(pubkey: &XOnlyPublicKey) -> [u8; BEACON_SIZE] { pub fn build_beacon(pubkey: &XOnlyPublicKey) -> [u8; BEACON_SIZE] {
let mut buf = [0u8; BEACON_SIZE]; let mut buf = [0u8; BEACON_SIZE];
buf[0] = FRAME_TYPE_BEACON; buf[0] = FRAME_TYPE_BEACON;
buf[1] = DISCOVERY_VERSION; buf[1] = BEACON_VERSION;
buf[2..BEACON_SIZE].copy_from_slice(&pubkey.serialize()); buf[2..BEACON_SIZE].copy_from_slice(&pubkey.serialize());
buf buf
} }
/// Parse a discovery announcement beacon payload. /// Parse a neighbor beacon payload.
/// ///
/// Returns the sender's public key, or None if the payload is invalid. /// Returns the sender's public key, or None if the payload is invalid.
pub fn parse_beacon(data: &[u8]) -> Option<XOnlyPublicKey> { pub fn parse_beacon(data: &[u8]) -> Option<XOnlyPublicKey> {
@@ -40,20 +40,20 @@ pub fn parse_beacon(data: &[u8]) -> Option<XOnlyPublicKey> {
if data[0] != FRAME_TYPE_BEACON { if data[0] != FRAME_TYPE_BEACON {
return None; return None;
} }
if data[1] != DISCOVERY_VERSION { if data[1] != BEACON_VERSION {
return None; return None;
} }
XOnlyPublicKey::from_slice(&data[2..34]).ok() XOnlyPublicKey::from_slice(&data[2..34]).ok()
} }
/// Buffer for discovered peers, drained by `discover()`. /// Buffer for discovered peers, drained by `discover()`.
pub struct DiscoveryBuffer { pub struct NeighborBuffer {
transport_id: TransportId, transport_id: TransportId,
peers: Mutex<Vec<DiscoveredPeer>>, peers: Mutex<Vec<DiscoveredPeer>>,
} }
impl DiscoveryBuffer { impl NeighborBuffer {
/// Create a new empty discovery buffer. /// Create a new empty neighbor buffer.
pub fn new(transport_id: TransportId) -> Self { pub fn new(transport_id: TransportId) -> Self {
Self { Self {
transport_id, transport_id,
@@ -101,7 +101,7 @@ mod tests {
assert_eq!(beacon.len(), BEACON_SIZE); assert_eq!(beacon.len(), BEACON_SIZE);
assert_eq!(beacon[0], FRAME_TYPE_BEACON); assert_eq!(beacon[0], FRAME_TYPE_BEACON);
assert_eq!(beacon[1], DISCOVERY_VERSION); assert_eq!(beacon[1], BEACON_VERSION);
let parsed = parse_beacon(&beacon).unwrap(); let parsed = parse_beacon(&beacon).unwrap();
assert_eq!(parsed, pubkey); assert_eq!(parsed, pubkey);
@@ -134,8 +134,8 @@ mod tests {
} }
#[test] #[test]
fn test_discovery_buffer() { fn test_neighbor_buffer() {
let buffer = DiscoveryBuffer::new(TransportId::new(1)); let buffer = NeighborBuffer::new(TransportId::new(1));
let pubkey = test_pubkey(); let pubkey = test_pubkey();
let mac = [0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]; let mac = [0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff];
@@ -152,8 +152,8 @@ mod tests {
} }
#[test] #[test]
fn test_discovery_buffer_dedup() { fn test_neighbor_buffer_dedup() {
let buffer = DiscoveryBuffer::new(TransportId::new(1)); let buffer = NeighborBuffer::new(TransportId::new(1));
let pubkey = test_pubkey(); let pubkey = test_pubkey();
let mac = [0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]; let mac = [0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff];