efbu.c 8.2 KB

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