~ubuntu-branches/debian/squeeze/ntp/squeeze-201010051545

« back to all changes in this revision

Viewing changes to ports/winnt/libntp/strerror.c

  • Committer: Bazaar Package Importer
  • Author(s): Matt Zimmerman
  • Date: 2004-10-11 16:10:27 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20041011161027-icyjbji8ujym633o
Tags: 1:4.2.0a-10ubuntu2
Use ntp.ubuntulinux.org instead of pool.ntp.org

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2002  Internet Software Consortium.
 
3
 *
 
4
 * Permission to use, copy, modify, and distribute this software for any
 
5
 * purpose with or without fee is hereby granted, provided that the above
 
6
 * copyright notice and this permission notice appear in all copies.
 
7
 *
 
8
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
 
9
 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
 
10
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
 
11
 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
 
12
 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
 
13
 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
 
14
 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
 
15
 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
16
 */
 
17
 
 
18
/* From BIND 9 lib/isc/win32/: strerror.c,v 1.5 2002/08/01 03:52:14 mayer */
 
19
 
 
20
#include <stdio.h>
 
21
#include <string.h>
 
22
#include <winsock2.h>
 
23
 
 
24
/*
 
25
 * Forward declarations
 
26
 */
 
27
 
 
28
char *
 
29
FormatError(int error);
 
30
 
 
31
char *
 
32
GetWSAErrorMessage(int errval);
 
33
 
 
34
char *
 
35
NTstrerror(int err, BOOL *bfreebuf);
 
36
 
 
37
char *
 
38
strerror(int errnum) {
 
39
        BOOL bfreebuf;
 
40
        return (NTstrerror(errnum, &bfreebuf));
 
41
}
 
42
/*
 
43
 * This routine needs to free up any buffer allocated by FormatMessage
 
44
 * if that routine gets used.
 
45
 */
 
46
 
 
47
void
 
48
isc__strerror(int num, char *buf, size_t size) {
 
49
        char *msg;
 
50
        BOOL freebuf;
 
51
        unsigned int unum = num;
 
52
 
 
53
        freebuf = FALSE;
 
54
        msg = NTstrerror(num, &freebuf);
 
55
        if (msg != NULL)
 
56
                _snprintf(buf, size, "%s", msg);
 
57
        else
 
58
                _snprintf(buf, size, "Unknown error: %u", unum);
 
59
        if(freebuf && msg != NULL) {
 
60
                LocalFree(msg);
 
61
        }
 
62
}
 
63
 
 
64
/*
 
65
 * Note this will cause a memory leak unless the memory allocated here
 
66
 * is freed by calling LocalFree.  isc__strerror does this before unlocking.
 
67
 * This only gets called if there is a system type of error and will likely
 
68
 * be an unusual event.
 
69
 */
 
70
char *
 
71
FormatError(int error) {
 
72
        LPVOID lpMsgBuf = NULL;
 
73
        FormatMessage( 
 
74
                FORMAT_MESSAGE_ALLOCATE_BUFFER | 
 
75
                FORMAT_MESSAGE_FROM_SYSTEM | 
 
76
                FORMAT_MESSAGE_IGNORE_INSERTS,
 
77
                NULL,
 
78
                error,
 
79
                /* Default language */
 
80
                MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
 
81
                (LPTSTR) &lpMsgBuf,
 
82
                0,
 
83
                NULL); 
 
84
 
 
85
        return (lpMsgBuf);
 
86
}
 
87
 
 
88
/*
 
89
 * This routine checks the error value and calls the WSA Windows Sockets
 
90
 * Error message function GetWSAErrorMessage below if it's within that range
 
91
 * since those messages are not available in the system error messages.
 
92
 */
 
93
char *
 
