xref: /freebsd/contrib/libpcap/pcap/funcattrs.h (revision 6f9cba8f)
1 /* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
2 /*
3  * Copyright (c) 1993, 1994, 1995, 1996, 1997
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by the Computer Systems
17  *	Engineering Group at Lawrence Berkeley Laboratory.
18  * 4. Neither the name of the University nor of the Laboratory may be used
19  *    to endorse or promote products derived from this software without
20  *    specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #ifndef lib_pcap_funcattrs_h
36 #define lib_pcap_funcattrs_h
37 
38 #include <pcap/compiler-tests.h>
39 
40 /*
41  * Attributes to apply to functions and their arguments, using various
42  * compiler-specific extensions.
43  */
44 
45 /*
46  * PCAP_API_DEF must be used when defining *data* exported from
47  * libpcap.  It can be used when defining *functions* exported
48  * from libpcap, but it doesn't have to be used there.  It
49  * should not be used in declarations in headers.
50  *
51  * PCAP_API must be used when *declaring* data or functions
52  * exported from libpcap; PCAP_API_DEF won't work on all platforms.
53  */
54 
55 #if defined(_WIN32)
56   /*
57    * For Windows:
58    *
59    *    when building libpcap:
60    *
61    *       if we're building it as a DLL, we have to declare API
62    *       functions with __declspec(dllexport);
63    *
64    *       if we're building it as a static library, we don't want
65    *       to do so.
66    *
67    *    when using libpcap:
68    *
69    *       if we're using the DLL, calls to its functions are a
70    *       little more efficient if they're declared with
71    *       __declspec(dllimport);
72    *
73    *       if we're not using the dll, we don't want to declare
74    *       them that way.
75    *
76    * So:
77    *
78    *    if pcap_EXPORTS is defined, we define PCAP_API_DEF as
79    *     __declspec(dllexport);
80    *
81    *    if PCAP_DLL is defined, we define PCAP_API_DEF as
82    *    __declspec(dllimport);
83    *
84    *    otherwise, we define PCAP_API_DEF as nothing.
85    */
86   #if defined(pcap_EXPORTS)
87     /*
88      * We're compiling libpcap as a DLL, so we should export functions
89      * in our API.
90      */
91     #define PCAP_API_DEF	__declspec(dllexport)
92   #elif defined(PCAP_DLL)
93     /*
94      * We're using libpcap as a DLL, so the calls will be a little more
95      * efficient if we explicitly import the functions.
96      */
97     #define PCAP_API_DEF	__declspec(dllimport)
98   #else
99     /*
100      * Either we're building libpcap as a static library, or we're using
101      * it as a static library, or we don't know for certain that we're
102      * using it as a dynamic library, so neither import nor export the
103      * functions explicitly.
104      */
105     #define PCAP_API_DEF
106   #endif
107 #elif defined(MSDOS)
108   /* XXX - does this need special treatment? */
109   #define PCAP_API_DEF
110 #else /* UN*X */
111   #ifdef pcap_EXPORTS
112     /*
113      * We're compiling libpcap as a (dynamic) shared library, so we should
114      * export functions in our API.  The compiler might be configured not
115      * to export functions from a shared library by default, so we might
116      * have to explicitly mark functions as exported.
117      */
118     #if PCAP_IS_AT_LEAST_GNUC_VERSION(3,4) \
119         || PCAP_IS_AT_LEAST_XL_C_VERSION(12,0)
120       /*
121        * GCC 3.4 and later, or some compiler asserting compatibility with
122        * GCC 3.4 and later, or XL C 13.0 and later, so we have
123        * __attribute__((visibility()).
124        */
125       #define PCAP_API_DEF	__attribute__((visibility("default")))
126     #elif PCAP_IS_AT_LEAST_SUNC_VERSION(5,5)
127       /*
128        * Sun C 5.5 and later, so we have __global.
129        * (Sun C 5.9 and later also have __attribute__((visibility()),
130        * but there's no reason to prefer it with Sun C.)
131        */
132       #define PCAP_API_DEF	__global
133     #else
134       /*
135        * We don't have anything to say.
136        */
137       #define PCAP_API_DEF
138     #endif
139   #else
140     /*
141      * We're not building libpcap.
142      */
143     #define PCAP_API_DEF
144   #endif
145 #endif /* _WIN32/MSDOS/UN*X */
146 
147 #define PCAP_API	PCAP_API_DEF extern
148 
149 /*
150  * Definitions to 1) indicate what version of libpcap first had a given
151  * API and 2) allow upstream providers whose build environments allow
152  * APIs to be designated as "first available in this release" to do so
153  * by appropriately defining them.
154  *
155  * Yes, that's you, Apple. :-)  Please define PCAP_AVAILABLE_MACOS()
156  * as necessary to make various APIs "weak exports" to make it easier
157  * for software that's distributed in binary form and that uses libpcap
158  * to run on multiple macOS versions and use new APIs when available.
159  * (Yes, such third-party software exists - Wireshark provides binary
160  * packages for macOS, for example.  tcpdump doesn't count, as that's
161  * provided by Apple, so each release can come with a version compiled
162  * to use the APIs present in that release.)
163  *
164  * The non-macOS versioning is based on
165  *
166  *    https://en.wikipedia.org/wiki/Darwin_(operating_system)#Release_history
167  *
168  * If there are any corrections, please submit it upstream to the
169  * libpcap maintainers, preferably as a pull request on
170  *
171  *    https://github.com/the-tcpdump-group/libpcap
172  *
173  * We don't define it ourselves because, if you're building and
174  * installing libpcap on macOS yourself, the APIs will be available
175  * no matter what OS version you're installing it on.
176  *
177  * For other platforms, we don't define them, leaving it up to
178  * others to do so based on their OS versions, if appropriate.
179  *
180  * We start with libpcap 0.4, as that was the last LBL release, and
181  * I've never seen earlier releases.
182  */
183 #ifdef __APPLE__
184 #include <Availability.h>
185 /*
186  * When building as part of macOS, define this as __API_AVAILABLE(__VA_ARGS__).
187  *
188  * XXX - if there's some #define to indicate that this is being built
189  * as part of the macOS build process, we could make that Just Work.
190  */
191 #define PCAP_AVAILABLE(...)
192 #define PCAP_AVAILABLE_0_4	PCAP_AVAILABLE(macos(10.0)) /* Did any version of Mac OS X ship with this? */
193 #define PCAP_AVAILABLE_0_5	PCAP_AVAILABLE(macos(10.0)) /* Did any version of Mac OS X ship with this? */
194 #define PCAP_AVAILABLE_0_6	PCAP_AVAILABLE(macos(10.1))
195 #define PCAP_AVAILABLE_0_7	PCAP_AVAILABLE(macos(10.4))
196 #define PCAP_AVAILABLE_0_8	PCAP_AVAILABLE(macos(10.4))
197 #define PCAP_AVAILABLE_0_9	PCAP_AVAILABLE(macos(10.5), ios(1.0))
198 #define PCAP_AVAILABLE_1_0	PCAP_AVAILABLE(macos(10.6), ios(4.0))
199 /* #define PCAP_AVAILABLE_1_1	no routines added to the API */
200 #define PCAP_AVAILABLE_1_2	PCAP_AVAILABLE(macos(10.9), ios(6.0))
201 /* #define PCAP_AVAILABLE_1_3	no routines added to the API */
202 /* #define PCAP_AVAILABLE_1_4	no routines added to the API */
203 #define PCAP_AVAILABLE_1_5	PCAP_AVAILABLE(macos(10.10), ios(7.0), watchos(1.0))
204 /* #define PCAP_AVAILABLE_1_6	no routines added to the API */
205 #define PCAP_AVAILABLE_1_7	PCAP_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
206 #define PCAP_AVAILABLE_1_8	PCAP_AVAILABLE(macos(10.13), ios(11.0), tvos(11.0), watchos(4.0)) /* only Windows adds routines to the API; XXX - what version first had it? */
207 #define PCAP_AVAILABLE_1_9	PCAP_AVAILABLE(macos(10.13), ios(11.0), tvos(11.0), watchos(4.0))
208 #define PCAP_AVAILABLE_1_10	/* not in macOS yet */
209 #define PCAP_AVAILABLE_1_11	/* not released yet, so not in macOS yet */
210 #else /* __APPLE__ */
211 #define PCAP_AVAILABLE_0_4
212 #define PCAP_AVAILABLE_0_5
213 #define PCAP_AVAILABLE_0_6
214 #define PCAP_AVAILABLE_0_7
215 #define PCAP_AVAILABLE_0_8
216 #define PCAP_AVAILABLE_0_9
217 #define PCAP_AVAILABLE_1_0
218 /* #define PCAP_AVAILABLE_1_1	no routines added to the API */
219 #define PCAP_AVAILABLE_1_2
220 /* #define PCAP_AVAILABLE_1_3	no routines added to the API */
221 /* #define PCAP_AVAILABLE_1_4	no routines added to the API */
222 #define PCAP_AVAILABLE_1_5
223 /* #define PCAP_AVAILABLE_1_6	no routines added to the API */
224 #define PCAP_AVAILABLE_1_7
225 #define PCAP_AVAILABLE_1_8
226 #define PCAP_AVAILABLE_1_9
227 #define PCAP_AVAILABLE_1_10
228 #define PCAP_AVAILABLE_1_11
229 #endif /* __APPLE__ */
230 
231 /*
232  * PCAP_NORETURN, before a function declaration, means "this function
233  * never returns".  (It must go before the function declaration, e.g.
234  * "extern PCAP_NORETURN func(...)" rather than after the function
235  * declaration, as the MSVC version has to go before the declaration.)
236  *
237  * PCAP_NORETURN_DEF, before a function *definition*, means "this
238  * function never returns"; it would be used only for static functions
239  * that are defined before any use, and thus have no declaration.
240  * (MSVC doesn't support that; I guess the "decl" in "__declspec"
241  * means "declaration", and __declspec doesn't work with definitions.)
242  */
243 #if __has_attribute(noreturn) \
244     || PCAP_IS_AT_LEAST_GNUC_VERSION(2,5) \
245     || PCAP_IS_AT_LEAST_SUNC_VERSION(5,9) \
246     || PCAP_IS_AT_LEAST_XL_C_VERSION(10,1) \
247     || PCAP_IS_AT_LEAST_HP_C_VERSION(6,10)
248   /*
249    * Compiler with support for __attribute((noreturn)), or GCC 2.5 and
250    * later, or some compiler asserting compatibility with GCC 2.5 and
251    * later, or Solaris Studio 12 (Sun C 5.9) and later, or IBM XL C 10.1
252    * and later (do any earlier versions of XL C support this?), or HP aCC
253    * A.06.10 and later.
254    */
255   #define PCAP_NORETURN __attribute((noreturn))
256   #define PCAP_NORETURN_DEF __attribute((noreturn))
257 #elif defined(_MSC_VER)
258   /*
259    * MSVC.
260    */
261   #define PCAP_NORETURN __declspec(noreturn)
262   #define PCAP_NORETURN_DEF
263 #else
264   #define PCAP_NORETURN
265   #define PCAP_NORETURN_DEF
266 #endif
267 
268 /*
269  * PCAP_PRINTFLIKE(x,y), after a function declaration, means "this function
270  * does printf-style formatting, with the xth argument being the format
271  * string and the yth argument being the first argument for the format
272  * string".
273  */
274 #if __has_attribute(__format__) \
275     || PCAP_IS_AT_LEAST_GNUC_VERSION(2,3) \
276     || PCAP_IS_AT_LEAST_XL_C_VERSION(10,1) \
277     || PCAP_IS_AT_LEAST_HP_C_VERSION(6,10)
278   /*
279    * Compiler with support for it, or GCC 2.3 and later, or some compiler
280    * asserting compatibility with GCC 2.3 and later, or IBM XL C 10.1
281    * and later (do any earlier versions of XL C support this?),
282    * or HP aCC A.06.10 and later.
283    */
284   #define PCAP_PRINTFLIKE(x,y) __attribute__((__format__(__printf__,x,y)))
285 #else
286   #define PCAP_PRINTFLIKE(x,y)
287 #endif
288 
289 /*
290  * PCAP_DEPRECATED(func, msg), after a function declaration, marks the
291  * function as deprecated.
292  *
293  * The argument is a string giving the warning message to use if the
294  * compiler supports that.
295  */
296 #if __has_attribute(deprecated) \
297     || PCAP_IS_AT_LEAST_GNUC_VERSION(4,5) \
298     || PCAP_IS_AT_LEAST_SUNC_VERSION(5,13)
299   /*
300    * Compiler that supports __has_attribute and __attribute__((deprecated)),
301    * or GCC 4.5 and later, or Sun/Oracle C 12.4 (Sun C 5.13) and later.
302    *
303    * Those support __attribute__((deprecated(msg))) (we assume, perhaps
304    * incorrectly, that anything that supports __has_attribute() is
305    * recent enough to support __attribute__((deprecated(msg)))).
306    */
307   #define PCAP_DEPRECATED(msg)	__attribute__((deprecated(msg)))
308 #elif PCAP_IS_AT_LEAST_GNUC_VERSION(3,1)
309   /*
310    * GCC 3.1 through 4.4.
311    *
312    * Those support __attribute__((deprecated)) but not
313    * __attribute__((deprecated(msg))).
314    */
315   #define PCAP_DEPRECATED(msg)	__attribute__((deprecated))
316 #elif defined(_MSC_VER) && !defined(BUILDING_PCAP)
317   /*
318    * MSVC, and we're not building libpcap itself; it's VS 2015
319    * and later, so we have __declspec(deprecated(...)).
320    *
321    * If we *are* building libpcap, we don't want this, as it'll warn
322    * us even if we *define* the function.
323    */
324   #define PCAP_DEPRECATED(msg)	_declspec(deprecated(msg))
325 #else
326   #define PCAP_DEPRECATED(msg)
327 #endif
328 
329 /*
330  * For flagging arguments as format strings in MSVC.
331  */
332 #ifdef _MSC_VER
333  #include <sal.h>
334  #define PCAP_FORMAT_STRING(p) _Printf_format_string_ p
335 #else
336  #define PCAP_FORMAT_STRING(p) p
337 #endif
338 
339 #endif /* lib_pcap_funcattrs_h */
340