hpav.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * hpav.c - Qualcomm Atheros HomePlug AV Packet Monitor;
  11. *
  12. * print message header and/or full message content on stdout for
  13. * each HomePlugAV or Atheros Vendor Specific message received by
  14. * the host;
  15. *
  16. *
  17. * Contributor(s):
  18. * Charles Maier <cmaier@qca.qualcomm.com>
  19. *
  20. *--------------------------------------------------------------------*/
  21. /*====================================================================*
  22. * system header files;
  23. *--------------------------------------------------------------------*/
  24. #include <fcntl.h>
  25. #include <stdio.h>
  26. #include <limits.h>
  27. #include <ctype.h>
  28. #include <unistd.h>
  29. #include <stdint.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <errno.h>
  33. #if defined (__linux__)
  34. #include <net/if.h>
  35. #include <net/if_arp.h>
  36. #include <netpacket/packet.h>
  37. #include <signal.h>
  38. #endif
  39. /*====================================================================*
  40. * custom header files;
  41. *--------------------------------------------------------------------*/
  42. #include "../tools/getoptv.h"
  43. #include "../tools/putoptv.h"
  44. #include "../tools/memory.h"
  45. #include "../tools/number.h"
  46. #include "../tools/types.h"
  47. #include "../tools/flags.h"
  48. #include "../tools/error.h"
  49. #include "../ether/channel.h"
  50. #include "../plc/plc.h"
  51. #include "../mme/mme.h"
  52. /*====================================================================*
  53. * custom source files;
  54. *--------------------------------------------------------------------*/
  55. #ifndef MAKEFILE
  56. #include "../tools/getoptv.c"
  57. #include "../tools/putoptv.c"
  58. #include "../tools/version.c"
  59. #include "../tools/hexdump.c"
  60. #include "../tools/hexdecode.c"
  61. #include "../tools/hexstring.c"
  62. #include "../tools/hexstream.c"
  63. #include "../tools/uintspec.c"
  64. #include "../tools/todigit.c"
  65. #include "../tools/error.c"
  66. #endif
  67. #ifndef MAKEFILE
  68. #include "../ether/channel.c"
  69. #include "../ether/openchannel.c"
  70. #include "../ether/closechannel.c"
  71. #include "../ether/sendpacket.c"
  72. #include "../ether/readpacket.c"
  73. #endif
  74. #ifndef MAKEFILE
  75. #include "../mme/MMEPeek.c"
  76. #include "../mme/MMEShow.c"
  77. #include "../mme/MMEName.c"
  78. #include "../mme/MMEMode.c"
  79. #endif
  80. /*====================================================================*
  81. * program constants;
  82. *--------------------------------------------------------------------*/
  83. #define HPAV_VERBOSE (1 << 0)
  84. #define HPAV_SILENCE (1 << 1)
  85. #define HPAV_CONCISE (1 << 2)
  86. #define PLCDEVICE "PLC"
  87. /*====================================================================*
  88. *
  89. * int main (int argc, char * argv[]);
  90. *
  91. *
  92. *--------------------------------------------------------------------*/
  93. int main (int argc, char const * argv [])
  94. {
  95. extern struct channel channel;
  96. struct message message;
  97. static char const * optv [] =
  98. {
  99. "di:qt:v",
  100. PUTOPTV_S_DIVINE,
  101. "Qualcomm Atheros HomePlug AV Packet Monitor",
  102. "d\tprint compact information",
  103. #if defined (WINPCAP)
  104. "i s\thost interface is (s) [" CHANNEL_ETHDEVICE "]",
  105. #else
  106. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  107. #endif
  108. "q\tsuppress normal output",
  109. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  110. "v\tverbose messages on stdout",
  111. (char const *) (0)
  112. };
  113. flag_t flags = (flag_t) (0);
  114. void (* function) (void const * memory, size_t extent, FILE *) = MMEPeek;
  115. signed length;
  116. signed c;
  117. if (getenv (PLCDEVICE))
  118. {
  119. #if defined (WINPCAP)
  120. channel.ifindex = atoi (getenv (PLCDEVICE));
  121. #else
  122. channel.ifname = strdup (getenv (PLCDEVICE));
  123. #endif
  124. }
  125. optind = 1;
  126. while (~ (c = getoptv (argc, argv, optv)))
  127. {
  128. switch (c)
  129. {
  130. case 'd':
  131. function = MMEShow;
  132. break;
  133. case 'i':
  134. #if defined (WIN32)
  135. channel.ifindex = atoi (optarg);
  136. #else
  137. channel.ifname = optarg;
  138. #endif
  139. break;
  140. case 'q':
  141. _setbits (flags, HPAV_SILENCE);
  142. break;
  143. case 't':
  144. channel.timeout = (unsigned) (uintspec (optarg, 0, UINT_MAX));
  145. break;
  146. case 'v':
  147. _setbits (flags, HPAV_VERBOSE);
  148. break;
  149. default:
  150. break;
  151. }
  152. }
  153. argc -= optind;
  154. argv += optind;
  155. openchannel (& channel);
  156. while ((length = readpacket (& channel, & message, sizeof (message))) >= 0)
  157. {
  158. if (! length)
  159. {
  160. continue;
  161. }
  162. if (_allclr (flags, HPAV_SILENCE))
  163. {
  164. function (& message, length, stdout);
  165. }
  166. if (_anyset (flags, HPAV_VERBOSE))
  167. {
  168. hexdump (& message, 0, length, stdout);
  169. }
  170. }
  171. closechannel (& channel);
  172. return (0);
  173. }