efsu.c.html 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?xml version='1.0' encoding='iso-8859-1'?>
  2. <!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  3. <html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
  4. <head>
  5. <title>
  6. efsu.c
  7. </title>
  8. <meta http-equiv='content-type' content='text/html;iso-8859-1'/>
  9. <meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
  10. <meta name='author' content='cmaier@cmassoc.net'/>
  11. <meta name='robots' content='noindex,nofollow'/>
  12. <link href='toolkit.css' rel='stylesheet' type='text/css'/>
  13. </head>
  14. <body>
  15. <div class='headerlink'>
  16. [<a href='efru.c.html' title=' efru.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='emalloc.c.html' title=' emalloc.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  24. *
  25. * All rights reserved.
  26. *
  27. * Redistribution and use in source and binary forms, with or
  28. * without modification, are permitted (subject to the limitations
  29. * in the disclaimer below) provided that the following conditions
  30. * are met:
  31. *
  32. * * Redistributions of source code must retain the above copyright
  33. * notice, this list of conditions and the following disclaimer.
  34. *
  35. * * Redistributions in binary form must reproduce the above
  36. * copyright notice, this list of conditions and the following
  37. * disclaimer in the documentation and/or other materials
  38. * provided with the distribution.
  39. *
  40. * * Neither the name of Qualcomm Atheros nor the names of
  41. * its contributors may be used to endorse or promote products
  42. * derived from this software without specific prior written
  43. * permission.
  44. *
  45. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  46. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  47. * COPYRIGHT HOLDERS AND CONTRIBUTORS &quot;AS IS&quot; AND ANY EXPRESS OR
  48. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  49. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  50. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  51. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  52. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  53. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  54. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  55. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  56. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  57. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  58. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  59. *
  60. *--------------------------------------------------------------------*/
  61. /*====================================================================*
  62. *
  63. * efsu.c - Ethernet Frame Send Utility;
  64. *
  65. * convert hexadecimal text files to ethernet frames and transmit
  66. * them over the network; basically, it is a 'send your own frame'
  67. * utility;
  68. *
  69. * the program works like cat, sending file after file to a given
  70. * interface; as each file is read, all hexadecimal octets in the
  71. * file are converted to bytes and buffered; a semicolon causes a
  72. * buffer transmit as does the end of file; script-style comments
  73. * starting with hash (#) and c-language-style comments starting
  74. * with slash-slash or slash-asterisk are consumed and discard as
  75. * the file is read; the errors that can occur are non-hex digits
  76. * and odd number of hex digits;
  77. *
  78. *
  79. *--------------------------------------------------------------------*/
  80. /*====================================================================*
  81. * system header files;
  82. *--------------------------------------------------------------------*/
  83. #include &lt;stdlib.h&gt;
  84. #include &lt;string.h&gt;
  85. #include &lt;limits.h&gt;
  86. /*====================================================================*
  87. * custom header files;
  88. *--------------------------------------------------------------------*/
  89. #include &quot;../tools/getoptv.h&quot;
  90. #include &quot;../tools/putoptv.h&quot;
  91. #include &quot;../tools/error.h&quot;
  92. #include &quot;../tools/memory.h&quot;
  93. #include &quot;../tools/number.h&quot;
  94. #include &quot;../tools/symbol.h&quot;
  95. #include &quot;../tools/flags.h&quot;
  96. #include &quot;../ether/channel.h&quot;
  97. /*====================================================================*
  98. * custom source files;
  99. *--------------------------------------------------------------------*/
  100. #ifndef MAKEFILE
  101. #include &quot;../tools/getoptv.c&quot;
  102. #include &quot;../tools/putoptv.c&quot;
  103. #include &quot;../tools/version.c&quot;
  104. #include &quot;../tools/error.c&quot;
  105. #include &quot;../tools/hexencode.c&quot;
  106. #include &quot;../tools/hexload.c&quot;
  107. #include &quot;../tools/hexdump.c&quot;
  108. #include &quot;../tools/todigit.c&quot;
  109. #include &quot;../tools/uintspec.c&quot;
  110. #include &quot;../tools/basespec.c&quot;
  111. #include &quot;../tools/synonym.c&quot;
  112. #endif
  113. #ifndef MAKEFILE
  114. #include &quot;../ether/openchannel.c&quot;
  115. #include &quot;../ether/closechannel.c&quot;
  116. #include &quot;../ether/readpacket.c&quot;
  117. #include &quot;../ether/sendpacket.c&quot;
  118. #include &quot;../ether/channel.c&quot;
  119. #endif
  120. /*====================================================================*
  121. * program contants;
  122. *--------------------------------------------------------------------*/
  123. #define EFSU_INTERFACE &quot;PLC&quot;
  124. #define EFSU_ETHERTYPE 0x88E1
  125. #define EFSU_PAUSE 0
  126. #define EFSU_DELAY 0
  127. #define EFSU_LOOP 1
  128. #ifndef ETHER_CRC_LEN
  129. #define ETHER_CRC_LEN 4
  130. #endif
  131. /*====================================================================*
  132. * program variables;
  133. *--------------------------------------------------------------------*/
  134. static const struct _term_ protocols [] =
  135. {
  136. {
  137. &quot;hp10&quot;,
  138. &quot;887B&quot;
  139. },
  140. {
  141. &quot;hpav&quot;,
  142. &quot;88E1&quot;
  143. }
  144. };
  145. /*====================================================================*
  146. *
  147. * void function (struct channel * channel, void * memory, ssize_t extent);
  148. *
  149. * read Ethernet frame descriptions from stdin and transmit them
  150. * as raw ethernet frames; wait for a response if CHANNEL_LISTEN
  151. * flagword bit is set;
  152. *
  153. *
  154. *--------------------------------------------------------------------*/
  155. static void function (struct channel * channel, void * memory, ssize_t extent)
  156. {
  157. struct ether_header * frame = (struct ether_header *)(memory);
  158. unsigned length;
  159. while ((length = (unsigned)(hexload (memory, extent, stdin))) &gt; 0)
  160. {
  161. if (length &lt; (ETHER_MIN_LEN - ETHER_CRC_LEN))
  162. {
  163. error (1, ENOTSUP, &quot;Frame size of %d is less than %d bytes&quot;, length, (ETHER_MIN_LEN - ETHER_CRC_LEN));
  164. }
  165. if (length &gt; (ETHER_MAX_LEN - ETHER_CRC_LEN))
  166. {
  167. error (1, ENOTSUP, &quot;Frame size of %d is more than %d bytes&quot;, length, (ETHER_MAX_LEN - ETHER_CRC_LEN));
  168. }
  169. if (_anyset (channel-&gt;flags, CHANNEL_UPDATE_TARGET))
  170. {
  171. memcpy (frame-&gt;ether_dhost, channel-&gt;peer, sizeof (frame-&gt;ether_dhost));
  172. }
  173. if (_anyset (channel-&gt;flags, CHANNEL_UPDATE_SOURCE))
  174. {
  175. memcpy (frame-&gt;ether_shost, channel-&gt;host, sizeof (frame-&gt;ether_shost));
  176. }
  177. sendpacket (channel, memory, length);
  178. if (_anyset (channel-&gt;flags, CHANNEL_LISTEN))
  179. {
  180. while (readpacket (channel, memory, extent) &gt; 0);
  181. }
  182. }
  183. return;
  184. }
  185. /*====================================================================*
  186. *
  187. * void iterate (int argc, char const * argv [], void * memory, ssize_t extent, unsigned pause);
  188. *
  189. *
  190. *
  191. *--------------------------------------------------------------------*/
  192. static void iterate (int argc, char const * argv [], struct channel * channel, unsigned pause)
  193. {
  194. byte buffer [ETHER_MAX_LEN];
  195. if (!argc)
  196. {
  197. function (channel, buffer, sizeof (buffer));
  198. }
  199. while ((argc) &amp;&amp; (* argv))
  200. {
  201. if (!freopen (* argv, &quot;rb&quot;, stdin))
  202. {
  203. error (1, errno, &quot;Can't open %s&quot;, * argv);
  204. }
  205. function (channel, buffer, sizeof (buffer));
  206. argc--;
  207. argv++;
  208. if ((argc) &amp;&amp; (* argv))
  209. {
  210. sleep (pause);
  211. }
  212. }
  213. return;
  214. }
  215. /*====================================================================*
  216. *
  217. * int main (int argc, char const * argv []);
  218. *
  219. *
  220. *
  221. *--------------------------------------------------------------------*/
  222. int main (int argc, char const * argv [])
  223. {
  224. extern struct channel channel;
  225. static char const * optv [] =
  226. {
  227. &quot;d:e:hi:l:p:t:vw:&quot;,
  228. PUTOPTV_S_FUNNEL,
  229. &quot;Ethernet Frame Send Utility&quot;,
  230. &quot;d x\treplace destination address with (x)&quot;,
  231. &quot;e x\techo return frames having ethertype (x) [&quot; LITERAL (EFSU_ETHERTYPE) &quot;]&quot;,
  232. &quot;h\treplace source address with host address&quot;,
  233. #if defined (WINPCAP) || defined (LIBPCAP)
  234. &quot;i n\thost interface is (n) [&quot; LITERAL (CHANNEL_ETHNUMBER) &quot;]&quot;,
  235. #else
  236. &quot;i s\thost interface is (s) [&quot; LITERAL (CHANNEL_ETHDEVICE) &quot;]&quot;,
  237. #endif
  238. &quot;l n\trepeat file sequence (n) times [&quot; LITERAL (EFSU_LOOP) &quot;]&quot;,
  239. &quot;p n\twait (n) seconds between files [&quot; LITERAL (EFSU_PAUSE) &quot;]&quot;,
  240. &quot;t n\tread timeout is (n) milliseconds [&quot; LITERAL (CHANNEL_TIMEOUT) &quot;]&quot;,
  241. &quot;v\tverbose messages&quot;,
  242. &quot;w n\twait (n) seconds between loops [&quot; LITERAL (EFSU_DELAY) &quot;]&quot;,
  243. (char const *) (0)
  244. };
  245. unsigned pause = EFSU_PAUSE;
  246. unsigned delay = EFSU_DELAY;
  247. unsigned loop = EFSU_LOOP;
  248. signed c;
  249. channel.type = EFSU_ETHERTYPE;
  250. if (getenv (EFSU_INTERFACE))
  251. {
  252. #if defined (WINPCAP) || defined (LIBPCAP)
  253. channel.ifindex = atoi (getenv (EFSU_INTERFACE));
  254. #else
  255. channel.ifname = strdup (getenv (EFSU_INTERFACE));
  256. #endif
  257. }
  258. optind = 1;
  259. while ((c = getoptv (argc, argv, optv)) != -1)
  260. {
  261. switch (c)
  262. {
  263. case 'd':
  264. _setbits (channel.flags, CHANNEL_UPDATE_TARGET);
  265. if (!hexencode (channel.peer, sizeof (channel.peer), optarg))
  266. {
  267. error (1, errno, &quot;%s&quot;, optarg);
  268. }
  269. break;
  270. case 'e':
  271. _setbits (channel.flags, CHANNEL_LISTEN);
  272. channel.type = (uint16_t)(basespec (synonym (optarg, protocols, SIZEOF (protocols)), 16, sizeof (channel.type)));
  273. break;
  274. case 'h':
  275. _setbits (channel.flags, CHANNEL_UPDATE_SOURCE);
  276. break;
  277. case 'i':
  278. #if defined (WINPCAP) || defined (LIBPCAP)
  279. channel.ifindex = atoi (optarg);
  280. #else
  281. channel.ifname = optarg;
  282. #endif
  283. break;
  284. case 'l':
  285. loop = (unsigned)(uintspec (optarg, 0, UINT_MAX));
  286. break;
  287. case 'p':
  288. pause = (unsigned)(uintspec (optarg, 0, 1200));
  289. break;
  290. case 'q':
  291. _setbits (channel.flags, CHANNEL_SILENCE);
  292. break;
  293. case 't':
  294. channel.timeout = (signed)(uintspec (optarg, 0, UINT_MAX));
  295. break;
  296. case 'v':
  297. _setbits (channel.flags, CHANNEL_VERBOSE);
  298. break;
  299. case 'w':
  300. delay = (unsigned)(uintspec (optarg, 0, 1200));
  301. break;
  302. default:
  303. break;
  304. }
  305. }
  306. argc -= optind;
  307. argv += optind;
  308. openchannel (&amp;channel);
  309. while (loop--)
  310. {
  311. iterate (argc, argv, &amp;channel, pause);
  312. if (loop)
  313. {
  314. sleep (delay);
  315. }
  316. }
  317. closechannel (&amp;channel);
  318. return (0);
  319. }
  320. </pre>
  321. <div class='footerlink'>
  322. [<a href='efru.c.html' title=' efru.c '>PREV</a>]
  323. [<a href='toolkit.html' title=' Index '>HOME</a>]
  324. [<a href='emalloc.c.html' title=' emalloc.c '>NEXT</a>]
  325. </div>
  326. </body>
  327. </html>