1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "socket_error.h"
17 
18 #include <cstring>
19 #include <map>
20 
21 #include <openssl/err.h>
22 #include <openssl/ssl.h>
23 #include "base_context.h"
24 
25 namespace OHOS {
26 namespace NetStack {
27 namespace TlsSocket {
28 static constexpr int32_t ERROR_DIVISOR = 1000;
29 static constexpr int32_t ERROR_RANGE = 500;
30 static constexpr const size_t MAX_ERR_LEN = 1024;
31 static constexpr int32_t PARSE_ERROR_CODE = 401;
32 static constexpr const char *PARSE_ERROR_MSG = "Parameter error";
33 
MakeErrorMessage(int error)34 std::string MakeErrorMessage(int error)
35 {
36     static const std::map<int32_t, std::string> ERROR_MAP = {
37         {PERMISSION_DENIED_CODE, PERMISSION_DENIED_MSG},
38         {PARSE_ERROR_CODE, PARSE_ERROR_MSG},
39         {SYSTEM_INTERNAL_ERROR, "System internal error"},
40         {TLS_ERR_SYS_EINTR, "Interrupted system call"},
41         {TLS_ERR_SYS_EIO, "I/O error"},
42         {TLS_ERR_SYS_EBADF, "Bad file number"},
43         {TLS_ERR_SYS_EAGAIN, "Resource temporarily unavailable try again"},
44         {TLS_ERR_SYS_EACCES, "System permission denied"},
45         {TLS_ERR_SYS_EFAULT, "Bad address"},
46         {TLS_ERR_SYS_EINVAL, "Invalid system argument"},
47         {TLS_ERR_SYS_ENOTSOCK, "Socket operation on non-socket"},
48         {TLS_ERR_SYS_EPROTOTYPE, "Protocol wrong type for socket"},
49         {TLS_ERR_SYS_EADDRINUSE, "Address already in use"},
50         {TLS_ERR_SYS_EADDRNOTAVAIL, "Cannot assign requested address"},
51         {TLS_ERR_SYS_ENOTCONN, "Transport endpoint is not connected"},
52         {TLS_ERR_SYS_ETIMEDOUT, "Connection timed out"},
53         {TLS_ERR_SSL_NULL, "SSL is null"},
54         {TLS_ERR_WANT_READ, "Error in tls reading"},
55         {TLS_ERR_WANT_WRITE, "Error in tls writing"},
56         {TLS_ERR_WANT_X509_LOOKUP, "Error looking up x509"},
57         {TLS_ERR_SYSCALL, "Error occurred in the tls system call"},
58         {TLS_ERR_ZERO_RETURN, "Error clearing tls connection"},
59         {TLS_ERR_WANT_CONNECT, "Error occurred in the tls connection"},
60         {TLS_ERR_WANT_ACCEPT, "Error occurred in the tls accept"},
61         {TLS_ERR_WANT_ASYNC, "Error occurred in the tls async"},
62         {TLS_ERR_WANT_ASYNC_JOB, "Error occurred in the tls async work"},
63         {TLS_ERR_WANT_CLIENT_HELLO_CB, "Error occured in client hello"},
64         {TLS_ERR_NO_BIND, "No bind socket"},
65         {TLS_ERR_SOCK_INVALID_FD, "Invalid socket FD"},
66         {TLS_ERR_SOCK_NOT_CONNECT, "Socket is not connected"},
67     };
68     auto search = ERROR_MAP.find(error);
69     if (search != ERROR_MAP.end()) {
70         return search->second;
71     }
72     if ((error % ERROR_DIVISOR) < ERROR_RANGE) {
73         return strerror(errno);
74     }
75     char err[MAX_ERR_LEN] = {0};
76     ERR_error_string_n(error - TLS_ERR_SYS_BASE, err, sizeof(err));
77     return err;
78 }
79 } // namespace TlsSocket
80 } // namespace NetStack
81 } // namespace OHOS
82