int6kstat.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*
  42. *
  43. * int6kstat.c - Atheros INT6x00 Link Statistics
  44. *
  45. *
  46. * Contributor(s):
  47. * Charles Maier
  48. *
  49. *--------------------------------------------------------------------*/
  50. /*====================================================================*"
  51. * system header files;
  52. *--------------------------------------------------------------------*/
  53. #include <unistd.h>
  54. #include <stdlib.h>
  55. #include <stdint.h>
  56. #include <limits.h>
  57. /*====================================================================*
  58. * custom header files;
  59. *--------------------------------------------------------------------*/
  60. #include "../tools/getoptv.h"
  61. #include "../tools/putoptv.h"
  62. #include "../tools/memory.h"
  63. #include "../tools/number.h"
  64. #include "../tools/symbol.h"
  65. #include "../tools/types.h"
  66. #include "../tools/flags.h"
  67. #include "../tools/files.h"
  68. #include "../tools/error.h"
  69. #include "../plc/plc.h"
  70. /*====================================================================*
  71. * custom source files;
  72. *--------------------------------------------------------------------*/
  73. #ifndef MAKEFILE
  74. #include "../plc/Display.c"
  75. #include "../plc/Failure.c"
  76. #include "../plc/Request.c"
  77. #include "../plc/ReadMME.c"
  78. #include "../plc/SendMME.c"
  79. #include "../plc/LinkStatistics.c"
  80. #include "../plc/LocalDevices.c"
  81. #include "../plc/NetworkInformation1.c"
  82. #include "../plc/Topology1.c"
  83. #include "../plc/Devices.c"
  84. #include "../plc/chipset.c"
  85. #include "../plc/Platform.c"
  86. #endif
  87. #ifndef MAKEFILE
  88. #include "../tools/error.c"
  89. #include "../tools/getoptv.c"
  90. #include "../tools/putoptv.c"
  91. #include "../tools/version.c"
  92. #include "../tools/uintspec.c"
  93. #include "../tools/hexdump.c"
  94. #include "../tools/hexencode.c"
  95. #include "../tools/hexdecode.c"
  96. #include "../tools/hexstring.c"
  97. #include "../tools/todigit.c"
  98. #include "../tools/synonym.c"
  99. #include "../tools/typename.c"
  100. #endif
  101. #ifndef MAKEFILE
  102. #include "../ether/openchannel.c"
  103. #include "../ether/closechannel.c"
  104. #include "../ether/readpacket.c"
  105. #include "../ether/sendpacket.c"
  106. #include "../ether/channel.c"
  107. #endif
  108. #ifndef MAKEFILE
  109. #include "../mme/EthernetHeader.c"
  110. #include "../mme/QualcommHeader1.c"
  111. #include "../mme/QualcommHeader.c"
  112. #include "../mme/UnwantedMessage.c"
  113. #include "../mme/MMECode.c"
  114. #endif
  115. /*====================================================================*
  116. * program constants;
  117. *--------------------------------------------------------------------*/
  118. #define INT6KSTAT_LOOP 1
  119. #define INT6KSTAT_WAIT 0
  120. /*====================================================================*
  121. * program constants;
  122. *--------------------------------------------------------------------*/
  123. static const struct _term_ linkids [] =
  124. {
  125. {
  126. "CSMA-ALL",
  127. "0xFC"
  128. },
  129. {
  130. "CSMA-CAP0",
  131. "0x00"
  132. },
  133. {
  134. "CSMA-CAP1",
  135. "0x01"
  136. },
  137. {
  138. "CSMA-CAP2",
  139. "0x02"
  140. },
  141. {
  142. "CSMA-CAP3",
  143. "0x03"
  144. },
  145. {
  146. "CSMA-PEER",
  147. "0xF8"
  148. },
  149. };
  150. static const struct _term_ directions [] =
  151. {
  152. {
  153. "both",
  154. "2"
  155. },
  156. {
  157. "rx",
  158. "1"
  159. },
  160. {
  161. "tx",
  162. "0"
  163. }
  164. };
  165. /*====================================================================*
  166. * program constants;
  167. *--------------------------------------------------------------------*/
  168. #define LINKIDS (sizeof (linkids) / sizeof (struct _term_))
  169. #define DIRECTIONS (sizeof (directions) / sizeof (struct _term_))
  170. /*====================================================================*
  171. *
  172. * void manager (struct plc * plc);
  173. *
  174. * perform operations in logical order despite any order specfied
  175. * on the command line; for example read PIB before writing PIB;
  176. *
  177. * operation order is controlled by the order of "if" statements
  178. * shown here; the entire operation sequence can be repeated with
  179. * an optional pause between each iteration;
  180. *
  181. *
  182. *--------------------------------------------------------------------*/
  183. void manager (struct plc * plc, signed count, signed pause)
  184. {
  185. while (count--)
  186. {
  187. if (_anyset (plc->flags, PLC_ANALYSE))
  188. {
  189. Topology1 (plc);
  190. }
  191. if (_anyset (plc->flags, PLC_NETWORK))
  192. {
  193. NetworkInformation1 (plc);
  194. }
  195. if (_anyset (plc->flags, PLC_LINK_STATS))
  196. {
  197. LinkStatistics (plc);
  198. }
  199. sleep (pause);
  200. }
  201. return;
  202. }
  203. /*====================================================================*
  204. *
  205. * int main (int argc, char const * argv[]);
  206. *
  207. * parse command line, populate plc structure and perform selected
  208. * operations; show help summary if asked; see getoptv and putoptv
  209. * to understand command line parsing and help summary display; see
  210. * plc.h for the definition of struct plc;
  211. *
  212. * the command line accepts multiple MAC addresses and the program
  213. * performs the specified operations on each address, in turn; the
  214. * address order is significant but the option order is not; the
  215. * default address is a local broadcast that causes all devices on
  216. * the local H1 interface to respond but not those at the remote
  217. * end of the powerline;
  218. *
  219. * the default address is 00:B0:52:00:00:01; omitting the address
  220. * will automatically address the local device; some options will
  221. * cancel themselves if this makes no sense;
  222. *
  223. * the default interface is eth1 because most people use eth0 as
  224. * their principle network connection; you can specify another
  225. * interface with -i or define environment string PLC to make
  226. * that the default interface and save typing;
  227. *
  228. *
  229. *--------------------------------------------------------------------*/
  230. int main (int argc, char const * argv [])
  231. {
  232. extern struct channel channel;
  233. static char const * optv [] =
  234. {
  235. "Cd:ei:l:mp:qs:tvw:",
  236. "device [device] [...] [> stdout]",
  237. "Qualcomm Atheros INT6x00 Powerline Link Statistics",
  238. #if defined (WINPCAP) || defined (LIBPCAP)
  239. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  240. #else
  241. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  242. #endif
  243. "C\tclear statistics without reading using VS_LNK_STATS",
  244. "d n\tdirection (0=tx, 1=rx, 2=both) for VS_LNK_STATS",
  245. "e\tredirect stderr to stdout",
  246. "l n\tloop n times [" LITERAL (INT6KSTAT_LOOP) "]",
  247. "s n\tLink ID for VS_LNK_STATS",
  248. "m\tprint network membership information using VS_NW_INFO",
  249. "p x\tpeer node address for options -s",
  250. "q\tquiet mode",
  251. "t\tprint network topology using VS_NW_INFO with VS_SW_VER",
  252. "v\tverbose mode",
  253. "w n\twait n seconds [" LITERAL (INT6KSTAT_WAIT) "]",
  254. (char const *) (0)
  255. };
  256. #include "../plc/plc.c"
  257. signed loop = INT6KSTAT_LOOP;
  258. signed wait = INT6KSTAT_WAIT;
  259. signed c;
  260. if (getenv (PLCDEVICE))
  261. {
  262. #if defined (WINPCAP) || defined (LIBPCAP)
  263. channel.ifindex = atoi (getenv (PLCDEVICE));
  264. #else
  265. channel.ifname = strdup (getenv (PLCDEVICE));
  266. #endif
  267. }
  268. optind = 1;
  269. plc.pushbutton = 0;
  270. while ((c = getoptv (argc, argv, optv)) != -1)
  271. {
  272. switch (c)
  273. {
  274. case 'C':
  275. _setbits (plc.flags, PLC_LINK_STATS);
  276. plc.pushbutton = 1;
  277. break;
  278. case 'd':
  279. _setbits (plc.flags, PLC_LINK_STATS);
  280. plc.module = (uint8_t)(uintspec (synonym (optarg, directions, DIRECTIONS), 0, UCHAR_MAX));
  281. break;
  282. case 'e':
  283. dup2 (STDOUT_FILENO, STDERR_FILENO);
  284. break;
  285. case 'i':
  286. #if defined (WINPCAP) || defined (LIBPCAP)
  287. channel.ifindex = atoi (optarg);
  288. #else
  289. channel.ifname = optarg;
  290. #endif
  291. break;
  292. case 'm':
  293. _setbits (plc.flags, PLC_NETWORK);
  294. break;
  295. case 'p':
  296. _setbits (plc.flags, PLC_LINK_STATS);
  297. if (!hexencode (plc.RDA, sizeof (plc.RDA), (char const *)(optarg)))
  298. {
  299. error (1, errno, PLC_BAD_MAC, optarg);
  300. }
  301. break;
  302. case 'l':
  303. loop = (unsigned)(uintspec (optarg, 0, UINT_MAX));
  304. break;
  305. case 'q':
  306. _setbits (channel.flags, CHANNEL_SILENCE);
  307. _setbits (plc.flags, PLC_SILENCE);
  308. break;
  309. case 's':
  310. _setbits (plc.flags, PLC_LINK_STATS);
  311. plc.action = (uint8_t)(uintspec (synonym (optarg, linkids, LINKIDS), 0, UCHAR_MAX));
  312. break;
  313. case 't':
  314. _setbits (plc.flags, PLC_ANALYSE);
  315. break;
  316. case 'v':
  317. _setbits (channel.flags, CHANNEL_VERBOSE);
  318. _setbits (plc.flags, PLC_VERBOSE);
  319. break;
  320. case 'w':
  321. wait = (unsigned)(uintspec (optarg, 0, 3600));
  322. break;
  323. default:
  324. break;
  325. }
  326. }
  327. argc -= optind;
  328. argv += optind;
  329. openchannel (&channel);
  330. if (!(plc.message = malloc (sizeof (* plc.message))))
  331. {
  332. error (1, errno, PLC_NOMEMORY);
  333. }
  334. if (!argc)
  335. {
  336. manager (&plc, loop, wait);
  337. }
  338. while ((argc) && (* argv))
  339. {
  340. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  341. {
  342. error (1, errno, PLC_BAD_MAC, * argv);
  343. }
  344. manager (&plc, loop, wait);
  345. argc--;
  346. argv++;
  347. }
  348. free (plc.message);
  349. closechannel (&channel);
  350. return (0);
  351. }