]> sjero.net Git - iperf/blob - compat/error.c
829ecc98bc247d4ef07b963f25dc0583050b77f9
[iperf] / compat / error.c
1 /*--------------------------------------------------------------- 
2  * Copyright (c) 1999,2000,2001,2002,2003                              
3  * The Board of Trustees of the University of Illinois            
4  * All Rights Reserved.                                           
5  *--------------------------------------------------------------- 
6  * Permission is hereby granted, free of charge, to any person    
7  * obtaining a copy of this software (Iperf) and associated       
8  * documentation files (the "Software"), to deal in the Software  
9  * without restriction, including without limitation the          
10  * rights to use, copy, modify, merge, publish, distribute,        
11  * sublicense, and/or sell copies of the Software, and to permit     
12  * persons to whom the Software is furnished to do
13  * so, subject to the following conditions: 
14  *
15  *     
16  * Redistributions of source code must retain the above 
17  * copyright notice, this list of conditions and 
18  * the following disclaimers. 
19  *
20  *     
21  * Redistributions in binary form must reproduce the above 
22  * copyright notice, this list of conditions and the following 
23  * disclaimers in the documentation and/or other materials 
24  * provided with the distribution. 
25  * 
26  *     
27  * Neither the names of the University of Illinois, NCSA, 
28  * nor the names of its contributors may be used to endorse 
29  * or promote products derived from this Software without
30  * specific prior written permission. 
31  * 
32  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 
33  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 
34  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
35  * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTIBUTORS OR COPYRIGHT 
36  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
37  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
38  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE
39  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
40  * ________________________________________________________________
41  * National Laboratory for Applied Network Research 
42  * National Center for Supercomputing Applications 
43  * University of Illinois at Urbana-Champaign 
44  * http://www.ncsa.uiuc.edu
45  * ________________________________________________________________ 
46  *
47  * error.c
48  * by Mark Gates <mgates@nlanr.net>
49  * -------------------------------------------------------------------
50  * error handlers
51  * ------------------------------------------------------------------- */
52
53 #include "headers.h"
54 #include "util.h"
55
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59
60 #ifdef WIN32
61
62 /* -------------------------------------------------------------------
63  * Implement a simple Win32 strerror function for our purposes.
64  * These error values weren't handled by FormatMessage;
65  * any particular reason why not??
66  * ------------------------------------------------------------------- */
67
68 struct mesg {
69     DWORD       err;
70     const char* str;
71 };
72
73 const struct mesg error_mesgs[] =
74 {
75     { WSAEACCES,             "Permission denied"},
76     { WSAEADDRINUSE,         "Address already in use"},
77     { WSAEADDRNOTAVAIL,      "Cannot assign requested address"},
78     { WSAEAFNOSUPPORT,       "Address family not supported by protocol family"},
79     { WSAEALREADY,           "Operation already in progress"},
80     { WSAECONNABORTED,       "Software caused connection abort"},
81     { WSAECONNREFUSED,       "Connection refused"},
82     { WSAECONNRESET,         "Connection reset by peer"},
83     { WSAEDESTADDRREQ,       "Destination address required"},
84     { WSAEFAULT,             "Bad address"},
85     { WSAEHOSTDOWN,          "Host is down"},
86     { WSAEHOSTUNREACH,       "No route to host"},
87     { WSAEINPROGRESS,        "Operation now in progress"},
88     { WSAEINTR,              "Interrupted function call."},
89     { WSAEINVAL,             "Invalid argument."},
90     { WSAEISCONN,            "Socket is already connected."},
91     { WSAEMFILE,             "Too many open files."},
92     { WSAEMSGSIZE,           "Message too long"},
93     { WSAENETDOWN,           "Network is down"},
94     { WSAENETRESET,          "Network dropped connection on reset"},
95     { WSAENETUNREACH,        "Network is unreachable"},
96     { WSAENOBUFS,            "No buffer space available."},
97     { WSAENOPROTOOPT,        "Bad protocol option."},
98     { WSAENOTCONN,           "Socket is not connected"},
99     { WSAENOTSOCK,           "Socket operation on non-socket."},
100     { WSAEOPNOTSUPP,         "Operation not supported"},
101     { WSAEPFNOSUPPORT,       "Protocol family not supported"},
102     { WSAEPROCLIM,           "Too many processes."},
103     { WSAEPROTONOSUPPORT,    "Protocol not supported"},
104     { WSAEPROTOTYPE,         "Protocol wrong type for socket"},
105     { WSAESHUTDOWN,          "Cannot send after socket shutdown"},
106     { WSAESOCKTNOSUPPORT,    "Socket type not supported."},
107     { WSAETIMEDOUT,          "Connection timed out."},
108     { WSATYPE_NOT_FOUND,     "Class type not found."},
109     { WSAEWOULDBLOCK,        "Resource temporarily unavailable"},
110     { WSAHOST_NOT_FOUND,     "Host not found."},
111     { WSA_INVALID_HANDLE,    "Specified event object handle is invalid."},
112     { WSA_INVALID_PARAMETER, "One or more parameters are invalid."},
113     { WSA_IO_INCOMPLETE,     "Overlapped I/O event object not in signaled state."},
114     { WSA_IO_PENDING,        "Overlapped operations will complete later."},
115     { WSA_NOT_ENOUGH_MEMORY, "Insufficient memory available."},
116     { WSANOTINITIALISED,     "Successful WSAStartup not yet performed."},
117     { WSANO_DATA,            "Valid name, no data record of requested type."},
118     { WSANO_RECOVERY,        "This is a non-recoverable error."},
119     { WSASYSCALLFAILURE,     "System call failure."},
120     { WSASYSNOTREADY,        "Network subsystem is unavailable."},
121     { WSATRY_AGAIN,          "Non-authoritative host not found."},
122     { WSAVERNOTSUPPORTED,    "WINSOCK.DLL version out of range."},
123     { WSAEDISCON,            "Graceful shutdown in progress."},
124     { WSA_OPERATION_ABORTED, "Overlapped operation aborted."},
125     { 0,                     "No error."}
126
127     /* These appeared in the documentation, but didn't compile.
128      * { WSAINVALIDPROCTABLE,   "Invalid procedure table from service provider." },
129      * { WSAINVALIDPROVIDER,    "Invalid service provider version number." },
130      * { WSAPROVIDERFAILEDINIT, "Unable to initialize a service provider." },
131      */
132
133 }; /* end error_mesgs[] */
134
135 const char* winsock_strerror( DWORD inErrno );
136
137 /* -------------------------------------------------------------------
138  * winsock_strerror
139  *
140  * returns a string representing the error code. The error messages
141  * were taken from Microsoft's online developer library.
142  * ------------------------------------------------------------------- */
143
144 const char* winsock_strerror( DWORD inErrno ) {
145     const char* str = "Unknown error";
146     int i;
147     for ( i = 0; i < sizeof(error_mesgs); i++ ) {
148         if ( error_mesgs[i].err == inErrno ) {
149             str = error_mesgs[i].str;
150             break;
151         }
152     }
153
154     return str;
155 } /* end winsock_strerror */
156
157 #endif /* WIN32 */
158
159 /* -------------------------------------------------------------------
160  * warn
161  *
162  * Prints message and return
163  * ------------------------------------------------------------------- */
164
165 void warn( const char *inMessage, const char *inFile, int inLine ) {
166     fflush( 0 );
167
168 #ifdef NDEBUG
169     fprintf( stderr, "%s failed\n", inMessage );
170 #else
171
172     /* while debugging output file/line number also */
173     fprintf( stderr, "%s failed (%s:%d)\n", inMessage, inFile, inLine );
174 #endif
175 } /* end warn */
176
177 /* -------------------------------------------------------------------
178  * warn_errno
179  *
180  * Prints message and errno message, and return.
181  * ------------------------------------------------------------------- */
182
183 void warn_errno( const char *inMessage, const char *inFile, int inLine ) {
184     int my_err;
185     const char* my_str;
186
187     /* get platform's errno and error message */
188 #ifdef WIN32
189     my_err = WSAGetLastError();
190     my_str = winsock_strerror( my_err );
191 #else
192     my_err = errno;
193     my_str = strerror( my_err );
194 #endif
195
196     fflush( 0 );
197
198 #ifdef NDEBUG
199     fprintf( stderr, "%s failed: %s\n", inMessage, my_str );
200 #else
201
202     /* while debugging output file/line number and errno value also */
203     fprintf( stderr, "%s failed (%s:%d): %s (%d)\n",
204              inMessage, inFile, inLine, my_str, my_err );
205 #endif
206 } /* end warn_errno */
207
208 #ifdef __cplusplus
209 } /* end extern "C" */
210 #endif