94
NTstrerror(int err, BOOL *bfreebuf) {
 
95
        char *retmsg = NULL;
 
96
 
 
97
        /* Copy the error value first in case of other errors */        
 
98
        DWORD errval = err; 
 
99
 
 
100
        *bfreebuf = FALSE;
 
101
 
 
102
        /* Get the Winsock2 error messages */
 
103
        if (errval >= WSABASEERR && errval <= (WSABASEERR + 1015)) {
 
104
                retmsg = GetWSAErrorMessage(errval);
 
105
                if (retmsg != NULL)
 
106
                        return (retmsg);
 
107
        }
 
108
        /*
 
109
         * If it's not one of the standard Unix error codes,
 
110
         * try a system error message
 
111
         */
 
112
        if (errval > (DWORD) _sys_nerr) {
 
113
                *bfreebuf = TRUE;
 
114
                return (FormatError(errval));
 
115
        } else {
 
116
                return (_sys_errlist[errval]);
 
117
        }
 
118
}
 
119
 
 
120
/*
 
121
 * This is a replacement for perror
 
122
 */
 
123
void __cdecl
 
124
NTperror(char *errmsg) {
 
125
        /* Copy the error value first in case of other errors */
 
126
        int errval = errno; 
 
127
        BOOL bfreebuf = FALSE;
 
128
        char *msg;
 
129
 
 
130
        msg = NTstrerror(errval, &bfreebuf);
 
131
        fprintf(stderr, "%s: %s\n", errmsg, msg);
 
132
        if(bfreebuf == TRUE) {
 
133
                LocalFree(msg);
 
134
        }
 
135
 
 
136
}
 
137
 
 
138
/*
 
139
 * Return the error string related to Winsock2 errors.
 
140
 * This function is necessary since FormatMessage knows nothing about them
 
141
 * and there is no function to get them.
 
142
 */
 
143
char *
 
