efbu.c.html 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. efbu.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='edsu.c.html' title=' edsu.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='efeu.c.html' title=' efeu.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. * efbu.c - Ethernet Frame Blast Utility;
  64. *
  65. * transmit an IP broadcast frame of maximum length at maximum rate
  66. * for a given period of time in seconds;
  67. *
  68. *
  69. *--------------------------------------------------------------------*/
  70. /*====================================================================*
  71. * system header files;
  72. *--------------------------------------------------------------------*/
  73. #include &lt;stdlib.h&gt;
  74. #include &lt;string.h&gt;
  75. #include &lt;limits.h&gt;
  76. #include &lt;time.h&gt;
  77. #include &lt;sys/time.h&gt;
  78. /*====================================================================*
  79. * custom header files;
  80. *--------------------------------------------------------------------*/
  81. #include &quot;../tools/getoptv.h&quot;
  82. #include &quot;../tools/putoptv.h&quot;
  83. #include &quot;../tools/error.h&quot;
  84. #include &quot;../tools/memory.h&quot;
  85. #include &quot;../tools/number.h&quot;
  86. #include &quot;../tools/symbol.h&quot;
  87. #include &quot;../tools/flags.h&quot;
  88. #include &quot;../ether/channel.h&quot;
  89. /*====================================================================*
  90. * custom source files;
  91. *--------------------------------------------------------------------*/
  92. #ifndef MAKEFILE
  93. #include &quot;../tools/getoptv.c&quot;
  94. #include &quot;../tools/putoptv.c&quot;
  95. #include &quot;../tools/version.c&quot;
  96. #include &quot;../tools/error.c&quot;
  97. #include &quot;../tools/basespec.c&quot;
  98. #include &quot;../tools/uintspec.c&quot;
  99. #include &quot;../tools/hexencode.c&quot;
  100. #include &quot;../tools/hexdump.c&quot;
  101. #include &quot;../tools/todigit.c&quot;
  102. #endif
  103. #ifndef MAKEFILE
  104. #include &quot;../ether/openchannel.c&quot;
  105. #include &quot;../ether/closechannel.c&quot;
  106. #include &quot;../ether/readpacket.c&quot;
  107. #include &quot;../ether/sendpacket.c&quot;
  108. #include &quot;../ether/channel.c&quot;
  109. #endif
  110. /*====================================================================*
  111. * program contants;
  112. *--------------------------------------------------------------------*/
  113. #define EFBU_VLAN_TAG 0
  114. #define EFBU_INTERFACE &quot;PLC&quot;
  115. #define EFBU_ETHERTYPE ETH_P_802_2
  116. #define EFBU_BINARY 0xAA
  117. #define EFBU_TIMER 1000
  118. #define EFBU_PAUSE 50
  119. #ifndef ETHER_CRC_LEN
  120. #define ETHER_CRC_LEN 4
  121. #endif
  122. #if defined (WIN32)
  123. #define SLEEP(milliseconds) Sleep(milliseconds)
  124. #else
  125. #define SLEEP(milliseconds) usleep(1000*(milliseconds))
  126. #endif
  127. /*====================================================================*
  128. *
  129. * void function (struct channel * channel, void * memory, unsigned extent, unsigned timer, unsigned pause);
  130. *
  131. * transmit an IP broadcast frame of given length at maximum rate
  132. * for a given period of time in seconds;
  133. *
  134. *
  135. *--------------------------------------------------------------------*/
  136. static void function (struct channel * channel, void * memory, ssize_t extent, byte binary, unsigned timer, unsigned pause)
  137. {
  138. #if EFBU_VLAN_TAG
  139. struct ether_header
  140. {
  141. uint8_t ether_dhost [ETH_ALEN];
  142. uint8_t ether_shost [ETH_ALEN];
  143. uint32_t ether_vlan;
  144. uint16_t ether_type;
  145. }
  146. __attribute__ ((__packed__)) * frame = (struct ether_header *)(memory);
  147. #else
  148. struct ether_header * frame = (struct ether_header *)(memory);
  149. #endif
  150. struct timeval ts;
  151. struct timeval tc;
  152. unsigned since;
  153. memset (memory, binary, extent);
  154. if (extent &gt; (ETHER_MAX_LEN - ETHER_CRC_LEN))
  155. {
  156. extent = ETHER_MAX_LEN - ETHER_CRC_LEN;
  157. }
  158. memcpy (frame-&gt;ether_dhost, channel-&gt;peer, sizeof (frame-&gt;ether_dhost));
  159. memcpy (frame-&gt;ether_shost, channel-&gt;host, sizeof (frame-&gt;ether_shost));
  160. #if EFBU_VLAN_TAG
  161. frame-&gt;ether_vlan = htonl (0x8100A000);
  162. #endif
  163. frame-&gt;ether_type = htons (channel-&gt;type);
  164. if (gettimeofday (&amp;ts, NULL) == -1)
  165. {
  166. error (1, errno, CANT_START_TIMER);
  167. }
  168. for (since = 0; since &lt; timer; since = (tc.tv_sec - ts.tv_sec) * 1000 + ((tc.tv_usec - ts.tv_usec) / 1000))
  169. {
  170. sendpacket (channel, memory, extent);
  171. if (gettimeofday (&amp;tc, NULL) == -1)
  172. {
  173. error (1, errno, CANT_RESET_TIMER);
  174. }
  175. SLEEP (pause);
  176. }
  177. return;
  178. }
  179. /*====================================================================*
  180. *
  181. * int main (int argc, char const * argv []);
  182. *
  183. *
  184. *
  185. *--------------------------------------------------------------------*/
  186. int main (int argc, char const * argv [])
  187. {
  188. extern struct channel channel;
  189. static char const * optv [] =
  190. {
  191. &quot;b:d:e:hi:p:t:v&quot;,
  192. &quot;&quot;,
  193. &quot;Ethernet Frame Blast Utility&quot;,
  194. &quot;b n\tbinary byte value is (n) [&quot; LITERAL (EFBU_BINARY) &quot;]&quot;,
  195. &quot;d x\treplace destination address with (x)&quot;,
  196. &quot;e x\tethertype is (x) [&quot; LITERAL (EFBU_ETHERTYPE) &quot;]&quot;,
  197. &quot;h\treplace source address with host address&quot;,
  198. #if defined (WINPCAP) || defined (LIBPCAP)
  199. &quot;i n\thost interface is (n) [&quot; LITERAL (CHANNEL_ETHNUMBER) &quot;]&quot;,
  200. #else
  201. &quot;i s\thost interface is (s) [&quot; LITERAL (CHANNEL_ETHDEVICE) &quot;]&quot;,
  202. #endif
  203. &quot;t n\ttransmit for (n) milliseconds [&quot; LITERAL (EFBU_TIMER) &quot;]&quot;,
  204. &quot;p n\tpause (n) milliseconds between frames [&quot; LITERAL (EFBU_PAUSE) &quot;]&quot;,
  205. &quot;v\tverbose messages&quot;,
  206. (char const *) (0)
  207. };
  208. byte buffer [ETHER_MAX_LEN];
  209. byte binary = EFBU_BINARY;
  210. unsigned timer = EFBU_TIMER;
  211. unsigned pause = EFBU_PAUSE;
  212. signed c;
  213. if (getenv (EFBU_INTERFACE))
  214. {
  215. #if defined (WINPCAP) || defined (LIBPCAP)
  216. channel.ifindex = atoi (getenv (EFBU_INTERFACE));
  217. #else
  218. channel.ifname = strdup (getenv (EFBU_INTERFACE));
  219. #endif
  220. }
  221. optind = 1;
  222. memset (channel.peer, 0xFF, sizeof (channel.peer));
  223. memset (channel.host, 0xFF, sizeof (channel.host));
  224. channel.type = EFBU_ETHERTYPE;
  225. while ((c = getoptv (argc, argv, optv)) != -1)
  226. {
  227. switch (c)
  228. {
  229. case 'b':
  230. binary = (uint8_t)(uintspec (optarg, 0, 255));
  231. break;
  232. case 'd':
  233. _setbits (channel.flags, CHANNEL_UPDATE_TARGET);
  234. if (!hexencode (channel.peer, sizeof (channel.peer), optarg))
  235. {
  236. error (1, errno, &quot;%s&quot;, optarg);
  237. }
  238. break;
  239. case 'e':
  240. channel.type = (uint16_t)(basespec (optarg, 16, sizeof (channel.type)));
  241. break;
  242. case 'h':
  243. _setbits (channel.flags, CHANNEL_UPDATE_SOURCE);
  244. break;
  245. case 'i':
  246. #if defined (WINPCAP) || defined (LIBPCAP)
  247. channel.ifindex = atoi (optarg);
  248. #else
  249. channel.ifname = optarg;
  250. #endif
  251. break;
  252. case 'p':
  253. pause = (unsigned)(uintspec (optarg, 0, UINT_MAX));
  254. break;
  255. case 'q':
  256. _setbits (channel.flags, CHANNEL_SILENCE);
  257. break;
  258. case 't':
  259. timer = (unsigned)(uintspec (optarg, 0, UINT_MAX));
  260. break;
  261. case 'v':
  262. _setbits (channel.flags, CHANNEL_VERBOSE);
  263. break;
  264. default:
  265. break;
  266. }
  267. }
  268. argc -= optind;
  269. argv += optind;
  270. if (argc)
  271. {
  272. error (1, ECANCELED, ERROR_TOOMANY);
  273. }
  274. openchannel (&amp;channel);
  275. function (&amp;channel, buffer, sizeof (buffer), binary, timer, pause);
  276. closechannel (&amp;channel);
  277. return (0);
  278. }
  279. </pre>
  280. <div class='footerlink'>
  281. [<a href='edsu.c.html' title=' edsu.c '>PREV</a>]
  282. [<a href='toolkit.html' title=' Index '>HOME</a>]
  283. [<a href='efeu.c.html' title=' efeu.c '>NEXT</a>]
  284. </div>
  285. </body>
  286. </html>