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