1use crate::sys::unix::net::{new_ip_socket, socket_addr};
2
3use std::io;
4use std::mem;
5use std::net::{self, SocketAddr};
6use std::os::unix::io::{AsRawFd, FromRawFd};
7
8pub fn bind(addr: SocketAddr) -> io::Result<net::UdpSocket> {
9 let fd: i32 = new_ip_socket(addr, socket_type:libc::SOCK_DGRAM)?;
10 let socket: UdpSocket = unsafe { net::UdpSocket::from_raw_fd(fd) };
11
12 let (raw_addr: SocketAddrCRepr, raw_addr_length: u32) = socket_addr(&addr);
13 syscall!(bind(fd, raw_addr.as_ptr(), raw_addr_length))?;
14
15 Ok(socket)
16}
17
18pub(crate) fn only_v6(socket: &net::UdpSocket) -> io::Result<bool> {
19 let mut optval: libc::c_int = 0;
20 let mut optlen: u32 = mem::size_of::<libc::c_int>() as libc::socklen_t;
21
22 syscall!(getsockopt(
23 socket.as_raw_fd(),
24 libc::IPPROTO_IPV6,
25 libc::IPV6_V6ONLY,
26 &mut optval as *mut _ as *mut _,
27 &mut optlen,
28 ))?;
29
30 Ok(optval != 0)
31}
32