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
9#include "base/logging.h"
10
11namespace net {
12
13Error MapSystemError(int os_error) {
14  // There are numerous posix error codes, but these are the ones we thus far
15  // find interesting.
16  switch (os_error) {
17    case EAGAIN:
18#if EWOULDBLOCK != EAGAIN
19    case EWOULDBLOCK:
20#endif
21      return ERR_IO_PENDING;
22    case EACCES:
23      return ERR_ACCESS_DENIED;
24    case ENETDOWN:
25      return ERR_INTERNET_DISCONNECTED;
26    case ETIMEDOUT:
27      return ERR_TIMED_OUT;
28    case ECONNRESET:
29    case ENETRESET:  // Related to keep-alive
30    case EPIPE:
31      return ERR_CONNECTION_RESET;
32    case ECONNABORTED:
33      return ERR_CONNECTION_ABORTED;
34    case ECONNREFUSED:
35      return ERR_CONNECTION_REFUSED;
36    case EHOSTUNREACH:
37    case EHOSTDOWN:
38    case ENETUNREACH:
39      return ERR_ADDRESS_UNREACHABLE;
40    case EADDRNOTAVAIL:
41      return ERR_ADDRESS_INVALID;
42    case EMSGSIZE:
43      return ERR_MSG_TOO_BIG;
44    case ENOTCONN:
45      return ERR_SOCKET_NOT_CONNECTED;
46    case 0:
47      return OK;
48    default:
49      LOG(WARNING) << "Unknown error " << os_error
50                   << " mapped to net::ERR_FAILED";
51      return ERR_FAILED;
52  }
53}
54
55}  // namespace net
56