]>
Commit | Line | Data |
---|---|---|
1 | // SPDX-License-Identifier: GPL-2.0-or-later | |
2 | /* | |
3 | * Network library header. | |
4 | * Copyright (C) 1998 Kunihiro Ishiguro | |
5 | */ | |
6 | ||
7 | #ifndef _ZEBRA_NETWORK_H | |
8 | #define _ZEBRA_NETWORK_H | |
9 | ||
10 | #ifdef HAVE_SYS_ENDIAN_H | |
11 | #include <sys/endian.h> | |
12 | #endif | |
13 | #ifdef HAVE_ENDIAN_H | |
14 | #include <endian.h> | |
15 | #endif | |
16 | ||
17 | #ifdef __cplusplus | |
18 | extern "C" { | |
19 | #endif | |
20 | ||
21 | /* Both readn and writen are deprecated and will be removed. They are not | |
22 | suitable for use with non-blocking file descriptors. | |
23 | */ | |
24 | extern int readn(int, uint8_t *, int); | |
25 | extern int writen(int, const uint8_t *, int); | |
26 | ||
27 | /* Set the file descriptor to use non-blocking I/O. Returns 0 for success, | |
28 | -1 on error. */ | |
29 | extern int set_nonblocking(int fd); | |
30 | ||
31 | extern int set_cloexec(int fd); | |
32 | ||
33 | /* Does the I/O error indicate that the operation should be retried later? */ | |
34 | #define ERRNO_IO_RETRY(EN) \ | |
35 | (((EN) == EAGAIN) || ((EN) == EWOULDBLOCK) || ((EN) == EINTR)) | |
36 | ||
37 | extern float htonf(float); | |
38 | extern float ntohf(float); | |
39 | ||
40 | /* force type for be64toh/htobe64 to be uint64_t, *without* a direct cast | |
41 | * | |
42 | * this is a workaround for false-positive printfrr warnings from FRR's | |
43 | * frr-format GCC plugin that would be triggered from | |
44 | * { printfrr("%"PRIu64, (uint64_t)be64toh(...)); } | |
45 | * | |
46 | * the key element here is that "(uint64_t)expr" causes the warning, while | |
47 | * "({ uint64_t x = expr; x; })" does not. (The cast is the trigger, a | |
48 | * variable of the same type works correctly.) | |
49 | */ | |
50 | ||
51 | /* zap system definitions... */ | |
52 | #ifdef be64toh | |
53 | #undef be64toh | |
54 | #endif | |
55 | #ifdef htobe64 | |
56 | #undef htobe64 | |
57 | #endif | |
58 | ||
59 | #if BYTE_ORDER == LITTLE_ENDIAN | |
60 | #define be64toh(x) ({ uint64_t r = __builtin_bswap64(x); r; }) | |
61 | #define htobe64(x) ({ uint64_t r = __builtin_bswap64(x); r; }) | |
62 | #elif BYTE_ORDER == BIG_ENDIAN | |
63 | #define be64toh(x) ({ uint64_t r = (x); r; }) | |
64 | #define htobe64(x) ({ uint64_t r = (x); r; }) | |
65 | #else | |
66 | #error nobody expects the endianish inquisition. check OS endian.h headers. | |
67 | #endif | |
68 | ||
69 | /** | |
70 | * Generate a sequence number using monotonic clock with a same second call | |
71 | * protection to help guarantee a unique incremental sequence number that never | |
72 | * goes back (except when wrapping/overflow). | |
73 | * | |
74 | * **NOTE** this function is not thread safe since it uses `static` variable. | |
75 | * | |
76 | * This function and `frr_sequence32_next` should be used to initialize | |
77 | * sequence numbers without directly calling other `time_t` returning | |
78 | * functions because of `time_t` truncation warnings. | |
79 | * | |
80 | * \returns `uint64_t` number based on the monotonic clock. | |
81 | */ | |
82 | extern uint64_t frr_sequence_next(void); | |
83 | ||
84 | /** Same as `frr_sequence_next` but returns truncated number. */ | |
85 | extern uint32_t frr_sequence32_next(void); | |
86 | ||
87 | /** | |
88 | * Helper function that returns a random long value. The main purpose of | |
89 | * this function is to hide a `random()` call that gets flagged by coverity | |
90 | * scan and put it into one place. | |
91 | * | |
92 | * The main usage of this function should be for generating jitter or weak | |
93 | * random values for simple purposes. | |
94 | * | |
95 | * See 'man 3 random' for more information. | |
96 | * | |
97 | * \returns random long integer. | |
98 | */ | |
99 | static inline long frr_weak_random(void) | |
100 | { | |
101 | /* coverity[dont_call] */ | |
102 | return random(); | |
103 | } | |
104 | ||
105 | #ifdef __cplusplus | |
106 | } | |
107 | #endif | |
108 | ||
109 | #endif /* _ZEBRA_NETWORK_H */ |