plcmcinfo.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * plcmcinfo.c -
  11. *
  12. * Contributor(s):
  13. * Charles Maier <cmaier@qca.qualcomm.com>
  14. * Nathaniel Houghton <nhoughto@qca.qualcomm.com>
  15. *
  16. *--------------------------------------------------------------------*/
  17. /*====================================================================*
  18. * system header files;
  19. *--------------------------------------------------------------------*/
  20. #include <unistd.h>
  21. #include <stdlib.h>
  22. #include <stdint.h>
  23. #include <limits.h>
  24. /*====================================================================*
  25. * custom header files;
  26. *--------------------------------------------------------------------*/
  27. #include "../tools/getoptv.h"
  28. #include "../tools/putoptv.h"
  29. #include "../tools/memory.h"
  30. #include "../tools/number.h"
  31. #include "../tools/symbol.h"
  32. #include "../tools/types.h"
  33. #include "../tools/flags.h"
  34. #include "../tools/files.h"
  35. #include "../tools/error.h"
  36. #include "../ether/channel.h"
  37. #include "../key/HPAVKey.h"
  38. #include "../key/keys.h"
  39. #include "../ram/sdram.h"
  40. #include "../pib/pib.h"
  41. #include "../nvm/nvm.h"
  42. #include "../plc/plc.h"
  43. /*====================================================================*
  44. * custom source files;
  45. *--------------------------------------------------------------------*/
  46. #ifndef MAKEFILE
  47. #include "../plc/Confirm.c"
  48. #include "../plc/Devices.c"
  49. #include "../plc/Display.c"
  50. #include "../plc/Failure.c"
  51. #include "../plc/ReadFMI.c"
  52. #include "../plc/ReadMME.c"
  53. #include "../plc/SendMME.c"
  54. #include "../plc/Request.c"
  55. #include "../plc/chipset.c"
  56. #endif
  57. #ifndef MAKEFILE
  58. #include "../nda/MulticastInfo1.c"
  59. #include "../nda/MulticastInfo2.c"
  60. #endif
  61. #ifndef MAKEFILE
  62. #include "../tools/error.c"
  63. #include "../tools/getoptv.c"
  64. #include "../tools/putoptv.c"
  65. #include "../tools/synonym.c"
  66. #include "../tools/uintspec.c"
  67. #include "../tools/basespec.c"
  68. #include "../tools/version.c"
  69. #include "../tools/hexdump.c"
  70. #include "../tools/hexencode.c"
  71. #include "../tools/hexdecode.c"
  72. #include "../tools/hexstring.c"
  73. #include "../tools/decstring.c"
  74. #include "../tools/decdecode.c"
  75. #include "../tools/hexout.c"
  76. #include "../tools/todigit.c"
  77. #include "../tools/checksum32.c"
  78. #include "../tools/strfbits.c"
  79. #include "../tools/typename.c"
  80. #endif
  81. #ifndef MAKEFILE
  82. #include "../ether/channel.c"
  83. #include "../ether/openchannel.c"
  84. #include "../ether/closechannel.c"
  85. #include "../ether/readpacket.c"
  86. #include "../ether/sendpacket.c"
  87. #endif
  88. #ifndef MAKEFILE
  89. #include "../ram/nvram.c"
  90. #include "../ram/sdramfile.c"
  91. #include "../ram/sdrampeek.c"
  92. #endif
  93. #ifndef MAKEFILE
  94. #include "../mme/EthernetHeader.c"
  95. #include "../mme/QualcommHeader.c"
  96. #include "../mme/QualcommHeader1.c"
  97. #include "../mme/UnwantedMessage.c"
  98. #include "../mme/MMECode.c"
  99. #endif
  100. /*====================================================================*
  101. * program constants;
  102. *--------------------------------------------------------------------*/
  103. #define PLCMCINFO_ADDRESS_TYPE 0
  104. #define PLCMCINFO_REQUEST_TYPE 0
  105. #define PLCMCINFO_CLASSIC (1 << 0)
  106. /*====================================================================*
  107. * program variables;
  108. *--------------------------------------------------------------------*/
  109. static struct _term_ const addresses [] =
  110. {
  111. {
  112. "IPv4",
  113. "0x00"
  114. },
  115. {
  116. "IPv6",
  117. "0x01"
  118. },
  119. {
  120. "MAC",
  121. "0x03"
  122. }
  123. };
  124. /*====================================================================*
  125. *
  126. * int main (int argc, char const * argv[]);
  127. *
  128. * parse command line, populate plc structure and perform selected
  129. * operations; show help summary if asked; see getoptv and putoptv
  130. * to understand command line parsing and help summary display; see
  131. * plc.h for the definition of struct plc;
  132. *
  133. * the command line accepts multiple MAC addresses and the program
  134. * performs the specified operations on each address, in turn; the
  135. * address order is significant but the option order is not; the
  136. * default address is a local broadcast that causes all devices on
  137. * the local H1 interface to respond but not those at the remote
  138. * end of the powerline;
  139. *
  140. * the default address is 00:B0:52:00:00:01; omitting the address
  141. * will automatically address the local device; some options will
  142. * cancel themselves if this makes no sense;
  143. *
  144. * the default interface is eth1 because most people use eth0 as
  145. * their principle network connection; you can specify another
  146. * interface with -i or define environment string PLC to make
  147. * that the default interface and save typing;
  148. *
  149. *--------------------------------------------------------------------*/
  150. int main (int argc, char const * argv [])
  151. {
  152. extern struct channel channel;
  153. static char const * optv [] =
  154. {
  155. "a:cegi:qt:v",
  156. "device [device] [...]",
  157. "Qualcomm Atheros Multicast Information Probe",
  158. "a n\taddress type is (n) [" LITERAL (PLCMCINFO_ADDRESS_TYPE) "]",
  159. "c\tclassic method",
  160. "e\tredirect stderr to stdout",
  161. #if defined (WINPCAP) || defined (LIBPCAP)
  162. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  163. #else
  164. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  165. #endif
  166. "q\tquiet mode",
  167. "t n\trequest type is (n) [" LITERAL (PLCMCINFO_REQUEST_TYPE) "]",
  168. "v\tverbose mode",
  169. (char const *) (0)
  170. };
  171. #include "../plc/plc.c"
  172. flag_t flags = (flag_t) (0);
  173. signed c;
  174. plc.action = PLCMCINFO_ADDRESS_TYPE;
  175. if (getenv (PLCDEVICE))
  176. {
  177. #if defined (WINPCAP) || defined (LIBPCAP)
  178. channel.ifindex = atoi (getenv (PLCDEVICE));
  179. #else
  180. channel.ifname = strdup (getenv (PLCDEVICE));
  181. #endif
  182. }
  183. optind = 1;
  184. while (~ (c = getoptv (argc, argv, optv)))
  185. {
  186. switch (c)
  187. {
  188. case 'a':
  189. plc.sector = (unsigned) (uintspec (synonym (optarg, addresses, SIZEOF (addresses)), 0, UCHAR_MAX));
  190. error (1, ENOTSUP, "Address Type");
  191. break;
  192. case 'c':
  193. _setbits (flags, PLCMCINFO_CLASSIC);
  194. break;
  195. case 'e':
  196. dup2 (STDOUT_FILENO, STDERR_FILENO);
  197. break;
  198. case 'i':
  199. #if defined (WINPCAP) || defined (LIBPCAP)
  200. channel.ifindex = atoi (optarg);
  201. #else
  202. channel.ifname = optarg;
  203. #endif
  204. break;
  205. case 't':
  206. plc.action = (unsigned) (uintspec (optarg, 0, UCHAR_MAX));
  207. error (1, ENOTSUP, "Request Type");
  208. break;
  209. case 'v':
  210. _setbits (channel.flags, CHANNEL_VERBOSE);
  211. _setbits (plc.flags, PLC_VERBOSE);
  212. break;
  213. default:
  214. break;
  215. }
  216. }
  217. argc -= optind;
  218. argv += optind;
  219. openchannel (& channel);
  220. if (! (plc.message = malloc (sizeof (* plc.message))))
  221. {
  222. error (1, errno, PLC_NOMEMORY);
  223. }
  224. if (! argc)
  225. {
  226. if (_anyset (flags, PLCMCINFO_CLASSIC))
  227. {
  228. MulticastInfo1 (& plc);
  229. }
  230. else
  231. {
  232. MulticastInfo2 (& plc);
  233. }
  234. }
  235. while ((argc) && (* argv))
  236. {
  237. if (! hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  238. {
  239. error (1, errno, PLC_BAD_MAC, * argv);
  240. }
  241. if (_anyset (flags, PLCMCINFO_CLASSIC))
  242. {
  243. MulticastInfo1 (& plc);
  244. }
  245. else
  246. {
  247. MulticastInfo2 (& plc);
  248. }
  249. argc--;
  250. argv++;
  251. }
  252. free (plc.message);
  253. closechannel (& channel);
  254. exit (0);
  255. }