funcattrs.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. #ifndef lib_pcap_funcattrs_h
  35. #define lib_pcap_funcattrs_h
  36. #include <pcap/compiler-tests.h>
  37. /*
  38. * Attributes to apply to functions and their arguments, using various
  39. * compiler-specific extensions.
  40. */
  41. /*
  42. * PCAP_API_DEF must be used when defining *data* exported from
  43. * libpcap. It can be used when defining *functions* exported
  44. * from libpcap, but it doesn't have to be used there. It
  45. * should not be used in declarations in headers.
  46. *
  47. * PCAP_API must be used when *declaring* data or functions
  48. * exported from libpcap; PCAP_API_DEF won't work on all platforms.
  49. */
  50. #if defined(_WIN32)
  51. /*
  52. * For Windows:
  53. *
  54. * when building libpcap:
  55. *
  56. * if we're building it as a DLL, we have to declare API
  57. * functions with __declspec(dllexport);
  58. *
  59. * if we're building it as a static library, we don't want
  60. * to do so.
  61. *
  62. * when using libpcap:
  63. *
  64. * if we're using the DLL, calls to its functions are a
  65. * little more efficient if they're declared with
  66. * __declspec(dllimport);
  67. *
  68. * if we're not using the dll, we don't want to declare
  69. * them that way.
  70. *
  71. * So:
  72. *
  73. * if pcap_EXPORTS is defined, we define PCAP_API_DEF as
  74. * __declspec(dllexport);
  75. *
  76. * if PCAP_DLL is defined, we define PCAP_API_DEF as
  77. * __declspec(dllimport);
  78. *
  79. * otherwise, we define PCAP_API_DEF as nothing.
  80. */
  81. #if defined(pcap_EXPORTS)
  82. /*
  83. * We're compiling libpcap as a DLL, so we should export functions
  84. * in our API.
  85. */
  86. #define PCAP_API_DEF __declspec(dllexport)
  87. #elif defined(PCAP_DLL)
  88. /*
  89. * We're using libpcap as a DLL, so the calls will be a little more
  90. * efficient if we explicitly import the functions.
  91. */
  92. #define PCAP_API_DEF __declspec(dllimport)
  93. #else
  94. /*
  95. * Either we're building libpcap as a static library, or we're using
  96. * it as a static library, or we don't know for certain that we're
  97. * using it as a dynamic library, so neither import nor export the
  98. * functions explicitly.
  99. */
  100. #define PCAP_API_DEF
  101. #endif
  102. #elif defined(MSDOS)
  103. /* XXX - does this need special treatment? */
  104. #define PCAP_API_DEF
  105. #else /* UN*X */
  106. #ifdef pcap_EXPORTS
  107. /*
  108. * We're compiling libpcap as a (dynamic) shared library, so we should
  109. * export functions in our API. The compiler might be configured not
  110. * to export functions from a shared library by default, so we might
  111. * have to explicitly mark functions as exported.
  112. */
  113. #if PCAP_IS_AT_LEAST_GNUC_VERSION(3,4) \
  114. || PCAP_IS_AT_LEAST_XL_C_VERSION(12,0)
  115. /*
  116. * GCC 3.4 and later, or some compiler asserting compatibility with
  117. * GCC 3.4 and later, or XL C 13.0 and later, so we have
  118. * __attribute__((visibility()).
  119. */
  120. #define PCAP_API_DEF __attribute__((visibility("default")))
  121. #elif PCAP_IS_AT_LEAST_SUNC_VERSION(5,5)
  122. /*
  123. * Sun C 5.5 and later, so we have __global.
  124. * (Sun C 5.9 and later also have __attribute__((visibility()),
  125. * but there's no reason to prefer it with Sun C.)
  126. */
  127. #define PCAP_API_DEF __global
  128. #else
  129. /*
  130. * We don't have anything to say.
  131. */
  132. #define PCAP_API_DEF
  133. #endif
  134. #else
  135. /*
  136. * We're not building libpcap.
  137. */
  138. #define PCAP_API_DEF
  139. #endif
  140. #endif /* _WIN32/MSDOS/UN*X */
  141. #define PCAP_API PCAP_API_DEF extern
  142. /*
  143. * Definitions to 1) indicate what version of libpcap first had a given
  144. * API and 2) allow upstream providers whose build environments allow
  145. * APIs to be designated as "first available in this release" to do so
  146. * by appropriately defining them.
  147. *
  148. * Yes, that's you, Apple. :-) Please define PCAP_AVAILABLE_MACOS()
  149. * as necessary to make various APIs "weak exports" to make it easier
  150. * for software that's distributed in binary form and that uses libpcap
  151. * to run on multiple macOS versions and use new APIs when available.
  152. * (Yes, such third-party software exists - Wireshark provides binary
  153. * packages for macOS, for example. tcpdump doesn't count, as that's
  154. * provided by Apple, so each release can come with a version compiled
  155. * to use the APIs present in that release.)
  156. *
  157. * The non-macOS versioning is based on
  158. *
  159. * https://en.wikipedia.org/wiki/Darwin_(operating_system)#Release_history
  160. *
  161. * If there are any corrections, please submit it upstream to the
  162. * libpcap maintainers, preferably as a pull request on
  163. *
  164. * https://github.com/the-tcpdump-group/libpcap
  165. *
  166. * We don't define it ourselves because, if you're building and
  167. * installing libpcap on macOS yourself, the APIs will be available
  168. * no matter what OS version you're installing it on.
  169. *
  170. * For other platforms, we don't define them, leaving it up to
  171. * others to do so based on their OS versions, if appropriate.
  172. *
  173. * We start with libpcap 0.4, as that was the last LBL release, and
  174. * I've never seen earlier releases.
  175. */
  176. #ifdef __APPLE__
  177. #include <Availability.h>
  178. /*
  179. * When building as part of macOS, define this as __API_AVAILABLE(__VA_ARGS__).
  180. *
  181. * XXX - if there's some #define to indicate that this is being built
  182. * as part of the macOS build process, we could make that Just Work.
  183. */
  184. #define PCAP_AVAILABLE(...)
  185. #define PCAP_AVAILABLE_0_4 PCAP_AVAILABLE(macos(10.0)) /* Did any version of Mac OS X ship with this? */
  186. #define PCAP_AVAILABLE_0_5 PCAP_AVAILABLE(macos(10.0)) /* Did any version of Mac OS X ship with this? */
  187. #define PCAP_AVAILABLE_0_6 PCAP_AVAILABLE(macos(10.1))
  188. #define PCAP_AVAILABLE_0_7 PCAP_AVAILABLE(macos(10.4))
  189. #define PCAP_AVAILABLE_0_8 PCAP_AVAILABLE(macos(10.4))
  190. #define PCAP_AVAILABLE_0_9 PCAP_AVAILABLE(macos(10.5), ios(1.0))
  191. #define PCAP_AVAILABLE_1_0 PCAP_AVAILABLE(macos(10.6), ios(4.0))
  192. /* #define PCAP_AVAILABLE_1_1 no routines added to the API */
  193. #define PCAP_AVAILABLE_1_2 PCAP_AVAILABLE(macos(10.9), ios(6.0))
  194. /* #define PCAP_AVAILABLE_1_3 no routines added to the API */
  195. /* #define PCAP_AVAILABLE_1_4 no routines added to the API */
  196. #define PCAP_AVAILABLE_1_5 PCAP_AVAILABLE(macos(10.10), ios(7.0), watchos(1.0))
  197. /* #define PCAP_AVAILABLE_1_6 no routines added to the API */
  198. #define PCAP_AVAILABLE_1_7 PCAP_AVAILABLE(macos(10.12), ios(10.0), tvos(10.0), watchos(3.0))
  199. #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? */
  200. #define PCAP_AVAILABLE_1_9 PCAP_AVAILABLE(macos(10.13), ios(11.0), tvos(11.0), watchos(4.0))
  201. #define PCAP_AVAILABLE_1_10 /* not in macOS yet */
  202. #define PCAP_AVAILABLE_1_11 /* not released yet, so not in macOS yet */
  203. #else /* __APPLE__ */
  204. #define PCAP_AVAILABLE_0_4
  205. #define PCAP_AVAILABLE_0_5
  206. #define PCAP_AVAILABLE_0_6
  207. #define PCAP_AVAILABLE_0_7
  208. #define PCAP_AVAILABLE_0_8
  209. #define PCAP_AVAILABLE_0_9
  210. #define PCAP_AVAILABLE_1_0
  211. /* #define PCAP_AVAILABLE_1_1 no routines added to the API */
  212. #define PCAP_AVAILABLE_1_2
  213. /* #define PCAP_AVAILABLE_1_3 no routines added to the API */
  214. /* #define PCAP_AVAILABLE_1_4 no routines added to the API */
  215. #define PCAP_AVAILABLE_1_5
  216. /* #define PCAP_AVAILABLE_1_6 no routines added to the API */
  217. #define PCAP_AVAILABLE_1_7
  218. #define PCAP_AVAILABLE_1_8
  219. #define PCAP_AVAILABLE_1_9
  220. #define PCAP_AVAILABLE_1_10
  221. #define PCAP_AVAILABLE_1_11
  222. #endif /* __APPLE__ */
  223. /*
  224. * PCAP_NORETURN, before a function declaration, means "this function
  225. * never returns". (It must go before the function declaration, e.g.
  226. * "extern PCAP_NORETURN func(...)" rather than after the function
  227. * declaration, as the MSVC version has to go before the declaration.)
  228. *
  229. * PCAP_NORETURN_DEF, before a function *definition*, means "this
  230. * function never returns"; it would be used only for static functions
  231. * that are defined before any use, and thus have no declaration.
  232. * (MSVC doesn't support that; I guess the "decl" in "__declspec"
  233. * means "declaration", and __declspec doesn't work with definitions.)
  234. */
  235. #if __has_attribute(noreturn) \
  236. || PCAP_IS_AT_LEAST_GNUC_VERSION(2,5) \
  237. || PCAP_IS_AT_LEAST_SUNC_VERSION(5,9) \
  238. || PCAP_IS_AT_LEAST_XL_C_VERSION(10,1) \
  239. || PCAP_IS_AT_LEAST_HP_C_VERSION(6,10)
  240. /*
  241. * Compiler with support for __attribute((noreturn)), or GCC 2.5 and
  242. * later, or some compiler asserting compatibility with GCC 2.5 and
  243. * later, or Solaris Studio 12 (Sun C 5.9) and later, or IBM XL C 10.1
  244. * and later (do any earlier versions of XL C support this?), or HP aCC
  245. * A.06.10 and later.
  246. */
  247. #define PCAP_NORETURN __attribute((noreturn))
  248. #define PCAP_NORETURN_DEF __attribute((noreturn))
  249. #elif defined(_MSC_VER)
  250. /*
  251. * MSVC.
  252. */
  253. #define PCAP_NORETURN __declspec(noreturn)
  254. #define PCAP_NORETURN_DEF
  255. #else
  256. #define PCAP_NORETURN
  257. #define PCAP_NORETURN_DEF
  258. #endif
  259. /*
  260. * PCAP_PRINTFLIKE(x,y), after a function declaration, means "this function
  261. * does printf-style formatting, with the xth argument being the format
  262. * string and the yth argument being the first argument for the format
  263. * string".
  264. */
  265. #if __has_attribute(__format__) \
  266. || PCAP_IS_AT_LEAST_GNUC_VERSION(2,3) \
  267. || PCAP_IS_AT_LEAST_XL_C_VERSION(10,1) \
  268. || PCAP_IS_AT_LEAST_HP_C_VERSION(6,10)
  269. /*
  270. * Compiler with support for it, or GCC 2.3 and later, or some compiler
  271. * asserting compatibility with GCC 2.3 and later, or IBM XL C 10.1
  272. * and later (do any earlier versions of XL C support this?),
  273. * or HP aCC A.06.10 and later.
  274. */
  275. #define PCAP_PRINTFLIKE(x,y) __attribute__((__format__(__printf__,x,y)))
  276. #else
  277. #define PCAP_PRINTFLIKE(x,y)
  278. #endif
  279. /*
  280. * PCAP_DEPRECATED(func, msg), after a function declaration, marks the
  281. * function as deprecated.
  282. *
  283. * The first argument is the name of the function; the second argument is
  284. * a string giving the warning message to use if the compiler supports that.
  285. *
  286. * (Thank you, Microsoft, for requiring the function name.)
  287. */
  288. #if __has_attribute(deprecated) \
  289. || PCAP_IS_AT_LEAST_GNUC_VERSION(4,5) \
  290. || PCAP_IS_AT_LEAST_SUNC_VERSION(5,13)
  291. /*
  292. * Compiler that supports __has_attribute and __attribute__((deprecated)),
  293. * or GCC 4.5 and later, or Sun/Oracle C 12.4 (Sun C 5.13) and later.
  294. *
  295. * Those support __attribute__((deprecated(msg))) (we assume, perhaps
  296. * incorrectly, that anything that supports __has_attribute() is
  297. * recent enough to support __attribute__((deprecated(msg)))).
  298. */
  299. #define PCAP_DEPRECATED(func, msg) __attribute__((deprecated(msg)))
  300. #elif PCAP_IS_AT_LEAST_GNUC_VERSION(3,1)
  301. /*
  302. * GCC 3.1 through 4.4.
  303. *
  304. * Those support __attribute__((deprecated)) but not
  305. * __attribute__((deprecated(msg))).
  306. */
  307. #define PCAP_DEPRECATED(func, msg) __attribute__((deprecated))
  308. #elif defined(_MSC_VER) && !defined(BUILDING_PCAP)
  309. /*
  310. * MSVC, and we're not building libpcap itself; it's VS 2015
  311. * and later, so we have the deprecated pragma.
  312. *
  313. * If we *are* building libpcap, we don't want this, as it'll warn
  314. * us even if we *define* the function.
  315. */
  316. #define PCAP_DEPRECATED(func, msg) __pragma(deprecated(func))
  317. #else
  318. #define PCAP_DEPRECATED(func, msg)
  319. #endif
  320. /*
  321. * For flagging arguments as format strings in MSVC.
  322. */
  323. #ifdef _MSC_VER
  324. #include <sal.h>
  325. #define PCAP_FORMAT_STRING(p) _Printf_format_string_ p
  326. #else
  327. #define PCAP_FORMAT_STRING(p) p
  328. #endif
  329. #endif /* lib_pcap_funcattrs_h */