]> The Tcpdump Group git mirrors - libpcap/commitdiff
Put pcap_win32_err_to_str() into fmtutils.c, and use it for all Win32 errors.
authorGuy Harris <[email protected]>
Wed, 9 Jan 2019 04:40:50 +0000 (20:40 -0800)
committerGuy Harris <[email protected]>
Wed, 9 Jan 2019 04:40:50 +0000 (20:40 -0800)
We don't need N different places all doing their own calls to
FormatMessage(); centralize it in pcap_win32_err_to_str(), now in
fmtutils.c for use in rpcapd as well as libpcap.

Merge in some fixes from the code in sock_fmterror().

fmtutils.c
fmtutils.h
pcap-int.h
pcap.c
rpcapd/daemon.c
rpcapd/win32-svc.c
sockutils.c

index f1a8907327deb5e34083110d9ec0a838e2d0150a..0c527453547f2c2e68d60518f9d070a22b3c6db6 100644 (file)
@@ -129,3 +129,59 @@ pcap_fmt_errmsg_for_errno(char *errbuf, size_t errbuflen, int errnum,
        pcap_snprintf(p, errbuflen_remaining, "%s", pcap_strerror(errnum));
 #endif
 }
+
+#ifdef _WIN32
+/*
+ * Generate a string for a Win32-specific error (i.e. an error generated when
+ * calling a Win32 API).
+ * For errors occurred during standard C calls, we still use pcap_strerror().
+ */
+void
+pcap_win32_err_to_str(DWORD error, char *errbuf)
+{
+       DWORD retval;
+       size_t errlen;
+       char *p;
+
+       /*
+        * XXX - what language ID to use?
+        *
+        * For UN*Xes, pcap_strerror() may or may not return localized
+        * strings.
+        *
+        * We currently don't have localized messages for libpcap, but
+        * we might want to do so.  On the other hand, if most of these
+        * messages are going to be read by libpcap developers and
+        * perhaps by developers of libpcap-based applications, English
+        * might be a better choice, so the developer doesn't have to
+        * get the message translated if it's in a language they don't
+        * happen to understand.
+        */
+       retval = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS|FORMAT_MESSAGE_MAX_WIDTH_MASK,
+           NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
+           errbuf, PCAP_ERRBUF_SIZE, NULL);
+       if (retval == 0) {
+               /*
+                * Failed.
+                */
+               pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
+                   "Couldn't get error message for error (%lu)", error);
+               return;
+       }
+
+       /*
+        * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
+        * message.  Get rid of it.
+        *
+        * XXX - still true with FORMAT_MESSAGE_MAX_WIDTH_MASK?
+        */
+       errlen = strlen(errbuf);
+       if (errlen >= 2 &&
+           errbuf[errlen - 2] == '\r' && errbuf[errlen - 1] == '\n') {
+               errbuf[errlen - 2] = '\0';
+               errbuf[errlen - 1] = '\0';
+       }
+       p = strchr(errbuf, '\0');
+       pcap_snprintf(p, PCAP_ERRBUF_SIZE+1-(p-errbuf), " (%lu)", error);
+}
+#endif
index 62c78fdba1b25c84bc83259631b19d6f31617fe1..d4a171d8555895c92dd73b2b1b250300a4009cc9 100644 (file)
@@ -43,6 +43,10 @@ extern "C" {
 void   pcap_fmt_errmsg_for_errno(char *, size_t, int,
     PCAP_FORMAT_STRING(const char *), ...) PCAP_PRINTFLIKE(4, 5);
 
+#ifdef _WIN32
+void   pcap_win32_err_to_str(DWORD, char *);
+#endif
+
 #ifdef __cplusplus
 }
 #endif
index ec643eba3359d9e16427b1a334627330470e260a..d18dd1928ec533cfbe982b60c2908824c7a2ee4b 100644 (file)
@@ -521,10 +521,6 @@ int        pcap_validate_filter(const struct bpf_insn *, int);
  */
 void   pcap_oneshot(u_char *, const struct pcap_pkthdr *, const u_char *);
 
-#ifdef _WIN32
-void   pcap_win32_err_to_str(DWORD, char *);
-#endif
-
 int    install_bpf_program(pcap_t *, struct bpf_program *);
 
 int    pcap_strcasecmp(const char *, const char *);
diff --git a/pcap.c b/pcap.c
index 6fe8e16aabce9047a8c1e0cfddb009fa84084c71..84dbe9a881f89c47ee7bfd423f4b7945b0a9cabc 100644 (file)
--- a/pcap.c
+++ b/pcap.c
@@ -3273,35 +3273,6 @@ pcap_setnonblock_fd(pcap_t *p, int nonblock)
 }
 #endif
 
-#ifdef _WIN32
-/*
- * Generate a string for a Win32-specific error (i.e. an error generated when
- * calling a Win32 API).
- * For errors occurred during standard C calls, we still use pcap_strerror()
- */
-void
-pcap_win32_err_to_str(DWORD error, char *errbuf)
-{
-       size_t errlen;
-       char *p;
-
-       FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
-           PCAP_ERRBUF_SIZE, NULL);
-
-       /*
-        * "FormatMessage()" "helpfully" sticks CR/LF at the end of the
-        * message.  Get rid of it.
-        */
-       errlen = strlen(errbuf);
-       if (errlen >= 2) {
-               errbuf[errlen - 1] = '\0';
-               errbuf[errlen - 2] = '\0';
-       }
-       p = strchr(errbuf, '\0');
-       pcap_snprintf (p, PCAP_ERRBUF_SIZE+1-(p-errbuf), " (%lu)", error);
-}
-#endif
-
 /*
  * Generate error strings for PCAP_ERROR_ and PCAP_WARNING_ values.
  */
