1335640Shselasky/* -*- Mode: c; tab-width: 8; indent-tabs-mode: 1; c-basic-offset: 8; -*- */
2335640Shselasky/*
3335640Shselasky * Copyright (c) 1993, 1994, 1995, 1996, 1997
4335640Shselasky *	The Regents of the University of California.  All rights reserved.
5335640Shselasky *
6335640Shselasky * Redistribution and use in source and binary forms, with or without
7335640Shselasky * modification, are permitted provided that the following conditions
8335640Shselasky * are met:
9335640Shselasky * 1. Redistributions of source code must retain the above copyright
10335640Shselasky *    notice, this list of conditions and the following disclaimer.
11335640Shselasky * 2. Redistributions in binary form must reproduce the above copyright
12335640Shselasky *    notice, this list of conditions and the following disclaimer in the
13335640Shselasky *    documentation and/or other materials provided with the distribution.
14335640Shselasky * 3. All advertising materials mentioning features or use of this software
15335640Shselasky *    must display the following acknowledgement:
16335640Shselasky *	This product includes software developed by the Computer Systems
17335640Shselasky *	Engineering Group at Lawrence Berkeley Laboratory.
18335640Shselasky * 4. Neither the name of the University nor of the Laboratory may be used
19335640Shselasky *    to endorse or promote products derived from this software without
20335640Shselasky *    specific prior written permission.
21335640Shselasky *
22335640Shselasky * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23335640Shselasky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24335640Shselasky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25335640Shselasky * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26335640Shselasky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27335640Shselasky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28335640Shselasky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29335640Shselasky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30335640Shselasky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31335640Shselasky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32335640Shselasky * SUCH DAMAGE.
33335640Shselasky */
34335640Shselasky
35335640Shselasky#ifndef lib_pcap_funcattrs_h
36335640Shselasky#define lib_pcap_funcattrs_h
37335640Shselasky
38335640Shselasky#include <pcap/compiler-tests.h>
39335640Shselasky
40335640Shselasky/*
41335640Shselasky * Attributes to apply to functions and their arguments, using various
42335640Shselasky * compiler-specific extensions.
43335640Shselasky */
44335640Shselasky
45335640Shselasky/*
46335640Shselasky * PCAP_API_DEF must be used when defining *data* exported from
47335640Shselasky * libpcap.  It can be used when defining *functions* exported
48335640Shselasky * from libpcap, but it doesn't have to be used there.  It
49335640Shselasky * should not be used in declarations in headers.
50335640Shselasky *
51335640Shselasky * PCAP_API must be used when *declaring* data or functions
52335640Shselasky * exported from libpcap; PCAP_API_DEF won't work on all platforms.
53335640Shselasky */
54335640Shselasky
55335640Shselasky#if defined(_WIN32)
56335640Shselasky  /*
57335640Shselasky   * For Windows:
58335640Shselasky   *
59335640Shselasky   *    when building libpcap:
60335640Shselasky   *
61335640Shselasky   *       if we're building it as a DLL, we have to declare API
62335640Shselasky   *       functions with __declspec(dllexport);
63335640Shselasky   *
64335640Shselasky   *       if we're building it as a static library, we don't want
65335640Shselasky   *       to do so.
66335640Shselasky   *
67335640Shselasky   *    when using libpcap:
68335640Shselasky   *
69335640Shselasky   *       if we're using the DLL, calls to its functions are a
70335640Shselasky   *       little more efficient if they're declared with
71335640Shselasky   *       __declspec(dllimport);
72335640Shselasky   *
73335640Shselasky   *       if we're not using the dll, we don't want to declare
74335640Shselasky   *       them that way.
75335640Shselasky   *
76335640Shselasky   * So:
77335640Shselasky   *
78335640Shselasky   *    if pcap_EXPORTS is defined, we define PCAP_API_DEF as
79335640Shselasky   *     __declspec(dllexport);
80335640Shselasky   *
81335640Shselasky   *    if PCAP_DLL is defined, we define PCAP_API_DEF as
82335640Shselasky   *    __declspec(dllimport);
83335640Shselasky   *
84335640Shselasky   *    otherwise, we define PCAP_API_DEF as nothing.
85335640Shselasky   */
86335640Shselasky  #if defined(pcap_EXPORTS)
87335640Shselasky    /*
88335640Shselasky     * We're compiling libpcap as a DLL, so we should export functions
89335640Shselasky     * in our API.
90335640Shselasky     */
91335640Shselasky    #define PCAP_API_DEF	__declspec(dllexport)
92335640Shselasky  #elif defined(PCAP_DLL)
93335640Shselasky    /*
94335640Shselasky     * We're using libpcap as a DLL, so the calls will be a little more
95335640Shselasky     * efficient if we explicitly import the functions.
96335640Shselasky     */
97335640Shselasky    #define PCAP_API_DEF	__declspec(dllimport)
98335640Shselasky  #else
99335640Shselasky    /*
100335640Shselasky     * Either we're building libpcap as a static library, or we're using
101335640Shselasky     * it as a static library, or we don't know for certain that we're
102335640Shselasky     * using it as a dynamic library, so neither import nor export the
103335640Shselasky     * functions explicitly.
104335640Shselasky     */
105335640Shselasky    #define PCAP_API_DEF
106335640Shselasky  #endif
107335640Shselasky#elif defined(MSDOS)
108335640Shselasky  /* XXX - does this need special treatment? */
109335640Shselasky  #define PCAP_API_DEF
110335640Shselasky#else /* UN*X */
111335640Shselasky  #ifdef pcap_EXPORTS
112335640Shselasky    /*
113335640Shselasky     * We're compiling libpcap as a (dynamic) shared library, so we should
114335640Shselasky     * export functions in our API.  The compiler might be configured not
115335640Shselasky     * to export functions from a shared library by default, so we might
116335640Shselasky     * have to explicitly mark functions as exported.
117335640Shselasky     */
118335640Shselasky    #if PCAP_IS_AT_LEAST_GNUC_VERSION(3,4) \
119335640Shselasky        || PCAP_IS_AT_LEAST_XL_C_VERSION(12,0)
120335640Shselasky      /*
121335640Shselasky       * GCC 3.4 or later, or some compiler asserting compatibility with
122335640Shselasky       * GCC 3.4 or later, or XL C 13.0 or later, so we have
123335640Shselasky       * __attribute__((visibility()).
124335640Shselasky       */
125335640Shselasky      #define PCAP_API_DEF	__attribute__((visibility("default")))
126335640Shselasky    #elif PCAP_IS_AT_LEAST_SUNC_VERSION(5,5)
127335640Shselasky      /*
128335640Shselasky       * Sun C 5.5 or later, so we have __global.
129335640Shselasky       * (Sun C 5.9 and later also have __attribute__((visibility()),
130335640Shselasky       * but there's no reason to prefer it with Sun C.)
131335640Shselasky       */
132335640Shselasky      #define PCAP_API_DEF	__global
133335640Shselasky    #else
134335640Shselasky      /*
135335640Shselasky       * We don't have anything to say.
136335640Shselasky       */
137335640Shselasky      #define PCAP_API_DEF
138335640Shselasky    #endif
139335640Shselasky  #else
140335640Shselasky    /*
141335640Shselasky     * We're not building libpcap.
142335640Shselasky     */
143335640Shselasky    #define PCAP_API_DEF
144335640Shselasky  #endif
145335640Shselasky#endif /* _WIN32/MSDOS/UN*X */
146335640Shselasky
147335640Shselasky#define PCAP_API	PCAP_API_DEF extern
148335640Shselasky
149335640Shselasky/*
150335640Shselasky * PCAP_NORETURN, before a function declaration, means "this function
151335640Shselasky * never returns".  (It must go before the function declaration, e.g.
152335640Shselasky * "extern PCAP_NORETURN func(...)" rather than after the function
153335640Shselasky * declaration, as the MSVC version has to go before the declaration.)
154335640Shselasky *
155335640Shselasky * PCAP_NORETURN_DEF, before a function *definition*, means "this
156335640Shselasky * function never returns"; it would be used only for static functions
157335640Shselasky * that are defined before any use, and thus have no declaration.
158335640Shselasky * (MSVC doesn't support that; I guess the "decl" in "__declspec"
159335640Shselasky * means "declaration", and __declspec doesn't work with definitions.)
160335640Shselasky */
161335640Shselasky#if __has_attribute(noreturn) \
162335640Shselasky    || PCAP_IS_AT_LEAST_GNUC_VERSION(2,5) \
163335640Shselasky    || PCAP_IS_AT_LEAST_SUNC_VERSION(5,9) \
164335640Shselasky    || PCAP_IS_AT_LEAST_XL_C_VERSION(10,1) \
165335640Shselasky    || PCAP_IS_AT_LEAST_HP_C_VERSION(6,10)
166335640Shselasky  /*
167356341Scy   * Compiler with support for __attribute((noreturn)), or GCC 2.5 or
168356341Scy   * later, or some compiler asserting compatibility with GCC 2.5 or
169356341Scy   * later, or Solaris Studio 12 (Sun C 5.9) or later, or IBM XL C 10.1
170356341Scy   * or later (do any earlier versions of XL C support this?), or HP aCC
171356341Scy   * A.06.10 or later.
172335640Shselasky   */
173335640Shselasky  #define PCAP_NORETURN __attribute((noreturn))
174335640Shselasky  #define PCAP_NORETURN_DEF __attribute((noreturn))
175335640Shselasky#elif defined(_MSC_VER)
176335640Shselasky  /*
177335640Shselasky   * MSVC.
178335640Shselasky   */
179335640Shselasky  #define PCAP_NORETURN __declspec(noreturn)
180335640Shselasky  #define PCAP_NORETURN_DEF
181335640Shselasky#else
182335640Shselasky  #define PCAP_NORETURN
183335640Shselasky  #define PCAP_NORETURN_DEF
184335640Shselasky#endif
185335640Shselasky
186335640Shselasky/*
187335640Shselasky * PCAP_PRINTFLIKE(x,y), after a function declaration, means "this function
188335640Shselasky * does printf-style formatting, with the xth argument being the format
189335640Shselasky * string and the yth argument being the first argument for the format
190335640Shselasky * string".
191335640Shselasky */
192335640Shselasky#if __has_attribute(__format__) \
193335640Shselasky    || PCAP_IS_AT_LEAST_GNUC_VERSION(2,3) \
194335640Shselasky    || PCAP_IS_AT_LEAST_XL_C_VERSION(10,1) \
195335640Shselasky    || PCAP_IS_AT_LEAST_HP_C_VERSION(6,10)
196335640Shselasky  /*
197356341Scy   * Compiler with support for it, or GCC 2.3 or later, or some compiler
198356341Scy   * asserting compatibility with GCC 2.3 or later, or IBM XL C 10.1
199335640Shselasky   * and later (do any earlier versions of XL C support this?),
200335640Shselasky   * or HP aCC A.06.10 and later.
201335640Shselasky   */
202335640Shselasky  #define PCAP_PRINTFLIKE(x,y) __attribute__((__format__(__printf__,x,y)))
203335640Shselasky#else
204335640Shselasky  #define PCAP_PRINTFLIKE(x,y)
205335640Shselasky#endif
206335640Shselasky
207335640Shselasky/*
208335640Shselasky * PCAP_DEPRECATED(func, msg), after a function declaration, marks the
209335640Shselasky * function as deprecated.
210335640Shselasky *
211335640Shselasky * The first argument is the name of the function; the second argument is
212335640Shselasky * a string giving the warning message to use if the compiler supports that.
213335640Shselasky *
214335640Shselasky * (Thank you, Microsoft, for requiring the function name.)
215335640Shselasky */
216335640Shselasky#if __has_attribute(deprecated) \
217335640Shselasky    || PCAP_IS_AT_LEAST_GNUC_VERSION(4,5) \
218335640Shselasky    || PCAP_IS_AT_LEAST_SUNC_VERSION(5,13)
219335640Shselasky  /*
220335640Shselasky   * Compiler that supports __has_attribute and __attribute__((deprecated)),
221356341Scy   * or GCC 4.5 or later, or Sun/Oracle C 12.4 (Sun C 5.13) or later.
222335640Shselasky   *
223335640Shselasky   * Those support __attribute__((deprecated(msg))) (we assume, perhaps
224335640Shselasky   * incorrectly, that anything that supports __has_attribute() is
225335640Shselasky   * recent enough to support __attribute__((deprecated(msg)))).
226335640Shselasky   */
227335640Shselasky  #define PCAP_DEPRECATED(func, msg)	__attribute__((deprecated(msg)))
228335640Shselasky#elif PCAP_IS_AT_LEAST_GNUC_VERSION(3,1)
229335640Shselasky  /*
230335640Shselasky   * GCC 3.1 through 4.4.
231335640Shselasky   *
232335640Shselasky   * Those support __attribute__((deprecated)) but not
233335640Shselasky   * __attribute__((deprecated(msg))).
234335640Shselasky   */
235335640Shselasky  #define PCAP_DEPRECATED(func, msg)	__attribute__((deprecated))
236335640Shselasky#elif (defined(_MSC_VER) && (_MSC_VER >= 1500)) && !defined(BUILDING_PCAP)
237335640Shselasky  /*
238335640Shselasky   * MSVC from Visual Studio 2008 or later, and we're not building
239335640Shselasky   * libpcap itself.
240335640Shselasky   *
241335640Shselasky   * If we *are* building libpcap, we don't want this, as it'll warn
242335640Shselasky   * us even if we *define* the function.
243335640Shselasky   */
244335640Shselasky  #define PCAP_DEPRECATED(func, msg)	__pragma(deprecated(func))
245335640Shselasky#else
246335640Shselasky  #define PCAP_DEPRECATED(func, msg)
247335640Shselasky#endif
248335640Shselasky
249335640Shselasky/*
250335640Shselasky * For flagging arguments as format strings in MSVC.
251335640Shselasky */
252335640Shselasky#ifdef _MSC_VER
253335640Shselasky #include <sal.h>
254335640Shselasky #if _MSC_VER > 1400
255335640Shselasky  #define PCAP_FORMAT_STRING(p) _Printf_format_string_ p
256335640Shselasky #else
257335640Shselasky  #define PCAP_FORMAT_STRING(p) __format_string p
258335640Shselasky #endif
259335640Shselasky#else
260335640Shselasky #define PCAP_FORMAT_STRING(p) p
261335640Shselasky#endif
262335640Shselasky
263335640Shselasky#endif /* lib_pcap_funcattrs_h */
264