2021-11-26 21:46:27 +00:00
|
|
|
use std::{
|
2021-12-05 20:15:01 +00:00
|
|
|
collections::HashMap,
|
2021-11-26 21:46:27 +00:00
|
|
|
env,
|
|
|
|
net::{
|
|
|
|
Ipv4Addr,
|
2021-12-05 19:58:18 +00:00
|
|
|
SocketAddr,
|
2021-12-05 03:39:53 +00:00
|
|
|
SocketAddrV4,
|
2021-11-26 21:46:27 +00:00
|
|
|
UdpSocket,
|
|
|
|
},
|
2021-12-08 20:54:01 +00:00
|
|
|
str::FromStr,
|
2021-12-05 20:15:01 +00:00
|
|
|
time::{Duration, Instant},
|
2021-11-26 21:46:27 +00:00
|
|
|
};
|
|
|
|
|
2021-12-05 20:15:01 +00:00
|
|
|
use mac_address::{
|
|
|
|
MacAddress,
|
|
|
|
get_mac_address,
|
|
|
|
};
|
2021-12-05 03:39:53 +00:00
|
|
|
use thiserror::Error;
|
|
|
|
|
2021-12-08 22:45:39 +00:00
|
|
|
mod ip;
|
2021-12-05 19:58:18 +00:00
|
|
|
mod message;
|
2021-12-05 05:01:25 +00:00
|
|
|
mod tlv;
|
|
|
|
|
2021-12-05 19:58:18 +00:00
|
|
|
use message::{
|
|
|
|
PACKET_SIZE,
|
|
|
|
Message,
|
|
|
|
};
|
|
|
|
|
2021-12-05 03:39:53 +00:00
|
|
|
#[derive (Debug, Error)]
|
2021-12-08 22:45:39 +00:00
|
|
|
pub enum AppError {
|
2021-12-08 20:54:01 +00:00
|
|
|
#[error (transparent)]
|
|
|
|
AddrParse (#[from] std::net::AddrParseError),
|
2021-12-05 03:39:53 +00:00
|
|
|
#[error (transparent)]
|
|
|
|
CliArgs (#[from] CliArgError),
|
2021-12-05 19:58:18 +00:00
|
|
|
#[error (transparent)]
|
2021-12-08 22:45:39 +00:00
|
|
|
FromUtf8 (#[from] std::string::FromUtf8Error),
|
|
|
|
#[error (transparent)]
|
2021-12-05 19:58:18 +00:00
|
|
|
Io (#[from] std::io::Error),
|
|
|
|
#[error (transparent)]
|
|
|
|
MacAddr (#[from] mac_address::MacAddressError),
|
|
|
|
#[error (transparent)]
|
|
|
|
Message (#[from] message::MessageError),
|
|
|
|
#[error (transparent)]
|
|
|
|
Tlv (#[from] tlv::TlvError),
|
2021-12-08 22:04:45 +00:00
|
|
|
#[error (transparent)]
|
|
|
|
Utf8 (#[from] std::str::Utf8Error),
|
2021-12-05 03:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive (Debug, Error)]
|
2021-12-08 22:45:39 +00:00
|
|
|
pub enum CliArgError {
|
2021-12-08 02:10:58 +00:00
|
|
|
#[error ("Missing value for argument `{0}`")]
|
|
|
|
MissingArgumentValue (String),
|
2021-12-05 03:39:53 +00:00
|
|
|
#[error ("First argument should be a subcommand")]
|
|
|
|
MissingSubcommand,
|
|
|
|
#[error ("Unknown subcommand `{0}`")]
|
|
|
|
UnknownSubcommand (String),
|
2021-12-08 02:10:58 +00:00
|
|
|
#[error ("Unrecognized argument `{0}`")]
|
|
|
|
UnrecognizedArgument (String),
|
2021-12-05 03:39:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct CommonParams {
|
|
|
|
// Servers bind on this port, clients must send to the port
|
|
|
|
server_port: u16,
|
|
|
|
|
|
|
|
// Clients and servers will all join the same multicast addr
|
|
|
|
multicast_addr: Ipv4Addr,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for CommonParams {
|
|
|
|
fn default () -> Self {
|
|
|
|
Self {
|
|
|
|
server_port: 9040,
|
|
|
|
multicast_addr: Ipv4Addr::new (225, 100, 99, 98),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-26 21:46:27 +00:00
|
|
|
|
2021-12-05 03:39:53 +00:00
|
|
|
fn main () -> Result <(), AppError> {
|
2021-12-08 23:55:22 +00:00
|
|
|
let rt = tokio::runtime::Builder::new_current_thread ().build ()?;
|
|
|
|
|
|
|
|
rt.block_on (async_main ())?;
|
|
|
|
|
|
|
|
Ok (())
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn async_main () -> Result <(), AppError> {
|
2021-11-26 21:46:27 +00:00
|
|
|
let mut args = env::args ();
|
|
|
|
|
|
|
|
let _exe_name = args.next ();
|
|
|
|
|
2021-12-05 03:46:56 +00:00
|
|
|
match get_mac_address() {
|
|
|
|
Ok(Some(ma)) => {
|
2021-12-05 20:15:01 +00:00
|
|
|
println!("Our MAC addr = {}", ma);
|
2021-12-05 03:46:56 +00:00
|
|
|
}
|
|
|
|
Ok(None) => println!("No MAC address found."),
|
|
|
|
Err(e) => println!("{:?}", e),
|
|
|
|
}
|
|
|
|
|
2021-12-07 03:18:11 +00:00
|
|
|
let subcommand: Option <String> = args.next ();
|
|
|
|
|
|
|
|
match subcommand.as_ref ().map (|x| &x[..]) {
|
2021-12-05 19:58:18 +00:00
|
|
|
None => return Err (CliArgError::MissingSubcommand.into ()),
|
2021-12-08 20:54:01 +00:00
|
|
|
Some ("client") => client (args)?,
|
2021-12-08 22:04:45 +00:00
|
|
|
Some ("my-ips") => my_ips ()?,
|
2021-12-08 00:26:13 +00:00
|
|
|
Some ("server") => server (args)?,
|
2021-12-05 19:58:18 +00:00
|
|
|
Some (x) => return Err (CliArgError::UnknownSubcommand (x.to_string ()).into ()),
|
2021-11-26 21:46:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok (())
|
|
|
|
}
|
|
|
|
|
2021-12-08 22:04:45 +00:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
fn my_ips () -> Result <(), AppError> {
|
2021-12-09 00:11:47 +00:00
|
|
|
let output = ip::linux::get_ip_addr_output ()?;
|
2021-12-08 22:45:39 +00:00
|
|
|
|
2021-12-09 00:11:47 +00:00
|
|
|
for addr in ip::linux::parse_ip_addr_output (&output)
|
2021-12-08 22:45:39 +00:00
|
|
|
.iter ()
|
|
|
|
.filter (|a| ! a.is_loopback ())
|
|
|
|
{
|
|
|
|
println! ("{:?}", addr);
|
|
|
|
}
|
|
|
|
|
2021-12-08 22:04:45 +00:00
|
|
|
Ok (())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "macos")]
|
|
|
|
fn my_ips () -> Result <(), AppError> {
|
|
|
|
println! ("my-ips subcommand not implemented for macos");
|
|
|
|
Ok (())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
fn my_ips () -> Result <(), AppError> {
|
2021-12-09 00:11:47 +00:00
|
|
|
let output = ip::windows::get_ip_config_output ()?;
|
2021-12-08 22:04:45 +00:00
|
|
|
|
2021-12-09 00:11:47 +00:00
|
|
|
for addr in ip::windows::parse_ip_config_output (&output) {
|
2021-12-08 22:04:45 +00:00
|
|
|
println! ("{:?}", addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok (())
|
|
|
|
}
|
|
|
|
|
2021-12-08 02:10:58 +00:00
|
|
|
struct ServerResponse {
|
|
|
|
mac: Option <[u8; 6]>,
|
|
|
|
nickname: Option <String>,
|
|
|
|
}
|
|
|
|
|
2021-12-08 20:54:01 +00:00
|
|
|
fn client <I : Iterator <Item=String>> (mut args: I) -> Result <(), AppError> {
|
2021-12-05 20:40:37 +00:00
|
|
|
use rand::RngCore;
|
|
|
|
|
2021-12-08 00:26:13 +00:00
|
|
|
let mut common_params = CommonParams::default ();
|
2021-12-08 20:54:01 +00:00
|
|
|
let mut bind_addr = "0.0.0.0".to_string ();
|
|
|
|
|
|
|
|
while let Some (arg) = args.next () {
|
|
|
|
match arg.as_str () {
|
|
|
|
"--bind-addr" => {
|
|
|
|
bind_addr = match args.next () {
|
|
|
|
None => return Err (CliArgError::MissingArgumentValue (arg).into ()),
|
|
|
|
Some (x) => x
|
|
|
|
};
|
|
|
|
},
|
|
|
|
_ => return Err (CliArgError::UnrecognizedArgument (arg).into ()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let socket = UdpSocket::bind (&format! ("{}:0", bind_addr))?;
|
2021-11-26 21:46:27 +00:00
|
|
|
|
2021-12-08 20:54:01 +00:00
|
|
|
socket.join_multicast_v4 (&common_params.multicast_addr, &Ipv4Addr::from_str (&bind_addr)?)?;
|
2021-12-05 20:15:01 +00:00
|
|
|
socket.set_read_timeout (Some (Duration::from_millis (1_000)))?;
|
2021-11-26 21:46:27 +00:00
|
|
|
|
2021-12-05 20:40:37 +00:00
|
|
|
let mut idem_id = [0u8; 8];
|
|
|
|
rand::thread_rng ().fill_bytes (&mut idem_id);
|
|
|
|
|
2021-12-08 02:10:58 +00:00
|
|
|
let msg = Message::Request1 {
|
2021-12-05 20:40:37 +00:00
|
|
|
idem_id,
|
|
|
|
mac: None,
|
|
|
|
}.to_vec ()?;
|
|
|
|
|
2021-12-05 20:42:56 +00:00
|
|
|
for _ in 0..10 {
|
2021-12-07 03:18:11 +00:00
|
|
|
socket.send_to (&msg, (common_params.multicast_addr, common_params.server_port))?;
|
2021-12-05 20:42:56 +00:00
|
|
|
std::thread::sleep (Duration::from_millis (100));
|
2021-12-05 20:40:37 +00:00
|
|
|
}
|
2021-12-05 20:15:01 +00:00
|
|
|
|
|
|
|
let start_time = Instant::now ();
|
|
|
|
|
|
|
|
let mut peers = HashMap::with_capacity (10);
|
|
|
|
|
|
|
|
while Instant::now () < start_time + Duration::from_secs (2) {
|
2021-12-08 02:10:58 +00:00
|
|
|
let (msgs, remote_addr) = match recv_msg_from (&socket) {
|
2021-12-05 20:15:01 +00:00
|
|
|
Err (_) => continue,
|
|
|
|
Ok (x) => x,
|
|
|
|
};
|
|
|
|
|
2021-12-08 02:10:58 +00:00
|
|
|
let mut resp = ServerResponse {
|
|
|
|
mac: None,
|
|
|
|
nickname: None,
|
2021-12-05 20:15:01 +00:00
|
|
|
};
|
|
|
|
|
2021-12-08 02:10:58 +00:00
|
|
|
for msg in msgs.into_iter () {
|
|
|
|
match msg {
|
|
|
|
Message::Response1 (x) => resp.mac = x,
|
|
|
|
Message::Response2 (x) => resp.nickname = Some (x.nickname),
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
peers.insert (remote_addr, resp);
|
2021-12-05 20:15:01 +00:00
|
|
|
}
|
2021-12-05 19:58:18 +00:00
|
|
|
|
2021-12-05 20:15:01 +00:00
|
|
|
let mut peers: Vec <_> = peers.into_iter ().collect ();
|
2021-12-08 02:10:58 +00:00
|
|
|
peers.sort_by_key (|(k, v)| v.mac);
|
2021-11-26 21:46:27 +00:00
|
|
|
|
2021-12-05 20:15:01 +00:00
|
|
|
println! ("Found {} peers:", peers.len ());
|
2021-12-08 02:10:58 +00:00
|
|
|
for (ip, resp) in peers.into_iter () {
|
|
|
|
let mac = match resp.mac {
|
|
|
|
None => {
|
|
|
|
println! ("<Unknown> = {}", ip);
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
Some (x) => x,
|
|
|
|
};
|
|
|
|
|
|
|
|
let nickname = match resp.nickname {
|
|
|
|
None => {
|
|
|
|
println! ("{} = {}", MacAddress::new (mac), ip.ip ());
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
Some (x) => x,
|
|
|
|
};
|
|
|
|
|
|
|
|
println! ("{} = {} `{}`", MacAddress::new (mac), ip.ip (), nickname);
|
2021-12-05 20:15:01 +00:00
|
|
|
}
|
2021-12-05 03:43:12 +00:00
|
|
|
|
2021-11-26 21:46:27 +00:00
|
|
|
Ok (())
|
|
|
|
}
|
|
|
|
|
2021-12-08 02:10:58 +00:00
|
|
|
fn server <I: Iterator <Item=String>> (mut args: I) -> Result <(), AppError>
|
2021-12-07 03:18:11 +00:00
|
|
|
{
|
2021-12-08 00:26:13 +00:00
|
|
|
let mut common_params = CommonParams::default ();
|
2021-12-08 20:54:01 +00:00
|
|
|
let mut bind_addr = "0.0.0.0".to_string ();
|
2021-12-08 00:26:13 +00:00
|
|
|
let mut nickname = String::new ();
|
|
|
|
|
2021-12-08 02:10:58 +00:00
|
|
|
while let Some (arg) = args.next () {
|
|
|
|
match arg.as_str () {
|
2021-12-08 20:54:01 +00:00
|
|
|
"--bind-addr" => {
|
|
|
|
bind_addr = match args.next () {
|
|
|
|
None => return Err (CliArgError::MissingArgumentValue (arg).into ()),
|
|
|
|
Some (x) => x
|
|
|
|
};
|
|
|
|
},
|
2021-12-08 02:10:58 +00:00
|
|
|
"--nickname" => {
|
|
|
|
nickname = match args.next () {
|
|
|
|
None => return Err (CliArgError::MissingArgumentValue (arg).into ()),
|
|
|
|
Some (x) => x
|
|
|
|
};
|
|
|
|
},
|
|
|
|
_ => return Err (CliArgError::UnrecognizedArgument (arg).into ()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-05 19:58:18 +00:00
|
|
|
let our_mac = get_mac_address ()?.map (|x| x.bytes ());
|
|
|
|
if our_mac.is_none () {
|
|
|
|
println! ("Warning: Can't find our own MAC address. We won't be able to respond to MAC-specific lookaround requests");
|
|
|
|
}
|
|
|
|
|
2021-12-08 20:54:01 +00:00
|
|
|
let socket = UdpSocket::bind (SocketAddrV4::new (Ipv4Addr::from_str (&bind_addr)?, common_params.server_port)).unwrap ();
|
2021-11-26 21:46:27 +00:00
|
|
|
|
2021-12-07 03:18:11 +00:00
|
|
|
socket.join_multicast_v4 (&common_params.multicast_addr, &([0u8, 0, 0, 0].into ())).unwrap ();
|
2021-12-05 20:22:12 +00:00
|
|
|
|
2021-12-05 20:40:37 +00:00
|
|
|
let mut recent_idem_ids = Vec::with_capacity (32);
|
|
|
|
|
2021-12-05 20:15:01 +00:00
|
|
|
loop {
|
2021-12-05 20:16:47 +00:00
|
|
|
println! ("Waiting for messages...");
|
2021-12-08 02:10:58 +00:00
|
|
|
let (req_msgs, remote_addr) = recv_msg_from (&socket)?;
|
|
|
|
|
|
|
|
let req = match req_msgs.into_iter ().next () {
|
|
|
|
Some (x) => x,
|
|
|
|
_ => {
|
|
|
|
println! ("Don't know how to handle this message, ignoring");
|
|
|
|
continue;
|
|
|
|
},
|
|
|
|
};
|
2021-12-05 20:15:01 +00:00
|
|
|
|
|
|
|
let resp = match req {
|
2021-12-08 02:10:58 +00:00
|
|
|
Message::Request1 {
|
2021-12-05 20:40:37 +00:00
|
|
|
mac: None,
|
|
|
|
idem_id,
|
|
|
|
} => {
|
|
|
|
if recent_idem_ids.contains (&idem_id) {
|
|
|
|
println! ("Ignoring request we already processed");
|
|
|
|
None
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
recent_idem_ids.insert (0, idem_id);
|
|
|
|
recent_idem_ids.truncate (30);
|
2021-12-08 02:10:58 +00:00
|
|
|
Some (vec! [
|
|
|
|
Message::Response1 (our_mac),
|
|
|
|
Message::Response2 (message::Response2 {
|
|
|
|
idem_id,
|
|
|
|
nickname: nickname.clone (),
|
|
|
|
}),
|
|
|
|
])
|
2021-12-05 20:40:37 +00:00
|
|
|
}
|
2021-12-05 20:15:01 +00:00
|
|
|
},
|
|
|
|
_ => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some (resp) = resp {
|
2021-12-08 02:10:58 +00:00
|
|
|
socket.send_to (&Message::many_to_vec (&resp)?, remote_addr).unwrap ();
|
2021-12-05 20:15:01 +00:00
|
|
|
}
|
2021-12-05 19:58:18 +00:00
|
|
|
}
|
2021-11-26 21:46:27 +00:00
|
|
|
}
|
2021-12-05 19:58:18 +00:00
|
|
|
|
2021-12-08 02:10:58 +00:00
|
|
|
fn recv_msg_from (socket: &UdpSocket) -> Result <(Vec <Message>, SocketAddr), AppError>
|
2021-12-05 19:58:18 +00:00
|
|
|
{
|
|
|
|
let mut buf = vec! [0u8; PACKET_SIZE];
|
|
|
|
let (bytes_recved, remote_addr) = socket.recv_from (&mut buf)?;
|
|
|
|
buf.truncate (bytes_recved);
|
2021-12-08 02:10:58 +00:00
|
|
|
let msgs = Message::from_slice2 (&buf)?;
|
2021-12-05 19:58:18 +00:00
|
|
|
|
2021-12-08 02:10:58 +00:00
|
|
|
Ok ((msgs, remote_addr))
|
2021-12-05 19:58:18 +00:00
|
|
|
}
|