index 90bc0cc6dc43297bdb0c00cea0a6c26add1638dd..7833d11b01509c48960a8438c686fb790784a6cf 100644 (file)
@@ -1228,12 +1228,7 @@ daemon_AuthUserPwd(char *username, char *password, char *errbuf)
        HANDLE Token;
        if (LogonUser(username, ".", password, LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &Token) == 0)
        {
-               int error;
-
-               error = GetLastError();
-               FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
-                       PCAP_ERRBUF_SIZE, NULL);
-
+               pcap_win32_err_to_str(GetLastError(), errbuf);
                return -1;
        }
 
@@ -1241,12 +1236,7 @@ daemon_AuthUserPwd(char *username, char *password, char *errbuf)
        // I didn't test it.
        if (ImpersonateLoggedOnUser(Token) == 0)
        {
-               int error;
-
-               error = GetLastError();
-               FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM, NULL, error, 0, errbuf,
-                       PCAP_ERRBUF_SIZE, NULL);
-
+               pcap_win32_err_to_str(GetLastError(), errbuf);
                CloseHandle(Token);
                return -1;
        }
index c846b2cb097e688207ea528879774a2b5890f37f..5d132a0655c53dc51193467ee083c5e8f29e5fd7 100644 (file)
@@ -33,6 +33,7 @@
 
 #include "rpcapd.h"
 #include <pcap.h>              // for PCAP_ERRBUF_SIZE
+#include "fmtutils.h"
 #include "portability.h"
 #include "fileconf.h"
 #include "log.h"
 static SERVICE_STATUS_HANDLE service_status_handle;
 static SERVICE_STATUS service_status;
 
-void svc_geterr(char *str);
 static void WINAPI svc_main(DWORD argc, char **argv);
 static void update_svc_status(DWORD state, DWORD progress_indicator);
 
 int svc_start(void)
 {
-       int rc;
        SERVICE_TABLE_ENTRY ste[] =
        {
                { PROGRAM_NAME, svc_main },
                { NULL, NULL }
        };
+       char string[PCAP_ERRBUF_SIZE];
 
        // This call is blocking. A new thread is created which will launch
        // the svc_main() function
-       if ( (rc = StartServiceCtrlDispatcher(ste)) == 0)
-               svc_geterr("StartServiceCtrlDispatcher()");
+       if (StartServiceCtrlDispatcher(ste) == 0) {
+               pcap_win32_err_to_str(GetLastError(), string);
+               rpcapd_log(LOGPRIO_ERROR,
+                   "StartServiceCtrlDispatcher() failed: %s", string);
+       }
 
        return rc; // FALSE if this is not started as a service
 }
 
-void svc_geterr(char *str)
-{
-       char message[PCAP_ERRBUF_SIZE];
-       char string[PCAP_ERRBUF_SIZE];
-       int val;
-
-       val = GetLastError();
-       FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS |
-                                 FORMAT_MESSAGE_MAX_WIDTH_MASK,
-                                 NULL, val, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
-                                 (LPSTR) string, PCAP_ERRBUF_SIZE, NULL);
-
-       rpcapd_log(LOGPRIO_ERROR, "%s failed with error %d: %s", str, val, string);
-}
-
 void WINAPI svc_control_handler(DWORD Opcode)
 {
        switch(Opcode)
index 09d1a9616f23aae4af4b6b2b81558995df6b44cf..57bcdf666370340a795219925826a65a77d12212 100644 (file)
@@ -133,25 +133,11 @@ void sock_fmterror(const char *caller, int errcode, char *errbuf, int errbuflen)
        if (errbuf == NULL)
                return;
 
-       retval = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS |
-               FORMAT_MESSAGE_MAX_WIDTH_MASK,
-               NULL, errcode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
-               message, sizeof(message) / sizeof(TCHAR), NULL);
-
-       if (retval == 0)
-       {
-               if ((caller) && (*caller))
-                       pcap_snprintf(errbuf, errbuflen, "%sUnable to get the exact error message", caller);
-               else
-                       pcap_snprintf(errbuf, errbuflen, "Unable to get the exact error message");
-       }
+       pcap_win32_err_to_str(errcode, message);
+       if ((caller) && (*caller))
+               pcap_snprintf(errbuf, errbuflen, "%s%s", caller, message, errcode);
        else
-       {
-               if ((caller) && (*caller))
-                       pcap_snprintf(errbuf, errbuflen, "%s%s (code %d)", caller, message, errcode);
-               else
-                       pcap_snprintf(errbuf, errbuflen, "%s (code %d)", message, errcode);
-       }
+               pcap_snprintf(errbuf, errbuflen, "%s", message, errcode);
 #else
        char *message;
 
@@ -161,9 +147,9 @@ void sock_fmterror(const char *caller, int errcode, char *errbuf, int errbuflen)
        message = strerror(errcode);
 
        if ((caller) && (*caller))
-               pcap_snprintf(errbuf, errbuflen, "%s%s (code %d)", caller, message, errcode);
+               pcap_snprintf(errbuf, errbuflen, "%s%s (%d)", caller, message, errcode);
        else
-               pcap_snprintf(errbuf, errbuflen, "%s (code %d)", message, errcode);
+               pcap_snprintf(errbuf, errbuflen, "%s (%d)", message, errcode);
 #endif
 }