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 static constexpr int32_t ERROR_DIVISOR = 1000;
28 static constexpr int32_t ERROR_RANGE = 500;
29 static constexpr const size_t MAX_ERR_LEN = 1024;
30
MakeErrorMessage(int error)31 std::string MakeErrorMessage(int error)
32 {
33 static const std::map<int32_t, std::string> ERROR_MAP = {
34 {PERMISSION_DENIED_CODE, PERMISSION_DENIED_MSG},
35 {TLS_ERR_SYS_EINTR, "Interrupted system call"},
36 {TLS_ERR_SYS_EIO, "I/O error"},
37 {TLS_ERR_SYS_EBADF, "Bad file number"},
38 {TLS_ERR_SYS_EAGAIN, "Resource temporarily unavailable try again"},
39 {TLS_ERR_SYS_EACCES, "System permission denied"},
40 {TLS_ERR_SYS_EFAULT, "Bad address"},
41 {TLS_ERR_SYS_EINVAL, "Invalid system argument"},
42 {TLS_ERR_SYS_ENOTSOCK, "Socket operation on non-socket"},
43 {TLS_ERR_SYS_EPROTOTYPE, "Protocol wrong type for socket"},
44 {TLS_ERR_SYS_EADDRINUSE, "Address already in use"},
45 {TLS_ERR_SYS_EADDRNOTAVAIL, "Cannot assign requested address"},
46 {TLS_ERR_SYS_ENOTCONN, "Transport endpoint is not connected"},
47 {TLS_ERR_SYS_ETIMEDOUT, "Connection timed out"},
48 {TLS_ERR_SSL_NULL, "SSL is null"},
49 {TLS_ERR_WANT_READ, "Error in tls reading"},
50 {TLS_ERR_WANT_WRITE, "Error in tls writing"},
51 {TLS_ERR_WANT_X509_LOOKUP, "Error looking up x509"},
52 {TLS_ERR_SYSCALL, "Error occurred in the tls system call"},
53 {TLS_ERR_ZERO_RETURN, "Error clearing tls connection"},
54 {TLS_ERR_WANT_CONNECT, "Error occurred in the tls connection"},
55 {TLS_ERR_WANT_ACCEPT, "Error occurred in the tls accept"},
56 {TLS_ERR_WANT_ASYNC, "Error occurred in the tls async"},
57 {TLS_ERR_WANT_ASYNC_JOB, "Error occurred in the tls async work"},
58 {TLS_ERR_WANT_CLIENT_HELLO_CB, "Error occured in client hello"},
59 {TLS_ERR_NO_BIND, "No bind socket"},
60 };
61 auto search = ERROR_MAP.find(error);
62 if (search != ERROR_MAP.end()) {
63 return search->second;
64 }
65 if ((error % ERROR_DIVISOR) < ERROR_RANGE) {
66 return strerror(errno);
67 }
68 char err[MAX_ERR_LEN] = {0};
69 ERR_error_string_n(error - TLS_ERR_SYS_BASE, err, sizeof(err));
70 return err;
71 }
72 } // namespace NetStack
73 } // namespace OHOS
74