Skip to content

Commit 3cc4a1a

Browse files
committed
Adjustments based on feedback
1 parent 8ad2d77 commit 3cc4a1a

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Utilities for handling networking sockets with a maximal amount of configuration
1212
possible intended.
1313
"""
1414

15+
[package.metadata.docs.rs]
16+
all-features = true
17+
1518
[target."cfg(windows)".dependencies]
1619
ws2_32-sys = "0.2"
1720
winapi = "0.2"
@@ -21,6 +24,9 @@ kernel32-sys = "0.2"
2124
cfg-if = "0.1"
2225
libc = "0.2.14"
2326

27+
[dev-dependencies]
28+
tempdir = "0.3"
29+
2430
[features]
2531
reuseport = []
2632
pair = []

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@
4444
#[cfg(windows)] extern crate winapi;
4545
#[cfg(windows)] extern crate ws2_32;
4646

47+
#[cfg(test)] extern crate tempdir;
48+
4749
use utils::NetInt;
4850

4951
#[cfg(unix)] use libc::{sockaddr_storage, socklen_t};

src/socket.rs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ impl Socket {
4343
/// This function is only available on Unix when the `pair` feature is
4444
/// enabled.
4545
#[cfg(all(unix, feature = "pair"))]
46-
pub fn pair(domain: Domain, type_: Type, protocol: Option<Protocol>) -> io::Result<(Socket, Socket)> {
46+
pub fn pair(domain: Domain,
47+
type_: Type,
48+
protocol: Option<Protocol>) -> io::Result<(Socket, Socket)> {
4749
let protocol = protocol.map(|p| p.0).unwrap_or(0);
4850
let sockets = sys::Socket::pair(domain.0, type_.0, protocol)?;
4951
Ok((Socket { inner: sockets.0 }, Socket { inner: sockets.1 }))
@@ -640,6 +642,15 @@ impl Domain {
640642
pub fn ipv6() -> Domain {
641643
Domain(c::AF_INET6)
642644
}
645+
646+
/// Domain for Unix socket communication, corresponding to `AF_UNIX`.
647+
///
648+
/// This function is only available on Unix when the `unix` feature is
649+
/// activated.
650+
#[cfg(all(unix, feature = "unix"))]
651+
pub fn unix() -> Domain {
652+
Domain(c::AF_UNIX)
653+
}
643654
}
644655

645656
impl From<i32> for Domain {
@@ -734,4 +745,37 @@ mod test {
734745
let socket = Socket::new(Domain::ipv4(), Type::stream(), None).unwrap();
735746
socket.connect_timeout(&addr, Duration::from_millis(250)).unwrap();
736747
}
748+
749+
#[test]
750+
#[cfg(all(unix, feature = "pair", feature = "unix"))]
751+
fn pair() {
752+
let (mut a, mut b) = Socket::pair(Domain::unix(), Type::stream(), None).unwrap();
753+
a.write_all(b"hello world").unwrap();
754+
let mut buf = [0; 11];
755+
b.read_exact(&mut buf).unwrap();
756+
assert_eq!(buf, &b"hello world"[..]);
757+
}
758+
759+
#[test]
760+
#[cfg(all(unix, feature = "unix"))]
761+
fn unix() {
762+
use tempdir::TempDir;
763+
764+
let dir = TempDir::new("unix").unwrap();
765+
let addr = SockAddr::unix(dir.path().join("sock")).unwrap();
766+
767+
let listener = Socket::new(Domain::unix(), Type::stream(), None).unwrap();
768+
listener.bind(&addr).unwrap();
769+
listener.listen(10).unwrap();
770+
771+
let mut a = Socket::new(Domain::unix(), Type::stream(), None).unwrap();
772+
a.connect(&addr).unwrap();
773+
774+
let mut b = listener.accept().unwrap().0;
775+
776+
a.write_all(b"hello world").unwrap();
777+
let mut buf = [0; 11];
778+
b.read_exact(&mut buf).unwrap();
779+
assert_eq!(buf, &b"hello world"[..]);
780+
}
737781
}

0 commit comments

Comments
 (0)