err.cpp 7.62 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
Martin Sustrik's avatar
Martin Sustrik committed
2
    Copyright (c) 2009-2011 250bpm s.r.o.
3 4
    Copyright (c) 2007-2011 iMatix Corporation
    Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
5 6 7 8

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
9
    the terms of the GNU Lesser General Public License as published by
Martin Sustrik's avatar
Martin Sustrik committed
10 11 12 13 14 15
    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
16
    GNU Lesser General Public License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
17

18
    You should have received a copy of the GNU Lesser General Public License
Martin Sustrik's avatar
Martin Sustrik committed
19 20 21 22 23 24
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

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

25 26 27 28 29 30 31 32 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
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
    }
}

66 67 68 69 70 71 72 73 74 75 76 77 78
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
    abort ();
#endif
}

Martin Sustrik's avatar
Martin Sustrik committed
79
#ifdef ZMQ_HAVE_WINDOWS
Martin Sustrik's avatar
Martin Sustrik committed
80

Martin Sustrik's avatar
Martin Sustrik committed
81
const char *zmq::wsa_error()
Martin Sustrik's avatar
Martin Sustrik committed
82
{
83
    int no = WSAGetLastError ();
Martin Sustrik's avatar
Martin Sustrik committed
84
    //  TODO: This is not a generic way to handle this...
85
    if (no == WSAEWOULDBLOCK)
Martin Sustrik's avatar
Martin Sustrik committed
86 87
        return NULL;

88 89 90 91 92
    return wsa_error_no (no);
}

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

Martin Sustrik's avatar
Martin Sustrik committed
202
void zmq::win_error (char *buffer_, size_t buffer_size_)
Martin Sustrik's avatar
Martin Sustrik committed
203 204 205 206
{
    DWORD errcode = GetLastError ();
    DWORD rc = FormatMessageA (FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS, NULL, errcode, MAKELANGID(LANG_NEUTRAL,
207
        SUBLANG_DEFAULT), buffer_, (DWORD) buffer_size_, NULL );
Martin Sustrik's avatar
Martin Sustrik committed
208
    zmq_assert (rc);
Martin Sustrik's avatar
Martin Sustrik committed
209 210
}

211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
void zmq::wsa_error_to_errno ()
{
    int errcode = WSAGetLastError ();
    switch (errcode) {
    case WSAEINPROGRESS:
        errno = EAGAIN;
        return;
    case WSAEBADF:
        errno = EBADF;
        return;
    case WSAEINVAL:
        errno = EINVAL;
        return;
    case WSAEMFILE:
        errno = EMFILE;
        return;
    case WSAEFAULT:
        errno = EFAULT;
        return;
    case WSAEPROTONOSUPPORT:
        errno = EPROTONOSUPPORT;
        return;
    case WSAENOBUFS:
        errno = ENOBUFS;
        return;
    case WSAENETDOWN:
        errno = ENETDOWN;
        return;
    case WSAEADDRINUSE:
        errno = EADDRINUSE;
        return;
    case WSAEADDRNOTAVAIL:
        errno = EADDRNOTAVAIL;
        return;
245 246 247
    case WSAEAFNOSUPPORT:
        errno = EAFNOSUPPORT;
        return;
248 249 250 251 252
    default:
        wsa_assert (false);
    }
}

Martin Sustrik's avatar
Martin Sustrik committed
253
#endif