diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/client.rs | 20 | ||||
-rw-r--r-- | src/common.rs | 15 | ||||
-rw-r--r-- | src/main.rs | 5 | ||||
-rw-r--r-- | src/server.rs | 21 | ||||
-rw-r--r-- | src/udt_extras.rs | 44 |
5 files changed, 86 insertions, 19 deletions
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::<u16>().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::<u16>().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<net::IpAddr> { + + 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<S: Read + Write>(stream: &mut S, file_path: &str, recursive: bool) { println!("SOURCE FILE: {}", file_path); @@ -68,8 +79,8 @@ fn raw_read_line<S: Read + Write>(stream: &mut S) -> io::Result<String> { } } -// 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<S: Read + Write>(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<usize> { + 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<usize> { + 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(()) + } +} |