GCC Code Coverage Report


Directory: ./
File: panels/network/panel-common.c
Date: 2024-05-04 07:58:27
Exec Total Coverage
Lines: 52 259 20.1%
Functions: 4 8 50.0%
Branches: 29 130 22.3%

Line Branch Exec Source
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2010 Richard Hughes <richard@hughsie.com>
4 * Copyright (C) 2012 Thomas Bechtold <thomasbechtold@jpberlin.de>
5 *
6 * Licensed under the GNU General Public License Version 2
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #include "config.h"
24
25 #include <glib/gi18n.h>
26
27 #include "panel-common.h"
28
29 static const gchar *
30 7 device_state_to_localized_string (NMDeviceState state)
31 {
32 7 const gchar *value = NULL;
33
34
2/10
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 5 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
7 switch (state) {
35 case NM_DEVICE_STATE_UNKNOWN:
36 /* TRANSLATORS: device status */
37 value = _("Status unknown");
38 break;
39 case NM_DEVICE_STATE_UNMANAGED:
40 /* TRANSLATORS: device status */
41 value = _("Unmanaged");
42 break;
43 case NM_DEVICE_STATE_UNAVAILABLE:
44 /* TRANSLATORS: device status */
45 value = _("Unavailable");
46 break;
47 5 case NM_DEVICE_STATE_DISCONNECTED:
48 5 value = NULL;
49 5 break;
50 case NM_DEVICE_STATE_PREPARE:
51 case NM_DEVICE_STATE_CONFIG:
52 case NM_DEVICE_STATE_IP_CONFIG:
53 case NM_DEVICE_STATE_IP_CHECK:
54 /* TRANSLATORS: device status */
55 value = _("Connecting");
56 break;
57 case NM_DEVICE_STATE_NEED_AUTH:
58 /* TRANSLATORS: device status */
59 value = _("Authentication required");
60 break;
61 2 case NM_DEVICE_STATE_ACTIVATED:
62 /* TRANSLATORS: device status */
63 2 value = _("Connected");
64 2 break;
65 case NM_DEVICE_STATE_DEACTIVATING:
66 /* TRANSLATORS: device status */
67 value = _("Disconnecting");
68 break;
69 case NM_DEVICE_STATE_FAILED:
70 /* TRANSLATORS: device status */
71 value = _("Connection failed");
72 break;
73 default:
74 /* TRANSLATORS: device status */
75 value = _("Status unknown (missing)");
76 break;
77 }
78 7 return value;
79 }
80
81 static const gchar *
82 15 device_state_reason_to_localized_string (NMDevice *device)
83 {
84 15 const gchar *value = NULL;
85 NMDeviceStateReason state_reason;
86
87 /* This only covers NMDeviceStateReasons that explain why a connection
88 * failed / can't be attempted, and aren't redundant with the state
89 * (eg, NM_DEVICE_STATE_REASON_CARRIER).
90 */
91
92 15 state_reason = nm_device_get_state_reason (device);
93
1/42
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✗ Branch 23 not taken.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 26 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
✗ Branch 37 not taken.
✗ Branch 38 not taken.
✗ Branch 39 not taken.
✗ Branch 40 not taken.
✓ Branch 41 taken 15 times.
15 switch (state_reason) {
94 case NM_DEVICE_STATE_REASON_CONFIG_FAILED:
95 /* TRANSLATORS: device status reason */
96 value = _("Configuration failed");
97 break;
98 case NM_DEVICE_STATE_REASON_IP_CONFIG_UNAVAILABLE:
99 /* TRANSLATORS: device status reason */
100 value = _("IP configuration failed");
101 break;
102 case NM_DEVICE_STATE_REASON_IP_CONFIG_EXPIRED:
103 /* TRANSLATORS: device status reason */
104 value = _("IP configuration expired");
105 break;
106 case NM_DEVICE_STATE_REASON_NO_SECRETS:
107 /* TRANSLATORS: device status reason */
108 value = _("Secrets were required, but not provided");
109 break;
110 case NM_DEVICE_STATE_REASON_SUPPLICANT_DISCONNECT:
111 /* TRANSLATORS: device status reason */
112 value = _("802.1x supplicant disconnected");
113 break;
114 case NM_DEVICE_STATE_REASON_SUPPLICANT_CONFIG_FAILED:
115 /* TRANSLATORS: device status reason */
116 value = _("802.1x supplicant configuration failed");
117 break;
118 case NM_DEVICE_STATE_REASON_SUPPLICANT_FAILED:
119 /* TRANSLATORS: device status reason */
120 value = _("802.1x supplicant failed");
121 break;
122 case NM_DEVICE_STATE_REASON_SUPPLICANT_TIMEOUT:
123 /* TRANSLATORS: device status reason */
124 value = _("802.1x supplicant took too long to authenticate");
125 break;
126 case NM_DEVICE_STATE_REASON_PPP_START_FAILED:
127 /* TRANSLATORS: device status reason */
128 value = _("PPP service failed to start");
129 break;
130 case NM_DEVICE_STATE_REASON_PPP_DISCONNECT:
131 /* TRANSLATORS: device status reason */
132 value = _("PPP service disconnected");
133 break;
134 case NM_DEVICE_STATE_REASON_PPP_FAILED:
135 /* TRANSLATORS: device status reason */
136 value = _("PPP failed");
137 break;
138 case NM_DEVICE_STATE_REASON_DHCP_START_FAILED:
139 /* TRANSLATORS: device status reason */
140 value = _("DHCP client failed to start");
141 break;
142 case NM_DEVICE_STATE_REASON_DHCP_ERROR:
143 /* TRANSLATORS: device status reason */
144 value = _("DHCP client error");
145 break;
146 case NM_DEVICE_STATE_REASON_DHCP_FAILED:
147 /* TRANSLATORS: device status reason */
148 value = _("DHCP client failed");
149 break;
150 case NM_DEVICE_STATE_REASON_SHARED_START_FAILED:
151 /* TRANSLATORS: device status reason */
152 value = _("Shared connection service failed to start");
153 break;
154 case NM_DEVICE_STATE_REASON_SHARED_FAILED:
155 /* TRANSLATORS: device status reason */
156 value = _("Shared connection service failed");
157 break;
158 case NM_DEVICE_STATE_REASON_AUTOIP_START_FAILED:
159 /* TRANSLATORS: device status reason */
160 value = _("AutoIP service failed to start");
161 break;
162 case NM_DEVICE_STATE_REASON_AUTOIP_ERROR:
163 /* TRANSLATORS: device status reason */
164 value = _("AutoIP service error");
165 break;
166 case NM_DEVICE_STATE_REASON_AUTOIP_FAILED:
167 /* TRANSLATORS: device status reason */
168 value = _("AutoIP service failed");
169 break;
170 case NM_DEVICE_STATE_REASON_MODEM_BUSY:
171 /* TRANSLATORS: device status reason */
172 value = _("Line busy");
173 break;
174 case NM_DEVICE_STATE_REASON_MODEM_NO_DIAL_TONE:
175 /* TRANSLATORS: device status reason */
176 value = _("No dial tone");
177 break;
178 case NM_DEVICE_STATE_REASON_MODEM_NO_CARRIER:
179 /* TRANSLATORS: device status reason */
180 value = _("No carrier could be established");
181 break;
182 case NM_DEVICE_STATE_REASON_MODEM_DIAL_TIMEOUT:
183 /* TRANSLATORS: device status reason */
184 value = _("Dialing request timed out");
185 break;
186 case NM_DEVICE_STATE_REASON_MODEM_DIAL_FAILED:
187 /* TRANSLATORS: device status reason */
188 value = _("Dialing attempt failed");
189 break;
190 case NM_DEVICE_STATE_REASON_MODEM_INIT_FAILED:
191 /* TRANSLATORS: device status reason */
192 value = _("Modem initialization failed");
193 break;
194 case NM_DEVICE_STATE_REASON_GSM_APN_FAILED:
195 /* TRANSLATORS: device status reason */
196 value = _("Failed to select the specified APN");
197 break;
198 case NM_DEVICE_STATE_REASON_GSM_REGISTRATION_NOT_SEARCHING:
199 /* TRANSLATORS: device status reason */
200 value = _("Not searching for networks");
201 break;
202 case NM_DEVICE_STATE_REASON_GSM_REGISTRATION_DENIED:
203 /* TRANSLATORS: device status reason */
204 value = _("Network registration denied");
205 break;
206 case NM_DEVICE_STATE_REASON_GSM_REGISTRATION_TIMEOUT:
207 /* TRANSLATORS: device status reason */
208 value = _("Network registration timed out");
209 break;
210 case NM_DEVICE_STATE_REASON_GSM_REGISTRATION_FAILED:
211 /* TRANSLATORS: device status reason */
212 value = _("Failed to register with the requested network");
213 break;
214 case NM_DEVICE_STATE_REASON_GSM_PIN_CHECK_FAILED:
215 /* TRANSLATORS: device status reason */
216 value = _("PIN check failed");
217 break;
218 case NM_DEVICE_STATE_REASON_FIRMWARE_MISSING:
219 /* TRANSLATORS: device status reason */
220 value = _("Firmware for the device may be missing");
221 break;
222 case NM_DEVICE_STATE_REASON_CONNECTION_REMOVED:
223 /* TRANSLATORS: device status reason */
224 value = _("Connection disappeared");
225 break;
226 case NM_DEVICE_STATE_REASON_CONNECTION_ASSUMED:
227 /* TRANSLATORS: device status reason */
228 value = _("Existing connection was assumed");
229 break;
230 case NM_DEVICE_STATE_REASON_MODEM_NOT_FOUND:
231 /* TRANSLATORS: device status reason */
232 value = _("Modem not found");
233 break;
234 case NM_DEVICE_STATE_REASON_BT_FAILED:
235 /* TRANSLATORS: device status reason */
236 value = _("Bluetooth connection failed");
237 break;
238 case NM_DEVICE_STATE_REASON_GSM_SIM_NOT_INSERTED:
239 /* TRANSLATORS: device status reason */
240 value = _("SIM Card not inserted");
241 break;
242 case NM_DEVICE_STATE_REASON_GSM_SIM_PIN_REQUIRED:
243 /* TRANSLATORS: device status reason */
244 value = _("SIM Pin required");
245 break;
246 case NM_DEVICE_STATE_REASON_GSM_SIM_PUK_REQUIRED:
247 /* TRANSLATORS: device status reason */
248 value = _("SIM Puk required");
249 break;
250 case NM_DEVICE_STATE_REASON_GSM_SIM_WRONG:
251 /* TRANSLATORS: device status reason */
252 value = _("SIM wrong");
253 break;
254 case NM_DEVICE_STATE_REASON_DEPENDENCY_FAILED:
255 /* TRANSLATORS: device status reason */
256 value = _("Connection dependency failed");
257 break;
258 15 default:
259 /* no StateReason to show */
260 15 value = "";
261 15 break;
262 }
263
264 15 return value;
265 }
266
267 static gchar *
268 get_mac_address_of_connection (NMConnection *connection)
269 {
270 if (!connection)
271 return NULL;
272
273 /* check the connection type */
274 if (nm_connection_is_type (connection,
275 NM_SETTING_WIRELESS_SETTING_NAME)) {
276 /* check wireless settings */
277 NMSettingWireless *s_wireless = nm_connection_get_setting_wireless (connection);
278 if (!s_wireless)
279 return NULL;
280 return g_strdup (nm_setting_wireless_get_mac_address (s_wireless));
281 } else if (nm_connection_is_type (connection,
282 NM_SETTING_WIRED_SETTING_NAME)) {
283 /* check wired settings */
284 NMSettingWired *s_wired = nm_connection_get_setting_wired (connection);
285 if (!s_wired)
286 return NULL;
287 return g_strdup (nm_setting_wired_get_mac_address (s_wired));
288 }
289 /* no MAC address found */
290 return NULL;
291 }
292
293 /* returns TRUE if both MACs are equal */
294 static gboolean
295 compare_mac_device_with_mac_connection (NMDevice *device,
296 NMConnection *connection)
297 {
298 const gchar *mac_dev = NULL;
299 g_autofree gchar *mac_conn = NULL;
300
301 mac_dev = nm_device_get_hw_address (device);
302 if (mac_dev == NULL)
303 return FALSE;
304
305 mac_conn = get_mac_address_of_connection (connection);
306 if (mac_conn == NULL)
307 return FALSE;
308
309 /* compare both MACs */
310 return g_strcmp0 (mac_dev, mac_conn) == 0;
311 }
312
313 gchar *
314 22 panel_device_status_to_localized_string (NMDevice *nm_device,
315 const gchar *speed)
316 {
317 NMDeviceState state;
318 GString *string;
319 22 const gchar *state_str = NULL, *reason_str = NULL;
320
321 22 string = g_string_new (NULL);
322
323 22 state = nm_device_get_state (nm_device);
324
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 7 times.
22 if (state == NM_DEVICE_STATE_UNAVAILABLE) {
325
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
15 if (nm_device_get_firmware_missing (nm_device)) {
326 /* TRANSLATORS: device status */
327 state_str = _("Firmware missing");
328
5/10
✗ Branch 1 not taken.
✓ Branch 2 taken 15 times.
✓ Branch 3 taken 15 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 15 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 15 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 15 times.
✗ Branch 10 not taken.
30 } else if (NM_IS_DEVICE_ETHERNET (nm_device) &&
329 15 !nm_device_ethernet_get_carrier (NM_DEVICE_ETHERNET (nm_device))) {
330 /* TRANSLATORS: device status */
331 15 state_str = _("Cable unplugged");
332 }
333 }
334
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 15 times.
22 if (!state_str)
335 7 state_str = device_state_to_localized_string (state);
336
2/2
✓ Branch 0 taken 17 times.
✓ Branch 1 taken 5 times.
22 if (state_str)
337 g_string_append (string, state_str);
338
339
3/4
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
22 if (state > NM_DEVICE_STATE_UNAVAILABLE && speed) {
340
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 5 times.
7 if (string->len)
341
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
4 g_string_append (string, " - ");
342 7 g_string_append (string, speed);
343
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
15 } else if (state == NM_DEVICE_STATE_UNAVAILABLE ||
344 state == NM_DEVICE_STATE_DISCONNECTED ||
345 state == NM_DEVICE_STATE_DEACTIVATING ||
346 state == NM_DEVICE_STATE_FAILED) {
347 15 reason_str = device_state_reason_to_localized_string (nm_device);
348
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15 times.
15 if (*reason_str) {
349 if (string->len)
350 g_string_append (string, " - ");
351 g_string_append (string, reason_str);
352 }
353 }
354
355 22 return g_string_free (string, FALSE);
356 }
357
358 NMConnection *
359 net_device_get_find_connection (NMClient *client, NMDevice *device)
360 {
361 GSList *list, *iterator;
362 NMConnection *connection = NULL;
363 NMActiveConnection *ac;
364
365 /* is the device available in a active connection? */
366 ac = nm_device_get_active_connection (device);
367 if (ac)
368 return (NMConnection*) nm_active_connection_get_connection (ac);
369
370 /* not found in active connections - check all available connections */
371 list = net_device_get_valid_connections (client, device);
372 if (list != NULL) {
373 /* if list has only one connection, use this connection */
374 if (g_slist_length (list) == 1) {
375 connection = list->data;
376 goto out;
377 }
378
379 /* is there connection with the MAC address of the device? */
380 for (iterator = list; iterator; iterator = iterator->next) {
381 connection = iterator->data;
382 if (compare_mac_device_with_mac_connection (device,
383 connection)) {
384 goto out;
385 }
386 }
387 }
388
389 /* no connection found for the given device */
390 connection = NULL;
391 out:
392 g_slist_free (list);
393 return connection;
394 }
395
396 GSList *
397 22 net_device_get_valid_connections (NMClient *client, NMDevice *device)
398 {
399 GSList *valid;
400 NMConnection *connection;
401 NMSettingConnection *s_con;
402 NMActiveConnection *active_connection;
403 const char *active_uuid;
404 const GPtrArray *all;
405 GPtrArray *filtered;
406 guint i;
407
408 22 all = nm_client_get_connections (client);
409 22 filtered = nm_device_filter_connections (device, all);
410
411 22 active_connection = nm_device_get_active_connection (device);
412
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 19 times.
22 active_uuid = active_connection ? nm_active_connection_get_uuid (active_connection) : NULL;
413
414 22 valid = NULL;
415
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 22 times.
31 for (i = 0; i < filtered->len; i++) {
416 9 connection = g_ptr_array_index (filtered, i);
417 9 s_con = nm_connection_get_setting_connection (connection);
418
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!s_con)
419 continue;
420
421
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
9 if (nm_setting_connection_get_master (s_con) &&
422 g_strcmp0 (nm_setting_connection_get_uuid (s_con), active_uuid) != 0)
423 continue;
424
425 9 valid = g_slist_prepend (valid, connection);
426 }
427 22 g_ptr_array_free (filtered, FALSE);
428
429 22 return g_slist_reverse (valid);
430 }
431
432 gchar *
433 net_device_get_ip6_addresses (NMIPConfig *ipv6_config)
434 {
435 g_autoptr(GPtrArray) ipv6 = NULL;
436 GPtrArray *addresses;
437
438 addresses = nm_ip_config_get_addresses (ipv6_config);
439 if (addresses->len == 0) {
440 return NULL;
441 }
442 ipv6 = g_ptr_array_sized_new (addresses->len + 1);
443
444 for (int i = 0; i < addresses->len; i++) {
445 g_ptr_array_add (ipv6, (char *) nm_ip_address_get_address (g_ptr_array_index (addresses, i)));
446 }
447 g_ptr_array_add (ipv6, NULL);
448 return g_strjoinv ("\n", (char **) ipv6->pdata);
449 }
450