edru.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. * edru.c - Qualcomm Atheros Ethernet II Data Read Utility
  44. *
  45. * Listen for incoming Ethernet II frames and write frame data to
  46. * stdout as a continuous binary data stream. Since frame data can
  47. * contain anything, users should send program output to a file or
  48. * pipe it through a filter program to prevent corruption of the
  49. * terminal session. If all incoming data is ASCII then directing
  50. * stdout to the console should not cause any problems.
  51. *
  52. * this program can be used as a data target when testing AR6405
  53. * UART applications; use program edsu to send files from a remote
  54. * host and this program to display or save them;
  55. *
  56. *
  57. * Contributor(s):
  58. * Charles Maier
  59. *
  60. *--------------------------------------------------------------------*/
  61. /*====================================================================*
  62. * system header files;
  63. *--------------------------------------------------------------------*/
  64. #include <fcntl.h>
  65. #include <stdio.h>
  66. #include <limits.h>
  67. #include <ctype.h>
  68. #include <unistd.h>
  69. #include <stdint.h>
  70. #include <stdlib.h>
  71. #include <string.h>
  72. #include <errno.h>
  73. #ifdef __linux__
  74. #include <net/if.h>
  75. #include <net/if_arp.h>
  76. #include <netpacket/packet.h>
  77. #include <signal.h>
  78. #endif
  79. /*====================================================================*
  80. * custom header files;
  81. *--------------------------------------------------------------------*/
  82. #include "../tools/getoptv.h"
  83. #include "../tools/putoptv.h"
  84. #include "../tools/memory.h"
  85. #include "../tools/number.h"
  86. #include "../tools/types.h"
  87. #include "../tools/flags.h"
  88. #include "../tools/error.h"
  89. #include "../ether/channel.h"
  90. /*====================================================================*
  91. * custom source files;
  92. *--------------------------------------------------------------------*/
  93. #ifndef MAKEFILE
  94. #include "../tools/getoptv.c"
  95. #include "../tools/putoptv.c"
  96. #include "../tools/version.c"
  97. #include "../tools/hexdump.c"
  98. #include "../tools/hexdecode.c"
  99. #include "../tools/hexstring.c"
  100. #include "../tools/basespec.c"
  101. #include "../tools/uintspec.c"
  102. #include "../tools/todigit.c"
  103. #include "../tools/error.c"
  104. #endif
  105. #ifndef MAKEFILE
  106. #include "../ether/channel.c"
  107. #include "../ether/openchannel.c"
  108. #include "../ether/closechannel.c"
  109. #include "../ether/sendpacket.c"
  110. #include "../ether/readpacket.c"
  111. #endif
  112. /*====================================================================*
  113. * program constants;
  114. *--------------------------------------------------------------------*/
  115. #define PLCDEVICE "PLC"
  116. #define EDRU_ETHERTYPE ETH_P_802_2
  117. /*====================================================================*
  118. *
  119. * int main (int argc, char * argv[]);
  120. *
  121. *
  122. *--------------------------------------------------------------------*/
  123. int main (int argc, char const * argv [])
  124. {
  125. extern struct channel channel;
  126. struct ethernet_frame frame;
  127. signed length;
  128. static char const * optv [] =
  129. {
  130. "e:i:qt:v",
  131. PUTOPTV_S_DIVINE,
  132. "Qualcomm Atheros Ethernet II Data Read Utility",
  133. "e x\tethertype is (x) [" LITERAL (EDRU_ETHERTYPE) "]",
  134. #if defined (WINPCAP) || defined (LIBPCAP)
  135. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  136. #else
  137. "i s\thost interface is (s) [" 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. signed c;
  145. channel.type = EDRU_ETHERTYPE;
  146. channel.timeout = CHANNEL_FOREVER;
  147. if (getenv (PLCDEVICE))
  148. {
  149. #if defined (WINPCAP) || defined (LIBPCAP)
  150. channel.ifindex = atoi (getenv (PLCDEVICE));
  151. #else
  152. channel.ifname = strdup (getenv (PLCDEVICE));
  153. #endif
  154. }
  155. optind = 1;
  156. while ((c = getoptv (argc, argv, optv)) != -1)
  157. {
  158. switch (c)
  159. {
  160. case 'e':
  161. channel.type = (uint16_t)(basespec (optarg, 16, sizeof (channel.type)));
  162. break;
  163. case 'i':
  164. #if defined (WINPCAP) || defined (LIBPCAP)
  165. channel.ifindex = atoi (optarg);
  166. #else
  167. channel.ifname = optarg;
  168. #endif
  169. break;
  170. case 'q':
  171. _setbits (channel.flags, CHANNEL_SILENCE);
  172. break;
  173. case 't':
  174. channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
  175. break;
  176. case 'v':
  177. _setbits (channel.flags, CHANNEL_VERBOSE);
  178. break;
  179. default:
  180. break;
  181. }
  182. }
  183. argc -= optind;
  184. argv += optind;
  185. if (argc)
  186. {
  187. error (1, ECANCELED, ERROR_TOOMANY);
  188. }
  189. openchannel (&channel);
  190. while ((length = readpacket (&channel, &frame, sizeof (frame))) > 0)
  191. {
  192. write (STDOUT_FILENO, frame.frame_data, ntohs (frame.frame_type));
  193. }
  194. closechannel (&channel);
  195. return (0);
  196. }