netinterfaces.cpp 10 KB

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