plcset.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*"
  42. *
  43. * plcset.c -
  44. *
  45. *
  46. * Contributor(s):
  47. * Charles Maier
  48. *
  49. *--------------------------------------------------------------------*/
  50. /*====================================================================*"
  51. * system header files;
  52. *--------------------------------------------------------------------*/
  53. #include <unistd.h>
  54. #include <stdlib.h>
  55. #include <stdint.h>
  56. #include <limits.h>
  57. /*====================================================================*
  58. * custom header files;
  59. *--------------------------------------------------------------------*/
  60. #include "../tools/getoptv.h"
  61. #include "../tools/putoptv.h"
  62. #include "../tools/memory.h"
  63. #include "../tools/number.h"
  64. #include "../tools/symbol.h"
  65. #include "../tools/types.h"
  66. #include "../tools/flags.h"
  67. #include "../tools/files.h"
  68. #include "../tools/error.h"
  69. #include "../plc/plc.h"
  70. #include "../ether/channel.h"
  71. /*====================================================================*
  72. * custom source files;
  73. *--------------------------------------------------------------------*/
  74. #ifndef MAKEFILE
  75. #include "../plc/Devices.c"
  76. #include "../plc/Confirm.c"
  77. #include "../plc/Display.c"
  78. #include "../plc/Failure.c"
  79. #include "../plc/Request.c"
  80. #include "../plc/ReadMME.c"
  81. #include "../plc/SendMME.c"
  82. #include "../plc/SetProperty.c"
  83. #endif
  84. #ifndef MAKEFILE
  85. #include "../tools/synonym.c"
  86. #include "../tools/getoptv.c"
  87. #include "../tools/putoptv.c"
  88. #include "../tools/version.c"
  89. #include "../tools/uintspec.c"
  90. #include "../tools/basespec.c"
  91. #include "../tools/dataspec.c"
  92. #include "../tools/bytespec.c"
  93. #include "../tools/hexdump.c"
  94. #include "../tools/hexencode.c"
  95. #include "../tools/hexdecode.c"
  96. #include "../tools/todigit.c"
  97. #include "../tools/binout.c"
  98. #include "../tools/error.c"
  99. #include "../tools/memencode.c"
  100. #endif
  101. #ifndef MAKEFILE
  102. #include "../ether/openchannel.c"
  103. #include "../ether/closechannel.c"
  104. #include "../ether/readpacket.c"
  105. #include "../ether/sendpacket.c"
  106. #include "../ether/channel.c"
  107. #endif
  108. #ifndef MAKEFILE
  109. #include "../mme/MMECode.c"
  110. #include "../mme/EthernetHeader.c"
  111. #include "../mme/QualcommHeader.c"
  112. #include "../mme/UnwantedMessage.c"
  113. #endif
  114. /*====================================================================*
  115. * program constants;
  116. *--------------------------------------------------------------------*/
  117. #define PLCSET_PROP_VERSION 0
  118. #define PLCSET_PROP_OPTION 1
  119. /*====================================================================*
  120. *
  121. * int main (int argc, char const * argv[]);
  122. *
  123. * parse command line, populate plc structure and perform selected
  124. * operations; show help summary if asked; see getoptv and putoptv
  125. * to understand command line parsing and help summary display; see
  126. * plc.h for the definition of struct plc;
  127. *
  128. * the command line accepts multiple MAC addresses and the program
  129. * performs the specified operations on each address, in turn; the
  130. * address order is significant but the option order is not; the
  131. * default address is a local broadcast that causes all devices on
  132. * the local H1 interface to respond but not those at the remote
  133. * end of the powerline;
  134. *
  135. * the default address is 00:B0:52:00:00:01; omitting the address
  136. * will automatically address the local device; some options will
  137. * cancel themselves if this makes no sense;
  138. *
  139. * the default interface is eth1 because most people use eth0 as
  140. * their principle network connection; you can specify another
  141. * interface with -i or define environment string PLC to make
  142. * that the default interface and save typing;
  143. *
  144. *
  145. *--------------------------------------------------------------------*/
  146. int main (int argc, char const * argv [])
  147. {
  148. extern struct channel channel;
  149. static char const * optv [] =
  150. {
  151. "ei:n:o:qs:t:v",
  152. "[type data] [type data] [...] [device] [device] [...]\n\n\ttype :== 'byte'|'word'|'long'|'huge'|'text'|'data'|'zero'|'fill'|'skip'|'hfid'|'mac'|'key'",
  153. "Qualcomm Atheros PLC Set Property",
  154. "e\tredirect stderr to stdout",
  155. #if defined (WINPCAP) || defined (LIBPCAP)
  156. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  157. #else
  158. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  159. #endif
  160. "n n\tproperty identifier or version is (n) [" LITERAL (PLCSET_PROP_VERSION) "]",
  161. "o n\tproperty option is (n) [" LITERAL (PLCSET_PROP_OPTION) "]",
  162. "q\tquiet mode",
  163. "s x\tsession identifier is (x) [" LITERAL (PLCSESSION) "]",
  164. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  165. "v\tverbose mode",
  166. (char const *) (0)
  167. };
  168. #include "../plc/plc.c"
  169. struct plcproperty plcproperty;
  170. signed c;
  171. if (getenv (PLCDEVICE))
  172. {
  173. #if defined (WINPCAP) || defined (LIBPCAP)
  174. channel.ifindex = atoi (getenv (PLCDEVICE));
  175. #else
  176. channel.ifname = strdup (getenv (PLCDEVICE));
  177. #endif
  178. }
  179. optind = 1;
  180. memset (&plcproperty, 0, sizeof (plcproperty));
  181. plcproperty.PROP_OPTION = PLCSET_PROP_OPTION;
  182. plcproperty.PROP_VERSION = PLCSET_PROP_VERSION;
  183. while ((c = getoptv (argc, argv, optv)) != -1)
  184. {
  185. switch (c)
  186. {
  187. case 'e':
  188. dup2 (STDOUT_FILENO, STDERR_FILENO);
  189. break;
  190. case 'i':
  191. #if defined (WINPCAP) || defined (LIBPCAP)
  192. channel.ifindex = atoi (optarg);
  193. #else
  194. channel.ifname = optarg;
  195. #endif
  196. break;
  197. case 'n':
  198. if (!plcproperty.PROP_NUMBER)
  199. {
  200. plcproperty.PROP_NUMBER = (uint32_t)(uintspec (optarg, 0, UINT_MAX));
  201. }
  202. else
  203. {
  204. plcproperty.PROP_VERSION = (uint32_t)(uintspec (optarg, 0, UINT_MAX));
  205. }
  206. break;
  207. case 'o':
  208. plcproperty.PROP_OPTION = (uint8_t)(basespec (optarg, 16, sizeof (plcproperty.PROP_OPTION)));
  209. break;
  210. case 'q':
  211. _setbits (channel.flags, CHANNEL_SILENCE);
  212. _setbits (plc.flags, PLC_SILENCE);
  213. break;
  214. case 's':
  215. plc.cookie = (uint32_t)(basespec (optarg, 16, sizeof (plc.cookie)));
  216. break;
  217. case 't':
  218. channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
  219. break;
  220. case 'v':
  221. _setbits (channel.flags, CHANNEL_VERBOSE);
  222. _setbits (plc.flags, PLC_VERBOSE);
  223. break;
  224. default:
  225. break;
  226. }
  227. }
  228. argc -= optind;
  229. argv += optind;
  230. while ((argc > 1) && (* argv))
  231. {
  232. plcproperty.DATA_LENGTH += (uint32_t)(memencode (plcproperty.DATA_BUFFER + plcproperty.DATA_LENGTH, sizeof (plcproperty.DATA_BUFFER) - plcproperty.DATA_LENGTH, argv [0], argv [1]));
  233. argc -= 2;
  234. argv += 2;
  235. }
  236. openchannel (&channel);
  237. if (!(plc.message = malloc (sizeof (* plc.message))))
  238. {
  239. error (1, errno, PLC_NOMEMORY);
  240. }
  241. if (!argc)
  242. {
  243. SetProperty (&plc, &plcproperty);
  244. }
  245. while ((argc) && (* argv))
  246. {
  247. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  248. {
  249. error (1, errno, PLC_BAD_MAC, * argv);
  250. }
  251. SetProperty (&plc, &plcproperty);
  252. argc--;
  253. argv++;
  254. }
  255. free (plc.message);
  256. closechannel (&channel);
  257. exit (0);
  258. }