1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-network.h"
4
5 #include "alloc-util.h"
6 #include "network-util.h"
7 #include "string-table.h"
8 #include "strv.h"
9
network_is_online(void)10 bool network_is_online(void) {
11 _cleanup_free_ char *online_state = NULL;
12 LinkOnlineState state;
13 int r;
14
15 r = sd_network_get_online_state(&online_state);
16 if (r < 0)
17 state = _LINK_ONLINE_STATE_INVALID;
18 else
19 state = link_online_state_from_string(online_state);
20
21 if (state >= LINK_ONLINE_STATE_PARTIAL)
22 return true;
23 else if (state < 0) {
24 _cleanup_free_ char *carrier_state = NULL, *addr_state = NULL;
25
26 r = sd_network_get_carrier_state(&carrier_state);
27 if (r < 0) /* if we don't know anything, we consider the system online */
28 return true;
29
30 r = sd_network_get_address_state(&addr_state);
31 if (r < 0) /* if we don't know anything, we consider the system online */
32 return true;
33
34 /* we don't know the online state for certain, so make an educated guess */
35 if (STR_IN_SET(carrier_state, "degraded-carrier", "carrier") &&
36 STR_IN_SET(addr_state, "routable", "degraded"))
37 return true;
38 }
39
40 return false;
41 }
42
43 static const char* const link_operstate_table[_LINK_OPERSTATE_MAX] = {
44 [LINK_OPERSTATE_MISSING] = "missing",
45 [LINK_OPERSTATE_OFF] = "off",
46 [LINK_OPERSTATE_NO_CARRIER] = "no-carrier",
47 [LINK_OPERSTATE_DORMANT] = "dormant",
48 [LINK_OPERSTATE_DEGRADED_CARRIER] = "degraded-carrier",
49 [LINK_OPERSTATE_CARRIER] = "carrier",
50 [LINK_OPERSTATE_DEGRADED] = "degraded",
51 [LINK_OPERSTATE_ENSLAVED] = "enslaved",
52 [LINK_OPERSTATE_ROUTABLE] = "routable",
53 };
54
55 DEFINE_STRING_TABLE_LOOKUP(link_operstate, LinkOperationalState);
56
57 static const char* const link_carrier_state_table[_LINK_CARRIER_STATE_MAX] = {
58 [LINK_CARRIER_STATE_OFF] = "off",
59 [LINK_CARRIER_STATE_NO_CARRIER] = "no-carrier",
60 [LINK_CARRIER_STATE_DORMANT] = "dormant",
61 [LINK_CARRIER_STATE_DEGRADED_CARRIER] = "degraded-carrier",
62 [LINK_CARRIER_STATE_CARRIER] = "carrier",
63 [LINK_CARRIER_STATE_ENSLAVED] = "enslaved",
64 };
65
66 DEFINE_STRING_TABLE_LOOKUP(link_carrier_state, LinkCarrierState);
67
68 static const char* const link_required_address_family_table[_ADDRESS_FAMILY_MAX] = {
69 [ADDRESS_FAMILY_NO] = "any",
70 [ADDRESS_FAMILY_IPV4] = "ipv4",
71 [ADDRESS_FAMILY_IPV6] = "ipv6",
72 [ADDRESS_FAMILY_YES] = "both",
73 };
74
75 DEFINE_STRING_TABLE_LOOKUP(link_required_address_family, AddressFamily);
76
77 static const char* const link_address_state_table[_LINK_ADDRESS_STATE_MAX] = {
78 [LINK_ADDRESS_STATE_OFF] = "off",
79 [LINK_ADDRESS_STATE_DEGRADED] = "degraded",
80 [LINK_ADDRESS_STATE_ROUTABLE] = "routable",
81 };
82
83 DEFINE_STRING_TABLE_LOOKUP(link_address_state, LinkAddressState);
84
85 static const char *const link_online_state_table[_LINK_ONLINE_STATE_MAX] = {
86 [LINK_ONLINE_STATE_OFFLINE] = "offline",
87 [LINK_ONLINE_STATE_PARTIAL] = "partial",
88 [LINK_ONLINE_STATE_ONLINE] = "online",
89 };
90
91 DEFINE_STRING_TABLE_LOOKUP(link_online_state, LinkOnlineState);
92
parse_operational_state_range(const char * str,LinkOperationalStateRange * out)93 int parse_operational_state_range(const char *str, LinkOperationalStateRange *out) {
94 LinkOperationalStateRange range = { _LINK_OPERSTATE_INVALID, _LINK_OPERSTATE_INVALID };
95 _cleanup_free_ const char *min = NULL;
96 const char *p;
97
98 assert(str);
99 assert(out);
100
101 p = strchr(str, ':');
102 if (p) {
103 min = strndup(str, p - str);
104
105 if (!isempty(p + 1)) {
106 range.max = link_operstate_from_string(p + 1);
107 if (range.max < 0)
108 return -EINVAL;
109 }
110 } else
111 min = strdup(str);
112
113 if (!min)
114 return -ENOMEM;
115
116 if (!isempty(min)) {
117 range.min = link_operstate_from_string(min);
118 if (range.min < 0)
119 return -EINVAL;
120 }
121
122 /* Fail on empty strings. */
123 if (range.min == _LINK_OPERSTATE_INVALID && range.max == _LINK_OPERSTATE_INVALID)
124 return -EINVAL;
125
126 if (range.min == _LINK_OPERSTATE_INVALID)
127 range.min = LINK_OPERSTATE_MISSING;
128 if (range.max == _LINK_OPERSTATE_INVALID)
129 range.max = LINK_OPERSTATE_ROUTABLE;
130
131 if (range.min > range.max)
132 return -EINVAL;
133
134 *out = range;
135
136 return 0;
137 }
138
network_link_get_operational_state(int ifindex,LinkOperationalState * ret)139 int network_link_get_operational_state(int ifindex, LinkOperationalState *ret) {
140 _cleanup_free_ char *str = NULL;
141 LinkOperationalState s;
142 int r;
143
144 assert(ifindex > 0);
145 assert(ret);
146
147 r = sd_network_link_get_operational_state(ifindex, &str);
148 if (r < 0)
149 return r;
150
151 s = link_operstate_from_string(str);
152 if (s < 0)
153 return s;
154
155 *ret = s;
156 return 0;
157 }
158