8#include "ipv4setting.h"
9#include "ipv4setting_p.h"
11#include "generictypes.h"
17NetworkManager::Ipv4SettingPrivate::Ipv4SettingPrivate()
18 : name(NMQT_SETTING_IP4_CONFIG_SETTING_NAME)
21 , ignoreAutoRoutes(false)
22 , ignoreAutoDns(false)
23 , dhcpSendHostname(true)
31NetworkManager::Ipv4Setting::Ipv4Setting()
32 : Setting(Setting::Ipv4)
33 , d_ptr(new Ipv4SettingPrivate())
37NetworkManager::Ipv4Setting::Ipv4Setting(
const Ptr &other)
39 , d_ptr(new Ipv4SettingPrivate())
41 setMethod(other->method());
43 setDnsSearch(other->dnsSearch());
44 setAddresses(other->addresses());
45 setRoutes(other->routes());
46 setRouteMetric(other->routeMetric());
47 setIgnoreAutoRoutes(other->ignoreAutoRoutes());
48 setIgnoreAutoDns(other->ignoreAutoDns());
49 setDhcpClientId(other->dhcpClientId());
50 setDhcpSendHostname(other->dhcpSendHostname());
51 setDhcpHostname(other->dhcpHostname());
52 setNeverDefault(other->neverDefault());
53 setMayFail(other->mayFail());
54 setDadTimeout(other->dadTimeout());
55 setDhcpFqdn(other->dhcpFqdn());
56 setDnsOptions(other->dnsOptions());
57 setDnsPriority(other->dnsPriority());
58 setAddressData(other->addressData());
59 setRouteData(other->routeData());
62NetworkManager::Ipv4Setting::~Ipv4Setting()
74void NetworkManager::Ipv4Setting::setMethod(NetworkManager::Ipv4Setting::ConfigMethod type)
81NetworkManager::Ipv4Setting::ConfigMethod NetworkManager::Ipv4Setting::method()
const
83 Q_D(
const Ipv4Setting);
97 Q_D(
const Ipv4Setting);
102void NetworkManager::Ipv4Setting::setDnsSearch(
const QStringList &domains)
106 d->dnsSearch = domains;
109QStringList NetworkManager::Ipv4Setting::dnsSearch()
const
111 Q_D(
const Ipv4Setting);
116void NetworkManager::Ipv4Setting::setAddresses(
const QList<IpAddress> &ipv4addresses)
120 d->addresses = ipv4addresses;
125 Q_D(
const Ipv4Setting);
130void NetworkManager::Ipv4Setting::setRoutes(
const QList<IpRoute> &ipv4routes)
134 d->routes = ipv4routes;
139 Q_D(
const Ipv4Setting);
144void NetworkManager::Ipv4Setting::setRouteMetric(
int metric)
148 d->routeMetric = metric;
151int NetworkManager::Ipv4Setting::routeMetric()
const
153 Q_D(
const Ipv4Setting);
155 return d->routeMetric;
158void NetworkManager::Ipv4Setting::setIgnoreAutoRoutes(
bool ignore)
162 d->ignoreAutoRoutes = ignore;
165bool NetworkManager::Ipv4Setting::ignoreAutoRoutes()
const
167 Q_D(
const Ipv4Setting);
169 return d->ignoreAutoRoutes;
172void NetworkManager::Ipv4Setting::setIgnoreAutoDns(
bool ignore)
176 d->ignoreAutoDns = ignore;
179bool NetworkManager::Ipv4Setting::ignoreAutoDns()
const
181 Q_D(
const Ipv4Setting);
183 return d->ignoreAutoDns;
186void NetworkManager::Ipv4Setting::setDhcpClientId(
const QString &
id)
190 d->dhcpClientId = id;
193QString NetworkManager::Ipv4Setting::dhcpClientId()
const
195 Q_D(
const Ipv4Setting);
197 return d->dhcpClientId;
200void NetworkManager::Ipv4Setting::setDhcpSendHostname(
bool send)
204 d->dhcpSendHostname = send;
207bool NetworkManager::Ipv4Setting::dhcpSendHostname()
const
209 Q_D(
const Ipv4Setting);
211 return d->dhcpSendHostname;
214void NetworkManager::Ipv4Setting::setDhcpHostname(
const QString &
hostname)
221QString NetworkManager::Ipv4Setting::dhcpHostname()
const
223 Q_D(
const Ipv4Setting);
225 return d->dhcpHostname;
228void NetworkManager::Ipv4Setting::setNeverDefault(
bool neverDefault)
232 d->neverDefault = neverDefault;
235bool NetworkManager::Ipv4Setting::neverDefault()
const
237 Q_D(
const Ipv4Setting);
239 return d->neverDefault;
242void NetworkManager::Ipv4Setting::setMayFail(
bool mayFail)
246 d->mayFail = mayFail;
249bool NetworkManager::Ipv4Setting::mayFail()
const
251 Q_D(
const Ipv4Setting);
256void NetworkManager::Ipv4Setting::setDadTimeout(qint32 timeout)
260 d->dadTimeout = timeout;
263qint32 NetworkManager::Ipv4Setting::dadTimeout()
const
265 Q_D(
const Ipv4Setting);
267 return d->dadTimeout;
270void NetworkManager::Ipv4Setting::setDhcpFqdn(
const QString &fqdn)
277QString NetworkManager::Ipv4Setting::dhcpFqdn()
const
279 Q_D(
const Ipv4Setting);
284void NetworkManager::Ipv4Setting::setDnsOptions(
const QStringList &options)
288 d->dnsOptions = options;
291QStringList NetworkManager::Ipv4Setting::dnsOptions()
const
293 Q_D(
const Ipv4Setting);
295 return d->dnsOptions;
298void NetworkManager::Ipv4Setting::setDnsPriority(qint32 priority)
302 d->dnsPriority = priority;
305qint32 NetworkManager::Ipv4Setting::dnsPriority()
const
307 Q_D(
const Ipv4Setting);
309 return d->dnsPriority;
312void NetworkManager::Ipv4Setting::setGateway(
const QString &gateway)
316 d->gateway = gateway;
319QString NetworkManager::Ipv4Setting::gateway()
const
321 Q_D(
const Ipv4Setting);
326void NetworkManager::Ipv4Setting::setAddressData(
const NMVariantMapList &addressData)
330 d->addressData = addressData;
335 Q_D(
const Ipv4Setting);
337 return d->addressData;
340void NetworkManager::Ipv4Setting::setRouteData(
const NMVariantMapList &routeData)
344 d->routeData = routeData;
349 Q_D(
const Ipv4Setting);
356 if (setting.contains(
QLatin1String(NMQT_SETTING_IP4_CONFIG_METHOD))) {
357 const QString methodType = setting.value(
QLatin1String(NMQT_SETTING_IP4_CONFIG_METHOD)).toString();
360 setMethod(Automatic);
362 setMethod(LinkLocal);
370 setMethod(Automatic);
374 if (setting.contains(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DNS))) {
377 if (setting.value(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DNS)).canConvert<QDBusArgument>()) {
379 temp = qdbus_cast<QList<uint>>(dnsArg);
384 for (
const uint utmp : std::as_const(temp)) {
392 if (setting.contains(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DNS_SEARCH))) {
393 setDnsSearch(setting.value(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DNS_SEARCH)).toStringList());
396 if (setting.contains(
QLatin1String(NMQT_SETTING_IP4_CONFIG_ADDRESSES))) {
399 if (setting.value(
QLatin1String(NMQT_SETTING_IP4_CONFIG_ADDRESSES)).canConvert<QDBusArgument>()) {
401 temp = qdbus_cast<QList<QList<uint>>>(addressArg);
406 for (
const QList<uint> &uintList : std::as_const(temp)) {
407 if (uintList.count() != 3) {
413 address.setPrefixLength(uintList.at(1));
414 address.setGateway(
QHostAddress(ntohl(uintList.at(2))));
415 if (!address.isValid()) {
419 addresses << address;
422 setAddresses(addresses);
425 if (setting.contains(
QLatin1String(NMQT_SETTING_IP4_CONFIG_ROUTES))) {
428 if (setting.value(
QLatin1String(NMQT_SETTING_IP4_CONFIG_ROUTES)).canConvert<QDBusArgument>()) {
430 temp = qdbus_cast<QList<QList<uint>>>(routeArg);
435 for (
const QList<uint> &uintList : std::as_const(temp)) {
436 if (uintList.count() != 4) {
444 route.
setMetric((quint32)uintList.at(3));
456 if (setting.contains(
QLatin1String(NMQT_SETTING_IP4_CONFIG_ROUTE_METRIC))) {
457 setRouteMetric(setting.value(
QLatin1String(NMQT_SETTING_IP4_CONFIG_ROUTE_METRIC)).toInt());
460 if (setting.contains(
QLatin1String(NMQT_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES))) {
461 setIgnoreAutoRoutes(setting.value(
QLatin1String(NMQT_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES)).toBool());
464 if (setting.contains(
QLatin1String(NMQT_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS))) {
465 setIgnoreAutoDns(setting.value(
QLatin1String(NMQT_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS)).toBool());
468 if (setting.contains(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DHCP_CLIENT_ID))) {
469 setDhcpClientId(setting.value(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DHCP_CLIENT_ID)).toString());
472 if (setting.contains(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DHCP_SEND_HOSTNAME))) {
473 setDhcpSendHostname(setting.value(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DHCP_SEND_HOSTNAME)).toBool());
476 if (setting.contains(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DHCP_HOSTNAME))) {
477 setDhcpHostname(setting.value(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DHCP_HOSTNAME)).toString());
480 if (setting.contains(
QLatin1String(NMQT_SETTING_IP4_CONFIG_NEVER_DEFAULT))) {
481 setNeverDefault(setting.value(
QLatin1String(NMQT_SETTING_IP4_CONFIG_NEVER_DEFAULT)).toBool());
484 if (setting.contains(
QLatin1String(NMQT_SETTING_IP4_CONFIG_MAY_FAIL))) {
485 setMayFail(setting.value(
QLatin1String(NMQT_SETTING_IP4_CONFIG_MAY_FAIL)).toBool());
488 if (setting.contains(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DAD_TIMEOUT))) {
489 setDadTimeout(setting.value(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DAD_TIMEOUT)).toUInt());
492 if (setting.contains(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DHCP_FQDN))) {
493 setDhcpFqdn(setting.value(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DHCP_FQDN)).toString());
496 if (setting.contains(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DNS_OPTIONS))) {
497 setDnsOptions(setting.value(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DNS_OPTIONS)).toStringList());
500 if (setting.contains(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DNS_PRIORITY))) {
501 setDnsPriority(setting.value(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DNS_PRIORITY)).toInt());
504 if (setting.contains(
QLatin1String(NMQT_SETTING_IP4_CONFIG_GATEWAY))) {
505 setGateway(setting.value(
QLatin1String(NMQT_SETTING_IP4_CONFIG_GATEWAY)).toString());
508 if (setting.contains(
QLatin1String(NMQT_SETTING_IP4_CONFIG_ROUTE_DATA))) {
509 setRouteData(qdbus_cast<NMVariantMapList>(setting.value(
QLatin1String(NMQT_SETTING_IP4_CONFIG_ROUTE_DATA))));
512 if (setting.contains(
QLatin1String(NMQT_SETTING_IP4_CONFIG_ADDRESS_DATA))) {
513 setAddressData(qdbus_cast<NMVariantMapList>(setting.value(
QLatin1String(NMQT_SETTING_IP4_CONFIG_ADDRESS_DATA))));
521 if (method() == Automatic) {
523 }
else if (method() == LinkLocal) {
525 }
else if (method() == Manual) {
527 }
else if (method() == Shared) {
529 }
else if (method() == Disabled) {
533 if (!dns().isEmpty()) {
537 dbusDns << htonl(dns.toIPv4Address());
543 if (!dnsSearch().isEmpty()) {
544 setting.insert(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DNS_SEARCH), dnsSearch());
547 if (!addresses().isEmpty()) {
549 const auto addressesList = addresses();
552 dbusAddress << htonl(addr.ip().toIPv4Address()) << addr.prefixLength() << htonl(addr.gateway().toIPv4Address());
553 dbusAddresses << dbusAddress;
559 if (!routes().isEmpty()) {
561 const auto routesList = routes();
564 dbusRoute << htonl(route.ip().toIPv4Address()) << route.prefixLength() << htonl(route.nextHop().toIPv4Address()) << route.metric();
565 dbusRoutes << dbusRoute;
571 if (routeMetric() >= 0) {
572 setting.insert(
QLatin1String(NMQT_SETTING_IP4_CONFIG_ROUTE_METRIC), routeMetric());
575 if (ignoreAutoRoutes()) {
576 setting.insert(
QLatin1String(NMQT_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES), ignoreAutoRoutes());
579 if (ignoreAutoDns()) {
580 setting.insert(
QLatin1String(NMQT_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS), ignoreAutoDns());
583 if (!dhcpClientId().isEmpty()) {
584 setting.insert(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DHCP_CLIENT_ID), dhcpClientId());
587 if (!dhcpSendHostname()) {
588 setting.insert(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DHCP_SEND_HOSTNAME), dhcpSendHostname());
591 if (!dhcpHostname().isEmpty()) {
592 setting.insert(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DHCP_HOSTNAME), dhcpHostname());
595 if (neverDefault()) {
596 setting.insert(
QLatin1String(NMQT_SETTING_IP4_CONFIG_NEVER_DEFAULT), neverDefault());
600 setting.insert(
QLatin1String(NMQT_SETTING_IP4_CONFIG_MAY_FAIL), mayFail());
603 if (dadTimeout() >= 0) {
604 setting.insert(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DAD_TIMEOUT), dadTimeout());
607 if (!dhcpFqdn().isEmpty()) {
608 setting.insert(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DHCP_FQDN), dhcpFqdn());
611 if (!dnsOptions().isEmpty()) {
612 setting.insert(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DNS_OPTIONS), dnsOptions());
615 if (dnsPriority() != 0) {
616 setting.insert(
QLatin1String(NMQT_SETTING_IP4_CONFIG_DNS_PRIORITY), dnsPriority());
619 if (!gateway().isEmpty()) {
620 setting.insert(
QLatin1String(NMQT_SETTING_IP4_CONFIG_GATEWAY), gateway());
623 if (!addressData().empty()) {
627 if (!routeData().empty()) {
636 dbg.
nospace() <<
"type: " << setting.typeAsString(setting.type()) <<
'\n';
637 dbg.
nospace() <<
"initialized: " << !setting.isNull() <<
'\n';
639 dbg.
nospace() << NMQT_SETTING_IP4_CONFIG_METHOD <<
": " << setting.method() <<
'\n';
640 dbg.
nospace() << NMQT_SETTING_IP4_CONFIG_DNS <<
":\n";
643 dbg.
nospace() << address.toString() <<
'\n';
645 dbg.
nospace() << NMQT_SETTING_IP4_CONFIG_DNS_SEARCH <<
": " << setting.dnsSearch() <<
'\n';
646 dbg.
nospace() << NMQT_SETTING_IP4_CONFIG_ADDRESSES <<
'\n';
647 const auto addressesList = setting.addresses();
649 dbg.
nospace() << address.ip() <<
": " << address.gateway() <<
": " << address.netmask() <<
'\n';
651 dbg.
nospace() << NMQT_SETTING_IP4_CONFIG_ROUTES <<
'\n';
652 const auto routesList = setting.routes();
654 dbg.
nospace() << route.ip() <<
": " << route.netmask() <<
": " << route.nextHop() <<
": " << route.metric() <<
'\n';
656 dbg.
nospace() << NMQT_SETTING_IP4_CONFIG_ROUTE_METRIC <<
":" << setting.routeMetric() <<
'\n';
657 dbg.
nospace() << NMQT_SETTING_IP4_CONFIG_IGNORE_AUTO_ROUTES <<
": " << setting.ignoreAutoRoutes() <<
'\n';
658 dbg.
nospace() << NMQT_SETTING_IP4_CONFIG_IGNORE_AUTO_DNS <<
": " << setting.ignoreAutoDns() <<
'\n';
659 dbg.
nospace() << NMQT_SETTING_IP4_CONFIG_DHCP_CLIENT_ID <<
": " << setting.dhcpClientId() <<
'\n';
660 dbg.
nospace() << NMQT_SETTING_IP4_CONFIG_DHCP_SEND_HOSTNAME <<
": " << setting.dhcpSendHostname() <<
'\n';
661 dbg.
nospace() << NMQT_SETTING_IP4_CONFIG_DHCP_HOSTNAME <<
": " << setting.dhcpHostname() <<
'\n';
662 dbg.
nospace() << NMQT_SETTING_IP4_CONFIG_NEVER_DEFAULT <<
": " << setting.neverDefault() <<
'\n';
663 dbg.
nospace() << NMQT_SETTING_IP4_CONFIG_MAY_FAIL <<
": " << setting.mayFail() <<
'\n';
664 dbg.
nospace() << NMQT_SETTING_IP4_CONFIG_DAD_TIMEOUT <<
": " << setting.dadTimeout() <<
'\n';
665 dbg.
nospace() << NMQT_SETTING_IP4_CONFIG_DHCP_FQDN <<
": " << setting.dhcpFqdn() <<
'\n';
666 dbg.
nospace() << NMQT_SETTING_IP4_CONFIG_DNS_OPTIONS <<
": " << setting.dnsOptions() <<
'\n';
667 dbg.
nospace() << NMQT_SETTING_IP4_CONFIG_DNS_PRIORITY <<
": " << setting.dnsPriority() <<
'\n';
668 dbg.
nospace() << NMQT_SETTING_IP4_CONFIG_GATEWAY <<
": " << setting.gateway() <<
'\n';
669 dbg.
nospace() << NMQT_SETTING_IP4_CONFIG_ADDRESS_DATA <<
": " <<
'\n';
671 for (
const QVariantMap &addressData : addDataList) {
672 QVariantMap::const_iterator i = addressData.constBegin();
673 while (i != addressData.constEnd()) {
674 dbg.
nospace() << i.key() <<
": " << i.value() <<
'\n';
677 dbg.
nospace() << NMQT_SETTING_IP4_CONFIG_ROUTE_DATA <<
": " <<
'\n';
679 for (
const QVariantMap &routeData : routeDataList) {
680 QVariantMap::const_iterator i = routeData.constBegin();
681 while (i != routeData.constEnd()) {
682 dbg.
nospace() << i.key() <<
": " << i.value() <<
'\n';
This class represents IP address.
This class represents IP route.
void setMetric(quint32 metric)
Defines the metric of the given route, lower values have higher priority.
bool isValid() const
Returns true if the route IP is defined.
void setNextHop(const QHostAddress &nextHop) const
Defines the next hop of the given route.
void fromMap(const QVariantMap &setting) override
Must be reimplemented, default implementation does nothing.
QString name() const override
Must be reimplemented, default implementationd does nothing.
QVariantMap toMap() const override
Must be reimplemented, default implementationd does nothing.
This class allows querying the underlying system to discover the available network interfaces and rea...
NETWORKMANAGERQT_EXPORT QString hostname()
Returns hostname of the machine.
bool isEmpty() const const
T value(qsizetype i) const const
void setIp(const QHostAddress &newIp)
void setPrefixLength(int length)
QString toLower() const const
QVariant fromValue(T &&value)