efru.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. * efru.c - Ethernet Frame Read Utility
  44. *
  45. * print message header and/or full message content on stdout for
  46. * each HomePlugAV or Atheros Vendor Specific message received by
  47. * the host;
  48. *
  49. * this program receives raw ethernet frames and so requires root
  50. * privileges; if you install it using "chmod 555" and "chown
  51. * root:root" then you must login as root to run it; otherwise, you
  52. * can install it using "chmod 4555" and "chown root:root" so that
  53. * anyone can run it; the program will refuse to run until you get
  54. * thing right;
  55. *
  56. * Contributor(s):
  57. * Charles Maier
  58. *
  59. *--------------------------------------------------------------------*/
  60. /*====================================================================*
  61. * system header files;
  62. *--------------------------------------------------------------------*/
  63. #include <fcntl.h>
  64. #include <stdio.h>
  65. #include <limits.h>
  66. #include <ctype.h>
  67. #include <unistd.h>
  68. #include <stdint.h>
  69. #include <stdlib.h>
  70. #include <string.h>
  71. #include <errno.h>
  72. #ifdef __linux__
  73. #include <net/if.h>
  74. #include <net/if_arp.h>
  75. #include <netpacket/packet.h>
  76. #include <signal.h>
  77. #endif
  78. /*====================================================================*
  79. * custom header files;
  80. *--------------------------------------------------------------------*/
  81. #include "../tools/getoptv.h"
  82. #include "../tools/putoptv.h"
  83. #include "../tools/memory.h"
  84. #include "../tools/number.h"
  85. #include "../tools/types.h"
  86. #include "../tools/flags.h"
  87. #include "../tools/error.h"
  88. #include "../ether/channel.h"
  89. #include "../plc/plc.h"
  90. #include "../mme/mme.h"
  91. /*====================================================================*
  92. * custom source files;
  93. *--------------------------------------------------------------------*/
  94. #ifndef MAKEFILE
  95. #include "../tools/getoptv.c"
  96. #include "../tools/putoptv.c"
  97. #include "../tools/version.c"
  98. #include "../tools/hexdump.c"
  99. #include "../tools/basespec.c"
  100. #include "../tools/uintspec.c"
  101. #include "../tools/todigit.c"
  102. #include "../tools/error.c"
  103. #endif
  104. #ifndef MAKEFILE
  105. #include "../ether/channel.c"
  106. #include "../ether/openchannel.c"
  107. #include "../ether/closechannel.c"
  108. #include "../ether/sendpacket.c"
  109. #include "../ether/readpacket.c"
  110. #endif
  111. /*====================================================================*
  112. * program constants;
  113. *--------------------------------------------------------------------*/
  114. #define EFRU_VERBOSE (1 << 0)
  115. #define EFRU_SILENCE (1 << 1)
  116. #define EFRU_INTERFACE "PLC"
  117. #define EFRU_ETHERTYPE ETH_P_802_2
  118. /*====================================================================*
  119. *
  120. * int main (int argc, char * argv[]);
  121. *
  122. *
  123. *--------------------------------------------------------------------*/
  124. int main (int argc, char const * argv [])
  125. {
  126. extern struct channel channel;
  127. struct message message;
  128. static char const * optv [] =
  129. {
  130. "e:i:qt:v",
  131. PUTOPTV_S_DIVINE,
  132. "Qualcomm Atheros Ethernet Frame Read Utility",
  133. "e x\tethertype is (x) [" LITERAL (EFRU_ETHERTYPE) "]",
  134. #if defined (WINPCAP) || defined (LIBPCAP)
  135. "i n\thost interface is (s) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  136. #else
  137. "i s\thost interface is (n) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  138. #endif
  139. "q\tsuppress normal output",
  140. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_FOREVER) "]",
  141. "v\tverbose messages on stdout",
  142. (char const *) (0)
  143. };
  144. flag_t flags = (flag_t)(0);
  145. signed length;
  146. signed c;
  147. if (getenv (EFRU_INTERFACE))
  148. {
  149. #if defined (WINPCAP) || defined (LIBPCAP)
  150. channel.ifindex = atoi (getenv (EFRU_INTERFACE));
  151. #else
  152. channel.ifname = strdup (getenv (EFRU_INTERFACE));
  153. #endif
  154. }
  155. optind = 1;
  156. channel.type = EFRU_ETHERTYPE;
  157. channel.timeout = CHANNEL_FOREVER;
  158. while ((c = getoptv (argc, argv, optv)) != -1)
  159. {
  160. switch (c)
  161. {
  162. case 'e':
  163. channel.type = (uint16_t)(basespec (optarg, 16, sizeof (channel.type)));
  164. break;
  165. case 'i':
  166. #if defined (WIN32)
  167. channel.ifindex = atoi (optarg);
  168. #else
  169. channel.ifname = optarg;
  170. #endif
  171. break;
  172. case 'q':
  173. _setbits (flags, EFRU_SILENCE);
  174. break;
  175. case 't':
  176. channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
  177. break;
  178. case 'v':
  179. _setbits (flags, EFRU_VERBOSE);
  180. break;
  181. default:
  182. break;
  183. }
  184. }
  185. argc -= optind;
  186. argv += optind;
  187. if (argc)
  188. {
  189. error (1, ECANCELED, ERROR_TOOMANY);
  190. }
  191. openchannel (&channel);
  192. while ((length = readpacket (&channel, &message, sizeof (message))) >= 0)
  193. {
  194. hexdump (&message, 0, length, stdout);
  195. }
  196. closechannel (&channel);
  197. return (0);
  198. }