err.cpp 12 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2015 Contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
3

4
    This file is part of libzmq, the ZeroMQ core engine in C++.
Martin Sustrik's avatar
Martin Sustrik committed
5

6 7 8
    libzmq is free software; you can redistribute it and/or modify it under
    the terms of the GNU Lesser General Public License (LGPL) as published
    by the Free Software Foundation; either version 3 of the License, or
Martin Sustrik's avatar
Martin Sustrik committed
9 10
    (at your option) any later version.

11 12 13 14 15 16 17 18 19 20 21 22 23 24
    As a special exception, the Contributors give you permission to link
    this library with independent modules to produce an executable,
    regardless of the license terms of these independent modules, and to
    copy and distribute the resulting executable under terms of your choice,
    provided that you also meet, for each linked independent module, the
    terms and conditions of the license of that module. An independent
    module is a module which is not derived from or based on this library.
    If you modify this library, you must extend this exception to your
    version of the library.

    libzmq is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
25

26
    You should have received a copy of the GNU Lesser General Public License
Martin Sustrik's avatar
Martin Sustrik committed
27 28 29 30 31 32
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "err.hpp"
#include "platform.hpp"

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
const char *zmq::errno_to_string (int errno_)
{
    switch (errno_) {
#if defined ZMQ_HAVE_WINDOWS
    case ENOTSUP:
        return "Not supported";
    case EPROTONOSUPPORT:
        return "Protocol not supported";
    case ENOBUFS:
        return "No buffer space available";
    case ENETDOWN:
        return "Network is down";
    case EADDRINUSE:
        return "Address in use";
    case EADDRNOTAVAIL:
        return "Address not available";
    case ECONNREFUSED:
        return "Connection refused";
    case EINPROGRESS:
        return "Operation in progress";
#endif
    case EFSM:
        return "Operation cannot be accomplished in current state";
    case ENOCOMPATPROTO:
        return "The protocol is not compatible with the socket type";
    case ETERM:
        return "Context was terminated";
    case EMTHREAD:
        return "No thread available";
    default:
#if defined _MSC_VER
#pragma warning (push)
#pragma warning (disable:4996)
#endif
        return strerror (errno_);
#if defined _MSC_VER
#pragma warning (pop)
#endif
    }
}

74 75 76 77 78 79 80 81 82
void zmq::zmq_abort(const char *errmsg_)
{
#if defined ZMQ_HAVE_WINDOWS

    //  Raise STATUS_FATAL_APP_EXIT.
    ULONG_PTR extra_info [1];
    extra_info [0] = (ULONG_PTR) errmsg_;
    RaiseException (0x40000015, EXCEPTION_NONCONTINUABLE, 1, extra_info);
#else
83
    (void)errmsg_;
84 85 86 87
    abort ();
#endif
}

Martin Sustrik's avatar
Martin Sustrik committed
88
#ifdef ZMQ_HAVE_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
89

Martin Sustrik's avatar
Martin Sustrik committed
90
const char *zmq::wsa_error()
Martin Sustrik's avatar
Martin Sustrik committed
91
{
92
    const int last_error = WSAGetLastError();
Martin Sustrik's avatar
Martin Sustrik committed
93
    //  TODO: This is not a generic way to handle this...
94
    if (last_error == WSAEWOULDBLOCK)
Martin Sustrik's avatar
Martin Sustrik committed
95 96
        return NULL;

97
    return wsa_error_no (last_error);
98 99 100 101
}

