]> The Tcpdump Group git mirrors - libpcap/blob - pcap/funcattrs.h
CI: Call print_so_deps() on rpcapd in remote enabled build
[libpcap] / pcap / funcattrs.h
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 #else /* UN*X */
108 #ifdef pcap_EXPORTS
109 /*
110 * We're compiling libpcap as a (dynamic) shared library, so we should
111 * export functions in our API. The compiler might be configured not
112 * to export functions from a shared library by default, so we might
113 * have to explicitly mark functions as exported.
114 */
115 #if PCAP_IS_AT_LEAST_GNUC_VERSION(3,4) \
116 || PCAP_IS_AT_LEAST_XL_C_VERSION(12,0)
117 /*
118 * GCC 3.4 and later, or some compiler asserting compatibility with
119 * GCC 3.4 and later, or XL C 13.0 and later, so we have
120 * __attribute__((visibility()).
121 */
122 #define PCAP_API_DEF __attribute__((visibility("default")))
123 #elif PCAP_IS_AT_LEAST_SUNC_VERSION(5,5)
124 /*
125 * Sun C 5.5 and later, so we have __global.
126 * (Sun C 5.9 and later also have __attribute__((visibility()),
127 * but there's no reason to prefer it with Sun C.)
128 */
129 #define PCAP_API_DEF __global
130 #else
131 /*
132 * We don't have anything to say.
133 */
134 #define PCAP_API_DEF
135 #endif
136 #else
137 /*
138 * We're not building libpcap.
139 */
140 #define PCAP_API_DEF
141 #endif
142 #endif /* _WIN32/UN*X */
143
144 #define PCAP_API PCAP_API_DEF extern
145
146 /*
147 * Definitions to 1) indicate what version of libpcap first had a given
148 * API and 2) allow upstream providers whose build environments allow
149 * APIs to be designated as "first available in this release" to do so
150 * by appropriately defining them.
151 *
152 * On macOS, Apple can tweak this to make various APIs "weakly exported
153 * symbols" to make it easier for software that's distributed in binary
154 * form and that uses libpcap to run on multiple macOS versions and use
155 * new APIs when available. (Yes, such third-party software exists -
156 * Wireshark provides binary packages for macOS, for example. tcpdump
157 * doesn't count, as that's provided by Apple, so each release can
158 * come with a version compiled to use the APIs present in that release.)
159 *
160 * We don't tweak it that way ourselves because, if you're building
161 * and installing libpcap on macOS yourself, the APIs will be available
162 * no matter what OS version you're installing it on.
163 *
164 * For other platforms, we don't define them, leaving it up to
165 * others to do so based on their OS versions, if appropriate.
166 *
167 * We start with libpcap 0.4, as that was the last LBL release, and
168 * I've never seen earlier releases.
169 */
170 #ifdef __APPLE__
171 /*
172 * Apple - insert #include <os/availability.h> here, and replace the two
173 * #defines below with:
174 *
175 * #define PCAP_API_AVAILABLE API_AVAILABLE
176 *
177 * and adjust availabilities as necessary, including adding information
178 * about operating systems other than macOS.
179 */
180 #define PCAP_API_AVAILABLE(...)
181 #define PCAP_AVAILABLE_0_4 PCAP_API_AVAILABLE(macos(10.0))
182 #define PCAP_AVAILABLE_0_5 PCAP_API_AVAILABLE(macos(10.0))
183 #define PCAP_AVAILABLE_0_6 PCAP_API_AVAILABLE(macos(10.1))
184 #define PCAP_AVAILABLE_0_7 PCAP_API_AVAILABLE(macos(10.4))
185 #define PCAP_AVAILABLE_0_8 PCAP_API_AVAILABLE(macos(10.4))
186 #define PCAP_AVAILABLE_0_9 PCAP_API_AVAILABLE(macos(10.5))
187 #define PCAP_AVAILABLE_1_0 PCAP_API_AVAILABLE(macos(10.6))
188 /* #define PCAP_AVAILABLE_1_1 no routines added to the API */
189 #define PCAP_AVAILABLE_1_2 PCAP_API_AVAILABLE(macos(10.9))
190 /* #define PCAP_AVAILABLE_1_3 no routines added to the API */
191 /* #define PCAP_AVAILABLE_1_4 no routines added to the API */
192 #define PCAP_AVAILABLE_1_5 PCAP_API_AVAILABLE(macos(10.10))
193 /* #define PCAP_AVAILABLE_1_6 no routines added to the API */
194 #define PCAP_AVAILABLE_1_7 PCAP_API_AVAILABLE(macos(10.12))
195 #define PCAP_AVAILABLE_1_8 PCAP_API_AVAILABLE(macos(10.13))
196 #define PCAP_AVAILABLE_1_9 PCAP_API_AVAILABLE(macos(10.13))
197 /*
198 * The remote capture APIs are, in 1.9 and 1.10, usually only
199 * available in the library if the library was built with
200 * remote capture enabled.
201 *
202 * However, macOS Sonoma provides stub versions of those routine,
203 * which return an error. This means that we need a separate
204 * availability indicator macro for those routines, so that
205 * progras built on macOS Sonoma that attempt to use weak
206 * importing and availability tests to use those routines
207 * if they're available will get those routines weakly imported,
208 * so that if they're run on releases prior to Sonoma, they
209 * won't get an error from dyld about those routines being
210 * missing in libpcap. (If they don't use run-time availability
211 * tests, they will, instead, get crashes if they call one of
212 * those routines, as the addresses of those routines will be
213 * set to 0 by dyld, meaning the program will dereference a
214 * null pointer and crash when trying to call them.)
215 *
216 * (Not that it's useful to use those routines *anyway*, as they're
217 * stubs that always fail. The stubs were necessary in order to
218 * support weak exporting at all.)
219 */
220 #define PCAP_AVAILABLE_1_9_REMOTE PCAP_API_AVAILABLE(macos(14.0))
221 #define PCAP_AVAILABLE_1_10 PCAP_API_AVAILABLE(macos(12.1))
222 #define PCAP_AVAILABLE_1_10_REMOTE PCAP_API_AVAILABLE(macos(14.0))
223 #define PCAP_AVAILABLE_1_11 /* not released yet, so not in macOS yet */
224 #else /* __APPLE__ */
225 #define PCAP_AVAILABLE_0_4
226 #define PCAP_AVAILABLE_0_5
227 #define PCAP_AVAILABLE_0_6
228 #define PCAP_AVAILABLE_0_7
229 #define PCAP_AVAILABLE_0_8
230 #define PCAP_AVAILABLE_0_9
231 #define PCAP_AVAILABLE_1_0
232 /* #define PCAP_AVAILABLE_1_1 no routines added to the API */
233 #define PCAP_AVAILABLE_1_2
234 /* #define PCAP_AVAILABLE_1_3 no routines added to the API */
235 /* #define PCAP_AVAILABLE_1_4 no routines added to the API */
236 #define PCAP_AVAILABLE_1_5
237 /* #define PCAP_AVAILABLE_1_6 no routines added to the API */
238 #define PCAP_AVAILABLE_1_7
239 #define PCAP_AVAILABLE_1_8
240 #define PCAP_AVAILABLE_1_9
241 #define PCAP_AVAILABLE_1_9_REMOTE
242 #define PCAP_AVAILABLE_1_10
243 #define PCAP_AVAILABLE_1_10_REMOTE
244 #define PCAP_AVAILABLE_1_11
245 #endif /* __APPLE__ */
246
247 /*
248 * PCAP_NORETURN, before a function declaration, means "this function
249 * never returns". (It must go before the function declaration, e.g.
250 * "extern PCAP_NORETURN func(...)" rather than after the function
251 * declaration, as the MSVC version has to go before the declaration.)
252 *
253 * PCAP_NORETURN_DEF, before a function *definition*, means "this
254 * function never returns"; it would be used only for static functions
255 * that are defined before any use, and thus have no declaration.
256 * (MSVC doesn't support that; I guess the "decl" in "__declspec"
257 * means "declaration", and __declspec doesn't work with definitions.)
258 */
259 #if __has_attribute(noreturn) \
260 || PCAP_IS_AT_LEAST_GNUC_VERSION(2,5) \
261 || PCAP_IS_AT_LEAST_SUNC_VERSION(5,9) \
262 || PCAP_IS_AT_LEAST_XL_C_VERSION(7,0) \
263 || PCAP_IS_AT_LEAST_HP_C_CXX_VERSION(6,10) \
264 || __TINYC__
265 /*
266 * Compiler with support for __attribute((noreturn)), or GCC 2.5 and
267 * later, or some compiler asserting compatibility with GCC 2.5 and
268 * later, or Solaris Studio 12 (Sun C 5.9) and later, or IBM XL C 7.0
269 * and later (do any earlier versions of XL C support this?), or HP aCC
270 * A.06.10 and later, or current TinyCC.
271 */
272 #define PCAP_NORETURN __attribute((noreturn))
273 #define PCAP_NORETURN_DEF __attribute((noreturn))
274 #elif defined(_MSC_VER)
275 /*
276 * MSVC.
277 */
278 #define PCAP_NORETURN __declspec(noreturn)
279 #define PCAP_NORETURN_DEF
280 #else
281 #define PCAP_NORETURN
282 #define PCAP_NORETURN_DEF
283 #endif
284
285 /*
286 * PCAP_PRINTFLIKE(x,y), after a function declaration, means "this function
287 * does printf-style formatting, with the xth argument being the format
288 * string and the yth argument being the first argument for the format
289 * string".
290 */
291 #if __has_attribute(__format__) \
292 || PCAP_IS_AT_LEAST_GNUC_VERSION(2,3) \
293 || PCAP_IS_AT_LEAST_XL_C_VERSION(7,0) \
294 || PCAP_IS_AT_LEAST_HP_C_CXX_VERSION(6,10)
295 /*
296 * Compiler with support for it, or GCC 2.3 and later, or some compiler
297 * asserting compatibility with GCC 2.3 and later, or IBM XL C 7.0
298 * and later (do any earlier versions of XL C support this?),
299 * or HP aCC A.06.10 and later.
300 */
301 #define PCAP_PRINTFLIKE(x,y) __attribute__((__format__(__printf__,x,y)))
302 #else
303 #define PCAP_PRINTFLIKE(x,y)
304 #endif
305
306 /*
307 * PCAP_NONNULL(...), after a function declaration, means "the arguments
308 * whose ordinal numbers are listed are pointer arguments that must be
309 * non-null".
310 */
311 #if __has_attribute(nonnull) \
312 || PCAP_IS_AT_LEAST_GNUC_VERSION(3,3) \
313 || PCAP_IS_AT_LEAST_XL_C_VERSION(10,1)
314 /*
315 * Compiler with support for it, or GCC 3.3 and later, or some compiler
316 * asserting compatibility with GCC 3.3 and later, or IBM XL C 10.1
317 * and later (do any earlier versions of XL C support this?).
318 */
319 #define PCAP_NONNULL(...) __attribute__((nonnull(__VA_ARGS__)))
320 #else
321 #define PCAP_NONNULL(...)
322 #endif
323
324 /*
325 * PCAP_WARN_UNUSED_RESULT(...), after a function declaration, means
326 * "the return value of this function should always be used".
327 */
328 #if __has_attribute(warn_unused_result) \
329 || PCAP_IS_AT_LEAST_GNUC_VERSION(3,4) \
330 || PCAP_IS_AT_LEAST_XL_C_VERSION(10,1)
331 /*
332 * Compiler with support for it, or GCC 3.4 and later, or some compiler
333 * asserting compatibility with GCC 3.4 and later, or IBM XL C 10.1
334 * and later (do any earlier versions of XL C support this?).
335 */
336 #define PCAP_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
337 #else
338 #define PCAP_WARN_UNUSED_RESULT
339 #endif
340
341 /*
342 * PCAP_DEPRECATED(func, msg), after a function declaration, marks the
343 * function as deprecated.
344 *
345 * The argument is a string giving the warning message to use if the
346 * compiler supports that.
347 */
348 #if __has_attribute(deprecated) \
349 || PCAP_IS_AT_LEAST_GNUC_VERSION(4,5) \
350 || PCAP_IS_AT_LEAST_SUNC_VERSION(5,13)
351 /*
352 * Compiler that supports __has_attribute and __attribute__((deprecated)),
353 * or GCC 4.5 and later, or Sun/Oracle C 12.4 (Sun C 5.13) and later.
354 *
355 * Those support __attribute__((deprecated(msg))) (we assume, perhaps
356 * incorrectly, that anything that supports __has_attribute() is
357 * recent enough to support __attribute__((deprecated(msg)))).
358 */
359 #define PCAP_DEPRECATED(msg) __attribute__((deprecated(msg)))
360 #elif PCAP_IS_AT_LEAST_GNUC_VERSION(3,1)
361 /*
362 * GCC 3.1 through 4.4.
363 *
364 * Those support __attribute__((deprecated)) but not
365 * __attribute__((deprecated(msg))).
366 */
367 #define PCAP_DEPRECATED(msg) __attribute__((deprecated))
368 #elif defined(_MSC_VER) && !defined(BUILDING_PCAP)
369 /*
370 * MSVC, and we're not building libpcap itself; it's VS 2015
371 * and later, so we have __declspec(deprecated(...)).
372 *
373 * If we *are* building libpcap, we don't want this, as it'll warn
374 * us even if we *define* the function.
375 */
376 #define PCAP_DEPRECATED(msg) _declspec(deprecated(msg))
377 #else
378 #define PCAP_DEPRECATED(msg)
379 #endif
380
381 /*
382 * For flagging arguments as format strings in MSVC.
383 */
384 #ifdef _MSC_VER
385 #include <sal.h>
386 #define PCAP_FORMAT_STRING(p) _Printf_format_string_ p
387 #else
388 #define PCAP_FORMAT_STRING(p) p
389 #endif
390
391 #endif /* lib_pcap_funcattrs_h */