efbu.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. * system header files;
  10. *--------------------------------------------------------------------*/
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <limits.h>
  14. #include <time.h>
  15. #include <sys/time.h>
  16. /*====================================================================*
  17. * custom header files;
  18. *--------------------------------------------------------------------*/
  19. #include "../tools/getoptv.h"
  20. #include "../tools/putoptv.h"
  21. #include "../tools/error.h"
  22. #include "../tools/timer.h"
  23. #include "../tools/memory.h"
  24. #include "../tools/number.h"
  25. #include "../tools/symbol.h"
  26. #include "../tools/flags.h"
  27. #include "../ether/channel.h"
  28. /*====================================================================*
  29. * custom source files;
  30. *--------------------------------------------------------------------*/
  31. #ifndef MAKEFILE
  32. #include "../tools/getoptv.c"
  33. #include "../tools/putoptv.c"
  34. #include "../tools/version.c"
  35. #include "../tools/error.c"
  36. #include "../tools/basespec.c"
  37. #include "../tools/uintspec.c"
  38. #include "../tools/hexencode.c"
  39. #include "../tools/hexdump.c"
  40. #include "../tools/todigit.c"
  41. #endif
  42. #ifndef MAKEFILE
  43. #include "../ether/openchannel.c"
  44. #include "../ether/closechannel.c"
  45. #include "../ether/readpacket.c"
  46. #include "../ether/sendpacket.c"
  47. #include "../ether/channel.c"
  48. #endif
  49. /*====================================================================*
  50. * program contants;
  51. *--------------------------------------------------------------------*/
  52. #define EFBU_VLAN_TAG 0
  53. #define EFBU_INTERFACE "PLC"
  54. #define EFBU_ETHERTYPE ETH_P_802_2
  55. #define EFBU_BINARY 0xAA
  56. #define EFBU_TIMER 1000
  57. #define EFBU_PAUSE 50
  58. #ifndef ETHER_CRC_LEN
  59. #define ETHER_CRC_LEN 4
  60. #endif
  61. /*====================================================================*
  62. *
  63. * void function (struct channel * channel, void * memory, unsigned extent, unsigned timer, unsigned pause);
  64. *
  65. * transmit an IP broadcast frame of given length at maximum rate
  66. * for a given period of time in seconds;
  67. *
  68. *--------------------------------------------------------------------*/
  69. static void function (struct channel * channel, void * memory, ssize_t extent, byte binary, unsigned timer, unsigned pause)
  70. {
  71. #if EFBU_VLAN_TAG
  72. struct ether_header
  73. {
  74. uint8_t ether_dhost [ETH_ALEN];
  75. uint8_t ether_shost [ETH_ALEN];
  76. uint32_t ether_vlan;
  77. uint16_t ether_type;
  78. }
  79. __attribute__ ((__packed__)) * frame = (struct ether_header *) (memory);
  80. #else
  81. struct ether_header * frame = (struct ether_header *) (memory);
  82. #endif
  83. struct timeval ts;
  84. struct timeval tc;
  85. unsigned since;
  86. memset (memory, binary, extent);
  87. if (extent > (ETHER_MAX_LEN - ETHER_CRC_LEN))
  88. {
  89. extent = ETHER_MAX_LEN - ETHER_CRC_LEN;
  90. }
  91. memcpy (frame->ether_dhost, channel->peer, sizeof (frame->ether_dhost));
  92. memcpy (frame->ether_shost, channel->host, sizeof (frame->ether_shost));
  93. #if EFBU_VLAN_TAG
  94. frame->ether_vlan = htonl (0x8100A000);
  95. #endif
  96. frame->ether_type = htons (channel->type);
  97. if (gettimeofday (& ts, NULL) == - 1)
  98. {
  99. error (1, errno, CANT_START_TIMER);
  100. }
  101. for (since = 0; since < timer; since = (tc.tv_sec - ts.tv_sec) * 1000 + ((tc.tv_usec - ts.tv_usec) / 1000))
  102. {
  103. sendpacket (channel, memory, extent);
  104. if (gettimeofday (& tc, NULL) == - 1)
  105. {
  106. error (1, errno, CANT_RESET_TIMER);
  107. }
  108. SLEEP (pause);
  109. }
  110. return;
  111. }
  112. /*====================================================================*
  113. *
  114. * int main (int argc, char const * argv []);
  115. *
  116. *--------------------------------------------------------------------*/
  117. int main (int argc, char const * argv [])
  118. {
  119. extern struct channel channel;
  120. static char const * optv [] =
  121. {
  122. "b:d:e:hi:p:t:v",
  123. "",
  124. "Ethernet Frame Blast Utility",
  125. "b n\tbinary byte value is (n) [" LITERAL (EFBU_BINARY) "]",
  126. "d x\treplace destination address with (x)",
  127. "e x\tethertype is (x) [" LITERAL (EFBU_ETHERTYPE) "]",
  128. "h\treplace source address with host address",
  129. #if defined (WINPCAP) || defined (LIBPCAP)
  130. "i n\thost interface is (n) [" LITERAL (CHANNEL_ETHNUMBER) "]",
  131. #else
  132. "i s\thost interface is (s) [" LITERAL (CHANNEL_ETHDEVICE) "]",
  133. #endif
  134. "t n\ttransmit for (n) milliseconds [" LITERAL (EFBU_TIMER) "]",
  135. "p n\tpause (n) milliseconds between frames [" LITERAL (EFBU_PAUSE) "]",
  136. "v\tverbose messages",
  137. (char const *) (0)
  138. };
  139. byte buffer [ETHER_MAX_LEN];
  140. byte binary = EFBU_BINARY;
  141. unsigned timer = EFBU_TIMER;
  142. unsigned pause = EFBU_PAUSE;
  143. signed c;
  144. if (getenv (EFBU_INTERFACE))
  145. {
  146. #if defined (WINPCAP) || defined (LIBPCAP)
  147. channel.ifindex = atoi (getenv (EFBU_INTERFACE));
  148. #else
  149. channel.ifname = strdup (getenv (EFBU_INTERFACE));
  150. #endif
  151. }
  152. optind = 1;
  153. memset (channel.peer, 0xFF, sizeof (channel.peer));
  154. memset (channel.host, 0xFF, sizeof (channel.host));
  155. channel.type = EFBU_ETHERTYPE;
  156. while (~ (c = getoptv (argc, argv, optv)))
  157. {
  158. switch (c)
  159. {
  160. case 'b':
  161. binary = (uint8_t) (uintspec (optarg, 0, 255));
  162. break;
  163. case 'd':
  164. _setbits (channel.flags, CHANNEL_UPDATE_TARGET);
  165. if (! hexencode (channel.peer, sizeof (channel.peer), optarg))
  166. {
  167. error (1, errno, "%s", optarg);
  168. }
  169. break;
  170. case 'e':
  171. channel.type = (uint16_t) (basespec (optarg, 16, sizeof (channel.type)));
  172. break;
  173. case 'h':
  174. _setbits (channel.flags, CHANNEL_UPDATE_SOURCE);
  175. break;
  176. case 'i':
  177. #if defined (WINPCAP) || defined (LIBPCAP)
  178. channel.ifindex = atoi (optarg);
  179. #else
  180. channel.ifname = optarg;
  181. #endif
  182. break;
  183. case 'p':
  184. pause = (unsigned) (uintspec (optarg, 0, UINT_MAX));
  185. break;
  186. case 'q':
  187. _setbits (channel.flags, CHANNEL_SILENCE);
  188. break;
  189. case 't':
  190. timer = (unsigned) (uintspec (optarg, 0, UINT_MAX));
  191. break;
  192. case 'v':
  193. _setbits (channel.flags, CHANNEL_VERBOSE);
  194. break;
  195. default:
  196. break;
  197. }
  198. }
  199. argc -= optind;
  200. argv += optind;
  201. if (argc)
  202. {
  203. error (1, ECANCELED, ERROR_TOOMANY);
  204. }
  205. openchannel (& channel);
  206. function (& channel, buffer, sizeof (buffer), binary, timer, pause);
  207. closechannel (& channel);
  208. return (0);
  209. }