const char *zmq::wsa_error_no (int no_)
{
102 103 104
    //  TODO:  It seems that list of Windows socket errors is longer than this.
    //         Investigate whether there's a way to convert it into the string
    //         automatically (wsaError->HRESULT->string?).
Martin Sustrik's avatar
Martin Sustrik committed
105
    return
106
        (no_ == WSABASEERR) ?
Martin Sustrik's avatar
Martin Sustrik committed
107
            "No Error" : 
108
        (no_ == WSAEINTR) ?
Martin Sustrik's avatar
Martin Sustrik committed
109
            "Interrupted system call" : 
110
        (no_ == WSAEBADF) ?
Martin Sustrik's avatar
Martin Sustrik committed
111
            "Bad file number" : 
112
        (no_ == WSAEACCES) ?
Martin Sustrik's avatar
Martin Sustrik committed
113
            "Permission denied" : 
114
        (no_ == WSAEFAULT) ?
Martin Sustrik's avatar
Martin Sustrik committed
115
            "Bad address" : 
116
        (no_ == WSAEINVAL) ?
Martin Sustrik's avatar
Martin Sustrik committed
117
            "Invalid argument" : 
118
        (no_ == WSAEMFILE) ?
Martin Sustrik's avatar
Martin Sustrik committed
119
            "Too many open files" : 
120
        (no_ == WSAEWOULDBLOCK) ?
Martin Sustrik's avatar
Martin Sustrik committed
121
            "Operation would block" : 
122
        (no_ == WSAEINPROGRESS) ?
Martin Sustrik's avatar
Martin Sustrik committed
123
            "Operation now in progress" : 
124
        (no_ == WSAEALREADY) ?
Martin Sustrik's avatar
Martin Sustrik committed
125
            "Operation already in progress" : 
126
        (no_ == WSAENOTSOCK) ?
Martin Sustrik's avatar
Martin Sustrik committed
127
            "Socket operation on non-socket" : 
128
        (no_ == WSAEDESTADDRREQ) ?
Martin Sustrik's avatar
Martin Sustrik committed
129
            "Destination address required" : 
130
        (no_ == WSAEMSGSIZE) ?
Martin Sustrik's avatar
Martin Sustrik committed
131
            "Message too long" : 
132
        (no_ == WSAEPROTOTYPE) ?
Martin Sustrik's avatar
Martin Sustrik committed
133
            "Protocol wrong type for socket" : 
134
        (no_ == WSAENOPROTOOPT) ?
Martin Sustrik's avatar
Martin Sustrik committed
135
            "Bad protocol option" : 
136
        (no_ == WSAEPROTONOSUPPORT) ?
Martin Sustrik's avatar
Martin Sustrik committed
137
            "Protocol not supported" : 
138
        (no_ == WSAESOCKTNOSUPPORT) ?
Martin Sustrik's avatar
Martin Sustrik committed
139
            "Socket type not supported" : 
140
        (no_ == WSAEOPNOTSUPP) ?
Martin Sustrik's avatar
Martin Sustrik committed
141
            "Operation not supported on socket" : 
142
        (no_ == WSAEPFNOSUPPORT) ?
Martin Sustrik's avatar
Martin Sustrik committed
143
            "Protocol family not supported" : 
144
        (no_ == WSAEAFNOSUPPORT) ?
Martin Sustrik's avatar
Martin Sustrik committed
145
            "Address family not supported by protocol family" : 
146
        (no_ == WSAEADDRINUSE) ?
Martin Sustrik's avatar
Martin Sustrik committed
147
            "Address already in use" : 
148
        (no_ == WSAEADDRNOTAVAIL) ?
Martin Sustrik's avatar
Martin Sustrik committed
149
            "Can't assign requested address" : 
150
        (no_ == WSAENETDOWN) ?
Martin Sustrik's avatar
Martin Sustrik committed
151
            "Network is down" : 
152
        (no_ == WSAENETUNREACH) ?
Martin Sustrik's avatar
Martin Sustrik committed
153
            "Network is unreachable" : 
154
        (no_ == WSAENETRESET) ?
Martin Sustrik's avatar
Martin Sustrik committed
155
            "Net dropped connection or reset" : 
156
        (no_ == WSAECONNABORTED) ?
Martin Sustrik's avatar
Martin Sustrik committed
157
            "Software caused connection abort" : 
158
        (no_ == WSAECONNRESET) ?
Martin Sustrik's avatar
Martin Sustrik committed
159
            "Connection reset by peer" : 
160
        (no_ == WSAENOBUFS) ?
Martin Sustrik's avatar
Martin Sustrik committed
161
            "No buffer space available" : 
162
        (no_ == WSAEISCONN) ?
Martin Sustrik's avatar
Martin Sustrik committed
163
            "Socket is already connected" : 
164
        (no_ == WSAENOTCONN) ?
Martin Sustrik's avatar
Martin Sustrik committed
165
            "Socket is not connected" : 
166
        (no_ == WSAESHUTDOWN) ?
Martin Sustrik's avatar
Martin Sustrik committed
167
            "Can't send after socket shutdown" : 
168
        (no_ == WSAETOOMANYREFS) ?
Martin Sustrik's avatar
Martin Sustrik committed
169
            "Too many references can't splice" : 
170
        (no_ == WSAETIMEDOUT) ?
Martin Sustrik's avatar
Martin Sustrik committed
171
            "Connection timed out" : 
172
        (no_ == WSAECONNREFUSED) ?
Martin Sustrik's avatar
Martin Sustrik committed
173
            "Connection refused" : 
174
        (no_ == WSAELOOP) ?
Martin Sustrik's avatar
Martin Sustrik committed
175
            "Too many levels of symbolic links" : 
176
        (no_ == WSAENAMETOOLONG) ?
Martin Sustrik's avatar
Martin Sustrik committed
177
            "File name too long" : 
178
        (no_ == WSAEHOSTDOWN) ?
Martin Sustrik's avatar
Martin Sustrik committed
179
            "Host is down" : 
180
        (no_ == WSAEHOSTUNREACH) ?
Martin Sustrik's avatar
Martin Sustrik committed
181
            "No Route to Host" : 
182
        (no_ == WSAENOTEMPTY) ?
Martin Sustrik's avatar
Martin Sustrik committed
183
            "Directory not empty" : 
184
        (no_ == WSAEPROCLIM) ?
Martin Sustrik's avatar
Martin Sustrik committed
185
            "Too many processes" : 
186
        (no_ == WSAEUSERS) ?
Martin Sustrik's avatar
Martin Sustrik committed
187
            "Too many users" : 
188
        (no_ == WSAEDQUOT) ?
Martin Sustrik's avatar
Martin Sustrik committed
189
            "Disc Quota Exceeded" : 
190
        (no_ == WSAESTALE) ?
Martin Sustrik's avatar
Martin Sustrik committed
191
            "Stale NFS file handle" : 
192
        (no_ == WSAEREMOTE) ?
Martin Sustrik's avatar
Martin Sustrik committed
193
            "Too many levels of remote in path" : 
194
        (no_ == WSASYSNOTREADY) ?
Martin Sustrik's avatar
Martin Sustrik committed
195
            "Network SubSystem is unavailable" : 
196
        (no_ == WSAVERNOTSUPPORTED) ?
Martin Sustrik's avatar
Martin Sustrik committed
197
            "WINSOCK DLL Version out of range" : 
198
        (no_ == WSANOTINITIALISED) ?
Martin Sustrik's avatar
Martin Sustrik committed
199
            "Successful WSASTARTUP not yet performed" : 
200
        (no_ == WSAHOST_NOT_FOUND) ?
Martin Sustrik's avatar
Martin Sustrik committed
201
            "Host not found" : 
202
        (no_ == WSATRY_AGAIN) ?
Martin Sustrik's avatar
Martin Sustrik committed
203
            "Non-Authoritative Host not found" : 
204
        (no_ == WSANO_RECOVERY) ?
Martin Sustrik's avatar
Martin Sustrik committed
205
            "Non-Recoverable errors: FORMERR REFUSED NOTIMP" : 
206
        (no_ == WSANO_DATA) ?
Martin Sustrik's avatar
Martin Sustrik committed
207 208 209
            "Valid name no data record of requested" :
        "error not defined"; 
}
210

