"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.11.23/lib/isc/win32/strerror.c" (7 Sep 2020, 9585 Bytes) of package /linux/misc/dns/bind9/9.11.23/bind-9.11.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "strerror.c" see the Fossies "Dox" file reference documentation.

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