Commit 369554d8 authored by Jasper Wallace's avatar Jasper Wallace

initial working egd sort of thing

parents
target
src/main.rs.bk
*~
[root]
name = "egd"
version = "0.1.0"
[package]
name = "egd"
version = "0.1.0"
authors = ["Jasper Wallace <jasper@pointless.net>"]
[dependencies]
// listen on ip:port
// accept connections
// read from connextions
// protocol is "\x02\x80" <- request rnd stuffs
// write 128 size blocks of random
// each client wants 4 blocks by default, range is 1-4
//
// 0x20x80 is EGD_CMD_BLOCKREAD, 128 bytes
//
//
//
// local EGD_CMD_QUERYPOOL = 0
//
// reutrns u32 of available entropy in bits
//
// local EGD_CMD_READBYTES = 1
//
// return u8 nbytes or entropy
//
// local EGD_CMD_BLOCKREAD = 2
//
// return u8 nbytes or entropy (blocking)
//
// local EGD_CMD_ADDENTROPY = 3
//
// Arguments: U16 shannons, U8 bytes, STR
//
// local EGD_CMD_GETPID = 4
//
// Return U8 slen, STR pidstr
//
//
use std::net::{TcpListener, TcpStream};
use std::thread;
use std::io::prelude::*;
use std::fs::File;
fn main() {
let listener = TcpListener::bind("127.0.0.1:8888").unwrap();
fn handle_client(mut stream: TcpStream) {
println!("Got connection: {} -> {}",
stream.peer_addr().unwrap(),
stream.local_addr().unwrap());
loop {
let mut buf = [0; 1];
let read = match stream.read(&mut buf) {
Err(e) => panic!("Got an error: {}", e),
Ok(m) => m,
};
if read < 1 {
return;
}
match buf[0] {
0 => println!("got querypool"),
1 => println!("got readbytes"),
2 => {
print!("got blockread ");
let read = match stream.read(&mut buf) {
Err(e) => panic!("Got an error: {}", e),
Ok(m) => m,
};
if read < 1 {
return;
} else {
let blocksize: u8 = buf[0];
println!("for {} bytes", blocksize);
let mut wrbuf = vec![0; usize::from(blocksize)];
// open /dev/urandom
let mut randfd = match File::open("/dev/urandom") {
Ok(f) => f,
Err(err) => {
println!("Error, unable to open /dev/urandom: {}", err);
return;
}
};
let got = match randfd.read(&mut wrbuf) {
Ok(s) => s,
Err(err) => {
println!("Error, unable to read {} from /dev/urandom: {}",
blocksize,
err);
return;
}
};
stream.write(&wrbuf).unwrap();
}
}
3 => println!("got addentropy"),
4 => println!("got getpid"),
_ => println!("got ???"),
}
}
}
// accept connections and process them, spawning a new thread for each one
for stream in listener.incoming() {
match stream {
Ok(stream) => {
thread::spawn(move || {
// connection succeeded
handle_client(stream)
});
}
Err(e) => {
// connection failed
panic!("Got an error: {}", e)
}
}
}
// close the socket server
drop(listener);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment