optee_utee/net/
error.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use optee_utee_sys as raw;
19
20#[derive(Debug, Clone, Eq, PartialEq)]
21pub enum SocketError {
22    ErrorProtocol(u32),
23    RemoteClosed,
24    Timeout,
25    OutOfResource,
26    WarningProtocol(u32),
27    LargeBuffer,
28    Hostname,
29    Tee(crate::ErrorKind),
30    Unknown(u32),
31}
32
33impl SocketError {
34    pub fn from_raw_error(code: u32, protocol_error: u32) -> Self {
35        match code {
36            raw::TEE_ISOCKET_ERROR_PROTOCOL => Self::ErrorProtocol(protocol_error),
37            raw::TEE_ISOCKET_ERROR_REMOTE_CLOSED => Self::RemoteClosed,
38            raw::TEE_ISOCKET_ERROR_TIMEOUT => Self::Timeout,
39            raw::TEE_ISOCKET_ERROR_OUT_OF_RESOURCES => Self::OutOfResource,
40            raw::TEE_ISOCKET_ERROR_LARGE_BUFFER => Self::LargeBuffer,
41            raw::TEE_ISOCKET_WARNING_PROTOCOL => Self::WarningProtocol(protocol_error),
42            raw::TEE_ISOCKET_ERROR_HOSTNAME => Self::Hostname,
43            raw::TEE_ERROR_CANCEL
44            | raw::TEE_ERROR_COMMUNICATION
45            | raw::TEE_ERROR_OUT_OF_MEMORY
46            | raw::TEE_ERROR_BAD_PARAMETERS => Self::Tee(crate::Error::from_raw_error(code).kind()),
47            _ => Self::Unknown(code),
48        }
49    }
50}
51
52impl core::fmt::Display for SocketError {
53    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
54        write!(f, "{:?}", self)
55    }
56}
57
58impl From<crate::Error> for SocketError {
59    fn from(value: crate::Error) -> Self {
60        Self::Tee(value.kind())
61    }
62}