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

Commit 469d92f

Browse files
committed
Merge pull request #15 from tailhook/unix_datagram_connect
Implement `UnixDatagram::connect()`
2 parents 65cd124 + 637f0e0 commit 469d92f

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

src/lib.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,21 @@ impl UnixDatagram {
688688
})
689689
}
690690

691+
/// Creates a Unix Datagram socket which is connected to specified addresss
692+
/// the socket is unnamed and similar to one created by `new()` except
693+
/// it will send message to the specified addresss *by default*
694+
pub fn connect<P: AsRef<Path>>(&mut self, path: P)
695+
-> io::Result<()>
696+
{
697+
unsafe {
698+
let (addr, len) = try!(sockaddr_un(path));
699+
700+
try!(cvt(libc::connect(self.inner.0, &addr as *const _ as *const _, len)));
701+
702+
Ok(())
703+
}
704+
}
705+
691706
/// Returns the address of this socket.
692707
pub fn local_addr(&self) -> io::Result<SocketAddr> {
693708
SocketAddr::new(|addr, len| unsafe { libc::getsockname(self.inner.0, addr, len) })
@@ -714,6 +729,19 @@ impl UnixDatagram {
714729
Ok((count as usize, addr))
715730
}
716731

732+
/// Receives data from the socket.
733+
///
734+
/// On success, returns the number of bytes read
735+
pub fn recv(&self, buf: &mut [u8]) -> io::Result<usize> {
736+
unsafe {
737+
let count = try!(cvt_s(libc::recv(self.inner.0,
738+
buf.as_mut_ptr() as *mut _,
739+
calc_len(buf),
740+
0)));
741+
Ok(count as usize)
742+
}
743+
}
744+
717745
/// Sends data on the socket to the given address.
718746
///
719747
/// On success, returns the number of bytes written.
@@ -731,6 +759,21 @@ impl UnixDatagram {
731759
}
732760
}
733761

762+
/// Sends data on the socket to the default address.
763+
///
764+
/// Default address is set when socket was created by `connect()` constructor
765+
///
766+
/// On success, returns the number of bytes written.
767+
pub fn send(&self, buf: &[u8]) -> io::Result<usize> {
768+
unsafe {
769+
let count = try!(cvt_s(libc::send(self.inner.0,
770+
buf.as_ptr() as *const _,
771+
calc_len(buf),
772+
0)));
773+
Ok(count as usize)
774+
}
775+
}
776+
734777
/// Sets the read timeout for the socket.
735778
///
736779
/// If the provided value is `None`, then `recv_from` calls will block
@@ -1070,4 +1113,54 @@ mod test {
10701113
assert_eq!(addr.address(), AddressKind::Unnamed);
10711114
assert_eq!(msg, &buf[..]);
10721115
}
1116+
1117+
#[test]
1118+
fn test_connect_unix_datagram() {
1119+
let dir = or_panic!(TempDir::new("unix_socket"));
1120+
let path1 = dir.path().join("sock1");
1121+
let path2 = dir.path().join("sock2");
1122+
1123+
let bsock1 = or_panic!(UnixDatagram::bind(&path1));
1124+
let bsock2 = or_panic!(UnixDatagram::bind(&path2));
1125+
let mut sock = or_panic!(UnixDatagram::new());
1126+
or_panic!(sock.connect(&path1));
1127+
1128+
// Check send()
1129+
let msg = b"hello there";
1130+
or_panic!(sock.send(msg));
1131+
let mut buf = [0; 11];
1132+
let (usize, addr) = or_panic!(bsock1.recv_from(&mut buf));
1133+
assert_eq!(usize, 11);
1134+
assert_eq!(addr.address(), AddressKind::Unnamed);
1135+
assert_eq!(msg, &buf[..]);
1136+
1137+
1138+
// Send to should still work too
1139+
let msg = b"hello world";
1140+
or_panic!(sock.send_to(msg, &path2));
1141+
or_panic!(bsock2.recv_from(&mut buf));
1142+
assert_eq!(msg, &buf[..]);
1143+
1144+
// Changing default socket works too
1145+
or_panic!(sock.connect(&path2));
1146+
or_panic!(sock.send(msg));
1147+
or_panic!(bsock2.recv_from(&mut buf));
1148+
}
1149+
1150+
#[test]
1151+
fn test_unix_datagram_recv() {
1152+
let dir = or_panic!(TempDir::new("unix_socket"));
1153+
let path1 = dir.path().join("sock1");
1154+
1155+
let sock1 = or_panic!(UnixDatagram::bind(&path1));
1156+
let mut sock2 = or_panic!(UnixDatagram::new());
1157+
or_panic!(sock2.connect(&path1));
1158+
1159+
let msg = b"hello world";
1160+
or_panic!(sock2.send_to(msg, &path1));
1161+
let mut buf = [0; 11];
1162+
let size = or_panic!(sock1.recv(&mut buf));
1163+
assert_eq!(size, 11);
1164+
assert_eq!(msg, &buf[..]);
1165+
}
10731166
}

0 commit comments

Comments
 (0)