#include <errno.h>
+#include "compiler-tests.h"
+
+#include "varattrs.h"
+
/*
* Get the C99 types, and the PRI[doux]64 format strings, defined.
*/
typedef unsigned long long uint64_t;
typedef long long int64_t;
#endif
+
+ /*
+ * We have _strtoi64(). Use that for strtoint64_t().
+ */
+ #define strtoint64_t _strtoi64
#endif
/*
#include <fcntl.h>
#include <sys/types.h>
+#ifdef _MSC_VER
+ /*
+ * Compiler is MSVC.
+ */
+ #if _MSC_VER >= 1800
+ /*
+ * VS 2013 or newer; we have strtoll(). Use that for strtoint64_t().
+ */
+ #define strtoint64_t strtoll
+ #else
+ /*
+ * Earlier VS; we don't have strtoll(), but we do have
+ * _strtoi64(). Use that for strtoint64_t().
+ */
+ #define strtoint64_t _strtoi64
+ #endif
+
+ /*
+ * Microsoft's documentation doesn't speak of LL as a valid
+ * suffix for 64-bit integers, so we'll just use i64.
+ */
+ #define INT64_T_CONSTANT(constant) (constant##i64)
+#else
+ /*
+ * Non-Microsoft compiler.
+ *
+ * XXX - should we use strtoll or should we use _strtoi64()?
+ */
+ #define strtoint64_t strtoll
+
+ /*
+ * Assume LL works.
+ */
+ #define INT64_T_CONSTANT(constant) (constant##LL)
+#endif
+
#ifdef _MSC_VER
#define stat _stat
#define open _open
#define inline __inline
#endif
-#ifdef AF_INET6
+#if defined(AF_INET6) && !defined(HAVE_OS_IPV6_SUPPORT)
#define HAVE_OS_IPV6_SUPPORT
#endif
#define MAXHOSTNAMELEN 64
#define snprintf _snprintf
#define vsnprintf _vsnprintf
-#define RETSIGTYPE void
#else /* _WIN32 */
#include <sys/socket.h>
#include <netinet/in.h>
-#ifdef TIME_WITH_SYS_TIME
#include <time.h>
-#endif
#include <arpa/inet.h>
-#endif /* _WIN32 */
+/*
+ * Assume all UN*Xes have strtoll(), and use it for strtoint64_t().
+ */
+#define strtoint64_t strtoll
-#ifndef HAVE___ATTRIBUTE__
-#define __attribute__(x)
-#endif
+/*
+ * Assume LL works.
+ */
+#define INT64_T_CONSTANT(constant) (constant##LL)
+
+#endif /* _WIN32 */
/*
* Used to declare a structure unaligned, so that the C compiler,
* This is required because there is no guarantee that the packet
* data we get from libpcap/WinPcap is properly aligned.
*
- * This assumes that, for all compilers that support __attribute__:
- *
- * 1) they support __attribute__((packed));
- *
- * 2) for all instruction set architectures requiring strict
- * alignment, declaring a structure with that attribute
- * causes the compiler to generate code that handles
- * misaligned 2-byte, 4-byte, and 8-byte integral
- * quantities.
+ * This assumes that, for all compilers that support __attribute__((packed)),
+ * for all instruction set architectures requiring strict alignment, declaring
+ * a structure with that attribute causes the compiler to generate code that
+ * handles misaligned 2-byte, 4-byte, and 8-byte integral quantities.
*
* It does not (yet) handle compilers where you can get the compiler
* to generate code of that sort by some other means.
*
* Note: this also requires that padding be put into the structure,
* at least for compilers where it's implemented as __attribute__((packed)).
+ *
+ * XXX - now that we're using nd_ types that are just arrays of bytes, is
+ * this still necessary? Are there any compilers that align structures,
+ * none of whose members require more than byte alignment, on more than
+ * one-byte boundaries, and assume a structure is aligned on such a
+ * boundary? (I have vague memories of either m68k or ARM compilers
+ * aligning on at least 2-byte boundaries.)
*/
-#if !(defined(_MSC_VER) && defined(UNALIGNED))
-/* MSVC may have its own macro defined with the same name and purpose. */
-#undef UNALIGNED
-#define UNALIGNED __attribute__((packed))
+#if ND_IS_AT_LEAST_GNUC_VERSION(2,0) || \
+ ND_IS_AT_LEAST_XL_C_VERSION(6,0)
+ /*
+ * GCC 2.0 or later, or a compiler that claims to be GCC 2.0 or later,
+ * or IBM XL C 6.0 or later.
+ *
+ * Use __attribute__((packed)).
+ */
+ #define ND_UNALIGNED __attribute__((packed))
+#else
+ /*
+ * Nothing.
+ */
+ #define ND_UNALIGNED
#endif
/*
* an 80386, so, for example, it avoids the bswap instruction added in
* the 80486.
*
- * (We don't use them on OS X; Apple provides their own, which *doesn't*
- * avoid the bswap instruction, as OS X only supports machines that
+ * (We don't use them on macOS; Apple provides their own, which *doesn't*
+ * avoid the bswap instruction, as macOS only supports machines that
* have it.)
*/
#if defined(__GNUC__) && defined(__i386__) && !defined(__APPLE__) && !defined(__ntohl)
#define DIAG_JOINSTR(x,y) XSTRINGIFY(x ## y)
#define DIAG_DO_PRAGMA(x) _Pragma (#x)
-#if defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
+/*
+ * The current clang compilers also define __GNUC__ and __GNUC_MINOR__
+ * thus we need to test the clang case before the GCC one
+ */
+#if defined(__clang__)
+# if (__clang_major__ * 100) + __clang_minor__ >= 208
+# define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(clang diagnostic x)
+# define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
+# define DIAG_ON(x) DIAG_PRAGMA(pop)
+# else
+# define DIAG_OFF(x)
+# define DIAG_ON(x)
+# endif
+#elif defined(__GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 402
# define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(GCC diagnostic x)
# if ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406
# define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
# define DIAG_OFF(x) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
# define DIAG_ON(x) DIAG_PRAGMA(warning DIAG_JOINSTR(-W,x))
# endif
-#elif defined(__clang__) && ((__clang_major__ * 100) + __clang_minor__ >= 208)
-# define DIAG_PRAGMA(x) DIAG_DO_PRAGMA(clang diagnostic x)
-# define DIAG_OFF(x) DIAG_PRAGMA(push) DIAG_PRAGMA(ignored DIAG_JOINSTR(-W,x))
-# define DIAG_ON(x) DIAG_PRAGMA(pop)
#else
# define DIAG_OFF(x)
# define DIAG_ON(x)
#endif
+/* Use for clang specific warnings */
+#ifdef __clang__
+# define DIAG_OFF_CLANG(x) DIAG_OFF(x)
+# define DIAG_ON_CLANG(x) DIAG_ON(x)
+#else
+# define DIAG_OFF_CLANG(x)
+# define DIAG_ON_CLANG(x)
+#endif
+
/*
* For dealing with APIs which are only deprecated in OSX (like the OpenSSL API)
*/
*/
#include "funcattrs.h"
-#ifndef min
-#define min(a,b) ((a)>(b)?(b):(a))
-#endif
-#ifndef max
-#define max(a,b) ((b)>(a)?(b):(a))
-#endif
-
-#ifdef __ATTRIBUTE___FALLTHROUGH_OK
+/*
+ * Statement attributes, for various compilers.
+ *
+ * This was introduced sufficiently recently that compilers implementing
+ * it also implement __has_attribute() (for example, GCC 5.0 and later
+ * have __has_attribute(), and the "fallthrough" attribute was introduced
+ * in GCC 7).
+ */
+#if __has_attribute(fallthrough)
# define ND_FALL_THROUGH __attribute__ ((fallthrough))
#else
# define ND_FALL_THROUGH
-#endif /* __ATTRIBUTE___FALLTHROUGH_OK */
+#endif /* __has_attribute(fallthrough) */
#endif /* netdissect_stdinc_h */