|
@@ -1,11 +1,6 @@
|
|
#include "netinterfaces.h"
|
|
#include "netinterfaces.h"
|
|
#include "../debug.h"
|
|
#include "../debug.h"
|
|
|
|
|
|
-#if 1
|
|
|
|
-#undef TRACE
|
|
|
|
-#define TRACE(...) (void)0
|
|
|
|
-#endif
|
|
|
|
-
|
|
|
|
#ifndef _countof
|
|
#ifndef _countof
|
|
#define _countof(a) (sizeof(a) / sizeof(*a))
|
|
#define _countof(a) (sizeof(a) / sizeof(*a))
|
|
#endif // _countof
|
|
#endif // _countof
|
|
@@ -34,7 +29,7 @@ static unsigned int _BitCount(T n)
|
|
template<typename T>
|
|
template<typename T>
|
|
static int _BitNumber(T n)
|
|
static int _BitNumber(T n)
|
|
{
|
|
{
|
|
- if(!_IsPowerOf2(n))
|
|
|
|
|
|
+ if(!n || !_IsPowerOf2(n))
|
|
return -1;
|
|
return -1;
|
|
int count = 0;
|
|
int count = 0;
|
|
while(n)
|
|
while(n)
|
|
@@ -42,14 +37,29 @@ static int _BitNumber(T n)
|
|
count++;
|
|
count++;
|
|
n >>= 1;
|
|
n >>= 1;
|
|
}
|
|
}
|
|
- return count;
|
|
|
|
|
|
+ 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 _FLAG_TO_ENUM(f) (_BitNumber(f))
|
|
#define _ENUM_TO_FLAG(e) (0x00000001 << e)
|
|
#define _ENUM_TO_FLAG(e) (0x00000001 << e)
|
|
|
|
|
|
-/////////////////////////////////////////////////////////////////////////////
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
NetInterfaces::NetInterfaces(QObject *pParent) : QObject(pParent)
|
|
NetInterfaces::NetInterfaces(QObject *pParent) : QObject(pParent)
|
|
@@ -58,43 +68,37 @@ NetInterfaces::NetInterfaces(QObject *pParent) : QObject(pParent)
|
|
m_itfFilterAF = Interface::AF_Unknown;
|
|
m_itfFilterAF = Interface::AF_Unknown;
|
|
m_itfFilterMethod = Interface::IM_Unknown;
|
|
m_itfFilterMethod = Interface::IM_Unknown;
|
|
m_itfFilterName.clear();
|
|
m_itfFilterName.clear();
|
|
- TRACE("%s\n", __FUNCTION__);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
NetInterfaces::~NetInterfaces(void)
|
|
NetInterfaces::~NetInterfaces(void)
|
|
{
|
|
{
|
|
reset();
|
|
reset();
|
|
- TRACE("%s\n", __FUNCTION__);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
void NetInterfaces::classBegin()
|
|
void NetInterfaces::classBegin()
|
|
{
|
|
{
|
|
- TRACE("%s\n", __FUNCTION__);
|
|
|
|
if(!initialize())
|
|
if(!initialize())
|
|
{
|
|
{
|
|
- TRACE("initialize failed!\n");
|
|
|
|
emitError("NetInterfaces::initialize failed!");
|
|
emitError("NetInterfaces::initialize failed!");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void NetInterfaces::componentComplete()
|
|
void NetInterfaces::componentComplete()
|
|
{
|
|
{
|
|
- TRACE("%s\n", __FUNCTION__);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
void NetInterfaces::reset(void)
|
|
void NetInterfaces::reset(void)
|
|
{
|
|
{
|
|
Interface *pItf;
|
|
Interface *pItf;
|
|
- TRACE("%s\n", __FUNCTION__);
|
|
|
|
-
|
|
|
|
|
|
+
|
|
for(int i = 0; i < m_interfaces.count(); i++)
|
|
for(int i = 0; i < m_interfaces.count(); i++)
|
|
{
|
|
{
|
|
if((pItf = m_interfaces.at(i)))
|
|
if((pItf = m_interfaces.at(i)))
|
|
delete pItf;
|
|
delete pItf;
|
|
}
|
|
}
|
|
m_interfaces.clear();
|
|
m_interfaces.clear();
|
|
- emit interfaces_Changed();
|
|
|
|
-
|
|
|
|
|
|
+ emit interfacesChanged();
|
|
|
|
+ emit filteredInterfacesChanged();
|
|
m_eni._reset();
|
|
m_eni._reset();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -102,21 +106,17 @@ bool NetInterfaces::initialize(void)
|
|
{
|
|
{
|
|
bool bRet;
|
|
bool bRet;
|
|
reset();
|
|
reset();
|
|
- TRACE("%s\n", __FUNCTION__);
|
|
|
|
|
|
|
|
if((bRet = ::ParseEtcNetworkInterfaces(m_eni)))
|
|
if((bRet = ::ParseEtcNetworkInterfaces(m_eni)))
|
|
{
|
|
{
|
|
- TRACE("ParseEtcNetworkInterfaces success.\n");
|
|
|
|
-
|
|
|
|
for(auto it = m_eni.ibl.begin(); it != m_eni.ibl.end(); it++)
|
|
for(auto it = m_eni.ibl.begin(); it != m_eni.ibl.end(); it++)
|
|
{
|
|
{
|
|
ITF_IFACE_BLOCK &ibl = *it;
|
|
ITF_IFACE_BLOCK &ibl = *it;
|
|
- TRACE(" appending interface ...\n");
|
|
|
|
- m_interfaces.append(new Interface(ibl, static_cast<const Emitter&>(*this), this));
|
|
|
|
- TRACE(" interface appended.\n");
|
|
|
|
|
|
+ m_interfaces.append(new Interface(ibl, static_cast<const NotificationSink&>(*this), this));
|
|
}
|
|
}
|
|
|
|
|
|
- emit interfaces_Changed();
|
|
|
|
|
|
+ emit interfacesChanged();
|
|
|
|
+ emit filteredInterfacesChanged();
|
|
}
|
|
}
|
|
|
|
|
|
return bRet;
|
|
return bRet;
|
|
@@ -142,52 +142,32 @@ void NetInterfaces::emitError(const char *pszFormatStr, ...) const
|
|
va_start(args, pszFormatStr);
|
|
va_start(args, pszFormatStr);
|
|
QString qs = QString::vasprintf(pszFormatStr, args);
|
|
QString qs = QString::vasprintf(pszFormatStr, args);
|
|
va_end (args);
|
|
va_end (args);
|
|
- emit sigError(qs);
|
|
|
|
|
|
+ emit error(qs);
|
|
}
|
|
}
|
|
|
|
|
|
-/*QVariantList NetInterfaces::getInterface(const QString &itfName)
|
|
|
|
|
|
+void NetInterfaces::filterPropertyChanged(void) const
|
|
{
|
|
{
|
|
- 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;
|
|
|
|
-}*/
|
|
|
|
|
|
+ emit filteredInterfacesChanged();
|
|
|
|
+}
|
|
|
|
|
|
-QVariant NetInterfaces::newInterface(QString name, int af, int method)
|
|
|
|
|
|
+Interface* NetInterfaces::newInterface(QString name, int af, int method)
|
|
{
|
|
{
|
|
if(name.isNull() || name.isEmpty())
|
|
if(name.isNull() || name.isEmpty())
|
|
{
|
|
{
|
|
emitError("Invalid or empty interface name!");
|
|
emitError("Invalid or empty interface name!");
|
|
- return QVariant();
|
|
|
|
|
|
+ return NULL;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
if(!_IsPowerOf2(af) || (af <= Interface::AF_Unknown) || (af >= Interface::AF_Invalid))
|
|
if(!_IsPowerOf2(af) || (af <= Interface::AF_Unknown) || (af >= Interface::AF_Invalid))
|
|
{
|
|
{
|
|
emitError("Invalid address family: %d!", af);
|
|
emitError("Invalid address family: %d!", af);
|
|
- return QVariant();
|
|
|
|
|
|
+ return NULL;
|
|
}
|
|
}
|
|
|
|
|
|
if(!_IsPowerOf2(method) || (method <= Interface::IM_Unknown) || (method >= Interface::IM_Invalid))
|
|
if(!_IsPowerOf2(method) || (method <= Interface::IM_Unknown) || (method >= Interface::IM_Invalid))
|
|
{
|
|
{
|
|
emitError("Invalid method: %d!", method);
|
|
emitError("Invalid method: %d!", method);
|
|
- return QVariant();
|
|
|
|
|
|
+ return NULL;
|
|
}
|
|
}
|
|
|
|
|
|
m_eni.ibl.emplace_back();
|
|
m_eni.ibl.emplace_back();
|
|
@@ -195,10 +175,37 @@ QVariant NetInterfaces::newInterface(QString name, int af, int method)
|
|
rib.cfgName = name.toStdString();
|
|
rib.cfgName = name.toStdString();
|
|
rib.proto = (IfaceProtos)_FLAG_TO_ENUM(af);
|
|
rib.proto = (IfaceProtos)_FLAG_TO_ENUM(af);
|
|
rib.method = (IfaceMethods)_FLAG_TO_ENUM(method);
|
|
rib.method = (IfaceMethods)_FLAG_TO_ENUM(method);
|
|
- Interface *pi = new Interface(rib, static_cast<const Emitter&>(*this), this);
|
|
|
|
|
|
+ Interface *pi = new Interface(rib, static_cast<const NotificationSink&>(*this), this);
|
|
m_interfaces.append(pi);
|
|
m_interfaces.append(pi);
|
|
- emit interfaces_Changed();
|
|
|
|
- return QVariant::fromValue(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)
|
|
QQmlListProperty<Interface> NetInterfaces::interfaces(void)
|
|
@@ -231,15 +238,15 @@ const QString& NetInterfaces::itfFilterName(void) const
|
|
return m_itfFilterName;
|
|
return m_itfFilterName;
|
|
}
|
|
}
|
|
|
|
|
|
-void NetInterfaces::set_itfFilterName(const QString &val)
|
|
|
|
|
|
+void NetInterfaces::setItfFilterName(const QString &val)
|
|
{
|
|
{
|
|
if(val != m_itfFilterName)
|
|
if(val != m_itfFilterName)
|
|
{
|
|
{
|
|
m_itfFilterName = val;
|
|
m_itfFilterName = val;
|
|
- emit itfFilterName_Changed(m_itfFilterName);
|
|
|
|
- emit filteredInterfaces_Changed();
|
|
|
|
|
|
+ emit itfFilterNameChanged(m_itfFilterName);
|
|
|
|
+ emit filteredInterfacesChanged();
|
|
}
|
|
}
|
|
- TRACE("%s\n", __FUNCTION__);
|
|
|
|
|
|
+
|
|
}
|
|
}
|
|
|
|
|
|
int NetInterfaces::itfFilterAF(void) const
|
|
int NetInterfaces::itfFilterAF(void) const
|
|
@@ -247,9 +254,9 @@ int NetInterfaces::itfFilterAF(void) const
|
|
return m_itfFilterAF;
|
|
return m_itfFilterAF;
|
|
}
|
|
}
|
|
|
|
|
|
-void NetInterfaces::set_itfFilterAF(int af)
|
|
|
|
|
|
+void NetInterfaces::setItfFilterAF(int af)
|
|
{
|
|
{
|
|
- TRACE("%s\n", __FUNCTION__);
|
|
|
|
|
|
+
|
|
|
|
|
|
if(af < Interface::AF_Unknown || af >= Interface::AF_Invalid)
|
|
if(af < Interface::AF_Unknown || af >= Interface::AF_Invalid)
|
|
{
|
|
{
|
|
@@ -260,8 +267,8 @@ void NetInterfaces::set_itfFilterAF(int af)
|
|
if(m_itfFilterAF != af)
|
|
if(m_itfFilterAF != af)
|
|
{
|
|
{
|
|
m_itfFilterAF = af;
|
|
m_itfFilterAF = af;
|
|
- emit itfFilterAF_Changed(af);
|
|
|
|
- emit filteredInterfaces_Changed();
|
|
|
|
|
|
+ emit itfFilterAFChanged(af);
|
|
|
|
+ emit filteredInterfacesChanged();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -270,9 +277,9 @@ int NetInterfaces::itfFilterMethod(void) const
|
|
return m_itfFilterMethod;
|
|
return m_itfFilterMethod;
|
|
}
|
|
}
|
|
|
|
|
|
-void NetInterfaces::set_itfFilterMethod(int method)
|
|
|
|
|
|
+void NetInterfaces::setItfFilterMethod(int method)
|
|
{
|
|
{
|
|
- TRACE("%s\n", __FUNCTION__);
|
|
|
|
|
|
+
|
|
|
|
|
|
if(method < Interface::IM_Unknown || method >= Interface::IM_Invalid)
|
|
if(method < Interface::IM_Unknown || method >= Interface::IM_Invalid)
|
|
{
|
|
{
|
|
@@ -283,41 +290,23 @@ void NetInterfaces::set_itfFilterMethod(int method)
|
|
if(m_itfFilterMethod != method)
|
|
if(m_itfFilterMethod != method)
|
|
{
|
|
{
|
|
m_itfFilterMethod = method;
|
|
m_itfFilterMethod = method;
|
|
- emit itfFilterMethod_Changed(method);
|
|
|
|
- emit filteredInterfaces_Changed();
|
|
|
|
|
|
+ emit itfFilterMethodChanged(method);
|
|
|
|
+ emit filteredInterfacesChanged();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
-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)
|
|
|
|
|
|
+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");
|
|
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);
|
|
|
|
- }
|
|
|
|
- TRACE("%s\n", __FUNCTION__);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
Interface::~Interface(void)
|
|
Interface::~Interface(void)
|
|
{
|
|
{
|
|
- IPv4Address *addr;
|
|
|
|
-
|
|
|
|
- for(int i = 0; i < m_dnsList.count(); i++)
|
|
|
|
- {
|
|
|
|
- if((addr = m_dnsList.at(i)))
|
|
|
|
- delete addr;
|
|
|
|
- }
|
|
|
|
- TRACE("%s\n", __FUNCTION__);
|
|
|
|
}
|
|
}
|
|
|
|
|
|
QString Interface::name(void) const
|
|
QString Interface::name(void) const
|
|
@@ -332,68 +321,232 @@ QString Interface::afName(void) const
|
|
|
|
|
|
int Interface::af(void) const
|
|
int Interface::af(void) const
|
|
{
|
|
{
|
|
- return (int)m_ifb.proto;
|
|
|
|
|
|
+ return (int)_ENUM_TO_FLAG(m_ifb.proto);
|
|
}
|
|
}
|
|
|
|
|
|
-void Interface::set_af(int af)
|
|
|
|
|
|
+void Interface::setAf(int af)
|
|
{
|
|
{
|
|
if(!_IsPowerOf2(af) || (af < Interface::AF_Unknown) || (af >= Interface::AF_Invalid))
|
|
if(!_IsPowerOf2(af) || (af < Interface::AF_Unknown) || (af >= Interface::AF_Invalid))
|
|
{
|
|
{
|
|
- m_errHandler.emitError("Invalid address family: %d!", af);
|
|
|
|
|
|
+ m_rNotifyer.emitError("Invalid address family: %d!", af);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
- if(m_ifb.proto != (IfaceProtos)_FLAG_TO_ENUM(af))
|
|
|
|
|
|
+ IfaceProtos proto = (IfaceProtos)_FLAG_TO_ENUM(af);
|
|
|
|
+
|
|
|
|
+ if(m_ifb.proto != proto)
|
|
{
|
|
{
|
|
- m_ifb.proto = (IfaceProtos)_FLAG_TO_ENUM(af);
|
|
|
|
- emit af_Changed(af);
|
|
|
|
- emit afName_Changed();
|
|
|
|
|
|
+ m_ifb.proto = proto;
|
|
|
|
+ emit afChanged(af);
|
|
|
|
+ emit afNameChanged();
|
|
|
|
+ m_rNotifyer.filterPropertyChanged();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-QString Interface::method(void) const
|
|
|
|
|
|
+QString Interface::methodName(void) const
|
|
{
|
|
{
|
|
return ::GetIfaceMethodStr(m_ifb.method);
|
|
return ::GetIfaceMethodStr(m_ifb.method);
|
|
}
|
|
}
|
|
|
|
|
|
-int Interface::itfMethod(void) const
|
|
|
|
|
|
+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 (int)m_ifb.method;
|
|
|
|
|
|
+ return &m_inet;
|
|
}
|
|
}
|
|
|
|
|
|
-IPv4Address* Interface::ipAddress(void)
|
|
|
|
|
|
+/////////////////////////////////////////////////////////////////////////////
|
|
|
|
+
|
|
|
|
+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;
|
|
return &m_ipAddr;
|
|
}
|
|
}
|
|
|
|
|
|
-IPv4Address* Interface::netMask(void)
|
|
|
|
|
|
+IPv4Address* Static::netMask(void)
|
|
{
|
|
{
|
|
return &m_netmask;
|
|
return &m_netmask;
|
|
}
|
|
}
|
|
|
|
|
|
-IPv4Address* Interface::gateway(void)
|
|
|
|
|
|
+IPv4Address* Static::gateway(void)
|
|
{
|
|
{
|
|
return &m_gateway;
|
|
return &m_gateway;
|
|
}
|
|
}
|
|
|
|
|
|
-IPv4Address* Interface::bcastAddress(void)
|
|
|
|
|
|
+IPv4Address* Static::bcastAddress(void)
|
|
{
|
|
{
|
|
return &m_bcastAddr;
|
|
return &m_bcastAddr;
|
|
}
|
|
}
|
|
|
|
|
|
-IPv4Address* Interface::ptpAddress(void)
|
|
|
|
|
|
+IPv4Address* Static::ptpAddress(void)
|
|
{
|
|
{
|
|
return &m_ptpAddr;
|
|
return &m_ptpAddr;
|
|
}
|
|
}
|
|
|
|
|
|
-QQmlListProperty<IPv4Address> Interface::dnsServer(void)
|
|
|
|
|
|
+QQmlListProperty<IPv4Address> Static::dnsServer(void)
|
|
{
|
|
{
|
|
return QQmlListProperty<IPv4Address>(this, m_dnsList);
|
|
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 Emitter &errHandler, QObject *pParent) : QObject(pParent), m_addr(addr), m_errHandler(errHandler)
|
|
|
|
|
|
+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");
|
|
setObjectName("IPv4Address");
|
|
}
|
|
}
|
|
@@ -430,15 +583,21 @@ int IPv4Address::b3(void) const
|
|
unsigned char *pb = (unsigned char*)&m_addr.s_addr;
|
|
unsigned char *pb = (unsigned char*)&m_addr.s_addr;
|
|
return (int)pb[3];
|
|
return (int)pb[3];
|
|
}
|
|
}
|
|
-
|
|
|
|
-void IPv4Address::set_address(const QString &addr)
|
|
|
|
|
|
+
|
|
|
|
+void IPv4Address::setAddress(const QString &addr)
|
|
{
|
|
{
|
|
struct in_addr newAddr, oldAddr;
|
|
struct in_addr newAddr, oldAddr;
|
|
std::string sa = addr.toStdString();
|
|
std::string sa = addr.toStdString();
|
|
|
|
|
|
if(!inet_aton(sa.c_str(), &newAddr))
|
|
if(!inet_aton(sa.c_str(), &newAddr))
|
|
{
|
|
{
|
|
- m_errHandler.emitError("Invalid IP address: '%s'!", sa.c_str());
|
|
|
|
|
|
+ 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;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -450,73 +609,73 @@ void IPv4Address::set_address(const QString &addr)
|
|
unsigned char *pb1 = (unsigned char*)&oldAddr.s_addr;
|
|
unsigned char *pb1 = (unsigned char*)&oldAddr.s_addr;
|
|
unsigned char *pb2 = (unsigned char*)&newAddr.s_addr;
|
|
unsigned char *pb2 = (unsigned char*)&newAddr.s_addr;
|
|
if(pb1[0] != pb2[0])
|
|
if(pb1[0] != pb2[0])
|
|
- emit b0_Changed(pb2[0]);
|
|
|
|
|
|
+ emit b0Changed(pb2[0]);
|
|
if(pb1[1] != pb2[1])
|
|
if(pb1[1] != pb2[1])
|
|
- emit b1_Changed(pb2[1]);
|
|
|
|
|
|
+ emit b1Changed(pb2[1]);
|
|
if(pb1[2] != pb2[2])
|
|
if(pb1[2] != pb2[2])
|
|
- emit b2_Changed(pb2[2]);
|
|
|
|
|
|
+ emit b2Changed(pb2[2]);
|
|
if(pb1[3] != pb2[3])
|
|
if(pb1[3] != pb2[3])
|
|
- emit b3_Changed(pb2[3]);
|
|
|
|
- emit address_Changed(address());
|
|
|
|
|
|
+ emit b3Changed(pb2[3]);
|
|
|
|
+ emit addressChanged(address());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-void IPv4Address::set_b0(int b)
|
|
|
|
|
|
+void IPv4Address::setB0(int b)
|
|
{
|
|
{
|
|
unsigned char *pb = (unsigned char*)&m_addr.s_addr;
|
|
unsigned char *pb = (unsigned char*)&m_addr.s_addr;
|
|
if(!_IS_VALID_BYTE_VALUE(b))
|
|
if(!_IS_VALID_BYTE_VALUE(b))
|
|
{
|
|
{
|
|
- m_errHandler.emitError("Invalid IP address byte 0: '%d'!", b);
|
|
|
|
|
|
+ m_rNotifyer.emitError("Invalid IP address byte 0: '%d'!", b);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
if(b == (int)pb[0])
|
|
if(b == (int)pb[0])
|
|
return;
|
|
return;
|
|
pb[0] = (unsigned char)b;
|
|
pb[0] = (unsigned char)b;
|
|
- emit b0_Changed(b);
|
|
|
|
- emit address_Changed(address());
|
|
|
|
|
|
+ emit b0Changed(b);
|
|
|
|
+ emit addressChanged(address());
|
|
}
|
|
}
|
|
|
|
|
|
-void IPv4Address::set_b1(int b)
|
|
|
|
|
|
+void IPv4Address::setB1(int b)
|
|
{
|
|
{
|
|
unsigned char *pb = (unsigned char*)&m_addr.s_addr;
|
|
unsigned char *pb = (unsigned char*)&m_addr.s_addr;
|
|
if(!_IS_VALID_BYTE_VALUE(b))
|
|
if(!_IS_VALID_BYTE_VALUE(b))
|
|
{
|
|
{
|
|
- m_errHandler.emitError("Invalid IP address byte 1: '%d'!", b);
|
|
|
|
|
|
+ m_rNotifyer.emitError("Invalid IP address byte 1: '%d'!", b);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
if(b == (int)pb[1])
|
|
if(b == (int)pb[1])
|
|
return;
|
|
return;
|
|
pb[1] = (unsigned char)b;
|
|
pb[1] = (unsigned char)b;
|
|
- emit b1_Changed(b);
|
|
|
|
- emit address_Changed(address());
|
|
|
|
|
|
+ emit b1Changed(b);
|
|
|
|
+ emit addressChanged(address());
|
|
}
|
|
}
|
|
|
|
|
|
-void IPv4Address::set_b2(int b)
|
|
|
|
|
|
+void IPv4Address::setB2(int b)
|
|
{
|
|
{
|
|
unsigned char *pb = (unsigned char*)&m_addr.s_addr;
|
|
unsigned char *pb = (unsigned char*)&m_addr.s_addr;
|
|
if(!_IS_VALID_BYTE_VALUE(b))
|
|
if(!_IS_VALID_BYTE_VALUE(b))
|
|
{
|
|
{
|
|
- m_errHandler.emitError("Invalid IP address byte 2: '%d'!", b);
|
|
|
|
|
|
+ m_rNotifyer.emitError("Invalid IP address byte 2: '%d'!", b);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
if(b == (int)pb[2])
|
|
if(b == (int)pb[2])
|
|
return;
|
|
return;
|
|
pb[2] = (unsigned char)b;
|
|
pb[2] = (unsigned char)b;
|
|
- emit b2_Changed(b);
|
|
|
|
- emit address_Changed(address());
|
|
|
|
|
|
+ emit b2Changed(b);
|
|
|
|
+ emit addressChanged(address());
|
|
}
|
|
}
|
|
|
|
|
|
-void IPv4Address::set_b3(int b)
|
|
|
|
|
|
+void IPv4Address::setB3(int b)
|
|
{
|
|
{
|
|
unsigned char *pb = (unsigned char*)&m_addr.s_addr;
|
|
unsigned char *pb = (unsigned char*)&m_addr.s_addr;
|
|
if(!_IS_VALID_BYTE_VALUE(b))
|
|
if(!_IS_VALID_BYTE_VALUE(b))
|
|
{
|
|
{
|
|
- m_errHandler.emitError("Invalid IP address byte 3: '%d'!", b);
|
|
|
|
|
|
+ m_rNotifyer.emitError("Invalid IP address byte 3: '%d'!", b);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
if(b == (int)pb[3])
|
|
if(b == (int)pb[3])
|
|
return;
|
|
return;
|
|
pb[3] = (unsigned char)b;
|
|
pb[3] = (unsigned char)b;
|
|
- emit b3_Changed(b);
|
|
|
|
- emit address_Changed(address());
|
|
|
|
|
|
+ emit b3Changed(b);
|
|
|
|
+ emit addressChanged(address());
|
|
}
|
|
}
|