xref: /aosp_15_r20/external/cronet/net/base/net_errors_posix.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/base/net_errors.h"
6 
7 #include <errno.h>
8 #include <stdlib.h>
9 #include <string>
10 #include <unistd.h>
11 
12 #include "base/logging.h"
13 #include "base/posix/safe_strerror.h"
14 #include "build/build_config.h"
15 
16 namespace net {
17 
MapSystemError(logging::SystemErrorCode os_error)18 Error MapSystemError(logging::SystemErrorCode os_error) {
19   if (os_error != 0)
20     DVLOG(2) << "Error " << os_error << ": "
21              << logging::SystemErrorCodeToString(os_error);
22 
23   // There are numerous posix error codes, but these are the ones we thus far
24   // find interesting.
25   switch (os_error) {
26     case EAGAIN:
27 #if EWOULDBLOCK != EAGAIN
28     case EWOULDBLOCK:
29 #endif
30       return ERR_IO_PENDING;
31     case EACCES:
32       return ERR_ACCESS_DENIED;
33     case ENETDOWN:
34       return ERR_INTERNET_DISCONNECTED;
35     case ETIMEDOUT:
36       return ERR_TIMED_OUT;
37     case ECONNRESET:
38     case ENETRESET:  // Related to keep-alive.
39     case EPIPE:
40       return ERR_CONNECTION_RESET;
41     case ECONNABORTED:
42       return ERR_CONNECTION_ABORTED;
43     case ECONNREFUSED:
44       return ERR_CONNECTION_REFUSED;
45     case EHOSTUNREACH:
46     case EHOSTDOWN:
47     case ENETUNREACH:
48     case EAFNOSUPPORT:
49       return ERR_ADDRESS_UNREACHABLE;
50     case EADDRNOTAVAIL:
51       return ERR_ADDRESS_INVALID;
52     case EMSGSIZE:
53       return ERR_MSG_TOO_BIG;
54     case ENOTCONN:
55       return ERR_SOCKET_NOT_CONNECTED;
56     case EISCONN:
57       return ERR_SOCKET_IS_CONNECTED;
58     case EINVAL:
59       return ERR_INVALID_ARGUMENT;
60     case EADDRINUSE:
61       return ERR_ADDRESS_IN_USE;
62     case E2BIG:  // Argument list too long.
63       return ERR_INVALID_ARGUMENT;
64     case EBADF:  // Bad file descriptor.
65       return ERR_INVALID_HANDLE;
66     case EBUSY:  // Device or resource busy.
67       return ERR_INSUFFICIENT_RESOURCES;
68     case ECANCELED:  // Operation canceled.
69       return ERR_ABORTED;
70     case EDEADLK:  // Resource deadlock avoided.
71       return ERR_INSUFFICIENT_RESOURCES;
72     case EDQUOT:  // Disk quota exceeded.
73       return ERR_FILE_NO_SPACE;
74     case EEXIST:  // File exists.
75       return ERR_FILE_EXISTS;
76     case EFAULT:  // Bad address.
77       return ERR_INVALID_ARGUMENT;
78     case EFBIG:  // File too large.
79       return ERR_FILE_TOO_BIG;
80     case EISDIR:  // Operation not allowed for a directory.
81       return ERR_ACCESS_DENIED;
82     case ENAMETOOLONG:  // Filename too long.
83       return ERR_FILE_PATH_TOO_LONG;
84     case ENFILE:  // Too many open files in system.
85       return ERR_INSUFFICIENT_RESOURCES;
86     case ENOBUFS:  // No buffer space available.
87       return ERR_NO_BUFFER_SPACE;
88     case ENODEV:  // No such device.
89       return ERR_INVALID_ARGUMENT;
90     case ENOENT:  // No such file or directory.
91       return ERR_FILE_NOT_FOUND;
92     case ENOLCK:  // No locks available.
93       return ERR_INSUFFICIENT_RESOURCES;
94     case ENOMEM:  // Not enough space.
95       return ERR_OUT_OF_MEMORY;
96     case ENOSPC:  // No space left on device.
97       return ERR_FILE_NO_SPACE;
98     case ENOSYS:  // Function not implemented.
99       return ERR_NOT_IMPLEMENTED;
100     case ENOTDIR:  // Not a directory.
101       return ERR_FILE_NOT_FOUND;
102     case ENOTSUP:  // Operation not supported.
103       return ERR_NOT_IMPLEMENTED;
104     case EPERM:  // Operation not permitted.
105       return ERR_ACCESS_DENIED;
106     case EROFS:  // Read-only file system.
107       return ERR_ACCESS_DENIED;
108     case ETXTBSY:  // Text file busy.
109       return ERR_ACCESS_DENIED;
110     case EUSERS:  // Too many users.
111       return ERR_INSUFFICIENT_RESOURCES;
112     case EMFILE:  // Too many open files.
113       return ERR_INSUFFICIENT_RESOURCES;
114     case ENOPROTOOPT:  // Protocol option not supported.
115       return ERR_NOT_IMPLEMENTED;
116 #if BUILDFLAG(IS_FUCHSIA)
117     case EIO:
118       // FDIO maps all unrecognized errors to EIO. If you see this message then
119       // consider adding custom error in FDIO for the corresponding error.
120       DLOG(FATAL) << "EIO was returned by FDIO.";
121       return ERR_FAILED;
122 #endif  // BUILDFLAG(IS_FUCHSIA)
123 
124     case 0:
125       return OK;
126     default:
127       LOG(WARNING) << "Unknown error " << base::safe_strerror(os_error) << " ("
128                    << os_error << ") mapped to net::ERR_FAILED";
129       return ERR_FAILED;
130   }
131 }
132 
133 }  // namespace net
134