Home | History | Annotate | Download | only in base
      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 
      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/strings/stringprintf.h"
     14 
     15 namespace net {
     16 
     17 Error MapSystemError(int os_error) {
     18   if (os_error != 0)
     19     DVLOG(2) << "Error " << os_error;
     20 
     21   // There are numerous posix error codes, but these are the ones we thus far
     22   // find interesting.
     23   switch (os_error) {
     24     case EAGAIN:
     25 #if EWOULDBLOCK != EAGAIN
     26     case EWOULDBLOCK:
     27 #endif
     28       return ERR_IO_PENDING;
     29     case EACCES:
     30       return ERR_ACCESS_DENIED;
     31     case ENETDOWN:
     32       return ERR_INTERNET_DISCONNECTED;
     33     case ETIMEDOUT:
     34       return ERR_TIMED_OUT;
     35     case ECONNRESET:
     36     case ENETRESET:  // Related to keep-alive.
     37     case EPIPE:
     38       return ERR_CONNECTION_RESET;
     39     case ECONNABORTED:
     40       return ERR_CONNECTION_ABORTED;
     41     case ECONNREFUSED:
     42       return ERR_CONNECTION_REFUSED;
     43     case EHOSTUNREACH:
     44     case EHOSTDOWN:
     45     case ENETUNREACH:
     46     case EAFNOSUPPORT:
     47       return ERR_ADDRESS_UNREACHABLE;
     48     case EADDRNOTAVAIL:
     49       return ERR_ADDRESS_INVALID;
     50     case EMSGSIZE:
     51       return ERR_MSG_TOO_BIG;
     52     case ENOTCONN:
     53       return ERR_SOCKET_NOT_CONNECTED;
     54     case EISCONN:
     55       return ERR_SOCKET_IS_CONNECTED;
     56     case EINVAL:
     57       return ERR_INVALID_ARGUMENT;
     58     case EADDRINUSE:
     59       return ERR_ADDRESS_IN_USE;
     60     case E2BIG:  // Argument list too long.
     61       return ERR_INVALID_ARGUMENT;
     62     case EBADF:  // Bad file descriptor.
     63       return ERR_INVALID_HANDLE;
     64     case EBUSY:  // Device or resource busy.
     65       return ERR_INSUFFICIENT_RESOURCES;
     66     case ECANCELED:  // Operation canceled.
     67       return ERR_ABORTED;
     68     case EDEADLK:  // Resource deadlock avoided.
     69       return ERR_INSUFFICIENT_RESOURCES;
     70     case EDQUOT:  // Disk quota exceeded.
     71       return ERR_FILE_NO_SPACE;
     72     case EEXIST:  // File exists.
     73       return ERR_FILE_EXISTS;
     74     case EFAULT:  // Bad address.
     75       return ERR_INVALID_ARGUMENT;
     76     case EFBIG:  // File too large.
     77       return ERR_FILE_TOO_BIG;
     78     case EISDIR:  // Operation not allowed for a directory.
     79       return ERR_ACCESS_DENIED;
     80     case ENAMETOOLONG:  // Filename too long.
     81       return ERR_FILE_PATH_TOO_LONG;
     82     case ENFILE:  // Too many open files in system.
     83       return ERR_INSUFFICIENT_RESOURCES;
     84     case ENOBUFS:  // No buffer space available.
     85       return ERR_OUT_OF_MEMORY;
     86     case ENODEV:  // No such device.
     87       return ERR_INVALID_ARGUMENT;
     88     case ENOENT:  // No such file or directory.
     89       return ERR_FILE_NOT_FOUND;
     90     case ENOLCK:  // No locks available.
     91       return ERR_INSUFFICIENT_RESOURCES;
     92     case ENOMEM:  // Not enough space.
     93       return ERR_OUT_OF_MEMORY;
     94     case ENOSPC:  // No space left on device.
     95       return ERR_FILE_NO_SPACE;
     96     case ENOSYS:  // Function not implemented.
     97       return ERR_NOT_IMPLEMENTED;
     98     case ENOTDIR:  // Not a directory.
     99       return ERR_FILE_NOT_FOUND;
    100     case ENOTSUP:  // Operation not supported.
    101       return ERR_NOT_IMPLEMENTED;
    102     case EPERM:  // Operation not permitted.
    103       return ERR_ACCESS_DENIED;
    104     case EROFS:  // Read-only file system.
    105       return ERR_ACCESS_DENIED;
    106     case ETXTBSY:  // Text file busy.
    107       return ERR_ACCESS_DENIED;
    108     case EUSERS:  // Too many users.
    109       return ERR_INSUFFICIENT_RESOURCES;
    110     case EMFILE:  // Too many open files.
    111       return ERR_INSUFFICIENT_RESOURCES;
    112 
    113     case 0:
    114       return OK;
    115     default:
    116       LOG(WARNING) << "Unknown error " << os_error
    117                    << " mapped to net::ERR_FAILED";
    118       return ERR_FAILED;
    119   }
    120 }
    121 
    122 }  // namespace net
    123