net_errors_posix.cc 4.2 KB

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