mdustats.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. * mdustats.c -
  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/Request.c"
  76. #include "../plc/ReadMME.c"
  77. #include "../plc/SendMME.c"
  78. #include "../plc/MDUTrafficStats.c"
  79. #include "../plc/Devices.c"
  80. #include "../plc/chipset.c"
  81. #include "../mme/UnwantedMessage.c"
  82. #endif
  83. #ifndef MAKEFILE
  84. #include "../tools/error.c"
  85. #include "../tools/getoptv.c"
  86. #include "../tools/putoptv.c"
  87. #include "../tools/version.c"
  88. #include "../tools/uintspec.c"
  89. #include "../tools/hexdump.c"
  90. #include "../tools/hexencode.c"
  91. #include "../tools/hexdecode.c"
  92. #include "../tools/hexstring.c"
  93. #include "../tools/todigit.c"
  94. #include "../tools/set32bitmap.c"
  95. #include "../tools/synonym.c"
  96. #include "../tools/typename.c"
  97. #endif
  98. #ifndef MAKEFILE
  99. #include "../ether/openchannel.c"
  100. #include "../ether/closechannel.c"
  101. #include "../ether/readpacket.c"
  102. #include "../ether/sendpacket.c"
  103. #include "../ether/channel.c"
  104. #endif
  105. #ifndef MAKEFILE
  106. #include "../mme/MMECode.c"
  107. #include "../mme/EthernetHeader.c"
  108. #include "../mme/QualcommHeader.c"
  109. #endif
  110. /*====================================================================*
  111. * program constants;
  112. *--------------------------------------------------------------------*/
  113. #define MDUSTATS_COMMAND 4
  114. #define MDUSTATS_SESSION 255
  115. #define MDUSTATS_SLAVE 0
  116. /*====================================================================*
  117. *
  118. * int main (int argc, char const * argv[]);
  119. *
  120. * parse command line, populate plc structure and perform selected
  121. * operations; show help summary if asked; see getoptv and putoptv
  122. * to understand command line parsing and help summary display; see
  123. * plc.h for the definition of struct plc;
  124. *
  125. * the command line accepts multiple MAC addresses and the program
  126. * performs the specified operations on each address, in turn; the
  127. * address order is significant but the option order is not; the
  128. * default address is a local broadcast that causes all devices on
  129. * the local H1 interface to respond but not those at the remote
  130. * end of the powerline;
  131. *
  132. * the default address is 00:B0:52:00:00:01; omitting the address
  133. * will automatically address the local device; some options will
  134. * cancel themselves if this makes no sense;
  135. *
  136. * the default interface is eth1 because most people use eth0 as
  137. * their principle network connection; you can specify another
  138. * interface with -i or define environment string PLC to make
  139. * that the default interface and save typing;
  140. *
  141. *
  142. *--------------------------------------------------------------------*/
  143. int main (int argc, char const * argv [])
  144. {
  145. extern struct channel channel;
  146. static char const * optv [] =
  147. {
  148. "c:ei:qs:t:u:v",
  149. "device [device] [...] [> stdout]",
  150. "Qualcomm Atheros INT6x00 Powerline Link Statistics",
  151. #if defined (WINPCAP) || defined (LIBPCAP)
  152. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  153. #else
  154. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  155. #endif
  156. "c n\tcommand [" LITERAL (MDUSTATS_COMMAND) "]",
  157. "e\tredirect stderr to stdout",
  158. "q\tquiet mode",
  159. "s n\tsession identifier [" LITERAL (MDUSTATS_SESSION) "]",
  160. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  161. "u n\tslave unit number [" LITERAL (MDUSTATS_SLAVE) "]",
  162. "v\tverbose mode",
  163. (char const *) (0)
  164. };
  165. #include "../plc/plc.c"
  166. uint8_t command = MDUSTATS_COMMAND;
  167. uint8_t session = MDUSTATS_SESSION;
  168. uint8_t slave = MDUSTATS_SLAVE;
  169. signed c;
  170. if (getenv (PLCDEVICE))
  171. {
  172. #if defined (WINPCAP) || defined (LIBPCAP)
  173. channel.ifindex = atoi (getenv (PLCDEVICE));
  174. #else
  175. channel.ifname = strdup (getenv (PLCDEVICE));
  176. #endif
  177. }
  178. optind = 1;
  179. while ((c = getoptv (argc, argv, optv)) != -1)
  180. {
  181. switch (c)
  182. {
  183. case 'c':
  184. command = (uint8_t)(uintspec (optarg, 0, 255));
  185. break;
  186. case 'e':
  187. dup2 (STDOUT_FILENO, STDERR_FILENO);
  188. break;
  189. case 'i':
  190. #if defined (WINPCAP) || defined (LIBPCAP)
  191. channel.ifindex = atoi (optarg);
  192. #else
  193. channel.ifname = optarg;
  194. #endif
  195. break;
  196. case 'q':
  197. _setbits (channel.flags, CHANNEL_SILENCE);
  198. _setbits (plc.flags, PLC_SILENCE);
  199. break;
  200. case 's':
  201. session = (uint8_t)(uintspec (optarg, 0, 255));
  202. break;
  203. case 't':
  204. channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
  205. break;
  206. case 'u':
  207. slave = (uint8_t)(uintspec (optarg, 0, 255));
  208. break;
  209. case 'v':
  210. _setbits (channel.flags, CHANNEL_VERBOSE);
  211. _setbits (plc.flags, PLC_VERBOSE);
  212. break;
  213. default:
  214. break;
  215. }
  216. }
  217. argc -= optind;
  218. argv += optind;
  219. openchannel (&channel);
  220. if (!(plc.message = malloc (sizeof (* plc.message))))
  221. {
  222. error (1, errno, PLC_NOMEMORY);
  223. }
  224. if (!argc)
  225. {
  226. MDUTrafficStats (&plc, command, session, slave);
  227. }
  228. while ((argc) && (* argv))
  229. {
  230. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  231. {
  232. error (1, errno, PLC_BAD_MAC, * argv);
  233. }
  234. MDUTrafficStats (&plc, command, session, slave);
  235. argc--;
  236. argv++;
  237. }
  238. free (plc.message);
  239. closechannel (&channel);
  240. return (0);
  241. }