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