1 /*
2  * Copyright (c) 2022-2023 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 #ifndef COMMUNICATIONNETSTACK_CONSTANT_H
17 #define COMMUNICATIONNETSTACK_CONSTANT_H
18 
19 #include <map>
20 
21 namespace OHOS::NetStack::Socket {
22 static constexpr const size_t MAX_ERR_NUM = 256;
23 
24 enum SocketErrorCode {
25     SOCKET_ERROR_CODE_BASE = 2301000,
26     SOCKET_SERVER_ERROR_CODE_BASE = 2303100,
27 };
28 
29 #if defined(IOS_PLATFORM)
30 enum OHOSErrorCode {
31     OHOS_ERR_UNDEFINED = -1,
32     OHOS_ERR_EPERM = 1,
33     OHOS_ERR_ENOENT = 2,
34     OHOS_ERR_ESRCH = 3,
35     OHOS_ERR_EINTR = 4,
36     OHOS_ERR_EIO = 5,
37     OHOS_ERR_ENXIO = 6,
38     OHOS_ERR_E2BIG = 7,
39     OHOS_ERR_ENOEXEC = 8,
40     OHOS_ERR_EBADF = 9,
41     OHOS_ERR_ECHILD = 10,
42     OHOS_ERR_EAGAIN = 11,
43     OHOS_ERR_ENOMEM = 12,
44     OHOS_ERR_EACCES = 13,
45     OHOS_ERR_EFAULT = 14,
46     OHOS_ERR_ENOTBLK = 15,
47     OHOS_ERR_EBUSY = 16,
48     OHOS_ERR_EEXIST = 17,
49     OHOS_ERR_EXDEV = 18,
50     OHOS_ERR_ENODEV = 19,
51     OHOS_ERR_ENOTDIR = 20,
52     OHOS_ERR_EISDIR = 21,
53     OHOS_ERR_EINVAL = 22,
54     OHOS_ERR_ENFILE = 23,
55     OHOS_ERR_EMFILE = 24,
56     OHOS_ERR_ENOTTY = 25,
57     OHOS_ERR_ETXTBSY = 26,
58     OHOS_ERR_EFBIG = 27,
59     OHOS_ERR_ENOSPC = 28,
60     OHOS_ERR_ESPIPE = 29,
61     OHOS_ERR_EROFS = 30,
62     OHOS_ERR_EMLINK = 31,
63     OHOS_ERR_EPIPE = 32,
64     OHOS_ERR_EDOM = 33,
65     OHOS_ERR_ERANGE = 34,
66     OHOS_ERR_EDEADLK = 35,
67     OHOS_ERR_ENAMETOOLONG = 36,
68     OHOS_ERR_ENOLCK = 37,
69     OHOS_ERR_ENOSYS = 38,
70     OHOS_ERR_ENOTEMPTY = 39,
71     OHOS_ERR_ELOOP = 40,
72     OHOS_ERR_ENOMSG = 42,
73     OHOS_ERR_EIDRM = 43,
74     OHOS_ERR_EBADE = 52,
75     OHOS_ERR_EBADR = 53,
76     OHOS_ERR_ENOSTR = 60,
77     OHOS_ERR_ENODATA = 61,
78     OHOS_ERR_ETIME = 62,
79     OHOS_ERR_ENOSR = 63,
80     OHOS_ERR_EREMOTE = 66,
81     OHOS_ERR_ENOLINK = 67,
82     OHOS_ERR_EPROTO = 71,
83     OHOS_ERR_EMULTIHOP = 72,
84     OHOS_ERR_EBADMSG = 74,
85     OHOS_ERR_EOVERFLOW = 75,
86     OHOS_ERR_EILSEQ = 84,
87     OHOS_ERR_EUSERS = 87,
88     OHOS_ERR_ENOTSOCK = 88,
89     OHOS_ERR_EDESTADDRREQ = 89,
90     OHOS_ERR_EMSGSIZE = 90,
91     OHOS_ERR_EPROTOTYPE = 91,
92     OHOS_ERR_ENOPROTOOPT = 92,
93     OHOS_ERR_EPROTONOSUPPORT = 93,
94     OHOS_ERR_ESOCKTNOSUPPORT = 94,
95     OHOS_ERR_EOPNOTSUPP = 95,
96     OHOS_ERR_EPFNOSUPPORT = 96,
97     OHOS_ERR_EAFNOSUPPORT = 97,
98     OHOS_ERR_EADDRINUSE = 98,
99     OHOS_ERR_EADDRNOTAVAIL = 99,
100     OHOS_ERR_ENETDOWN = 100,
101     OHOS_ERR_ENETUNREACH = 101,
102     OHOS_ERR_ENETRESET = 102,
103     OHOS_ERR_ECONNABORTED = 103,
104     OHOS_ERR_ECONNRESET = 104,
105     OHOS_ERR_ENOBUFS = 105,
106     OHOS_ERR_EISCONN = 106,
107     OHOS_ERR_ENOTCONN = 107,
108     OHOS_ERR_ESHUTDOWN = 108,
109     OHOS_ERR_ETOOMANYREFS = 109,
110     OHOS_ERR_ETIMEDOUT = 110,
111     OHOS_ERR_ECONNREFUSED = 111,
112     OHOS_ERR_EHOSTDOWN = 112,
113     OHOS_ERR_EHOSTUNREACH = 113,
114     OHOS_ERR_EALREADY = 114,
115     OHOS_ERR_EINPROGRESS = 115,
116     OHOS_ERR_ESTALE = 116,
117     OHOS_ERR_EDQUOT = 122,
118     OHOS_ERR_ECANCELED = 125,
119     OHOS_ERR_EOWNERDEAD = 130,
120     OHOS_ERR_ENOTRECOVERABLE = 131,
121     OHOS_ERR_EWOULDBLOCK = EAGAIN
122 };
123 
124 static const std::map<int32_t, std::pair<OHOSErrorCode, std::string>> errCodeMap = {
125     {EPERM, {OHOSErrorCode::OHOS_ERR_EPERM, "Operation not permitted"}},
126     {ENOENT, {OHOSErrorCode::OHOS_ERR_ENOENT, "No such file or directory"}},
127     {ESRCH, {OHOSErrorCode::OHOS_ERR_ESRCH, "No such process"}},
128     {EINTR, {OHOSErrorCode::OHOS_ERR_EINTR, "Interrupted system call"}},
129     {EIO, {OHOSErrorCode::OHOS_ERR_EIO, "Input/output error"}},
130     {ENXIO, {OHOSErrorCode::OHOS_ERR_ENXIO, "No such device or address"}},
131     {E2BIG, {OHOSErrorCode::OHOS_ERR_E2BIG, "Argument list too long"}},
132     {ENOEXEC, {OHOSErrorCode::OHOS_ERR_ENOEXEC, "Exec format error"}},
133     {EBADF, {OHOSErrorCode::OHOS_ERR_EBADF, "Bad file descriptor"}},
134     {ECHILD, {OHOSErrorCode::OHOS_ERR_ECHILD, "No child processes"}},
135     {EDEADLK, {OHOSErrorCode::OHOS_ERR_EDEADLK, "Resource deadlock avoided"}},
136     {ENOMEM, {OHOSErrorCode::OHOS_ERR_ENOMEM, "Cannot allocate memory"}},
137     {EACCES, {OHOSErrorCode::OHOS_ERR_EACCES, "Permission denied"}},
138     {EFAULT, {OHOSErrorCode::OHOS_ERR_EFAULT, "Bad address"}},
139     {ENOTBLK, {OHOSErrorCode::OHOS_ERR_ENOTBLK, "Block device required"}},
140     {EBUSY, {OHOSErrorCode::OHOS_ERR_EBUSY, "Device or resource busy"}},
141     {EEXIST, {OHOSErrorCode::OHOS_ERR_EEXIST, "File exists"}},
142     {EXDEV, {OHOSErrorCode::OHOS_ERR_EXDEV, "Invalid cross-device link"}},
143     {ENODEV, {OHOSErrorCode::OHOS_ERR_ENODEV, "No such device"}},
144     {ENOTDIR, {OHOSErrorCode::OHOS_ERR_ENOTDIR, "Not a directory"}},
145     {EISDIR, {OHOSErrorCode::OHOS_ERR_EISDIR, "Is a directory"}},
146     {EINVAL, {OHOSErrorCode::OHOS_ERR_EINVAL, "Invalid argument"}},
147     {ENFILE, {OHOSErrorCode::OHOS_ERR_ENFILE, "Too many open files in system"}},
148     {EMFILE, {OHOSErrorCode::OHOS_ERR_EMFILE, "Too many open files"}},
149     {ENOTTY, {OHOSErrorCode::OHOS_ERR_ENOTTY, "Inappropriate ioctl for device"}},
150     {ETXTBSY, {OHOSErrorCode::OHOS_ERR_ETXTBSY, "Text file busy"}},
151     {EFBIG, {OHOSErrorCode::OHOS_ERR_EFBIG, "File too large"}},
152     {ENOSPC, {OHOSErrorCode::OHOS_ERR_ENOSPC, "No space left on device"}},
153     {ESPIPE, {OHOSErrorCode::OHOS_ERR_ESPIPE, "Illegal seek"}},
154     {EROFS, {OHOSErrorCode::OHOS_ERR_EROFS, "Read-only file system"}},
155     {EMLINK, {OHOSErrorCode::OHOS_ERR_EMLINK, "Too many links"}},
156     {EPIPE, {OHOSErrorCode::OHOS_ERR_EPIPE, "Broken pipe"}},
157     {EDOM, {OHOSErrorCode::OHOS_ERR_EDOM, "Numerical argument out of domain"}},
158     {ERANGE, {OHOSErrorCode::OHOS_ERR_ERANGE, "Numerical result out of range"}},
159     {EAGAIN, {OHOSErrorCode::OHOS_ERR_EAGAIN, "Resource temporarily unavailable"}},
160     {EWOULDBLOCK, {OHOSErrorCode::OHOS_ERR_EWOULDBLOCK, "Operation would block"}},
161     {EINPROGRESS, {OHOSErrorCode::OHOS_ERR_EINPROGRESS, "Operation now in progress"}},
162     {EALREADY, {OHOSErrorCode::OHOS_ERR_EALREADY, "Operation already in progress"}},
163     {ENOTSOCK, {OHOSErrorCode::OHOS_ERR_ENOTSOCK, "Socket operation on non-socket"}},
164     {EDESTADDRREQ, {OHOSErrorCode::OHOS_ERR_EDESTADDRREQ, "Destination address required"}},
165     {EMSGSIZE, {OHOSErrorCode::OHOS_ERR_EMSGSIZE, "Message too long"}},
166     {EPROTOTYPE, {OHOSErrorCode::OHOS_ERR_EPROTOTYPE, "Protocol wrong type for socket"}},
167     {ENOPROTOOPT, {OHOSErrorCode::OHOS_ERR_ENOPROTOOPT, "Protocol not available"}},
168     {EPROTONOSUPPORT, {OHOSErrorCode::OHOS_ERR_EPROTONOSUPPORT, "Protocol not supported"}},
169     {ESOCKTNOSUPPORT, {OHOSErrorCode::OHOS_ERR_ESOCKTNOSUPPORT, "Socket type not supported"}},
170     {ENOTSUP, {OHOSErrorCode::OHOS_ERR_UNDEFINED, "Unknown Other Error"}},
171     {EOPNOTSUPP, {OHOSErrorCode::OHOS_ERR_EOPNOTSUPP, "Operation not supported"}},
172     {EPFNOSUPPORT, {OHOSErrorCode::OHOS_ERR_EPFNOSUPPORT, "Protocol family not supported"}},
173     {EAFNOSUPPORT, {OHOSErrorCode::OHOS_ERR_EAFNOSUPPORT, "Address family not supported by protocol"}},
174     {EADDRINUSE, {OHOSErrorCode::OHOS_ERR_EADDRINUSE, "Address already in use"}},
175     {EADDRNOTAVAIL, {OHOSErrorCode::OHOS_ERR_EADDRNOTAVAIL, "Cannot assign requested address"}},
176     {ENETDOWN, {OHOSErrorCode::OHOS_ERR_ENETDOWN, "Network is down"}},
177     {ENETUNREACH, {OHOSErrorCode::OHOS_ERR_ENETUNREACH, "Network is unreachable"}},
178     {ENETRESET, {OHOSErrorCode::OHOS_ERR_ENETRESET, "Network dropped connection on reset"}},
179     {ECONNABORTED, {OHOSErrorCode::OHOS_ERR_ECONNABORTED, "Software caused connection abort"}},
180     {ECONNRESET, {OHOSErrorCode::OHOS_ERR_ECONNRESET, "Connection reset by peer"}},
181     {ENOBUFS, {OHOSErrorCode::OHOS_ERR_ENOBUFS, "No buffer space available"}},
182     {EISCONN, {OHOSErrorCode::OHOS_ERR_EISCONN, "Transport endpoint is already connected"}},
183     {ENOTCONN, {OHOSErrorCode::OHOS_ERR_ENOTCONN, "Transport endpoint is not connected"}},
184     {ESHUTDOWN, {OHOSErrorCode::OHOS_ERR_ESHUTDOWN, "Cannot send after transport endpoint shutdown"}},
185     {ETOOMANYREFS, {OHOSErrorCode::OHOS_ERR_ETOOMANYREFS, "Too many references: cannot splice"}},
186     {ETIMEDOUT, {OHOSErrorCode::OHOS_ERR_ETIMEDOUT, "Connection timed out"}},
187     {ECONNREFUSED, {OHOSErrorCode::OHOS_ERR_ECONNREFUSED, "Connection refused"}},
188     {ELOOP, {OHOSErrorCode::OHOS_ERR_ELOOP, "Too many levels of symbolic links"}},
189     {ENAMETOOLONG, {OHOSErrorCode::OHOS_ERR_ENAMETOOLONG, "File name too long"}},
190     {EHOSTDOWN, {OHOSErrorCode::OHOS_ERR_EHOSTDOWN, "Host is down"}},
191     {EHOSTUNREACH, {OHOSErrorCode::OHOS_ERR_EHOSTUNREACH, "No route to host"}},
192     {ENOTEMPTY, {OHOSErrorCode::OHOS_ERR_ENOTEMPTY, "Directory not empty"}},
193     {EPROCLIM, {OHOSErrorCode::OHOS_ERR_UNDEFINED, "Unknown Other Error"}},
194     {EUSERS, {OHOSErrorCode::OHOS_ERR_EUSERS, "Too many users"}},
195     {EDQUOT, {OHOSErrorCode::OHOS_ERR_EDQUOT, "Disk quota exceeded"}},
196     {ESTALE, {OHOSErrorCode::OHOS_ERR_ESTALE, "Stale NFS file handle"}},
197     {EREMOTE, {OHOSErrorCode::OHOS_ERR_EREMOTE, "Object is remote"}},
198     {EBADRPC, {OHOSErrorCode::OHOS_ERR_UNDEFINED, "Unknown Other Error"}},
199     {ERPCMISMATCH, {OHOSErrorCode::OHOS_ERR_UNDEFINED, "Unknown Other Error"}},
200     {EPROGUNAVAIL, {OHOSErrorCode::OHOS_ERR_UNDEFINED, "Unknown Other Error"}},
201     {EPROGMISMATCH, {OHOSErrorCode::OHOS_ERR_UNDEFINED, "Unknown Other Error"}},
202     {EPROCUNAVAIL, {OHOSErrorCode::OHOS_ERR_UNDEFINED, "Unknown Other Error"}},
203     {ENOLCK, {OHOSErrorCode::OHOS_ERR_ENOLCK, "No locks available"}},
204     {ENOSYS, {OHOSErrorCode::OHOS_ERR_ENOSYS, "Function not implemented"}},
205     {EFTYPE, {OHOSErrorCode::OHOS_ERR_UNDEFINED, "Unknown Other Error"}},
206     {EAUTH, {OHOSErrorCode::OHOS_ERR_UNDEFINED, "Unknown Other Error"}},
207     {ENEEDAUTH, {OHOSErrorCode::OHOS_ERR_UNDEFINED, "Unknown Other Error"}},
208     {EPWROFF, {OHOSErrorCode::OHOS_ERR_UNDEFINED, "Unknown Other Error"}},
209     {EDEVERR, {OHOSErrorCode::OHOS_ERR_UNDEFINED, "Unknown Other Error"}},
210     {EOVERFLOW, {OHOSErrorCode::OHOS_ERR_EOVERFLOW, "Value too large for defined data type"}},
211     {EBADEXEC, {OHOSErrorCode::OHOS_ERR_EBADE, "Invalid exchange"}},
212     {EBADARCH, {OHOSErrorCode::OHOS_ERR_EBADR, "Invalid request descriptor"}},
213     {ESHLIBVERS, {OHOSErrorCode::OHOS_ERR_UNDEFINED, "Unknown Other Error"}},
214     {EBADMACHO, {OHOSErrorCode::OHOS_ERR_UNDEFINED, "Unknown Other Error"}},
215     {ECANCELED, {OHOSErrorCode::OHOS_ERR_ECANCELED, "Operation Canceled"}},
216     {EIDRM, {OHOSErrorCode::OHOS_ERR_EIDRM, "Identifier removed"}},
217     {ENOMSG, {OHOSErrorCode::OHOS_ERR_ENOMSG, "No message of desired type"}},
218     {EILSEQ, {OHOSErrorCode::OHOS_ERR_EILSEQ, "Invalid or incomplete multibyte or wide character"}},
219     {ENOATTR, {OHOSErrorCode::OHOS_ERR_UNDEFINED, "Unknown Other Error"}},
220     {EBADMSG, {OHOSErrorCode::OHOS_ERR_EBADMSG, "Bad message"}},
221     {EMULTIHOP, {OHOSErrorCode::OHOS_ERR_EMULTIHOP, "Multihop attempted"}},
222     {ENODATA, {OHOSErrorCode::OHOS_ERR_ENODATA, "No data available"}},
223     {ENOLINK, {OHOSErrorCode::OHOS_ERR_ENOLINK, "Link has been severed"}},
224     {ENOSR, {OHOSErrorCode::OHOS_ERR_ENOSR, "Out of streams resources"}},
225     {ENOSTR, {OHOSErrorCode::OHOS_ERR_ENOSTR, "Device not a stream"}},
226     {EPROTO, {OHOSErrorCode::OHOS_ERR_EPROTO, "Protocol error"}},
227     {ETIME, {OHOSErrorCode::OHOS_ERR_ETIME, "Timer expired"}},
228     {EOPNOTSUPP, {OHOSErrorCode::OHOS_ERR_EOPNOTSUPP, "Operation not supported"}},
229     {ENOPOLICY, {OHOSErrorCode::OHOS_ERR_UNDEFINED, "Unknown Other Error"}},
230     {ENOTRECOVERABLE, {OHOSErrorCode::OHOS_ERR_ENOTRECOVERABLE, "State not recoverable"}},
231     {EOWNERDEAD, {OHOSErrorCode::OHOS_ERR_EOWNERDEAD, "Owner died"}},
232     {EQFULL, {OHOSErrorCode::OHOS_ERR_UNDEFINED, "Unknown Other Error"}},
233     {ELAST, {OHOSErrorCode::OHOS_ERR_UNDEFINED, "Unknown Other Error"}},
234 };
235 
236 class ErrCodePlatformAdapter {
237 public:
GetOHOSErrCode(int32_t iosErrCode)238     static int32_t GetOHOSErrCode(int32_t iosErrCode)
239     {
240         auto iter = errCodeMap.find(iosErrCode);
241         if (iter != errCodeMap.end()) {
242             return static_cast<int32_t>(iter->second.first);
243         }
244         return static_cast<int32_t>(OHOSErrorCode::OHOS_ERR_UNDEFINED);
245     }
246 
GetOHOSErrMessage(int32_t iosErrCode,std::string & errMessage)247     static void GetOHOSErrMessage(int32_t iosErrCode, std::string &errMessage)
248     {
249         auto iter = errCodeMap.find(iosErrCode);
250         if (iter != errCodeMap.end()) {
251             errMessage = iter->second.second;
252         } else {
253             errMessage = "Unknown Other Error";
254         }
255     }
256 };
257 #endif
258 } // namespace OHOS::NetStack::Socket
259 
260 #endif /* COMMUNICATIONNETSTACK_CONSTANT_H */
261