123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- #include "netinterfaces.h"
- #ifndef _countof
- #define _countof(a) (sizeof(a) / sizeof(*a))
- #endif // _countof
- #define _IS_VALID_BYTE_VALUE(b) (((b) >= 0) && ((b) <= 255))
- /////////////////////////////////////////////////////////////////////////////
- /////////////////////////////////////////////////////////////////////////////
- NetInterfaces::NetInterfaces(QObject *pParent) : QObject(pParent)
- {
- setObjectName("NetInterfaces");
- m_itfFilterAF = Interface::AF_Inet;
- m_itfFilterMethod = Interface::IM_Static;
- }
- NetInterfaces::~NetInterfaces(void)
- {
- reset();
- }
- void NetInterfaces::classBegin()
- {
- }
- void NetInterfaces::componentComplete()
- {
- if(!initialize())
- emitError("NetInterfaces::initialize failed!");
- }
- 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 interfaces_Changed();
- 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 Emitter&>(*this), this));
- }
- emit interfaces_Changed();
- }
- 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 sigError(qs);
- }
- QVariantList NetInterfaces::getInterface(const QString &itfName)
- {
- QVariantList list;
- list.clear();
- if(m_eni.ibl.size() > 0)
- {
- for(int i = 0; i < m_interfaces.count(); i++)
- {
- Interface *pi = m_interfaces.at(i);
- const ITF_IFACE_BLOCK &ibl = pi->getIface();
- if( itfName == ibl.cfgName.c_str() &&
- ibl.proto == IFP_Inet &&
- (ibl.method == IFM_Static || ibl.method == IFM_Dhcp))
- {
- QVariant var = QVariant::fromValue(pi);
- list.append(var);
- }
- }
- }
- return list;
- }
- QVariant NetInterfaces::newInterface(QString name, int af, int method, QString cfg)
- {
- m_eni.ibl.emplace_back();
- ITF_IFACE_BLOCK &rib = m_eni.ibl.back();
- rib.cfgName = name.toStdString();
- rib.proto = (IfaceProtos)af;
- rib.method = (IfaceMethods)method;
- Interface *pi = new Interface(rib, static_cast<const Emitter&>(*this), this);
- m_interfaces.append(pi);
- emit interfaces_Changed();
- return QVariant::fromValue(pi);
- }
- 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 == ibl.cfgName.c_str() &&
- (int)ibl.proto == m_itfFilterAF &&
- (int)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::set_itfFilterName(const QString &val)
- {
- if(val != m_itfFilterName)
- {
- m_itfFilterName = val;
- emit itfFilterName_Changed(m_itfFilterName);
- emit filteredInterfaces_Changed();
- }
- }
- int NetInterfaces::itfFilterAF(void) const
- {
- return m_itfFilterAF;
- }
- void NetInterfaces::set_itfFilterAF(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 itfFilterAF_Changed(af);
- emit filteredInterfaces_Changed();
- }
- }
- int NetInterfaces::itfFilterMethod(void) const
- {
- return m_itfFilterMethod;
- }
- void NetInterfaces::set_itfFilterMethod(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 itfFilterMethod_Changed(method);
- emit filteredInterfaces_Changed();
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- Interface::Interface(ITF_IFACE_BLOCK &ifb, const Emitter &errHandler, QObject *pParent) : QObject(pParent),
- m_ifb(ifb),
- m_ipAddr(ifb.inet4s.addr, errHandler, this),
- m_netmask(ifb.inet4s.netmask, errHandler, this),
- m_gateway(ifb.inet4s.gate, errHandler, this),
- m_bcastAddr(ifb.inet4s.bcast, errHandler, this),
- m_ptpAddr(ifb.inet4s.pointopoint, errHandler, this),
- m_errHandler(errHandler)
- {
- setObjectName("Interface");
- for(size_t i = 0; i < _countof(m_ifb.inet4s.namesvr); i++)
- {
- IPv4Address *addr = new IPv4Address(m_ifb.inet4s.namesvr[i], errHandler, this);
- m_dnsList.append(addr);
- }
- #if 0
- for(size_t i = 0; i < _countof(m_ifb.inet4s.namesvr); i++)
- {
- if(m_ifb.inet4s.namesvr[i].s_addr)
- {
- QString qs(inet_ntoa(m_ifb.inet4s.namesvr[i]));
- m_dnsList.append(qs);
- }
- else
- {
- QString qs("");
- m_dnsList.append(qs);
- }
- }
- #endif
- }
- Interface::~Interface(void)
- {
- IPv4Address *addr;
-
- for(int i = 0; i < m_dnsList.count(); i++)
- {
- if((addr = m_dnsList.at(i)))
- delete addr;
- }
- }
- QString Interface::name(void) const
- {
- return QString::fromStdString(m_ifb.cfgName);
- }
- QString Interface::family(void) const
- {
- return ::GetIfaceProtoStr(m_ifb.proto);
- }
- int Interface::af(void) const
- {
- return (int)m_ifb.proto;
- }
- QString Interface::method(void) const
- {
- return ::GetIfaceMethodStr(m_ifb.method);
- }
- int Interface::itfMethod(void) const
- {
- return (int)m_ifb.method;
- }
- IPv4Address* Interface::ipAddress(void)
- {
- return &m_ipAddr;
- }
- IPv4Address* Interface::netMask(void)
- {
- return &m_netmask;
- }
- IPv4Address* Interface::gateway(void)
- {
- return &m_gateway;
- }
- IPv4Address* Interface::bcastAddress(void)
- {
- return &m_bcastAddr;
- }
- IPv4Address* Interface::ptpAddress(void)
- {
- return &m_ptpAddr;
- }
- QQmlListProperty<IPv4Address> Interface::dnsServer(void)
- {
- return QQmlListProperty<IPv4Address>(this, m_dnsList);
- }
- /////////////////////////////////////////////////////////////////////////////
- IPv4Address::IPv4Address(struct in_addr &addr, const Emitter &errHandler, QObject *pParent) : QObject(pParent), m_addr(addr), m_errHandler(errHandler)
- {
- 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::set_address(const QString &addr)
- {
- struct in_addr newAddr, oldAddr;
- std::string sa = addr.toStdString();
- if(!inet_aton(sa.c_str(), &newAddr))
- {
- m_errHandler.emitError("Invalid IP 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 b0_Changed(pb2[0]);
- if(pb1[1] != pb2[1])
- emit b1_Changed(pb2[1]);
- if(pb1[2] != pb2[2])
- emit b2_Changed(pb2[2]);
- if(pb1[3] != pb2[3])
- emit b3_Changed(pb2[3]);
- emit address_Changed(address());
- }
- }
- void IPv4Address::set_b0(int b)
- {
- unsigned char *pb = (unsigned char*)&m_addr.s_addr;
- if(!_IS_VALID_BYTE_VALUE(b))
- {
- m_errHandler.emitError("Invalid IP address byte 0: '%d'!", b);
- return;
- }
- if(b == (int)pb[0])
- return;
- pb[0] = (unsigned char)b;
- emit b0_Changed(b);
- emit address_Changed(address());
- }
- void IPv4Address::set_b1(int b)
- {
- unsigned char *pb = (unsigned char*)&m_addr.s_addr;
- if(!_IS_VALID_BYTE_VALUE(b))
- {
- m_errHandler.emitError("Invalid IP address byte 1: '%d'!", b);
- return;
- }
- if(b == (int)pb[1])
- return;
- pb[1] = (unsigned char)b;
- emit b1_Changed(b);
- emit address_Changed(address());
- }
- void IPv4Address::set_b2(int b)
- {
- unsigned char *pb = (unsigned char*)&m_addr.s_addr;
- if(!_IS_VALID_BYTE_VALUE(b))
- {
- m_errHandler.emitError("Invalid IP address byte 2: '%d'!", b);
- return;
- }
- if(b == (int)pb[2])
- return;
- pb[2] = (unsigned char)b;
- emit b2_Changed(b);
- emit address_Changed(address());
- }
- void IPv4Address::set_b3(int b)
- {
- unsigned char *pb = (unsigned char*)&m_addr.s_addr;
- if(!_IS_VALID_BYTE_VALUE(b))
- {
- m_errHandler.emitError("Invalid IP address byte 3: '%d'!", b);
- return;
- }
- if(b == (int)pb[3])
- return;
- pb[3] = (unsigned char)b;
- emit b3_Changed(b);
- emit address_Changed(address());
- }
|