Skip to content
This repository was archived by the owner on May 17, 2018. It is now read-only.

Commit 04fcc9e

Browse files
committed
Merge branch 'release-v0.4.5' into release
2 parents 43ef565 + d866f70 commit 04fcc9e

File tree

3 files changed

+85
-32
lines changed

3 files changed

+85
-32
lines changed

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
[package]
22
name = "unix_socket"
3-
version = "0.4.4"
3+
version = "0.4.5"
44
authors = ["Steven Fackler <[email protected]>"]
55
license = "MIT"
66
description = "Unix domain socket bindings"
77
repository = "https://github.com/sfackler/rust-unix-socket"
8-
documentation = "https://sfackler.github.io/rust-unix-socket/doc/v0.4.4/unix_socket"
8+
documentation = "https://sfackler.github.io/rust-unix-socket/doc/v0.4.5/unix_socket"
99
readme = "README.md"
1010
keywords = ["posix", "unix", "socket", "domain"]
1111

@@ -17,5 +17,7 @@ debug-builders = "0.1"
1717
tempdir = "0.3"
1818

1919
[features]
20+
default = ["from_raw_fd"]
21+
2022
from_raw_fd = []
2123
socket_timeout = []

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44

55
Support for Unix domain socket clients and servers.
66

7-
[Documentation](https://sfackler.github.io/rust-unix-socket/doc/v0.4.4/unix_socket)
7+
[Documentation](https://sfackler.github.io/rust-unix-socket/doc/v0.4.5/unix_socket)

src/lib.rs

Lines changed: 80 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Support for Unix domain socket clients and servers.
22
#![warn(missing_docs)]
3-
#![doc(html_root_url="https://sfackler.github.io/rust-unix-socket/doc/v0.4.4")]
3+
#![doc(html_root_url="https://sfackler.github.io/rust-unix-socket/doc/v0.4.5")]
44
#![cfg_attr(feature = "socket_timeout", feature(duration))]
55
#![cfg_attr(all(test, feature = "socket_timeout"), feature(duration_span))]
66

@@ -81,10 +81,10 @@ impl Inner {
8181
}
8282
}
8383

84-
fn new_pair() -> io::Result<(Inner, Inner)> {
84+
fn new_pair(kind: libc::c_int) -> io::Result<(Inner, Inner)> {
8585
unsafe {
8686
let mut fds = [0, 0];
87-
try!(cvt(socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, &mut fds)));
87+
try!(cvt(socketpair(libc::AF_UNIX, kind, 0, &mut fds)));
8888
Ok((Inner(fds[0]), Inner(fds[1])))
8989
}
9090
}
@@ -132,19 +132,19 @@ impl Inner {
132132
fn set_timeout(&self, dur: Option<std::time::Duration>, kind: libc::c_int) -> io::Result<()> {
133133
let timeout = match dur {
134134
Some(dur) => {
135-
if dur.secs() == 0 && dur.extra_nanos() == 0 {
135+
if dur.as_secs() == 0 && dur.subsec_nanos() == 0 {
136136
return Err(io::Error::new(io::ErrorKind::InvalidInput,
137137
"cannot set a 0 duration timeout"));
138138
}
139139

140-
let secs = if dur.secs() > libc::time_t::max_value() as u64 {
140+
let secs = if dur.as_secs() > libc::time_t::max_value() as u64 {
141141
libc::time_t::max_value()
142142
} else {
143-
dur.secs() as libc::time_t
143+
dur.as_secs() as libc::time_t
144144
};
145145
let mut timeout = libc::timeval {
146146
tv_sec: secs,
147-
tv_usec: (dur.extra_nanos() / 1000) as libc::suseconds_t,
147+
tv_usec: (dur.subsec_nanos() / 1000) as libc::suseconds_t,
148148
};
149149
if timeout.tv_sec == 0 && timeout.tv_usec == 0 {
150150
timeout.tv_usec = 1;
@@ -242,9 +242,7 @@ impl SocketAddr {
242242
// When there is a datagram from unnamed unix socket
243243
// linux returns zero bytes of address
244244
len = sun_path_offset() as libc::socklen_t; // i.e. zero-length address
245-
} else if (len as usize) < size_of::<libc::sa_family_t>() ||
246-
addr.sun_family != libc::AF_UNIX as libc::sa_family_t
247-
{
245+
} else if addr.sun_family != libc::AF_UNIX as libc::sa_family_t {
248246
return Err(io::Error::new(io::ErrorKind::InvalidInput,
249247
"file descriptor did not correspond to a Unix socket"));
250248
}
@@ -353,11 +351,18 @@ impl UnixStream {
353351
/// Create an unnamed pair of connected sockets.
354352
///
355353
/// Returns two `UnixStream`s which are connected to each other.
356-
pub fn unnamed() -> io::Result<(UnixStream, UnixStream)> {
357-
let (i1, i2) = try!(Inner::new_pair());
354+
pub fn pair() -> io::Result<(UnixStream, UnixStream)> {
355+
let (i1, i2) = try!(Inner::new_pair(libc::SOCK_STREAM));
358356
Ok((UnixStream { inner: i1 }, UnixStream { inner: i2 }))
359357
}
360358

359+
/// # Deprecated
360+
///
361+
/// Use `UnixStream::pair` instead.
362+
pub fn unnamed() -> io::Result<(UnixStream, UnixStream)> {
363+
UnixStream::pair()
364+
}
365+
361366
/// Create a new independently owned handle to the underlying socket.
362367
///
363368
/// The returned `UnixStream` is a reference to the same stream that this
@@ -479,7 +484,7 @@ impl AsRawFd for UnixStream {
479484
}
480485

481486
#[cfg(feature = "from_raw_fd")]
482-
/// Requires the `from_raw_fd` feature.
487+
/// Requires the `from_raw_fd` feature (enabled by default).
483488
impl std::os::unix::io::FromRawFd for UnixStream {
484489
unsafe fn from_raw_fd(fd: RawFd) -> UnixStream {
485490
UnixStream {
@@ -598,7 +603,7 @@ impl AsRawFd for UnixListener {
598603
}
599604

600605
#[cfg(feature = "from_raw_fd")]
601-
/// Requires the `from_raw_fd` feature.
606+
/// Requires the `from_raw_fd` feature (enabled by default).
602607
impl std::os::unix::io::FromRawFd for UnixListener {
603608
unsafe fn from_raw_fd(fd: RawFd) -> UnixListener {
604609
UnixListener {
@@ -660,12 +665,21 @@ impl fmt::Debug for UnixDatagram {
660665
if let Ok(addr) = self.local_addr() {
661666
builder = builder.field("local", &addr);
662667
}
668+
if let Ok(addr) = self.peer_addr() {
669+
builder = builder.field("peer", &addr);
670+
}
663671
builder.finish()
664672
}
665673
}
666674

667675
impl UnixDatagram {
668676
/// Creates a Unix datagram socket bound to the given path.
677+
///
678+
/// Linux provides, as a nonportable extension, a separate "abstract"
679+
/// address namespace as opposed to filesystem-based addressing. If `path`
680+
/// begins with a null byte, it will be interpreted as an "abstract"
681+
/// address. Otherwise, it will be interpreted as a "pathname" address,
682+
/// corresponding to a path on the filesystem.
669683
pub fn bind<P: AsRef<Path>>(path: P) -> io::Result<UnixDatagram> {
670684
unsafe {
671685
let inner = try!(Inner::new(libc::SOCK_DGRAM));
@@ -687,6 +701,14 @@ impl UnixDatagram {
687701
})
688702
}
689703

704+
/// Create an unnamed pair of connected sockets.
705+
///
706+
/// Returns two `UnixDatagrams`s which are connected to each other.
707+
pub fn pair() -> io::Result<(UnixDatagram, UnixDatagram)> {
708+
let (i1, i2) = try!(Inner::new_pair(libc::SOCK_DGRAM));
709+
Ok((UnixDatagram { inner: i1 }, UnixDatagram { inner: i2 }))
710+
}
711+
690712
/// Connect the socket to the specified address.
691713
///
692714
/// The `send` method may be used to send data to the specified address.
@@ -708,6 +730,13 @@ impl UnixDatagram {
708730
SocketAddr::new(|addr, len| unsafe { libc::getsockname(self.inner.0, addr, len) })
709731
}
710732

733+
/// Returns the address of this socket's peer.
734+
///
735+
/// The `connect` method will connect the socket to a peer.
736+
pub fn peer_addr(&self) -> io::Result<SocketAddr> {
737+
SocketAddr::new(|addr, len| unsafe { libc::getpeername(self.inner.0, addr, len) })
738+
}
739+
711740
/// Receives data from the socket.
712741
///
713742
/// On success, returns the number of bytes read and the address from
@@ -777,8 +806,8 @@ impl UnixDatagram {
777806

778807
/// Sets the read timeout for the socket.
779808
///
780-
/// If the provided value is `None`, then `recv_from` calls will block
781-
/// indefinitely. It is an error to pass the zero `Duration` to this
809+
/// If the provided value is `None`, then `recv` and `recv_from` calls will
810+
/// block indefinitely. It is an error to pass the zero `Duration` to this
782811
/// method.
783812
///
784813
/// Requires the `socket_timeout` feature.
@@ -789,8 +818,8 @@ impl UnixDatagram {
789818

790819
/// Sets the write timeout for the socket.
791820
///
792-
/// If the provided value is `None`, then `send_to` calls will block
793-
/// indefinitely. It is an error to pass the zero `Duration` to this
821+
/// If the provided value is `None`, then `send` and `send_to` calls will
822+
/// block indefinitely. It is an error to pass the zero `Duration` to this
794823
/// method.
795824
///
796825
/// Requires the `socket_timeout` feature.
@@ -832,7 +861,7 @@ impl AsRawFd for UnixDatagram {
832861
}
833862

834863
#[cfg(feature = "from_raw_fd")]
835-
/// Requires the `from_raw_fd` feature.
864+
/// Requires the `from_raw_fd` feature (enabled by default).
836865
impl std::os::unix::io::FromRawFd for UnixDatagram {
837866
unsafe fn from_raw_fd(fd: RawFd) -> UnixDatagram {
838867
UnixDatagram {
@@ -888,11 +917,11 @@ mod test {
888917
}
889918

890919
#[test]
891-
fn unnamed() {
920+
fn pair() {
892921
let msg1 = b"hello";
893922
let msg2 = b"world!";
894923

895-
let (mut s1, mut s2) = or_panic!(UnixStream::unnamed());
924+
let (mut s1, mut s2) = or_panic!(UnixStream::pair());
896925
let thread = thread::spawn(move || {
897926
// s1 must be moved in or the test will hang!
898927
let mut buf = [0; 5];
@@ -1000,6 +1029,12 @@ mod test {
10001029
Err(e) => panic!("unexpected error {}", e),
10011030
Ok(_) => panic!("unexpected success"),
10021031
}
1032+
1033+
match UnixDatagram::bind(&socket_path) {
1034+
Err(ref e) if e.kind() == io::ErrorKind::InvalidInput => {}
1035+
Err(e) => panic!("unexpected error {}", e),
1036+
Ok(_) => panic!("unexpected success"),
1037+
}
10031038
}
10041039

10051040
#[test]
@@ -1135,13 +1170,6 @@ mod test {
11351170
assert_eq!(addr.address(), AddressKind::Unnamed);
11361171
assert_eq!(msg, &buf[..]);
11371172

1138-
1139-
// Send to should still work too
1140-
let msg = b"hello world";
1141-
or_panic!(sock.send_to(msg, &path2));
1142-
or_panic!(bsock2.recv_from(&mut buf));
1143-
assert_eq!(msg, &buf[..]);
1144-
11451173
// Changing default socket works too
11461174
or_panic!(sock.connect(&path2));
11471175
or_panic!(sock.send(msg));
@@ -1158,10 +1186,33 @@ mod test {
11581186
or_panic!(sock2.connect(&path1));
11591187

11601188
let msg = b"hello world";
1161-
or_panic!(sock2.send_to(msg, &path1));
1189+
or_panic!(sock2.send(msg));
11621190
let mut buf = [0; 11];
11631191
let size = or_panic!(sock1.recv(&mut buf));
11641192
assert_eq!(size, 11);
11651193
assert_eq!(msg, &buf[..]);
11661194
}
1195+
1196+
#[test]
1197+
fn datagram_pair() {
1198+
let msg1 = b"hello";
1199+
let msg2 = b"world!";
1200+
1201+
let (s1, s2) = or_panic!(UnixDatagram::pair());
1202+
let thread = thread::spawn(move || {
1203+
// s1 must be moved in or the test will hang!
1204+
let mut buf = [0; 5];
1205+
or_panic!(s1.recv(&mut buf));
1206+
assert_eq!(&msg1[..], &buf[..]);
1207+
or_panic!(s1.send(msg2));
1208+
});
1209+
1210+
or_panic!(s2.send(msg1));
1211+
let mut buf = [0; 6];
1212+
or_panic!(s2.recv(&mut buf));
1213+
assert_eq!(&msg2[..], &buf[..]);
1214+
drop(s2);
1215+
1216+
thread.join().unwrap();
1217+
}
11671218
}

0 commit comments

Comments
 (0)