libzmq master
The Intelligent Transport Layer

err.cpp

Go to the documentation of this file.
00001 /*
00002     Copyright (c) 2009-2011 250bpm s.r.o.
00003     Copyright (c) 2007-2009 iMatix Corporation
00004     Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
00005 
00006     This file is part of 0MQ.
00007 
00008     0MQ is free software; you can redistribute it and/or modify it under
00009     the terms of the GNU Lesser General Public License as published by
00010     the Free Software Foundation; either version 3 of the License, or
00011     (at your option) any later version.
00012 
00013     0MQ is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU Lesser General Public License for more details.
00017 
00018     You should have received a copy of the GNU Lesser General Public License
00019     along with this program.  If not, see <http://www.gnu.org/licenses/>.
00020 */
00021 
00022 #include "err.hpp"
00023 #include "platform.hpp"
00024 
00025 const char *zmq::errno_to_string (int errno_)
00026 {
00027     switch (errno_) {
00028 #if defined ZMQ_HAVE_WINDOWS
00029     case ENOTSUP:
00030         return "Not supported";
00031     case EPROTONOSUPPORT:
00032         return "Protocol not supported";
00033     case ENOBUFS:
00034         return "No buffer space available";
00035     case ENETDOWN:
00036         return "Network is down";
00037     case EADDRINUSE:
00038         return "Address in use";
00039     case EADDRNOTAVAIL:
00040         return "Address not available";
00041     case ECONNREFUSED:
00042         return "Connection refused";
00043     case EINPROGRESS:
00044         return "Operation in progress";
00045 #endif
00046     case EFSM:
00047         return "Operation cannot be accomplished in current state";
00048     case ENOCOMPATPROTO:
00049         return "The protocol is not compatible with the socket type";
00050     case ETERM:
00051         return "Context was terminated";
00052     case EMTHREAD:
00053         return "No thread available";
00054     default:
00055 #if defined _MSC_VER
00056 #pragma warning (push)
00057 #pragma warning (disable:4996)
00058 #endif
00059         return strerror (errno_);
00060 #if defined _MSC_VER
00061 #pragma warning (pop)
00062 #endif
00063     }
00064 }
00065 
00066 void zmq::zmq_abort(const char *errmsg_)
00067 {
00068 #if defined ZMQ_HAVE_WINDOWS
00069 
00070     //  Raise STATUS_FATAL_APP_EXIT.
00071     ULONG_PTR extra_info [1];
00072     extra_info [0] = (ULONG_PTR) errmsg_;
00073     RaiseException (0x40000015, EXCEPTION_NONCONTINUABLE, 1, extra_info);
00074 #else
00075     abort ();
00076 #endif
00077 }
00078 
00079 #ifdef ZMQ_HAVE_WINDOWS
00080 
00081 const char *zmq::wsa_error()
00082 {
00083     int no = WSAGetLastError ();
00084     //  TODO: This is not a generic way to handle this...
00085     if (no == WSAEWOULDBLOCK)
00086         return NULL;
00087 
00088     return wsa_error_no (no);
00089 }
00090 
00091 const char *zmq::wsa_error_no (int no_)
00092 {
00093     //  TODO:  It seems that list of Windows socket errors is longer than this.
00094     //         Investigate whether there's a way to convert it into the string
00095     //         automatically (wsaError->HRESULT->string?).
00096     return
00097         (no_ == WSABASEERR) ?
00098             "No Error" : 
00099         (no_ == WSAEINTR) ?
00100             "Interrupted system call" : 
00101         (no_ == WSAEBADF) ?
00102             "Bad file number" : 
00103         (no_ == WSAEACCES) ?
00104             "Permission denied" : 
00105         (no_ == WSAEFAULT) ?
00106             "Bad address" : 
00107         (no_ == WSAEINVAL) ?
00108             "Invalid argument" : 
00109         (no_ == WSAEMFILE) ?
00110             "Too many open files" : 
00111         (no_ == WSAEWOULDBLOCK) ?
00112             "Operation would block" : 
00113         (no_ == WSAEINPROGRESS) ?
00114             "Operation now in progress" : 
00115         (no_ == WSAEALREADY) ?
00116             "Operation already in progress" : 
00117         (no_ == WSAENOTSOCK) ?
00118             "Socket operation on non-socket" : 
00119         (no_ == WSAEDESTADDRREQ) ?
00120             "Destination address required" : 
00121         (no_ == WSAEMSGSIZE) ?
00122             "Message too long" : 
00123         (no_ == WSAEPROTOTYPE) ?
00124             "Protocol wrong type for socket" : 
00125         (no_ == WSAENOPROTOOPT) ?
00126             "Bad protocol option" : 
00127         (no_ == WSAEPROTONOSUPPORT) ?
00128             "Protocol not supported" : 
00129         (no_ == WSAESOCKTNOSUPPORT) ?
00130             "Socket type not supported" : 
00131         (no_ == WSAEOPNOTSUPP) ?
00132             "Operation not supported on socket" : 
00133         (no_ == WSAEPFNOSUPPORT) ?
00134             "Protocol family not supported" : 
00135         (no_ == WSAEAFNOSUPPORT) ?
00136             "Address family not supported by protocol family" : 
00137         (no_ == WSAEADDRINUSE) ?
00138             "Address already in use" : 
00139         (no_ == WSAEADDRNOTAVAIL) ?
00140             "Can't assign requested address" : 
00141         (no_ == WSAENETDOWN) ?
00142             "Network is down" : 
00143         (no_ == WSAENETUNREACH) ?
00144             "Network is unreachable" : 
00145         (no_ == WSAENETRESET) ?
00146             "Net dropped connection or reset" : 
00147         (no_ == WSAECONNABORTED) ?
00148             "Software caused connection abort" : 
00149         (no_ == WSAECONNRESET) ?
00150             "Connection reset by peer" : 
00151         (no_ == WSAENOBUFS) ?
00152             "No buffer space available" : 
00153         (no_ == WSAEISCONN) ?
00154             "Socket is already connected" : 
00155         (no_ == WSAENOTCONN) ?
00156             "Socket is not connected" : 
00157         (no_ == WSAESHUTDOWN) ?
00158             "Can't send after socket shutdown" : 
00159         (no_ == WSAETOOMANYREFS) ?
00160             "Too many references can't splice" : 
00161         (no_ == WSAETIMEDOUT) ?
00162             "Connection timed out" : 
00163         (no_ == WSAECONNREFUSED) ?
00164             "Connection refused" : 
00165         (no_ == WSAELOOP) ?
00166             "Too many levels of symbolic links" : 
00167         (no_ == WSAENAMETOOLONG) ?
00168             "File name too long" : 
00169         (no_ == WSAEHOSTDOWN) ?
00170             "Host is down" : 
00171         (no_ == WSAEHOSTUNREACH) ?
00172             "No Route to Host" : 
00173         (no_ == WSAENOTEMPTY) ?
00174             "Directory not empty" : 
00175         (no_ == WSAEPROCLIM) ?
00176             "Too many processes" : 
00177         (no_ == WSAEUSERS) ?
00178             "Too many users" : 
00179         (no_ == WSAEDQUOT) ?
00180             "Disc Quota Exceeded" : 
00181         (no_ == WSAESTALE) ?
00182             "Stale NFS file handle" : 
00183         (no_ == WSAEREMOTE) ?
00184             "Too many levels of remote in path" : 
00185         (no_ == WSASYSNOTREADY) ?
00186             "Network SubSystem is unavailable" : 
00187         (no_ == WSAVERNOTSUPPORTED) ?
00188             "WINSOCK DLL Version out of range" : 
00189         (no_ == WSANOTINITIALISED) ?
00190             "Successful WSASTARTUP not yet performed" : 
00191         (no_ == WSAHOST_NOT_FOUND) ?
00192             "Host not found" : 
00193         (no_ == WSATRY_AGAIN) ?
00194             "Non-Authoritative Host not found" : 
00195         (no_ == WSANO_RECOVERY) ?
00196             "Non-Recoverable errors: FORMERR REFUSED NOTIMP" : 
00197         (no_ == WSANO_DATA) ?
00198             "Valid name no data record of requested" :
00199         "error not defined"; 
00200 }
00201 
00202 void zmq::win_error (char *buffer_, size_t buffer_size_)
00203 {
00204     DWORD errcode = GetLastError ();
00205     DWORD rc = FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM |
00206         FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode, MAKELANGID(LANG_NEUTRAL,
00207         SUBLANG_DEFAULT), buffer_, (DWORD) buffer_size_, NULL );
00208     zmq_assert (rc);
00209 }
00210 
00211 void zmq::wsa_error_to_errno ()
00212 {
00213     int errcode = WSAGetLastError ();
00214     switch (errcode) {
00215     case WSAEINPROGRESS:
00216         errno = EAGAIN;
00217         return;
00218     case WSAEBADF:
00219         errno = EBADF;
00220         return;
00221     case WSAEINVAL:
00222         errno = EINVAL;
00223         return;
00224     case WSAEMFILE:
00225         errno = EMFILE;
00226         return;
00227     case WSAEFAULT:
00228         errno = EFAULT;
00229         return;
00230     case WSAEPROTONOSUPPORT:
00231         errno = EPROTONOSUPPORT;
00232         return;
00233     case WSAENOBUFS:
00234         errno = ENOBUFS;
00235         return;
00236     case WSAENETDOWN:
00237         errno = ENETDOWN;
00238         return;
00239     case WSAEADDRINUSE:
00240         errno = EADDRINUSE;
00241         return;
00242     case WSAEADDRNOTAVAIL:
00243         errno = EADDRNOTAVAIL;
00244         return;
00245     case WSAEAFNOSUPPORT:
00246         errno = EAFNOSUPPORT;
00247         return;
00248     default:
00249         wsa_assert (false);
00250     }
00251 }
00252 
00253 #endif
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines