1 | /* |
2 | SPDX-FileCopyrightText: 2012-2013 Jan Grulich <jgrulich@redhat.com> |
3 | |
4 | SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL |
5 | */ |
6 | |
7 | #include "wiredsetting.h" |
8 | #include "manager.h" |
9 | #include "wiredsetting_p.h" |
10 | |
11 | #include <QDebug> |
12 | |
13 | #define NM_SETTING_WIRED_ASSIGNED_MAC_ADDRESS "assigned-mac-address" |
14 | |
15 | NetworkManager::WiredSettingPrivate::WiredSettingPrivate() |
16 | : name(NM_SETTING_WIRED_SETTING_NAME) |
17 | , port(NetworkManager::WiredSetting::UnknownPort) |
18 | , speed(0) |
19 | , duplex(NetworkManager::WiredSetting::UnknownDuplexType) |
20 | , mtu(0) |
21 | , s390NetType(NetworkManager::WiredSetting::Undefined) |
22 | , wakeOnLan(NetworkManager::WiredSetting::WakeOnLanDefault) |
23 | { |
24 | if (NetworkManager::checkVersion(x: 1, y: 6, z: 0)) { |
25 | autoNegotiate = false; |
26 | } else { |
27 | autoNegotiate = true; |
28 | } |
29 | } |
30 | |
31 | NetworkManager::WiredSetting::WiredSetting() |
32 | : Setting(Setting::Wired) |
33 | , d_ptr(new WiredSettingPrivate()) |
34 | { |
35 | } |
36 | |
37 | NetworkManager::WiredSetting::WiredSetting(const WiredSetting::Ptr &other) |
38 | : Setting(other) |
39 | , d_ptr(new WiredSettingPrivate()) |
40 | { |
41 | setPort(other->port()); |
42 | setSpeed(other->speed()); |
43 | setDuplexType(other->duplexType()); |
44 | setAutoNegotiate(other->autoNegotiate()); |
45 | setGenerateMacAddressMask(other->generateMacAddressMask()); |
46 | setMacAddress(other->macAddress()); |
47 | setClonedMacAddress(other->clonedMacAddress()); |
48 | setMacAddressBlacklist(other->macAddressBlacklist()); |
49 | setMtu(other->mtu()); |
50 | setS390Subchannels(other->s390Subchannels()); |
51 | setS390NetType(other->s390NetType()); |
52 | setS390Options(other->s390Options()); |
53 | setWakeOnLan(other->wakeOnLan()); |
54 | setWakeOnLanPassword(other->wakeOnLanPassword()); |
55 | setAssignedMacAddress(other->assignedMacAddress()); |
56 | } |
57 | |
58 | NetworkManager::WiredSetting::~WiredSetting() |
59 | { |
60 | delete d_ptr; |
61 | } |
62 | |
63 | QString NetworkManager::WiredSetting::name() const |
64 | { |
65 | Q_D(const WiredSetting); |
66 | |
67 | return d->name; |
68 | } |
69 | |
70 | void NetworkManager::WiredSetting::setPort(NetworkManager::WiredSetting::PortType port) |
71 | { |
72 | Q_D(WiredSetting); |
73 | |
74 | d->port = port; |
75 | } |
76 | |
77 | NetworkManager::WiredSetting::PortType NetworkManager::WiredSetting::port() const |
78 | { |
79 | Q_D(const WiredSetting); |
80 | |
81 | return d->port; |
82 | } |
83 | |
84 | void NetworkManager::WiredSetting::setSpeed(quint32 speed) |
85 | { |
86 | Q_D(WiredSetting); |
87 | |
88 | d->speed = speed; |
89 | } |
90 | |
91 | quint32 NetworkManager::WiredSetting::speed() const |
92 | { |
93 | Q_D(const WiredSetting); |
94 | |
95 | return d->speed; |
96 | } |
97 | |
98 | void NetworkManager::WiredSetting::setDuplexType(NetworkManager::WiredSetting::DuplexType type) |
99 | { |
100 | Q_D(WiredSetting); |
101 | |
102 | d->duplex = type; |
103 | } |
104 | |
105 | NetworkManager::WiredSetting::DuplexType NetworkManager::WiredSetting::duplexType() const |
106 | { |
107 | Q_D(const WiredSetting); |
108 | |
109 | return d->duplex; |
110 | } |
111 | |
112 | void NetworkManager::WiredSetting::setAutoNegotiate(bool autoNegotiate) |
113 | { |
114 | Q_D(WiredSetting); |
115 | |
116 | d->autoNegotiate = autoNegotiate; |
117 | } |
118 | |
119 | bool NetworkManager::WiredSetting::autoNegotiate() const |
120 | { |
121 | Q_D(const WiredSetting); |
122 | |
123 | return d->autoNegotiate; |
124 | } |
125 | |
126 | QString NetworkManager::WiredSetting::generateMacAddressMask() const |
127 | { |
128 | Q_D(const WiredSetting); |
129 | |
130 | return d->generateMacAddressMask; |
131 | } |
132 | |
133 | void NetworkManager::WiredSetting::setGenerateMacAddressMask(const QString &mask) |
134 | { |
135 | Q_D(WiredSetting); |
136 | |
137 | d->generateMacAddressMask = mask; |
138 | } |
139 | |
140 | void NetworkManager::WiredSetting::setMacAddress(const QByteArray &address) |
141 | { |
142 | Q_D(WiredSetting); |
143 | |
144 | d->macAddress = address; |
145 | } |
146 | |
147 | QByteArray NetworkManager::WiredSetting::macAddress() const |
148 | { |
149 | Q_D(const WiredSetting); |
150 | |
151 | return d->macAddress; |
152 | } |
153 | |
154 | void NetworkManager::WiredSetting::setClonedMacAddress(const QByteArray &address) |
155 | { |
156 | Q_D(WiredSetting); |
157 | |
158 | d->clonedMacAddress = address; |
159 | } |
160 | |
161 | QByteArray NetworkManager::WiredSetting::clonedMacAddress() const |
162 | { |
163 | Q_D(const WiredSetting); |
164 | |
165 | return d->clonedMacAddress; |
166 | } |
167 | |
168 | void NetworkManager::WiredSetting::setMacAddressBlacklist(const QStringList &list) |
169 | { |
170 | Q_D(WiredSetting); |
171 | |
172 | d->macAddressBlacklist = list; |
173 | } |
174 | |
175 | QStringList NetworkManager::WiredSetting::macAddressBlacklist() const |
176 | { |
177 | Q_D(const WiredSetting); |
178 | |
179 | return d->macAddressBlacklist; |
180 | } |
181 | |
182 | void NetworkManager::WiredSetting::setMtu(quint32 mtu) |
183 | { |
184 | Q_D(WiredSetting); |
185 | |
186 | d->mtu = mtu; |
187 | } |
188 | |
189 | quint32 NetworkManager::WiredSetting::mtu() const |
190 | { |
191 | Q_D(const WiredSetting); |
192 | |
193 | return d->mtu; |
194 | } |
195 | |
196 | void NetworkManager::WiredSetting::setS390Subchannels(const QStringList &channels) |
197 | { |
198 | Q_D(WiredSetting); |
199 | |
200 | d->s390Subchannels = channels; |
201 | } |
202 | |
203 | QStringList NetworkManager::WiredSetting::s390Subchannels() const |
204 | { |
205 | Q_D(const WiredSetting); |
206 | |
207 | return d->s390Subchannels; |
208 | } |
209 | |
210 | void NetworkManager::WiredSetting::setS390NetType(NetworkManager::WiredSetting::S390Nettype type) |
211 | { |
212 | Q_D(WiredSetting); |
213 | |
214 | d->s390NetType = type; |
215 | } |
216 | |
217 | NetworkManager::WiredSetting::S390Nettype NetworkManager::WiredSetting::s390NetType() const |
218 | { |
219 | Q_D(const WiredSetting); |
220 | |
221 | return d->s390NetType; |
222 | } |
223 | |
224 | void NetworkManager::WiredSetting::setS390Options(const QMap<QString, QString> &options) |
225 | { |
226 | Q_D(WiredSetting); |
227 | |
228 | d->s390Options = options; |
229 | } |
230 | |
231 | QMap<QString, QString> NetworkManager::WiredSetting::s390Options() const |
232 | { |
233 | Q_D(const WiredSetting); |
234 | |
235 | return d->s390Options; |
236 | } |
237 | |
238 | NetworkManager::WiredSetting::WakeOnLanFlags NetworkManager::WiredSetting::wakeOnLan() const |
239 | { |
240 | Q_D(const WiredSetting); |
241 | |
242 | return d->wakeOnLan; |
243 | } |
244 | |
245 | void NetworkManager::WiredSetting::setWakeOnLan(NetworkManager::WiredSetting::WakeOnLanFlags wol) |
246 | { |
247 | Q_D(WiredSetting); |
248 | |
249 | d->wakeOnLan = wol; |
250 | } |
251 | |
252 | QString NetworkManager::WiredSetting::wakeOnLanPassword() const |
253 | { |
254 | Q_D(const WiredSetting); |
255 | |
256 | return d->wakeOnLanPassword; |
257 | } |
258 | |
259 | void NetworkManager::WiredSetting::setWakeOnLanPassword(const QString &password) |
260 | { |
261 | Q_D(WiredSetting); |
262 | |
263 | d->wakeOnLanPassword = password; |
264 | } |
265 | |
266 | void NetworkManager::WiredSetting::setAssignedMacAddress(const QString &assignedMacAddress) |
267 | { |
268 | Q_D(WiredSetting); |
269 | |
270 | d->assignedMacAddress = assignedMacAddress; |
271 | } |
272 | |
273 | QString NetworkManager::WiredSetting::assignedMacAddress() const |
274 | { |
275 | Q_D(const WiredSetting); |
276 | |
277 | return d->assignedMacAddress; |
278 | } |
279 | |
280 | void NetworkManager::WiredSetting::fromMap(const QVariantMap &setting) |
281 | { |
282 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRED_PORT))) { |
283 | const QString port = setting.value(key: QLatin1String(NM_SETTING_WIRED_PORT)).toString(); |
284 | |
285 | if (port == "tp" ) { |
286 | setPort(Tp); |
287 | } else if (port == "aui" ) { |
288 | setPort(Aui); |
289 | } else if (port == "bnc" ) { |
290 | setPort(Bnc); |
291 | } else if (port == "mii" ) { |
292 | setPort(Mii); |
293 | } |
294 | } |
295 | |
296 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRED_SPEED))) { |
297 | setSpeed(setting.value(key: QLatin1String(NM_SETTING_WIRED_SPEED)).toUInt()); |
298 | } |
299 | |
300 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRED_DUPLEX))) { |
301 | const QString duplex = setting.value(key: QLatin1String(NM_SETTING_WIRED_DUPLEX)).toString(); |
302 | |
303 | if (duplex == "half" ) { |
304 | setDuplexType(Half); |
305 | } else if (duplex == "full" ) { |
306 | setDuplexType(Full); |
307 | } |
308 | } |
309 | |
310 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRED_AUTO_NEGOTIATE))) { |
311 | setAutoNegotiate(setting.value(key: QLatin1String(NM_SETTING_WIRED_AUTO_NEGOTIATE)).toBool()); |
312 | } |
313 | |
314 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRED_GENERATE_MAC_ADDRESS_MASK))) { |
315 | setGenerateMacAddressMask(setting.value(key: QLatin1String(NM_SETTING_WIRED_GENERATE_MAC_ADDRESS_MASK)).toString()); |
316 | } |
317 | |
318 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRED_MAC_ADDRESS))) { |
319 | setMacAddress(setting.value(key: QLatin1String(NM_SETTING_WIRED_MAC_ADDRESS)).toByteArray()); |
320 | } |
321 | |
322 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRED_CLONED_MAC_ADDRESS))) { |
323 | setClonedMacAddress(setting.value(key: QLatin1String(NM_SETTING_WIRED_CLONED_MAC_ADDRESS)).toByteArray()); |
324 | } |
325 | |
326 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRED_MAC_ADDRESS_BLACKLIST))) { |
327 | setMacAddressBlacklist(setting.value(key: QLatin1String(NM_SETTING_WIRED_MAC_ADDRESS_BLACKLIST)).toStringList()); |
328 | } |
329 | |
330 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRED_MTU))) { |
331 | setMtu(setting.value(key: QLatin1String(NM_SETTING_WIRED_MTU)).toUInt()); |
332 | } |
333 | |
334 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRED_S390_SUBCHANNELS))) { |
335 | setS390Subchannels(setting.value(key: QLatin1String(NM_SETTING_WIRED_S390_SUBCHANNELS)).toStringList()); |
336 | } |
337 | |
338 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRED_S390_NETTYPE))) { |
339 | const QString nettype = setting.value(key: QLatin1String(NM_SETTING_WIRED_S390_NETTYPE)).toString(); |
340 | |
341 | if (nettype == "qeth" ) { |
342 | setS390NetType(Qeth); |
343 | } else if (nettype == "lcs" ) { |
344 | setS390NetType(Lcs); |
345 | } else if (nettype == "ctc" ) { |
346 | setS390NetType(Ctc); |
347 | } |
348 | } |
349 | |
350 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRED_S390_OPTIONS))) { |
351 | QMap<QString, QString> tmp; |
352 | if (setting.value(key: QLatin1String(NM_SETTING_WIRED_S390_OPTIONS)).canConvert<QDBusArgument>()) { |
353 | QDBusArgument arg = setting.value(key: QLatin1String(NM_SETTING_WIRED_S390_OPTIONS)).value<QDBusArgument>(); |
354 | tmp = qdbus_cast<NMStringMap>(arg); |
355 | } else { |
356 | tmp = setting.value(key: QLatin1String(NM_SETTING_WIRED_S390_OPTIONS)).value<NMStringMap>(); |
357 | } |
358 | setS390Options(tmp); |
359 | } |
360 | |
361 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRED_WAKE_ON_LAN))) { |
362 | setWakeOnLan((NetworkManager::WiredSetting::WakeOnLanFlags)setting.value(key: QLatin1String(NM_SETTING_WIRED_WAKE_ON_LAN)).toUInt()); |
363 | } |
364 | |
365 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRED_WAKE_ON_LAN_PASSWORD))) { |
366 | setWakeOnLanPassword(setting.value(key: QLatin1String(NM_SETTING_WIRED_WAKE_ON_LAN_PASSWORD)).toString()); |
367 | } |
368 | |
369 | if (setting.contains(key: QLatin1String(NM_SETTING_WIRED_ASSIGNED_MAC_ADDRESS))) { |
370 | setAssignedMacAddress(setting.value(key: QLatin1String(NM_SETTING_WIRED_ASSIGNED_MAC_ADDRESS)).toString()); |
371 | } |
372 | } |
373 | |
374 | QVariantMap NetworkManager::WiredSetting::toMap() const |
375 | { |
376 | QVariantMap setting; |
377 | |
378 | switch (port()) { |
379 | case Tp: |
380 | setting.insert(key: QLatin1String(NM_SETTING_WIRED_PORT), value: "tp" ); |
381 | break; |
382 | case Aui: |
383 | setting.insert(key: QLatin1String(NM_SETTING_WIRED_PORT), value: "aui" ); |
384 | break; |
385 | case Bnc: |
386 | setting.insert(key: QLatin1String(NM_SETTING_WIRED_PORT), value: "bnc" ); |
387 | break; |
388 | case Mii: |
389 | setting.insert(key: QLatin1String(NM_SETTING_WIRED_PORT), value: "mii" ); |
390 | break; |
391 | case UnknownPort: // Make GCC hapy |
392 | break; |
393 | } |
394 | |
395 | // When autonegotiation is set we have to avoid setting speed and duplex, while |
396 | // when autonegotiation is disabled, we have to specify both |
397 | if (autoNegotiate()) { |
398 | setting.insert(key: QLatin1String(NM_SETTING_WIRED_AUTO_NEGOTIATE), value: autoNegotiate()); |
399 | } else { |
400 | setting.insert(key: QLatin1String(NM_SETTING_WIRED_AUTO_NEGOTIATE), value: autoNegotiate()); |
401 | setting.insert(key: QLatin1String(NM_SETTING_WIRED_SPEED), value: speed()); |
402 | |
403 | switch (duplexType()) { |
404 | case Half: |
405 | setting.insert(key: QLatin1String(NM_SETTING_WIRED_DUPLEX), value: "half" ); |
406 | break; |
407 | case Full: |
408 | setting.insert(key: QLatin1String(NM_SETTING_WIRED_DUPLEX), value: "full" ); |
409 | break; |
410 | case UnknownDuplexType: |
411 | break; |
412 | } |
413 | } |
414 | |
415 | if (!generateMacAddressMask().isEmpty()) { |
416 | setting.insert(key: QLatin1String(NM_SETTING_WIRED_GENERATE_MAC_ADDRESS_MASK), value: generateMacAddressMask()); |
417 | } |
418 | |
419 | if (!macAddress().isEmpty()) { |
420 | setting.insert(key: QLatin1String(NM_SETTING_WIRED_MAC_ADDRESS), value: macAddress()); |
421 | } |
422 | if (!clonedMacAddress().isEmpty()) { |
423 | setting.insert(key: QLatin1String(NM_SETTING_WIRED_CLONED_MAC_ADDRESS), value: clonedMacAddress()); |
424 | } |
425 | if (!macAddressBlacklist().isEmpty()) { |
426 | setting.insert(key: QLatin1String(NM_SETTING_WIRED_MAC_ADDRESS_BLACKLIST), value: macAddressBlacklist()); |
427 | } |
428 | if (mtu()) { |
429 | setting.insert(key: QLatin1String(NM_SETTING_WIRED_MTU), value: mtu()); |
430 | } |
431 | if (!s390Subchannels().isEmpty()) { |
432 | setting.insert(key: QLatin1String(NM_SETTING_WIRED_S390_SUBCHANNELS), value: s390Subchannels()); |
433 | } |
434 | |
435 | switch (s390NetType()) { |
436 | case Qeth: |
437 | setting.insert(key: QLatin1String(NM_SETTING_WIRED_S390_NETTYPE), value: "qeth" ); |
438 | break; |
439 | case Lcs: |
440 | setting.insert(key: QLatin1String(NM_SETTING_WIRED_S390_NETTYPE), value: "lcs" ); |
441 | break; |
442 | case Ctc: |
443 | setting.insert(key: QLatin1String(NM_SETTING_WIRED_S390_NETTYPE), value: "ctc" ); |
444 | break; |
445 | case Undefined: |
446 | break; |
447 | } |
448 | |
449 | if (!s390Options().isEmpty()) { |
450 | setting.insert(key: QLatin1String(NM_SETTING_WIRED_S390_OPTIONS), value: QVariant::fromValue(value: s390Options())); |
451 | } |
452 | |
453 | setting.insert(key: QLatin1String(NM_SETTING_WIRED_WAKE_ON_LAN), value: (uint)wakeOnLan()); |
454 | |
455 | if (!wakeOnLanPassword().isEmpty()) { |
456 | setting.insert(key: QLatin1String(NM_SETTING_WIRED_WAKE_ON_LAN_PASSWORD), value: wakeOnLanPassword()); |
457 | } |
458 | |
459 | if (!assignedMacAddress().isEmpty()) { |
460 | setting.insert(key: QLatin1String(NM_SETTING_WIRED_ASSIGNED_MAC_ADDRESS), value: assignedMacAddress()); |
461 | } |
462 | |
463 | return setting; |
464 | } |
465 | |
466 | QDebug NetworkManager::operator<<(QDebug dbg, const NetworkManager::WiredSetting &setting) |
467 | { |
468 | dbg.nospace() << "type: " << setting.typeAsString(type: setting.type()) << '\n'; |
469 | dbg.nospace() << "initialized: " << !setting.isNull() << '\n'; |
470 | |
471 | dbg.nospace() << NM_SETTING_WIRED_PORT << ": " << setting.port() << '\n'; |
472 | dbg.nospace() << NM_SETTING_WIRED_SPEED << ": " << setting.speed() << '\n'; |
473 | dbg.nospace() << NM_SETTING_WIRED_DUPLEX << ": " << setting.duplexType() << '\n'; |
474 | dbg.nospace() << NM_SETTING_WIRED_AUTO_NEGOTIATE << ": " << setting.autoNegotiate() << '\n'; |
475 | dbg.nospace() << NM_SETTING_WIRED_GENERATE_MAC_ADDRESS_MASK << ": " << setting.generateMacAddressMask() << '\n'; |
476 | dbg.nospace() << NM_SETTING_WIRED_MAC_ADDRESS << ": " << setting.macAddress() << '\n'; |
477 | dbg.nospace() << NM_SETTING_WIRED_CLONED_MAC_ADDRESS << ": " << setting.clonedMacAddress() << '\n'; |
478 | dbg.nospace() << NM_SETTING_WIRED_MAC_ADDRESS_BLACKLIST << ": " << setting.macAddressBlacklist() << '\n'; |
479 | dbg.nospace() << NM_SETTING_WIRED_MTU << ": " << setting.mtu() << '\n'; |
480 | dbg.nospace() << NM_SETTING_WIRED_S390_SUBCHANNELS << ": " << setting.s390Subchannels() << '\n'; |
481 | dbg.nospace() << NM_SETTING_WIRED_S390_NETTYPE << ": " << setting.s390NetType() << '\n'; |
482 | dbg.nospace() << NM_SETTING_WIRED_S390_OPTIONS << ": " << setting.s390Options() << '\n'; |
483 | dbg.nospace() << NM_SETTING_WIRED_WAKE_ON_LAN << ": " << setting.wakeOnLan() << '\n'; |
484 | dbg.nospace() << NM_SETTING_WIRED_WAKE_ON_LAN_PASSWORD << ": " << setting.wakeOnLanPassword() << '\n'; |
485 | dbg.nospace() << NM_SETTING_WIRED_ASSIGNED_MAC_ADDRESS << ": " << setting.assignedMacAddress() << '\n'; |
486 | |
487 | return dbg.maybeSpace(); |
488 | } |
489 | |