efru.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * efru.c - Ethernet Frame Read Utility
  11. *
  12. * print message header and/or full message content on stdout for
  13. * each HomePlugAV or Atheros Vendor Specific message received by
  14. * the host;
  15. *
  16. * this program receives raw ethernet frames and so requires root
  17. * privileges; if you install it using "chmod 555" and "chown
  18. * root:root" then you must login as root to run it; otherwise, you
  19. * can install it using "chmod 4555" and "chown root:root" so that
  20. * anyone can run it; the program will refuse to run until you get
  21. * thing right;
  22. *
  23. * Contributor(s):
  24. * Charles Maier <cmaier@qca.qualcomm.com>
  25. *
  26. *--------------------------------------------------------------------*/
  27. /*====================================================================*
  28. * system header files;
  29. *--------------------------------------------------------------------*/
  30. #include <fcntl.h>
  31. #include <stdio.h>
  32. #include <limits.h>
  33. #include <ctype.h>
  34. #include <unistd.h>
  35. #include <stdint.h>
  36. #include <stdlib.h>
  37. #include <string.h>
  38. #include <errno.h>
  39. #ifdef __linux__
  40. #include <net/if.h>
  41. #include <net/if_arp.h>
  42. #include <netpacket/packet.h>
  43. #include <signal.h>
  44. #endif
  45. /*====================================================================*
  46. * custom header files;
  47. *--------------------------------------------------------------------*/
  48. #include "../tools/getoptv.h"
  49. #include "../tools/putoptv.h"
  50. #include "../tools/memory.h"
  51. #include "../tools/number.h"
  52. #include "../tools/types.h"
  53. #include "../tools/flags.h"
  54. #include "../tools/error.h"
  55. #include "../ether/channel.h"
  56. #include "../plc/plc.h"
  57. #include "../mme/mme.h"
  58. /*====================================================================*
  59. * custom source files;
  60. *--------------------------------------------------------------------*/
  61. #ifndef MAKEFILE
  62. #include "../tools/getoptv.c"
  63. #include "../tools/putoptv.c"
  64. #include "../tools/version.c"
  65. #include "../tools/hexdump.c"
  66. #include "../tools/basespec.c"
  67. #include "../tools/uintspec.c"
  68. #include "../tools/todigit.c"
  69. #include "../tools/error.c"
  70. #endif
  71. #ifndef MAKEFILE
  72. #include "../ether/channel.c"
  73. #include "../ether/openchannel.c"
  74. #include "../ether/closechannel.c"
  75. #include "../ether/sendpacket.c"
  76. #include "../ether/readpacket.c"
  77. #endif
  78. /*====================================================================*
  79. * program constants;
  80. *--------------------------------------------------------------------*/
  81. #define EFRU_VERBOSE (1 << 0)
  82. #define EFRU_SILENCE (1 << 1)
  83. #define EFRU_INTERFACE "PLC"
  84. #define EFRU_ETHERTYPE ETH_P_802_2
  85. /*====================================================================*
  86. *
  87. * int main (int argc, char * argv[]);
  88. *
  89. *
  90. *--------------------------------------------------------------------*/
  91. int main (int argc, char const * argv [])
  92. {
  93. extern struct channel channel;
  94. struct message message;
  95. static char const * optv [] =
  96. {
  97. "e:i:qt:v",
  98. PUTOPTV_S_DIVINE,
  99. "Qualcomm Atheros Ethernet Frame Read Utility",
  100. "e x\tethertype is (x) [" LITERAL (EFRU_ETHERTYPE) "]",
  101. #if defined (WINPCAP) || defined (LIBPCAP)
  102. "i n\thost interface is (s) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  103. #else
  104. "i s\thost interface is (n) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  105. #endif
  106. "q\tsuppress normal output",
  107. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_FOREVER) "]",
  108. "v\tverbose messages on stdout",
  109. (char const *) (0)
  110. };
  111. flag_t flags = (flag_t) (0);
  112. signed length;
  113. signed c;
  114. if (getenv (EFRU_INTERFACE))
  115. {
  116. #if defined (WINPCAP) || defined (LIBPCAP)
  117. channel.ifindex = atoi (getenv (EFRU_INTERFACE));
  118. #else
  119. channel.ifname = strdup (getenv (EFRU_INTERFACE));
  120. #endif
  121. }
  122. optind = 1;
  123. channel.type = EFRU_ETHERTYPE;
  124. channel.timeout = CHANNEL_FOREVER;
  125. while (~ (c = getoptv (argc, argv, optv)))
  126. {
  127. switch (c)
  128. {
  129. case 'e':
  130. channel.type = (uint16_t) (basespec (optarg, 16, sizeof (channel.type)));
  131. break;
  132. case 'i':
  133. #if defined (WIN32)
  134. channel.ifindex = atoi (optarg);
  135. #else
  136. channel.ifname = optarg;
  137. #endif
  138. break;
  139. case 'q':
  140. _setbits (flags, EFRU_SILENCE);
  141. break;
  142. case 't':
  143. channel.timeout = (signed) (uintspec (optarg, 0, UINT_MAX));
  144. break;
  145. case 'v':
  146. _setbits (flags, EFRU_VERBOSE);
  147. break;
  148. default:
  149. break;
  150. }
  151. }
  152. argc -= optind;
  153. argv += optind;
  154. if (argc)
  155. {
  156. error (1, ECANCELED, ERROR_TOOMANY);
  157. }
  158. openchannel (& channel);
  159. while ((length = readpacket (& channel, & message, sizeof (message))) >= 0)
  160. {
  161. hexdump (& message, 0, length, stdout);
  162. }
  163. closechannel (& channel);
  164. return (0);
  165. }