netinterfaces.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include "netinterfaces.h"
  2. #ifndef _countof
  3. #define _countof(a) (sizeof(a) / sizeof(*a))
  4. #endif // _countof
  5. /////////////////////////////////////////////////////////////////////////////
  6. /////////////////////////////////////////////////////////////////////////////
  7. NetInterfaces::NetInterfaces(QObject *pParent) : QObject(pParent)
  8. {
  9. setObjectName("NetInterfaces");
  10. }
  11. NetInterfaces::~NetInterfaces(void)
  12. {
  13. reset();
  14. }
  15. void NetInterfaces::classBegin()
  16. {
  17. }
  18. void NetInterfaces::componentComplete()
  19. {
  20. }
  21. void NetInterfaces::reset(void)
  22. {
  23. for(auto it = m_itfList.begin(); it != m_itfList.end(); it++)
  24. {
  25. Interface *pi = *it;
  26. delete pi;
  27. }
  28. m_itfList.clear();
  29. m_eni._reset();
  30. }
  31. bool NetInterfaces::initialize(void)
  32. {
  33. bool bRet = false;
  34. reset();
  35. if((bRet = ::ParseEtcNetworkInterfaces(m_eni)))
  36. {
  37. for(auto it = m_eni.ibl.begin(); it != m_eni.ibl.end(); it++)
  38. {
  39. ITF_IFACE_BLOCK &ibl = *it;
  40. m_itfList.push_back(new Interface(ibl));
  41. }
  42. }
  43. return bRet;
  44. }
  45. QVariantList NetInterfaces::getInterface(const QString &itfName)
  46. {
  47. QVariantList list;
  48. list.clear();
  49. if(m_eni.ibl.size() > 0)
  50. {
  51. for(auto it = m_itfList.begin(); it != m_itfList.end(); it++)
  52. {
  53. Interface *pi = *it;
  54. const ITF_IFACE_BLOCK &ibl = pi->getIface();
  55. if( itfName == ibl.cfgName.c_str() &&
  56. ibl.proto == IFP_Inet &&
  57. (ibl.method == IFM_Static || ibl.method == IFM_Dhcp))
  58. {
  59. QVariant var = QVariant::fromValue(pi);
  60. list.append(var);
  61. }
  62. }
  63. }
  64. return list;
  65. }
  66. /////////////////////////////////////////////////////////////////////////////
  67. Interface::Interface(ITF_IFACE_BLOCK &ifb, QObject *pParent) : QObject(pParent), m_ifb(ifb)
  68. {
  69. setObjectName("Interface");
  70. for(size_t i = 0; i < _countof(m_ifb.inet4s.namesvr); i++)
  71. {
  72. if(m_ifb.inet4s.namesvr[i].s_addr)
  73. {
  74. QString qs(inet_ntoa(m_ifb.inet4s.namesvr[i]));
  75. m_dnsList.append(qs);
  76. }
  77. else
  78. {
  79. QString qs("");
  80. m_dnsList.append(qs);
  81. }
  82. }
  83. }
  84. Interface::~Interface(void)
  85. {
  86. }
  87. QString Interface::name(void) const
  88. {
  89. return QString::fromStdString(m_ifb.cfgName);
  90. }
  91. QString Interface::family(void) const
  92. {
  93. return ::GetIfaceProtoStr(m_ifb.proto);
  94. }
  95. QString Interface::method(void) const
  96. {
  97. return ::GetIfaceMethodStr(m_ifb.method);
  98. }
  99. QString Interface::ipAddress(void) const
  100. {
  101. // if(m_ifb.proto == IFP_Inet && m_ifb.method == IFM_Static)
  102. return inet_ntoa(m_ifb.inet4s.addr);
  103. // else
  104. // return "";
  105. }
  106. QString Interface::netMask(void) const
  107. {
  108. return inet_ntoa(m_ifb.inet4s.netmask);
  109. }
  110. QString Interface::gateway(void) const
  111. {
  112. return inet_ntoa(m_ifb.inet4s.gate);
  113. }
  114. QStringList Interface::dnsServers(void) const
  115. {
  116. return m_dnsList;
  117. }