144
GetWSAErrorMessage(int errval) {
 
145
        char *msg;
 
146
 
 
147
        switch (errval) {
 
148
 
 
149
        case WSAEINTR:
 
150
                msg = "Interrupted system call";
 
151
                break;
 
152
 
 
153
        case WSAEBADF:
 
154
                msg = "Bad file number";
 
155
                break;
 
156
 
 
157
        case WSAEACCES:
 
158
                msg = "Permission denied";
 
159
                break;
 
160
 
 
161
        case WSAEFAULT:
 
162
                msg = "Bad address";
 
163
                break;
 
164
 
 
165
        case WSAEINVAL:
 
166
                msg = "Invalid argument";
 
167
                break;
 
168
 
 
169
        case WSAEMFILE:
 
170
                msg = "Too many open sockets";
 
171
                break;
 
172
 
 
173
        case WSAEWOULDBLOCK:
 
174
                msg = "Operation would block";
 
175
                break;
 
176
 
 
177
        case WSAEINPROGRESS:
 
178
                msg = "Operation now in progress";
 
179
                break;
 
180
 
 
181
        case WSAEALREADY:
 
182
                msg = "Operation already in progress";
 
183
                break;
 
184
 
 
185
        case WSAENOTSOCK:
 
186
                msg = "Socket operation on non-socket";
 
187
                break;
 
188
 
 
189
        case WSAEDESTADDRREQ:
 
190
                msg = "Destination address required";
 
191
                break;
 
192
 
 
193
        case WSAEMSGSIZE:
 
194
                msg = "Message too long";
 
195
                break;
 
196
 
 
197
        case WSAEPROTOTYPE:
 
198
                msg = "Protocol wrong type for socket";
 
199
                break;
 
200
 
 
201
        case WSAENOPROTOOPT:
 
202
                msg = "Bad protocol option";
 
203
                break;
 
204
 
 
205
        case WSAEPROTONOSUPPORT:
 
206
                msg = "Protocol not supported";
 
207
                break;
 
208
 
 
209
        case WSAESOCKTNOSUPPORT:
 
210
                msg = "Socket type not supported";
 
211
                break;
 
212
 
 
213
        case WSAEOPNOTSUPP:
 
214
                msg = "Operation not supported on socket";
 
215
                break;
 
216
 
 
217
        case WSAEPFNOSUPPORT:
 
218
                msg = "Protocol family not supported";
 
219
                break;
 
220
 
 
221
        case WSAEAFNOSUPPORT:
 
222
                msg = "Address family not supported";
 
223
                break;
 
224
 
 
225
        case WSAEADDRINUSE:
 
226
                msg = "Address already in use";
 
227
                break;
 
228
 
 
229
        case WSAEADDRNOTAVAIL:
 
230
                msg = "Can't assign requested address";
 
231
                break;
 
232
 
 
233
        case WSAENETDOWN:
 
234
                msg = "Network is down";
 
235
                break;
 
236
 
 
237
        case WSAENETUNREACH:
 
238
                msg = "Network is unreachable";
 
239
                break;
 
240
 
 
241
        case WSAENETRESET:
 
242
                msg = "Net connection reset";
 
243
                break;
 
244
 
 
245
        case WSAECONNABORTED:
 
246
                msg = "Software caused connection abort";
 
247
                break;
 
248
 
 
249
        case WSAECONNRESET:
 
250
                msg = "Connection reset by peer";
 
251
                break;
 
252
 
 
253
        case WSAENOBUFS:
 
254
                msg = "No buffer space available";
 
255
                break;
 
256
 
 
257
        case WSAEISCONN:
 
258
                msg = "Socket is already connected";
 
259
                break;
 
260
 
 
261
        case WSAENOTCONN:
 
262
                msg = "Socket is not connected";
 
263
                break;
 
264
 
 
265
        case WSAESHUTDOWN:
 
266
                msg = "Can't send after socket shutdown";
 
267
                break;
 
268
 
 
269
        case WSAETOOMANYREFS:
 
270
                msg = "Too many references: can't splice";
 
271
                break;
 
272
 
 
273
        case WSAETIMEDOUT:
 
274
                msg = "Connection timed out";
 
275
                break;
 
276
 
 
277
        case WSAECONNREFUSED:
 
278
                msg = "Connection refused";
 
279
                break;
 
280
 
 
281
        case WSAELOOP:
 
282
                msg = "Too many levels of symbolic links";
 
283
                break;
 
284
 
 
285
        case WSAENAMETOOLONG:
 
286
                msg = "File name too long";
 
287
                break;
 
288
 
 
289
        case WSAEHOSTDOWN:
 
290
                msg = "Host is down";
 
291
                break;
 
292
 
 
293
        case WSAEHOSTUNREACH:
 
294
                msg = "No route to host";
 
295
                break;
 
296
 
 
297
        case WSAENOTEMPTY:
 
298
                msg = "Directory not empty";
 
299
                break;
 
300
 
 
301
        case WSAEPROCLIM:
 
302
                msg = "Too many processes";
 
303
                break;
 
304
 
 
305
        case WSAEUSERS:
 
306
                msg = "Too many users";
 
307
                break;
 
308
 
 
309
        case WSAEDQUOT:
 
310
                msg = "Disc quota exceeded";
 
311
                break;
 
312
 
 
313
        case WSAESTALE:
 
314
                msg = "Stale NFS file handle";
 
315
                break;
 
316
 
 
317
        case WSAEREMOTE:
 
318
                msg = "Too many levels of remote in path";
 
319
                break;
 
320
 
 
321
        case WSASYSNOTREADY:
 
322
                msg = "Network system is unavailable";
 
323
                break;
 
324
 
 
325
        case WSAVERNOTSUPPORTED:
 
326
                msg = "Winsock version out of range";
 
327
                break;
 
328
 
 
329
        case WSANOTINITIALISED:
 
330
                msg = "WSAStartup not yet called";
 
331
                break;
 
332
 
 
333
        case WSAEDISCON:
 
334
                msg = "Graceful shutdown in progress";
 
335
                break;
 
336
/*
 
337
        case WSAHOST_NOT_FOUND:
 
338
                msg = "Host not found";
 
339
                break;
 
340
 
 
341
        case WSANO_DATA:
 
342
                msg = "No host data of that type was found";
 
343
                break;
 
344
*/
 
345
        default:
 
346
                msg = NULL;
 
347
                break;
 
348
        }
 
349
        return (msg);
 
350
}
 
