#ifdef _WIN32
/*
- * DllMain(), required when built as a Windows DLL.
- *
* To quote the WSAStartup() documentation:
*
* The WSAStartup function typically leads to protocol-specific helper
* be called from the DllMain function in a application DLL. This can
* potentially cause deadlocks.
*
- * So we don't initialize Winsock here. pcap_init() should be called
- * to initialize pcap on both UN*X and Windows; it will initialize
- * Winsock on Windows. (It will also be initialized as needed if
- * pcap_init() hasn't been called.)
+ * So we don't initialize Winsock in a DllMain() routine.
+ *
+ * pcap_init() should be called to initialize pcap on both UN*X and
+ * Windows; it will initialize Winsock on Windows. (It will also be
+ * initialized as needed if pcap_init() hasn't been called.)
*/
-BOOL WINAPI DllMain(
- HANDLE hinstDLL _U_,
- DWORD dwReason _U_,
- LPVOID lpvReserved _U_
-)
-{
- return (TRUE);
-}
/*
* Start Winsock.
if (pcap_utf_8_mode) {
snprintf(errbuf, PCAP_ERRBUF_SIZE,
"Multiple pcap_init calls with different character encodings");
- return (-1);
+ return (PCAP_ERROR);
}
}
break;
if (!pcap_utf_8_mode) {
snprintf(errbuf, PCAP_ERRBUF_SIZE,
"Multiple pcap_init calls with different character encodings");
- return (-1);
+ return (PCAP_ERROR);
}
}
pcap_utf_8_mode = 1;
default:
snprintf(errbuf, PCAP_ERRBUF_SIZE, "Unknown options specified");
- return (-1);
+ return (PCAP_ERROR);
}
/*
*/
if (internal_wsockinit(errbuf) == -1) {
/* Failed. */
- return (-1);
+ return (PCAP_ERROR);
}
#endif
* Return codes for pcap_offline_read() are:
* - 0: EOF
* - -1: error
- * - >1: OK
+ * - >0: OK - result is number of packets read, so
+ * it will be 1 in this case, as we've passed
+ * a maximum packet count of 1
* The first one ('0') conflicts with the return code of
* 0 from pcap_read() meaning "no packets arrived before
* the timeout expired", so we map it to -2 so you can
* - 0: timeout
* - -1: error
* - -2: loop was broken out of with pcap_breakloop()
- * - >1: OK
+ * - >0: OK, result is number of packets captured, so
+ * it will be 1 in this case, as we've passed
+ * a maximum packet count of 1
* The first one ('0') conflicts with the return code of 0 from
* pcap_offline_read() meaning "end of file".
*/
fail:
if (status == PCAP_ERROR) {
/*
- * Another buffer is a bit cumbersome, but it avoids -Wformat-truncation.
+ * Another buffer is a bit cumbersome, but it avoids
+ * -Wformat-truncation.
*/
char trimbuf[PCAP_ERRBUF_SIZE - 5]; /* 2 bytes shorter */
status == PCAP_ERROR_PERM_DENIED ||
status == PCAP_ERROR_PROMISC_PERM_DENIED) {
/*
- * Idem.
+ * Only show the additional message if it's not
+ * empty.
*/
- char trimbuf[PCAP_ERRBUF_SIZE - 8]; /* 2 bytes shorter */
+ if (p->errbuf[0] != '\0') {
+ /*
+ * Idem.
+ */
+ char trimbuf[PCAP_ERRBUF_SIZE - 8]; /* 2 bytes shorter */
- pcap_strlcpy(trimbuf, p->errbuf, sizeof(trimbuf));
- snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s (%.*s)", device,
- pcap_statustostr(status), PCAP_ERRBUF_SIZE - 6, trimbuf);
- } else
+ pcap_strlcpy(trimbuf, p->errbuf, sizeof(trimbuf));
+ snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s (%.*s)",
+ device, pcap_statustostr(status),
+ PCAP_ERRBUF_SIZE - 6, trimbuf);
+ } else {
+ snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s",
+ device, pcap_statustostr(status));
+ }
+ } else {
snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", device,
pcap_statustostr(status));
+ }
pcap_close(p);
return (NULL);
}
if (description != NULL) {
return description;
} else {
- (void)snprintf(unkbuf, sizeof(unkbuf), "DLT %u", dlt);
+ (void)snprintf(unkbuf, sizeof(unkbuf), "DLT %d", dlt);
return unkbuf;
}
}
return ("That operation is supported only in monitor mode");
case PCAP_ERROR_PERM_DENIED:
- return ("You don't have permission to capture on that device");
+ return ("You don't have permission to perform this capture on that device");
case PCAP_ERROR_IFACE_NOT_UP:
return ("That device is not up");
}
/*
- * Helpers for safely loding code at run time.
+ * Helpers for safely loading code at run time.
* Currently Windows-only.
*/
#ifdef _WIN32
return (-1);
}
+static void
+pcap_breakloop_dead(pcap_t *p _U_)
+{
+ /*
+ * A "dead" pcap_t is just a placeholder to use in order to
+ * compile a filter to BPF code or to open a savefile for
+ * writing. It doesn't support any operations, including
+ * capturing or reading packets, so there will never be a
+ * get-packets loop in progress to break out *of*.
+ *
+ * As such, this routine doesn't need to do anything.
+ */
+}
+
static int
pcap_inject_dead(pcap_t *p, const void *buf _U_, int size _U_)
{
p->live_dump_ended_op = pcap_live_dump_ended_dead;
p->get_airpcap_handle_op = pcap_get_airpcap_handle_dead;
#endif
+ p->breakloop_op = pcap_breakloop_dead;
p->cleanup_op = pcap_cleanup_dead;
/*