plcsnif.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * plcsnif.c -
  11. *
  12. * Contributor(s):
  13. * Charles Maier <cmaier@qca.qualcomm.com>
  14. *
  15. *--------------------------------------------------------------------*/
  16. /*====================================================================*
  17. * system header files;
  18. *--------------------------------------------------------------------*/
  19. #include <unistd.h>
  20. #include <stdlib.h>
  21. #include <stdint.h>
  22. #include <limits.h>
  23. /*====================================================================*
  24. * custom header files;
  25. *--------------------------------------------------------------------*/
  26. #include "../tools/getoptv.h"
  27. #include "../tools/putoptv.h"
  28. #include "../tools/memory.h"
  29. #include "../tools/number.h"
  30. #include "../tools/symbol.h"
  31. #include "../tools/types.h"
  32. #include "../tools/flags.h"
  33. #include "../tools/files.h"
  34. #include "../tools/error.h"
  35. #include "../plc/plc.h"
  36. #include "../nda/nda.h"
  37. /*====================================================================*
  38. * custom source files;
  39. *--------------------------------------------------------------------*/
  40. #ifndef MAKEFILE
  41. #include "../plc/chipset.c"
  42. #include "../plc/Devices.c"
  43. #include "../plc/Confirm.c"
  44. #include "../plc/Display.c"
  45. #include "../plc/Failure.c"
  46. #include "../plc/Request.c"
  47. #include "../plc/ReadMME.c"
  48. #include "../plc/SendMME.c"
  49. #endif
  50. #ifndef MAKEFILE
  51. #include "../tools/getoptv.c"
  52. #include "../tools/putoptv.c"
  53. #include "../tools/version.c"
  54. #include "../tools/uintspec.c"
  55. #include "../tools/hexdump.c"
  56. #include "../tools/hexdecode.c"
  57. #include "../tools/hexencode.c"
  58. #include "../tools/hexstring.c"
  59. #include "../tools/hexout.c"
  60. #include "../tools/todigit.c"
  61. #include "../tools/synonym.c"
  62. #include "../tools/error.c"
  63. #include "../tools/typename.c"
  64. #endif
  65. #ifndef MAKEFILE
  66. #include "../ether/openchannel.c"
  67. #include "../ether/closechannel.c"
  68. #include "../ether/readpacket.c"
  69. #include "../ether/sendpacket.c"
  70. #include "../ether/channel.c"
  71. #endif
  72. #ifndef MAKEFILE
  73. #include "../mme/MMECode.c"
  74. #include "../mme/EthernetHeader.c"
  75. #include "../mme/QualcommHeader.c"
  76. #include "../mme/UnwantedMessage.c"
  77. #endif
  78. #ifndef MAKEFILE
  79. #include "../nda/Sniffer.c"
  80. #include "../nda/Monitor.c"
  81. #endif
  82. /*====================================================================*
  83. * program constants;
  84. *--------------------------------------------------------------------*/
  85. #define DEFAULT_MODE 0
  86. /*====================================================================*
  87. *
  88. * int main (int argc, char const * argv[]);
  89. *
  90. *--------------------------------------------------------------------*/
  91. int main (int argc, char const * argv [])
  92. {
  93. extern struct channel channel;
  94. extern const struct _term_ devices [];
  95. static char const * optv [] =
  96. {
  97. "dbei:m:qt:v",
  98. "device [device] [...]",
  99. "Qualcomm Atheros Powerline Traffic Sniffer",
  100. "d\tdump sniffer data in hex format on stdout",
  101. "e\tredirect stderr messages to stdout",
  102. #if defined (WINPCAP) || defined (LIBPCAP)
  103. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  104. #else
  105. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  106. #endif
  107. "m n\tmode is (n) [" LITERAL (DEFAULT_MODE) "]",
  108. "q\tquiet mode",
  109. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  110. "v\tverbose mode",
  111. (char const *) (0)
  112. };
  113. #include "../plc/plc.c"
  114. signed colon = '\0';
  115. signed space = ' ';
  116. signed c;
  117. plc.action = DEFAULT_MODE;
  118. optind = 1;
  119. if (getenv (PLCDEVICE))
  120. {
  121. #if defined (WINPCAP) || defined (LIBPCAP)
  122. channel.ifindex = atoi (getenv (PLCDEVICE));
  123. #else
  124. channel.ifname = strdup (getenv (PLCDEVICE));
  125. #endif
  126. }
  127. while (~ (c = getoptv (argc, argv, optv)))
  128. {
  129. switch (c)
  130. {
  131. case 'd':
  132. _setbits (plc.flags, PLC_MONITOR);
  133. break;
  134. case 'e':
  135. dup2 (STDOUT_FILENO, STDERR_FILENO);
  136. break;
  137. case 'i':
  138. #if defined (WINPCAP) || defined (LIBPCAP)
  139. channel.ifindex = atoi (optarg);
  140. #else
  141. channel.ifname = optarg;
  142. #endif
  143. break;
  144. case 'm':
  145. plc.action = (uint8_t) (uintspec (optarg, 0, UCHAR_MAX));
  146. break;
  147. case 'q':
  148. _setbits (plc.flags, PLC_SILENCE);
  149. break;
  150. case 't':
  151. channel.timeout = (signed) (uintspec (optarg, 0, UINT_MAX));
  152. break;
  153. case 'v':
  154. _setbits (channel.flags, CHANNEL_VERBOSE);
  155. _setbits (plc.flags, PLC_VERBOSE);
  156. break;
  157. default:
  158. break;
  159. }
  160. }
  161. argc -= optind;
  162. argv += optind;
  163. openchannel (& channel);
  164. if (! (plc.message = malloc (sizeof (* plc.message))))
  165. {
  166. error (1, errno, PLC_NOMEMORY);
  167. }
  168. if (! argc)
  169. {
  170. if (_anyset (plc.flags, PLC_MONITOR))
  171. {
  172. Monitor (& plc, colon, space);
  173. }
  174. else
  175. {
  176. Sniffer (& plc);
  177. }
  178. }
  179. while ((argc) && (* argv))
  180. {
  181. if (! hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  182. {
  183. error (1, errno, PLC_BAD_MAC, * argv);
  184. }
  185. Sniffer (& plc);
  186. argv++;
  187. argc--;
  188. }
  189. free (plc.message);
  190. closechannel (& channel);
  191. exit (0);
  192. }