xref: /dflybsd-src/contrib/libpcap/pcap/funcattrs.h (revision e75ef36f1332e115895388cede9dfd24ca1a806c)
13a289941SAaron LI /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
23a289941SAaron LI /*
33a289941SAaron LI  * Copyright (c) 1993, 1994, 1995, 1996, 1997
43a289941SAaron LI  *	The Regents of the University of California.  All rights reserved.
53a289941SAaron LI  *
63a289941SAaron LI  * Redistribution and use in source and binary forms, with or without
73a289941SAaron LI  * modification, are permitted provided that the following conditions
83a289941SAaron LI  * are met:
93a289941SAaron LI  * 1. Redistributions of source code must retain the above copyright
103a289941SAaron LI  *    notice, this list of conditions and the following disclaimer.
113a289941SAaron LI  * 2. Redistributions in binary form must reproduce the above copyright
123a289941SAaron LI  *    notice, this list of conditions and the following disclaimer in the
133a289941SAaron LI  *    documentation and/or other materials provided with the distribution.
143a289941SAaron LI  * 3. All advertising materials mentioning features or use of this software
153a289941SAaron LI  *    must display the following acknowledgement:
163a289941SAaron LI  *	This product includes software developed by the Computer Systems
173a289941SAaron LI  *	Engineering Group at Lawrence Berkeley Laboratory.
183a289941SAaron LI  * 4. Neither the name of the University nor of the Laboratory may be used
193a289941SAaron LI  *    to endorse or promote products derived from this software without
203a289941SAaron LI  *    specific prior written permission.
213a289941SAaron LI  *
223a289941SAaron LI  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
233a289941SAaron LI  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
243a289941SAaron LI  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
253a289941SAaron LI  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
263a289941SAaron LI  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
273a289941SAaron LI  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
283a289941SAaron LI  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
293a289941SAaron LI  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
303a289941SAaron LI  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
313a289941SAaron LI  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
323a289941SAaron LI  * SUCH DAMAGE.
333a289941SAaron LI  */
343a289941SAaron LI 
353a289941SAaron LI #ifndef lib_pcap_funcattrs_h
363a289941SAaron LI #define lib_pcap_funcattrs_h
373a289941SAaron LI 
383a289941SAaron LI #include <pcap/compiler-tests.h>
393a289941SAaron LI 
403a289941SAaron LI /*
413a289941SAaron LI  * Attributes to apply to functions and their arguments, using various
423a289941SAaron LI  * compiler-specific extensions.
433a289941SAaron LI  */
443a289941SAaron LI 
453a289941SAaron LI /*
463a289941SAaron LI  * PCAP_API_DEF must be used when defining *data* exported from
473a289941SAaron LI  * libpcap.  It can be used when defining *functions* exported
483a289941SAaron LI  * from libpcap, but it doesn't have to be used there.  It
493a289941SAaron LI  * should not be used in declarations in headers.
503a289941SAaron LI  *
513a289941SAaron LI  * PCAP_API must be used when *declaring* data or functions
523a289941SAaron LI  * exported from libpcap; PCAP_API_DEF won't work on all platforms.
533a289941SAaron LI  */
543a289941SAaron LI 
553a289941SAaron LI #if defined(_WIN32)
563a289941SAaron LI   /*
573a289941SAaron LI    * For Windows:
583a289941SAaron LI    *
593a289941SAaron LI    *    when building libpcap:
603a289941SAaron LI    *
613a289941SAaron LI    *       if we're building it as a DLL, we have to declare API
623a289941SAaron LI    *       functions with __declspec(dllexport);
633a289941SAaron LI    *
643a289941SAaron LI    *       if we're building it as a static library, we don't want
653a289941SAaron LI    *       to do so.
663a289941SAaron LI    *
673a289941SAaron LI    *    when using libpcap:
683a289941SAaron LI    *
693a289941SAaron LI    *       if we're using the DLL, calls to its functions are a
703a289941SAaron LI    *       little more efficient if they're declared with
713a289941SAaron LI    *       __declspec(dllimport);
723a289941SAaron LI    *
733a289941SAaron LI    *       if we're not using the dll, we don't want to declare
743a289941SAaron LI    *       them that way.
753a289941SAaron LI    *
763a289941SAaron LI    * So:
773a289941SAaron LI    *
783a289941SAaron LI    *    if pcap_EXPORTS is defined, we define PCAP_API_DEF as
793a289941SAaron LI    *     __declspec(dllexport);
803a289941SAaron LI    *
813a289941SAaron LI    *    if PCAP_DLL is defined, we define PCAP_API_DEF as
823a289941SAaron LI    *    __declspec(dllimport);
833a289941SAaron LI    *
843a289941SAaron LI    *    otherwise, we define PCAP_API_DEF as nothing.
853a289941SAaron LI    */
863a289941SAaron LI   #if defined(pcap_EXPORTS)
873a289941SAaron LI     /*
883a289941SAaron LI      * We're compiling libpcap as a DLL, so we should export functions
893a289941SAaron LI      * in our API.
903a289941SAaron LI      */
913a289941SAaron LI     #define PCAP_API_DEF	__declspec(dllexport)
923a289941SAaron LI   #elif defined(PCAP_DLL)
933a289941SAaron LI     /*
943a289941SAaron LI      * We're using libpcap as a DLL, so the calls will be a little more
953a289941SAaron LI      * efficient if we explicitly import the functions.
963a289941SAaron LI      */
973a289941SAaron LI     #define PCAP_API_DEF	__declspec(dllimport)
983a289941SAaron LI   #else
993a289941SAaron LI     /*
1003a289941SAaron LI      * Either we're building libpcap as a static library, or we're using
1013a289941SAaron LI      * it as a static library, or we don't know for certain that we're
1023a289941SAaron LI      * using it as a dynamic library, so neither import nor export the
1033a289941SAaron LI      * functions explicitly.
1043a289941SAaron LI      */
1053a289941SAaron LI     #define PCAP_API_DEF
1063a289941SAaron LI   #endif
1073a289941SAaron LI #elif defined(MSDOS)
1083a289941SAaron LI   /* XXX - does this need special treatment? */
1093a289941SAaron LI   #define PCAP_API_DEF
1103a289941SAaron LI #else /* UN*X */
1113a289941SAaron LI   #ifdef pcap_EXPORTS
1123a289941SAaron LI     /*
1133a289941SAaron LI      * We're compiling libpcap as a (dynamic) shared library, so we should
1143a289941SAaron LI      * export functions in our API.  The compiler might be configured not
1153a289941SAaron LI      * to export functions from a shared library by default, so we might
1163a289941SAaron LI      * have to explicitly mark functions as exported.
1173a289941SAaron LI      */
1183a289941SAaron LI     #if PCAP_IS_AT_LEAST_GNUC_VERSION(3,4) \
1193a289941SAaron LI         || PCAP_IS_AT_LEAST_XL_C_VERSION(12,0)
1203a289941SAaron LI       /*
1213a289941SAaron LI        * GCC 3.4 or later, or some compiler asserting compatibility with
1223a289941SAaron LI        * GCC 3.4 or later, or XL C 13.0 or later, so we have
1233a289941SAaron LI        * __attribute__((visibility()).
1243a289941SAaron LI        */
1253a289941SAaron LI       #define PCAP_API_DEF	__attribute__((visibility("default")))
1263a289941SAaron LI     #elif PCAP_IS_AT_LEAST_SUNC_VERSION(5,5)
1273a289941SAaron LI       /*
1283a289941SAaron LI        * Sun C 5.5 or later, so we have __global.
1293a289941SAaron LI        * (Sun C 5.9 and later also have __attribute__((visibility()),
1303a289941SAaron LI        * but there's no reason to prefer it with Sun C.)
1313a289941SAaron LI        */
1323a289941SAaron LI       #define PCAP_API_DEF	__global
1333a289941SAaron LI     #else
1343a289941SAaron LI       /*
1353a289941SAaron LI        * We don't have anything to say.
1363a289941SAaron LI        */
1373a289941SAaron LI       #define PCAP_API_DEF
1383a289941SAaron LI     #endif
1393a289941SAaron LI   #else
1403a289941SAaron LI     /*
1413a289941SAaron LI      * We're not building libpcap.
1423a289941SAaron LI      */
1433a289941SAaron LI     #define PCAP_API_DEF
1443a289941SAaron LI   #endif
1453a289941SAaron LI #endif /* _WIN32/MSDOS/UN*X */
1463a289941SAaron LI 
1473a289941SAaron LI #define PCAP_API	PCAP_API_DEF extern
1483a289941SAaron LI 
1493a289941SAaron LI /*
150*ea16f64eSAntonio Huete Jimenez  * Definitions to 1) indicate what version of libpcap first had a given
151*ea16f64eSAntonio Huete Jimenez  * API and 2) allow upstream providers whose build environments allow
152*ea16f64eSAntonio Huete Jimenez  * APIs to be designated as "first available in this release" to do so
153*ea16f64eSAntonio Huete Jimenez  * by appropriately defining them.
154*ea16f64eSAntonio Huete Jimenez  *
155*ea16f64eSAntonio Huete Jimenez  * Yes, that's you, Apple. :-)  Please define PCAP_AVAILABLE_MACOS()
156*ea16f64eSAntonio Huete Jimenez  * as necessary to make various APIs "weak exports" to make it easier
157*ea16f64eSAntonio Huete Jimenez  * for software that's distributed in binary form and that uses libpcap
158*ea16f64eSAntonio Huete Jimenez  * to run on multiple macOS versions and use new APIs when available.
159*ea16f64eSAntonio Huete Jimenez  * (Yes, such third-party software exists - Wireshark provides binary
160*ea16f64eSAntonio Huete Jimenez  * packages for macOS, for example.  tcpdump doesn't count, as that's
161*ea16f64eSAntonio Huete Jimenez  * provided by Apple, so each release can come with a version compiled
162*ea16f64eSAntonio Huete Jimenez  * to use the APIs present in that release.)
163*ea16f64eSAntonio Huete Jimenez  *
164*ea16f64eSAntonio Huete Jimenez  * We don't define it ourselves because, if you're building and
165*ea16f64eSAntonio Huete Jimenez  * installing libpcap on macOS yourself, the APIs will be available
166*ea16f64eSAntonio Huete Jimenez  * no matter what OS version you're installing it on.
167*ea16f64eSAntonio Huete Jimenez  *
168*ea16f64eSAntonio Huete Jimenez  * For other platforms, we don't define them, leaving it up to
169*ea16f64eSAntonio Huete Jimenez  * others to do so based on their OS versions, if appropriate.
170*ea16f64eSAntonio Huete Jimenez  *
171*ea16f64eSAntonio Huete Jimenez  * We start with libpcap 0.4, as that was the last LBL release, and
172*ea16f64eSAntonio Huete Jimenez  * I've never seen earlier releases.
173*ea16f64eSAntonio Huete Jimenez  */
174*ea16f64eSAntonio Huete Jimenez #ifdef __APPLE__
175*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_MACOS(v)	/* define to say "first appears in v" */
176*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_0_4	PCAP_AVAILABLE_MACOS(10.0) /* Did any version of Mac OS X ship with this? */
177*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_0_5	PCAP_AVAILABLE_MACOS(10.0) /* Did any version of Mac OS X ship with this? */
178*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_0_6	PCAP_AVAILABLE_MACOS(10.1)
179*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_0_7	PCAP_AVAILABLE_MACOS(10.4)
180*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_0_8	PCAP_AVAILABLE_MACOS(10.4)
181*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_0_9	PCAP_AVAILABLE_MACOS(10.5)
182*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_1_0	PCAP_AVAILABLE_MACOS(10.6)
183*ea16f64eSAntonio Huete Jimenez /* #define PCAP_AVAILABLE_1_1	no routines added to the API */
184*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_1_2	PCAP_AVAILABLE_MACOS(10.9)
185*ea16f64eSAntonio Huete Jimenez /* #define PCAP_AVAILABLE_1_3	no routines added to the API */
186*ea16f64eSAntonio Huete Jimenez /* #define PCAP_AVAILABLE_1_4	no routines added to the API */
187*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_1_5	PCAP_AVAILABLE_MACOS(10.10)
188*ea16f64eSAntonio Huete Jimenez /* #define PCAP_AVAILABLE_1_6	no routines added to the API */
189*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_1_7	PCAP_AVAILABLE_MACOS(10.12)
190*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_1_8	PCAP_AVAILABLE_MACOS(10.13) /* only Windows adds routines to the API; XXX - what version first had it? */
191*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_1_9	PCAP_AVAILABLE_MACOS(10.13)
192*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_1_10	/* not in macOS yet */
193*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_1_11	/* not released yet, so not in macOS yet */
194*ea16f64eSAntonio Huete Jimenez #else /* __APPLE__ */
195*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_0_4
196*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_0_5
197*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_0_6
198*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_0_7
199*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_0_8
200*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_0_9
201*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_1_0
202*ea16f64eSAntonio Huete Jimenez /* #define PCAP_AVAILABLE_1_1	no routines added to the API */
203*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_1_2
204*ea16f64eSAntonio Huete Jimenez /* #define PCAP_AVAILABLE_1_3	no routines added to the API */
205*ea16f64eSAntonio Huete Jimenez /* #define PCAP_AVAILABLE_1_4	no routines added to the API */
206*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_1_5
207*ea16f64eSAntonio Huete Jimenez /* #define PCAP_AVAILABLE_1_6	no routines added to the API */
208*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_1_7
209*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_1_8
210*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_1_9
211*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_1_10
212*ea16f64eSAntonio Huete Jimenez #define PCAP_AVAILABLE_1_11
213*ea16f64eSAntonio Huete Jimenez #endif /* __APPLE__ */
214*ea16f64eSAntonio Huete Jimenez 
215*ea16f64eSAntonio Huete Jimenez /*
2163a289941SAaron LI  * PCAP_NORETURN, before a function declaration, means "this function
2173a289941SAaron LI  * never returns".  (It must go before the function declaration, e.g.
2183a289941SAaron LI  * "extern PCAP_NORETURN func(...)" rather than after the function
2193a289941SAaron LI  * declaration, as the MSVC version has to go before the declaration.)
2203a289941SAaron LI  *
2213a289941SAaron LI  * PCAP_NORETURN_DEF, before a function *definition*, means "this
2223a289941SAaron LI  * function never returns"; it would be used only for static functions
2233a289941SAaron LI  * that are defined before any use, and thus have no declaration.
2243a289941SAaron LI  * (MSVC doesn't support that; I guess the "decl" in "__declspec"
2253a289941SAaron LI  * means "declaration", and __declspec doesn't work with definitions.)
2263a289941SAaron LI  */
2273a289941SAaron LI #if __has_attribute(noreturn) \
2283a289941SAaron LI     || PCAP_IS_AT_LEAST_GNUC_VERSION(2,5) \
2293a289941SAaron LI     || PCAP_IS_AT_LEAST_SUNC_VERSION(5,9) \
2303a289941SAaron LI     || PCAP_IS_AT_LEAST_XL_C_VERSION(10,1) \
2313a289941SAaron LI     || PCAP_IS_AT_LEAST_HP_C_VERSION(6,10)
2323a289941SAaron LI   /*
2333a289941SAaron LI    * Compiler with support for __attribute((noreturn)), or GCC 2.5 or
2343a289941SAaron LI    * later, or some compiler asserting compatibility with GCC 2.5 or
2353a289941SAaron LI    * later, or Solaris Studio 12 (Sun C 5.9) or later, or IBM XL C 10.1
2363a289941SAaron LI    * or later (do any earlier versions of XL C support this?), or HP aCC
2373a289941SAaron LI    * A.06.10 or later.
2383a289941SAaron LI    */
2393a289941SAaron LI   #define PCAP_NORETURN __attribute((noreturn))
2403a289941SAaron LI   #define PCAP_NORETURN_DEF __attribute((noreturn))
2413a289941SAaron LI #elif defined(_MSC_VER)
2423a289941SAaron LI   /*
2433a289941SAaron LI    * MSVC.
2443a289941SAaron LI    */
2453a289941SAaron LI   #define PCAP_NORETURN __declspec(noreturn)
2463a289941SAaron LI   #define PCAP_NORETURN_DEF
2473a289941SAaron LI #else
2483a289941SAaron LI   #define PCAP_NORETURN
2493a289941SAaron LI   #define PCAP_NORETURN_DEF
2503a289941SAaron LI #endif
2513a289941SAaron LI 
2523a289941SAaron LI /*
2533a289941SAaron LI  * PCAP_PRINTFLIKE(x,y), after a function declaration, means "this function
2543a289941SAaron LI  * does printf-style formatting, with the xth argument being the format
2553a289941SAaron LI  * string and the yth argument being the first argument for the format
2563a289941SAaron LI  * string".
2573a289941SAaron LI  */
2583a289941SAaron LI #if __has_attribute(__format__) \
2593a289941SAaron LI     || PCAP_IS_AT_LEAST_GNUC_VERSION(2,3) \
2603a289941SAaron LI     || PCAP_IS_AT_LEAST_XL_C_VERSION(10,1) \
2613a289941SAaron LI     || PCAP_IS_AT_LEAST_HP_C_VERSION(6,10)
2623a289941SAaron LI   /*
2633a289941SAaron LI    * Compiler with support for it, or GCC 2.3 or later, or some compiler
2643a289941SAaron LI    * asserting compatibility with GCC 2.3 or later, or IBM XL C 10.1
2653a289941SAaron LI    * and later (do any earlier versions of XL C support this?),
2663a289941SAaron LI    * or HP aCC A.06.10 and later.
2673a289941SAaron LI    */
2683a289941SAaron LI   #define PCAP_PRINTFLIKE(x,y) __attribute__((__format__(__printf__,x,y)))
2693a289941SAaron LI #else
2703a289941SAaron LI   #define PCAP_PRINTFLIKE(x,y)
2713a289941SAaron LI #endif
2723a289941SAaron LI 
2733a289941SAaron LI /*
2743a289941SAaron LI  * PCAP_DEPRECATED(func, msg), after a function declaration, marks the
2753a289941SAaron LI  * function as deprecated.
2763a289941SAaron LI  *
2773a289941SAaron LI  * The first argument is the name of the function; the second argument is
2783a289941SAaron LI  * a string giving the warning message to use if the compiler supports that.
2793a289941SAaron LI  *
2803a289941SAaron LI  * (Thank you, Microsoft, for requiring the function name.)
2813a289941SAaron LI  */
2823a289941SAaron LI #if __has_attribute(deprecated) \
2833a289941SAaron LI     || PCAP_IS_AT_LEAST_GNUC_VERSION(4,5) \
2843a289941SAaron LI     || PCAP_IS_AT_LEAST_SUNC_VERSION(5,13)
2853a289941SAaron LI   /*
2863a289941SAaron LI    * Compiler that supports __has_attribute and __attribute__((deprecated)),
2873a289941SAaron LI    * or GCC 4.5 or later, or Sun/Oracle C 12.4 (Sun C 5.13) or later.
2883a289941SAaron LI    *
2893a289941SAaron LI    * Those support __attribute__((deprecated(msg))) (we assume, perhaps
2903a289941SAaron LI    * incorrectly, that anything that supports __has_attribute() is
2913a289941SAaron LI    * recent enough to support __attribute__((deprecated(msg)))).
2923a289941SAaron LI    */
2933a289941SAaron LI   #define PCAP_DEPRECATED(func, msg)	__attribute__((deprecated(msg)))
2943a289941SAaron LI #elif PCAP_IS_AT_LEAST_GNUC_VERSION(3,1)
2953a289941SAaron LI   /*
2963a289941SAaron LI    * GCC 3.1 through 4.4.
2973a289941SAaron LI    *
2983a289941SAaron LI    * Those support __attribute__((deprecated)) but not
2993a289941SAaron LI    * __attribute__((deprecated(msg))).
3003a289941SAaron LI    */
3013a289941SAaron LI   #define PCAP_DEPRECATED(func, msg)	__attribute__((deprecated))
302*ea16f64eSAntonio Huete Jimenez #elif defined(_MSC_VER) && !defined(BUILDING_PCAP)
3033a289941SAaron LI   /*
304*ea16f64eSAntonio Huete Jimenez    * MSVC, and we're not building libpcap itself; it's VS 2015
305*ea16f64eSAntonio Huete Jimenez    * or later, so we have the deprecated pragma.
3063a289941SAaron LI    *
3073a289941SAaron LI    * If we *are* building libpcap, we don't want this, as it'll warn
3083a289941SAaron LI    * us even if we *define* the function.
3093a289941SAaron LI    */
3103a289941SAaron LI   #define PCAP_DEPRECATED(func, msg)	__pragma(deprecated(func))
3113a289941SAaron LI #else
3123a289941SAaron LI   #define PCAP_DEPRECATED(func, msg)
3133a289941SAaron LI #endif
3143a289941SAaron LI 
3153a289941SAaron LI /*
3163a289941SAaron LI  * For flagging arguments as format strings in MSVC.
3173a289941SAaron LI  */
3183a289941SAaron LI #ifdef _MSC_VER
3193a289941SAaron LI  #include <sal.h>
3203a289941SAaron LI  #define PCAP_FORMAT_STRING(p) _Printf_format_string_ p
3213a289941SAaron LI #else
3223a289941SAaron LI  #define PCAP_FORMAT_STRING(p) p
3233a289941SAaron LI #endif
3243a289941SAaron LI 
3253a289941SAaron LI #endif /* lib_pcap_funcattrs_h */
326