1 use crate::fd
::OwnedFd
;
2 use crate::net
::{SocketAddr, SocketAddrAny, SocketAddrV4, SocketAddrV6}
;
3 use crate::{backend, io}
;
4 use backend
::fd
::{AsFd, BorrowedFd}
;
7 pub use backend
::net
::addr
::SocketAddrUnix
;
8 pub use backend
::net
::types
::{
9 AcceptFlags
, AddressFamily
, Protocol
, Shutdown
, SocketFlags
, SocketType
,
12 impl Default
for Protocol
{
14 fn default() -> Self {
19 /// `socket(domain, type_, protocol)`—Creates a socket.
21 /// POSIX guarantees that `socket` will use the lowest unused file descriptor,
22 /// however it is not safe in general to rely on this, as file descriptors
23 /// may be unexpectedly allocated on other threads or in libraries.
31 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html
32 /// [Linux]: https://man7.org/linux/man-pages/man2/socket.2.html
33 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/socket.2.html
34 /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-socket
36 pub fn socket(domain
: AddressFamily
, type_
: SocketType
, protocol
: Protocol
) -> io
::Result
<OwnedFd
> {
37 backend
::net
::syscalls
::socket(domain
, type_
, protocol
)
40 /// `socket_with(domain, type_ | flags, protocol)`—Creates a socket, with
43 /// POSIX guarantees that `socket` will use the lowest unused file descriptor,
44 /// however it is not safe in general to rely on this, as file descriptors
45 /// may be unexpectedly allocated on other threads or in libraries.
47 /// `socket_with` is the same as [`socket`] but adds an additional flags
56 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/socket.html
57 /// [Linux]: https://man7.org/linux/man-pages/man2/socket.2.html
58 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/socket.2.html
59 /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-socket
62 domain
: AddressFamily
,
66 ) -> io
::Result
<OwnedFd
> {
67 backend
::net
::syscalls
::socket_with(domain
, type_
, flags
, protocol
)
70 /// `bind(sockfd, addr)`—Binds a socket to an IP address.
78 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html
79 /// [Linux]: https://man7.org/linux/man-pages/man2/bind.2.html
80 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/bind.2.html
81 /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-bind
82 pub fn bind
<Fd
: AsFd
>(sockfd
: Fd
, addr
: &SocketAddr
) -> io
::Result
<()> {
83 _bind(sockfd
.as_fd(), addr
)
86 fn _bind(sockfd
: BorrowedFd
<'_
>, addr
: &SocketAddr
) -> io
::Result
<()> {
88 SocketAddr
::V4(v4
) => backend
::net
::syscalls
::bind_v4(sockfd
, v4
),
89 SocketAddr
::V6(v6
) => backend
::net
::syscalls
::bind_v6(sockfd
, v6
),
93 /// `bind(sockfd, addr)`—Binds a socket to an address.
101 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html
102 /// [Linux]: https://man7.org/linux/man-pages/man2/bind.2.html
103 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/bind.2.html
104 /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-bind
105 #[doc(alias = "bind")]
106 pub fn bind_any
<Fd
: AsFd
>(sockfd
: Fd
, addr
: &SocketAddrAny
) -> io
::Result
<()> {
107 _bind_any(sockfd
.as_fd(), addr
)
110 fn _bind_any(sockfd
: BorrowedFd
<'_
>, addr
: &SocketAddrAny
) -> io
::Result
<()> {
112 SocketAddrAny
::V4(v4
) => backend
::net
::syscalls
::bind_v4(sockfd
, v4
),
113 SocketAddrAny
::V6(v6
) => backend
::net
::syscalls
::bind_v6(sockfd
, v6
),
115 SocketAddrAny
::Unix(unix
) => backend
::net
::syscalls
::bind_unix(sockfd
, unix
),
119 /// `bind(sockfd, addr, sizeof(struct sockaddr_in))`—Binds a socket to an
128 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html
129 /// [Linux]: https://man7.org/linux/man-pages/man2/bind.2.html
130 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/bind.2.html
131 /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-bind
133 #[doc(alias = "bind")]
134 pub fn bind_v4
<Fd
: AsFd
>(sockfd
: Fd
, addr
: &SocketAddrV4
) -> io
::Result
<()> {
135 backend
::net
::syscalls
::bind_v4(sockfd
.as_fd(), addr
)
138 /// `bind(sockfd, addr, sizeof(struct sockaddr_in6))`—Binds a socket to an
147 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html
148 /// [Linux]: https://man7.org/linux/man-pages/man2/bind.2.html
149 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/bind.2.html
150 /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-bind
152 #[doc(alias = "bind")]
153 pub fn bind_v6
<Fd
: AsFd
>(sockfd
: Fd
, addr
: &SocketAddrV6
) -> io
::Result
<()> {
154 backend
::net
::syscalls
::bind_v6(sockfd
.as_fd(), addr
)
157 /// `bind(sockfd, addr, sizeof(struct sockaddr_un))`—Binds a socket to a
158 /// Unix-domain address.
166 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/bind.html
167 /// [Linux]: https://man7.org/linux/man-pages/man2/bind.2.html
168 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/bind.2.html
169 /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-bind
172 #[doc(alias = "bind")]
173 pub fn bind_unix
<Fd
: AsFd
>(sockfd
: Fd
, addr
: &SocketAddrUnix
) -> io
::Result
<()> {
174 backend
::net
::syscalls
::bind_unix(sockfd
.as_fd(), addr
)
177 /// `connect(sockfd, addr)`—Initiates a connection to an IP address.
185 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html
186 /// [Linux]: https://man7.org/linux/man-pages/man2/connect.2.html
187 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/connect.2.html
188 /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-connect
189 pub fn connect
<Fd
: AsFd
>(sockfd
: Fd
, addr
: &SocketAddr
) -> io
::Result
<()> {
190 _connect(sockfd
.as_fd(), addr
)
193 fn _connect(sockfd
: BorrowedFd
<'_
>, addr
: &SocketAddr
) -> io
::Result
<()> {
195 SocketAddr
::V4(v4
) => backend
::net
::syscalls
::connect_v4(sockfd
, v4
),
196 SocketAddr
::V6(v6
) => backend
::net
::syscalls
::connect_v6(sockfd
, v6
),
200 /// `connect(sockfd, addr)`—Initiates a connection.
208 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html
209 /// [Linux]: https://man7.org/linux/man-pages/man2/connect.2.html
210 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/connect.2.html
211 /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-connect
212 #[doc(alias = "connect")]
213 pub fn connect_any
<Fd
: AsFd
>(sockfd
: Fd
, addr
: &SocketAddrAny
) -> io
::Result
<()> {
214 _connect_any(sockfd
.as_fd(), addr
)
217 fn _connect_any(sockfd
: BorrowedFd
<'_
>, addr
: &SocketAddrAny
) -> io
::Result
<()> {
219 SocketAddrAny
::V4(v4
) => backend
::net
::syscalls
::connect_v4(sockfd
, v4
),
220 SocketAddrAny
::V6(v6
) => backend
::net
::syscalls
::connect_v6(sockfd
, v6
),
222 SocketAddrAny
::Unix(unix
) => backend
::net
::syscalls
::connect_unix(sockfd
, unix
),
226 /// `connect(sockfd, addr, sizeof(struct sockaddr_in))`—Initiates a
227 /// connection to an IPv4 address.
235 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html
236 /// [Linux]: https://man7.org/linux/man-pages/man2/connect.2.html
237 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/connect.2.html
238 /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-connect
240 #[doc(alias = "connect")]
241 pub fn connect_v4
<Fd
: AsFd
>(sockfd
: Fd
, addr
: &SocketAddrV4
) -> io
::Result
<()> {
242 backend
::net
::syscalls
::connect_v4(sockfd
.as_fd(), addr
)
245 /// `connect(sockfd, addr, sizeof(struct sockaddr_in6))`—Initiates a
246 /// connection to an IPv6 address.
254 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html
255 /// [Linux]: https://man7.org/linux/man-pages/man2/connect.2.html
256 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/connect.2.html
257 /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-connect
259 #[doc(alias = "connect")]
260 pub fn connect_v6
<Fd
: AsFd
>(sockfd
: Fd
, addr
: &SocketAddrV6
) -> io
::Result
<()> {
261 backend
::net
::syscalls
::connect_v6(sockfd
.as_fd(), addr
)
264 /// `connect(sockfd, addr, sizeof(struct sockaddr_un))`—Initiates a
265 /// connection to a Unix-domain address.
273 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/connect.html
274 /// [Linux]: https://man7.org/linux/man-pages/man2/connect.2.html
275 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/connect.2.html
276 /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-connect
279 #[doc(alias = "connect")]
280 pub fn connect_unix
<Fd
: AsFd
>(sockfd
: Fd
, addr
: &SocketAddrUnix
) -> io
::Result
<()> {
281 backend
::net
::syscalls
::connect_unix(sockfd
.as_fd(), addr
)
284 /// `listen(fd, backlog)`—Enables listening for incoming connections.
292 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/listen.html
293 /// [Linux]: https://man7.org/linux/man-pages/man2/listen.2.html
294 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/listen.2.html
295 /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-listen
297 pub fn listen
<Fd
: AsFd
>(sockfd
: Fd
, backlog
: i32) -> io
::Result
<()> {
298 backend
::net
::syscalls
::listen(sockfd
.as_fd(), backlog
)
301 /// `accept(fd, NULL, NULL)`—Accepts an incoming connection.
303 /// Use [`acceptfrom`] to retrieve the peer address.
305 /// POSIX guarantees that `accept` will use the lowest unused file descriptor,
306 /// however it is not safe in general to rely on this, as file descriptors may
307 /// be unexpectedly allocated on other threads or in libraries.
315 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html
316 /// [Linux]: https://man7.org/linux/man-pages/man2/accept.2.html
317 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/accept.2.html
318 /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-accept
320 #[doc(alias = "accept4")]
321 pub fn accept
<Fd
: AsFd
>(sockfd
: Fd
) -> io
::Result
<OwnedFd
> {
322 backend
::net
::syscalls
::accept(sockfd
.as_fd())
325 /// `accept4(fd, NULL, NULL, flags)`—Accepts an incoming connection, with
328 /// Use [`acceptfrom_with`] to retrieve the peer address.
330 /// Even though POSIX guarantees that this will use the lowest unused file
331 /// descriptor, it is not safe in general to rely on this, as file descriptors
332 /// may be unexpectedly allocated on other threads or in libraries.
334 /// `accept_with` is the same as [`accept`] but adds an additional flags
340 /// [Linux]: https://man7.org/linux/man-pages/man2/accept4.2.html
342 #[doc(alias = "accept4")]
343 pub fn accept_with
<Fd
: AsFd
>(sockfd
: Fd
, flags
: AcceptFlags
) -> io
::Result
<OwnedFd
> {
344 backend
::net
::syscalls
::accept_with(sockfd
.as_fd(), flags
)
347 /// `accept(fd, &addr, &len)`—Accepts an incoming connection and returns the
350 /// Use [`accept`] if the peer address isn't needed.
358 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/accept.html
359 /// [Linux]: https://man7.org/linux/man-pages/man2/accept.2.html
360 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/accept.2.html
361 /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-accept
363 #[doc(alias = "accept4")]
364 pub fn acceptfrom
<Fd
: AsFd
>(sockfd
: Fd
) -> io
::Result
<(OwnedFd
, Option
<SocketAddrAny
>)> {
365 backend
::net
::syscalls
::acceptfrom(sockfd
.as_fd())
368 /// `accept4(fd, &addr, &len, flags)`—Accepts an incoming connection and
369 /// returns the peer address, with flags.
371 /// Use [`accept_with`] if the peer address isn't needed.
373 /// `acceptfrom_with` is the same as [`acceptfrom`] but adds an additional
379 /// [Linux]: https://man7.org/linux/man-pages/man2/accept4.2.html
381 #[doc(alias = "accept4")]
382 pub fn acceptfrom_with
<Fd
: AsFd
>(
385 ) -> io
::Result
<(OwnedFd
, Option
<SocketAddrAny
>)> {
386 backend
::net
::syscalls
::acceptfrom_with(sockfd
.as_fd(), flags
)
389 /// `shutdown(fd, how)`—Closes the read and/or write sides of a stream.
397 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/shutdown.html
398 /// [Linux]: https://man7.org/linux/man-pages/man2/shutdown.2.html
399 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/shutdown.2.html
400 /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-shutdown
402 pub fn shutdown
<Fd
: AsFd
>(sockfd
: Fd
, how
: Shutdown
) -> io
::Result
<()> {
403 backend
::net
::syscalls
::shutdown(sockfd
.as_fd(), how
)
406 /// `getsockname(fd, addr, len)`—Returns the address a socket is bound to.
414 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/getsockname.html
415 /// [Linux]: https://man7.org/linux/man-pages/man2/getsockname.2.html
416 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/getsockname.2.html
417 /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-getsockname
419 pub fn getsockname
<Fd
: AsFd
>(sockfd
: Fd
) -> io
::Result
<SocketAddrAny
> {
420 backend
::net
::syscalls
::getsockname(sockfd
.as_fd())
423 /// `getpeername(fd, addr, len)`—Returns the address a socket is connected
432 /// [POSIX]: https://pubs.opengroup.org/onlinepubs/9699919799/functions/getpeername.html
433 /// [Linux]: https://man7.org/linux/man-pages/man2/getpeername.2.html
434 /// [Apple]: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/getpeername.2.html
435 /// [Winsock2]: https://docs.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-getpeername
437 pub fn getpeername
<Fd
: AsFd
>(sockfd
: Fd
) -> io
::Result
<Option
<SocketAddrAny
>> {
438 backend
::net
::syscalls
::getpeername(sockfd
.as_fd())