]> The Tcpdump Group git mirrors - libpcap/blob - pcap/funcattrs.h
Clean up the ether_hostton() stuff.
[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 /*
39 * Attributes to apply to functions and their arguments, using various
40 * compiler-specific extensions.
41 */
42
43 /*
44 * PCAP_API_DEF must be used when defining *data* exported from
45 * libpcap. It can be used when defining *functions* exported
46 * from libpcap, but it doesn't have to be used there. It
47 * should not be used in declarations in headers.
48 *
49 * PCAP_API must be used when *declaring* data or functions
50 * exported from libpcap; PCAP_API_DEF won't work on all platforms.
51 */
52
53 /*
54 * This was introduced by Clang:
55 *
56 * http://clang.llvm.org/docs/LanguageExtensions.html#has-attribute
57 *
58 * in some version (which version?); it has been picked up by GCC 5.0.
59 */
60 #ifndef __has_attribute
61 /*
62 * It's a macro, so you can check whether it's defined to check
63 * whether it's supported.
64 *
65 * If it's not, define it to always return 0, so that we move on to
66 * the fallback checks.
67 */
68 #define __has_attribute(x) 0
69 #endif
70
71 /*
72 * Check whether this is GCC major.minor or a later release, or some
73 * compiler that claims to be "just like GCC" of that version or a
74 * later release.
75 */
76 #define PCAP_IS_AT_LEAST_GNUC_VERSION(major, minor) \
77 (defined(__GNUC__) && \
78 (__GNUC__ > (major) || \
79 (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor))))
80
81 /*
82 * Check wehether this is Sun C/SunPro C/Oracle Studio major.minor
83 * or a later release.
84 *
85 * The version number in __SUNPRO_C is encoded in hex BCD, with the
86 * uppermost hex digit being the major version number, the next
87 * one or two hex digits being the minor version number, and
88 * the last digit being the patch version.
89 *
90 * It represents the *compiler* version, not the product version;
91 * see
92 *
93 * https://sourceforge.net/p/predef/wiki/Compilers/
94 *
95 * for a partial mapping, which we assume continues for later
96 * 12.x product releases.
97 */
98 #define PCAP_SUNPRO_VERSION_TO_BCD(major, minor) \
99 (((minor) >= 10) ? \
100 (((major) << 12) | (((minor)/10) << 8) | (((minor)%10) << 4)) : \
101 (((major) << 8) | ((minor) << 4)))
102 #define PCAP_IS_AT_LEAST_SUNC_VERSION(major, minor) \
103 (defined(__SUNPRO_C) && \
104 (__SUNPRO_C >= PCAP_SUNPRO_VERSION_TO_BCD((major), (minor))))
105
106 /*
107 * Check wehether this is IBM XL C major.minor or a later release.
108 *
109 * The version number in __xlC__ has the major version in the
110 * upper 8 bits and the minor version in the lower 8 bits.
111 */
112 #define PCAP_IS_AT_LEAST_XL_C_VERSION(major, minor) \
113 (defined(__xlC__) && __xlC__ >= (((major) << 8) | (minor)))
114
115 /*
116 * Check wehether this is Sun C/SunPro C/Oracle Studio major.minor
117 * or a later release.
118 *
119 * The version number in __HP_aCC is encoded in zero-padded decimal BCD,
120 * with the "A." stripped off, the uppermost two decimal digits being
121 * the major version number, the next two decimal digits being the minor
122 * version number, and the last two decimal digits being the patch version.
123 * (Strip off the A., remove the . between the major and minor version
124 * number, and add two digits of patch.)
125 */
126 #define PCAP_IS_AT_LEAST_HP_C_VERSION(major, minor) \
127 (defined(__HP_aCC) && \
128 (__HP_aCC >= ((major)*10000 + (minor)*100)))
129
130 #if defined(_WIN32)
131 #ifdef BUILDING_PCAP
132 /*
133 * We're compiling libpcap, so we should export functions in our
134 * API.
135 */
136 #define PCAP_API_DEF __declspec(dllexport)
137 #else
138 #define PCAP_API_DEF __declspec(dllimport)
139 #endif
140 #elif defined(MSDOS)
141 /* XXX - does this need special treatment? */
142 #define PCAP_API_DEF
143 #else /* UN*X */
144 #ifdef BUILDING_PCAP
145 /*
146 * We're compiling libpcap, so we should export functions in our API.
147 * The compiler might be configured not to export functions from a
148 * shared library by default, so we might have to explicitly mark
149 * functions as exported.
150 */
151 #if PCAP_IS_AT_LEAST_GNUC_VERSION(3, 4) \
152 || PCAP_IS_AT_LEAST_XL_C_VERSION(12, 0)
153 /*
154 * GCC 3.4 or later, or some compiler asserting compatibility with
155 * GCC 3.4 or later, or XL C 13.0 or later, so we have
156 * __attribute__((visibility()).
157 */
158 #define PCAP_API_DEF __attribute__((visibility("default")))
159 #elif PCAP_IS_AT_LEAST_SUNC_VERSION(5, 5)
160 /*
161 * Sun C 5.5 or later, so we have __global.
162 * (Sun C 5.9 and later also have __attribute__((visibility()),
163 * but there's no reason to prefer it with Sun C.)
164 */
165 #define PCAP_API_DEF __global
166 #else
167 /*
168 * We don't have anything to say.
169 */
170 #define PCAP_API_DEF
171 #endif
172 #else
173 /*
174 * We're not building libpcap.
175 */
176 #define PCAP_API_DEF
177 #endif
178 #endif /* _WIN32/MSDOS/UN*X */
179
180 #define PCAP_API PCAP_API_DEF extern
181
182 /*
183 * PCAP_NORETURN, before a function declaration, means "this function
184 * never returns". (It must go before the function declaration, e.g.
185 * "extern PCAP_NORETURN func(...)" rather than after the function
186 * declaration, as the MSVC version has to go before the declaration.)
187 */
188 #if __has_attribute(noreturn) \
189 || PCAP_IS_AT_LEAST_GNUC_VERSION(2, 5) \
190 || PCAP_IS_AT_LEAST_SUNC_VERSION(5, 9) \
191 || PCAP_IS_AT_LEAST_XL_C_VERSION(10, 1) \
192 || PCAP_IS_AT_LEAST_HP_C_VERSION(6, 10)
193 /*
194 * Compiler with support for __attribute((noreturn)), or GCC 2.5 and
195 * later, or Solaris Studio 12 (Sun C 5.9) and later, or IBM XL C 10.1
196 * and later (do any earlier versions of XL C support this?), or
197 * HP aCC A.06.10 and later.
198 */
199 #define PCAP_NORETURN __attribute((noreturn))
200 #elif defined(_MSC_VER)
201 /*
202 * MSVC.
203 */
204 #define PCAP_NORETURN __declspec(noreturn)
205 #else
206 #define PCAP_NORETURN
207 #endif
208
209 /*
210 * PCAP_PRINTFLIKE(x,y), after a function declaration, means "this function
211 * does printf-style formatting, with the xth argument being the format
212 * string and the yth argument being the first argument for the format
213 * string".
214 */
215 #if __has_attribute(__format__) \
216 || PCAP_IS_AT_LEAST_GNUC_VERSION(2, 3) \
217 || PCAP_IS_AT_LEAST_XL_C_VERSION(10, 1) \
218 || PCAP_IS_AT_LEAST_HP_C_VERSION(6, 10)
219 /*
220 * Compiler with support for it, or GCC 2.3 and later, or IBM XL C 10.1
221 * and later (do any earlier versions of XL C support this?),
222 * or HP aCC A.06.10 and later.
223 */
224 #define PCAP_PRINTFLIKE(x,y) __attribute__((__format__(__printf__,x,y)))
225 #else
226 #define PCAP_PRINTFLIKE(x,y)
227 #endif
228
229 /*
230 * PCAP_DEPRECATED(func, msg), after a function declaration, marks the
231 * function as deprecated.
232 *
233 * The first argument is the name of the function; the second argument is
234 * a string giving the warning message to use if the compiler supports that.
235 *
236 * (Thank you, Microsoft, for requiring the function name.)
237 */
238 #if __has_attribute(deprecated) \
239 || PCAP_IS_AT_LEAST_GNUC_VERSION(4, 5) \
240 || PCAP_IS_AT_LEAST_SUNC_VERSION(5, 13)
241 /*
242 * Compiler that supports __has_attribute and __attribute__((deprecated)),
243 * or GCC 4.5 and later, or Sun/Oracle C 12.4 (Sun C 5.13) or later.
244 *
245 * Those support __attribute__((deprecated(msg))) (we assume, perhaps
246 * incorrectly, that anything that supports __has_attribute() is
247 * recent enough to support __attribute__((deprecated(msg)))).
248 */
249 #define PCAP_DEPRECATED(func, msg) __attribute__((deprecated(msg)))
250 #elif PCAP_IS_AT_LEAST_GNUC_VERSION(3, 1)
251 /*
252 * GCC 3.1 through 4.4.
253 *
254 * Those support __attribute__((deprecated)) but not
255 * __attribute__((deprecated(msg))).
256 */
257 #define PCAP_DEPRECATED(func, msg) __attribute__((deprecated))
258 #elif (defined(_MSC_VER) && (_MSC_VER >= 1500)) && !defined(BUILDING_PCAP)
259 /*
260 * MSVC from Visual Studio 2008 or later, and we're not building
261 * libpcap itself.
262 *
263 * If we *are* building libpcap, we don't want this, as it'll warn
264 * us even if we *define* the function.
265 */
266 #define PCAP_DEPRECATED(func, msg) __pragma(deprecated(func))
267 #else
268 #define PCAP_DEPRECATED(func, msg)
269 #endif
270
271 /*
272 * For flagging arguments as format strings in MSVC.
273 */
274 #if _MSC_VER >= 1400
275 #include <sal.h>
276 #if _MSC_VER > 1400
277 #define PCAP_FORMAT_STRING(p) _Printf_format_string_ p
278 #else
279 #define PCAP_FORMAT_STRING(p) __format_string p
280 #endif
281 #else
282 #define PCAP_FORMAT_STRING(p) p
283 #endif
284
285 #endif /* lib_pcap_funcattrs_h */