tcpreplay.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /* $Id: tcpreplay.c 2346 2009-05-19 00:14:52Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2008 Aaron Turner.
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the names of the copyright owners nor the names of its
  16. * contributors may be used to endorse or promote products derived from
  17. * this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
  20. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  22. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  25. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
  27. * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  28. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  29. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. */
  31. #include "config.h"
  32. #include "defines.h"
  33. #include "common.h"
  34. #include <ctype.h>
  35. #include <fcntl.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <sys/types.h>
  40. #include <unistd.h>
  41. #include <errno.h>
  42. #include "tcpreplay.h"
  43. #ifdef TCPREPLAY_EDIT
  44. #include "tcpreplay_edit_opts.h"
  45. #include "tcpedit/tcpedit.h"
  46. tcpedit_t *tcpedit;
  47. #else
  48. #include "tcpreplay_opts.h"
  49. #endif
  50. #include "send_packets.h"
  51. #include "signal_handler.h"
  52. tcpreplay_opt_t options;
  53. struct timeval begin, end;
  54. COUNTER bytes_sent, failed, pkts_sent;
  55. int cache_bit, cache_byte;
  56. volatile int didsig;
  57. #ifdef DEBUG
  58. int debug = 0;
  59. #endif
  60. #ifdef HAVE_ABSOLUTE_TIME
  61. #include <CoreServices/CoreServices.h>
  62. #endif
  63. void replay_file(int file_idx);
  64. void usage(void);
  65. void init(void);
  66. void post_args(void);
  67. int
  68. main(int argc, char *argv[])
  69. {
  70. int i, optct = 0;
  71. #ifdef TCPREPLAY_EDIT
  72. int rcode;
  73. #endif
  74. init(); /* init our globals */
  75. optct = optionProcess(&tcpreplayOptions, argc, argv);
  76. argc -= optct;
  77. argv += optct;
  78. post_args();
  79. #ifdef TCPREPLAY_EDIT
  80. /* init tcpedit context */
  81. if (tcpedit_init(&tcpedit, sendpacket_get_dlt(options.intf1)) < 0) {
  82. errx(-1, "Error initializing tcpedit: %s", tcpedit_geterr(tcpedit));
  83. }
  84. /* parse the tcpedit args */
  85. rcode = tcpedit_post_args(&tcpedit);
  86. if (rcode < 0) {
  87. errx(-1, "Unable to parse args: %s", tcpedit_geterr(tcpedit));
  88. } else if (rcode == 1) {
  89. warnx("%s", tcpedit_geterr(tcpedit));
  90. }
  91. if (tcpedit_validate(tcpedit) < 0) {
  92. errx(-1, "Unable to edit packets given options:\n%s",
  93. tcpedit_geterr(tcpedit));
  94. }
  95. #endif
  96. if (options.enable_file_cache && ! HAVE_OPT(QUIET)) {
  97. notice("File Cache is enabled");
  98. }
  99. /*
  100. * Setup up the file cache, if required
  101. */
  102. if (options.enable_file_cache) {
  103. options.file_cache = safe_malloc(argc * sizeof(file_cache_t));
  104. /*
  105. Initialise each of the file cache structures
  106. */
  107. for (i = 0; i < argc; i++) {
  108. options.file_cache[i].index = i;
  109. options.file_cache[i].cached = FALSE;
  110. options.file_cache[i].packet_cache = NULL;
  111. }
  112. }
  113. for (i = 0; i < argc; i++)
  114. options.files[i] = safe_strdup(argv[i]);
  115. /* init the signal handlers */
  116. init_signal_handlers();
  117. if (gettimeofday(&begin, NULL) < 0)
  118. errx(-1, "gettimeofday() failed: %s", strerror(errno));
  119. /* main loop for non-bridge mode */
  120. if (options.loop > 0) {
  121. while (options.loop--) { /* limited loop */
  122. /* process each pcap file in order */
  123. for (i = 0; i < argc; i++) {
  124. /* reset cache markers for each iteration */
  125. cache_byte = 0;
  126. cache_bit = 0;
  127. replay_file(i);
  128. }
  129. }
  130. }
  131. else {
  132. /* loop forever */
  133. while (1) {
  134. for (i = 0; i < argc; i++) {
  135. /* reset cache markers for each iteration */
  136. cache_byte = 0;
  137. cache_bit = 0;
  138. replay_file(i);
  139. }
  140. }
  141. }
  142. if (bytes_sent > 0) {
  143. packet_stats(&begin, &end, bytes_sent, pkts_sent, failed);
  144. printf("%s", sendpacket_getstat(options.intf1));
  145. if (options.intf2 != NULL)
  146. printf("%s", sendpacket_getstat(options.intf2));
  147. }
  148. return 0;
  149. } /* main() */
  150. /**
  151. * replay a pcap file out an interface
  152. */
  153. void
  154. replay_file(int file_idx)
  155. {
  156. char *path = options.files[file_idx];
  157. pcap_t *pcap = NULL;
  158. char ebuf[PCAP_ERRBUF_SIZE];
  159. int dlt;
  160. if (! HAVE_OPT(QUIET))
  161. notice("processing file: %s", path);
  162. /* close stdin if reading from it (needed for some OS's) */
  163. if (strncmp(path, "-", 1) == 0)
  164. if (close(1) == -1)
  165. warnx("unable to close stdin: %s", strerror(errno));
  166. /* read from pcap file if we haven't cached things yet */
  167. if (!options.enable_file_cache) {
  168. if ((pcap = pcap_open_offline(path, ebuf)) == NULL)
  169. errx(-1, "Error opening pcap file: %s", ebuf);
  170. } else {
  171. if (!options.file_cache[file_idx].cached)
  172. if ((pcap = pcap_open_offline(path, ebuf)) == NULL)
  173. errx(-1, "Error opening pcap file: %s", ebuf);
  174. }
  175. #ifdef ENABLE_VERBOSE
  176. if (options.verbose) {
  177. /* in cache mode, we may not have opened the file */
  178. if (pcap == NULL)
  179. if ((pcap = pcap_open_offline(path, ebuf)) == NULL)
  180. errx(-1, "Error opening pcap file: %s", ebuf);
  181. /* init tcpdump */
  182. tcpdump_open(options.tcpdump, pcap);
  183. }
  184. #endif
  185. if (pcap != NULL) {
  186. dlt = sendpacket_get_dlt(options.intf1);
  187. if ((dlt > 0) && (dlt != pcap_datalink(pcap)))
  188. warnx("%s DLT (%s) does not match that of the outbound interface: %s (%s)",
  189. path, pcap_datalink_val_to_name(pcap_datalink(pcap)),
  190. options.intf1->device, pcap_datalink_val_to_name(dlt));
  191. }
  192. send_packets(pcap, file_idx);
  193. if (pcap != NULL)
  194. pcap_close(pcap);
  195. #ifdef ENABLE_VERBOSE
  196. tcpdump_close(options.tcpdump);
  197. #endif
  198. }
  199. /**
  200. * Initialize globals
  201. */
  202. void
  203. init(void)
  204. {
  205. bytes_sent = failed = pkts_sent = 0;
  206. memset(&options, 0, sizeof(options));
  207. /* replay packets only once */
  208. options.loop = 1;
  209. /* Default mode is to replay pcap once in real-time */
  210. options.speed.mode = SPEED_MULTIPLIER;
  211. options.speed.speed = 1.0;
  212. /* set the default MTU size */
  213. options.mtu = DEFAULT_MTU;
  214. /* disable limit send */
  215. options.limit_send = -1;
  216. #ifdef ENABLE_VERBOSE
  217. /* clear out tcpdump struct */
  218. options.tcpdump = (tcpdump_t *)safe_malloc(sizeof(tcpdump_t));
  219. #endif
  220. cache_bit = cache_byte = 0;
  221. if (fcntl(STDERR_FILENO, F_SETFL, O_NONBLOCK) < 0)
  222. warnx("Unable to set STDERR to non-blocking: %s", strerror(errno));
  223. }
  224. /**
  225. * post processes the args and puts them into our options
  226. */
  227. void
  228. post_args(void)
  229. {
  230. char *temp, *intname;
  231. char ebuf[SENDPACKET_ERRBUF_SIZE];
  232. int int1dlt, int2dlt;
  233. #ifdef ENABLE_PCAP_FINDALLDEVS
  234. interface_list_t *intlist = get_interface_list();
  235. #else
  236. interface_list_t *intlist = NULL;
  237. #endif
  238. #ifdef DEBUG
  239. if (HAVE_OPT(DBUG))
  240. debug = OPT_VALUE_DBUG;
  241. #else
  242. if (HAVE_OPT(DBUG))
  243. warn("not configured with --enable-debug. Debugging disabled.");
  244. #endif
  245. options.loop = OPT_VALUE_LOOP;
  246. options.sleep_accel = OPT_VALUE_SLEEP_ACCEL;
  247. if (HAVE_OPT(LIMIT))
  248. options.limit_send = OPT_VALUE_LIMIT;
  249. if (HAVE_OPT(TOPSPEED)) {
  250. options.speed.mode = SPEED_TOPSPEED;
  251. options.speed.speed = 0.0;
  252. } else if (HAVE_OPT(PPS)) {
  253. options.speed.mode = SPEED_PACKETRATE;
  254. options.speed.speed = (float)OPT_VALUE_PPS;
  255. options.speed.pps_multi = OPT_VALUE_PPS_MULTI;
  256. } else if (HAVE_OPT(ONEATATIME)) {
  257. options.speed.mode = SPEED_ONEATATIME;
  258. options.speed.speed = 0.0;
  259. } else if (HAVE_OPT(MBPS)) {
  260. options.speed.mode = SPEED_MBPSRATE;
  261. options.speed.speed = atof(OPT_ARG(MBPS));
  262. } else if (HAVE_OPT(MULTIPLIER)) {
  263. options.speed.mode = SPEED_MULTIPLIER;
  264. options.speed.speed = atof(OPT_ARG(MULTIPLIER));
  265. }
  266. #ifdef ENABLE_VERBOSE
  267. if (HAVE_OPT(VERBOSE))
  268. options.verbose = 1;
  269. if (HAVE_OPT(DECODE))
  270. options.tcpdump->args = safe_strdup(OPT_ARG(DECODE));
  271. #endif
  272. /*
  273. * Check if the file cache should be enabled - if we're looping more than
  274. * once and the command line option has been spec'd
  275. */
  276. if (HAVE_OPT(ENABLE_FILE_CACHE) && (options.loop != 1)) {
  277. options.enable_file_cache = TRUE;
  278. }
  279. if (HAVE_OPT(TIMER)) {
  280. if (strcmp(OPT_ARG(TIMER), "select") == 0) {
  281. #ifdef HAVE_SELECT
  282. options.accurate = ACCURATE_SELECT;
  283. #else
  284. err(-1, "tcpreplay not compiled with select support");
  285. #endif
  286. } else if (strcmp(OPT_ARG(TIMER), "rdtsc") == 0) {
  287. #ifdef HAVE_RDTSC
  288. options.accurate = ACCURATE_RDTSC;
  289. #else
  290. err(-1, "tcpreplay not compiled with rdtsc support");
  291. #endif
  292. } else if (strcmp(OPT_ARG(TIMER), "ioport") == 0) {
  293. #if defined HAVE_IOPERM && defined(__i386__)
  294. options.accurate = ACCURATE_IOPORT;
  295. ioport_sleep_init();
  296. #else
  297. err(-1, "tcpreplay not compiled with IO Port 0x80 support");
  298. #endif
  299. } else if (strcmp(OPT_ARG(TIMER), "gtod") == 0) {
  300. options.accurate = ACCURATE_GTOD;
  301. } else if (strcmp(OPT_ARG(TIMER), "nano") == 0) {
  302. options.accurate = ACCURATE_NANOSLEEP;
  303. } else if (strcmp(OPT_ARG(TIMER), "abstime") == 0) {
  304. #ifdef HAVE_ABSOLUTE_TIME
  305. options.accurate = ACCURATE_ABS_TIME;
  306. if (!MPLibraryIsLoaded()) {
  307. err(-1, "The MP library did not load.\n");
  308. }
  309. #else
  310. err(-1, "tcpreplay only supports absolute time on Apple OS X");
  311. #endif
  312. } else {
  313. errx(-1, "Unsupported timer mode: %s", OPT_ARG(TIMER));
  314. }
  315. }
  316. #ifdef HAVE_RDTSC
  317. if (HAVE_OPT(RDTSC_CLICKS)) {
  318. rdtsc_calibrate(OPT_VALUE_RDTSC_CLICKS);
  319. }
  320. #endif
  321. if (HAVE_OPT(PKTLEN))
  322. warn("--pktlen may cause problems. Use with caution.");
  323. if ((intname = get_interface(intlist, OPT_ARG(INTF1))) == NULL)
  324. errx(-1, "Invalid interface name/alias: %s", OPT_ARG(INTF1));
  325. options.intf1_name = safe_strdup(intname);
  326. /* open interfaces for writing */
  327. if ((options.intf1 = sendpacket_open(options.intf1_name, ebuf, TCPR_DIR_C2S)) == NULL)
  328. errx(-1, "Can't open %s: %s", options.intf1_name, ebuf);
  329. int1dlt = sendpacket_get_dlt(options.intf1);
  330. if (HAVE_OPT(INTF2)) {
  331. if ((intname = get_interface(intlist, OPT_ARG(INTF2))) == NULL)
  332. errx(-1, "Invalid interface name/alias: %s", OPT_ARG(INTF2));
  333. options.intf2_name = safe_strdup(intname);
  334. /* open interface for writing */
  335. if ((options.intf2 = sendpacket_open(options.intf2_name, ebuf, TCPR_DIR_S2C)) == NULL)
  336. errx(-1, "Can't open %s: %s", options.intf2_name, ebuf);
  337. int2dlt = sendpacket_get_dlt(options.intf2);
  338. if (int2dlt != int1dlt)
  339. errx(-1, "DLT type missmatch for %s (%s) and %s (%s)",
  340. options.intf1_name, pcap_datalink_val_to_name(int1dlt),
  341. options.intf2_name, pcap_datalink_val_to_name(int2dlt));
  342. }
  343. if (HAVE_OPT(CACHEFILE)) {
  344. temp = safe_strdup(OPT_ARG(CACHEFILE));
  345. options.cache_packets = read_cache(&options.cachedata, temp,
  346. &options.comment);
  347. safe_free(temp);
  348. }
  349. if (! HAVE_OPT(QUIET))
  350. notice("sending out %s %s", options.intf1_name,
  351. options.intf2_name == NULL ? "" : options.intf2_name);
  352. }
  353. /*
  354. Local Variables:
  355. mode:c
  356. indent-tabs-mode:nil
  357. c-basic-offset:4
  358. End:
  359. */