int6kprobe.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * int6kprobe.c - Qualcomm Atheros Network Probe Utility
  11. *
  12. * this program sends and receives raw ethernet frames and so needs
  13. * root privileges; if you install it using "chmod 555" and "chown
  14. * root:root" then you must login as root to run it; otherwise, you
  15. * can install it using "chmod 4555" and "chown root:root" so that
  16. * anyone can run it; the program will refuse to run until you get
  17. * things right;
  18. *
  19. * Contributor(s):
  20. * Charles Maier <cmaier@qca.qualcomm.com>
  21. *
  22. *--------------------------------------------------------------------*/
  23. /*====================================================================*
  24. * system header files;
  25. *--------------------------------------------------------------------*/
  26. #include <unistd.h>
  27. #include <stdint.h>
  28. #include <stdlib.h>
  29. /*====================================================================*
  30. * custom header files;
  31. *--------------------------------------------------------------------*/
  32. #include "../tools/getoptv.h"
  33. #include "../tools/putoptv.h"
  34. #include "../tools/memory.h"
  35. #include "../tools/number.h"
  36. #include "../tools/symbol.h"
  37. #include "../tools/types.h"
  38. #include "../tools/flags.h"
  39. #include "../tools/files.h"
  40. #include "../tools/error.h"
  41. #include "../plc/plc.h"
  42. #include "../nda/nda.h"
  43. /*====================================================================*
  44. * custom source files;
  45. *--------------------------------------------------------------------*/
  46. #ifndef MAKEFILE
  47. #include "../plc/Confirm.c"
  48. #include "../plc/Display.c"
  49. #include "../plc/Failure.c"
  50. #include "../plc/Request.c"
  51. #include "../plc/ReadMME.c"
  52. #include "../plc/SendMME.c"
  53. #include "../mme/UnwantedMessage.c"
  54. #include "../plc/Devices.c"
  55. #endif
  56. #ifndef MAKEFILE
  57. #include "../tools/error.c"
  58. #include "../tools/getoptv.c"
  59. #include "../tools/putoptv.c"
  60. #include "../tools/version.c"
  61. #include "../tools/uintspec.c"
  62. #include "../tools/hexdump.c"
  63. #include "../tools/hexencode.c"
  64. #include "../tools/hexdecode.c"
  65. #include "../tools/hexstring.c"
  66. #include "../tools/todigit.c"
  67. #include "../tools/synonym.c"
  68. #endif
  69. #ifndef MAKEFILE
  70. #include "../ether/openchannel.c"
  71. #include "../ether/closechannel.c"
  72. #include "../ether/readpacket.c"
  73. #include "../ether/sendpacket.c"
  74. #include "../ether/channel.c"
  75. #endif
  76. #ifndef MAKEFILE
  77. #include "../mme/MMECode.c"
  78. #include "../mme/EthernetHeader.c"
  79. #include "../mme/QualcommHeader.c"
  80. #endif
  81. #ifndef MAKEFILE
  82. #include "../nda/NetworkProbe.c"
  83. #endif
  84. /*====================================================================*
  85. *
  86. * int main (int argc, char const * argv[]);
  87. *
  88. * parse command line, populate plc structure and perform selected
  89. * operations; show help summary if asked; see getoptv and putoptv
  90. * to understand command line parsing and help summary display; see
  91. * plc.h for the definition of struct plc;
  92. *
  93. * the command line accepts multiple MAC addresses and the program
  94. * performs the specified operations on each address, in turn; the
  95. * address order is significant but the option order is not; the
  96. * default address is a local broadcast that causes all devices on
  97. * the local H1 interface to respond but not those at the remote
  98. * end of the powerline;
  99. *
  100. * the default address is 00:B0:52:00:00:01; omitting the address
  101. * will automatically address the local device; some options will
  102. * cancel themselves if this makes no sense;
  103. *
  104. * the default interface is eth1 because most people use eth0 as
  105. * their principle network connection; you can specify another
  106. * interface with -i or define environment string PLC to make
  107. * that the default interface and save typing;
  108. *
  109. *--------------------------------------------------------------------*/
  110. int main (int argc, char const * argv [])
  111. {
  112. extern struct channel channel;
  113. extern const struct _term_ devices [];
  114. static char const * optv [] =
  115. {
  116. "eCi:qv",
  117. "device [device] [...] [> stdout]",
  118. "Qualcomm Atheros Network Probe Utility",
  119. "e\tredirect stderr messages to stdout",
  120. "C\tclear counters after reading",
  121. #if defined (WINPCAP) || defined (LIBPCAP)
  122. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  123. #else
  124. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  125. #endif
  126. "q\tquiet mode",
  127. "v\tverbose mode",
  128. (char const *) (0)
  129. };
  130. #include "../plc/plc.c"
  131. signed c;
  132. if (getenv (PLCDEVICE))
  133. {
  134. #if defined (WINPCAP) || defined (LIBPCAP)
  135. channel.ifindex = atoi (getenv (PLCDEVICE));
  136. #else
  137. channel.ifname = strdup (getenv (PLCDEVICE));
  138. #endif
  139. }
  140. optind = 1;
  141. while (~ (c = getoptv (argc, argv, optv)))
  142. {
  143. switch (c)
  144. {
  145. case 'e':
  146. dup2 (STDOUT_FILENO, STDERR_FILENO);
  147. break;
  148. case 'C':
  149. plc.action = 1;
  150. break;
  151. case 'i':
  152. #if defined (WINPCAP) || defined (LIBPCAP)
  153. channel.ifindex = atoi (optarg);
  154. #else
  155. channel.ifname = optarg;
  156. #endif
  157. break;
  158. case 'q':
  159. _setbits (channel.flags, CHANNEL_SILENCE);
  160. _setbits (plc.flags, PLC_SILENCE);
  161. break;
  162. case 'v':
  163. _setbits (channel.flags, CHANNEL_VERBOSE);
  164. _setbits (plc.flags, PLC_VERBOSE);
  165. break;
  166. default:
  167. break;
  168. }
  169. }
  170. argc -= optind;
  171. argv += optind;
  172. openchannel (& channel);
  173. if (! (plc.message = malloc (sizeof (* plc.message))))
  174. {
  175. error (1, errno, PLC_NOMEMORY);
  176. }
  177. if (! argc)
  178. {
  179. NetworkProbe (& plc);
  180. }
  181. while ((argc) && (* argv))
  182. {
  183. if (! hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  184. {
  185. error (1, errno, PLC_BAD_MAC, * argv);
  186. }
  187. NetworkProbe (& plc);
  188. argc--;
  189. argv++;
  190. }
  191. free (plc.message);
  192. closechannel (& channel);
  193. return (0);
  194. }