int6kecho.c 6.0 KB

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