plclist.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. * plclist.c -
  44. *
  45. *
  46. *--------------------------------------------------------------------*/
  47. /*====================================================================*"
  48. * system header files;
  49. *--------------------------------------------------------------------*/
  50. #include <unistd.h>
  51. #include <stdlib.h>
  52. #include <stdint.h>
  53. #include <limits.h>
  54. /*====================================================================*
  55. * custom header files;
  56. *--------------------------------------------------------------------*/
  57. #include "../tools/getoptv.h"
  58. #include "../tools/putoptv.h"
  59. #include "../tools/symbol.h"
  60. #include "../tools/types.h"
  61. #include "../tools/flags.h"
  62. #include "../tools/files.h"
  63. #include "../tools/error.h"
  64. #include "../plc/plc.h"
  65. /*====================================================================*
  66. * custom source files;
  67. *--------------------------------------------------------------------*/
  68. #ifndef MAKEFILE
  69. #include "../plc/Confirm.c"
  70. #include "../plc/Display.c"
  71. #include "../plc/Failure.c"
  72. #include "../plc/Request.c"
  73. #include "../plc/ReadMME.c"
  74. #include "../plc/SendMME.c"
  75. #include "../plc/Devices.c"
  76. #include "../plc/ListRemoteDevices.c"
  77. #include "../plc/ListRemoteDevices1.c"
  78. #include "../plc/ListRemoteDevices2.c"
  79. #include "../plc/ListLocalDevices.c"
  80. #endif
  81. #ifndef MAKEFILE
  82. #include "../tools/error.c"
  83. #include "../tools/getoptv.c"
  84. #include "../tools/putoptv.c"
  85. #include "../tools/version.c"
  86. #include "../tools/uintspec.c"
  87. #include "../tools/hexdump.c"
  88. #include "../tools/hexencode.c"
  89. #include "../tools/hexdecode.c"
  90. #include "../tools/hexout.c"
  91. #include "../tools/todigit.c"
  92. #include "../tools/synonym.c"
  93. #endif
  94. #ifndef MAKEFILE
  95. #include "../ether/openchannel.c"
  96. #include "../ether/closechannel.c"
  97. #include "../ether/readpacket.c"
  98. #include "../ether/sendpacket.c"
  99. #include "../ether/channel.c"
  100. #endif
  101. #ifndef MAKEFILE
  102. #include "../mme/EthernetHeader.c"
  103. #include "../mme/QualcommHeader.c"
  104. #include "../mme/QualcommHeader1.c"
  105. #include "../mme/UnwantedMessage.c"
  106. #include "../mme/MMECode.c"
  107. #endif
  108. /*====================================================================*
  109. *
  110. * int main (int argc, char const * argv[]);
  111. *
  112. *
  113. * Contributor(s):
  114. * Charles Maier
  115. *
  116. *--------------------------------------------------------------------*/
  117. int main (int argc, char const * argv [])
  118. {
  119. extern struct channel channel;
  120. extern const struct _term_ devices [];
  121. static char const * optv [] =
  122. {
  123. "i:bnqrsv",
  124. "device [device] [...] [> stdout]",
  125. "Qualcomm Atheros Powerline Device Enumerator",
  126. "b\tprint bridge devices; exclude remote devices",
  127. #if defined (WINPCAP) || defined (LIBPCAP)
  128. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  129. #else
  130. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  131. #endif
  132. "n\tappend newline to output",
  133. "q\tquiet mode",
  134. "r\tprint remote devices; exclude bridge devices",
  135. "s\tprint newlines instead of spaces",
  136. "v\tverbose mode",
  137. (char const *) (0)
  138. };
  139. #include "../plc/plc.c"
  140. char const * space = " ";
  141. char const * comma = "\0";
  142. signed c;
  143. if (getenv (PLCDEVICE))
  144. {
  145. #if defined (WINPCAP) || defined (LIBPCAP)
  146. channel.ifindex = atoi (getenv (PLCDEVICE));
  147. #else
  148. channel.ifname = strdup (getenv (PLCDEVICE));
  149. #endif
  150. }
  151. optind = 1;
  152. while ((c = getoptv (argc, argv, optv)) != -1)
  153. {
  154. switch (c)
  155. {
  156. case 'b':
  157. _setbits (plc.flags, PLC_BRIDGE_LIST);
  158. break;
  159. case 'i':
  160. #if defined (WINPCAP) || defined (LIBPCAP)
  161. channel.ifindex = atoi (optarg);
  162. #else
  163. channel.ifname = optarg;
  164. #endif
  165. break;
  166. case 'n':
  167. comma = "\n";
  168. break;
  169. case 'r':
  170. _setbits (plc.flags, PLC_REMOTE_LIST);
  171. break;
  172. case 's':
  173. space = "\n";
  174. break;
  175. case 'q':
  176. _setbits (channel.flags, CHANNEL_SILENCE);
  177. _setbits (plc.flags, PLC_SILENCE);
  178. break;
  179. case 'v':
  180. _setbits (channel.flags, CHANNEL_VERBOSE);
  181. _setbits (plc.flags, PLC_VERBOSE);
  182. break;
  183. default:
  184. break;
  185. }
  186. }
  187. argc -= optind;
  188. argv += optind;
  189. if (_allclr (plc.flags, (PLC_BRIDGE_LIST|PLC_REMOTE_LIST)))
  190. {
  191. _setbits (plc.flags, (PLC_BRIDGE_LIST|PLC_REMOTE_LIST));
  192. }
  193. openchannel (&channel);
  194. if (!(plc.message = malloc (sizeof (* plc.message))))
  195. {
  196. error (1, errno, PLC_NOMEMORY);
  197. }
  198. if (!argc)
  199. {
  200. ListLocalDevices (&plc, space, comma);
  201. }
  202. while ((argc) && (* argv))
  203. {
  204. if (!hexencode (channel.peer, sizeof (channel.peer), synonym (* argv, devices, SIZEOF (devices))))
  205. {
  206. error (1, errno, PLC_BAD_MAC, * argv);
  207. }
  208. ListRemoteDevices (&plc, space, comma);
  209. argc--;
  210. argv++;
  211. }
  212. closechannel (&channel);
  213. return (0);
  214. }