diff options
author | Guy Harris <guy@alum.mit.edu> | 2017-09-06 22:01:38 -0700 |
---|---|---|
committer | Guy Harris <guy@alum.mit.edu> | 2017-09-06 22:01:38 -0700 |
commit | f290068cc95f6e635791ee7f871e7118b6458502 (patch) | |
tree | 9f3fd77cf51551ccbc12bf6598218978c2fe33e4 | |
parent | bad8c25c99cf5511125c7a7e688113c0811841c4 (diff) |
Push pcap_lib_version() into the pcap-XXX.c files.
This lets us move the Windows version to pcap-win32.c, so we don't have
to include packet32.h in pcap.c.
It also gets rid of some #ifdefs, and lets us construct the version
string on UN*X platforms, and MS-DOS, at compile time.
-rw-r--r-- | pcap-bpf.c | 8 | ||||
-rw-r--r-- | pcap-dag.c | 8 | ||||
-rw-r--r-- | pcap-dlpi.c | 8 | ||||
-rw-r--r-- | pcap-dos.c | 10 | ||||
-rw-r--r-- | pcap-int.h | 8 | ||||
-rw-r--r-- | pcap-libdlpi.c | 8 | ||||
-rw-r--r-- | pcap-linux.c | 32 | ||||
-rw-r--r-- | pcap-nit.c | 8 | ||||
-rw-r--r-- | pcap-null.c | 8 | ||||
-rw-r--r-- | pcap-pf.c | 8 | ||||
-rw-r--r-- | pcap-septel.c | 8 | ||||
-rw-r--r-- | pcap-sita.c | 8 | ||||
-rw-r--r-- | pcap-snf.c | 8 | ||||
-rw-r--r-- | pcap-snit.c | 8 | ||||
-rw-r--r-- | pcap-snoop.c | 8 | ||||
-rw-r--r-- | pcap-win32.c | 120 | ||||
-rw-r--r-- | pcap.c | 204 | ||||
-rw-r--r-- | pcap/pcap.h | 15 | ||||
-rw-r--r-- | pcap_version.h.in | 17 |
19 files changed, 224 insertions, 278 deletions
@@ -3161,15 +3161,17 @@ pcap_set_datalink_bpf(pcap_t *p, int dlt) return (0); } +#include "pcap_version.h" + /* * Platform-specific information. */ const char * -pcap_platform_lib_version(void) +pcap_lib_version(void) { #ifdef HAVE_ZEROCOPY_BPF - return ("with zerocopy support"); + return (PCAP_VERSION_STRING " (with zerocopy support)"); #else - return (NULL); + return (PCAP_VERSION_STRING); #endif } @@ -1360,12 +1360,14 @@ pcap_create_interface(const char *device, char *errbuf) return NULL; } +#include "pcap_version.h" + /* - * Platform-specific information. + * Libpcap version string. */ const char * -pcap_platform_lib_version(void) +pcap_lib_version(void) { - return ("DAG-only"); + return (PCAP_VERSION_STRING " (DAG-only)"); } #endif diff --git a/pcap-dlpi.c b/pcap-dlpi.c index fc3f397d..b7b54e71 100644 --- a/pcap-dlpi.c +++ b/pcap-dlpi.c @@ -1860,11 +1860,13 @@ pcap_create_interface(const char *device _U_, char *ebuf) return (p); } +#include "pcap_version.h" + /* - * No platform-specific information. + * Libpcap version string. */ const char * -pcap_platform_lib_version(void) +pcap_lib_version(void) { - return (NULL); + return (PCAP_VERSION_STRING); } @@ -1523,3 +1523,13 @@ static void pktq_clear (struct rx_ringbuf *q) #endif /* USE_32BIT_DRIVERS */ +#include "pcap_version.h" + +/* + * Libpcap version string. + */ +const char * +pcap_lib_version(void) +{ + return ("DOS-" PCAP_VERSION_STRING); +} @@ -393,14 +393,6 @@ int pcap_setnonblock_fd(pcap_t *p, int); #endif /* - * Internal interfaces for "pcap_lib_version()". - * - * "pcap_platform_lib_version()" returns platform-specific version - * information, or NULL if there isn't any. - */ -const char *pcap_platform_lib_version(void); - -/* * Internal interfaces for "pcap_create()". * * "pcap_create_interface()" is the routine to do a pcap_create on diff --git a/pcap-libdlpi.c b/pcap-libdlpi.c index 097dd518..d19a95b0 100644 --- a/pcap-libdlpi.c +++ b/pcap-libdlpi.c @@ -462,11 +462,13 @@ pcap_create_interface(const char *device _U_, char *ebuf) return (p); } +#include "pcap_version.h" + /* - * No platform-specific information. + * Libpcap version string. */ const char * -pcap_platform_lib_version(void) +pcap_lib_version(void) { - return (NULL); + return (PCAP_VERSION_STRING); } diff --git a/pcap-linux.c b/pcap-linux.c index 9934c5e1..fd839416 100644 --- a/pcap-linux.c +++ b/pcap-linux.c @@ -6946,30 +6946,32 @@ reset_kernel_filter(pcap_t *handle) } #endif +int +pcap_set_protocol(pcap_t *p, int protocol) +{ + if (pcap_check_activated(p)) + return (PCAP_ERROR_ACTIVATED); + p->opt.protocol = protocol; + return (0); +} + +#include "pcap_version.h" + /* - * Platform-specific information. + * Libpcap version string. */ const char * -pcap_platform_lib_version(void) +pcap_lib_version(void) { #ifdef HAVE_PACKET_RING #if defined(HAVE_TPACKET3) - return ("with TPACKET_V3"); + return (PCAP_VERSION_STRING " (with TPACKET_V3)"); #elif defined(HAVE_TPACKET2) - return ("with TPACKET_V2"); + return (PCAP_VERSION_STRING " (with TPACKET_V2)"); #else - return ("with TPACKET_V1"); + return (PCAP_VERSION_STRING " (with TPACKET_V1)"); #endif #else - return ("without TPACKET"); + return (PCAP_VERSION_STRING " (without TPACKET)"); #endif } - -int -pcap_set_protocol(pcap_t *p, int protocol) -{ - if (pcap_check_activated(p)) - return (PCAP_ERROR_ACTIVATED); - p->opt.protocol = protocol; - return (0); -} @@ -394,11 +394,13 @@ pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) return (pcap_findalldevs_interfaces(devlistp, errbuf, can_be_bound)); } +#include "pcap_version.h" + /* - * No platform-specific information. + * Libpcap version string. */ const char * -pcap_platform_lib_version(void) +pcap_lib_version(void) { - return (NULL); + return (PCAP_VERSION_STRING); } diff --git a/pcap-null.c b/pcap-null.c index 2334b946..c4d4cfa9 100644 --- a/pcap-null.c +++ b/pcap-null.c @@ -51,11 +51,13 @@ pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) return (0); } +#include "pcap_version.h" + /* - * No platform-specific information. + * Libpcap version string. */ const char * -pcap_platform_lib_version(void) +pcap_lib_version(void) { - return (NULL); + return (PCAP_VERSION_STRING); } @@ -633,11 +633,13 @@ pcap_setfilter_pf(pcap_t *p, struct bpf_program *fp) return (0); } +#include "pcap_version.h" + /* - * No platform-specific information. + * Libpcap version string. */ const char * -pcap_platform_lib_version(void) +pcap_lib_version(void) { - return (NULL); + return (PCAP_VERSION_STRING); } diff --git a/pcap-septel.c b/pcap-septel.c index 8f8bcb27..cd864f8e 100644 --- a/pcap-septel.c +++ b/pcap-septel.c @@ -331,12 +331,14 @@ pcap_create_interface(const char *device, char *errbuf) return (NULL); } +#include "pcap_version.h" + /* - * Platform-specific information. + * Libpcap version string. */ const char * -pcap_platform_lib_version(void) +pcap_lib_version(void) { - return ("Septel-only"); + return (PCAP_VERSION_STRING " (Septel-only)"); } #endif diff --git a/pcap-sita.c b/pcap-sita.c index 6bb82ef9..ea63f7cd 100644 --- a/pcap-sita.c +++ b/pcap-sita.c @@ -1060,11 +1060,13 @@ int pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) { return 0; } +#include "pcap_version.h" + /* - * Platform-specific information. + * Libpcap version string. */ const char * -pcap_platform_lib_version(void) +pcap_lib_version(void) { - return ("SITA-only"); + return PCAP_VERSION_STRING " (SITA-only)"; } @@ -592,12 +592,14 @@ pcap_create_interface(const char *device, char *errbuf) return NULL; } +#include "pcap_version.h" + /* - * Platform-specific information. + * Libpcap version string. */ const char * -pcap_platform_lib_version(void) +pcap_lib_version(void) { - return ("SNF-only"); + return (PCAP_VERSION_STRING " (SNF-only)"); } #endif diff --git a/pcap-snit.c b/pcap-snit.c index ef2cac02..a129b11e 100644 --- a/pcap-snit.c +++ b/pcap-snit.c @@ -470,11 +470,13 @@ pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) return (pcap_findalldevs_interfaces(devlistp, errbuf, can_be_bound)); } +#include "pcap_version.h" + /* - * No platform-specific information. + * Libpcap version string. */ const char * -pcap_platform_lib_version(void) +pcap_lib_version(void) { - return (NULL); + return (PCAP_VERSION_STRING); } diff --git a/pcap-snoop.c b/pcap-snoop.c index 76c2b857..facf2fdd 100644 --- a/pcap-snoop.c +++ b/pcap-snoop.c @@ -445,11 +445,13 @@ pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) return (pcap_findalldevs_interfaces(devlistp, errbuf, can_be_bound)); } +#include "pcap_version.h" + /* - * No platform-specific information. + * Libpcap version string. */ const char * -pcap_platform_lib_version(void) +pcap_lib_version(void) { - return (NULL); + return (PCAP_VERSION_STRING); } diff --git a/pcap-win32.c b/pcap-win32.c index c1f8352c..8570396a 100644 --- a/pcap-win32.c +++ b/pcap-win32.c @@ -1755,3 +1755,123 @@ pcap_lookupnet(device, netp, maskp, errbuf) *netp = *maskp = 0; return (0); } + +#include "pcap_version.h" + +static const char *pcap_lib_version_string; + +#ifdef HAVE_VERSION_H +/* + * libpcap being built for Windows, as part of a WinPcap/Npcap source + * tree. Include version.h from that source tree to get the WinPcap/Npcap + * version. + * + * XXX - it'd be nice if we could somehow generate the WinPcap version number + * when building WinPcap. (It'd be nice to do so for the packet.dll version + * number as well.) + */ +#include "../../version.h" + +static const char wpcap_version_string[] = WINPCAP_VER_STRING; +static const char pcap_version_string_fmt[] = + WINPCAP_PRODUCT_NAME " version %s, based on %s"; +static const char pcap_version_string_packet_dll_fmt[] = + WINPCAP_PRODUCT_NAME " version %s (packet.dll version %s), based on %s"; + +const char * +pcap_lib_version(void) +{ + char *packet_version_string; + size_t full_pcap_version_string_len; + char *full_pcap_version_string; + + if (pcap_lib_version_string == NULL) { + /* + * Generate the version string. + */ + packet_version_string = PacketGetVersion(); + if (strcmp(wpcap_version_string, packet_version_string) == 0) { + /* + * WinPcap version string and packet.dll version + * string are the same; just report the WinPcap + * version. + */ + full_pcap_version_string_len = + (sizeof pcap_version_string_fmt - 4) + + strlen(wpcap_version_string) + + strlen(pcap_version_string); + full_pcap_version_string = + malloc(full_pcap_version_string_len); + if (full_pcap_version_string == NULL) + return (NULL); + pcap_snprintf(full_pcap_version_string, + full_pcap_version_string_len, + pcap_version_string_fmt, + wpcap_version_string, + pcap_version_string); + } else { + /* + * WinPcap version string and packet.dll version + * string are different; that shouldn't be the + * case (the two libraries should come from the + * same version of WinPcap), so we report both + * versions. + */ + full_pcap_version_string_len = + (sizeof pcap_version_string_packet_dll_fmt - 6) + + strlen(wpcap_version_string) + + strlen(packet_version_string) + + strlen(pcap_version_string); + full_pcap_version_string = malloc(full_pcap_version_string_len); + if (full_pcap_version_string == NULL) + return (NULL); + pcap_snprintf(full_pcap_version_string, + full_pcap_version_string_len, + pcap_version_string_packet_dll_fmt, + wpcap_version_string, + packet_version_string, + pcap_version_string); + } + pcap_lib_version_string = full_pcap_version_string; + } + return (pcap_lib_version_string); +} + +#else /* HAVE_VERSION_H */ + +/* + * libpcap being built for Windows, not as part of a WinPcap/Npcap source + * tree. + */ +static const char pcap_version_string_packet_dll_fmt[] = + "%s (packet.dll version %s)"; +const char * +pcap_lib_version(void) +{ + char *packet_version_string; + size_t full_pcap_version_string_len; + char *full_pcap_version_string; + + if (pcap_lib_version_string == NULL) { + /* + * Generate the version string. Report the packet.dll + * version. + */ + packet_version_string = PacketGetVersion(); + full_pcap_version_string_len = + (sizeof pcap_version_string_packet_dll_fmt - 4) + + strlen(pcap_version_string) + + strlen(packet_version_string); + full_pcap_version_string = malloc(full_pcap_version_string_len); + if (full_pcap_version_string == NULL) + return (NULL); + pcap_snprintf(full_pcap_version_string, + full_pcap_version_string_len, + pcap_version_string_packet_dll_fmt, + pcap_version_string, + packet_version_string); + pcap_lib_version_string = full_pcap_version_string; + } + return (pcap_lib_version_string); +} +#endif /* HAVE_VERSION_H */ @@ -36,14 +36,7 @@ #endif #include <pcap-types.h> -#ifdef _WIN32 -/* - * Make sure Packet32.h doesn't define BPF structures that we've - * probably already defined as a result of including <pcap/pcap.h>. - */ -#define BPF_MAJOR_VERSION -#include <Packet32.h> /* for PacketGetVersion() */ -#else +#ifndef _WIN32 #include <sys/param.h> #ifndef MSDOS #include <sys/file.h> @@ -3512,201 +3505,6 @@ pcap_offline_filter(const struct bpf_program *fp, const struct pcap_pkthdr *h, return (0); } -#include "pcap_version.h" - -static const char *pcap_lib_version_string; - -#ifdef _WIN32 - -#ifdef HAVE_VERSION_H -/* - * libpcap being built for Windows, as part of a WinPcap/Npcap source - * tree. Include version.h from that source tree to get the WinPcap/Npcap - * version. - * - * XXX - it'd be nice if we could somehow generate the WinPcap version number - * when building WinPcap. (It'd be nice to do so for the packet.dll version - * number as well.) - */ -#include "../../version.h" - -static const char wpcap_version_string[] = WINPCAP_VER_STRING; -static const char pcap_version_string_fmt[] = - WINPCAP_PRODUCT_NAME " version %s, based on %s"; -static const char pcap_version_string_packet_dll_fmt[] = - WINPCAP_PRODUCT_NAME " version %s (packet.dll version %s), based on %s"; - -const char * -pcap_lib_version(void) -{ - char *packet_version_string; - size_t full_pcap_version_string_len; - char *full_pcap_version_string; - - if (pcap_lib_version_string == NULL) { - /* - * Generate the version string. - */ - packet_version_string = PacketGetVersion(); - if (strcmp(wpcap_version_string, packet_version_string) == 0) { - /* - * WinPcap version string and packet.dll version - * string are the same; just report the WinPcap - * version. - */ - full_pcap_version_string_len = - (sizeof pcap_version_string_fmt - 4) + - strlen(wpcap_version_string) + - strlen(pcap_version_string); - full_pcap_version_string = - malloc(full_pcap_version_string_len); - if (full_pcap_version_string == NULL) - return (NULL); - pcap_snprintf(full_pcap_version_string, - full_pcap_version_string_len, - pcap_version_string_fmt, - wpcap_version_string, - pcap_version_string); - } else { - /* - * WinPcap version string and packet.dll version - * string are different; that shouldn't be the - * case (the two libraries should come from the - * same version of WinPcap), so we report both - * versions. - */ - full_pcap_version_string_len = - (sizeof pcap_version_string_packet_dll_fmt - 6) + - strlen(wpcap_version_string) + - strlen(packet_version_string) + - strlen(pcap_version_string); - full_pcap_version_string = malloc(full_pcap_version_string_len); - if (full_pcap_version_string == NULL) - return (NULL); - pcap_snprintf(full_pcap_version_string, - full_pcap_version_string_len, - pcap_version_string_packet_dll_fmt, - wpcap_version_string, - packet_version_string, - pcap_version_string); - } - pcap_lib_version_string = full_pcap_version_string; - } - return (pcap_lib_version_string); -} - -#else /* HAVE_VERSION_H */ - -/* - * libpcap being built for Windows, not as part of a WinPcap/Npcap source - * tree. - */ -static const char pcap_version_string_packet_dll_fmt[] = - "%s (packet.dll version %s)"; -const char * -pcap_lib_version(void) -{ - char *packet_version_string; - size_t full_pcap_version_string_len; - char *full_pcap_version_string; - - if (pcap_lib_version_string == NULL) { - /* - * Generate the version string. Report the packet.dll - * version. - */ - packet_version_string = PacketGetVersion(); - full_pcap_version_string_len = - (sizeof pcap_version_string_packet_dll_fmt - 4) + - strlen(pcap_version_string) + - strlen(packet_version_string); - full_pcap_version_string = malloc(full_pcap_version_string_len); - if (full_pcap_version_string == NULL) - return (NULL); - pcap_snprintf(full_pcap_version_string, - full_pcap_version_string_len, - pcap_version_string_packet_dll_fmt, - pcap_version_string, - packet_version_string); - pcap_lib_version_string = full_pcap_version_string; - } - return (pcap_lib_version_string); -} - -#endif /* HAVE_VERSION_H */ - -#elif defined(MSDOS) - -const char * -pcap_lib_version(void) -{ - char *packet_version_string; - size_t full_pcap_version_string_len; - char *full_pcap_version_string; - static char dospfx[] = "DOS-"; - - if (pcap_lib_version_string == NULL) { - /* - * Generate the version string. - */ - full_pcap_version_string_len = - sizeof dospfx + strlen(pcap_version_string); - full_pcap_version_string = - malloc(full_pcap_version_string_len); - if (full_pcap_version_string == NULL) - return (NULL); - strcpy(full_pcap_version_string, dospfx); - strcat(full_pcap_version_string, pcap_version_string); - pcap_lib_version_string = full_pcap_version_string; - } - return (pcap_lib_version_string); -} - -#else /* UN*X */ - -const char * -pcap_lib_version(void) -{ - const char *platform_version_string; - size_t full_pcap_version_string_len; - char *full_pcap_version_string; - - if (pcap_lib_version_string == NULL) { - /* - * Generate the version string. - * Get any platform-specific information. - * - * XXX - what about all the local capture modules other - * that the "native interface" one? That could make - * the version string really long. - */ - platform_version_string = pcap_platform_lib_version(); - if (platform_version_string == NULL) { - /* - * No platform-specific information. - */ - pcap_lib_version_string = pcap_version_string; - } else { - /* - * Add on the platform-specific information. - */ - full_pcap_version_string_len = - strlen(pcap_version_string) + 2 + strlen(platform_version_string) + 1 + 1; - full_pcap_version_string = - malloc(full_pcap_version_string_len); - if (full_pcap_version_string == NULL) - return (NULL); - pcap_snprintf(full_pcap_version_string, - full_pcap_version_string_len, - "%s (%s)", pcap_version_string, - platform_version_string); - pcap_lib_version_string = full_pcap_version_string; - } - } - return (pcap_lib_version_string); -} -#endif - #ifdef YYDEBUG /* * Set the internal "debug printout" flag for the filter expression parser. diff --git a/pcap/pcap.h b/pcap/pcap.h index 59ab73f6..92a7270e 100644 --- a/pcap/pcap.h +++ b/pcap/pcap.h @@ -479,6 +479,21 @@ PCAP_API void pcap_dump(u_char *, const struct pcap_pkthdr *, const u_char *); PCAP_API int pcap_findalldevs(pcap_if_t **, char *); PCAP_API void pcap_freealldevs(pcap_if_t *); +/* + * We return a pointer to the version string, rather than exporting the + * version string directly. + * + * On at least some UNIXes, if you import data from a shared library into + * an program, the data is bound into the program binary, so if the string + * in the version of the library with which the program was linked isn't + * the same as the string in the version of the library with which the + * program is being run, various undesirable things may happen (warnings, + * the string being the one from the version of the library with which the + * program was linked, or even weirder things, such as the string being the + * one from the library but being truncated). + * + * On Windows, the string is constructed at run time. + */ PCAP_API const char *pcap_lib_version(void); /* diff --git a/pcap_version.h.in b/pcap_version.h.in index 187bc160..2ae91d04 100644 --- a/pcap_version.h.in +++ b/pcap_version.h.in @@ -1,16 +1 @@ -/* - * We make the version string static, and return a pointer to it, rather - * than exporting the version string directly. On at least some UNIXes, - * if you import data from a shared library into an program, the data is - * bound into the program binary, so if the string in the version of the - * library with which the program was linked isn't the same as the - * string in the version of the library with which the program is being - * run, various undesirable things may happen (warnings, the string - * being the one from the version of the library with which the program - * was linked, or even weirder things, such as the string being the one - * from the library but being truncated). - * - * In addition, we may add additional information to this string, so - * pcap_lib_version() might not return this string alone anyway. - */ -static const char pcap_version_string[] = "libpcap version %%LIBPCAP_VERSION%%"; +#define PCAP_VERSION_STRING "libpcap version %%LIBPCAP_VERSION%%" |