123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681 |
- #include "netinterfaces.h"
- #include "../debug.h"
- #ifndef _countof
- #define _countof(a) (sizeof(a) / sizeof(*a))
- #endif // _countof
- #define _IS_VALID_BYTE_VALUE(b) (((b) >= 0) && ((b) <= 255))
- template<typename T>
- static bool _IsPowerOf2(T x)
- {
- return x && !(x & (x - 1));
- }
- template<typename T>
- static unsigned int _BitCount(T n)
- {
- unsigned int count = 0;
- while(n)
- {
- count++;
- n &= (n - 1);
- }
- return count;
- }
- template<typename T>
- static int _BitNumber(T n)
- {
- if(!n || !_IsPowerOf2(n))
- return -1;
- int count = 0;
- while(n)
- {
- count++;
- n >>= 1;
- }
- return count - 1;
- }
- static int _Mask2Prefix(const struct in_addr &in)
- {
- return _BitCount(in.s_addr);
- }
- static in_addr_t _Prefix2Mask(int prefix)
- {
- return htonl(~((1 << (32 - prefix)) - 1));
- }
- static bool _IsValidNetmask(const struct in_addr &in)
- {
- return _IsPowerOf2(~ntohl(in.s_addr) + 1);
- }
- /////////////////////////////////////////////////////////////////////////////
- #define _FLAG_TO_ENUM(f) (_BitNumber(f))
- #define _ENUM_TO_FLAG(e) (0x00000001 << e)
- /////////////////////////////////////////////////////////////////////////////
- NetInterfaces::NetInterfaces(QObject *pParent) : QObject(pParent)
- {
- setObjectName("NetInterfaces");
- m_itfFilterAF = Interface::AF_Unknown;
- m_itfFilterMethod = Interface::IM_Unknown;
- m_itfFilterName.clear();
- }
- NetInterfaces::~NetInterfaces(void)
- {
- reset();
- }
- void NetInterfaces::classBegin()
- {
- if(!initialize())
- {
- emitError("NetInterfaces::initialize failed!");
- }
- }
- void NetInterfaces::componentComplete()
- {
- }
- void NetInterfaces::reset(void)
- {
- Interface *pItf;
- for(int i = 0; i < m_interfaces.count(); i++)
- {
- if((pItf = m_interfaces.at(i)))
- delete pItf;
- }
- m_interfaces.clear();
- emit interfacesChanged();
- emit filteredInterfacesChanged();
- m_eni._reset();
- }
- bool NetInterfaces::initialize(void)
- {
- bool bRet;
- reset();
- if((bRet = ::ParseEtcNetworkInterfaces(m_eni)))
- {
- for(auto it = m_eni.ibl.begin(); it != m_eni.ibl.end(); it++)
- {
- ITF_IFACE_BLOCK &ibl = *it;
- m_interfaces.append(new Interface(ibl, static_cast<const NotificationSink&>(*this), this));
- }
- emit interfacesChanged();
- emit filteredInterfacesChanged();
- }
- return bRet;
- }
- bool NetInterfaces::save(void)
- {
- return ::WriteEtcNetworkInterfaces(m_eni, NULL);
- }
- bool NetInterfaces::saveAs(const QString &path)
- {
- if(!path.length())
- return false;
- std::string p = path.toStdString();
- const char *pszPath = p.c_str();
- return ::WriteEtcNetworkInterfaces(m_eni, pszPath);
- }
- void NetInterfaces::emitError(const char *pszFormatStr, ...) const
- {
- va_list args;
- va_start(args, pszFormatStr);
- QString qs = QString::vasprintf(pszFormatStr, args);
- va_end (args);
- emit error(qs);
- }
- void NetInterfaces::filterPropertyChanged(void) const
- {
- emit filteredInterfacesChanged();
- }
- Interface* NetInterfaces::newInterface(QString name, int af, int method)
- {
- if(name.isNull() || name.isEmpty())
- {
- emitError("Invalid or empty interface name!");
- return NULL;
- }
- if(!_IsPowerOf2(af) || (af <= Interface::AF_Unknown) || (af >= Interface::AF_Invalid))
- {
- emitError("Invalid address family: %d!", af);
- return NULL;
- }
- if(!_IsPowerOf2(method) || (method <= Interface::IM_Unknown) || (method >= Interface::IM_Invalid))
- {
- emitError("Invalid method: %d!", method);
- return NULL;
- }
- m_eni.ibl.emplace_back();
- ITF_IFACE_BLOCK &rib = m_eni.ibl.back();
- rib.cfgName = name.toStdString();
- rib.proto = (IfaceProtos)_FLAG_TO_ENUM(af);
- rib.method = (IfaceMethods)_FLAG_TO_ENUM(method);
- Interface *pi = new Interface(rib, static_cast<const NotificationSink&>(*this), this);
- m_interfaces.append(pi);
- emit interfacesChanged();
- emit filteredInterfacesChanged();
- return pi;
- }
- void NetInterfaces::removeInterface(Interface *pi)
- {
- if(!pi)
- {
- emitError("%s: Attempt to remove invalid interface!", __FUNCTION__);
- return;
- }
- unsigned long id = pi->getID();
- for(int i = 0; i < m_interfaces.count(); i++)
- {
- Interface *pil = m_interfaces.at(i);
- if(pil->getID() == id)
- {
- m_interfaces.removeAt(i);
- emit interfacesChanged();
- emit filteredInterfacesChanged();
- delete pil;
- ::RemoveInterfaceBlock(m_eni, id);
- break;
- }
- }
- }
- QQmlListProperty<Interface> NetInterfaces::interfaces(void)
- {
- return QQmlListProperty<Interface>(this, m_interfaces);
- }
- QQmlListProperty<Interface> NetInterfaces::filteredInterfaces(void)
- {
- m_filteredInterfaces.clear();
- for(int i = 0; i < m_interfaces.count(); i++)
- {
- Interface *pi = m_interfaces.at(i);
- const ITF_IFACE_BLOCK &ibl = pi->getIface();
- if( (m_itfFilterName.isNull() || m_itfFilterName.isEmpty() || (m_itfFilterName == ibl.cfgName.c_str())) &&
- (_ENUM_TO_FLAG(ibl.proto) & m_itfFilterAF) &&
- (_ENUM_TO_FLAG(ibl.method) & m_itfFilterMethod))
- {
- m_filteredInterfaces.append(pi);
- }
- }
- return QQmlListProperty<Interface>(this, m_filteredInterfaces);
- }
- const QString& NetInterfaces::itfFilterName(void) const
- {
- return m_itfFilterName;
- }
- void NetInterfaces::setItfFilterName(const QString &val)
- {
- if(val != m_itfFilterName)
- {
- m_itfFilterName = val;
- emit itfFilterNameChanged(m_itfFilterName);
- emit filteredInterfacesChanged();
- }
- }
- int NetInterfaces::itfFilterAF(void) const
- {
- return m_itfFilterAF;
- }
- void NetInterfaces::setItfFilterAF(int af)
- {
- if(af < Interface::AF_Unknown || af >= Interface::AF_Invalid)
- {
- emitError("Invalid address family filter: %d!", af);
- return;
- }
- if(m_itfFilterAF != af)
- {
- m_itfFilterAF = af;
- emit itfFilterAFChanged(af);
- emit filteredInterfacesChanged();
- }
- }
- int NetInterfaces::itfFilterMethod(void) const
- {
- return m_itfFilterMethod;
- }
- void NetInterfaces::setItfFilterMethod(int method)
- {
- if(method < Interface::IM_Unknown || method >= Interface::IM_Invalid)
- {
- emitError("Invalid method filter: %d!", method);
- return;
- }
- if(m_itfFilterMethod != method)
- {
- m_itfFilterMethod = method;
- emit itfFilterMethodChanged(method);
- emit filteredInterfacesChanged();
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- Interface::Interface(ITF_IFACE_BLOCK &ifb, const NotificationSink ¬ifyer, QObject *pParent) : QObject(pParent),
- m_ifb(ifb),
- m_inet(ifb, notifyer, this),
- m_rNotifyer(notifyer)
- {
- setObjectName("Interface");
- }
- Interface::~Interface(void)
- {
- }
- QString Interface::name(void) const
- {
- return QString::fromStdString(m_ifb.cfgName);
- }
- QString Interface::afName(void) const
- {
- return ::GetIfaceProtoStr(m_ifb.proto);
- }
- int Interface::af(void) const
- {
- return (int)_ENUM_TO_FLAG(m_ifb.proto);
- }
- void Interface::setAf(int af)
- {
- if(!_IsPowerOf2(af) || (af < Interface::AF_Unknown) || (af >= Interface::AF_Invalid))
- {
- m_rNotifyer.emitError("Invalid address family: %d!", af);
- return;
- }
- IfaceProtos proto = (IfaceProtos)_FLAG_TO_ENUM(af);
- if(m_ifb.proto != proto)
- {
- m_ifb.proto = proto;
- emit afChanged(af);
- emit afNameChanged();
- m_rNotifyer.filterPropertyChanged();
- }
- }
- QString Interface::methodName(void) const
- {
- return ::GetIfaceMethodStr(m_ifb.method);
- }
- int Interface::method(void) const
- {
- return (int)_ENUM_TO_FLAG(m_ifb.method);
- }
- void Interface::setMethod(int method)
- {
- if(!_IsPowerOf2(method) || (method < Interface::IM_Unknown) || (method >= Interface::IM_Invalid))
- {
- m_rNotifyer.emitError("Invalid interface method: %d!", method);
- return;
- }
- IfaceMethods meth = (IfaceMethods)_FLAG_TO_ENUM(method);
- if(m_ifb.method != meth)
- {
- m_ifb.method = meth;
- emit methodChanged(meth);
- emit methodNameChanged();
- m_rNotifyer.filterPropertyChanged();
- }
- }
- Inet* Interface::inet(void)
- {
- return &m_inet;
- }
- /////////////////////////////////////////////////////////////////////////////
- Inet::Inet(ITF_IFACE_BLOCK &ifb, const NotificationSink ¬ifyer, QObject *pParent) : QObject(pParent),
- m_static(ifb.inet4s, notifyer, this),
- m_dhcp(ifb.inet4d, notifyer, this),
- m_rNotifyer(notifyer)
- {
- setObjectName("Inet");
- }
- Inet::~Inet(void)
- {
- }
- Static* Inet::stat(void)
- {
- return &m_static;
- }
- Dhcp* Inet::dhcp(void)
- {
- return &m_dhcp;
- }
- /////////////////////////////////////////////////////////////////////////////
- Static::Static(IFACE_INET_STATIC &itfs, const NotificationSink ¬ifyer, QObject *pParent) : QObject(pParent),
- m_itfs(itfs),
- m_rNotifyer(notifyer),
- m_ipAddr(itfs.addr, notifyer, this),
- m_netmask(itfs.netmask, notifyer, this, _IsValidNetmask),
- m_gateway(itfs.gate, notifyer, this),
- m_bcastAddr(itfs.bcast, notifyer, this),
- m_ptpAddr(itfs.pointopoint, notifyer, this)
- {
- setObjectName("Static");
- for(size_t i = 0; i < _countof(m_itfs.namesvr); i++)
- {
- IPv4Address *addr = new IPv4Address(m_itfs.namesvr[i], notifyer, this);
- m_dnsList.append(addr);
- }
-
- QObject::connect(&m_netmask, SIGNAL(addressChanged(const QString&)), this, SLOT(netmaskChanged(const QString&)));
- }
- Static::~Static(void)
- {
- IPv4Address *addr;
- for(int i = 0; i < m_dnsList.count(); i++)
- {
- if((addr = m_dnsList.at(i)))
- delete addr;
- }
- }
- IPv4Address* Static::ipAddress(void)
- {
- return &m_ipAddr;
- }
- IPv4Address* Static::netMask(void)
- {
- return &m_netmask;
- }
- IPv4Address* Static::gateway(void)
- {
- return &m_gateway;
- }
- IPv4Address* Static::bcastAddress(void)
- {
- return &m_bcastAddr;
- }
- IPv4Address* Static::ptpAddress(void)
- {
- return &m_ptpAddr;
- }
- QQmlListProperty<IPv4Address> Static::dnsServer(void)
- {
- return QQmlListProperty<IPv4Address>(this, m_dnsList);
- }
- int Static::metric(void) const
- {
- return m_itfs.metric;
- }
- void Static::setMetric(int metric)
- {
- if(m_itfs.metric != metric)
- {
- m_itfs.metric = metric;
- emit metricChanged(metric);
- }
- }
- int Static::mtu(void) const
- {
- return m_itfs.mtu;
- }
- void Static::setMtu(int mtu)
- {
- if(m_itfs.mtu != mtu)
- {
- m_itfs.mtu = mtu;
- emit mtuChanged(mtu);
- }
- }
- int Static::netPrefix(void) const
- {
- return m_itfs.netprefix;
- }
- void Static::setNetPrefix(int netprefix)
- {
- if(netprefix < 0 || netprefix > 32)
- {
- m_rNotifyer.emitError("Invalid net prefix: %d!", netprefix);
- return;
- }
- if(m_itfs.netprefix != (unsigned int)netprefix)
- {
- struct in_addr in;
- in.s_addr = _Prefix2Mask(netprefix);
- m_netmask.setProperty("address", QVariant(inet_ntoa(in)));
- }
- }
- void Static::netmaskChanged(const QString &mask)
- {
- struct in_addr in;
- std::string sa = mask.toStdString();
- if( inet_aton(sa.c_str(), &in) &&
- _IsValidNetmask(in))
- {
- m_itfs.netprefix = _Mask2Prefix(in);
- emit netPrefixChanged(m_itfs.netprefix);
- }
- else
- {
- m_rNotifyer.emitError("Invalid net mask: %s!", sa.c_str());
- m_netmask.setProperty("address", QVariant("255.255.255.0"));
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- Dhcp::Dhcp(IFACE_INET_DHCP &itfd, const NotificationSink ¬ifyer, QObject *pParent) : QObject(pParent), m_itfd(itfd), m_rNotifyer(notifyer)
- {
- setObjectName("Dhcp");
- }
- Dhcp::~Dhcp(void)
- {
- }
- /////////////////////////////////////////////////////////////////////////////
- IPv4Address::IPv4Address(struct in_addr &addr, const NotificationSink ¬ifyer, QObject *pParent, PFN_ADDRESS_VALIDATOR pfnAddrValidator) : QObject(pParent),
- m_addr(addr),
- m_pfnAddrValidator(pfnAddrValidator),
- m_rNotifyer(notifyer)
- {
- setObjectName("IPv4Address");
- }
- IPv4Address::~IPv4Address(void)
- {
- }
- QString IPv4Address::address(void) const
- {
- return inet_ntoa(m_addr);
- }
- int IPv4Address::b0(void) const
- {
- unsigned char *pb = (unsigned char*)&m_addr.s_addr;
- return (int)pb[0];
- }
- int IPv4Address::b1(void) const
- {
- unsigned char *pb = (unsigned char*)&m_addr.s_addr;
- return (int)pb[1];
- }
- int IPv4Address::b2(void) const
- {
- unsigned char *pb = (unsigned char*)&m_addr.s_addr;
- return (int)pb[2];
- }
- int IPv4Address::b3(void) const
- {
- unsigned char *pb = (unsigned char*)&m_addr.s_addr;
- return (int)pb[3];
- }
- void IPv4Address::setAddress(const QString &addr)
- {
- struct in_addr newAddr, oldAddr;
- std::string sa = addr.toStdString();
- if(!inet_aton(sa.c_str(), &newAddr))
- {
- m_rNotifyer.emitError("Invalid IP address: '%s'!", sa.c_str());
- return;
- }
-
- if(m_pfnAddrValidator && !(*m_pfnAddrValidator)(newAddr))
- {
- m_rNotifyer.emitError("Invalid address: '%s'!", sa.c_str());
- return;
- }
- if(m_addr.s_addr != newAddr.s_addr)
- {
- oldAddr.s_addr = m_addr.s_addr;
- m_addr.s_addr = newAddr.s_addr;
- unsigned char *pb1 = (unsigned char*)&oldAddr.s_addr;
- unsigned char *pb2 = (unsigned char*)&newAddr.s_addr;
- if(pb1[0] != pb2[0])
- emit b0Changed(pb2[0]);
- if(pb1[1] != pb2[1])
- emit b1Changed(pb2[1]);
- if(pb1[2] != pb2[2])
- emit b2Changed(pb2[2]);
- if(pb1[3] != pb2[3])
- emit b3Changed(pb2[3]);
- emit addressChanged(address());
- }
- }
- void IPv4Address::setB0(int b)
- {
- unsigned char *pb = (unsigned char*)&m_addr.s_addr;
- if(!_IS_VALID_BYTE_VALUE(b))
- {
- m_rNotifyer.emitError("Invalid IP address byte 0: '%d'!", b);
- return;
- }
- if(b == (int)pb[0])
- return;
- pb[0] = (unsigned char)b;
- emit b0Changed(b);
- emit addressChanged(address());
- }
- void IPv4Address::setB1(int b)
- {
- unsigned char *pb = (unsigned char*)&m_addr.s_addr;
- if(!_IS_VALID_BYTE_VALUE(b))
- {
- m_rNotifyer.emitError("Invalid IP address byte 1: '%d'!", b);
- return;
- }
- if(b == (int)pb[1])
- return;
- pb[1] = (unsigned char)b;
- emit b1Changed(b);
- emit addressChanged(address());
- }
- void IPv4Address::setB2(int b)
- {
- unsigned char *pb = (unsigned char*)&m_addr.s_addr;
- if(!_IS_VALID_BYTE_VALUE(b))
- {
- m_rNotifyer.emitError("Invalid IP address byte 2: '%d'!", b);
- return;
- }
- if(b == (int)pb[2])
- return;
- pb[2] = (unsigned char)b;
- emit b2Changed(b);
- emit addressChanged(address());
- }
- void IPv4Address::setB3(int b)
- {
- unsigned char *pb = (unsigned char*)&m_addr.s_addr;
- if(!_IS_VALID_BYTE_VALUE(b))
- {
- m_rNotifyer.emitError("Invalid IP address byte 3: '%d'!", b);
- return;
- }
- if(b == (int)pb[3])
- return;
- pb[3] = (unsigned char)b;
- emit b3Changed(b);
- emit addressChanged(address());
- }
|