| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "env-util.h" |
| 4 |
#include "format-ifname.h" |
| 5 |
#include "network-common.h" |
| 6 |
#include "socket-util.h" |
| 7 |
#include "unaligned.h" |
| 8 |
|
| 9 |
int get_ifname(int ifindex, char **ifname) { |
| 10 |
assert(ifname); |
| 11 |
|
| 12 |
/* This sets ifname only when it is not set yet. */ |
| 13 |
|
| 14 |
if (*ifname) |
| 15 |
return 0; |
| 16 |
|
| 17 |
return format_ifname_alloc(ifindex, ifname); |
| 18 |
} |
| 19 |
|
| 20 |
usec_t unaligned_be32_sec_to_usec(const void *p, bool max_as_infinity) { |
| 21 |
uint32_t s = unaligned_read_be32(ASSERT_PTR(p)); |
| 22 |
|
| 23 |
if (s == UINT32_MAX && max_as_infinity) |
| 24 |
return USEC_INFINITY; |
| 25 |
|
| 26 |
return s * USEC_PER_SEC; |
| 27 |
} |
| 28 |
|
| 29 |
usec_t be32_sec_to_usec(be32_t t, bool max_as_infinity) { |
| 30 |
uint32_t s = be32toh(t); |
| 31 |
|
| 32 |
if (s == UINT32_MAX && max_as_infinity) |
| 33 |
return USEC_INFINITY; |
| 34 |
|
| 35 |
return s * USEC_PER_SEC; |
| 36 |
} |
| 37 |
|
| 38 |
usec_t be32_msec_to_usec(be32_t t, bool max_as_infinity) { |
| 39 |
uint32_t s = be32toh(t); |
| 40 |
|
| 41 |
if (s == UINT32_MAX && max_as_infinity) |
| 42 |
return USEC_INFINITY; |
| 43 |
|
| 44 |
return s * USEC_PER_MSEC; |
| 45 |
} |
| 46 |
|
| 47 |
usec_t be16_sec_to_usec(be16_t t, bool max_as_infinity) { |
| 48 |
uint16_t s = be16toh(t); |
| 49 |
|
| 50 |
if (s == UINT16_MAX && max_as_infinity) |
| 51 |
return USEC_INFINITY; |
| 52 |
|
| 53 |
return s * USEC_PER_SEC; |
| 54 |
} |
| 55 |
|
| 56 |
be32_t usec_to_be32_sec(usec_t t) { |
| 57 |
if (t == USEC_INFINITY) |
| 58 |
/* Some settings, e.g. a lifetime of an address, UINT32_MAX is handled as infinity. so let's |
| 59 |
* map USEC_INFINITY to UINT32_MAX. */ |
| 60 |
return htobe32(UINT32_MAX); |
| 61 |
|
| 62 |
if (t >= (UINT32_MAX - 1) * USEC_PER_SEC) |
| 63 |
/* Finite but too large. Let's use the largest (or off-by-one from the largest) finite value. */ |
| 64 |
return htobe32(UINT32_MAX - 1); |
| 65 |
|
| 66 |
return htobe32((uint32_t) DIV_ROUND_UP(t, USEC_PER_SEC)); |
| 67 |
} |
| 68 |
|
| 69 |
be32_t usec_to_be32_msec(usec_t t) { |
| 70 |
if (t == USEC_INFINITY) |
| 71 |
return htobe32(UINT32_MAX); |
| 72 |
|
| 73 |
if (t >= (UINT32_MAX - 1) * USEC_PER_MSEC) |
| 74 |
return htobe32(UINT32_MAX - 1); |
| 75 |
|
| 76 |
return htobe32((uint32_t) DIV_ROUND_UP(t, USEC_PER_MSEC)); |
| 77 |
} |
| 78 |
|
| 79 |
be16_t usec_to_be16_sec(usec_t t) { |
| 80 |
if (t == USEC_INFINITY) |
| 81 |
return htobe16(UINT16_MAX); |
| 82 |
|
| 83 |
if (t >= (UINT16_MAX - 1) * USEC_PER_SEC) |
| 84 |
return htobe16(UINT16_MAX - 1); |
| 85 |
|
| 86 |
return htobe16((uint16_t) DIV_ROUND_UP(t, USEC_PER_SEC)); |
| 87 |
} |
| 88 |
|
| 89 |
usec_t time_span_to_stamp(usec_t span, usec_t base) { |
| 90 |
/* Typically, 0 lifetime (timespan) indicates the corresponding configuration (address or so) must be |
| 91 |
* dropped. So, when the timespan is zero, here we return 0 rather than 'base'. This makes the caller |
| 92 |
* easily understand that the configuration needs to be dropped immediately. */ |
| 93 |
if (span == 0) |
| 94 |
return 0; |
| 95 |
|
| 96 |
return usec_add(base, span); |
| 97 |
} |
| 98 |
|
| 99 |
bool network_test_mode_enabled(void) { |
| 100 |
static int test_mode = -1; |
| 101 |
int r; |
| 102 |
|
| 103 |
if (test_mode < 0) { |
| 104 |
r = getenv_bool("SYSTEMD_NETWORK_TEST_MODE"); |
| 105 |
if (r < 0) { |
| 106 |
if (r != -ENXIO) |
| 107 |
log_debug_errno(r, "Failed to parse $SYSTEMD_NETWORK_TEST_MODE environment variable, ignoring: %m"); |
| 108 |
|
| 109 |
test_mode = false; |
| 110 |
} else |
| 111 |
test_mode = r; |
| 112 |
} |
| 113 |
|
| 114 |
return test_mode; |
| 115 |
} |
| 116 |
|
| 117 |
triple_timestamp* triple_timestamp_from_cmsg(triple_timestamp *t, struct msghdr *mh) { |
| 118 |
assert(t); |
| 119 |
assert(mh); |
| 120 |
|
| 121 |
struct timeval *tv = CMSG_FIND_AND_COPY_DATA(mh, SOL_SOCKET, SCM_TIMESTAMP, struct timeval); |
| 122 |
if (tv) |
| 123 |
return triple_timestamp_from_realtime(t, timeval_load(tv)); |
| 124 |
|
| 125 |
return triple_timestamp_now(t); |
| 126 |
} |