netinterfaces.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "netinterfaces.h"
  2. /////////////////////////////////////////////////////////////////////////////
  3. /////////////////////////////////////////////////////////////////////////////
  4. NetInterfaces::NetInterfaces(QObject *pParent) : QObject(pParent)
  5. {
  6. setObjectName("NetInterfaces");
  7. }
  8. NetInterfaces::~NetInterfaces(void)
  9. {
  10. reset();
  11. }
  12. void NetInterfaces::classBegin()
  13. {
  14. }
  15. void NetInterfaces::componentComplete()
  16. {
  17. }
  18. void NetInterfaces::reset(void)
  19. {
  20. for(auto it = m_itfList.begin(); it != m_itfList.end(); it++)
  21. {
  22. Interface *pi = *it;
  23. delete pi;
  24. }
  25. m_itfList.clear();
  26. m_eni._reset();
  27. }
  28. bool NetInterfaces::initialize(void)
  29. {
  30. bool bRet = false;
  31. reset();
  32. if((bRet = ::ParseEtcNetworkInterfaces(m_eni)))
  33. {
  34. for(auto it = m_eni.ibl.begin(); it != m_eni.ibl.end(); it++)
  35. {
  36. ITF_IFACE_BLOCK &ibl = *it;
  37. m_itfList.push_back(new Interface(ibl));
  38. }
  39. }
  40. return bRet;
  41. }
  42. QVariantList NetInterfaces::getInterface(const QString &itfName)
  43. {
  44. QVariantList list;
  45. list.clear();
  46. if(m_eni.ibl.size() > 0)
  47. {
  48. for(auto it = m_itfList.begin(); it != m_itfList.end(); it++)
  49. {
  50. Interface *pi = *it;
  51. const ITF_IFACE_BLOCK &ibl = pi->getIface();
  52. if( itfName == ibl.cfgName.c_str() &&
  53. ibl.proto == IFP_Inet &&
  54. (ibl.method == IFM_Static || ibl.method == IFM_Dhcp))
  55. {
  56. QVariant var = QVariant::fromValue(pi);
  57. list.append(var);
  58. }
  59. }
  60. }
  61. return list;
  62. }
  63. /////////////////////////////////////////////////////////////////////////////
  64. Interface::Interface(ITF_IFACE_BLOCK &ifb, QObject *pParent) : QObject(pParent), m_ifb(ifb)
  65. {
  66. setObjectName("Interface");
  67. }
  68. Interface::~Interface(void)
  69. {
  70. }
  71. QString Interface::name(void) const
  72. {
  73. return QString::fromStdString(m_ifb.cfgName);
  74. }
  75. QString Interface::ipAddress(void) const
  76. {
  77. if(m_ifb.proto == IFP_Inet && m_ifb.method == IFM_Static)
  78. return inet_ntoa(m_ifb.inet4s.addr);
  79. else
  80. return "";
  81. }
  82. QString Interface::family(void) const
  83. {
  84. return ::GetIfaceProtoStr(m_ifb.proto);
  85. }
  86. QString Interface::method(void) const
  87. {
  88. return ::GetIfaceMethodStr(m_ifb.method);
  89. }