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 <winsock2.h>
8
9#include "base/logging.h"
10
11namespace net {
12
13// Map winsock and system errors to Chromium errors.
14Error MapSystemError(int os_error) {
15  if (os_error != 0)
16    DVLOG(2) << "Error " << os_error;
17
18  // There are numerous Winsock error codes, but these are the ones we thus far
19  // find interesting.
20  switch (os_error) {
21    case WSAEWOULDBLOCK:
22    case WSA_IO_PENDING:
23      return ERR_IO_PENDING;
24    case WSAEACCES:
25      return ERR_ACCESS_DENIED;
26    case WSAENETDOWN:
27      return ERR_INTERNET_DISCONNECTED;
28    case WSAETIMEDOUT:
29      return ERR_TIMED_OUT;
30    case WSAECONNRESET:
31    case WSAENETRESET:  // Related to keep-alive
32      return ERR_CONNECTION_RESET;
33    case WSAECONNABORTED:
34      return ERR_CONNECTION_ABORTED;
35    case WSAECONNREFUSED:
36      return ERR_CONNECTION_REFUSED;
37    case WSA_IO_INCOMPLETE:
38    case WSAEDISCON:
39      return ERR_CONNECTION_CLOSED;
40    case WSAEISCONN:
41      return ERR_SOCKET_IS_CONNECTED;
42    case WSAEHOSTUNREACH:
43    case WSAENETUNREACH:
44      return ERR_ADDRESS_UNREACHABLE;
45    case WSAEADDRNOTAVAIL:
46      return ERR_ADDRESS_INVALID;
47    case WSAEMSGSIZE:
48      return ERR_MSG_TOO_BIG;
49    case WSAENOTCONN:
50      return ERR_SOCKET_NOT_CONNECTED;
51    case WSAEAFNOSUPPORT:
52      return ERR_ADDRESS_UNREACHABLE;
53    case WSAEINVAL:
54      return ERR_INVALID_ARGUMENT;
55    case WSAEADDRINUSE:
56      return ERR_ADDRESS_IN_USE;
57
58    // System errors.
59    case ERROR_FILE_NOT_FOUND:  // The system cannot find the file specified.
60      return ERR_FILE_NOT_FOUND;
61    case ERROR_PATH_NOT_FOUND:  // The system cannot find the path specified.
62      return ERR_FILE_NOT_FOUND;
63    case ERROR_TOO_MANY_OPEN_FILES:  // The system cannot open the file.
64      return ERR_INSUFFICIENT_RESOURCES;
65    case ERROR_ACCESS_DENIED:  // Access is denied.
66      return ERR_ACCESS_DENIED;
67    case ERROR_INVALID_HANDLE:  // The handle is invalid.
68      return ERR_INVALID_HANDLE;
69    case ERROR_NOT_ENOUGH_MEMORY:  // Not enough storage is available to
70      return ERR_OUT_OF_MEMORY;    // process this command.
71    case ERROR_OUTOFMEMORY:      // Not enough storage is available to complete
72      return ERR_OUT_OF_MEMORY;  // this operation.
73    case ERROR_WRITE_PROTECT:  // The media is write protected.
74      return ERR_ACCESS_DENIED;
75    case ERROR_SHARING_VIOLATION:  // Cannot access the file because it is
76      return ERR_ACCESS_DENIED;    // being used by another process.
77    case ERROR_LOCK_VIOLATION:   // The process cannot access the file because
78      return ERR_ACCESS_DENIED;  // another process has locked the file.
79    case ERROR_HANDLE_EOF:  // Reached the end of the file.
80      return ERR_FAILED;
81    case ERROR_HANDLE_DISK_FULL:  // The disk is full.
82      return ERR_FILE_NO_SPACE;
83    case ERROR_FILE_EXISTS:  // The file exists.
84      return ERR_FILE_EXISTS;
85    case ERROR_INVALID_PARAMETER:  // The parameter is incorrect.
86      return ERR_INVALID_ARGUMENT;
87    case ERROR_BUFFER_OVERFLOW:  // The file name is too long.
88      return ERR_FILE_PATH_TOO_LONG;
89    case ERROR_DISK_FULL:  // There is not enough space on the disk.
90      return ERR_FILE_NO_SPACE;
91    case ERROR_CALL_NOT_IMPLEMENTED:  // This function is not supported on
92      return ERR_NOT_IMPLEMENTED;     // this system.
93    case ERROR_INVALID_NAME:        // The filename, directory name, or volume
94      return ERR_INVALID_ARGUMENT;  // label syntax is incorrect.
95    case ERROR_DIR_NOT_EMPTY:  // The directory is not empty.
96      return ERR_FAILED;
97    case ERROR_BUSY:  // The requested resource is in use.
98      return ERR_ACCESS_DENIED;
99    case ERROR_ALREADY_EXISTS:  // Cannot create a file when that file
100      return ERR_FILE_EXISTS;   // already exists.
101    case ERROR_FILENAME_EXCED_RANGE:  // The filename or extension is too long.
102      return ERR_FILE_PATH_TOO_LONG;
103    case ERROR_FILE_TOO_LARGE:   // The file size exceeds the limit allowed
104      return ERR_FILE_NO_SPACE;  // and cannot be saved.
105    case ERROR_VIRUS_INFECTED:         // Operation failed because the file
106      return ERR_FILE_VIRUS_INFECTED;  // contains a virus.
107    case ERROR_IO_DEVICE:        // The request could not be performed
108      return ERR_ACCESS_DENIED;  // because of an I/O device error.
109    case ERROR_POSSIBLE_DEADLOCK:  // A potential deadlock condition has
110      return ERR_ACCESS_DENIED;    // been detected.
111    case ERROR_BAD_DEVICE:  // The specified device name is invalid.
112      return ERR_INVALID_ARGUMENT;
113    case ERROR_BROKEN_PIPE:  // Pipe is not connected.
114      return ERR_CONNECTION_RESET;
115
116    case ERROR_SUCCESS:
117      return OK;
118    default:
119      LOG(WARNING) << "Unknown error " << os_error
120                   << " mapped to net::ERR_FAILED";
121      return ERR_FAILED;
122  }
123}
124
125}  // namespace net
126