plcecho.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * plcecho.c -
  11. *
  12. *--------------------------------------------------------------------*/
  13. /*====================================================================*
  14. * system header files;
  15. *--------------------------------------------------------------------*/
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <stdint.h>
  19. #include <limits.h>
  20. /*====================================================================*
  21. * custom header files;
  22. *--------------------------------------------------------------------*/
  23. #include "../tools/getoptv.h"
  24. #include "../tools/putoptv.h"
  25. #include "../tools/memory.h"
  26. #include "../tools/number.h"
  27. #include "../tools/symbol.h"
  28. #include "../tools/types.h"
  29. #include "../tools/flags.h"
  30. #include "../tools/files.h"
  31. #include "../tools/error.h"
  32. #include "../mme/mme.h"
  33. #include "../plc/plc.h"
  34. #include "../nda/nda.h"
  35. /*====================================================================*
  36. * custom source files;
  37. *--------------------------------------------------------------------*/
  38. #ifndef MAKEFILE
  39. #include "../plc/Failure.c"
  40. #include "../plc/Display.c"
  41. #include "../plc/ReadMME.c"
  42. #include "../plc/SendMME.c"
  43. #include "../plc/Devices.c"
  44. #endif
  45. #ifndef MAKEFILE
  46. #include "../tools/assist.c"
  47. #include "../tools/error.c"
  48. #include "../tools/codelist.c"
  49. #include "../tools/getoptv.c"
  50. #include "../tools/putoptv.c"
  51. #include "../tools/version.c"
  52. #include "../tools/uintspec.c"
  53. #include "../tools/hexdump.c"
  54. #include "../tools/hexencode.c"
  55. #include "../tools/hexdecode.c"
  56. #include "../tools/hexstring.c"
  57. #include "../tools/hexload.c"
  58. #include "../tools/todigit.c"
  59. #include "../tools/checkfilename.c"
  60. #include "../tools/checksum32.c"
  61. #include "../tools/fdchecksum32.c"
  62. #include "../tools/strfbits.c"
  63. #include "../tools/typename.c"
  64. #include "../tools/lookup.c"
  65. #include "../tools/synonym.c"
  66. #endif
  67. #ifndef MAKEFILE
  68. #include "../ether/openchannel.c"
  69. #include "../ether/closechannel.c"
  70. #include "../ether/readpacket.c"
  71. #include "../ether/sendpacket.c"
  72. #include "../ether/channel.c"
  73. #endif
  74. #ifndef MAKEFILE
  75. #include "../mme/EthernetHeader.c"
  76. #include "../mme/QualcommHeader.c"
  77. #include "../mme/UnwantedMessage.c"
  78. #include "../mme/MMECode.c"
  79. #endif
  80. #ifndef MAKEFILE
  81. #include "../nda/Loopback.c"
  82. #endif
  83. /*====================================================================*
  84. * program constants;
  85. *--------------------------------------------------------------------*/
  86. #define INT6KECHO_VERBOSE (1 << 0)
  87. #define INT6KECHO_SILENCE (1 << 1)
  88. #define INT6KECHO_DEFAULT (1 << 2)
  89. /*====================================================================*
  90. *
  91. * int main (int argc, char const * argv[]);
  92. *
  93. * parse command line, populate plc structure and perform selected
  94. * operations; show help summary if asked; see getoptv and putoptv
  95. * to understand command line parsing and help summary display; see
  96. * plc.h for the definition of struct plc;
  97. *
  98. * the command line accepts multiple MAC addresses and the program
  99. * performs the specified operations on each address, in turn; the
  100. * address order is significant but the option order is not; the
  101. * default address is a local broadcast that causes all devices on
  102. * the local H1 interface to respond but not those at the remote
  103. * end of the powerline;
  104. *
  105. * the default address is 00:B0:52:00:00:01; omitting the address
  106. * will automatically address the local device; some options will
  107. * cancel themselves if this makes no sense;
  108. *
  109. * the default interface is eth1 because most people use eth0 as
  110. * their principle network connection; you can specify another
  111. * interface with -i or define environment string PLC to make
  112. * that the default interface and save typing;
  113. *
  114. *--------------------------------------------------------------------*/
  115. int main (int argc, char const * argv [])
  116. {
  117. extern struct channel channel;
  118. static char const * optv [] =
  119. {
  120. "d:f:i:qv",
  121. "device [device] [...] [> stdout]",
  122. "Qualcomm Atheros Loopback Utility",
  123. "d n\tduration is (n) seconds [" LITERAL (PLC_ECHOTIME) "]",
  124. "f s\tframe file is (s)",
  125. #if defined (WINPCAP) || defined (LIBPCAP)
  126. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  127. #else
  128. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  129. #endif
  130. "q\tquiet mode",
  131. "v\tverbose mode",
  132. (char const *) (0)
  133. };
  134. #include "../plc/plc.c"
  135. FILE * fp;
  136. byte memory [1038];
  137. size_t extent = 0;
  138. signed c;
  139. if (getenv (PLCDEVICE))
  140. {
  141. #if defined (WINPCAP) || defined (LIBPCAP)
  142. channel.ifindex = atoi (getenv (PLCDEVICE));
  143. #else
  144. channel.ifname = strdup (getenv (PLCDEVICE));
  145. #endif
  146. }
  147. optind = 1;
  148. plc.timer = PLC_ECHOTIME;
  149. memset (memory, 0, sizeof (memory));
  150. while (~ (c = getoptv (argc, argv, optv)))
  151. {
  152. switch (c)
  153. {
  154. case 'd':
  155. plc.timer = (signed) (uintspec (optarg, 0, UCHAR_MAX));
  156. break;
  157. case 'f':
  158. if (! (fp = fopen (optarg, "rb")))
  159. {
  160. error (1, errno, "%s", optarg);
  161. }
  162. extent = hexload (memory, sizeof (memory), fp);
  163. fclose (fp);
  164. break;
  165. case 'i':
  166. #if defined (WINPCAP) || defined (LIBPCAP)
  167. channel.ifindex = atoi (optarg);
  168. #else
  169. channel.ifname = optarg;
  170. #endif
  171. break;
  172. case 'q':
  173. _setbits (channel.flags, CHANNEL_SILENCE);
  174. break;
  175. case 'v':
  176. _setbits (channel.flags, CHANNEL_VERBOSE);
  177. break;
  178. default:
  179. break;
  180. }
  181. }
  182. argc -= optind;
  183. argv += optind;
  184. openchannel (& channel);
  185. if (! (plc.message = malloc (sizeof (* plc.message))))
  186. {
  187. error (1, errno, PLC_NOMEMORY);
  188. }
  189. if (! argc)
  190. {
  191. Loopback (& plc, memory, extent);
  192. }
  193. while ((argc) && (* argv))
  194. {
  195. if (! hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  196. {
  197. error (1, errno, PLC_BAD_MAC, * argv);
  198. }
  199. Loopback (& plc, memory, extent);
  200. argc--;
  201. argv++;
  202. }
  203. closechannel (& channel);
  204. return (0);
  205. }