src: add simple broadcast listening logic

This commit is contained in:
Ujjwal Sharma 2021-03-28 11:03:53 +05:30
parent 4111aed599
commit 1f2635d845
Signed by untrusted user: ryzokuken
GPG Key ID: 460B292812C67D9F
1 changed files with 30 additions and 0 deletions

View File

@ -1,6 +1,7 @@
use clap::{load_yaml, App};
use ed25519_dalek::Keypair;
use std::net::UdpSocket;
use std::path::PathBuf;
mod keypair;
@ -9,6 +10,28 @@ use keypair::SSBKeypair;
type Config = toml::map::Map<String, toml::Value>;
#[derive(Debug)]
struct Host {
protocol: String,
host: String,
port: String,
pubkey: String,
}
fn parse_packet(packet: String) -> Host {
let mut packet = packet.splitn(4, ':');
let protocol = packet.next().unwrap();
let host = packet.next().unwrap();
let port = packet.next().unwrap().splitn(2, '~').next().unwrap();
let pubkey = packet.next().unwrap();
Host {
protocol: protocol.to_string(),
host: host.to_string(),
port: port.to_string(),
pubkey: pubkey.to_string(),
}
}
fn main() {
let options = load_yaml!("options.yaml");
let options = App::from(options).get_matches();
@ -32,4 +55,11 @@ fn main() {
};
let keypair = Keypair::read_or_generate(path.join("secret"));
println!("{}", keypair.to_json().pretty(2));
let socket = UdpSocket::bind("0.0.0.0:8008").unwrap();
let mut buf = [0; 1024];
let (amt, _) = socket.recv_from(&mut buf).unwrap();
let buf = &mut buf[..amt];
let packet = String::from_utf8(buf.to_vec()).unwrap();
println!("{:?}", parse_packet(packet));
}