19185e895Schristos /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */ 29185e895Schristos /* 39185e895Schristos * Copyright (c) 1993, 1994, 1995, 1996, 1997 49185e895Schristos * The Regents of the University of California. All rights reserved. 59185e895Schristos * 69185e895Schristos * Redistribution and use in source and binary forms, with or without 79185e895Schristos * modification, are permitted provided that the following conditions 89185e895Schristos * are met: 99185e895Schristos * 1. Redistributions of source code must retain the above copyright 109185e895Schristos * notice, this list of conditions and the following disclaimer. 119185e895Schristos * 2. Redistributions in binary form must reproduce the above copyright 129185e895Schristos * notice, this list of conditions and the following disclaimer in the 139185e895Schristos * documentation and/or other materials provided with the distribution. 149185e895Schristos * 3. All advertising materials mentioning features or use of this software 159185e895Schristos * must display the following acknowledgement: 169185e895Schristos * This product includes software developed by the Computer Systems 179185e895Schristos * Engineering Group at Lawrence Berkeley Laboratory. 189185e895Schristos * 4. Neither the name of the University nor of the Laboratory may be used 199185e895Schristos * to endorse or promote products derived from this software without 209185e895Schristos * specific prior written permission. 219185e895Schristos * 229185e895Schristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 239185e895Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 249185e895Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 259185e895Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 269185e895Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 279185e895Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 289185e895Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 299185e895Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 309185e895Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 319185e895Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 329185e895Schristos * SUCH DAMAGE. 339185e895Schristos */ 349185e895Schristos 359185e895Schristos #ifndef lib_pcap_funcattrs_h 369185e895Schristos #define lib_pcap_funcattrs_h 379185e895Schristos 389185e895Schristos #include <pcap/compiler-tests.h> 399185e895Schristos 409185e895Schristos /* 419185e895Schristos * Attributes to apply to functions and their arguments, using various 429185e895Schristos * compiler-specific extensions. 439185e895Schristos */ 449185e895Schristos 459185e895Schristos /* 469185e895Schristos * PCAP_API_DEF must be used when defining *data* exported from 479185e895Schristos * libpcap. It can be used when defining *functions* exported 489185e895Schristos * from libpcap, but it doesn't have to be used there. It 499185e895Schristos * should not be used in declarations in headers. 509185e895Schristos * 519185e895Schristos * PCAP_API must be used when *declaring* data or functions 529185e895Schristos * exported from libpcap; PCAP_API_DEF won't work on all platforms. 539185e895Schristos */ 549185e895Schristos 559185e895Schristos #if defined(_WIN32) 569185e895Schristos /* 579185e895Schristos * For Windows: 589185e895Schristos * 599185e895Schristos * when building libpcap: 609185e895Schristos * 619185e895Schristos * if we're building it as a DLL, we have to declare API 629185e895Schristos * functions with __declspec(dllexport); 639185e895Schristos * 649185e895Schristos * if we're building it as a static library, we don't want 659185e895Schristos * to do so. 669185e895Schristos * 679185e895Schristos * when using libpcap: 689185e895Schristos * 699185e895Schristos * if we're using the DLL, calls to its functions are a 709185e895Schristos * little more efficient if they're declared with 719185e895Schristos * __declspec(dllimport); 729185e895Schristos * 739185e895Schristos * if we're not using the dll, we don't want to declare 749185e895Schristos * them that way. 759185e895Schristos * 769185e895Schristos * So: 779185e895Schristos * 789185e895Schristos * if pcap_EXPORTS is defined, we define PCAP_API_DEF as 799185e895Schristos * __declspec(dllexport); 809185e895Schristos * 819185e895Schristos * if PCAP_DLL is defined, we define PCAP_API_DEF as 829185e895Schristos * __declspec(dllimport); 839185e895Schristos * 849185e895Schristos * otherwise, we define PCAP_API_DEF as nothing. 859185e895Schristos */ 869185e895Schristos #if defined(pcap_EXPORTS) 879185e895Schristos /* 889185e895Schristos * We're compiling libpcap as a DLL, so we should export functions 899185e895Schristos * in our API. 909185e895Schristos */ 919185e895Schristos #define PCAP_API_DEF __declspec(dllexport) 929185e895Schristos #elif defined(PCAP_DLL) 939185e895Schristos /* 949185e895Schristos * We're using libpcap as a DLL, so the calls will be a little more 959185e895Schristos * efficient if we explicitly import the functions. 969185e895Schristos */ 979185e895Schristos #define PCAP_API_DEF __declspec(dllimport) 989185e895Schristos #else 999185e895Schristos /* 1009185e895Schristos * Either we're building libpcap as a static library, or we're using 1019185e895Schristos * it as a static library, or we don't know for certain that we're 1029185e895Schristos * using it as a dynamic library, so neither import nor export the 1039185e895Schristos * functions explicitly. 1049185e895Schristos */ 1059185e895Schristos #define PCAP_API_DEF 1069185e895Schristos #endif 1079185e895Schristos #elif defined(MSDOS) 1089185e895Schristos /* XXX - does this need special treatment? */ 1099185e895Schristos #define PCAP_API_DEF 1109185e895Schristos #else /* UN*X */ 1119185e895Schristos #ifdef pcap_EXPORTS 1129185e895Schristos /* 1139185e895Schristos * We're compiling libpcap as a (dynamic) shared library, so we should 1149185e895Schristos * export functions in our API. The compiler might be configured not 1159185e895Schristos * to export functions from a shared library by default, so we might 1169185e895Schristos * have to explicitly mark functions as exported. 1179185e895Schristos */ 1189185e895Schristos #if PCAP_IS_AT_LEAST_GNUC_VERSION(3,4) \ 1199185e895Schristos || PCAP_IS_AT_LEAST_XL_C_VERSION(12,0) 1209185e895Schristos /* 121748408edSchristos * GCC 3.4 and later, or some compiler asserting compatibility with 122748408edSchristos * GCC 3.4 and later, or XL C 13.0 and later, so we have 1239185e895Schristos * __attribute__((visibility()). 1249185e895Schristos */ 1259185e895Schristos #define PCAP_API_DEF __attribute__((visibility("default"))) 1269185e895Schristos #elif PCAP_IS_AT_LEAST_SUNC_VERSION(5,5) 1279185e895Schristos /* 128748408edSchristos * Sun C 5.5 and later, so we have __global. 1299185e895Schristos * (Sun C 5.9 and later also have __attribute__((visibility()), 1309185e895Schristos * but there's no reason to prefer it with Sun C.) 1319185e895Schristos */ 1329185e895Schristos #define PCAP_API_DEF __global 1339185e895Schristos #else 1349185e895Schristos /* 1359185e895Schristos * We don't have anything to say. 1369185e895Schristos */ 1379185e895Schristos #define PCAP_API_DEF 1389185e895Schristos #endif 1399185e895Schristos #else 1409185e895Schristos /* 1419185e895Schristos * We're not building libpcap. 1429185e895Schristos */ 1439185e895Schristos #define PCAP_API_DEF 1449185e895Schristos #endif 1459185e895Schristos #endif /* _WIN32/MSDOS/UN*X */ 1469185e895Schristos 1479185e895Schristos #define PCAP_API PCAP_API_DEF extern 1489185e895Schristos 1499185e895Schristos /* 150748408edSchristos * Definitions to 1) indicate what version of libpcap first had a given 151748408edSchristos * API and 2) allow upstream providers whose build environments allow 152748408edSchristos * APIs to be designated as "first available in this release" to do so 153748408edSchristos * by appropriately defining them. 154748408edSchristos * 155*f73a5f05Schristos * On macOS, Apple can tweak this to make various APIs "weakly exported 156*f73a5f05Schristos * symbols" to make it easier for software that's distributed in binary 157*f73a5f05Schristos * form and that uses libpcap to run on multiple macOS versions and use 158*f73a5f05Schristos * new APIs when available. (Yes, such third-party software exists - 159*f73a5f05Schristos * Wireshark provides binary packages for macOS, for example. tcpdump 160*f73a5f05Schristos * doesn't count, as that's provided by Apple, so each release can 161*f73a5f05Schristos * come with a version compiled to use the APIs present in that release.) 162748408edSchristos * 163*f73a5f05Schristos * We don't tweak it that way ourselves because, if you're building 164*f73a5f05Schristos * and installing libpcap on macOS yourself, the APIs will be available 165748408edSchristos * no matter what OS version you're installing it on. 166748408edSchristos * 167748408edSchristos * For other platforms, we don't define them, leaving it up to 168748408edSchristos * others to do so based on their OS versions, if appropriate. 169748408edSchristos * 170748408edSchristos * We start with libpcap 0.4, as that was the last LBL release, and 171748408edSchristos * I've never seen earlier releases. 172748408edSchristos */ 173748408edSchristos #ifdef __APPLE__ 174748408edSchristos /* 175*f73a5f05Schristos * Apple - insert #include <os/availability.h> here, and replace the two 176*f73a5f05Schristos * #defines below with: 177748408edSchristos * 178*f73a5f05Schristos * #define PCAP_API_AVAILABLE API_AVAILABLE 179*f73a5f05Schristos * 180*f73a5f05Schristos * and adjust availabilities as necessary, including adding information 181*f73a5f05Schristos * about operating systems other than macOS. 182748408edSchristos */ 183*f73a5f05Schristos #define PCAP_API_AVAILABLE(...) 184*f73a5f05Schristos #define PCAP_AVAILABLE_0_4 PCAP_API_AVAILABLE(macos(10.0)) 185*f73a5f05Schristos #define PCAP_AVAILABLE_0_5 PCAP_API_AVAILABLE(macos(10.0)) 186*f73a5f05Schristos #define PCAP_AVAILABLE_0_6 PCAP_API_AVAILABLE(macos(10.1)) 187*f73a5f05Schristos #define PCAP_AVAILABLE_0_7 PCAP_API_AVAILABLE(macos(10.4)) 188*f73a5f05Schristos #define PCAP_AVAILABLE_0_8 PCAP_API_AVAILABLE(macos(10.4)) 189*f73a5f05Schristos #define PCAP_AVAILABLE_0_9 PCAP_API_AVAILABLE(macos(10.5)) 190*f73a5f05Schristos #define PCAP_AVAILABLE_1_0 PCAP_API_AVAILABLE(macos(10.6)) 191748408edSchristos /* #define PCAP_AVAILABLE_1_1 no routines added to the API */ 192*f73a5f05Schristos #define PCAP_AVAILABLE_1_2 PCAP_API_AVAILABLE(macos(10.9)) 193748408edSchristos /* #define PCAP_AVAILABLE_1_3 no routines added to the API */ 194748408edSchristos /* #define PCAP_AVAILABLE_1_4 no routines added to the API */ 195*f73a5f05Schristos #define PCAP_AVAILABLE_1_5 PCAP_API_AVAILABLE(macos(10.10)) 196748408edSchristos /* #define PCAP_AVAILABLE_1_6 no routines added to the API */ 197*f73a5f05Schristos #define PCAP_AVAILABLE_1_7 PCAP_API_AVAILABLE(macos(10.12)) 198*f73a5f05Schristos #define PCAP_AVAILABLE_1_8 PCAP_API_AVAILABLE(macos(10.13)) 199*f73a5f05Schristos #define PCAP_AVAILABLE_1_9 PCAP_API_AVAILABLE(macos(10.13)) 200*f73a5f05Schristos /* 201*f73a5f05Schristos * The remote capture APIs are, in 1.9 and 1.10, usually only 202*f73a5f05Schristos * available in the library if the library was built with 203*f73a5f05Schristos * remote capture enabled. 204*f73a5f05Schristos * 205*f73a5f05Schristos * However, macOS Sonoma provides stub versions of those routine, 206*f73a5f05Schristos * which return an error. This means that we need a separate 207*f73a5f05Schristos * availability indicator macro for those routines, so that 208*f73a5f05Schristos * progras built on macOS Sonoma that attempt to use weak 209*f73a5f05Schristos * importing and availability tests to use those routines 210*f73a5f05Schristos * if they're available will get those routines weakly imported, 211*f73a5f05Schristos * so that if they're run on releases prior to Sonoma, they 212*f73a5f05Schristos * won't get an error from dyld about those routines being 213*f73a5f05Schristos * missing in libpcap. (If they don't use run-time availability 214*f73a5f05Schristos * tests, they will, instead, get crashes if they call one of 215*f73a5f05Schristos * those routines, as the addresses of those routines will be 216*f73a5f05Schristos * set to 0 by dyld, meaning the program will dereference a 217*f73a5f05Schristos * null pointer and crash when trying to call them.) 218*f73a5f05Schristos * 219*f73a5f05Schristos * (Not that it's useful to use those routines *anyway*, as they're 220*f73a5f05Schristos * stubs that always fail. The stubs were necessary in order to 221*f73a5f05Schristos * support weak exporting at all.) 222*f73a5f05Schristos */ 223*f73a5f05Schristos #define PCAP_AVAILABLE_1_9_REMOTE PCAP_API_AVAILABLE(macos(14.0)) 224*f73a5f05Schristos #define PCAP_AVAILABLE_1_10 PCAP_API_AVAILABLE(macos(12.1)) 225*f73a5f05Schristos #define PCAP_AVAILABLE_1_10_REMOTE PCAP_API_AVAILABLE(macos(14.0)) 226748408edSchristos #define PCAP_AVAILABLE_1_11 /* not released yet, so not in macOS yet */ 227748408edSchristos #else /* __APPLE__ */ 228748408edSchristos #define PCAP_AVAILABLE_0_4 229748408edSchristos #define PCAP_AVAILABLE_0_5 230748408edSchristos #define PCAP_AVAILABLE_0_6 231748408edSchristos #define PCAP_AVAILABLE_0_7 232748408edSchristos #define PCAP_AVAILABLE_0_8 233748408edSchristos #define PCAP_AVAILABLE_0_9 234748408edSchristos #define PCAP_AVAILABLE_1_0 235748408edSchristos /* #define PCAP_AVAILABLE_1_1 no routines added to the API */ 236748408edSchristos #define PCAP_AVAILABLE_1_2 237748408edSchristos /* #define PCAP_AVAILABLE_1_3 no routines added to the API */ 238748408edSchristos /* #define PCAP_AVAILABLE_1_4 no routines added to the API */ 239748408edSchristos #define PCAP_AVAILABLE_1_5 240748408edSchristos /* #define PCAP_AVAILABLE_1_6 no routines added to the API */ 241748408edSchristos #define PCAP_AVAILABLE_1_7 242748408edSchristos #define PCAP_AVAILABLE_1_8 243748408edSchristos #define PCAP_AVAILABLE_1_9 244*f73a5f05Schristos #define PCAP_AVAILABLE_1_9_REMOTE 245748408edSchristos #define PCAP_AVAILABLE_1_10 246*f73a5f05Schristos #define PCAP_AVAILABLE_1_10_REMOTE 247748408edSchristos #define PCAP_AVAILABLE_1_11 248748408edSchristos #endif /* __APPLE__ */ 249748408edSchristos 250748408edSchristos /* 2519185e895Schristos * PCAP_NORETURN, before a function declaration, means "this function 2529185e895Schristos * never returns". (It must go before the function declaration, e.g. 2539185e895Schristos * "extern PCAP_NORETURN func(...)" rather than after the function 2549185e895Schristos * declaration, as the MSVC version has to go before the declaration.) 2559185e895Schristos * 2569185e895Schristos * PCAP_NORETURN_DEF, before a function *definition*, means "this 2579185e895Schristos * function never returns"; it would be used only for static functions 2589185e895Schristos * that are defined before any use, and thus have no declaration. 2599185e895Schristos * (MSVC doesn't support that; I guess the "decl" in "__declspec" 2609185e895Schristos * means "declaration", and __declspec doesn't work with definitions.) 2619185e895Schristos */ 2629185e895Schristos #if __has_attribute(noreturn) \ 2639185e895Schristos || PCAP_IS_AT_LEAST_GNUC_VERSION(2,5) \ 2649185e895Schristos || PCAP_IS_AT_LEAST_SUNC_VERSION(5,9) \ 265*f73a5f05Schristos || PCAP_IS_AT_LEAST_XL_C_VERSION(7,0) \ 266*f73a5f05Schristos || PCAP_IS_AT_LEAST_HP_C_VERSION(6,10) \ 267*f73a5f05Schristos || __TINYC__ 2689185e895Schristos /* 269748408edSchristos * Compiler with support for __attribute((noreturn)), or GCC 2.5 and 270748408edSchristos * later, or some compiler asserting compatibility with GCC 2.5 and 271*f73a5f05Schristos * later, or Solaris Studio 12 (Sun C 5.9) and later, or IBM XL C 7.0 272748408edSchristos * and later (do any earlier versions of XL C support this?), or HP aCC 273*f73a5f05Schristos * A.06.10 and later, or current TinyCC. 2749185e895Schristos */ 2759185e895Schristos #define PCAP_NORETURN __attribute((noreturn)) 2769185e895Schristos #define PCAP_NORETURN_DEF __attribute((noreturn)) 2779185e895Schristos #elif defined(_MSC_VER) 2789185e895Schristos /* 2799185e895Schristos * MSVC. 2809185e895Schristos */ 2819185e895Schristos #define PCAP_NORETURN __declspec(noreturn) 2829185e895Schristos #define PCAP_NORETURN_DEF 2839185e895Schristos #else 2849185e895Schristos #define PCAP_NORETURN 2859185e895Schristos #define PCAP_NORETURN_DEF 2869185e895Schristos #endif 2879185e895Schristos 2889185e895Schristos /* 2899185e895Schristos * PCAP_PRINTFLIKE(x,y), after a function declaration, means "this function 2909185e895Schristos * does printf-style formatting, with the xth argument being the format 2919185e895Schristos * string and the yth argument being the first argument for the format 2929185e895Schristos * string". 2939185e895Schristos */ 2949185e895Schristos #if __has_attribute(__format__) \ 2959185e895Schristos || PCAP_IS_AT_LEAST_GNUC_VERSION(2,3) \ 296*f73a5f05Schristos || PCAP_IS_AT_LEAST_XL_C_VERSION(7,0) \ 2979185e895Schristos || PCAP_IS_AT_LEAST_HP_C_VERSION(6,10) 2989185e895Schristos /* 299748408edSchristos * Compiler with support for it, or GCC 2.3 and later, or some compiler 300*f73a5f05Schristos * asserting compatibility with GCC 2.3 and later, or IBM XL C 7.0 3019185e895Schristos * and later (do any earlier versions of XL C support this?), 3029185e895Schristos * or HP aCC A.06.10 and later. 3039185e895Schristos */ 3049185e895Schristos #define PCAP_PRINTFLIKE(x,y) __attribute__((__format__(__printf__,x,y))) 3059185e895Schristos #else 3069185e895Schristos #define PCAP_PRINTFLIKE(x,y) 3079185e895Schristos #endif 3089185e895Schristos 3099185e895Schristos /* 3109185e895Schristos * PCAP_DEPRECATED(func, msg), after a function declaration, marks the 3119185e895Schristos * function as deprecated. 3129185e895Schristos * 313748408edSchristos * The argument is a string giving the warning message to use if the 314748408edSchristos * compiler supports that. 3159185e895Schristos */ 3169185e895Schristos #if __has_attribute(deprecated) \ 3179185e895Schristos || PCAP_IS_AT_LEAST_GNUC_VERSION(4,5) \ 3189185e895Schristos || PCAP_IS_AT_LEAST_SUNC_VERSION(5,13) 3199185e895Schristos /* 3209185e895Schristos * Compiler that supports __has_attribute and __attribute__((deprecated)), 321748408edSchristos * or GCC 4.5 and later, or Sun/Oracle C 12.4 (Sun C 5.13) and later. 3229185e895Schristos * 3239185e895Schristos * Those support __attribute__((deprecated(msg))) (we assume, perhaps 3249185e895Schristos * incorrectly, that anything that supports __has_attribute() is 3259185e895Schristos * recent enough to support __attribute__((deprecated(msg)))). 3269185e895Schristos */ 327748408edSchristos #define PCAP_DEPRECATED(msg) __attribute__((deprecated(msg))) 3289185e895Schristos #elif PCAP_IS_AT_LEAST_GNUC_VERSION(3,1) 3299185e895Schristos /* 3309185e895Schristos * GCC 3.1 through 4.4. 3319185e895Schristos * 3329185e895Schristos * Those support __attribute__((deprecated)) but not 3339185e895Schristos * __attribute__((deprecated(msg))). 3349185e895Schristos */ 335748408edSchristos #define PCAP_DEPRECATED(msg) __attribute__((deprecated)) 336748408edSchristos #elif defined(_MSC_VER) && !defined(BUILDING_PCAP) 3379185e895Schristos /* 338748408edSchristos * MSVC, and we're not building libpcap itself; it's VS 2015 339748408edSchristos * and later, so we have __declspec(deprecated(...)). 3409185e895Schristos * 3419185e895Schristos * If we *are* building libpcap, we don't want this, as it'll warn 3429185e895Schristos * us even if we *define* the function. 3439185e895Schristos */ 344748408edSchristos #define PCAP_DEPRECATED(msg) _declspec(deprecated(msg)) 3459185e895Schristos #else 346748408edSchristos #define PCAP_DEPRECATED(msg) 3479185e895Schristos #endif 3489185e895Schristos 3499185e895Schristos /* 3509185e895Schristos * For flagging arguments as format strings in MSVC. 3519185e895Schristos */ 3529185e895Schristos #ifdef _MSC_VER 3539185e895Schristos #include <sal.h> 3549185e895Schristos #define PCAP_FORMAT_STRING(p) _Printf_format_string_ p 3559185e895Schristos #else 3569185e895Schristos #define PCAP_FORMAT_STRING(p) p 3579185e895Schristos #endif 3589185e895Schristos 3599185e895Schristos #endif /* lib_pcap_funcattrs_h */ 360