1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
extern crate nix;

pub use nix::sys::socket::SockType;
use nix::sys::socket::{self, AddressFamily, MsgFlags, SockAddr, SockFlag};

use std::io::{Read, Write};
use std::os::unix::io::RawFd;
use std::sync::Arc;
pub type Result<T> = std::result::Result<T, StreamError>;

#[derive(Debug)]
pub enum StreamError {
	PathDNE,
	InternalSys,
	BadError,
}

struct StreamFd(RawFd);

impl std::convert::From<nix::Error> for StreamError {
	fn from(err: nix::Error) -> StreamError {
		match err {
			nix::Error::InvalidPath => StreamError::PathDNE,
			nix::Error::Sys(_) => StreamError::InternalSys,
			_ => StreamError::BadError,
		}
	}
}

pub struct UnixStream {
	fd: Arc<StreamFd>,
}

pub struct UnixStreamRd {
	fd: Arc<StreamFd>,
}

pub struct UnixStreamWrt {
	fd: Arc<StreamFd>,
}

impl UnixStream {
	pub fn split(self) -> Result<(UnixStreamWrt, UnixStreamRd)> {
		Ok((
			UnixStreamWrt {
				fd: Arc::clone(&self.fd),
			},
			UnixStreamRd { fd: self.fd },
		))
	}

	pub fn new_with_type(path: &str, tp: SockType) -> Result<UnixStream> {
		let soc = socket::socket(AddressFamily::Unix, tp, SockFlag::empty(), None)?;
		let p = SockAddr::new_unix(path)?;
		socket::connect(soc, &p)?;

		Ok(UnixStream {
			fd: Arc::new(StreamFd(soc)),
		})
	}
	pub fn new(path: &str) -> Result<UnixStream> {
		UnixStream::new_with_type(path, SockType::SeqPacket)
	}
}

impl Drop for StreamFd {
	fn drop(&mut self) {
		socket::shutdown(self.0, socket::Shutdown::Both).unwrap_or(());
	}
}

impl Read for UnixStream {
	fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
		match socket::recv(self.fd.0, buf, MsgFlags::empty()) {
			Ok(sz) => Ok(sz),
			_ => Err(std::io::Error::last_os_error()),
		}
	}
}

impl Read for UnixStreamRd {
	fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
		match socket::recv(self.fd.0, buf, MsgFlags::empty()) {
			Ok(sz) => Ok(sz),
			_ => Err(std::io::Error::last_os_error()),
		}
	}
}

impl Write for UnixStreamWrt {
	fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
		match socket::send(self.fd.0, buf, MsgFlags::empty()) {
			Ok(sz) => Ok(sz),
			_ => Err(std::io::Error::last_os_error()),
		}
	}

	// Is this right?
	fn flush(&mut self) -> std::result::Result<(), std::io::Error> {
		Ok(())
	}
}

impl Write for UnixStream {
	fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
		match socket::send(self.fd.0, buf, MsgFlags::empty()) {
			Ok(sz) => Ok(sz),
			_ => Err(std::io::Error::last_os_error()),
		}
	}

	// Is this right?
	fn flush(&mut self) -> std::result::Result<(), std::io::Error> {
		Ok(())
	}
}

#[cfg(test)]
mod tests {

	#[test]
	fn it_works() {
		assert_eq!(1 + 1, 2);
	}
}