Skip to main content

relibc/platform/pal/
socket.rs

1use crate::{
2    error::Result,
3    header::sys_socket::{msghdr, sockaddr, socklen_t},
4    platform::{
5        Pal,
6        types::{c_int, c_void, size_t},
7    },
8};
9
10/// Platform abstraction of socket functionality.
11pub trait PalSocket: Pal {
12    /// Platform implementation of [`accept()`](crate::header::sys_socket::accept) from [`sys/socket.h`](crate::header::sys_socket).
13    unsafe fn accept(
14        socket: c_int,
15        address: *mut sockaddr,
16        address_len: *mut socklen_t,
17    ) -> Result<c_int>;
18
19    /// Platform implementation of [`bind()`](crate::header::sys_socket::bind) from [`sys/socket.h`](crate::header::sys_socket).
20    unsafe fn bind(socket: c_int, address: *const sockaddr, address_len: socklen_t) -> Result<()>;
21
22    /// Platform implementation of [`connect()`](crate::header::sys_socket::connect) from [`sys/socket.h`](crate::header::sys_socket).
23    unsafe fn connect(
24        socket: c_int,
25        address: *const sockaddr,
26        address_len: socklen_t,
27    ) -> Result<c_int>;
28
29    /// Platform implementation of [`getpeername()`](crate::header::sys_socket::getpeername) from [`sys/socket.h`](crate::header::sys_socket).
30    unsafe fn getpeername(
31        socket: c_int,
32        address: *mut sockaddr,
33        address_len: *mut socklen_t,
34    ) -> Result<()>;
35
36    /// Platform implementation of [`getsockname()`](crate::header::sys_socket::getsockname) from [`sys/socket.h`](crate::header::sys_socket).
37    unsafe fn getsockname(
38        socket: c_int,
39        address: *mut sockaddr,
40        address_len: *mut socklen_t,
41    ) -> Result<()>;
42
43    /// Platform implementation of [`getsockopt()`](crate::header::sys_socket::getsockopt) from [`sys/socket.h`](crate::header::sys_socket).
44    unsafe fn getsockopt(
45        socket: c_int,
46        level: c_int,
47        option_name: c_int,
48        option_value: *mut c_void,
49        option_len: *mut socklen_t,
50    ) -> Result<()>;
51
52    /// Platform implementation of [`listen()`](crate::header::sys_socket::listen) from [`sys/socket.h`](crate::header::sys_socket).
53    fn listen(socket: c_int, backlog: c_int) -> Result<()>;
54
55    /// Platform implementation of [`recvfrom()`](crate::header::sys_socket::recvfrom) from [`sys/socket.h`](crate::header::sys_socket).
56    unsafe fn recvfrom(
57        socket: c_int,
58        buf: *mut c_void,
59        len: size_t,
60        flags: c_int,
61        address: *mut sockaddr,
62        address_len: *mut socklen_t,
63    ) -> Result<usize>;
64
65    /// Platform implementation of [`recvmsg()`](crate::header::sys_socket::recvmsg) from [`sys/socket.h`](crate::header::sys_socket).
66    unsafe fn recvmsg(socket: c_int, msg: *mut msghdr, flags: c_int) -> Result<usize>;
67
68    /// Platform implementation of [`sendmsg()`](crate::header::sys_socket::sendmsg) from [`sys/socket.h`](crate::header::sys_socket).
69    unsafe fn sendmsg(socket: c_int, msg: *const msghdr, flags: c_int) -> Result<usize>;
70
71    /// Platform implementation of [`sendto()`](crate::header::sys_socket::sendto) from [`sys/socket.h`](crate::header::sys_socket).
72    unsafe fn sendto(
73        socket: c_int,
74        buf: *const c_void,
75        len: size_t,
76        flags: c_int,
77        dest_addr: *const sockaddr,
78        dest_len: socklen_t,
79    ) -> Result<usize>;
80
81    /// Platform implementation of [`setsockopt()`](crate::header::sys_socket::setsockopt) from [`sys/socket.h`](crate::header::sys_socket).
82    unsafe fn setsockopt(
83        socket: c_int,
84        level: c_int,
85        option_name: c_int,
86        option_value: *const c_void,
87        option_len: socklen_t,
88    ) -> Result<()>;
89
90    /// Platform implementation of [`shutdown()`](crate::header::sys_socket::shutdown) from [`sys/socket.h`](crate::header::sys_socket).
91    fn shutdown(socket: c_int, how: c_int) -> Result<()>;
92
93    /// Platform implementation of [`socket()`](crate::header::sys_socket::socket) from [`sys/socket.h`](crate::header::sys_socket).
94    unsafe fn socket(domain: c_int, kind: c_int, protocol: c_int) -> Result<c_int>;
95
96    /// Platform implementation of [`socketpair()`](crate::header::sys_socket::socketpair) from [`sys/socket.h`](crate::header::sys_socket).
97    fn socketpair(domain: c_int, kind: c_int, protocol: c_int, sv: &mut [c_int; 2]) -> Result<()>;
98}