summaryrefslogtreecommitdiff
blob: e29cf2206aa2bdc6e68e7d024cfb1a7ffe8e85cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
From a18461bc7d446f8e130e9276de4397d00059267f Mon Sep 17 00:00:00 2001
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
Date: Mon, 29 Jan 2018 20:58:24 +0100
Subject: [PATCH 1/4] networkd: display wireguard devtype

It's not useful to simply show "none", when we have more interesting
information to display.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
 src/network/networkctl.c | 22 +++++++++++++++-------
 1 file changed, 15 insertions(+), 7 deletions(-)

diff --git a/src/network/networkctl.c b/src/network/networkctl.c
index 59ce098cd1..6ce00dff6d 100644
--- a/src/network/networkctl.c
+++ b/src/network/networkctl.c
@@ -62,18 +62,26 @@ static int link_get_type_string(unsigned short iftype, sd_device *d, char **ret)
 
         assert(ret);
 
-        if (iftype == ARPHRD_ETHER && d) {
+        if (d) {
                 const char *devtype = NULL, *id = NULL;
+
+                (void) sd_device_get_devtype(d, &devtype);
+
                 /* WLANs have iftype ARPHRD_ETHER, but we want
                  * to show a more useful type string for
                  * them */
+                if (iftype == ARPHRD_ETHER) {
+                        if (streq_ptr(devtype, "wlan"))
+                                id = "wlan";
+                        else if (streq_ptr(devtype, "wwan"))
+                                id = "wwan";
+                }
 
-                (void) sd_device_get_devtype(d, &devtype);
-
-                if (streq_ptr(devtype, "wlan"))
-                        id = "wlan";
-                else if (streq_ptr(devtype, "wwan"))
-                        id = "wwan";
+                /* Likewise, WireGuard has iftype ARPHRD_NONE,
+                 * since it's layer 3, but we of course want
+                 * something more useful than that. */
+                if (iftype == ARPHRD_NONE && streq_ptr(devtype, "wireguard"))
+                        id = "wireguard";
 
                 if (id) {
                         p = strdup(id);

From f119082e7a1ccfbf50c30a99819b6e303cdf09a1 Mon Sep 17 00:00:00 2001
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
Date: Mon, 29 Jan 2018 21:01:46 +0100
Subject: [PATCH 2/4] networkd: simplify and display all devtypes

Every place the kernel actually calls SET_NETDEV_DEVTYPE, it's adding a
piece of information that looks useful and relevant for us to use. So
let's use it when it's there.

The previous matching based on the corresponding ARPHRD didn't really
make much sense. The more sensible logic for getting a textual
representation of the link type is to see if the kernel supplies a
devtype. If it does, great. If not, then we can fall back on the ARPHRD,
as before.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
 src/network/networkctl.c | 23 +++--------------------
 1 file changed, 3 insertions(+), 20 deletions(-)

diff --git a/src/network/networkctl.c b/src/network/networkctl.c
index 6ce00dff6d..8a08304240 100644
--- a/src/network/networkctl.c
+++ b/src/network/networkctl.c
@@ -63,28 +63,11 @@ static int link_get_type_string(unsigned short iftype, sd_device *d, char **ret)
         assert(ret);
 
         if (d) {
-                const char *devtype = NULL, *id = NULL;
+                const char *devtype = NULL;
 
                 (void) sd_device_get_devtype(d, &devtype);
-
-                /* WLANs have iftype ARPHRD_ETHER, but we want
-                 * to show a more useful type string for
-                 * them */
-                if (iftype == ARPHRD_ETHER) {
-                        if (streq_ptr(devtype, "wlan"))
-                                id = "wlan";
-                        else if (streq_ptr(devtype, "wwan"))
-                                id = "wwan";
-                }
-
-                /* Likewise, WireGuard has iftype ARPHRD_NONE,
-                 * since it's layer 3, but we of course want
-                 * something more useful than that. */
-                if (iftype == ARPHRD_NONE && streq_ptr(devtype, "wireguard"))
-                        id = "wireguard";
-
-                if (id) {
-                        p = strdup(id);
+                if (!isempty(devtype)) {
+                        p = strdup(devtype);
                         if (!p)
                                 return -ENOMEM;
 

From fdce7817b9a27a370c01b7dd9da6a84fcae1038e Mon Sep 17 00:00:00 2001
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
Date: Mon, 29 Jan 2018 21:05:36 +0100
Subject: [PATCH 3/4] networkd: clean up link_get_type_string

The return value is always ignored, so get rid of it.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
 src/network/networkctl.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

diff --git a/src/network/networkctl.c b/src/network/networkctl.c
index 8a08304240..7b33e0db17 100644
--- a/src/network/networkctl.c
+++ b/src/network/networkctl.c
@@ -56,7 +56,7 @@ static bool arg_no_pager = false;
 static bool arg_legend = true;
 static bool arg_all = false;
 
-static int link_get_type_string(unsigned short iftype, sd_device *d, char **ret) {
+static void link_get_type_string(unsigned short iftype, sd_device *d, char **ret) {
         const char *t;
         char *p;
 
@@ -69,27 +69,25 @@ static int link_get_type_string(unsigned short iftype, sd_device *d, char **ret)
                 if (!isempty(devtype)) {
                         p = strdup(devtype);
                         if (!p)
-                                return -ENOMEM;
+                                return;
 
                         *ret = p;
-                        return 1;
+                        return;
                 }
         }
 
         t = arphrd_to_name(iftype);
         if (!t) {
                 *ret = NULL;
-                return 0;
+                return;
         }
 
         p = strdup(t);
         if (!p)
-                return -ENOMEM;
+                return;
 
         ascii_strlower(p);
         *ret = p;
-
-        return 0;
 }
 
 static void operational_state_to_color(const char *state, const char **on, const char **off) {
@@ -314,7 +312,7 @@ static int list_links(int argc, char *argv[], void *userdata) {
                 xsprintf(devid, "n%i", links[i].ifindex);
                 (void) sd_device_new_from_device_id(&d, devid);
 
-                (void) link_get_type_string(links[i].iftype, d, &t);
+                link_get_type_string(links[i].iftype, d, &t);
 
                 printf("%3i %-16s %-18s %s%-11s%s %s%-10s%s\n",
                        links[i].ifindex, links[i].name, strna(t),
@@ -807,7 +805,7 @@ static int link_status_one(
                         (void) sd_device_get_property_value(d, "ID_MODEL", &model);
         }
 
-        (void) link_get_type_string(info->iftype, d, &t);
+        link_get_type_string(info->iftype, d, &t);
 
         (void) sd_network_link_get_network_file(info->ifindex, &network);
 

From b55822c349d3e0559c1efc7475fd0f74cf086453 Mon Sep 17 00:00:00 2001
From: "Jason A. Donenfeld" <Jason@zx2c4.com>
Date: Mon, 29 Jan 2018 21:08:39 +0100
Subject: [PATCH 4/4] networkd: clean up link_get_type_string returns

It's cleaner and more consistent to actually return what we were
planning on returning.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
 src/network/networkctl.c | 28 +++++++++-------------------
 1 file changed, 9 insertions(+), 19 deletions(-)

diff --git a/src/network/networkctl.c b/src/network/networkctl.c
index 7b33e0db17..14d8ecb03f 100644
--- a/src/network/networkctl.c
+++ b/src/network/networkctl.c
@@ -56,38 +56,28 @@ static bool arg_no_pager = false;
 static bool arg_legend = true;
 static bool arg_all = false;
 
-static void link_get_type_string(unsigned short iftype, sd_device *d, char **ret) {
+static char *link_get_type_string(unsigned short iftype, sd_device *d) {
         const char *t;
         char *p;
 
-        assert(ret);
-
         if (d) {
                 const char *devtype = NULL;
 
                 (void) sd_device_get_devtype(d, &devtype);
-                if (!isempty(devtype)) {
-                        p = strdup(devtype);
-                        if (!p)
-                                return;
-
-                        *ret = p;
-                        return;
-                }
+                if (!isempty(devtype))
+                        return strdup(devtype);
         }
 
         t = arphrd_to_name(iftype);
-        if (!t) {
-                *ret = NULL;
-                return;
-        }
+        if (!t)
+                return NULL;
 
         p = strdup(t);
         if (!p)
-                return;
+                return NULL;
 
         ascii_strlower(p);
-        *ret = p;
+        return p;
 }
 
 static void operational_state_to_color(const char *state, const char **on, const char **off) {
@@ -312,7 +302,7 @@ static int list_links(int argc, char *argv[], void *userdata) {
                 xsprintf(devid, "n%i", links[i].ifindex);
                 (void) sd_device_new_from_device_id(&d, devid);
 
-                link_get_type_string(links[i].iftype, d, &t);
+                t = link_get_type_string(links[i].iftype, d);
 
                 printf("%3i %-16s %-18s %s%-11s%s %s%-10s%s\n",
                        links[i].ifindex, links[i].name, strna(t),
@@ -805,7 +795,7 @@ static int link_status_one(
                         (void) sd_device_get_property_value(d, "ID_MODEL", &model);
         }
 
-        link_get_type_string(info->iftype, d, &t);
+        t = link_get_type_string(info->iftype, d);
 
         (void) sd_network_link_get_network_file(info->ifindex, &network);