edsu.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. * edsu.c - Qualcomm Atheros Ethernet II Data Send Utility
  44. *
  45. * send one or more files over Ethernet using IEEE 802.2 Ethernet
  46. * Frames;
  47. *
  48. * this program can be used as a data source when testing AR6405
  49. * UART applications; use this program to send files and program
  50. * edru to read and display or save them at the other end;
  51. *
  52. *
  53. * Contributor(s):
  54. * Charles Maier
  55. *
  56. *--------------------------------------------------------------------*/
  57. /*====================================================================*
  58. * system header files;
  59. *--------------------------------------------------------------------*/
  60. #include <stdlib.h>
  61. #include <limits.h>
  62. #include <memory.h>
  63. #include <signal.h>
  64. #include <unistd.h>
  65. /*====================================================================*
  66. * custom header files;
  67. *--------------------------------------------------------------------*/
  68. #include "../tools/getoptv.h"
  69. #include "../tools/putoptv.h"
  70. #include "../tools/memory.h"
  71. #include "../tools/number.h"
  72. #include "../tools/error.h"
  73. #include "../tools/files.h"
  74. #include "../tools/flags.h"
  75. #include "../ether/ether.h"
  76. #include "../ether/channel.h"
  77. /*====================================================================*
  78. * custom source files;
  79. *--------------------------------------------------------------------*/
  80. #ifndef MAKEFILE
  81. #include "../tools/getoptv.c"
  82. #include "../tools/putoptv.c"
  83. #include "../tools/version.c"
  84. #include "../tools/efreopen.c"
  85. #include "../tools/basespec.c"
  86. #include "../tools/uintspec.c"
  87. #include "../tools/todigit.c"
  88. #include "../tools/hexdump.c"
  89. #include "../tools/hexencode.c"
  90. #include "../tools/error.c"
  91. #endif
  92. #ifndef MAKEFILE
  93. #include "../ether/channel.c"
  94. #include "../ether/openchannel.c"
  95. #include "../ether/closechannel.c"
  96. #include "../ether/readpacket.c"
  97. #include "../ether/sendpacket.c"
  98. #endif
  99. /*====================================================================*
  100. * program constants;
  101. *--------------------------------------------------------------------*/
  102. #define EDSU_INTERFACE "PLC"
  103. #define EDSU_ETHERTYPE ETH_P_802_2
  104. #define EDSU_PAUSE 0
  105. /*====================================================================*
  106. *
  107. * signed function (struct channel * channel, unsigned pause, signed fd);
  108. *
  109. * read a file and transmit it over network as a stream of Ethernet
  110. * frames; pause between frames to prevent over-loading the remote
  111. * host;
  112. *
  113. *
  114. *--------------------------------------------------------------------*/
  115. signed function (struct channel * channel, unsigned pause, signed fd)
  116. {
  117. struct ethernet_frame frame;
  118. signed length = sizeof (frame.frame_data);
  119. memcpy (frame.frame_dhost, channel->peer, sizeof (frame.frame_dhost));
  120. memcpy (frame.frame_shost, channel->host, sizeof (frame.frame_shost));
  121. while ((length = read (fd, frame.frame_data, sizeof (frame.frame_data))) > 0)
  122. {
  123. frame.frame_type = htons (length);
  124. if (length < ETHERMIN)
  125. {
  126. length = ETHERMIN;
  127. }
  128. length += ETHER_HDR_LEN;
  129. if (sendpacket (channel, &frame, length) < 0)
  130. {
  131. error (1, errno, CHANNEL_CANTSEND);
  132. }
  133. sleep (pause);
  134. }
  135. return (0);
  136. }
  137. /*====================================================================*
  138. *
  139. * int main (int argc, char const * argv []);
  140. *
  141. *
  142. *
  143. *--------------------------------------------------------------------*/
  144. int main (int argc, char const * argv [])
  145. {
  146. extern struct channel channel;
  147. static char const * optv [] =
  148. {
  149. "e:d:i:p:qv",
  150. "file [file] [...]",
  151. "Qualcomm Atheros Ethernet II Data Send Utility",
  152. "e x\tethertype is (x) [" LITERAL (EDSU_ETHERTYPE) "]",
  153. "d x\tdestination address is (x) [00:B0:52:00:00:01]",
  154. #if defined (WINPCAP) || defined (LIBPCAP)
  155. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  156. #else
  157. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  158. #endif
  159. "p n\tpause (n) seconds between frames [" LITERAL (EDSU_PAUSE) "]",
  160. "q\tquiet mode",
  161. "v\tverbose mode",
  162. (char const *)(0)
  163. };
  164. unsigned pause = EDSU_PAUSE;
  165. signed c;
  166. if (getenv (EDSU_INTERFACE))
  167. {
  168. #if defined (WINPCAP) || defined (LIBPCAP)
  169. channel.ifindex = atoi (getenv (EDSU_INTERFACE));
  170. #else
  171. channel.ifname = strdup (getenv (EDSU_INTERFACE));
  172. #endif
  173. }
  174. channel.type = EDSU_ETHERTYPE;
  175. while ((c = getoptv (argc, argv, optv)) != -1)
  176. {
  177. switch (c)
  178. {
  179. case 'e':
  180. channel.type = (uint16_t)(basespec (optarg, 16, sizeof (channel.type)));
  181. break;
  182. case 'd':
  183. if (!hexencode (channel.peer, sizeof (channel.peer), optarg))
  184. {
  185. error (1, errno, "%s", optarg);
  186. }
  187. break;
  188. case 'i':
  189. #if defined (WINPCAP) || defined (LIBPCAP)
  190. channel.ifindex = atoi (optarg);
  191. #else
  192. channel.ifname = optarg;
  193. #endif
  194. break;
  195. case 'p':
  196. pause = (unsigned)(uintspec (optarg, 0, UCHAR_MAX));
  197. break;
  198. case 'q':
  199. _setbits (channel.flags, CHANNEL_SILENCE);
  200. break;
  201. case 'v':
  202. _setbits (channel.flags, CHANNEL_VERBOSE);
  203. break;
  204. default:
  205. break;
  206. }
  207. }
  208. argc -= optind;
  209. argv += optind;
  210. openchannel (&channel);
  211. if (!argc)
  212. {
  213. function (&channel, pause, STDIN_FILENO);
  214. }
  215. while ((argc) && (* argv))
  216. {
  217. if (efreopen (* argv, "rb", stdin))
  218. {
  219. function (&channel, pause, fileno (stdin));
  220. }
  221. argc--;
  222. argv++;
  223. }
  224. closechannel (&channel);
  225. exit (0);
  226. }