Line data Source code
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 : * Generate a sequence number using monotonic clock with a same second call
86 : * protection to help guarantee a unique incremental sequence number that never
87 : * goes back (except when wrapping/overflow).
88 : *
89 : * **NOTE** this function is not thread safe since it uses `static` variable.
90 : *
91 : * This function and `frr_sequence32_next` should be used to initialize
92 : * sequence numbers without directly calling other `time_t` returning
93 : * functions because of `time_t` truncation warnings.
94 : *
95 : * \returns `uint64_t` number based on the monotonic clock.
96 : */
97 : extern uint64_t frr_sequence_next(void);
98 :
99 : /** Same as `frr_sequence_next` but returns truncated number. */
100 : extern uint32_t frr_sequence32_next(void);
101 :
102 : /**
103 : * Helper function that returns a random long value. The main purpose of
104 : * this function is to hide a `random()` call that gets flagged by coverity
105 : * scan and put it into one place.
106 : *
107 : * The main usage of this function should be for generating jitter or weak
108 : * random values for simple purposes.
109 : *
110 : * See 'man 3 random' for more information.
111 : *
112 : * \returns random long integer.
113 : */
114 64 : static inline long frr_weak_random(void)
115 : {
116 : /* coverity[dont_call] */
117 34 : return random();
118 : }
119 :
120 : #ifdef __cplusplus
121 : }
122 : #endif
123 :
124 : #endif /* _ZEBRA_NETWORK_H */
|