1 | use crate::sys::unix::net::{new_ip_socket, socket_addr}; |
2 | |
3 | use std::io; |
4 | use std::mem; |
5 | use std::net::{self, SocketAddr}; |
6 | use std::os::unix::io::{AsRawFd, FromRawFd}; |
7 | |
8 | pub 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 | |
18 | pub(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 | |