psp_net/types/socket_options.rs
1#![allow(clippy::module_name_repetitions)]
2
3use alloc::string::String;
4use psp::sys;
5
6use crate::socket::SocketAddr;
7
8use super::Certificate;
9
10/// Socket options, such as remote address to connect to.
11///
12/// This is used by [`TcpSocket`](super::socket::tcp::TcpSocket) and
13/// [`UdpSocket`](super::socket::udp::UdpSocket) when used as
14/// [`EasySocket`](super::traits::io::EasySocket)s.
15///
16/// # Fields
17/// - [`remote`](Self::remote): Remote address to connect to
18#[derive(Clone, Copy, Debug, PartialEq, Eq)]
19pub struct SocketOptions {
20 /// Remote address to connect to
21 remote: SocketAddr,
22}
23
24impl SocketOptions {
25 /// Create a new socket options
26 #[must_use]
27 pub fn new(remote: SocketAddr) -> SocketOptions {
28 Self { remote }
29 }
30
31 /// Get the remote address
32 #[must_use]
33 pub fn remote(&self) -> SocketAddr {
34 self.remote
35 }
36}
37
38/// TLS socket options.
39///
40/// This is used by [`TlsSocket`](super::socket::tls::TlsSocket) when used as a
41/// [`EasySocket`](super::traits::io::EasySocket).
42#[derive(Clone, Debug, Default)]
43pub struct TlsSocketOptions<'a> {
44 /// The seed to use for the RNG
45 seed: u64,
46 /// The server name to use
47 server_name: String,
48 /// The certificate
49 cert: Option<Certificate<'a>>,
50 /// The CA
51 ca: Option<Certificate<'a>>,
52 /// Whether RSA signatures should be enabled
53 enable_rsa_signatures: bool,
54 /// Whether the max fragment length should be reset
55 reset_max_fragment_length: bool,
56}
57
58impl<'a> TlsSocketOptions<'a> {
59 /// Create a new socket options
60 ///
61 /// # Arguments
62 /// - `seed`: The seed to use for the RNG
63 /// - `server_name`: The server name to use
64 ///
65 /// # Returns
66 /// - A new socket options object
67 ///
68 /// # Notes
69 /// By default
70 /// - RSA signatures are enabled
71 /// - The max fragment length is not reset
72 /// - The certificate and CA are not set.
73 #[must_use]
74 pub fn new<S>(seed: u64, server_name: S) -> Self
75 where
76 S: Into<String>,
77 {
78 Self {
79 seed,
80 server_name: server_name.into(),
81 cert: None,
82 ca: None,
83 enable_rsa_signatures: true,
84 reset_max_fragment_length: false,
85 }
86 }
87
88 /// Create a new socket options with a seed based on the current time
89 ///
90 /// # Returns
91 /// - A new socket options object
92 ///
93 /// # Notes
94 /// Like [`TlsSocketOptions::new`], but the seed is based on the current time.
95 /// Uses [`sys::sceRtcGetCurrentTick`] to get the current time.
96 #[must_use]
97 pub fn new_with_seed_from_time<S>(server_name: S) -> Self
98 where
99 S: Into<String>,
100 {
101 let seed = unsafe {
102 let mut seed: u64 = 0;
103 sys::sceRtcGetCurrentTick(&mut seed);
104 seed
105 };
106
107 Self::new(seed, server_name)
108 }
109
110 /// Disable RSA signatures
111 ///
112 /// By default, RSA signatures are enabled.
113 pub fn disable_rsa_signatures(&mut self) {
114 self.enable_rsa_signatures = false;
115 }
116
117 /// Set the certificate
118 ///
119 /// # Arguments
120 /// - `cert`: The certificate
121 pub fn set_cert(&mut self, cert: Option<Certificate<'a>>) {
122 self.cert = cert;
123 }
124
125 /// Get the seed
126 #[must_use]
127 pub fn seed(&self) -> u64 {
128 self.seed
129 }
130
131 /// Set the seed
132 ///
133 /// # Arguments
134 /// - `seed`: The seed
135 pub fn set_seed(&mut self, seed: u64) {
136 self.seed = seed;
137 }
138
139 /// Get the server name
140 #[must_use]
141 pub fn server_name(&self) -> &str {
142 &self.server_name
143 }
144
145 /// Get the certificate
146 #[must_use]
147 pub fn cert(&self) -> Option<&Certificate<'a>> {
148 self.cert.as_ref()
149 }
150
151 /// Return whether RSA signatures are enabled
152 #[must_use]
153 pub fn rsa_signatures_enabled(&self) -> bool {
154 self.enable_rsa_signatures
155 }
156
157 /// Return whether the max fragment length should be reset
158 #[must_use]
159 pub fn reset_max_fragment_length(&self) -> bool {
160 self.reset_max_fragment_length
161 }
162
163 /// Set whether the max fragment length should be reset
164 ///
165 /// By default, the max fragment length is not reset.
166 pub fn set_reset_max_fragment_length(&mut self, reset_max_fragment_length: bool) {
167 self.reset_max_fragment_length = reset_max_fragment_length;
168 }
169
170 /// Get the CA
171 #[must_use]
172 pub fn ca(&self) -> Option<&Certificate<'a>> {
173 self.ca.as_ref()
174 }
175
176 /// Set the CA (certificate authority)
177 ///
178 /// # Arguments
179 /// - `ca`: The CA
180 pub fn set_ca(&mut self, ca: Option<Certificate<'a>>) {
181 self.ca = ca;
182 }
183
184 /// Set whether RSA signatures should be enabled
185 ///
186 /// # Arguments
187 /// - `enable_rsa_signatures`: Whether RSA signatures should be enabled
188 pub fn set_enable_rsa_signatures(&mut self, enable_rsa_signatures: bool) {
189 self.enable_rsa_signatures = enable_rsa_signatures;
190 }
191
192 /// Set the server name
193 ///
194 /// # Arguments
195 /// - `server_name`: The server name
196 pub fn set_server_name<S>(&mut self, server_name: S)
197 where
198 S: Into<String>,
199 {
200 self.server_name = server_name.into();
201 }
202}