hpav.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. * hpav.c - Atheros HomePlug AV Packet Monitor;
  44. *
  45. * print message header and/or full message content on stdout for
  46. * each HomePlugAV or Atheros Vendor Specific message received by
  47. * the host;
  48. *
  49. *
  50. * Contributor(s):
  51. * Charles Maier
  52. *
  53. *--------------------------------------------------------------------*/
  54. /*====================================================================*
  55. * system header files;
  56. *--------------------------------------------------------------------*/
  57. #include <fcntl.h>
  58. #include <stdio.h>
  59. #include <limits.h>
  60. #include <ctype.h>
  61. #include <unistd.h>
  62. #include <stdint.h>
  63. #include <stdlib.h>
  64. #include <string.h>
  65. #include <errno.h>
  66. #if defined (__linux__)
  67. #include <net/if.h>
  68. #include <net/if_arp.h>
  69. #include <netpacket/packet.h>
  70. #include <signal.h>
  71. #endif
  72. /*====================================================================*
  73. * custom header files;
  74. *--------------------------------------------------------------------*/
  75. #include "../tools/getoptv.h"
  76. #include "../tools/putoptv.h"
  77. #include "../tools/memory.h"
  78. #include "../tools/number.h"
  79. #include "../tools/types.h"
  80. #include "../tools/flags.h"
  81. #include "../tools/error.h"
  82. #include "../ether/channel.h"
  83. #include "../plc/plc.h"
  84. #include "../mme/mme.h"
  85. /*====================================================================*
  86. * custom source files;
  87. *--------------------------------------------------------------------*/
  88. #ifndef MAKEFILE
  89. #include "../tools/getoptv.c"
  90. #include "../tools/putoptv.c"
  91. #include "../tools/version.c"
  92. #include "../tools/hexdump.c"
  93. #include "../tools/hexdecode.c"
  94. #include "../tools/hexstring.c"
  95. #include "../tools/uintspec.c"
  96. #include "../tools/todigit.c"
  97. #include "../tools/error.c"
  98. #endif
  99. #ifndef MAKEFILE
  100. #include "../ether/channel.c"
  101. #include "../ether/openchannel.c"
  102. #include "../ether/closechannel.c"
  103. #include "../ether/sendpacket.c"
  104. #include "../ether/readpacket.c"
  105. #endif
  106. #ifndef MAKEFILE
  107. #include "../mme/MMEPeek.c"
  108. #include "../mme/MMEName.c"
  109. #include "../mme/MMEMode.c"
  110. #endif
  111. /*====================================================================*
  112. * program constants;
  113. *--------------------------------------------------------------------*/
  114. #define HPAV_VERBOSE (1 << 0)
  115. #define HPAV_SILENCE (1 << 1)
  116. #define PLCDEVICE "PLC"
  117. /*====================================================================*
  118. *
  119. * int main (int argc, char * argv[]);
  120. *
  121. *
  122. *--------------------------------------------------------------------*/
  123. int main (int argc, char const * argv [])
  124. {
  125. extern struct channel channel;
  126. struct message message;
  127. static char const * optv [] =
  128. {
  129. "i:qt:v",
  130. PUTOPTV_S_DIVINE,
  131. "Qualcomm Atheros HomePlug AV Packet Monitor",
  132. #if defined (WINPCAP)
  133. "i s\thost interface is (s) [" CHANNEL_ETHDEVICE "]",
  134. #else
  135. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  136. #endif
  137. "q\tsuppress normal output",
  138. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  139. "v\tverbose messages on stdout",
  140. (char const *) (0)
  141. };
  142. flag_t flags = (flag_t)(0);
  143. signed length;
  144. signed c;
  145. if (getenv (PLCDEVICE))
  146. {
  147. #if defined (WINPCAP)
  148. channel.ifindex = atoi (getenv (PLCDEVICE));
  149. #else
  150. channel.ifname = strdup (getenv (PLCDEVICE));
  151. #endif
  152. }
  153. optind = 1;
  154. while ((c = getoptv (argc, argv, optv)) != -1)
  155. {
  156. switch (c)
  157. {
  158. case 'i':
  159. #if defined (WIN32)
  160. channel.ifindex = atoi (optarg);
  161. #else
  162. channel.ifname = optarg;
  163. #endif
  164. break;
  165. case 'q':
  166. _setbits (flags, HPAV_SILENCE);
  167. break;
  168. case 't':
  169. channel.timeout = (unsigned)(uintspec (optarg, 0, UINT_MAX));
  170. break;
  171. case 'v':
  172. _setbits (flags, HPAV_VERBOSE);
  173. break;
  174. default:
  175. break;
  176. }
  177. }
  178. argc -= optind;
  179. argv += optind;
  180. openchannel (&channel);
  181. while ((length = readpacket (&channel, &message, sizeof (message))) >= 0)
  182. {
  183. if (length > 0)
  184. {
  185. if (_allclr (flags, HPAV_SILENCE))
  186. {
  187. MMEPeek (&message, length, stdout);
  188. }
  189. if (_anyset (flags, HPAV_VERBOSE))
  190. {
  191. hexdump (&message, 0, length, stdout);
  192. }
  193. }
  194. }
  195. closechannel (&channel);
  196. return (0);
  197. }