netinterfaces.cpp 11 KB

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