int6kid.c 10.0 KB

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