1use super::{socket_addr, SocketAddr};
2use crate::sys::unix::net::new_socket;
3
4use std::io;
5use std::os::unix::ffi::OsStrExt;
6use std::os::unix::io::{AsRawFd, FromRawFd};
7use std::os::unix::net;
8use std::path::Path;
9
10pub(crate) fn bind(path: &Path) -> io::Result<net::UnixDatagram> {
11 let (sockaddr: sockaddr_un, socklen: u32) = socket_addr(path.as_os_str().as_bytes())?;
12 let sockaddr: *const sockaddr = &sockaddr as *const libc::sockaddr_un as *const _;
13
14 let socket: UnixDatagram = unbound()?;
15 syscall!(bind(socket.as_raw_fd(), sockaddr, socklen))?;
16
17 Ok(socket)
18}
19
20pub(crate) fn unbound() -> io::Result<net::UnixDatagram> {
21 let fd: i32 = new_socket(domain:libc::AF_UNIX, socket_type:libc::SOCK_DGRAM)?;
22 Ok(unsafe { net::UnixDatagram::from_raw_fd(fd) })
23}
24
25pub(crate) fn pair() -> io::Result<(net::UnixDatagram, net::UnixDatagram)> {
26 super::pair(flags:libc::SOCK_DGRAM)
27}
28
29pub(crate) fn local_addr(socket: &net::UnixDatagram) -> io::Result<SocketAddr> {
30 super::local_addr(socket:socket.as_raw_fd())
31}
32
33pub(crate) fn peer_addr(socket: &net::UnixDatagram) -> io::Result<SocketAddr> {
34 super::peer_addr(socket:socket.as_raw_fd())
35}
36
37pub(crate) fn recv_from(
38 socket: &net::UnixDatagram,
39 dst: &mut [u8],
40) -> io::Result<(usize, SocketAddr)> {
41 let mut count: isize = 0;
42 let socketaddr: SocketAddr = SocketAddr::new(|sockaddr: *mut sockaddr, socklen: &mut u32| {
43 syscall!(recvfrom(
44 socket.as_raw_fd(),
45 dst.as_mut_ptr() as *mut _,
46 dst.len(),
47 0,
48 sockaddr,
49 socklen,
50 ))
51 .map(|c: isize| {
52 count = c;
53 c as libc::c_int
54 })
55 })?;
56 Ok((count as usize, socketaddr))
57}
58