efsu.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * efsu.c - Ethernet Frame Send Utility;
  11. *
  12. * convert hexadecimal text files to ethernet frames and transmit
  13. * them over the network; basically, it is a 'send your own frame'
  14. * utility;
  15. *
  16. * the program works like cat, sending file after file to a given
  17. * interface; as each file is read, all hexadecimal octets in the
  18. * file are converted to bytes and buffered; a semicolon causes a
  19. * buffer transmit as does the end of file; script-style comments
  20. * starting with hash (#) and c-language-style comments starting
  21. * with slash-slash or slash-asterisk are consumed and discard as
  22. * the file is read; the errors that can occur are non-hex digits
  23. * and odd number of hex digits;
  24. *
  25. * Contributor(s):
  26. * Charles Maier <cmaier@qca.qualcomm.com>
  27. *
  28. *--------------------------------------------------------------------*/
  29. /*====================================================================*
  30. * system header files;
  31. *--------------------------------------------------------------------*/
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <limits.h>
  35. /*====================================================================*
  36. * custom header files;
  37. *--------------------------------------------------------------------*/
  38. #include "../tools/getoptv.h"
  39. #include "../tools/putoptv.h"
  40. #include "../tools/error.h"
  41. #include "../tools/memory.h"
  42. #include "../tools/number.h"
  43. #include "../tools/symbol.h"
  44. #include "../tools/flags.h"
  45. #include "../ether/channel.h"
  46. /*====================================================================*
  47. * custom source files;
  48. *--------------------------------------------------------------------*/
  49. #ifndef MAKEFILE
  50. #include "../tools/getoptv.c"
  51. #include "../tools/putoptv.c"
  52. #include "../tools/version.c"
  53. #include "../tools/error.c"
  54. #include "../tools/hexencode.c"
  55. #include "../tools/hexload.c"
  56. #include "../tools/hexdump.c"
  57. #include "../tools/todigit.c"
  58. #include "../tools/uintspec.c"
  59. #include "../tools/basespec.c"
  60. #include "../tools/synonym.c"
  61. #endif
  62. #ifndef MAKEFILE
  63. #include "../ether/openchannel.c"
  64. #include "../ether/closechannel.c"
  65. #include "../ether/readpacket.c"
  66. #include "../ether/sendpacket.c"
  67. #include "../ether/channel.c"
  68. #endif
  69. /*====================================================================*
  70. * program contants;
  71. *--------------------------------------------------------------------*/
  72. #define EFSU_INTERFACE "PLC"
  73. #define EFSU_ETHERTYPE 0x88E1
  74. #define EFSU_PAUSE 0
  75. #define EFSU_DELAY 0
  76. #define EFSU_LOOP 1
  77. #ifndef ETHER_CRC_LEN
  78. #define ETHER_CRC_LEN 4
  79. #endif
  80. /*====================================================================*
  81. * program variables;
  82. *--------------------------------------------------------------------*/
  83. static const struct _term_ protocols [] =
  84. {
  85. {
  86. "hp10",
  87. "887B"
  88. },
  89. {
  90. "hpav",
  91. "88E1"
  92. }
  93. };
  94. /*====================================================================*
  95. *
  96. * void function (struct channel * channel, void * memory, ssize_t extent);
  97. *
  98. * read Ethernet frame descriptions from stdin and transmit them
  99. * as raw ethernet frames; wait for a response if CHANNEL_LISTEN
  100. * flagword bit is set;
  101. *
  102. *--------------------------------------------------------------------*/
  103. static void function (struct channel * channel, void * memory, ssize_t extent)
  104. {
  105. struct ether_header * frame = (struct ether_header *) (memory);
  106. unsigned length;
  107. while ((length = (unsigned) (hexload (memory, extent, stdin))) > 0)
  108. {
  109. if (length < (ETHER_MIN_LEN - ETHER_CRC_LEN))
  110. {
  111. error (1, ENOTSUP, "Frame size of %d is less than %d bytes", length, (ETHER_MIN_LEN - ETHER_CRC_LEN));
  112. }
  113. if (length > (ETHER_MAX_LEN - ETHER_CRC_LEN))
  114. {
  115. error (1, ENOTSUP, "Frame size of %d is more than %d bytes", length, (ETHER_MAX_LEN - ETHER_CRC_LEN));
  116. }
  117. if (_anyset (channel->flags, CHANNEL_UPDATE_TARGET))
  118. {
  119. memcpy (frame->ether_dhost, channel->peer, sizeof (frame->ether_dhost));
  120. }
  121. if (_anyset (channel->flags, CHANNEL_UPDATE_SOURCE))
  122. {
  123. memcpy (frame->ether_shost, channel->host, sizeof (frame->ether_shost));
  124. }
  125. sendpacket (channel, memory, length);
  126. if (_anyset (channel->flags, CHANNEL_LISTEN))
  127. {
  128. while (readpacket (channel, memory, extent) > 0);
  129. }
  130. }
  131. return;
  132. }
  133. /*====================================================================*
  134. *
  135. * void iterate (int argc, char const * argv [], void * memory, ssize_t extent, unsigned pause);
  136. *
  137. *--------------------------------------------------------------------*/
  138. static void iterate (int argc, char const * argv [], struct channel * channel, unsigned pause)
  139. {
  140. byte buffer [ETHER_MAX_LEN];
  141. if (! argc)
  142. {
  143. function (channel, buffer, sizeof (buffer));
  144. }
  145. while ((argc) && (* argv))
  146. {
  147. if (! freopen (* argv, "rb", stdin))
  148. {
  149. error (1, errno, "Can't open %s", * argv);
  150. }
  151. function (channel, buffer, sizeof (buffer));
  152. argc--;
  153. argv++;
  154. if ((argc) && (* argv))
  155. {
  156. sleep (pause);
  157. }
  158. }
  159. return;
  160. }
  161. /*====================================================================*
  162. *
  163. * int main (int argc, char const * argv []);
  164. *
  165. *--------------------------------------------------------------------*/
  166. int main (int argc, char const * argv [])
  167. {
  168. extern struct channel channel;
  169. static char const * optv [] =
  170. {
  171. "d:e:hi:l:p:t:vw:",
  172. PUTOPTV_S_FUNNEL,
  173. "Ethernet Frame Send Utility",
  174. "d x\treplace destination address with (x)",
  175. "e x\techo return frames having ethertype (x) [" LITERAL (EFSU_ETHERTYPE) "]",
  176. "h\treplace source address with host address",
  177. #if defined (WINPCAP) || defined (LIBPCAP)
  178. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  179. #else
  180. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  181. #endif
  182. "l n\trepeat file sequence (n) times [" LITERAL (EFSU_LOOP) "]",
  183. "p n\twait (n) seconds between files [" LITERAL (EFSU_PAUSE) "]",
  184. "t n\tread timeout is (n) milliseconds [" LITERAL (CHANNEL_TIMEOUT) "]",
  185. "v\tverbose messages",
  186. "w n\twait (n) seconds between loops [" LITERAL (EFSU_DELAY) "]",
  187. (char const *) (0)
  188. };
  189. unsigned pause = EFSU_PAUSE;
  190. unsigned delay = EFSU_DELAY;
  191. unsigned loop = EFSU_LOOP;
  192. signed c;
  193. channel.type = EFSU_ETHERTYPE;
  194. if (getenv (EFSU_INTERFACE))
  195. {
  196. #if defined (WINPCAP) || defined (LIBPCAP)
  197. channel.ifindex = atoi (getenv (EFSU_INTERFACE));
  198. #else
  199. channel.ifname = strdup (getenv (EFSU_INTERFACE));
  200. #endif
  201. }
  202. optind = 1;
  203. while (~ (c = getoptv (argc, argv, optv)))
  204. {
  205. switch (c)
  206. {
  207. case 'd':
  208. _setbits (channel.flags, CHANNEL_UPDATE_TARGET);
  209. if (! hexencode (channel.peer, sizeof (channel.peer), optarg))
  210. {
  211. error (1, errno, "%s", optarg);
  212. }
  213. break;
  214. case 'e':
  215. _setbits (channel.flags, CHANNEL_LISTEN);
  216. channel.type = (uint16_t) (basespec (synonym (optarg, protocols, SIZEOF (protocols)), 16, sizeof (channel.type)));
  217. break;
  218. case 'h':
  219. _setbits (channel.flags, CHANNEL_UPDATE_SOURCE);
  220. break;
  221. case 'i':
  222. #if defined (WINPCAP) || defined (LIBPCAP)
  223. channel.ifindex = atoi (optarg);
  224. #else
  225. channel.ifname = optarg;
  226. #endif
  227. break;
  228. case 'l':
  229. loop = (unsigned) (uintspec (optarg, 0, UINT_MAX));
  230. break;
  231. case 'p':
  232. pause = (unsigned) (uintspec (optarg, 0, 1200));
  233. break;
  234. case 'q':
  235. _setbits (channel.flags, CHANNEL_SILENCE);
  236. break;
  237. case 't':
  238. channel.timeout = (signed) (uintspec (optarg, 0, UINT_MAX));
  239. break;
  240. case 'v':
  241. _setbits (channel.flags, CHANNEL_VERBOSE);
  242. break;
  243. case 'w':
  244. delay = (unsigned) (uintspec (optarg, 0, 1200));
  245. break;
  246. default:
  247. break;
  248. }
  249. }
  250. argc -= optind;
  251. argv += optind;
  252. openchannel (& channel);
  253. while (loop--)
  254. {
  255. iterate (argc, argv, & channel, pause);
  256. if (loop)
  257. {
  258. sleep (delay);
  259. }
  260. }
  261. closechannel (& channel);
  262. return (0);
  263. }