|
@@ -1,973 +0,0 @@
|
|
-#include <signal.h>
|
|
|
|
-#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),
|
|
|
|
- m_itfFilterAF(Interface::AF_Unknown),
|
|
|
|
- m_itfFilterMethod(Interface::IM_Unknown),
|
|
|
|
- m_bIfUpDownInProgress(false),
|
|
|
|
- m_ifUpDownPid(-1)
|
|
|
|
-{
|
|
|
|
- setObjectName("NetInterfaces");
|
|
|
|
- m_mutex.Create(true);
|
|
|
|
- m_itfFilterName.clear();
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-NetInterfaces::~NetInterfaces(void)
|
|
|
|
-{
|
|
|
|
- reset();
|
|
|
|
- m_mutex.Release();
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-void NetInterfaces::classBegin()
|
|
|
|
-{
|
|
|
|
- if(!initialize())
|
|
|
|
- {
|
|
|
|
- reportError("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<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::reportError(const char *pszFormatStr, ...)
|
|
|
|
-{
|
|
|
|
- va_list args;
|
|
|
|
- va_start(args, pszFormatStr);
|
|
|
|
- QString qs = QString::vasprintf(pszFormatStr, args);
|
|
|
|
- va_end (args);
|
|
|
|
- m_mutex.Lock();
|
|
|
|
- emit error(qs);
|
|
|
|
- m_mutex.Unlock();
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-void NetInterfaces::filterPropertyChanged(void) const
|
|
|
|
-{
|
|
|
|
- emit filteredInterfacesChanged();
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-void NetInterfaces::selConfigChanged(Interface* pi, unsigned int cfgOld, unsigned int cfgNew)
|
|
|
|
-{
|
|
|
|
- unsigned long id = pi->getID();
|
|
|
|
- unsigned int diff = cfgOld ^ cfgNew;
|
|
|
|
- unsigned int add = cfgNew & diff;
|
|
|
|
- unsigned int rem = cfgOld & diff;
|
|
|
|
-
|
|
|
|
- for(int mask = Interface::SC_Auto; mask < Interface::SC_Invalid; mask <<= 1)
|
|
|
|
- {
|
|
|
|
- if(add & mask)
|
|
|
|
- ::AddInterfaceToCfgGroup(m_eni, id, (CfgGroup)_FLAG_TO_ENUM(mask));
|
|
|
|
-
|
|
|
|
- if(rem & mask)
|
|
|
|
- ::RemoveInterfaceFromCfgGroup(m_eni, id, (CfgGroup)_FLAG_TO_ENUM(mask));
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-Interface* NetInterfaces::newInterface(QString name, int af, int method)
|
|
|
|
-{
|
|
|
|
- if(name.isNull() || name.isEmpty())
|
|
|
|
- {
|
|
|
|
- reportError("Invalid or empty interface name!");
|
|
|
|
- return NULL;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if(!_IsPowerOf2(af) || (af <= Interface::AF_Unknown) || (af >= Interface::AF_Invalid))
|
|
|
|
- {
|
|
|
|
- reportError("Invalid address family: %d!", af);
|
|
|
|
- return NULL;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if(!_IsPowerOf2(method) || (method <= Interface::IM_Unknown) || (method >= Interface::IM_Invalid))
|
|
|
|
- {
|
|
|
|
- reportError("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<NotificationSink&>(*this), this);
|
|
|
|
- m_interfaces.append(pi);
|
|
|
|
- emit interfacesChanged();
|
|
|
|
- emit filteredInterfacesChanged();
|
|
|
|
- return pi;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-void NetInterfaces::removeInterface(Interface *pi)
|
|
|
|
-{
|
|
|
|
- if(!pi)
|
|
|
|
- {
|
|
|
|
- reportError("NetInterfaces::removeInterface: An attempt was made to remove an invalid interface!");
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- m_mutex.Lock();
|
|
|
|
- if(GetIfUpDownInProgress())
|
|
|
|
- {
|
|
|
|
- m_mutex.Unlock();
|
|
|
|
- reportError("NetInterfaces::removeInterface: Interface start/stop in progress! Please try again later!");
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- m_mutex.Unlock();
|
|
|
|
-
|
|
|
|
- unsigned long id = pi->getID();
|
|
|
|
- int selCfg = pi->getSelCfg();
|
|
|
|
-
|
|
|
|
- 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;
|
|
|
|
-
|
|
|
|
- for(int mask = Interface::SC_Auto; mask < Interface::SC_Invalid; mask <<= 1)
|
|
|
|
- {
|
|
|
|
- if(selCfg & mask)
|
|
|
|
- ::RemoveInterfaceFromCfgGroup(m_eni, id, (CfgGroup)_FLAG_TO_ENUM(mask));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- ::RemoveInterfaceBlock(m_eni, id);
|
|
|
|
- break;
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-bool NetInterfaces::SetInterlockedIfUpDownInProgress(void)
|
|
|
|
-{
|
|
|
|
- m_mutex.Lock();
|
|
|
|
- if(m_bIfUpDownInProgress)
|
|
|
|
- {
|
|
|
|
- m_mutex.Unlock();
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
- m_bIfUpDownInProgress = true;
|
|
|
|
- m_mutex.Unlock();
|
|
|
|
- return true;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-void NetInterfaces::onIfUpDown(const char *pszMsg, void *pCtx)
|
|
|
|
-{
|
|
|
|
- if(pCtx)
|
|
|
|
- {
|
|
|
|
- NetInterfaces *pThis = static_cast<NetInterfaces*>(pCtx);
|
|
|
|
- pThis->m_mutex.Lock();
|
|
|
|
- emit pThis->ifUpDown(pszMsg);
|
|
|
|
- pThis->m_mutex.Unlock();
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-void NetInterfaces::onIfUpCompleted(int nExitCode, void *pCtx)
|
|
|
|
-{
|
|
|
|
- if(pCtx)
|
|
|
|
- {
|
|
|
|
- LPIF_UPDOWN_CONTEXT piudc = static_cast<LPIF_UPDOWN_CONTEXT>(pCtx);
|
|
|
|
- piudc->pThis->m_mutex.Lock();
|
|
|
|
- emit piudc->pThis->ifUpDownCompleted(piudc->ctx, nExitCode);
|
|
|
|
- piudc->pThis->SetIfUpDownInProgress(false);
|
|
|
|
- piudc->pThis->SetIfUpDownPid(-1);
|
|
|
|
- piudc->pThis->m_mutex.Unlock();
|
|
|
|
- delete piudc;
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-void NetInterfaces::onIfDownCompleted(int nExitCode, void *pCtx)
|
|
|
|
-{
|
|
|
|
- if(pCtx)
|
|
|
|
- {
|
|
|
|
- LPIF_UPDOWN_CONTEXT piudc = static_cast<LPIF_UPDOWN_CONTEXT>(pCtx);
|
|
|
|
- piudc->pThis->m_mutex.Lock();
|
|
|
|
- emit piudc->pThis->ifUpDownCompleted(piudc->ctx, nExitCode);
|
|
|
|
- piudc->pThis->SetIfUpDownInProgress(false);
|
|
|
|
- piudc->pThis->SetIfUpDownPid(-1);
|
|
|
|
- piudc->pThis->m_mutex.Unlock();
|
|
|
|
- delete piudc;
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-void NetInterfaces::onIfRestartCompleted(int nExitCode, void *pCtx)
|
|
|
|
-{
|
|
|
|
- if(pCtx)
|
|
|
|
- {
|
|
|
|
- LPIF_UPDOWN_CONTEXT piudc = static_cast<LPIF_UPDOWN_CONTEXT>(pCtx);
|
|
|
|
-
|
|
|
|
- if(piudc->ctx == UDC_Stop && !nExitCode) // ifdown succeeded
|
|
|
|
- {
|
|
|
|
- int nRet;
|
|
|
|
- piudc->ctx = UDC_Start;
|
|
|
|
-
|
|
|
|
- if((nRet = ::IfUpAsync(piudc->pi->getName(), piudc->pThis->m_ifUpDownPid, &NetInterfaces::onIfRestartCompleted, static_cast<void*>(piudc), &NetInterfaces::onIfUpDown, static_cast<void*>(piudc->pThis))))
|
|
|
|
- {
|
|
|
|
- piudc->pThis->m_mutex.Lock();
|
|
|
|
- piudc->pThis->reportError("NetInterfaces::onIfRestartCompleted: IfUpAsync failed with code: %d", nRet);
|
|
|
|
- emit piudc->pThis->ifUpDownCompleted(UDC_Restart, nRet);
|
|
|
|
- piudc->pThis->SetIfUpDownPid(-1);
|
|
|
|
- piudc->pThis->SetIfUpDownInProgress(false);
|
|
|
|
- piudc->pThis->m_mutex.Unlock();
|
|
|
|
- delete piudc;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- piudc->pThis->m_mutex.Lock();
|
|
|
|
- emit piudc->pThis->ifUpDownCompleted(UDC_Restart, nExitCode);
|
|
|
|
- piudc->pThis->SetIfUpDownPid(-1);
|
|
|
|
- piudc->pThis->SetIfUpDownInProgress(false);
|
|
|
|
- piudc->pThis->m_mutex.Unlock();
|
|
|
|
- delete piudc;
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-bool NetInterfaces::startInterface(Interface *pi)
|
|
|
|
-{
|
|
|
|
- if(!pi)
|
|
|
|
- {
|
|
|
|
- reportError("NetInterfaces::stopInterface: Invalid interface!");
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- int nRet;
|
|
|
|
-
|
|
|
|
- if(!SetInterlockedIfUpDownInProgress())
|
|
|
|
- {
|
|
|
|
- reportError("NetInterfaces::startInterface: IfUp/Down already in progress!");
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- LPIF_UPDOWN_CONTEXT piudc = new IF_UPDOWN_CONTEXT;
|
|
|
|
- piudc->pThis = this;
|
|
|
|
- piudc->pi = pi;
|
|
|
|
- piudc->ctx = UDC_Start;
|
|
|
|
-
|
|
|
|
- if((nRet = ::IfUpAsync(pi->getName(), m_ifUpDownPid, &NetInterfaces::onIfUpCompleted, static_cast<void*>(piudc), &NetInterfaces::onIfUpDown, static_cast<void*>(this))))
|
|
|
|
- {
|
|
|
|
- m_mutex.Lock();
|
|
|
|
- reportError("NetInterfaces::startInterface: IfUpAsync failed with code: %d", nRet);
|
|
|
|
- emit ifUpDownCompleted(UDC_Start, -3);
|
|
|
|
- SetIfUpDownInProgress(false);
|
|
|
|
- SetIfUpDownPid(-1);
|
|
|
|
- m_mutex.Unlock();
|
|
|
|
- delete piudc;
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return true;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-bool NetInterfaces::stopInterface(Interface *pi)
|
|
|
|
-{
|
|
|
|
- if(!pi)
|
|
|
|
- {
|
|
|
|
- reportError("NetInterfaces::stopInterface: Invalid interface!");
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- int nRet;
|
|
|
|
-
|
|
|
|
- if(!SetInterlockedIfUpDownInProgress())
|
|
|
|
- {
|
|
|
|
- reportError("NetInterfaces::stopInterface: IfUp/Down already in progress!");
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- LPIF_UPDOWN_CONTEXT piudc = new IF_UPDOWN_CONTEXT;
|
|
|
|
- piudc->pThis = this;
|
|
|
|
- piudc->pi = pi;
|
|
|
|
- piudc->ctx = UDC_Stop;
|
|
|
|
-
|
|
|
|
- if((nRet = ::IfDownAsync(pi->getName(), m_ifUpDownPid, &NetInterfaces::onIfDownCompleted, static_cast<void*>(piudc), &NetInterfaces::onIfUpDown, static_cast<void*>(this))))
|
|
|
|
- {
|
|
|
|
- m_mutex.Lock();
|
|
|
|
- reportError("NetInterfaces::stopInterface: IfDownAsync failed with code: %d", nRet);
|
|
|
|
- emit ifUpDownCompleted(UDC_Stop, -3);
|
|
|
|
- SetIfUpDownInProgress(false);
|
|
|
|
- SetIfUpDownPid(-1);
|
|
|
|
- m_mutex.Unlock();
|
|
|
|
- delete piudc;
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return true;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-bool NetInterfaces::restartInterface(Interface *pi)
|
|
|
|
-{
|
|
|
|
- if(!pi)
|
|
|
|
- {
|
|
|
|
- reportError("NetInterfaces::restartInterface: Invalid interface!");
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- int nRet;
|
|
|
|
-
|
|
|
|
- if(!SetInterlockedIfUpDownInProgress())
|
|
|
|
- {
|
|
|
|
- reportError("NetInterfaces::restartInterface: IfUp/Down already in progress!");
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- LPIF_UPDOWN_CONTEXT piudc = new IF_UPDOWN_CONTEXT;
|
|
|
|
- piudc->pThis = this;
|
|
|
|
- piudc->pi = pi;
|
|
|
|
- piudc->ctx = UDC_Stop;
|
|
|
|
-
|
|
|
|
- if((nRet = ::IfDownAsync(pi->getName(), m_ifUpDownPid, &NetInterfaces::onIfRestartCompleted, static_cast<void*>(piudc), &NetInterfaces::onIfUpDown, static_cast<void*>(this))))
|
|
|
|
- {
|
|
|
|
- m_mutex.Lock();
|
|
|
|
- reportError("NetInterfaces::restartInterface: IfDownAsync failed with code: %d", nRet);
|
|
|
|
- emit ifUpDownCompleted(UDC_Restart, -3);
|
|
|
|
- SetIfUpDownInProgress(false);
|
|
|
|
- SetIfUpDownPid(-1);
|
|
|
|
- m_mutex.Unlock();
|
|
|
|
- delete piudc;
|
|
|
|
- return false;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- return true;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-bool NetInterfaces::cancelStartStopInterface(void)
|
|
|
|
-{
|
|
|
|
- m_mutex.Lock();
|
|
|
|
- pid_t pid = GetIfUpDownPid();
|
|
|
|
- bool bCancel = GetIfUpDownInProgress() && (pid != -1);
|
|
|
|
- if(bCancel)
|
|
|
|
- {
|
|
|
|
-// bCancel = !kill(-pid, SIGKILL);
|
|
|
|
- bCancel = !system("killall -SIGTERM ifup");
|
|
|
|
- }
|
|
|
|
- m_mutex.Unlock();
|
|
|
|
- return bCancel;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-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)
|
|
|
|
- {
|
|
|
|
- reportError("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)
|
|
|
|
- {
|
|
|
|
- reportError("Invalid method filter: %d!", method);
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if(m_itfFilterMethod != method)
|
|
|
|
- {
|
|
|
|
- m_itfFilterMethod = method;
|
|
|
|
- emit itfFilterMethodChanged(method);
|
|
|
|
- emit filteredInterfacesChanged();
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-int NetInterfaces::getInterfaceSelConfig(Interface &ri)
|
|
|
|
-{
|
|
|
|
- int mask = 0;
|
|
|
|
-
|
|
|
|
- ::EnumInterfaceCfgGroups(m_eni, ri.getID(),
|
|
|
|
- [] (CfgGroup cg, void *pCtx) -> void
|
|
|
|
- {
|
|
|
|
- if(cg > CG_Unknown)
|
|
|
|
- {
|
|
|
|
- int *m = (int*)pCtx;
|
|
|
|
- *m |= _ENUM_TO_FLAG(cg);
|
|
|
|
- }
|
|
|
|
- }, &mask);
|
|
|
|
-
|
|
|
|
- return mask;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-bool NetInterfaces::ifUpDownInProgress(void) const
|
|
|
|
-{
|
|
|
|
- return m_bIfUpDownInProgress;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-/////////////////////////////////////////////////////////////////////////////
|
|
|
|
-
|
|
|
|
-Interface::Interface(ITF_IFACE_BLOCK &ifb, NotificationSink ¬ifyer, QObject *pParent) : QObject(pParent),
|
|
|
|
- m_ifb(ifb),
|
|
|
|
- m_inet(ifb, notifyer, this),
|
|
|
|
- m_rNotifyer(notifyer),
|
|
|
|
- m_selCfg(notifyer.getInterfaceSelConfig(*this))
|
|
|
|
-{
|
|
|
|
- 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.reportError("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.reportError("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();
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-int Interface::selCfg(void) const
|
|
|
|
-{
|
|
|
|
- return m_selCfg;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-void Interface::setSelCfg(int cfg)
|
|
|
|
-{
|
|
|
|
- if(cfg < SC_None || cfg >= SC_Invalid)
|
|
|
|
- {
|
|
|
|
- m_rNotifyer.reportError("Invalid start/selection configuration: 0x%X!", cfg);
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if(m_selCfg != cfg)
|
|
|
|
- {
|
|
|
|
- m_rNotifyer.selConfigChanged(this, m_selCfg, cfg);
|
|
|
|
- m_selCfg = cfg;
|
|
|
|
- emit selCfgChanged(cfg);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-Inet* Interface::inet(void)
|
|
|
|
-{
|
|
|
|
- return &m_inet;
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-/////////////////////////////////////////////////////////////////////////////
|
|
|
|
-
|
|
|
|
-Inet::Inet(ITF_IFACE_BLOCK &ifb, 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, 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.reportError("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.reportError("Invalid net mask: %s!", sa.c_str());
|
|
|
|
- m_netmask.setProperty("address", QVariant("255.255.255.0"));
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-/////////////////////////////////////////////////////////////////////////////
|
|
|
|
-
|
|
|
|
-Dhcp::Dhcp(IFACE_INET_DHCP &itfd, NotificationSink ¬ifyer, QObject *pParent) : QObject(pParent), m_itfd(itfd), m_rNotifyer(notifyer)
|
|
|
|
-{
|
|
|
|
- setObjectName("Dhcp");
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-Dhcp::~Dhcp(void)
|
|
|
|
-{
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-/////////////////////////////////////////////////////////////////////////////
|
|
|
|
-
|
|
|
|
-IPv4Address::IPv4Address(struct in_addr &addr, 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.reportError("Invalid IP address: '%s'!", sa.c_str());
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if(m_pfnAddrValidator && !(*m_pfnAddrValidator)(newAddr))
|
|
|
|
- {
|
|
|
|
- m_rNotifyer.reportError("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.reportError("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.reportError("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.reportError("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.reportError("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());
|
|
|
|
-}
|
|
|