2024-01-03 03:22:36 +00:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
2024-09-06 10:49:37 +00:00
|
|
|
use aster_bigtcp::socket::{
|
2024-12-05 08:40:20 +00:00
|
|
|
NeedIfacePoll, TCP_RECV_BUF_LEN, TCP_SEND_BUF_LEN, UDP_RECV_PAYLOAD_LEN, UDP_SEND_PAYLOAD_LEN,
|
2024-09-06 10:49:37 +00:00
|
|
|
};
|
|
|
|
|
|
2025-06-12 15:07:47 +00:00
|
|
|
use super::LingerOption;
|
2024-02-25 14:09:24 +00:00
|
|
|
use crate::{
|
2024-08-24 04:00:42 +00:00
|
|
|
match_sock_option_mut, match_sock_option_ref,
|
2024-09-06 10:49:37 +00:00
|
|
|
net::socket::options::{
|
2025-02-19 07:33:03 +00:00
|
|
|
KeepAlive, Linger, RecvBuf, ReuseAddr, ReusePort, SendBuf, SocketOption,
|
2024-08-24 04:00:42 +00:00
|
|
|
},
|
2024-02-25 14:09:24 +00:00
|
|
|
prelude::*,
|
|
|
|
|
};
|
2023-12-28 09:24:23 +00:00
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, CopyGetters, Setters)]
|
|
|
|
|
#[get_copy = "pub"]
|
|
|
|
|
#[set = "pub"]
|
|
|
|
|
pub struct SocketOptionSet {
|
|
|
|
|
reuse_addr: bool,
|
|
|
|
|
reuse_port: bool,
|
|
|
|
|
send_buf: u32,
|
|
|
|
|
recv_buf: u32,
|
|
|
|
|
linger: LingerOption,
|
2024-12-05 08:40:20 +00:00
|
|
|
keep_alive: bool,
|
2023-12-28 09:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl SocketOptionSet {
|
|
|
|
|
/// Return the default socket level options for tcp socket.
|
|
|
|
|
pub fn new_tcp() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
reuse_addr: false,
|
|
|
|
|
reuse_port: false,
|
2024-08-24 04:00:42 +00:00
|
|
|
send_buf: TCP_SEND_BUF_LEN as u32,
|
|
|
|
|
recv_buf: TCP_RECV_BUF_LEN as u32,
|
2023-12-28 09:24:23 +00:00
|
|
|
linger: LingerOption::default(),
|
2024-12-05 08:40:20 +00:00
|
|
|
keep_alive: false,
|
2023-12-28 09:24:23 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-08-24 04:00:42 +00:00
|
|
|
|
|
|
|
|
/// Return the default socket level options for udp socket.
|
|
|
|
|
pub fn new_udp() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
reuse_addr: false,
|
|
|
|
|
reuse_port: false,
|
|
|
|
|
send_buf: UDP_SEND_PAYLOAD_LEN as u32,
|
|
|
|
|
recv_buf: UDP_RECV_PAYLOAD_LEN as u32,
|
|
|
|
|
linger: LingerOption::default(),
|
2024-12-05 08:40:20 +00:00
|
|
|
keep_alive: false,
|
2024-08-24 04:00:42 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Gets socket-level options.
|
|
|
|
|
///
|
|
|
|
|
/// Note that the socket error has to be handled separately, because it is automatically
|
|
|
|
|
/// cleared after reading. This method does not handle it. Instead,
|
|
|
|
|
/// [`Self::get_and_clear_socket_errors`] should be used.
|
|
|
|
|
pub fn get_option(&self, option: &mut dyn SocketOption) -> Result<()> {
|
|
|
|
|
match_sock_option_mut!(option, {
|
|
|
|
|
socket_reuse_addr: ReuseAddr => {
|
|
|
|
|
let reuse_addr = self.reuse_addr();
|
|
|
|
|
socket_reuse_addr.set(reuse_addr);
|
|
|
|
|
},
|
|
|
|
|
socket_send_buf: SendBuf => {
|
|
|
|
|
let send_buf = self.send_buf();
|
|
|
|
|
socket_send_buf.set(send_buf);
|
|
|
|
|
},
|
|
|
|
|
socket_recv_buf: RecvBuf => {
|
|
|
|
|
let recv_buf = self.recv_buf();
|
|
|
|
|
socket_recv_buf.set(recv_buf);
|
|
|
|
|
},
|
|
|
|
|
socket_reuse_port: ReusePort => {
|
|
|
|
|
let reuse_port = self.reuse_port();
|
|
|
|
|
socket_reuse_port.set(reuse_port);
|
|
|
|
|
},
|
|
|
|
|
socket_linger: Linger => {
|
|
|
|
|
let linger = self.linger();
|
|
|
|
|
socket_linger.set(linger);
|
|
|
|
|
},
|
2024-12-05 08:40:20 +00:00
|
|
|
socket_keepalive: KeepAlive => {
|
|
|
|
|
let keep_alive = self.keep_alive();
|
|
|
|
|
socket_keepalive.set(keep_alive);
|
|
|
|
|
},
|
2024-08-24 04:00:42 +00:00
|
|
|
_ => return_errno_with_message!(Errno::ENOPROTOOPT, "the socket option to get is unknown")
|
|
|
|
|
});
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Sets socket-level options.
|
2024-12-05 08:40:20 +00:00
|
|
|
pub fn set_option(
|
|
|
|
|
&mut self,
|
|
|
|
|
option: &dyn SocketOption,
|
2025-04-20 08:43:38 +00:00
|
|
|
socket: &dyn SetSocketLevelOption,
|
2024-12-05 08:40:20 +00:00
|
|
|
) -> Result<NeedIfacePoll> {
|
2024-08-24 04:00:42 +00:00
|
|
|
match_sock_option_ref!(option, {
|
|
|
|
|
socket_recv_buf: RecvBuf => {
|
|
|
|
|
let recv_buf = socket_recv_buf.get().unwrap();
|
|
|
|
|
if *recv_buf <= MIN_RECVBUF {
|
|
|
|
|
self.set_recv_buf(MIN_RECVBUF);
|
|
|
|
|
} else {
|
|
|
|
|
self.set_recv_buf(*recv_buf);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
socket_send_buf: SendBuf => {
|
|
|
|
|
let send_buf = socket_send_buf.get().unwrap();
|
|
|
|
|
if *send_buf <= MIN_SENDBUF {
|
|
|
|
|
self.set_send_buf(MIN_SENDBUF);
|
|
|
|
|
} else {
|
|
|
|
|
self.set_send_buf(*send_buf);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
socket_reuse_addr: ReuseAddr => {
|
|
|
|
|
let reuse_addr = socket_reuse_addr.get().unwrap();
|
|
|
|
|
self.set_reuse_addr(*reuse_addr);
|
|
|
|
|
},
|
|
|
|
|
socket_reuse_port: ReusePort => {
|
|
|
|
|
let reuse_port = socket_reuse_port.get().unwrap();
|
|
|
|
|
self.set_reuse_port(*reuse_port);
|
|
|
|
|
},
|
|
|
|
|
socket_linger: Linger => {
|
|
|
|
|
let linger = socket_linger.get().unwrap();
|
|
|
|
|
self.set_linger(*linger);
|
|
|
|
|
},
|
2024-12-05 08:40:20 +00:00
|
|
|
socket_keepalive: KeepAlive => {
|
|
|
|
|
let keep_alive = socket_keepalive.get().unwrap();
|
|
|
|
|
self.set_keep_alive(*keep_alive);
|
|
|
|
|
return Ok(socket.set_keep_alive(*keep_alive));
|
|
|
|
|
},
|
2024-08-24 04:00:42 +00:00
|
|
|
_ => return_errno_with_message!(Errno::ENOPROTOOPT, "the socket option to be set is unknown")
|
|
|
|
|
});
|
|
|
|
|
|
2024-12-05 08:40:20 +00:00
|
|
|
Ok(NeedIfacePoll::FALSE)
|
2024-08-24 04:00:42 +00:00
|
|
|
}
|
2023-12-28 09:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub const MIN_SENDBUF: u32 = 2304;
|
|
|
|
|
pub const MIN_RECVBUF: u32 = 2304;
|
|
|
|
|
|
2024-12-05 08:40:20 +00:00
|
|
|
/// A trait used for setting socket level options on actual sockets.
|
|
|
|
|
pub(in crate::net) trait SetSocketLevelOption {
|
|
|
|
|
/// Sets whether keepalive messages are enabled.
|
2024-12-02 15:11:43 +00:00
|
|
|
fn set_keep_alive(&self, _keep_alive: bool) -> NeedIfacePoll {
|
2024-12-05 08:40:20 +00:00
|
|
|
NeedIfacePoll::FALSE
|
|
|
|
|
}
|
|
|
|
|
}
|