|
@@ -4,6 +4,8 @@
|
|
#define _countof(a) (sizeof(a) / sizeof(*a))
|
|
#define _countof(a) (sizeof(a) / sizeof(*a))
|
|
#endif // _countof
|
|
#endif // _countof
|
|
|
|
|
|
|
|
+#define _IS_VALID_BYTE_VALUE(b) (((b) >= 0) && ((b) <= 255))
|
|
|
|
+
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
@@ -46,12 +48,38 @@ bool NetInterfaces::initialize(void)
|
|
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;
|
|
- m_itfList.push_back(new Interface(ibl));
|
|
|
|
|
|
+ m_itfList.push_back(new Interface(ibl, static_cast<const Emitter&>(*this), this));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return bRet;
|
|
return bRet;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+bool NetInterfaces::save(void)
|
|
|
|
+{
|
|
|
|
+ return ::WriteEtcNetworkInterfaces(m_eni, NULL);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+bool NetInterfaces::save(QString path)
|
|
|
|
+{
|
|
|
|
+ std::string p;
|
|
|
|
+ const char *pszPath = NULL;
|
|
|
|
+ if(path.length() > 0)
|
|
|
|
+ {
|
|
|
|
+ p = path.toStdString();
|
|
|
|
+ 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 NetInterfaces::getInterface(const QString &itfName)
|
|
{
|
|
{
|
|
QVariantList list;
|
|
QVariantList list;
|
|
@@ -79,10 +107,23 @@ QVariantList NetInterfaces::getInterface(const QString &itfName)
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
-Interface::Interface(ITF_IFACE_BLOCK &ifb, QObject *pParent) : QObject(pParent), m_ifb(ifb)
|
|
|
|
|
|
+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)
|
|
|
|
+ /*, m_dnsSvr(ifb.inet4s.namesvr)*/
|
|
{
|
|
{
|
|
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);
|
|
|
|
+ }
|
|
|
|
+#if 0
|
|
for(size_t i = 0; i < _countof(m_ifb.inet4s.namesvr); i++)
|
|
for(size_t i = 0; i < _countof(m_ifb.inet4s.namesvr); i++)
|
|
{
|
|
{
|
|
if(m_ifb.inet4s.namesvr[i].s_addr)
|
|
if(m_ifb.inet4s.namesvr[i].s_addr)
|
|
@@ -96,10 +137,18 @@ Interface::Interface(ITF_IFACE_BLOCK &ifb, QObject *pParent) : QObject(pParent),
|
|
m_dnsList.append(qs);
|
|
m_dnsList.append(qs);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+#endif
|
|
}
|
|
}
|
|
|
|
|
|
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;
|
|
|
|
+ }
|
|
}
|
|
}
|
|
|
|
|
|
QString Interface::name(void) const
|
|
QString Interface::name(void) const
|
|
@@ -117,25 +166,199 @@ QString Interface::method(void) const
|
|
return ::GetIfaceMethodStr(m_ifb.method);
|
|
return ::GetIfaceMethodStr(m_ifb.method);
|
|
}
|
|
}
|
|
|
|
|
|
-QString Interface::ipAddress(void) const
|
|
|
|
|
|
+IPv4Address* Interface::ipAddress(void)
|
|
{
|
|
{
|
|
|
|
+ return &m_ipAddr;
|
|
// if(m_ifb.proto == IFP_Inet && m_ifb.method == IFM_Static)
|
|
// if(m_ifb.proto == IFP_Inet && m_ifb.method == IFM_Static)
|
|
- return inet_ntoa(m_ifb.inet4s.addr);
|
|
|
|
|
|
+// return inet_ntoa(m_ifb.inet4s.addr);
|
|
// else
|
|
// else
|
|
// return "";
|
|
// return "";
|
|
}
|
|
}
|
|
|
|
|
|
-QString Interface::netMask(void) const
|
|
|
|
|
|
+IPv4Address* Interface::netMask(void)
|
|
|
|
+{
|
|
|
|
+ return &m_netmask;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+IPv4Address* Interface::gateway(void)
|
|
|
|
+{
|
|
|
|
+ return &m_gateway;
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+IPv4Address* Interface::bcastAddress(void)
|
|
{
|
|
{
|
|
- return inet_ntoa(m_ifb.inet4s.netmask);
|
|
|
|
|
|
+ return &m_bcastAddr;
|
|
}
|
|
}
|
|
|
|
|
|
-QString Interface::gateway(void) const
|
|
|
|
|
|
+IPv4Address* Interface::ptpAddress(void)
|
|
{
|
|
{
|
|
- return inet_ntoa(m_ifb.inet4s.gate);
|
|
|
|
|
|
+ return &m_ptpAddr;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+#if 0
|
|
QStringList Interface::dnsServers(void) const
|
|
QStringList Interface::dnsServers(void) const
|
|
{
|
|
{
|
|
return m_dnsList;
|
|
return m_dnsList;
|
|
}
|
|
}
|
|
|
|
+#endif
|
|
|
|
+
|
|
|
|
+QQmlListProperty<IPv4Address> Interface::dnsServer(void)
|
|
|
|
+{
|
|
|
|
+ return QQmlListProperty<IPv4Address>(this, m_dnsList);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/*QString Interface::dns1(void) const
|
|
|
|
+{
|
|
|
|
+ return inet_ntoa(m_ifb.inet4s.namesvr[0]);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+QString Interface::dns2(void) const
|
|
|
|
+{
|
|
|
|
+ return inet_ntoa(m_ifb.inet4s.namesvr[1]);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+QString Interface::dns3(void) const
|
|
|
|
+{
|
|
|
|
+ return inet_ntoa(m_ifb.inet4s.namesvr[2]);
|
|
|
|
+}*/
|
|
|
|
+
|
|
|
|
+/////////////////////////////////////////////////////////////////////////////
|
|
|
|
+
|
|
|
|
+IPv4Address::IPv4Address(struct in_addr &addr, const Emitter &errHandler, QObject *pParent) : QObject(pParent), m_addr(addr), m_errHandler(errHandler)
|
|
|
|
+{
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+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());
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/////////////////////////////////////////////////////////////////////////////
|
|
|
|
+
|
|
|
|
+#if 0
|
|
|
|
+Nameservers::Nameservers(struct in_addr (&namesvr)[3], QObject *pParent) : QObject(pParent), m_dnsSvr(namesvr)
|
|
|
|
+{
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+Nameservers::~Nameservers(void)
|
|
|
|
+{
|
|
|
|
+}
|
|
|
|
+#endif
|