Martin Sustrik's avatar
Martin Sustrik committed
211
void zmq::win_error (char *buffer_, size_t buffer_size_)
Martin Sustrik's avatar
Martin Sustrik committed
212 213
{
    DWORD errcode = GetLastError ();
214
#if defined _WIN32_WCE
evoskuil's avatar
evoskuil committed
215
    DWORD rc = FormatMessageW (FORMAT_MESSAGE_FROM_SYSTEM |
boris@boressoft.ru's avatar
boris@boressoft.ru committed
216
        FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode, MAKELANGID(LANG_NEUTRAL,
evoskuil's avatar
evoskuil committed
217
        SUBLANG_DEFAULT), (LPWSTR)buffer_, buffer_size_ / sizeof(wchar_t), NULL);
boris@boressoft.ru's avatar
boris@boressoft.ru committed
218
#else
Martin Sustrik's avatar
Martin Sustrik committed
219 220
    DWORD rc = FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode, MAKELANGID(LANG_NEUTRAL,
evoskuil's avatar
evoskuil committed
221
        SUBLANG_DEFAULT), buffer_, (DWORD) buffer_size_, NULL);
boris@boressoft.ru's avatar
boris@boressoft.ru committed
222
#endif
Martin Sustrik's avatar
Martin Sustrik committed
223
    zmq_assert (rc);
Martin Sustrik's avatar
Martin Sustrik committed
224 225
}

