Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify example code #175

Merged
merged 2 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ sodiumoxide = { version = "0.2", optional = true }
byteorder = { version = "1.4", optional = true }

[dev-dependencies]
clap = "4.0"
criterion = "0.5"
serde = "1.0"
serde_json = "1.0"
Expand Down
19 changes: 9 additions & 10 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
//! as `cargo run --example simple` to see the magic happen.

use lazy_static::lazy_static;

use clap::{arg, Command};
use snow::{params::NoiseParams, Builder};
use std::{
io::{self, Read, Write},
Expand All @@ -25,9 +23,10 @@ lazy_static! {

#[cfg(any(feature = "default-resolver", feature = "ring-accelerated"))]
fn main() {
let matches = Command::new("simple").arg(arg!(-s --server "Server mode")).get_matches();
let server_mode =
std::env::args().next_back().map(|arg| arg == "-s" || arg == "--server").unwrap_or(true);

if matches.contains_id("server") {
if server_mode {
run_server();
} else {
run_client();
Expand All @@ -40,7 +39,7 @@ fn run_server() {
let mut buf = vec![0u8; 65535];

// Initialize our responder using a builder.
let builder: Builder<'_> = Builder::new(PARAMS.clone());
let builder = Builder::new(PARAMS.clone());
let static_key = builder.generate_keypair().unwrap().private;
let mut noise =
builder.local_private_key(&static_key).psk(3, SECRET).build_responder().unwrap();
Expand All @@ -53,7 +52,7 @@ fn run_server() {
noise.read_message(&recv(&mut stream).unwrap(), &mut buf).unwrap();

// -> e, ee, s, es
let len = noise.write_message(&[0u8; 0], &mut buf).unwrap();
let len = noise.write_message(&[], &mut buf).unwrap();
send(&mut stream, &buf[..len]);

// <- s, se
Expand All @@ -74,7 +73,7 @@ fn run_client() {
let mut buf = vec![0u8; 65535];

// Initialize our initiator using a builder.
let builder: Builder<'_> = Builder::new(PARAMS.clone());
let builder = Builder::new(PARAMS.clone());
let static_key = builder.generate_keypair().unwrap().private;
let mut noise =
builder.local_private_key(&static_key).psk(3, SECRET).build_initiator().unwrap();
Expand Down Expand Up @@ -109,16 +108,16 @@ fn run_client() {
fn recv(stream: &mut TcpStream) -> io::Result<Vec<u8>> {
let mut msg_len_buf = [0u8; 2];
stream.read_exact(&mut msg_len_buf)?;
let msg_len = ((msg_len_buf[0] as usize) << 8) + (msg_len_buf[1] as usize);
let msg_len = usize::from(u16::from_be_bytes(msg_len_buf));
let mut msg = vec![0u8; msg_len];
stream.read_exact(&mut msg[..])?;
Ok(msg)
}

/// Hyper-basic stream transport sender. 16-bit BE size followed by payload.
fn send(stream: &mut TcpStream, buf: &[u8]) {
let msg_len_buf = [(buf.len() >> 8) as u8, (buf.len() & 0xff) as u8];
stream.write_all(&msg_len_buf).unwrap();
let len = u16::try_from(buf.len()).expect("message too large");
stream.write_all(&len.to_be_bytes()).unwrap();
stream.write_all(buf).unwrap();
}

Expand Down
Loading