351
 
 
352
/*
 
353
 * These error messages are more informative about CryptAPI Errors than the
 
354
 * standard error messages
 
355
 */
 
356
 
 
357
char *
 
358
GetCryptErrorMessage(int errval) {
 
359
        char *msg;
 
360
 
 
361
        switch (errval) {
 
362
 
 
363
        case NTE_BAD_FLAGS:
 
364
                msg = "The dwFlags parameter has an illegal value.";
 
365
                break;
 
366
        case NTE_BAD_KEYSET:
 
367
                msg = "The Registry entry for the key container "
 
368
                        "could not be opened and may not exist.";
 
369
                break;
 
370
        case NTE_BAD_KEYSET_PARAM:
 
371
                msg = "The pszContainer or pszProvider parameter "
 
372
                        "is set to an illegal value.";
 
373
                break;
 
374
        case NTE_BAD_PROV_TYPE:
 
375
                msg = "The value of the dwProvType parameter is out "
 
376
                        "of range. All provider types must be from "
 
377
                        "1 to 999, inclusive.";
 
378
                break;
 
379
        case NTE_BAD_SIGNATURE:
 
380
                msg = "The provider DLL signature did not verify "
 
381
                        "correctly. Either the DLL or the digital "
 
382
                        "signature has been tampered with.";
 
383
                break;
 
384
        case NTE_EXISTS:
 
385
                msg = "The dwFlags parameter is CRYPT_NEWKEYSET, but the key"
 
386
                      " container already exists.";
 
387
                break;
 
388
        case NTE_KEYSET_ENTRY_BAD:
 
389
                msg = "The Registry entry for the pszContainer key container "
 
390
                      "was found (in the HKEY_CURRENT_USER window), but is "
 
391
                      "corrupt. See the section System Administration for "
 
392
                      " etails about CryptoAPI's Registry usage.";
 
393
                break;
 
394
        case NTE_KEYSET_NOT_DEF:
 
395
                msg = "No Registry entry exists in the HKEY_CURRENT_USER "
 
396
                        "window for the key container specified by "
 
397
                        "pszContainer.";
 
398
                break;
 
399
        case NTE_NO_MEMORY:
 
400
                msg = "The CSP ran out of memory during the operation.";
 
401
                break;
 
402
        case NTE_PROV_DLL_NOT_FOUND:
 
403
                msg = "The provider DLL file does not exist or is not on the "
 
404
                      "current path.";
 
405
                break;
 
406
        case NTE_PROV_TYPE_ENTRY_BAD:
 
407
                msg = "The Registry entry for the provider type specified by "
 
408
                      "dwProvType is corrupt. This error may relate to "
 
409
                      "either the user default CSP list or the machine "
 
410
                      "default CSP list. See the section System "
 
411
                      "Administration for details about CryptoAPI's "
 
412
                      "Registry usage.";
 
413
                break;
 
414
        case NTE_PROV_TYPE_NO_MATCH:
 
415
                msg = "The provider type specified by dwProvType does not "
 
416
                      "match the provider type found in the Registry. Note "
 
417
                      "that this error can only occur when pszProvider "
 
418
                      "specifies an actual CSP name.";
 
419
                break;
 
420
        case NTE_PROV_TYPE_NOT_DEF:
 
421
                msg = "No Registry entry exists for the provider type "
 
422
                      "specified by dwProvType.";
 
423
                break;
 
424
        case NTE_PROVIDER_DLL_FAIL:
 
425
                msg = "The provider DLL file could not be loaded, and "
 
426
                      "may not exist. If it exists, then the file is "
 
427
                      "not a valid DLL.";
 
428
                break;
 
429
        case NTE_SIGNATURE_FILE_BAD:
 
430
                msg = "An error occurred while loading the DLL file image, "
 
431
                      "prior to verifying its signature.";
 
432
                break;
 
433
 
 
434
        default:
 
435
                msg = NULL;
 
436
                break;
 
437
        }
 
438
        return msg;
 
439
}
 
440