1 /*---------------------------------------------------------------
2 * Copyright (c) 1999,2000,2001,2002,2003
3 * The Board of Trustees of the University of Illinois
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:
16 * Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and
18 * the following disclaimers.
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.
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.
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 * ________________________________________________________________
48 * by Mark Gates <mgates@nlanr.net>
49 * -------------------------------------------------------------------
51 * ------------------------------------------------------------------- */
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 * ------------------------------------------------------------------- */
73 const struct mesg error_mesgs[] =
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."},
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." },
133 }; /* end error_mesgs[] */
135 const char* winsock_strerror( DWORD inErrno );
137 /* -------------------------------------------------------------------
140 * returns a string representing the error code. The error messages
141 * were taken from Microsoft's online developer library.
142 * ------------------------------------------------------------------- */
144 const char* winsock_strerror( DWORD inErrno ) {
145 const char* str = "Unknown error";
147 for ( i = 0; i < sizeof(error_mesgs); i++ ) {
148 if ( error_mesgs[i].err == inErrno ) {
149 str = error_mesgs[i].str;
155 } /* end winsock_strerror */
159 /* -------------------------------------------------------------------
162 * Prints message and return
163 * ------------------------------------------------------------------- */
165 void warn( const char *inMessage, const char *inFile, int inLine ) {
169 fprintf( stderr, "%s failed\n", inMessage );
172 /* while debugging output file/line number also */
173 fprintf( stderr, "%s failed (%s:%d)\n", inMessage, inFile, inLine );
177 /* -------------------------------------------------------------------
180 * Prints message and errno message, and return.
181 * ------------------------------------------------------------------- */
183 void warn_errno( const char *inMessage, const char *inFile, int inLine ) {
187 /* get platform's errno and error message */
189 my_err = WSAGetLastError();
190 my_str = winsock_strerror( my_err );
193 my_str = strerror( my_err );
199 fprintf( stderr, "%s failed: %s\n", inMessage, my_str );
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 );
206 } /* end warn_errno */
209 } /* end extern "C" */