psp_net/types/
socket_options.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#![allow(clippy::module_name_repetitions)]

use alloc::string::String;

use crate::socket::SocketAddr;

use super::Certificate;

/// Socket options, such as remote address to connect to.
///
/// This is used by [`TcpSocket`](super::socket::tcp::TcpSocket) and
/// [`UdpSocket`](super::socket::udp::UdpSocket) when used as
/// [`EasySocket`](super::traits::io::EasySocket)s.
///
/// # Fields
/// - [`remote`](Self::remote): Remote address to connect to
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct SocketOptions {
    /// Remote address to connect to
    remote: SocketAddr,
}

impl SocketOptions {
    /// Create a new socket options
    #[must_use]
    pub fn new(remote: SocketAddr) -> SocketOptions {
        Self { remote }
    }

    /// Get the remote address
    #[must_use]
    pub fn remote(&self) -> SocketAddr {
        self.remote
    }
}

/// TLS socket options.
///
/// This is used by [`TlsSocket`](super::socket::tls::TlsSocket) when used as a
/// [`EasySocket`](super::traits::io::EasySocket).
#[derive(Clone, Debug)]
pub struct TlsSocketOptions<'a> {
    /// The seed to use for the RNG
    seed: u64,
    /// The server name to use
    server_name: String,
    /// The certificate
    cert: Option<Certificate<'a>>,
    /// The CA
    ca: Option<Certificate<'a>>,
    /// Whether RSA signatures should be enabled
    enable_rsa_signatures: bool,
    /// Whether the max fragment length should be reset
    reset_max_fragment_length: bool,
}

impl<'a> TlsSocketOptions<'a> {
    /// Create a new socket options
    ///
    /// # Arguments
    /// - `seed`: The seed to use for the RNG
    /// - `server_name`: The server name to use
    ///
    /// # Returns
    /// - A new socket options object
    ///
    /// # Notes
    /// By default
    /// - RSA signatures are enabled
    /// - The max fragment length is not reset
    /// - The certificate and CA are not set.
    #[must_use]
    pub fn new(seed: u64, server_name: String) -> Self {
        Self {
            seed,
            server_name,
            cert: None,
            ca: None,
            enable_rsa_signatures: true,
            reset_max_fragment_length: false,
        }
    }

    /// Disable RSA signatures
    ///
    /// By default, RSA signatures are enabled.
    pub fn disable_rsa_signatures(&mut self) {
        self.enable_rsa_signatures = false;
    }

    /// Set the certificate
    ///
    /// # Arguments
    /// - `cert`: The certificate
    pub fn set_cert(&mut self, cert: Option<Certificate<'a>>) {
        self.cert = cert;
    }

    /// Get the seed
    #[must_use]
    pub fn seed(&self) -> u64 {
        self.seed
    }

    /// Get the server name
    #[must_use]
    pub fn server_name(&self) -> &str {
        &self.server_name
    }

    /// Get the certificate
    #[must_use]
    pub fn cert(&self) -> Option<&Certificate<'a>> {
        self.cert.as_ref()
    }

    /// Return whether RSA signatures are enabled
    #[must_use]
    pub fn rsa_signatures_enabled(&self) -> bool {
        self.enable_rsa_signatures
    }

    /// Return whether the max fragment length should be reset
    #[must_use]
    pub fn reset_max_fragment_length(&self) -> bool {
        self.reset_max_fragment_length
    }

    /// Set whether the max fragment length should be reset
    ///
    /// By default, the max fragment length is not reset.
    pub fn set_reset_max_fragment_length(&mut self, reset_max_fragment_length: bool) {
        self.reset_max_fragment_length = reset_max_fragment_length;
    }

    /// Get the CA
    #[must_use]
    pub fn ca(&self) -> Option<&Certificate<'a>> {
        self.ca.as_ref()
    }

    /// Set the CA (certificate authority)
    ///
    /// # Arguments
    /// - `ca`: The CA
    pub fn set_ca(&mut self, ca: Option<Certificate<'a>>) {
        self.ca = ca;
    }

    /// Set whether RSA signatures should be enabled
    ///
    /// # Arguments
    /// - `enable_rsa_signatures`: Whether RSA signatures should be enabled
    pub fn set_enable_rsa_signatures(&mut self, enable_rsa_signatures: bool) {
        self.enable_rsa_signatures = enable_rsa_signatures;
    }
}