From 362dbf20d6b990a092bc220d36289144e86aaa6d Mon Sep 17 00:00:00 2001 From: bnewbold Date: Thu, 2 Jun 2016 17:18:44 -0400 Subject: start transition to UDT transport --- Cargo.lock | 180 ++++++------------------------------------------------ Cargo.toml | 2 +- src/client.rs | 20 ++++-- src/common.rs | 15 ++++- src/main.rs | 5 +- src/server.rs | 21 ++++--- src/udt_extras.rs | 44 +++++++++++++ 7 files changed, 105 insertions(+), 182 deletions(-) create mode 100644 src/udt_extras.rs diff --git a/Cargo.lock b/Cargo.lock index 5db5474..9ccb3e4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,16 +6,13 @@ dependencies = [ "getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", "sodiumoxide 0.0.10 (registry+https://github.com/rust-lang/crates.io-index)", - "temp_utp 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "udt 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] -name = "aho-corasick" -version = "0.5.2" +name = "bitflags" +version = "0.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", -] [[package]] name = "daemonize" @@ -26,13 +23,9 @@ dependencies = [ ] [[package]] -name = "env_logger" -version = "0.3.3" +name = "gcc" +version = "0.3.28" source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", - "regex 0.1.71 (registry+https://github.com/rust-lang/crates.io-index)", -] [[package]] name = "getopts" @@ -40,13 +33,9 @@ version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] -name = "kernel32-sys" -version = "0.2.2" +name = "libc" +version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "winapi 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi-build 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", -] [[package]] name = "libc" @@ -63,82 +52,17 @@ dependencies = [ ] [[package]] -name = "log" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" - -[[package]] -name = "memchr" -version = "0.1.11" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "num" -version = "0.1.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "num-bigint 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", - "num-complex 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", - "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", - "num-iter 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", - "num-rational 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "num-bigint" -version = "0.1.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "num-complex" -version = "0.1.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "num-integer" -version = "0.1.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "num-iter" -version = "0.1.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "num-rational" -version = "0.1.32" +name = "libudt4-sys" +version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "num-bigint 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", - "num-integer 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", - "rustc-serialize 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)", + "gcc 0.3.28 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] -name = "num-traits" -version = "0.1.32" +name = "log" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -146,31 +70,6 @@ name = "pkg-config" version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -[[package]] -name = "rand" -version = "0.3.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "regex" -version = "0.1.71" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "aho-corasick 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", - "memchr 0.1.11 (registry+https://github.com/rust-lang/crates.io-index)", - "regex-syntax 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", - "thread_local 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", - "utf8-ranges 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "regex-syntax" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" - [[package]] name = "rustc-serialize" version = "0.3.19" @@ -187,56 +86,13 @@ dependencies = [ ] [[package]] -name = "temp_utp" -version = "0.8.1" +name = "udt" +version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "env_logger 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "bitflags 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", + "libudt4-sys 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", - "num 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)", - "rand 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", - "time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)", ] -[[package]] -name = "thread-id" -version = "2.0.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "thread_local" -version = "0.2.5" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "thread-id 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "time" -version = "0.1.35" -source = "registry+https://github.com/rust-lang/crates.io-index" -dependencies = [ - "kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)", - "winapi 0.2.7 (registry+https://github.com/rust-lang/crates.io-index)", -] - -[[package]] -name = "utf8-ranges" -version = "0.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" - -[[package]] -name = "winapi" -version = "0.2.7" -source = "registry+https://github.com/rust-lang/crates.io-index" - -[[package]] -name = "winapi-build" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" - diff --git a/Cargo.toml b/Cargo.toml index b98d003..d1df197 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,6 @@ authors = ["bnewbold "] [dependencies] getopts = "^0.2" daemonize = "^0.2" -temp_utp = "*" +udt = "0.1" sodiumoxide = "*" rustc-serialize = "0.3" diff --git a/src/client.rs b/src/client.rs index b1e553a..6aa48ce 100644 --- a/src/client.rs +++ b/src/client.rs @@ -2,12 +2,15 @@ use super::common; use std::string::String; +use std::str::{self, FromStr}; use std::env; +use std::net; use std::process; use std::process::exit; use std::process::Command; use getopts::Options; -use utp::{UtpSocket, UtpStream}; +use udt::{self, UdtSocket}; +use udt_extras::{UdtStream}; use crypto::{SecretStream, key2string, string2key, nonce2string, string2nonce}; use sodiumoxide::crypto::secretbox; @@ -57,8 +60,10 @@ pub fn run_client(host: &str, local_file: &str, remote_file: &str, remote_is_dir println!("\tsecret read nonce: {}", remote_read_nonce); println!("\tsecret write nonce: {}", remote_write_nonce); - let mut socket = UtpSocket::connect((remote_host, remote_port)).unwrap();; - let mut stream: UtpStream = socket.into(); + let addr = net::IpAddr::from_str(remote_host).unwrap(); + let mut socket = UdtSocket::new(udt::SocketFamily::AFInet, udt::SocketType::Stream).unwrap(); + socket.connect(net::SocketAddr::new(addr, remote_port)).unwrap();; + let mut stream: UdtStream = UdtStream::new(socket); if !no_crypto { let mut stream = SecretStream::new(stream); @@ -128,9 +133,12 @@ pub fn main_client() { _ => {}, } - let port = matches.opt_str("port").unwrap().parse::().unwrap(); - let mut socket = UtpSocket::connect((matches.opt_str("host").unwrap().as_str(), port)).unwrap(); - let mut stream: UtpStream = socket.into(); + let remote_host = matches.opt_str("host").unwrap(); + let remote_port = matches.opt_str("port").unwrap().parse::().unwrap(); + let addr = net::IpAddr::from_str(&remote_host).unwrap(); + let mut socket = UdtSocket::new(udt::SocketFamily::AFInet, udt::SocketType::Stream).unwrap(); + socket.connect(net::SocketAddr::new(addr, remote_port)).unwrap();; + let mut stream: UdtStream = UdtStream::new(socket); println!("opened socket"); if !no_crypto { diff --git a/src/common.rs b/src/common.rs index 09f2b5b..175c4b8 100644 --- a/src/common.rs +++ b/src/common.rs @@ -7,6 +7,17 @@ use std::os::unix::fs::PermissionsExt; use std::io; use std::io::{Read, Write, BufRead, BufReader}; use std::process::exit; +use std::net; + +// This function is an ugly hack fall-back. It tries to find public IP address of this machine by +// creating an outdoing connection +pub fn get_local_ip() -> io::Result { + + let dummy = try!(net::TcpListener::bind("0.0.0.0:0")); + let addr = try!(dummy.local_addr()).ip(); + drop(dummy); + Ok(addr) +} pub fn source_files(stream: &mut S, file_path: &str, recursive: bool) { println!("SOURCE FILE: {}", file_path); @@ -68,8 +79,8 @@ fn raw_read_line(stream: &mut S) -> io::Result { } } -// TODO: it would be nice to be able to do BufReader/BufWriter on UtpStream. This would require -// implementations of Read and Write on immutable references to UtpStream (a la TcpStream, File, et +// TODO: it would be nice to be able to do BufReader/BufWriter on stream. This would require +// implementations of Read and Write on immutable references to stream (a la TcpStream, File, et // al) pub fn sink_files(stream: &mut S, file_path: &str, recursive: bool) { println!("SINK FILE: {}", file_path); diff --git a/src/main.rs b/src/main.rs index 8255547..6eb7de5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,20 +3,21 @@ #![allow(unused_imports, unused_variables, unused_mut)] extern crate getopts; -extern crate utp; +extern crate udt; extern crate sodiumoxide; extern crate rustc_serialize; mod client; mod server; mod common; +mod udt_extras; mod crypto; use std::str; use std::env; use std::process::exit; use getopts::Options; -use utp::{UtpSocket, UtpListener}; +use udt::{UdtSocket}; fn usage(opts: Options) { let brief = "usage:\tucp [-h] [-v] [[user@]host1:]srcfile [[user@]host2:]destfile"; diff --git a/src/server.rs b/src/server.rs index 596320d..3288867 100644 --- a/src/server.rs +++ b/src/server.rs @@ -3,13 +3,15 @@ extern crate daemonize; use super::common; -use std::str; +use std::str::{self, FromStr}; use std::env; +use std::net; use std::env::home_dir; use std::process::exit; use getopts::Options; -use utp::{UtpSocket, UtpStream, UtpListener}; +use udt::{self, UdtSocket}; use crypto::{SecretStream, key2string, string2key, nonce2string, string2nonce}; +use udt_extras::{UdtStream}; use sodiumoxide::crypto::secretbox; fn run_server(path: &str, is_recv: bool, recursive: bool, daemonize: bool, no_crypto: bool) { @@ -18,13 +20,15 @@ fn run_server(path: &str, is_recv: bool, recursive: bool, daemonize: bool, no_cr // variable. // Connect to an hypothetical local server running on port 61000 - let addr = "127.0.0.1:61000"; + let listen_addr = common::get_local_ip().unwrap(); + let port = 61000; // Accept connection from anybody - let listener = UtpListener::bind(addr).expect("Error binding to local port"); + let listener = UdtSocket::new(udt::SocketFamily::AFInet, udt::SocketType::Stream).unwrap(); + listener.bind(net::SocketAddr::new(listen_addr, port)).expect("Error binding to local port"); + listener.listen(1).unwrap(); - let listen_port = listener.local_addr().unwrap().port(); - let listen_addr = listener.local_addr().unwrap().ip(); + let listen_port = listener.getsockname().unwrap().port(); let secret_key = secretbox::gen_key(); let read_nonce = secretbox::gen_nonce(); @@ -65,10 +69,9 @@ fn run_server(path: &str, is_recv: bool, recursive: bool, daemonize: bool, no_cr } else { println!("Not daemonizing (DEBUG MODE)"); } - let (mut socket, _src) = listener.accept().unwrap(); - println!("Got connection from {}", socket.peer_addr().unwrap()); - let mut stream: UtpStream = socket.into(); + println!("Got connection from {}", socket.getpeername().unwrap()); + let mut stream: UdtStream = UdtStream::new(socket); if !no_crypto { let mut stream = SecretStream::new(stream); diff --git a/src/udt_extras.rs b/src/udt_extras.rs new file mode 100644 index 0000000..f4a714b --- /dev/null +++ b/src/udt_extras.rs @@ -0,0 +1,44 @@ + +extern crate udt; + +use std::str::{self, FromStr}; +use udt::{UdtSocket}; +use std::io; +use std::io::{Read, Write, ErrorKind}; + +pub struct UdtStream { + socket: UdtSocket, +} + +impl UdtStream { + + pub fn new(socket: UdtSocket) -> UdtStream { + UdtStream { socket: socket } + + } +} + +impl Read for UdtStream { + fn read(&mut self, buf: &mut [u8]) -> io::Result { + let buf_len = buf.len(); + match self.socket.recv(buf, buf_len) { + Ok(len) => Ok(len as usize), + Err(err) => Err(io::Error::new( + ErrorKind::Other, err.err_msg)), + } + } +} + +impl Write for UdtStream { + fn write(&mut self, buf: &[u8]) -> io::Result { + match self.socket.send(buf) { + Ok(len) => Ok(len as usize), + Err(err) => Err(io::Error::new( + ErrorKind::Other, err.err_msg)), + } + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} -- cgit v1.2.3