CPLNetworks.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /*====================================================================*
  2. Copyright (c) 2020 Qualcomm Technologies, Inc.
  3. All Rights Reserved.
  4. Confidential and Proprietary - Qualcomm Technologies, Inc.
  5. ******************************************************************
  6. 2013 Qualcomm Atheros, Inc.
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * CPLNetworks.cpp - CPLNetworks class definition;
  11. *
  12. * powerline network enumerator;
  13. *
  14. * the collection of CPLNetworks on a single host interface;
  15. *
  16. * Contributor(s):
  17. * Charles Maier <charles.maier@intellon.com>
  18. *
  19. *--------------------------------------------------------------------*/
  20. #ifndef CPLNETWORKS_SOURCE
  21. #define CPLNETWORKS_SOURCE
  22. /*====================================================================*
  23. * system header files;
  24. *--------------------------------------------------------------------*/
  25. #include <iostream>
  26. #include <cstring>
  27. /*====================================================================*
  28. * system header files;
  29. *--------------------------------------------------------------------*/
  30. #if defined (__linux__)
  31. # include <sys/socket.h>
  32. # include <net/ethernet.h>
  33. # include <net/if.h>
  34. #elif defined (__APPLE__)
  35. # include <sys/socket.h>
  36. # include <net/ethernet.h>
  37. # include <net/if.h>
  38. #elif defined (__OpenBSD__)
  39. # include <sys/socket.h>
  40. # include <net/if.h>
  41. # include <netinet/in.h>
  42. # include <netinet/if_ether.h>
  43. #elif defined (WINPCAP)
  44. # include <pcap.h>
  45. #else
  46. #error "Unknown environment"
  47. #endif
  48. /*====================================================================*
  49. * custom header files;
  50. *--------------------------------------------------------------------*/
  51. #include "../classes/CPLNetworks.hpp"
  52. #include "../classes/CPLChannel.hpp"
  53. /*====================================================================*
  54. *
  55. * bool Empty (void) const;
  56. *
  57. * return true if there are no table networks;
  58. *
  59. *--------------------------------------------------------------------*/
  60. bool CPLNetworks::Empty (void) const
  61. {
  62. return (!this->mcount);
  63. }
  64. /*====================================================================*
  65. *
  66. * bool End (void) const;
  67. *
  68. * return true once the lasttable network has been reached;
  69. *
  70. *--------------------------------------------------------------------*/
  71. bool CPLNetworks::End (void) const
  72. {
  73. return (this->mindex >= this->mcount);
  74. }
  75. /*====================================================================*
  76. *
  77. * unsigned Count (void) const;
  78. *
  79. * return the number of table networks;
  80. *
  81. *--------------------------------------------------------------------*/
  82. unsigned CPLNetworks::Count (void) const
  83. {
  84. return (this->mcount);
  85. }
  86. /*====================================================================*
  87. *
  88. * unsigned Index (void) const;
  89. *
  90. * return the current table network number;
  91. *
  92. *--------------------------------------------------------------------*/
  93. unsigned CPLNetworks::Index (void) const
  94. {
  95. return (this->mindex);
  96. }
  97. /*====================================================================*
  98. *
  99. * CPLNetworks & SelectFirst (void);
  100. *
  101. * select first table network;
  102. *
  103. *--------------------------------------------------------------------*/
  104. CPLNetworks & CPLNetworks::SelectFirst (void)
  105. {
  106. this->mindex = 0;
  107. return (*this);
  108. }
  109. /*====================================================================*
  110. *
  111. * CPLNetworks & SelectFinal (void);
  112. *
  113. * select final table network;
  114. *
  115. *--------------------------------------------------------------------*/
  116. CPLNetworks & CPLNetworks::SelectFinal (void)
  117. {
  118. this->mindex = this->mcount - 1;
  119. return (*this);
  120. }
  121. /*====================================================================*
  122. *
  123. * CPLNetworks & SelectPrev (void)
  124. *
  125. * select the prev table network unless the current network is the
  126. * first network; same as operator --;
  127. *
  128. *--------------------------------------------------------------------*/
  129. CPLNetworks & CPLNetworks::SelectPrev (void)
  130. {
  131. if (this->mindex > 0)
  132. {
  133. this->mindex--;
  134. }
  135. return (*this);
  136. }
  137. /*====================================================================*
  138. *
  139. * CPLNetworks & SelectNext (void)
  140. *
  141. * select the next table network unless the current network is the
  142. * final network; same as operator ++;
  143. *
  144. *--------------------------------------------------------------------*/
  145. CPLNetworks & CPLNetworks::SelectNext (void)
  146. {
  147. if (this->mindex < this->mcount)
  148. {
  149. this->mindex++;
  150. }
  151. return (*this);
  152. }
  153. /*====================================================================*
  154. *
  155. * CPLNetworks & Select (unsigned index);
  156. *
  157. * select a table network by number; same as operator [];
  158. *
  159. *--------------------------------------------------------------------*/
  160. CPLNetworks & CPLNetworks::Select (unsigned index)
  161. {
  162. this->mindex = index;
  163. if (this->mindex > this->mcount)
  164. {
  165. this->mindex = this->mcount;
  166. }
  167. return (*this);
  168. }
  169. /*====================================================================*
  170. *
  171. * CPLNetwork & operator = (unsigned index)
  172. *
  173. * select a table network by number;
  174. *
  175. *--------------------------------------------------------------------*/
  176. CPLNetworks & CPLNetworks::operator = (unsigned index)
  177. {
  178. return (this->Select (index));
  179. }
  180. /*====================================================================*
  181. *
  182. * CPLNetwork & operator [] (unsigned index)
  183. *
  184. * select a table network then return a reference to it;
  185. *
  186. *--------------------------------------------------------------------*/
  187. CPLNetwork & CPLNetworks::operator [] (unsigned index)
  188. {
  189. return (this->Select (index).Selected ());
  190. }
  191. /*====================================================================*
  192. *
  193. * CPLNetwork & Selected (void) const
  194. *
  195. * return a reference to the selected table network;
  196. *
  197. *--------------------------------------------------------------------*/
  198. CPLNetwork & CPLNetworks::Selected (void) const
  199. {
  200. return (* this->mtable [this->mindex]);
  201. }
  202. /*====================================================================*
  203. *
  204. * CPLNetwork & Station (void) const
  205. *
  206. * return a reference to the selected table network;
  207. *
  208. *--------------------------------------------------------------------*/
  209. CPLNetwork & CPLNetworks::Network (void) const
  210. {
  211. return (* this->mtable [this->mindex]);
  212. }
  213. /*====================================================================*
  214. *
  215. * CPLNetworks & Enumerate (void);
  216. *
  217. * interate through networks and enumerate each on stdout but do
  218. * not change the selected network;
  219. *
  220. *--------------------------------------------------------------------*/
  221. CPLNetworks & CPLNetworks::Enumerate (void)
  222. {
  223. for (unsigned index = 0; index < this->mcount; index++)
  224. {
  225. this->mtable [index]->Enumerate ();
  226. }
  227. return (*this);
  228. }
  229. /*====================================================================*
  230. *
  231. * CPLNetworks (CPLChannel * channel, unsigned timeout);
  232. *
  233. *--------------------------------------------------------------------*/
  234. CPLNetworks::CPLNetworks (char const * ifname, unsigned timeout)
  235. {
  236. CPLChannel channel (ifname, timeout);
  237. byte bridge [CPLCHANNEL_BRIDGES_MAX][ETHER_ADDR_LEN];
  238. this->mtable = new CPLNetwork * [CPLCHANNEL_BRIDGES_MAX];
  239. this->mcount = channel.Bridges (bridge, sizeof (bridge));
  240. this->mindex = 0;
  241. while (this->mindex < this->mcount)
  242. {
  243. channel.ImportPeerAddress (bridge [this->mindex]);
  244. this->mtable [this->mindex] = new CPLNetwork (&channel);
  245. this->mindex++;
  246. }
  247. this->mindex = 0;
  248. return;
  249. }
  250. /*====================================================================*
  251. *
  252. * ~CPLNetworks ()
  253. *
  254. * delete table after deleting networks;
  255. *
  256. *--------------------------------------------------------------------*/
  257. CPLNetworks::~CPLNetworks ()
  258. {
  259. delete [] this->mtable;
  260. return;
  261. }
  262. /*====================================================================*
  263. * end definition;
  264. *--------------------------------------------------------------------*/
  265. #endif