]> git.proxmox.com Git - mirror_frr.git/blame - lib/network.h
Merge pull request #13649 from donaldsharp/unlock_the_node_or_else
[mirror_frr.git] / lib / network.h
CommitLineData
acddc0ed 1// SPDX-License-Identifier: GPL-2.0-or-later
718e3744 2/*
3 * Network library header.
4 * Copyright (C) 1998 Kunihiro Ishiguro
718e3744 5 */
6
7#ifndef _ZEBRA_NETWORK_H
8#define _ZEBRA_NETWORK_H
9
f62de63c
DL
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
5e244469
RW
17#ifdef __cplusplus
18extern "C" {
19#endif
20
a269d613 21/* Both readn and writen are deprecated and will be removed. They are not
22 suitable for use with non-blocking file descriptors.
23 */
d7c0a89a
QY
24extern int readn(int, uint8_t *, int);
25extern int writen(int, const uint8_t *, int);
718e3744 26
a269d613 27/* Set the file descriptor to use non-blocking I/O. Returns 0 for success,
28 -1 on error. */
29extern int set_nonblocking(int fd);
30
2da59394
DL
31extern int set_cloexec(int fd);
32
d7e2a818 33/* Does the I/O error indicate that the operation should be retried later? */
d62a17ae 34#define ERRNO_IO_RETRY(EN) \
d7e2a818 35 (((EN) == EAGAIN) || ((EN) == EWOULDBLOCK) || ((EN) == EINTR))
36
d62a17ae 37extern float htonf(float);
38extern float ntohf(float);
16f1b9ee 39
f62de63c
DL
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
fce7f209
RZ
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 */
82extern uint64_t frr_sequence_next(void);
83
84/** Same as `frr_sequence_next` but returns truncated number. */
85extern uint32_t frr_sequence32_next(void);
86
3c191fb1
DL
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 */
99static inline long frr_weak_random(void)
100{
101 /* coverity[dont_call] */
102 return random();
103}
5920b3eb 104
5e244469
RW
105#ifdef __cplusplus
106}
107#endif
108
718e3744 109#endif /* _ZEBRA_NETWORK_H */