#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(*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(*this), this); m_interfaces.append(pi); emit interfaces_Changed(); return QVariant::fromValue(pi); } QQmlListProperty NetInterfaces::interfaces(void) { return QQmlListProperty(this, m_interfaces); } QQmlListProperty 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(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 Interface::dnsServer(void) { return QQmlListProperty(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()); }