netinterfaces.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. #include "netinterfaces.h"
  2. #ifndef _countof
  3. #define _countof(a) (sizeof(a) / sizeof(*a))
  4. #endif // _countof
  5. #define _IS_VALID_BYTE_VALUE(b) (((b) >= 0) && ((b) <= 255))
  6. /////////////////////////////////////////////////////////////////////////////
  7. /////////////////////////////////////////////////////////////////////////////
  8. NetInterfaces::NetInterfaces(QObject *pParent) : QObject(pParent)
  9. {
  10. setObjectName("NetInterfaces");
  11. m_itfFilterAF = Interface::AF_Inet;
  12. m_itfFilterMethod = Interface::IM_Static;
  13. }
  14. NetInterfaces::~NetInterfaces(void)
  15. {
  16. reset();
  17. }
  18. void NetInterfaces::classBegin()
  19. {
  20. }
  21. void NetInterfaces::componentComplete()
  22. {
  23. if(!initialize())
  24. emitError("NetInterfaces::initialize failed!");
  25. }
  26. void NetInterfaces::reset(void)
  27. {
  28. Interface *pItf;
  29. for(int i = 0; i < m_interfaces.count(); i++)
  30. {
  31. if((pItf = m_interfaces.at(i)))
  32. delete pItf;
  33. }
  34. m_interfaces.clear();
  35. emit interfaces_Changed();
  36. m_eni._reset();
  37. }
  38. bool NetInterfaces::initialize(void)
  39. {
  40. bool bRet;
  41. reset();
  42. if((bRet = ::ParseEtcNetworkInterfaces(m_eni)))
  43. {
  44. for(auto it = m_eni.ibl.begin(); it != m_eni.ibl.end(); it++)
  45. {
  46. ITF_IFACE_BLOCK &ibl = *it;
  47. m_interfaces.append(new Interface(ibl, static_cast<const Emitter&>(*this), this));
  48. }
  49. emit interfaces_Changed();
  50. }
  51. return bRet;
  52. }
  53. bool NetInterfaces::save(void)
  54. {
  55. return ::WriteEtcNetworkInterfaces(m_eni, NULL);
  56. }
  57. bool NetInterfaces::saveAs(const QString &path)
  58. {
  59. if(!path.length())
  60. return false;
  61. std::string p = path.toStdString();
  62. const char *pszPath = p.c_str();
  63. return ::WriteEtcNetworkInterfaces(m_eni, pszPath);
  64. }
  65. void NetInterfaces::emitError(const char *pszFormatStr, ...) const
  66. {
  67. va_list args;
  68. va_start(args, pszFormatStr);
  69. QString qs = QString::vasprintf(pszFormatStr, args);
  70. va_end (args);
  71. emit sigError(qs);
  72. }
  73. QVariantList NetInterfaces::getInterface(const QString &itfName)
  74. {
  75. QVariantList list;
  76. list.clear();
  77. if(m_eni.ibl.size() > 0)
  78. {
  79. for(int i = 0; i < m_interfaces.count(); i++)
  80. {
  81. Interface *pi = m_interfaces.at(i);
  82. const ITF_IFACE_BLOCK &ibl = pi->getIface();
  83. if( itfName == ibl.cfgName.c_str() &&
  84. ibl.proto == IFP_Inet &&
  85. (ibl.method == IFM_Static || ibl.method == IFM_Dhcp))
  86. {
  87. QVariant var = QVariant::fromValue(pi);
  88. list.append(var);
  89. }
  90. }
  91. }
  92. return list;
  93. }
  94. QVariant NetInterfaces::newInterface(QString name, int af, int method, QString cfg)
  95. {
  96. m_eni.ibl.emplace_back();
  97. ITF_IFACE_BLOCK &rib = m_eni.ibl.back();
  98. rib.cfgName = name.toStdString();
  99. rib.proto = (IfaceProtos)af;
  100. rib.method = (IfaceMethods)method;
  101. Interface *pi = new Interface(rib, static_cast<const Emitter&>(*this), this);
  102. m_interfaces.append(pi);
  103. emit interfaces_Changed();
  104. return QVariant::fromValue(pi);
  105. }
  106. QQmlListProperty<Interface> NetInterfaces::interfaces(void)
  107. {
  108. return QQmlListProperty<Interface>(this, m_interfaces);
  109. }
  110. QQmlListProperty<Interface> NetInterfaces::filteredInterfaces(void)
  111. {
  112. m_filteredInterfaces.clear();
  113. for(int i = 0; i < m_interfaces.count(); i++)
  114. {
  115. Interface *pi = m_interfaces.at(i);
  116. const ITF_IFACE_BLOCK &ibl = pi->getIface();
  117. if( m_itfFilterName == ibl.cfgName.c_str() &&
  118. (int)ibl.proto == m_itfFilterAF &&
  119. (int)ibl.method == m_itfFilterMethod)
  120. {
  121. m_filteredInterfaces.append(pi);
  122. }
  123. }
  124. return QQmlListProperty<Interface>(this, m_filteredInterfaces);
  125. }
  126. const QString& NetInterfaces::itfFilterName(void) const
  127. {
  128. return m_itfFilterName;
  129. }
  130. void NetInterfaces::set_itfFilterName(const QString &val)
  131. {
  132. if(val != m_itfFilterName)
  133. {
  134. m_itfFilterName = val;
  135. emit itfFilterName_Changed(m_itfFilterName);
  136. emit filteredInterfaces_Changed();
  137. }
  138. }
  139. int NetInterfaces::itfFilterAF(void) const
  140. {
  141. return m_itfFilterAF;
  142. }
  143. void NetInterfaces::set_itfFilterAF(int af)
  144. {
  145. if(af <= Interface::AF_Unknown || af >= Interface::AF_Invalid)
  146. {
  147. emitError("Invalid address family filter: %d!", af);
  148. return;
  149. }
  150. if(m_itfFilterAF != af)
  151. {
  152. m_itfFilterAF = af;
  153. emit itfFilterAF_Changed(af);
  154. emit filteredInterfaces_Changed();
  155. }
  156. }
  157. int NetInterfaces::itfFilterMethod(void) const
  158. {
  159. return m_itfFilterMethod;
  160. }
  161. void NetInterfaces::set_itfFilterMethod(int method)
  162. {
  163. if(method <= Interface::IM_Unknown || method >= Interface::IM_Invalid)
  164. {
  165. emitError("Invalid method filter: %d!", method);
  166. return;
  167. }
  168. if(m_itfFilterMethod != method)
  169. {
  170. m_itfFilterMethod = method;
  171. emit itfFilterMethod_Changed(method);
  172. emit filteredInterfaces_Changed();
  173. }
  174. }
  175. /////////////////////////////////////////////////////////////////////////////
  176. Interface::Interface(ITF_IFACE_BLOCK &ifb, const Emitter &errHandler, QObject *pParent) : QObject(pParent),
  177. m_ifb(ifb),
  178. m_ipAddr(ifb.inet4s.addr, errHandler, this),
  179. m_netmask(ifb.inet4s.netmask, errHandler, this),
  180. m_gateway(ifb.inet4s.gate, errHandler, this),
  181. m_bcastAddr(ifb.inet4s.bcast, errHandler, this),
  182. m_ptpAddr(ifb.inet4s.pointopoint, errHandler, this),
  183. m_errHandler(errHandler)
  184. {
  185. setObjectName("Interface");
  186. for(size_t i = 0; i < _countof(m_ifb.inet4s.namesvr); i++)
  187. {
  188. IPv4Address *addr = new IPv4Address(m_ifb.inet4s.namesvr[i], errHandler, this);
  189. m_dnsList.append(addr);
  190. }
  191. #if 0
  192. for(size_t i = 0; i < _countof(m_ifb.inet4s.namesvr); i++)
  193. {
  194. if(m_ifb.inet4s.namesvr[i].s_addr)
  195. {
  196. QString qs(inet_ntoa(m_ifb.inet4s.namesvr[i]));
  197. m_dnsList.append(qs);
  198. }
  199. else
  200. {
  201. QString qs("");
  202. m_dnsList.append(qs);
  203. }
  204. }
  205. #endif
  206. }
  207. Interface::~Interface(void)
  208. {
  209. IPv4Address *addr;
  210. for(int i = 0; i < m_dnsList.count(); i++)
  211. {
  212. if((addr = m_dnsList.at(i)))
  213. delete addr;
  214. }
  215. }
  216. QString Interface::name(void) const
  217. {
  218. return QString::fromStdString(m_ifb.cfgName);
  219. }
  220. QString Interface::family(void) const
  221. {
  222. return ::GetIfaceProtoStr(m_ifb.proto);
  223. }
  224. int Interface::af(void) const
  225. {
  226. return (int)m_ifb.proto;
  227. }
  228. QString Interface::method(void) const
  229. {
  230. return ::GetIfaceMethodStr(m_ifb.method);
  231. }
  232. int Interface::itfMethod(void) const
  233. {
  234. return (int)m_ifb.method;
  235. }
  236. IPv4Address* Interface::ipAddress(void)
  237. {
  238. return &m_ipAddr;
  239. }
  240. IPv4Address* Interface::netMask(void)
  241. {
  242. return &m_netmask;
  243. }
  244. IPv4Address* Interface::gateway(void)
  245. {
  246. return &m_gateway;
  247. }
  248. IPv4Address* Interface::bcastAddress(void)
  249. {
  250. return &m_bcastAddr;
  251. }
  252. IPv4Address* Interface::ptpAddress(void)
  253. {
  254. return &m_ptpAddr;
  255. }
  256. QQmlListProperty<IPv4Address> Interface::dnsServer(void)
  257. {
  258. return QQmlListProperty<IPv4Address>(this, m_dnsList);
  259. }
  260. /////////////////////////////////////////////////////////////////////////////
  261. IPv4Address::IPv4Address(struct in_addr &addr, const Emitter &errHandler, QObject *pParent) : QObject(pParent), m_addr(addr), m_errHandler(errHandler)
  262. {
  263. setObjectName("IPv4Address");
  264. }
  265. IPv4Address::~IPv4Address(void)
  266. {
  267. }
  268. QString IPv4Address::address(void) const
  269. {
  270. return inet_ntoa(m_addr);
  271. }
  272. int IPv4Address::b0(void) const
  273. {
  274. unsigned char *pb = (unsigned char*)&m_addr.s_addr;
  275. return (int)pb[0];
  276. }
  277. int IPv4Address::b1(void) const
  278. {
  279. unsigned char *pb = (unsigned char*)&m_addr.s_addr;
  280. return (int)pb[1];
  281. }
  282. int IPv4Address::b2(void) const
  283. {
  284. unsigned char *pb = (unsigned char*)&m_addr.s_addr;
  285. return (int)pb[2];
  286. }
  287. int IPv4Address::b3(void) const
  288. {
  289. unsigned char *pb = (unsigned char*)&m_addr.s_addr;
  290. return (int)pb[3];
  291. }
  292. void IPv4Address::set_address(const QString &addr)
  293. {
  294. struct in_addr newAddr, oldAddr;
  295. std::string sa = addr.toStdString();
  296. if(!inet_aton(sa.c_str(), &newAddr))
  297. {
  298. m_errHandler.emitError("Invalid IP address: '%s'!", sa.c_str());
  299. return;
  300. }
  301. if(m_addr.s_addr != newAddr.s_addr)
  302. {
  303. oldAddr.s_addr = m_addr.s_addr;
  304. m_addr.s_addr = newAddr.s_addr;
  305. unsigned char *pb1 = (unsigned char*)&oldAddr.s_addr;
  306. unsigned char *pb2 = (unsigned char*)&newAddr.s_addr;
  307. if(pb1[0] != pb2[0])
  308. emit b0_Changed(pb2[0]);
  309. if(pb1[1] != pb2[1])
  310. emit b1_Changed(pb2[1]);
  311. if(pb1[2] != pb2[2])
  312. emit b2_Changed(pb2[2]);
  313. if(pb1[3] != pb2[3])
  314. emit b3_Changed(pb2[3]);
  315. emit address_Changed(address());
  316. }
  317. }
  318. void IPv4Address::set_b0(int b)
  319. {
  320. unsigned char *pb = (unsigned char*)&m_addr.s_addr;
  321. if(!_IS_VALID_BYTE_VALUE(b))
  322. {
  323. m_errHandler.emitError("Invalid IP address byte 0: '%d'!", b);
  324. return;
  325. }
  326. if(b == (int)pb[0])
  327. return;
  328. pb[0] = (unsigned char)b;
  329. emit b0_Changed(b);
  330. emit address_Changed(address());
  331. }
  332. void IPv4Address::set_b1(int b)
  333. {
  334. unsigned char *pb = (unsigned char*)&m_addr.s_addr;
  335. if(!_IS_VALID_BYTE_VALUE(b))
  336. {
  337. m_errHandler.emitError("Invalid IP address byte 1: '%d'!", b);
  338. return;
  339. }
  340. if(b == (int)pb[1])
  341. return;
  342. pb[1] = (unsigned char)b;
  343. emit b1_Changed(b);
  344. emit address_Changed(address());
  345. }
  346. void IPv4Address::set_b2(int b)
  347. {
  348. unsigned char *pb = (unsigned char*)&m_addr.s_addr;
  349. if(!_IS_VALID_BYTE_VALUE(b))
  350. {
  351. m_errHandler.emitError("Invalid IP address byte 2: '%d'!", b);
  352. return;
  353. }
  354. if(b == (int)pb[2])
  355. return;
  356. pb[2] = (unsigned char)b;
  357. emit b2_Changed(b);
  358. emit address_Changed(address());
  359. }
  360. void IPv4Address::set_b3(int b)
  361. {
  362. unsigned char *pb = (unsigned char*)&m_addr.s_addr;
  363. if(!_IS_VALID_BYTE_VALUE(b))
  364. {
  365. m_errHandler.emitError("Invalid IP address byte 3: '%d'!", b);
  366. return;
  367. }
  368. if(b == (int)pb[3])
  369. return;
  370. pb[3] = (unsigned char)b;
  371. emit b3_Changed(b);
  372. emit address_Changed(address());
  373. }