summaryrefslogtreecommitdiff
path: root/lib/network.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/network.h')
-rw-r--r--lib/network.h36
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/network.h b/lib/network.h
index 4a9666984f..10ed917572 100644
--- a/lib/network.h
+++ b/lib/network.h
@@ -22,6 +22,13 @@
#ifndef _ZEBRA_NETWORK_H
#define _ZEBRA_NETWORK_H
+#ifdef HAVE_SYS_ENDIAN_H
+#include <sys/endian.h>
+#endif
+#ifdef HAVE_ENDIAN_H
+#include <endian.h>
+#endif
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -45,6 +52,35 @@ extern int set_cloexec(int fd);
extern float htonf(float);
extern float ntohf(float);
+/* force type for be64toh/htobe64 to be uint64_t, *without* a direct cast
+ *
+ * this is a workaround for false-positive printfrr warnings from FRR's
+ * frr-format GCC plugin that would be triggered from
+ * { printfrr("%"PRIu64, (uint64_t)be64toh(...)); }
+ *
+ * the key element here is that "(uint64_t)expr" causes the warning, while
+ * "({ uint64_t x = expr; x; })" does not. (The cast is the trigger, a
+ * variable of the same type works correctly.)
+ */
+
+/* zap system definitions... */
+#ifdef be64toh
+#undef be64toh
+#endif
+#ifdef htobe64
+#undef htobe64
+#endif
+
+#if BYTE_ORDER == LITTLE_ENDIAN
+#define be64toh(x) ({ uint64_t r = __builtin_bswap64(x); r; })
+#define htobe64(x) ({ uint64_t r = __builtin_bswap64(x); r; })
+#elif BYTE_ORDER == BIG_ENDIAN
+#define be64toh(x) ({ uint64_t r = (x); r; })
+#define htobe64(x) ({ uint64_t r = (x); r; })
+#else
+#error nobody expects the endianish inquisition. check OS endian.h headers.
+#endif
+
/**
* Helper function that returns a random long value. The main purpose of
* this function is to hide a `random()` call that gets flagged by coverity