#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 static bool _IsPowerOf2(T x) { return x && !(x & (x - 1)); } template static unsigned int _BitCount(T n) { unsigned int count = 0; while(n) { count++; n &= (n - 1); } return count; } template 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(*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(*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 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.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(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 Static::dnsServer(void) { return QQmlListProperty(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()); }