libzmq/src/err.cpp

290 lines
8.9 KiB
C++
Raw Normal View History

2009-07-29 12:07:54 +02:00
/*
2014-01-02 12:00:57 +01:00
Copyright (c) 2007-2014 Contributors as noted in the AUTHORS file
2009-07-29 12:07:54 +02:00
This file is part of 0MQ.
0MQ is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
2009-07-29 12:07:54 +02:00
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
0MQ 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.
2009-07-29 12:07:54 +02:00
You should have received a copy of the GNU Lesser General Public License
2009-07-29 12:07:54 +02:00
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "err.hpp"
#include "platform.hpp"
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
}
}
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
(void)errmsg_;
abort ();
#endif
}
2009-08-03 11:30:13 +02:00
#ifdef ZMQ_HAVE_WINDOWS
2009-07-29 12:07:54 +02:00
2009-08-03 11:30:13 +02:00
const char *zmq::wsa_error()
2009-07-29 12:07:54 +02:00
{
int no = WSAGetLastError ();
2009-07-29 12:07:54 +02:00
// TODO: This is not a generic way to handle this...
if (no == WSAEWOULDBLOCK)
2009-07-29 12:07:54 +02:00
return NULL;
return wsa_error_no (no);
}
const char *zmq::wsa_error_no (int no_)
{
2009-09-22 11:52:35 +02:00
// 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?).
2009-07-29 12:07:54 +02:00
return
(no_ == WSABASEERR) ?
2009-07-29 12:07:54 +02:00
"No Error" :
(no_ == WSAEINTR) ?
2009-07-29 12:07:54 +02:00
"Interrupted system call" :
(no_ == WSAEBADF) ?
2009-07-29 12:07:54 +02:00
"Bad file number" :
(no_ == WSAEACCES) ?
2009-07-29 12:07:54 +02:00
"Permission denied" :
(no_ == WSAEFAULT) ?
2009-07-29 12:07:54 +02:00
"Bad address" :
(no_ == WSAEINVAL) ?
2009-07-29 12:07:54 +02:00
"Invalid argument" :
(no_ == WSAEMFILE) ?
2009-07-29 12:07:54 +02:00
"Too many open files" :
(no_ == WSAEWOULDBLOCK) ?
2009-07-29 12:07:54 +02:00
"Operation would block" :
(no_ == WSAEINPROGRESS) ?
2009-07-29 12:07:54 +02:00
"Operation now in progress" :
(no_ == WSAEALREADY) ?
2009-07-29 12:07:54 +02:00
"Operation already in progress" :
(no_ == WSAENOTSOCK) ?
2009-07-29 12:07:54 +02:00
"Socket operation on non-socket" :
(no_ == WSAEDESTADDRREQ) ?
2009-07-29 12:07:54 +02:00
"Destination address required" :
(no_ == WSAEMSGSIZE) ?
2009-07-29 12:07:54 +02:00
"Message too long" :
(no_ == WSAEPROTOTYPE) ?
2009-07-29 12:07:54 +02:00
"Protocol wrong type for socket" :
(no_ == WSAENOPROTOOPT) ?
2009-07-29 12:07:54 +02:00
"Bad protocol option" :
(no_ == WSAEPROTONOSUPPORT) ?
2009-07-29 12:07:54 +02:00
"Protocol not supported" :
(no_ == WSAESOCKTNOSUPPORT) ?
2009-07-29 12:07:54 +02:00
"Socket type not supported" :
(no_ == WSAEOPNOTSUPP) ?
2009-07-29 12:07:54 +02:00
"Operation not supported on socket" :
(no_ == WSAEPFNOSUPPORT) ?
2009-07-29 12:07:54 +02:00
"Protocol family not supported" :
(no_ == WSAEAFNOSUPPORT) ?
2009-07-29 12:07:54 +02:00
"Address family not supported by protocol family" :
(no_ == WSAEADDRINUSE) ?
2009-07-29 12:07:54 +02:00
"Address already in use" :
(no_ == WSAEADDRNOTAVAIL) ?
2009-07-29 12:07:54 +02:00
"Can't assign requested address" :
(no_ == WSAENETDOWN) ?
2009-07-29 12:07:54 +02:00
"Network is down" :
(no_ == WSAENETUNREACH) ?
2009-07-29 12:07:54 +02:00
"Network is unreachable" :
(no_ == WSAENETRESET) ?
2009-07-29 12:07:54 +02:00
"Net dropped connection or reset" :
(no_ == WSAECONNABORTED) ?
2009-07-29 12:07:54 +02:00
"Software caused connection abort" :
(no_ == WSAECONNRESET) ?
2009-07-29 12:07:54 +02:00
"Connection reset by peer" :
(no_ == WSAENOBUFS) ?
2009-07-29 12:07:54 +02:00
"No buffer space available" :
(no_ == WSAEISCONN) ?
2009-07-29 12:07:54 +02:00
"Socket is already connected" :
(no_ == WSAENOTCONN) ?
2009-07-29 12:07:54 +02:00
"Socket is not connected" :
(no_ == WSAESHUTDOWN) ?
2009-07-29 12:07:54 +02:00
"Can't send after socket shutdown" :
(no_ == WSAETOOMANYREFS) ?
2009-07-29 12:07:54 +02:00
"Too many references can't splice" :
(no_ == WSAETIMEDOUT) ?
2009-07-29 12:07:54 +02:00
"Connection timed out" :
(no_ == WSAECONNREFUSED) ?
2009-07-29 12:07:54 +02:00
"Connection refused" :
(no_ == WSAELOOP) ?
2009-07-29 12:07:54 +02:00
"Too many levels of symbolic links" :
(no_ == WSAENAMETOOLONG) ?
2009-07-29 12:07:54 +02:00
"File name too long" :
(no_ == WSAEHOSTDOWN) ?
2009-07-29 12:07:54 +02:00
"Host is down" :
(no_ == WSAEHOSTUNREACH) ?
2009-07-29 12:07:54 +02:00
"No Route to Host" :
(no_ == WSAENOTEMPTY) ?
2009-07-29 12:07:54 +02:00
"Directory not empty" :
(no_ == WSAEPROCLIM) ?
2009-07-29 12:07:54 +02:00
"Too many processes" :
(no_ == WSAEUSERS) ?
2009-07-29 12:07:54 +02:00
"Too many users" :
(no_ == WSAEDQUOT) ?
2009-07-29 12:07:54 +02:00
"Disc Quota Exceeded" :
(no_ == WSAESTALE) ?
2009-07-29 12:07:54 +02:00
"Stale NFS file handle" :
(no_ == WSAEREMOTE) ?
2009-07-29 12:07:54 +02:00
"Too many levels of remote in path" :
(no_ == WSASYSNOTREADY) ?
2009-07-29 12:07:54 +02:00
"Network SubSystem is unavailable" :
(no_ == WSAVERNOTSUPPORTED) ?
2009-07-29 12:07:54 +02:00
"WINSOCK DLL Version out of range" :
(no_ == WSANOTINITIALISED) ?
2009-07-29 12:07:54 +02:00
"Successful WSASTARTUP not yet performed" :
(no_ == WSAHOST_NOT_FOUND) ?
2009-07-29 12:07:54 +02:00
"Host not found" :
(no_ == WSATRY_AGAIN) ?
2009-07-29 12:07:54 +02:00
"Non-Authoritative Host not found" :
(no_ == WSANO_RECOVERY) ?
2009-07-29 12:07:54 +02:00
"Non-Recoverable errors: FORMERR REFUSED NOTIMP" :
(no_ == WSANO_DATA) ?
2009-07-29 12:07:54 +02:00
"Valid name no data record of requested" :
"error not defined";
}
2009-08-03 11:30:13 +02:00
void zmq::win_error (char *buffer_, size_t buffer_size_)
2009-07-29 12:07:54 +02:00
{
DWORD errcode = GetLastError ();
#if defined _WIN32_WCE
DWORD rc = FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode, MAKELANGID(LANG_NEUTRAL,
SUBLANG_DEFAULT), (LPWSTR)buffer_, buffer_size_ / sizeof(wchar_t), NULL );
#else
2009-07-29 12:07:54 +02:00
DWORD rc = FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode, MAKELANGID(LANG_NEUTRAL,
SUBLANG_DEFAULT), buffer_, (DWORD) buffer_size_, NULL );
#endif
2009-08-03 11:30:13 +02:00
zmq_assert (rc);
2009-07-29 12:07:54 +02:00
}
2012-05-27 16:10:19 +02:00
int zmq::wsa_error_to_errno (int errcode)
2009-10-01 13:48:04 +02:00
{
switch (errcode) {
// 10009 - File handle is not valid.
2009-10-01 13:48:04 +02:00
case WSAEBADF:
2012-05-27 16:10:19 +02:00
return EBADF;
// 10013 - Permission denied.
case WSAEACCES:
return EACCES;
// 10014 - Bad address.
case WSAEFAULT:
return EFAULT;
// 10022 - Invalid argument.
2009-10-01 13:48:04 +02:00
case WSAEINVAL:
2012-05-27 16:10:19 +02:00
return EINVAL;
// 10024 - Too many open files.
2009-10-01 13:48:04 +02:00
case WSAEMFILE:
2012-05-27 16:10:19 +02:00
return EMFILE;
// 10036 - Operation now in progress.
case WSAEINPROGRESS:
return EAGAIN;
// 10040 - Message too long.
case WSAEMSGSIZE:
return EMSGSIZE;
// 10043 - Protocol not supported.
2009-10-01 13:48:04 +02:00
case WSAEPROTONOSUPPORT:
2012-05-27 16:10:19 +02:00
return EPROTONOSUPPORT;
// 10047 - Address family not supported by protocol family.
case WSAEAFNOSUPPORT:
return EAFNOSUPPORT;
// 10048 - Address already in use.
2009-10-01 13:48:04 +02:00
case WSAEADDRINUSE:
2012-05-27 16:10:19 +02:00
return EADDRINUSE;
// 10049 - Cannot assign requested address.
2009-10-01 13:48:04 +02:00
case WSAEADDRNOTAVAIL:
2012-05-27 16:10:19 +02:00
return EADDRNOTAVAIL;
// 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;
// 10057 - Socket is not connected.
case WSAENOTCONN:
return ENOTCONN;
// 10060 - Connection timed out.
case WSAETIMEDOUT:
return ETIMEDOUT;
// 10061 - Connection refused.
case WSAECONNREFUSED:
return ECONNREFUSED;
// 10065 - No route to host.
case WSAEHOSTUNREACH:
return EHOSTUNREACH;
2009-10-01 13:48:04 +02:00
default:
wsa_assert (false);
}
2012-05-27 16:10:19 +02:00
// Not reachable
return 0;
2009-10-01 13:48:04 +02:00
}
2009-07-29 12:07:54 +02:00
#endif