int6kid.c.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?xml version='1.0' encoding='iso-8859-1'?>
  2. <!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  3. <html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
  4. <head>
  5. <title>
  6. int6kid.c
  7. </title>
  8. <meta http-equiv='content-type' content='text/html;iso-8859-1'/>
  9. <meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
  10. <meta name='author' content='cmaier@cmassoc.net'/>
  11. <meta name='robots' content='noindex,nofollow'/>
  12. <link href='toolkit.css' rel='stylesheet' type='text/css'/>
  13. </head>
  14. <body>
  15. <div class='headerlink'>
  16. [<a href='int6khost.c.html' title=' int6khost.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='int6klist.c.html' title=' int6klist.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  24. *
  25. * All rights reserved.
  26. *
  27. * Redistribution and use in source and binary forms, with or
  28. * without modification, are permitted (subject to the limitations
  29. * in the disclaimer below) provided that the following conditions
  30. * are met:
  31. *
  32. * * Redistributions of source code must retain the above copyright
  33. * notice, this list of conditions and the following disclaimer.
  34. *
  35. * * Redistributions in binary form must reproduce the above
  36. * copyright notice, this list of conditions and the following
  37. * disclaimer in the documentation and/or other materials
  38. * provided with the distribution.
  39. *
  40. * * Neither the name of Qualcomm Atheros nor the names of
  41. * its contributors may be used to endorse or promote products
  42. * derived from this software without specific prior written
  43. * permission.
  44. *
  45. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  46. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  47. * COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot; AND ANY EXPRESS OR
  48. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  49. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  50. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  51. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  52. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  53. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  54. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  56. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  57. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  58. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59. *
  60. *--------------------------------------------------------------------*/
  61. /*====================================================================*
  62. *
  63. * int6kid.c - Atheros Powerline Device Identity
  64. *
  65. *
  66. * Contributor(s):
  67. * Charles Maier &lt;cmaier@qca.qualcomm.com&gt;
  68. * Matthieu Poullet &lt;m.poullet@avm.de&gt;
  69. *
  70. *--------------------------------------------------------------------*/
  71. /*====================================================================*&quot;
  72. * system header files;
  73. *--------------------------------------------------------------------*/
  74. #include &lt;unistd.h&gt;
  75. #include &lt;stdlib.h&gt;
  76. #include &lt;limits.h&gt;
  77. #include &lt;string.h&gt;
  78. #include &lt;ctype.h&gt;
  79. /*====================================================================*
  80. * custom header files;
  81. *--------------------------------------------------------------------*/
  82. #include &quot;../tools/getoptv.h&quot;
  83. #include &quot;../tools/putoptv.h&quot;
  84. #include &quot;../tools/memory.h&quot;
  85. #include &quot;../tools/number.h&quot;
  86. #include &quot;../tools/symbol.h&quot;
  87. #include &quot;../tools/types.h&quot;
  88. #include &quot;../tools/flags.h&quot;
  89. #include &quot;../tools/files.h&quot;
  90. #include &quot;../tools/error.h&quot;
  91. #include &quot;../plc/plc.h&quot;
  92. #include &quot;../ram/nvram.h&quot;
  93. #include &quot;../ram/sdram.h&quot;
  94. #include &quot;../nvm/nvm.h&quot;
  95. #include &quot;../pib/pib.h&quot;
  96. #include &quot;../mme/mme.h&quot;
  97. /*====================================================================*
  98. * custom source files;
  99. *--------------------------------------------------------------------*/
  100. #ifndef MAKEFILE
  101. #include &quot;../tools/getoptv.c&quot;
  102. #include &quot;../tools/putoptv.c&quot;
  103. #include &quot;../tools/version.c&quot;
  104. #include &quot;../tools/uintspec.c&quot;
  105. #include &quot;../tools/hexencode.c&quot;
  106. #include &quot;../tools/hexdecode.c&quot;
  107. #include &quot;../tools/todigit.c&quot;
  108. #include &quot;../tools/hexdump.c&quot;
  109. #include &quot;../tools/hexout.c&quot;
  110. #include &quot;../tools/error.c&quot;
  111. #include &quot;../tools/synonym.c&quot;
  112. #endif
  113. #ifndef MAKEFILE
  114. #include &quot;../mme/EthernetHeader.c&quot;
  115. #include &quot;../mme/QualcommHeader.c&quot;
  116. #include &quot;../mme/UnwantedMessage.c&quot;
  117. #include &quot;../plc/Display.c&quot;
  118. #include &quot;../plc/Devices.c&quot;
  119. #endif
  120. #ifndef MAKEFILE
  121. #include &quot;../ether/openchannel.c&quot;
  122. #include &quot;../ether/closechannel.c&quot;
  123. #include &quot;../ether/readpacket.c&quot;
  124. #include &quot;../ether/sendpacket.c&quot;
  125. #include &quot;../ether/channel.c&quot;
  126. #endif
  127. #ifndef MAKEFILE
  128. #include &quot;../mme/MMECode.c&quot;
  129. #endif
  130. /*====================================================================*
  131. * program constants;
  132. *--------------------------------------------------------------------*/
  133. #define INT6KID_VERBOSE (1 &lt;&lt; 0)
  134. #define INT6KID_SILENCE (1 &lt;&lt; 1)
  135. #define INT6KID_NEWLINE (1 &lt;&lt; 2)
  136. #define INT6KID_DAK 0
  137. #define INT6KID_NMK 1
  138. #define INT6KID_MAC 2
  139. #define INT6KID_MFG 3
  140. #define INT6KID_USR 4
  141. #define INT6KID_NET 5
  142. /*====================================================================*
  143. *
  144. * void ReadKey1 (struct channel * channel, unsigned c, int key);
  145. *
  146. * read the first block of the PIB from a device then echo one of
  147. * several parameters on stdout as a string; program output can be
  148. * used in scripts to define variables or compare strings;
  149. *
  150. * this function is an abridged version of ReadParameters(); it reads only
  151. * the first 1024 bytes of the PIB then stops; most parameters of
  152. * general interest occur in that block;
  153. *
  154. *
  155. *--------------------------------------------------------------------*/
  156. static void ReadKey1 (struct channel * channel, unsigned c, int key)
  157. {
  158. struct message message;
  159. static signed count = 0;
  160. signed packetsize;
  161. #ifndef __GNUC__
  162. #pragma pack (push,1)
  163. #endif
  164. struct __packed vs_rd_mod_request
  165. {
  166. struct ethernet_hdr ethernet;
  167. struct qualcomm_hdr qualcomm;
  168. uint8_t MODULEID;
  169. uint8_t RESERVED;
  170. uint16_t MLENGTH;
  171. uint32_t MOFFSET;
  172. uint8_t DAK [16];
  173. }
  174. * request = (struct vs_rd_mod_request *)(&amp;message);
  175. struct __packed vs_rd_mod_confirm
  176. {
  177. struct ethernet_hdr ethernet;
  178. struct qualcomm_hdr qualcomm;
  179. uint8_t MSTATUS;
  180. uint8_t RESERVED1 [3];
  181. uint8_t MODULEID;
  182. uint8_t RESERVED2;
  183. uint16_t MLENGTH;
  184. uint32_t MOFFSET;
  185. uint32_t MCHKSUM;
  186. struct simple_pib pib;
  187. }
  188. * confirm = (struct vs_rd_mod_confirm *)(&amp;message);
  189. #ifndef __GNUC__
  190. #pragma pack (pop)
  191. #endif
  192. memset (&amp;message, 0, sizeof (message));
  193. EthernetHeader (&amp;request-&gt;ethernet, channel-&gt;peer, channel-&gt;host, channel-&gt;type);
  194. QualcommHeader (&amp;request-&gt;qualcomm, 0, (VS_RD_MOD | MMTYPE_REQ));
  195. request-&gt;MODULEID = VS_MODULE_PIB;
  196. request-&gt;MLENGTH = HTOLE16 (PLC_RECORD_SIZE);
  197. request-&gt;MOFFSET = HTOLE32 (0);
  198. if (sendpacket (channel, &amp;message, (ETHER_MIN_LEN - ETHER_CRC_LEN)) &lt; 0)
  199. {
  200. error (1, errno, CHANNEL_CANTSEND);
  201. }
  202. while ((packetsize = readpacket (channel, &amp;message, sizeof (message))) &gt; 0)
  203. {
  204. if (UnwantedMessage (&amp;message, packetsize, 0, (VS_RD_MOD | MMTYPE_CNF)))
  205. {
  206. continue;
  207. }
  208. if (confirm-&gt;MSTATUS)
  209. {
  210. error (0, 0, &quot;%s (%0X): &quot;, MMECode (confirm-&gt;qualcomm.MMTYPE, confirm-&gt;MSTATUS), confirm-&gt;MSTATUS);
  211. continue;
  212. }
  213. if (count++ &gt; 0)
  214. {
  215. putc (c, stdout);
  216. }
  217. if (key == INT6KID_MAC)
  218. {
  219. hexout (confirm-&gt;pib.MAC, sizeof (confirm-&gt;pib.MAC), HEX_EXTENDER, 0, stdout);
  220. continue;
  221. }
  222. if (key == INT6KID_DAK)
  223. {
  224. hexout (confirm-&gt;pib.DAK, sizeof (confirm-&gt;pib.DAK), HEX_EXTENDER, 0, stdout);
  225. continue;
  226. }
  227. if (key == INT6KID_NMK)
  228. {
  229. hexout (confirm-&gt;pib.NMK, sizeof (confirm-&gt;pib.NMK), HEX_EXTENDER, 0, stdout);
  230. continue;
  231. }
  232. if (key == INT6KID_MFG)
  233. {
  234. confirm-&gt;pib.MFG [PIB_HFID_LEN - 1] = (char)(0);
  235. printf (&quot;%s&quot;, confirm-&gt;pib.MFG);
  236. continue;
  237. }
  238. if (key == INT6KID_USR)
  239. {
  240. confirm-&gt;pib.USR [PIB_HFID_LEN - 1] = (char)(0);
  241. printf (&quot;%s&quot;, confirm-&gt;pib.USR);
  242. continue;
  243. }
  244. if (key == INT6KID_NET)
  245. {
  246. confirm-&gt;pib.NET [PIB_HFID_LEN - 1] = (char)(0);
  247. printf (&quot;%s&quot;, confirm-&gt;pib.NET);
  248. continue;
  249. }
  250. }
  251. if (packetsize &lt; 0)
  252. {
  253. error (1, errno, CHANNEL_CANTREAD);
  254. }
  255. return;
  256. }
  257. /*====================================================================*
  258. *
  259. * int main (int argc, char const * argv []);
  260. *
  261. *
  262. *
  263. *--------------------------------------------------------------------*/
  264. int main (int argc, char const * argv [])
  265. {
  266. extern struct channel channel;
  267. static char const * optv [] =
  268. {
  269. &quot;Ac:Dei:MnNqSUv&quot;,
  270. &quot;device&quot;,
  271. &quot;Qualcomm Atheros Powerline Device Identity&quot;,
  272. &quot;A\tEthernet address (MAC)&quot;,
  273. &quot;c c\tcharacter delimiter is (c)&quot;,
  274. &quot;D\tDevice Access Key (DAK)&quot;,
  275. &quot;e\tredirect stderr to stdout&quot;,
  276. #if defined (WINPCAP) || defined (LIBPCAP)
  277. &quot;i n\thost interface is (n) [&quot; LITERAL (CHANNEL_ETHNUMBER) &quot;]&quot;,
  278. #else
  279. &quot;i s\thost interface is (s) [&quot; LITERAL (CHANNEL_ETHDEVICE) &quot;]&quot;,
  280. #endif
  281. &quot;M\tNetwork Membership Key (NMK)&quot;,
  282. &quot;n\tappend newline on output&quot;,
  283. &quot;N\tnetwork HFID&quot;,
  284. &quot;q\tquiet mode&quot;,
  285. &quot;S\tmanufacturer HFID&quot;,
  286. &quot;U\tuser HFID&quot;,
  287. &quot;v\tverbose mode&quot;,
  288. (char const *) (0)
  289. };
  290. signed newline = '\n';
  291. signed key = INT6KID_DAK;
  292. flag_t flags = (flag_t)(0);
  293. signed c;
  294. if (getenv (PLCDEVICE))
  295. {
  296. #if defined (WINPCAP) || defined (LIBPCAP)
  297. channel.ifindex = atoi (getenv (PLCDEVICE));
  298. #else
  299. channel.ifname = strdup (getenv (PLCDEVICE));
  300. #endif
  301. }
  302. optind = 1;
  303. while ((c = getoptv (argc, argv, optv)) != -1)
  304. {
  305. switch (c)
  306. {
  307. case 'A':
  308. key = INT6KID_MAC;
  309. break;
  310. case 'c':
  311. newline = * optarg;
  312. break;
  313. case 'D':
  314. key = INT6KID_DAK;
  315. break;
  316. case 'e':
  317. dup2 (STDOUT_FILENO, STDERR_FILENO);
  318. break;
  319. case 'i':
  320. #if defined (WINPCAP) || defined (LIBPCAP)
  321. channel.ifindex = atoi (optarg);
  322. #else
  323. channel.ifname = optarg;
  324. #endif
  325. break;
  326. case 'M':
  327. key = INT6KID_NMK;
  328. break;
  329. case 'n':
  330. _setbits (flags, INT6KID_NEWLINE);
  331. break;
  332. case 'N':
  333. key = INT6KID_NET;
  334. break;
  335. case 'q':
  336. _setbits (channel.flags, CHANNEL_SILENCE);
  337. _setbits (flags, PLC_SILENCE);
  338. break;
  339. case 'S':
  340. key = INT6KID_MFG;
  341. break;
  342. case 'U':
  343. key = INT6KID_USR;
  344. break;
  345. case 'v':
  346. _setbits (channel.flags, CHANNEL_VERBOSE);
  347. _setbits (flags, PLC_VERBOSE);
  348. break;
  349. default:
  350. break;
  351. }
  352. }
  353. argc -= optind;
  354. argv += optind;
  355. openchannel (&amp;channel);
  356. if (!argc)
  357. {
  358. ReadKey1 (&amp;channel, newline, key);
  359. if (_anyset (flags, INT6KID_NEWLINE))
  360. {
  361. putc (newline, stdout);
  362. }
  363. }
  364. while ((argc) &amp;&amp; (* argv))
  365. {
  366. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  367. {
  368. error (1, errno, PLC_BAD_MAC, * argv);
  369. }
  370. ReadKey1 (&amp;channel, newline, key);
  371. if (_anyset (flags, INT6KID_NEWLINE))
  372. {
  373. putc (newline, stdout);
  374. }
  375. argv++;
  376. argc--;
  377. }
  378. closechannel (&amp;channel);
  379. return (0);
  380. }
  381. </pre>
  382. <div class='footerlink'>
  383. [<a href='int6khost.c.html' title=' int6khost.c '>PREV</a>]
  384. [<a href='toolkit.html' title=' Index '>HOME</a>]
  385. [<a href='int6klist.c.html' title=' int6klist.c '>NEXT</a>]
  386. </div>
  387. </body>
  388. </html>