efeu.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * edeu.c - Ethernet Frame Echo Utility
  11. *
  12. * Listen for incoming Ethernet II frames and write frame data to
  13. * stdout as a continuous binary data stream. Since frame data can
  14. * contain anything, users should send program output to a file or
  15. * pipe it through a filter program to prevent corruption of the
  16. * terminal session. If all incoming data is ASCII then directing
  17. * stdout to the console should not cause any problems.
  18. *
  19. * this program can be used as a data target when testing AR6405
  20. * UART applications; use program edsu to send files from a remote
  21. * host and this program to display or save them;
  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. /*====================================================================*
  57. * custom source files;
  58. *--------------------------------------------------------------------*/
  59. #ifndef MAKEFILE
  60. #include "../tools/getoptv.c"
  61. #include "../tools/putoptv.c"
  62. #include "../tools/version.c"
  63. #include "../tools/hexdump.c"
  64. #include "../tools/hexdecode.c"
  65. #include "../tools/hexstring.c"
  66. #include "../tools/uintspec.c"
  67. #include "../tools/memswap.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 EFEU_INTERFACE "PLC"
  82. #define EFEU_ETHERTYPE ETH_P_802_2
  83. /*====================================================================*
  84. *
  85. * int main (int argc, char * argv[]);
  86. *
  87. *
  88. *--------------------------------------------------------------------*/
  89. int main (int argc, char const * argv [])
  90. {
  91. extern struct channel channel;
  92. struct ethernet_frame frame;
  93. signed length;
  94. static char const * optv [] =
  95. {
  96. "e:i:qt:v",
  97. PUTOPTV_S_DIVINE,
  98. "Qualcomm Atheros Ethernet Frame Echo Utility",
  99. "e x\tethertype is (x) [" LITERAL (EFEU_ETHERTYPE) "]",
  100. #if defined (WINPCAP) || defined (LIBPCAP)
  101. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  102. #else
  103. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  104. #endif
  105. "q\tsuppress normal output",
  106. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_FOREVER) "]",
  107. "v\tverbose messages on stdout",
  108. (char const *) (0)
  109. };
  110. signed c;
  111. channel.type = EFEU_ETHERTYPE;
  112. channel.timeout = CHANNEL_FOREVER;
  113. if (getenv (EFEU_INTERFACE))
  114. {
  115. #if defined (WINPCAP) || defined (LIBPCAP)
  116. channel.ifindex = atoi (getenv (EFEU_INTERFACE));
  117. #else
  118. channel.ifname = strdup (getenv (EFEU_INTERFACE));
  119. #endif
  120. }
  121. optind = 1;
  122. while (~ (c = getoptv (argc, argv, optv)))
  123. {
  124. switch (c)
  125. {
  126. case 'e':
  127. channel.type = (uint16_t) (basespec (optarg, 16, sizeof (channel.type)));
  128. break;
  129. case 'i':
  130. #if defined (WINPCAP) || defined (LIBPCAP)
  131. channel.ifindex = atoi (optarg);
  132. #else
  133. channel.ifname = optarg;
  134. #endif
  135. break;
  136. case 'q':
  137. _setbits (channel.flags, CHANNEL_SILENCE);
  138. break;
  139. case 't':
  140. channel.timeout = (signed) (uintspec (optarg, 0, UINT_MAX));
  141. break;
  142. case 'v':
  143. _setbits (channel.flags, CHANNEL_VERBOSE);
  144. break;
  145. default:
  146. break;
  147. }
  148. }
  149. argc -= optind;
  150. argv += optind;
  151. if (argc)
  152. {
  153. error (1, ECANCELED, ERROR_TOOMANY);
  154. }
  155. openchannel (& channel);
  156. while ((length = readpacket (& channel, & frame, sizeof (frame))) > 0)
  157. {
  158. memswap (& frame.frame_dhost, & frame.frame_shost, sizeof (frame.frame_dhost));
  159. if (sendpacket (& channel, & frame, length) != length)
  160. {
  161. error (1, errno, CHANNEL_CANTSEND);
  162. }
  163. }
  164. closechannel (& channel);
  165. return (0);
  166. }