Skip to content
Snippets Groups Projects
Commit 358d8069 authored by chapeau's avatar chapeau
Browse files

tun première partie

parent 1b4c3e99
Branches tun
No related tags found
No related merge requests found
......@@ -8,6 +8,7 @@ use crypto_box::{PublicKey, SecretKey};
use std::net::{Ipv4Addr, SocketAddrV4};
use yaml_rust::YamlLoader;
use std::convert::TryInto;
use super::tun::TunData;
use hex;
pub mod config;
......@@ -61,8 +62,8 @@ pub fn parse_config(path: &Path) -> Result<config::Config, Box<dyn Error>> {
Ok(config_generator(&docs[0])?)
}
pub fn thread_init(config: config::Config) {
let mut tun_send: Vec<mpsc::Sender<Vec<u8>>> = Vec::new();
pub fn thread_init(config: Arc<config::Config>) {
let mut tun_send: Vec<mpsc::Sender<TunData>> = Vec::new();
let mut udp_send: Vec<mpsc::Sender<Vec<u8>>> = Vec::new();
// From peer thread to udp
......@@ -70,15 +71,15 @@ pub fn thread_init(config: config::Config) {
// From peer thread to tun
let (send_to_tun, recv_to_tun) = mpsc::channel::<Vec<u8>>();
for elt in config.peers {
for elt in &config.peers {
// From udp to peer thread
let (send_from_udp, recv_from_udp) = mpsc::channel::<Vec<u8>>();
// From tun to peer thread
let (send_from_tun, recv_from_tun) = mpsc::channel::<Vec<u8>>();
let (send_from_tun, recv_from_tun) = mpsc::channel::<TunData>();
let connexion_infos = Arc::new(Mutex::new(peer::data::ConnexionInfos{
endpoint: elt.endpoint,
pubkey: elt.pubkey,
pubkey: elt.pubkey.clone(),
privkey: config.privkey.clone(),
ip_addr: Some(elt.ip)
}));
......@@ -97,12 +98,18 @@ pub fn thread_init(config: config::Config) {
}
let iface = Arc::new(tun::init(&config.ip, &config.mask).unwrap());
let iface_copy = Arc::clone(&iface);
// TUN threads
std::thread::spawn(move || {
tun::thread::tun_recv_thread(tun_send);
tun::thread::tun_recv_thread(Arc::clone(&config), iface, tun_send);
});
std::thread::spawn(move || {
tun::thread::tun_send_thread(recv_to_tun);
tun::thread::tun_send_thread(iface_copy, recv_to_tun);
});
// UDP Threads
std::thread::spawn(move || {
udp::thread::udp_recv_thread(udp_send);
});
......
mod init;
pub mod init;
pub mod peer;
pub mod tun;
pub mod udp;
......
......@@ -14,7 +14,7 @@ use std::sync::{
pub fn sender_thread(
connexion_infos: Arc<Mutex<data::ConnexionInfos>>,
rx: Receiver<Vec<u8>>,
rx: Receiver<super::tun::TunData>,
tx: Sender<Vec<u8>>,
) {
let mut rng = OsRng;
......@@ -29,7 +29,7 @@ pub fn sender_thread(
let nonce = crypto_box::generate_nonce(&mut rng);
let mut ciphertext = Box::new(&public_key, &secret_key)
.encrypt(&nonce, &msg[..])
.encrypt(&nonce, &msg.payload[..])
.unwrap();
// On ajoute le nonce au début du message (24 octets)
......
......@@ -25,3 +25,9 @@ pub fn init(ip: &Ipv4Addr, mask: &u8) -> Result<Iface, Box<dyn Error>> {
Ok(iface)
}
pub struct TunData {
pub payload: Vec<u8>,
}
pub const TUN_PACKET_SIZE: usize = 1500;
\ No newline at end of file
use std::sync::mpsc::{Receiver, Sender};
use std::sync::Arc;
use std::collections::HashMap;
use std::net::Ipv4Addr;
use tun_tap::Iface;
use std::convert::TryInto;
use super::super::init::config;
pub fn tun_recv_thread(
recv_send: Vec<Sender<Vec<u8>>>
config: Arc<config::Config>,
iface: Arc<Iface>,
sender_vec: Vec<Sender<super::TunData>>
) {
let mut ip_to_thread: HashMap<Ipv4Addr, u32> = HashMap::new();
for (i, elt) in config.peers.iter().enumerate() {
ip_to_thread.insert(elt.ip, i as u32);
}
loop {
let mut buf = vec![0; super::TUN_PACKET_SIZE];
let long = match iface.recv(&mut buf) {
Ok(l) => l,
Err(e) => {
println!("{:?}", e);
0
}
};
// min ipv4 header size
if long > 20 {
let ip_dest = Ipv4Addr::new(buf[16], buf[17], buf[18], buf[19]);
let thread_id: usize = (*ip_to_thread.get(&ip_dest).unwrap()).try_into().unwrap();
sender_vec[thread_id].send(super::TunData {
payload: buf
}).unwrap();
}
}
}
pub fn tun_send_thread(
recv_send: Receiver<Vec<u8>>
iface: Arc<Iface>,
recv: Receiver<Vec<u8>>
) {
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment