net_util: split create_socket() into AF_UNIX and AF_INET varieties

Some host kernels are IPv6-only, and they cannot make an AF_INET socket.
The tun ioctls such as SIOCGIFHWADDR work with an AF_UNIX socket, which
all host kernels should have.

Signed-off-by: Barret Rhoden <brho@google.com>
This commit is contained in:
Barret Rhoden
2022-03-02 10:38:36 -05:00
committed by Sebastien Boeuf
parent 2c3908eed9
commit cbf5068e17
2 changed files with 21 additions and 7 deletions

View File

@@ -77,7 +77,7 @@ fn create_sockaddr(ip_addr: net::Ipv4Addr) -> net_gen::sockaddr {
unsafe { mem::transmute(addr_in) }
}
fn create_socket() -> Result<net::UdpSocket> {
fn create_inet_socket() -> Result<net::UdpSocket> {
// This is safe since we check the return value.
let sock = unsafe { libc::socket(libc::AF_INET, libc::SOCK_DGRAM, 0) };
if sock < 0 {
@@ -88,6 +88,17 @@ fn create_socket() -> Result<net::UdpSocket> {
Ok(unsafe { net::UdpSocket::from_raw_fd(sock) })
}
fn create_unix_socket() -> Result<net::UdpSocket> {
// This is safe since we check the return value.
let sock = unsafe { libc::socket(libc::AF_UNIX, libc::SOCK_DGRAM, 0) };
if sock < 0 {
return Err(Error::CreateSocket(IoError::last_os_error()));
}
// This is safe; nothing else will use or hold onto the raw sock fd.
Ok(unsafe { net::UdpSocket::from_raw_fd(sock) })
}
fn vnet_hdr_len() -> usize {
std::mem::size_of::<virtio_net_hdr_v1>()
}