226
int zmq::wsa_error_to_errno (int errcode)
227 228
{
    switch (errcode) {
229 230 231
//  10004 - Interrupted system call.
    case WSAEINTR:
        return EINTR;
232
//  10009 - File handle is not valid.
233
    case WSAEBADF:
234
        return EBADF;
235 236 237 238 239 240 241
//  10013 - Permission denied.
    case WSAEACCES:
        return EACCES;
//  10014 - Bad address.
    case WSAEFAULT:
        return EFAULT;
//  10022 - Invalid argument.
242
    case WSAEINVAL:
243
        return EINVAL;
244
//  10024 - Too many open files.
245
    case WSAEMFILE:
246
        return EMFILE;
247 248 249
//  10035 - Operation would block.
    case WSAEWOULDBLOCK:
        return EBUSY;
250 251 252
//  10036 - Operation now in progress.
    case WSAEINPROGRESS:
        return EAGAIN;
253 254 255 256 257
//  10037 - Operation already in progress.
    case WSAEALREADY:
        return EAGAIN;
//  10038 - Socket operation on non-socket.
    case WSAENOTSOCK:
258
        return ENOTSOCK;
259 260 261
//  10039 - Destination address required.
    case WSAEDESTADDRREQ:
        return EFAULT;
262 263 264
//  10040 - Message too long.
    case WSAEMSGSIZE:
        return EMSGSIZE;
265 266 267 268 269 270
//  10041 - Protocol wrong type for socket.
    case WSAEPROTOTYPE:
        return EFAULT;
//  10042 - Bad protocol option.
    case WSAENOPROTOOPT:
        return EINVAL;
271
//  10043 - Protocol not supported.
272
    case WSAEPROTONOSUPPORT:
273
        return EPROTONOSUPPORT;
274 275 276 277 278 279 280 281 282
//  10044 - Socket type not supported.
    case WSAESOCKTNOSUPPORT:
        return EFAULT;
//  10045 - Operation not supported on socket.
    case WSAEOPNOTSUPP:
        return EFAULT;
//  10046 - Protocol family not supported.
    case WSAEPFNOSUPPORT:
        return EPROTONOSUPPORT;
283 284 285 286
//  10047 - Address family not supported by protocol family.
    case WSAEAFNOSUPPORT:
        return EAFNOSUPPORT;
//  10048 - Address already in use.
287
    case WSAEADDRINUSE:
288
        return EADDRINUSE;
289
//  10049 - Cannot assign requested address.
290
    case WSAEADDRNOTAVAIL:
291
        return EADDRNOTAVAIL;
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
//  10050 - Network is down.
    case WSAENETDOWN:
        return ENETDOWN;
//  10051 - Network is unreachable.
    case WSAENETUNREACH:
        return ENETUNREACH;
//  10052 - Network dropped connection on reset.
    case WSAENETRESET:
        return ENETRESET;
//  10053 - Software caused connection abort.
    case WSAECONNABORTED:
        return ECONNABORTED;
//  10054 - Connection reset by peer.
    case WSAECONNRESET:
        return ECONNRESET;
//  10055 - No buffer space available.
    case WSAENOBUFS:
        return ENOBUFS;
310 311 312
//  10056 - Socket is already connected.
    case WSAEISCONN:
        return EFAULT;
313 314 315
//  10057 - Socket is not connected.
    case WSAENOTCONN:
        return ENOTCONN;
316 317 318 319 320 321
//  10058 - Can't send after socket shutdown.
    case WSAESHUTDOWN:
        return EFAULT;
//  10059 - Too many references can't splice.
    case WSAETOOMANYREFS:
        return EFAULT;
322 323 324 325 326 327
//  10060 - Connection timed out.
    case WSAETIMEDOUT:
        return ETIMEDOUT;
//  10061 - Connection refused.
    case WSAECONNREFUSED:
        return ECONNREFUSED;
328 329 330 331 332 333 334 335 336
//  10062 - Too many levels of symbolic links.
    case WSAELOOP:
        return EFAULT;
//  10063 - File name too long.
    case WSAENAMETOOLONG:
        return EFAULT;
//  10064 - Host is down.
    case WSAEHOSTDOWN:
        return EAGAIN;
337 338 339
//  10065 - No route to host.
    case WSAEHOSTUNREACH:
        return EHOSTUNREACH;
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
//  10066 - Directory not empty.
    case WSAENOTEMPTY:
        return EFAULT;
//  10067 - Too many processes.
    case WSAEPROCLIM:
        return EFAULT;
//  10068 - Too many users.
    case WSAEUSERS:
        return EFAULT;
//  10069 - Disc Quota Exceeded.
    case WSAEDQUOT:
        return EFAULT;
//  10070 - Stale NFS file handle.
    case WSAESTALE:
        return EFAULT;
//  10071 - Too many levels of remote in path.
    case WSAEREMOTE:
        return EFAULT;
//  10091 - Network SubSystem is unavailable.
    case WSASYSNOTREADY:
        return EFAULT;
//  10092 - WINSOCK DLL Version out of range.
    case WSAVERNOTSUPPORTED:
        return EFAULT;
//  10093 - Successful WSASTARTUP not yet performed.
    case WSANOTINITIALISED:
        return EFAULT;
//  11001 - Host not found.
    case WSAHOST_NOT_FOUND:
        return EFAULT;
//  11002 - Non-Authoritative Host not found.
    case WSATRY_AGAIN:
        return EFAULT;
//  11003 - Non-Recoverable errors: FORMERR REFUSED NOTIMP.
    case WSANO_RECOVERY:
        return EFAULT;
//  11004 - Valid name no data record of requested.
    case WSANO_DATA:
        return EFAULT;
379 380 381
    default:
        wsa_assert (false);
    }
382 383
    //  Not reachable
    return 0;
384 385
}

Martin Sustrik's avatar
Martin Sustrik committed
386
#endif