| 1 |
//! libc syscalls supporting `rustix::net`. |
| 2 |
|
| 3 |
use super::read_sockaddr::initialize_family_to_unspec; |
| 4 |
use super::send_recv::{RecvFlags, SendFlags}; |
| 5 |
use crate::backend::c; |
| 6 |
#[cfg(target_os = "linux")] |
| 7 |
use crate::backend::conv::ret_u32; |
| 8 |
use crate::backend::conv::{borrowed_fd, ret, ret_owned_fd, ret_send_recv, send_recv_len}; |
| 9 |
use crate::fd::{BorrowedFd, OwnedFd}; |
| 10 |
use crate::io; |
| 11 |
use crate::net::addr::SocketAddrArg; |
| 12 |
#[cfg(target_os = "linux")] |
| 13 |
use crate::net::MMsgHdr; |
| 14 |
use crate::net::{ |
| 15 |
AddressFamily, Protocol, Shutdown, SocketAddrAny, SocketAddrBuf, SocketFlags, SocketType, |
| 16 |
}; |
| 17 |
use crate::utils::as_ptr; |
| 18 |
use core::mem::{size_of, MaybeUninit}; |
| 19 |
use core::ptr::null_mut; |
| 20 |
#[cfg(not(any( |
| 21 |
windows, |
| 22 |
target_os = "espidf", |
| 23 |
target_os = "horizon", |
| 24 |
target_os = "vita" |
| 25 |
)))] |
| 26 |
use { |
| 27 |
super::msghdr::{noaddr_msghdr, with_msghdr, with_recv_msghdr}, |
| 28 |
super::send_recv::ReturnFlags, |
| 29 |
crate::io::{IoSlice, IoSliceMut}, |
| 30 |
crate::net::{RecvAncillaryBuffer, RecvMsg, SendAncillaryBuffer}, |
| 31 |
}; |
| 32 |
|
| 33 |
pub(crate) unsafe fn recv( |
| 34 |
fd: BorrowedFd<'_>, |
| 35 |
buf: (*mut u8, usize), |
| 36 |
flags: RecvFlags, |
| 37 |
) -> io::Result<usize> { |
| 38 |
ret_send_recv(c::recv( |
| 39 |
borrowed_fd(fd), |
| 40 |
buf.0.cast(), |
| 41 |
send_recv_len(buf.1), |
| 42 |
bitflags_bits!(flags), |
| 43 |
)) |
| 44 |
} |
| 45 |
|
| 46 |
pub(crate) fn send(fd: BorrowedFd<'_>, buf: &[u8], flags: SendFlags) -> io::Result<usize> { |
| 47 |
unsafe { |
| 48 |
ret_send_recv(c::send( |
| 49 |
borrowed_fd(fd), |
| 50 |
buf.as_ptr().cast(), |
| 51 |
send_recv_len(buf.len()), |
| 52 |
bitflags_bits!(flags), |
| 53 |
)) |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
pub(crate) unsafe fn recvfrom( |
| 58 |
fd: BorrowedFd<'_>, |
| 59 |
buf: (*mut u8, usize), |
| 60 |
flags: RecvFlags, |
| 61 |
) -> io::Result<(usize, Option<SocketAddrAny>)> { |
| 62 |
let mut addr = SocketAddrBuf::new(); |
| 63 |
|
| 64 |
// `recvfrom` does not write to the storage if the socket is |
| 65 |
// connection-oriented sockets, so we initialize the family field to |
| 66 |
// `AF_UNSPEC` so that we can detect this case. |
| 67 |
initialize_family_to_unspec(addr.storage.as_mut_ptr().cast::<c::sockaddr>()); |
| 68 |
|
| 69 |
let nread = ret_send_recv(c::recvfrom( |
| 70 |
borrowed_fd(fd), |
| 71 |
buf.0.cast(), |
| 72 |
send_recv_len(buf.1), |
| 73 |
bitflags_bits!(flags), |
| 74 |
addr.storage.as_mut_ptr().cast::<c::sockaddr>(), |
| 75 |
&mut addr.len, |
| 76 |
))?; |
| 77 |
|
| 78 |
Ok((nread, addr.into_any_option())) |
| 79 |
} |
| 80 |
|
| 81 |
pub(crate) fn sendto( |
| 82 |
fd: BorrowedFd<'_>, |
| 83 |
buf: &[u8], |
| 84 |
flags: SendFlags, |
| 85 |
addr: &impl SocketAddrArg, |
| 86 |
) -> io::Result<usize> { |
| 87 |
unsafe { |
| 88 |
addr.with_sockaddr(|addr_ptr, addr_len| { |
| 89 |
ret_send_recv(c::sendto( |
| 90 |
borrowed_fd(fd), |
| 91 |
buf.as_ptr().cast(), |
| 92 |
send_recv_len(buf.len()), |
| 93 |
bitflags_bits!(flags), |
| 94 |
addr_ptr.cast(), |
| 95 |
bitcast!(addr_len), |
| 96 |
)) |
| 97 |
}) |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
pub(crate) fn socket( |
| 102 |
domain: AddressFamily, |
| 103 |
type_: SocketType, |
| 104 |
protocol: Option<Protocol>, |
| 105 |
) -> io::Result<OwnedFd> { |
| 106 |
let raw_protocol = match protocol { |
| 107 |
Some(p) => p.0.get(), |
| 108 |
None => 0, |
| 109 |
}; |
| 110 |
unsafe { |
| 111 |
ret_owned_fd(c::socket( |
| 112 |
domain.0 as c::c_int, |
| 113 |
type_.0 as c::c_int, |
| 114 |
raw_protocol as c::c_int, |
| 115 |
)) |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
pub(crate) fn socket_with( |
| 120 |
domain: AddressFamily, |
| 121 |
type_: SocketType, |
| 122 |
flags: SocketFlags, |
| 123 |
protocol: Option<Protocol>, |
| 124 |
) -> io::Result<OwnedFd> { |
| 125 |
let raw_protocol = match protocol { |
| 126 |
Some(p) => p.0.get(), |
| 127 |
None => 0, |
| 128 |
}; |
| 129 |
unsafe { |
| 130 |
ret_owned_fd(c::socket( |
| 131 |
domain.0 as c::c_int, |
| 132 |
(type_.0 | flags.bits()) as c::c_int, |
| 133 |
raw_protocol as c::c_int, |
| 134 |
)) |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
pub(crate) fn bind(sockfd: BorrowedFd<'_>, addr: &impl SocketAddrArg) -> io::Result<()> { |
| 139 |
unsafe { |
| 140 |
addr.with_sockaddr(|addr_ptr, addr_len| { |
| 141 |
ret(c::bind( |
| 142 |
borrowed_fd(sockfd), |
| 143 |
addr_ptr.cast(), |
| 144 |
bitcast!(addr_len), |
| 145 |
)) |
| 146 |
}) |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
pub(crate) fn connect(sockfd: BorrowedFd<'_>, addr: &impl SocketAddrArg) -> io::Result<()> { |
| 151 |
unsafe { |
| 152 |
addr.with_sockaddr(|addr_ptr, addr_len| { |
| 153 |
ret(c::connect( |
| 154 |
borrowed_fd(sockfd), |
| 155 |
addr_ptr.cast(), |
| 156 |
bitcast!(addr_len), |
| 157 |
)) |
| 158 |
}) |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
pub(crate) fn connect_unspec(sockfd: BorrowedFd<'_>) -> io::Result<()> { |
| 163 |
debug_assert_eq!(c::AF_UNSPEC, 0); |
| 164 |
let addr = MaybeUninit::<c::sockaddr_storage>::zeroed(); |
| 165 |
unsafe { |
| 166 |
ret(c::connect( |
| 167 |
borrowed_fd(sockfd), |
| 168 |
as_ptr(&addr).cast(), |
| 169 |
size_of::<c::sockaddr_storage>() as c::socklen_t, |
| 170 |
)) |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
pub(crate) fn listen(sockfd: BorrowedFd<'_>, backlog: c::c_int) -> io::Result<()> { |
| 175 |
unsafe { ret(c::listen(borrowed_fd(sockfd), backlog)) } |
| 176 |
} |
| 177 |
|
| 178 |
pub(crate) fn accept(sockfd: BorrowedFd<'_>) -> io::Result<OwnedFd> { |
| 179 |
unsafe { |
| 180 |
let owned_fd = ret_owned_fd(c::accept(borrowed_fd(sockfd), null_mut(), null_mut()))?; |
| 181 |
Ok(owned_fd) |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
#[cfg(not(any( |
| 186 |
windows, |
| 187 |
target_os = "espidf", |
| 188 |
target_os = "horizon", |
| 189 |
target_os = "vita" |
| 190 |
)))] |
| 191 |
pub(crate) fn recvmsg( |
| 192 |
sockfd: BorrowedFd<'_>, |
| 193 |
iov: &mut [IoSliceMut<'_>], |
| 194 |
control: &mut RecvAncillaryBuffer<'_>, |
| 195 |
msg_flags: RecvFlags, |
| 196 |
) -> io::Result<RecvMsg> { |
| 197 |
let mut addr = SocketAddrBuf::new(); |
| 198 |
|
| 199 |
// SAFETY: This passes the `msghdr` reference to the OS which reads the |
| 200 |
// buffers only within the designated bounds. |
| 201 |
let (bytes, flags) = unsafe { |
| 202 |
with_recv_msghdr(&mut addr, iov, control, |msghdr| { |
| 203 |
let bytes = ret_send_recv(c::recvmsg( |
| 204 |
borrowed_fd(sockfd), |
| 205 |
msghdr, |
| 206 |
bitflags_bits!(msg_flags), |
| 207 |
))?; |
| 208 |
Ok((bytes, msghdr.msg_flags)) |
| 209 |
})? |
| 210 |
}; |
| 211 |
|
| 212 |
Ok(RecvMsg { |
| 213 |
bytes, |
| 214 |
address: unsafe { addr.into_any_option() }, |
| 215 |
flags: ReturnFlags::from_bits_retain(bitcast!(flags)), |
| 216 |
}) |
| 217 |
} |
| 218 |
|
| 219 |
#[cfg(not(any( |
| 220 |
windows, |
| 221 |
target_os = "espidf", |
| 222 |
target_os = "horizon", |
| 223 |
target_os = "vita" |
| 224 |
)))] |
| 225 |
pub(crate) fn sendmsg( |
| 226 |
sockfd: BorrowedFd<'_>, |
| 227 |
iov: &[IoSlice<'_>], |
| 228 |
control: &mut SendAncillaryBuffer<'_, '_, '_>, |
| 229 |
msg_flags: SendFlags, |
| 230 |
) -> io::Result<usize> { |
| 231 |
let msghdr = noaddr_msghdr(iov, control); |
| 232 |
unsafe { |
| 233 |
ret_send_recv(c::sendmsg( |
| 234 |
borrowed_fd(sockfd), |
| 235 |
&msghdr, |
| 236 |
bitflags_bits!(msg_flags), |
| 237 |
)) |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
#[cfg(not(any( |
| 242 |
windows, |
| 243 |
target_os = "espidf", |
| 244 |
target_os = "horizon", |
| 245 |
target_os = "vita" |
| 246 |
)))] |
| 247 |
pub(crate) fn sendmsg_addr( |
| 248 |
sockfd: BorrowedFd<'_>, |
| 249 |
addr: &impl SocketAddrArg, |
| 250 |
iov: &[IoSlice<'_>], |
| 251 |
control: &mut SendAncillaryBuffer<'_, '_, '_>, |
| 252 |
msg_flags: SendFlags, |
| 253 |
) -> io::Result<usize> { |
| 254 |
// SAFETY: This passes the `msghdr` reference to the OS which reads the |
| 255 |
// buffers only within the designated bounds. |
| 256 |
unsafe { |
| 257 |
with_msghdr(addr, iov, control, |msghdr| { |
| 258 |
ret_send_recv(c::sendmsg( |
| 259 |
borrowed_fd(sockfd), |
| 260 |
msghdr, |
| 261 |
bitflags_bits!(msg_flags), |
| 262 |
)) |
| 263 |
}) |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
#[cfg(target_os = "linux")] |
| 268 |
pub(crate) fn sendmmsg( |
| 269 |
sockfd: BorrowedFd<'_>, |
| 270 |
msgs: &mut [MMsgHdr<'_>], |
| 271 |
flags: SendFlags, |
| 272 |
) -> io::Result<usize> { |
| 273 |
unsafe { |
| 274 |
ret_u32(c::sendmmsg( |
| 275 |
borrowed_fd(sockfd), |
| 276 |
msgs.as_mut_ptr() as _, |
| 277 |
msgs.len().try_into().unwrap_or(c::c_uint::MAX), |
| 278 |
bitflags_bits!(flags), |
| 279 |
)) |
| 280 |
.map(|ret| ret as usize) |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
#[cfg(not(any( |
| 285 |
apple, |
| 286 |
windows, |
| 287 |
target_os = "aix", |
| 288 |
target_os = "espidf", |
| 289 |
target_os = "haiku", |
| 290 |
target_os = "horizon", |
| 291 |
target_os = "nto", |
| 292 |
target_os = "redox", |
| 293 |
target_os = "vita", |
| 294 |
)))] |
| 295 |
pub(crate) fn accept_with(sockfd: BorrowedFd<'_>, flags: SocketFlags) -> io::Result<OwnedFd> { |
| 296 |
unsafe { |
| 297 |
let owned_fd = ret_owned_fd(c::accept4( |
| 298 |
borrowed_fd(sockfd), |
| 299 |
null_mut(), |
| 300 |
null_mut(), |
| 301 |
flags.bits() as c::c_int, |
| 302 |
))?; |
| 303 |
Ok(owned_fd) |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
pub(crate) fn acceptfrom(sockfd: BorrowedFd<'_>) -> io::Result<(OwnedFd, Option<SocketAddrAny>)> { |
| 308 |
unsafe { |
| 309 |
let mut addr = SocketAddrBuf::new(); |
| 310 |
let owned_fd = ret_owned_fd(c::accept( |
| 311 |
borrowed_fd(sockfd), |
| 312 |
addr.storage.as_mut_ptr().cast::<c::sockaddr>(), |
| 313 |
&mut addr.len, |
| 314 |
))?; |
| 315 |
Ok((owned_fd, addr.into_any_option())) |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
#[cfg(not(any( |
| 320 |
apple, |
| 321 |
windows, |
| 322 |
target_os = "aix", |
| 323 |
target_os = "espidf", |
| 324 |
target_os = "haiku", |
| 325 |
target_os = "horizon", |
| 326 |
target_os = "nto", |
| 327 |
target_os = "redox", |
| 328 |
target_os = "vita", |
| 329 |
)))] |
| 330 |
pub(crate) fn acceptfrom_with( |
| 331 |
sockfd: BorrowedFd<'_>, |
| 332 |
flags: SocketFlags, |
| 333 |
) -> io::Result<(OwnedFd, Option<SocketAddrAny>)> { |
| 334 |
unsafe { |
| 335 |
let mut addr = SocketAddrBuf::new(); |
| 336 |
let owned_fd = ret_owned_fd(c::accept4( |
| 337 |
borrowed_fd(sockfd), |
| 338 |
addr.storage.as_mut_ptr().cast::<c::sockaddr>(), |
| 339 |
&mut addr.len, |
| 340 |
flags.bits() as c::c_int, |
| 341 |
))?; |
| 342 |
Ok((owned_fd, addr.into_any_option())) |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
/// Darwin lacks `accept4`, but does have `accept`. We define `SocketFlags` to |
| 347 |
/// have no flags, so we can discard it here. |
| 348 |
#[cfg(any( |
| 349 |
apple, |
| 350 |
windows, |
| 351 |
target_os = "aix", |
| 352 |
target_os = "espidf", |
| 353 |
target_os = "haiku", |
| 354 |
target_os = "horizon", |
| 355 |
target_os = "nto", |
| 356 |
target_os = "redox", |
| 357 |
target_os = "vita", |
| 358 |
))] |
| 359 |
pub(crate) fn accept_with(sockfd: BorrowedFd<'_>, _flags: SocketFlags) -> io::Result<OwnedFd> { |
| 360 |
accept(sockfd) |
| 361 |
} |
| 362 |
|
| 363 |
/// Darwin lacks `accept4`, but does have `accept`. We define `SocketFlags` to |
| 364 |
/// have no flags, so we can discard it here. |
| 365 |
#[cfg(any( |
| 366 |
apple, |
| 367 |
windows, |
| 368 |
target_os = "aix", |
| 369 |
target_os = "espidf", |
| 370 |
target_os = "haiku", |
| 371 |
target_os = "horizon", |
| 372 |
target_os = "nto", |
| 373 |
target_os = "redox", |
| 374 |
target_os = "vita", |
| 375 |
))] |
| 376 |
pub(crate) fn acceptfrom_with( |
| 377 |
sockfd: BorrowedFd<'_>, |
| 378 |
_flags: SocketFlags, |
| 379 |
) -> io::Result<(OwnedFd, Option<SocketAddrAny>)> { |
| 380 |
acceptfrom(sockfd) |
| 381 |
} |
| 382 |
|
| 383 |
pub(crate) fn shutdown(sockfd: BorrowedFd<'_>, how: Shutdown) -> io::Result<()> { |
| 384 |
unsafe { ret(c::shutdown(borrowed_fd(sockfd), how as c::c_int)) } |
| 385 |
} |
| 386 |
|
| 387 |
pub(crate) fn getsockname(sockfd: BorrowedFd<'_>) -> io::Result<SocketAddrAny> { |
| 388 |
unsafe { |
| 389 |
let mut addr = SocketAddrBuf::new(); |
| 390 |
ret(c::getsockname( |
| 391 |
borrowed_fd(sockfd), |
| 392 |
addr.storage.as_mut_ptr().cast::<c::sockaddr>(), |
| 393 |
&mut addr.len, |
| 394 |
))?; |
| 395 |
Ok(addr.into_any()) |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
pub(crate) fn getpeername(sockfd: BorrowedFd<'_>) -> io::Result<Option<SocketAddrAny>> { |
| 400 |
unsafe { |
| 401 |
let mut addr = SocketAddrBuf::new(); |
| 402 |
ret(c::getpeername( |
| 403 |
borrowed_fd(sockfd), |
| 404 |
addr.storage.as_mut_ptr().cast::<c::sockaddr>(), |
| 405 |
&mut addr.len, |
| 406 |
))?; |
| 407 |
Ok(addr.into_any_option()) |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
#[cfg(not(windows))] |
| 412 |
pub(crate) fn socketpair( |
| 413 |
domain: AddressFamily, |
| 414 |
type_: SocketType, |
| 415 |
flags: SocketFlags, |
| 416 |
protocol: Option<Protocol>, |
| 417 |
) -> io::Result<(OwnedFd, OwnedFd)> { |
| 418 |
let raw_protocol = match protocol { |
| 419 |
Some(p) => p.0.get(), |
| 420 |
None => 0, |
| 421 |
}; |
| 422 |
unsafe { |
| 423 |
let mut fds = MaybeUninit::<[OwnedFd; 2]>::uninit(); |
| 424 |
ret(c::socketpair( |
| 425 |
c::c_int::from(domain.0), |
| 426 |
(type_.0 | flags.bits()) as c::c_int, |
| 427 |
raw_protocol as c::c_int, |
| 428 |
fds.as_mut_ptr().cast::<c::c_int>(), |
| 429 |
))?; |
| 430 |
|
| 431 |
let [fd0, fd1] = fds.assume_init(); |
| 432 |
Ok((fd0, fd1)) |
| 433 |
} |
| 434 |
} |