Coverage Report

Created: 2025-08-03 06:36

/src/frr/lib/network.h
Line
Count
Source (jump to first uncovered line)
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
0
  (((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
199
{
101
  /* coverity[dont_call] */
102
199
  return random();
103
199
}
Unexecuted instantiation: ospf_interface.c:frr_weak_random
ospf_lsa.c:frr_weak_random
Line
Count
Source
100
185
{
101
  /* coverity[dont_call] */
102
185
  return random();
103
185
}
Unexecuted instantiation: ospf_nsm.c:frr_weak_random
Unexecuted instantiation: ospf_sr.c:frr_weak_random
Unexecuted instantiation: ospf_te.c:frr_weak_random
Unexecuted instantiation: ospf_zebra.c:frr_weak_random
Unexecuted instantiation: ospf_ext.c:frr_weak_random
Unexecuted instantiation: ospf_api.c:frr_weak_random
Unexecuted instantiation: buffer.c:frr_weak_random
Unexecuted instantiation: command.c:frr_weak_random
Unexecuted instantiation: libfrr.c:frr_weak_random
Unexecuted instantiation: mgmt_be_client.c:frr_weak_random
Unexecuted instantiation: mgmt_fe_client.c:frr_weak_random
Unexecuted instantiation: mgmt_msg.c:frr_weak_random
Unexecuted instantiation: network.c:frr_weak_random
Unexecuted instantiation: pid_output.c:frr_weak_random
qobj.c:frr_weak_random
Line
Count
Source
100
14
{
101
  /* coverity[dont_call] */
102
14
  return random();
103
14
}
Unexecuted instantiation: skiplist.c:frr_weak_random
Unexecuted instantiation: stream.c:frr_weak_random
Unexecuted instantiation: event.c:frr_weak_random
Unexecuted instantiation: typesafe.c:frr_weak_random
Unexecuted instantiation: vty.c:frr_weak_random
Unexecuted instantiation: zclient.c:frr_weak_random
Unexecuted instantiation: zlog_live.c:frr_weak_random
Unexecuted instantiation: label_manager.c:frr_weak_random
Unexecuted instantiation: router-id.c:frr_weak_random
Unexecuted instantiation: table_manager.c:frr_weak_random
Unexecuted instantiation: zapi_msg.c:frr_weak_random
Unexecuted instantiation: zebra_gr.c:frr_weak_random
Unexecuted instantiation: zebra_srv6.c:frr_weak_random
Unexecuted instantiation: zebra_netns_id.c:frr_weak_random
Unexecuted instantiation: zebra_ptm.c:frr_weak_random
Unexecuted instantiation: zserv.c:frr_weak_random
Unexecuted instantiation: bgp_io.c:frr_weak_random
Unexecuted instantiation: bgp_network.c:frr_weak_random
Unexecuted instantiation: bgp_nexthop.c:frr_weak_random
Unexecuted instantiation: bgp_nht.c:frr_weak_random
Unexecuted instantiation: bgp_packet.c:frr_weak_random
Unexecuted instantiation: bgp_routemap.c:frr_weak_random
Unexecuted instantiation: bgp_updgrp.c:frr_weak_random
Unexecuted instantiation: bgp_updgrp_packet.c:frr_weak_random
Unexecuted instantiation: bgp_zebra.c:frr_weak_random
Unexecuted instantiation: bgpd.c:frr_weak_random
Unexecuted instantiation: bgp_label.c:frr_weak_random
Unexecuted instantiation: pim_iface.c:frr_weak_random
Unexecuted instantiation: pim_mroute.c:frr_weak_random
Unexecuted instantiation: pim_nht.c:frr_weak_random
Unexecuted instantiation: pim_pim.c:frr_weak_random
Unexecuted instantiation: pim_rp.c:frr_weak_random
Unexecuted instantiation: pim_sock.c:frr_weak_random
Unexecuted instantiation: pim_ssmpingd.c:frr_weak_random
Unexecuted instantiation: pim_upstream.c:frr_weak_random
Unexecuted instantiation: pim_zebra.c:frr_weak_random
Unexecuted instantiation: pim_zlookup.c:frr_weak_random
Unexecuted instantiation: pim_msdp_packet.c:frr_weak_random
Unexecuted instantiation: pim_msdp_socket.c:frr_weak_random
104
105
#ifdef __cplusplus
106
}
107
#endif
108
109
#endif /* _ZEBRA_NETWORK_H */