]> git.proxmox.com Git - mirror_frr.git/blob - lib/network.h
*: `frr-format` with unmodified GCC
[mirror_frr.git] / lib / network.h
1 /*
2 * Network library header.
3 * Copyright (C) 1998 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #ifndef _ZEBRA_NETWORK_H
23 #define _ZEBRA_NETWORK_H
24
25 #ifdef HAVE_SYS_ENDIAN_H
26 #include <sys/endian.h>
27 #endif
28 #ifdef HAVE_ENDIAN_H
29 #include <endian.h>
30 #endif
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 /* Both readn and writen are deprecated and will be removed. They are not
37 suitable for use with non-blocking file descriptors.
38 */
39 extern int readn(int, uint8_t *, int);
40 extern int writen(int, const uint8_t *, int);
41
42 /* Set the file descriptor to use non-blocking I/O. Returns 0 for success,
43 -1 on error. */
44 extern int set_nonblocking(int fd);
45
46 extern int set_cloexec(int fd);
47
48 /* Does the I/O error indicate that the operation should be retried later? */
49 #define ERRNO_IO_RETRY(EN) \
50 (((EN) == EAGAIN) || ((EN) == EWOULDBLOCK) || ((EN) == EINTR))
51
52 extern float htonf(float);
53 extern float ntohf(float);
54
55 /* force type for be64toh/htobe64 to be uint64_t, *without* a direct cast
56 *
57 * this is a workaround for false-positive printfrr warnings from FRR's
58 * frr-format GCC plugin that would be triggered from
59 * { printfrr("%"PRIu64, (uint64_t)be64toh(...)); }
60 *
61 * the key element here is that "(uint64_t)expr" causes the warning, while
62 * "({ uint64_t x = expr; x; })" does not. (The cast is the trigger, a
63 * variable of the same type works correctly.)
64 */
65
66 /* zap system definitions... */
67 #ifdef be64toh
68 #undef be64toh
69 #endif
70 #ifdef htobe64
71 #undef htobe64
72 #endif
73
74 #if BYTE_ORDER == LITTLE_ENDIAN
75 #define be64toh(x) ({ uint64_t r = __builtin_bswap64(x); r; })
76 #define htobe64(x) ({ uint64_t r = __builtin_bswap64(x); r; })
77 #elif BYTE_ORDER == BIG_ENDIAN
78 #define be64toh(x) ({ uint64_t r = (x); r; })
79 #define htobe64(x) ({ uint64_t r = (x); r; })
80 #else
81 #error nobody expects the endianish inquisition. check OS endian.h headers.
82 #endif
83
84 /**
85 * Helper function that returns a random long value. The main purpose of
86 * this function is to hide a `random()` call that gets flagged by coverity
87 * scan and put it into one place.
88 *
89 * The main usage of this function should be for generating jitter or weak
90 * random values for simple purposes.
91 *
92 * See 'man 3 random' for more information.
93 *
94 * \returns random long integer.
95 */
96 static inline long frr_weak_random(void)
97 {
98 /* coverity[dont_call] */
99 return random();
100 }
101
102 #ifdef __cplusplus
103 }
104 #endif
105
106 #endif /* _ZEBRA_NETWORK_H */