|
@@ -1,4 +1,10 @@
|
|
|
#include "netinterfaces.h"
|
|
|
+#include "../debug.h"
|
|
|
+
|
|
|
+#if 1
|
|
|
+#undef TRACE
|
|
|
+#define TRACE(...) (void)0
|
|
|
+#endif
|
|
|
|
|
|
#ifndef _countof
|
|
|
#define _countof(a) (sizeof(a) / sizeof(*a))
|
|
@@ -6,34 +12,80 @@
|
|
|
|
|
|
#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(!_IsPowerOf2(n))
|
|
|
+ return -1;
|
|
|
+ int count = 0;
|
|
|
+ while(n)
|
|
|
+ {
|
|
|
+ count++;
|
|
|
+ n >>= 1;
|
|
|
+ }
|
|
|
+ return count;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+#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_Inet;
|
|
|
- m_itfFilterMethod = Interface::IM_Static;
|
|
|
+ m_itfFilterAF = Interface::AF_Unknown;
|
|
|
+ m_itfFilterMethod = Interface::IM_Unknown;
|
|
|
+ m_itfFilterName.clear();
|
|
|
+ TRACE("%s\n", __FUNCTION__);
|
|
|
}
|
|
|
|
|
|
NetInterfaces::~NetInterfaces(void)
|
|
|
{
|
|
|
reset();
|
|
|
+ TRACE("%s\n", __FUNCTION__);
|
|
|
}
|
|
|
|
|
|
void NetInterfaces::classBegin()
|
|
|
{
|
|
|
+ TRACE("%s\n", __FUNCTION__);
|
|
|
+ if(!initialize())
|
|
|
+ {
|
|
|
+ TRACE("initialize failed!\n");
|
|
|
+ emitError("NetInterfaces::initialize failed!");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
void NetInterfaces::componentComplete()
|
|
|
{
|
|
|
- if(!initialize())
|
|
|
- emitError("NetInterfaces::initialize failed!");
|
|
|
+ TRACE("%s\n", __FUNCTION__);
|
|
|
}
|
|
|
|
|
|
void NetInterfaces::reset(void)
|
|
|
{
|
|
|
Interface *pItf;
|
|
|
+ TRACE("%s\n", __FUNCTION__);
|
|
|
|
|
|
for(int i = 0; i < m_interfaces.count(); i++)
|
|
|
{
|
|
@@ -50,13 +102,18 @@ bool NetInterfaces::initialize(void)
|
|
|
{
|
|
|
bool bRet;
|
|
|
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++)
|
|
|
{
|
|
|
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");
|
|
|
}
|
|
|
|
|
|
emit interfaces_Changed();
|
|
@@ -88,7 +145,7 @@ void NetInterfaces::emitError(const char *pszFormatStr, ...) const
|
|
|
emit sigError(qs);
|
|
|
}
|
|
|
|
|
|
-QVariantList NetInterfaces::getInterface(const QString &itfName)
|
|
|
+/*QVariantList NetInterfaces::getInterface(const QString &itfName)
|
|
|
{
|
|
|
QVariantList list;
|
|
|
list.clear();
|
|
@@ -111,17 +168,34 @@ QVariantList NetInterfaces::getInterface(const QString &itfName)
|
|
|
}
|
|
|
|
|
|
return list;
|
|
|
-}
|
|
|
+}*/
|
|
|
|
|
|
-QVariant NetInterfaces::newInterface(QString name, int af, int method, QString cfg)
|
|
|
+QVariant NetInterfaces::newInterface(QString name, int af, int method)
|
|
|
{
|
|
|
+ if(name.isNull() || name.isEmpty())
|
|
|
+ {
|
|
|
+ emitError("Invalid or empty interface name!");
|
|
|
+ return QVariant();
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!_IsPowerOf2(af) || (af <= Interface::AF_Unknown) || (af >= Interface::AF_Invalid))
|
|
|
+ {
|
|
|
+ emitError("Invalid address family: %d!", af);
|
|
|
+ return QVariant();
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!_IsPowerOf2(method) || (method <= Interface::IM_Unknown) || (method >= Interface::IM_Invalid))
|
|
|
+ {
|
|
|
+ emitError("Invalid method: %d!", method);
|
|
|
+ return QVariant();
|
|
|
+ }
|
|
|
+
|
|
|
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;
|
|
|
+ rib.proto = (IfaceProtos)_FLAG_TO_ENUM(af);
|
|
|
+ rib.method = (IfaceMethods)_FLAG_TO_ENUM(method);
|
|
|
Interface *pi = new Interface(rib, static_cast<const Emitter&>(*this), this);
|
|
|
-
|
|
|
m_interfaces.append(pi);
|
|
|
emit interfaces_Changed();
|
|
|
return QVariant::fromValue(pi);
|
|
@@ -141,9 +215,9 @@ QQmlListProperty<Interface> NetInterfaces::filteredInterfaces(void)
|
|
|
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)
|
|
|
+ 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);
|
|
|
}
|
|
@@ -165,6 +239,7 @@ void NetInterfaces::set_itfFilterName(const QString &val)
|
|
|
emit itfFilterName_Changed(m_itfFilterName);
|
|
|
emit filteredInterfaces_Changed();
|
|
|
}
|
|
|
+ TRACE("%s\n", __FUNCTION__);
|
|
|
}
|
|
|
|
|
|
int NetInterfaces::itfFilterAF(void) const
|
|
@@ -174,7 +249,9 @@ int NetInterfaces::itfFilterAF(void) const
|
|
|
|
|
|
void NetInterfaces::set_itfFilterAF(int af)
|
|
|
{
|
|
|
- if(af <= Interface::AF_Unknown || af >= Interface::AF_Invalid)
|
|
|
+ TRACE("%s\n", __FUNCTION__);
|
|
|
+
|
|
|
+ if(af < Interface::AF_Unknown || af >= Interface::AF_Invalid)
|
|
|
{
|
|
|
emitError("Invalid address family filter: %d!", af);
|
|
|
return;
|
|
@@ -195,7 +272,9 @@ int NetInterfaces::itfFilterMethod(void) const
|
|
|
|
|
|
void NetInterfaces::set_itfFilterMethod(int method)
|
|
|
{
|
|
|
- if(method <= Interface::IM_Unknown || method >= Interface::IM_Invalid)
|
|
|
+ TRACE("%s\n", __FUNCTION__);
|
|
|
+
|
|
|
+ if(method < Interface::IM_Unknown || method >= Interface::IM_Invalid)
|
|
|
{
|
|
|
emitError("Invalid method filter: %d!", method);
|
|
|
return;
|
|
@@ -226,21 +305,7 @@ Interface::Interface(ITF_IFACE_BLOCK &ifb, const Emitter &errHandler, QObject *p
|
|
|
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
|
|
|
+ TRACE("%s\n", __FUNCTION__);
|
|
|
}
|
|
|
|
|
|
Interface::~Interface(void)
|
|
@@ -252,6 +317,7 @@ Interface::~Interface(void)
|
|
|
if((addr = m_dnsList.at(i)))
|
|
|
delete addr;
|
|
|
}
|
|
|
+ TRACE("%s\n", __FUNCTION__);
|
|
|
}
|
|
|
|
|
|
QString Interface::name(void) const
|
|
@@ -259,7 +325,7 @@ QString Interface::name(void) const
|
|
|
return QString::fromStdString(m_ifb.cfgName);
|
|
|
}
|
|
|
|
|
|
-QString Interface::family(void) const
|
|
|
+QString Interface::afName(void) const
|
|
|
{
|
|
|
return ::GetIfaceProtoStr(m_ifb.proto);
|
|
|
}
|
|
@@ -269,6 +335,22 @@ int Interface::af(void) const
|
|
|
return (int)m_ifb.proto;
|
|
|
}
|
|
|
|
|
|
+void Interface::set_af(int af)
|
|
|
+{
|
|
|
+ if(!_IsPowerOf2(af) || (af < Interface::AF_Unknown) || (af >= Interface::AF_Invalid))
|
|
|
+ {
|
|
|
+ m_errHandler.emitError("Invalid address family: %d!", af);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(m_ifb.proto != (IfaceProtos)_FLAG_TO_ENUM(af))
|
|
|
+ {
|
|
|
+ m_ifb.proto = (IfaceProtos)_FLAG_TO_ENUM(af);
|
|
|
+ emit af_Changed(af);
|
|
|
+ emit afName_Changed();
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
QString Interface::method(void) const
|
|
|
{
|
|
|
return ::GetIfaceMethodStr(m_ifb.method);
|