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