ampID.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. * ampID.c - Atheros Powerline Device Identity
  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 <limits.h>
  56. #include <string.h>
  57. #include <ctype.h>
  58. /*====================================================================*
  59. * custom header files;
  60. *--------------------------------------------------------------------*/
  61. #include "../tools/getoptv.h"
  62. #include "../tools/putoptv.h"
  63. #include "../tools/memory.h"
  64. #include "../tools/number.h"
  65. #include "../tools/symbol.h"
  66. #include "../tools/types.h"
  67. #include "../tools/flags.h"
  68. #include "../tools/files.h"
  69. #include "../tools/error.h"
  70. #include "../plc/plc.h"
  71. #include "../ram/nvram.h"
  72. #include "../ram/sdram.h"
  73. #include "../nvm/nvm.h"
  74. #include "../pib/pib.h"
  75. #include "../mme/mme.h"
  76. /*====================================================================*
  77. * custom source files;
  78. *--------------------------------------------------------------------*/
  79. #ifndef MAKEFILE
  80. #include "../tools/getoptv.c"
  81. #include "../tools/putoptv.c"
  82. #include "../tools/version.c"
  83. #include "../tools/uintspec.c"
  84. #include "../tools/hexencode.c"
  85. #include "../tools/hexdecode.c"
  86. #include "../tools/todigit.c"
  87. #include "../tools/hexdump.c"
  88. #include "../tools/hexout.c"
  89. #include "../tools/error.c"
  90. #include "../tools/synonym.c"
  91. #endif
  92. #ifndef MAKEFILE
  93. #include "../mme/EthernetHeader.c"
  94. #include "../mme/QualcommHeader.c"
  95. #include "../mme/UnwantedMessage.c"
  96. #include "../plc/Display.c"
  97. #include "../plc/Devices.c"
  98. #endif
  99. #ifndef MAKEFILE
  100. #include "../ether/openchannel.c"
  101. #include "../ether/closechannel.c"
  102. #include "../ether/readpacket.c"
  103. #include "../ether/sendpacket.c"
  104. #include "../ether/channel.c"
  105. #endif
  106. #ifndef MAKEFILE
  107. #include "../mme/MMECode.c"
  108. #endif
  109. /*====================================================================*
  110. * program constants;
  111. *--------------------------------------------------------------------*/
  112. #define INT6KID_DAK 0
  113. #define INT6KID_NMK 1
  114. #define INT6KID_MAC 2
  115. #define INT6KID_MFG 3
  116. #define INT6KID_USR 4
  117. #define INT6KID_NET 5
  118. /*====================================================================*
  119. *
  120. * void ReadKey1 (struct channel * channel, unsigned c, int key);
  121. *
  122. * read the first block of the PIB from a device then echo one of
  123. * several parameters on stdout as a string; program output can be
  124. * used in scripts to define variables or compare strings;
  125. *
  126. * this function is an abridged version of ReadParameters(); it reads only
  127. * the first 1024 bytes of the PIB then stops; most parameters of
  128. * general interest occur in that block;
  129. *
  130. *
  131. *--------------------------------------------------------------------*/
  132. static void ReadKey1 (struct channel * channel, unsigned c, int key)
  133. {
  134. struct message message;
  135. static signed count = 0;
  136. signed packetsize;
  137. #ifndef __GNUC__
  138. #pragma pack (push,1)
  139. #endif
  140. struct __packed vs_rd_mod_request
  141. {
  142. struct ethernet_hdr ethernet;
  143. struct qualcomm_hdr qualcomm;
  144. uint8_t MODULEID;
  145. uint8_t RESERVED;
  146. uint16_t MLENGTH;
  147. uint32_t MOFFSET;
  148. uint8_t DAK [16];
  149. }
  150. * request = (struct vs_rd_mod_request *)(&message);
  151. struct __packed vs_rd_mod_confirm
  152. {
  153. struct ethernet_hdr ethernet;
  154. struct qualcomm_hdr qualcomm;
  155. uint8_t MSTATUS;
  156. uint8_t RESERVED1 [3];
  157. uint8_t MODULEID;
  158. uint8_t RESERVED2;
  159. uint16_t MLENGTH;
  160. uint32_t MOFFSET;
  161. uint32_t MCHKSUM;
  162. struct simple_pib pib;
  163. }
  164. * confirm = (struct vs_rd_mod_confirm *)(&message);
  165. #ifndef __GNUC__
  166. #pragma pack (pop)
  167. #endif
  168. memset (&message, 0, sizeof (message));
  169. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  170. QualcommHeader (&request->qualcomm, 0, (VS_RD_MOD | MMTYPE_REQ));
  171. request->MODULEID = VS_MODULE_PIB;
  172. request->MLENGTH = HTOLE16 (PLC_RECORD_SIZE);
  173. request->MOFFSET = HTOLE32 (0);
  174. if (sendpacket (channel, &message, (ETHER_MIN_LEN - ETHER_CRC_LEN)) < 0)
  175. {
  176. error (1, errno, CHANNEL_CANTSEND);
  177. }
  178. while ((packetsize = readpacket (channel, &message, sizeof (message))) > 0)
  179. {
  180. if (UnwantedMessage (&message, packetsize, 0, (VS_RD_MOD | MMTYPE_CNF)))
  181. {
  182. continue;
  183. }
  184. if (confirm->MSTATUS)
  185. {
  186. error (0, 0, "%s (%0X): ", MMECode (confirm->qualcomm.MMTYPE, confirm->MSTATUS), confirm->MSTATUS);
  187. continue;
  188. }
  189. if (count++ > 0)
  190. {
  191. putc (c, stdout);
  192. }
  193. if (key == INT6KID_MAC)
  194. {
  195. hexout (confirm->pib.MAC, sizeof (confirm->pib.MAC), HEX_EXTENDER, 0, stdout);
  196. continue;
  197. }
  198. if (key == INT6KID_DAK)
  199. {
  200. hexout (confirm->pib.DAK, sizeof (confirm->pib.DAK), HEX_EXTENDER, 0, stdout);
  201. continue;
  202. }
  203. if (key == INT6KID_NMK)
  204. {
  205. hexout (confirm->pib.NMK, sizeof (confirm->pib.NMK), HEX_EXTENDER, 0, stdout);
  206. continue;
  207. }
  208. if (key == INT6KID_MFG)
  209. {
  210. confirm->pib.MFG [PIB_HFID_LEN - 1] = (char)(0);
  211. printf ("%s", confirm->pib.MFG);
  212. continue;
  213. }
  214. if (key == INT6KID_USR)
  215. {
  216. confirm->pib.USR [PIB_HFID_LEN - 1] = (char)(0);
  217. printf ("%s", confirm->pib.USR);
  218. continue;
  219. }
  220. if (key == INT6KID_NET)
  221. {
  222. confirm->pib.NET [PIB_HFID_LEN - 1] = (char)(0);
  223. printf ("%s", confirm->pib.NET);
  224. continue;
  225. }
  226. }
  227. if (packetsize < 0)
  228. {
  229. error (1, errno, CHANNEL_CANTREAD);
  230. }
  231. return;
  232. }
  233. /*====================================================================*
  234. *
  235. * int main (int argc, char const * argv []);
  236. *
  237. *
  238. *
  239. *--------------------------------------------------------------------*/
  240. int main (int argc, char const * argv [])
  241. {
  242. extern struct channel channel;
  243. static char const * optv [] =
  244. {
  245. "Ac:Dei:MnNqSUv",
  246. "device",
  247. "Qualcomm Atheros Powerline Device Identity",
  248. "A\tEthernet address (MAC)",
  249. "c c\tcharacter delimiter is (c)",
  250. "D\tDevice Access Key (DAK)",
  251. "e\tredirect stderr to stdout",
  252. #if defined (WINPCAP) || defined (LIBPCAP)
  253. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  254. #else
  255. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  256. #endif
  257. "M\tNetwork Membership Key (NMK)",
  258. "n\tappend newline on output",
  259. "N\tnetwork HFID",
  260. "q\tquiet mode",
  261. "S\tmanufacturer HFID",
  262. "U\tuser HFID",
  263. "v\tverbose mode",
  264. (char const *) (0)
  265. };
  266. signed newline = '\n';
  267. signed key = INT6KID_DAK;
  268. flag_t flags = (flag_t)(0);
  269. signed c;
  270. if (getenv (PLCDEVICE))
  271. {
  272. #if defined (WINPCAP) || defined (LIBPCAP)
  273. channel.ifindex = atoi (getenv (PLCDEVICE));
  274. #else
  275. channel.ifname = strdup (getenv (PLCDEVICE));
  276. #endif
  277. }
  278. optind = 1;
  279. while ((c = getoptv (argc, argv, optv)) != -1)
  280. {
  281. switch (c)
  282. {
  283. case 'A':
  284. key = INT6KID_MAC;
  285. break;
  286. case 'c':
  287. newline = * optarg;
  288. break;
  289. case 'D':
  290. key = INT6KID_DAK;
  291. break;
  292. case 'e':
  293. dup2 (STDOUT_FILENO, STDERR_FILENO);
  294. break;
  295. case 'i':
  296. #if defined (WINPCAP) || defined (LIBPCAP)
  297. channel.ifindex = atoi (optarg);
  298. #else
  299. channel.ifname = optarg;
  300. #endif
  301. break;
  302. case 'M':
  303. key = INT6KID_NMK;
  304. break;
  305. case 'n':
  306. _setbits (flags, PLC_NEWLINE);
  307. break;
  308. case 'N':
  309. key = INT6KID_NET;
  310. break;
  311. case 'q':
  312. _setbits (channel.flags, CHANNEL_SILENCE);
  313. _setbits (flags, PLC_SILENCE);
  314. break;
  315. case 'S':
  316. key = INT6KID_MFG;
  317. break;
  318. case 'U':
  319. key = INT6KID_USR;
  320. break;
  321. case 'v':
  322. _setbits (channel.flags, CHANNEL_VERBOSE);
  323. _setbits (flags, PLC_VERBOSE);
  324. break;
  325. default:
  326. break;
  327. }
  328. }
  329. argc -= optind;
  330. argv += optind;
  331. openchannel (&channel);
  332. if (!argc)
  333. {
  334. ReadKey1 (&channel, newline, key);
  335. if (_anyset (flags, PLC_NEWLINE))
  336. {
  337. putc (newline, stdout);
  338. }
  339. }
  340. while ((argc) && (* argv))
  341. {
  342. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  343. {
  344. error (1, errno, PLC_BAD_MAC, * argv);
  345. }
  346. ReadKey1 (&channel, newline, key);
  347. if (_anyset (flags, PLC_NEWLINE))
  348. {
  349. putc (newline, stdout);
  350. }
  351. argv++;
  352. argc--;
  353. }
  354. closechannel (&channel);
  355. return (0);
  356. }