tcpreplay.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /* $Id: tcpreplay.c 2433 2010-03-28 20:57:36Z aturner $ */
  2. /*
  3. * Copyright (c) 2001-2010 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 preload_pcap_file(int file_idx);
  64. void replay_file(int file_idx);
  65. void usage(void);
  66. void init(void);
  67. void post_args(void);
  68. int
  69. main(int argc, char *argv[])
  70. {
  71. int i, optct = 0;
  72. #ifdef TCPREPLAY_EDIT
  73. int rcode;
  74. #endif
  75. init(); /* init our globals */
  76. optct = optionProcess(&tcpreplayOptions, argc, argv);
  77. argc -= optct;
  78. argv += optct;
  79. post_args();
  80. #ifdef TCPREPLAY_EDIT
  81. /* init tcpedit context */
  82. if (tcpedit_init(&tcpedit, sendpacket_get_dlt(options.intf1)) < 0) {
  83. errx(-1, "Error initializing tcpedit: %s", tcpedit_geterr(tcpedit));
  84. }
  85. /* parse the tcpedit args */
  86. rcode = tcpedit_post_args(&tcpedit);
  87. if (rcode < 0) {
  88. errx(-1, "Unable to parse args: %s", tcpedit_geterr(tcpedit));
  89. } else if (rcode == 1) {
  90. warnx("%s", tcpedit_geterr(tcpedit));
  91. }
  92. if (tcpedit_validate(tcpedit) < 0) {
  93. errx(-1, "Unable to edit packets given options:\n%s",
  94. tcpedit_geterr(tcpedit));
  95. }
  96. #endif
  97. if ((options.enable_file_cache || options.preload_pcap) && ! HAVE_OPT(QUIET)) {
  98. notice("File Cache is enabled");
  99. }
  100. /*
  101. * Setup up the file cache, if required
  102. */
  103. if (options.enable_file_cache || options.preload_pcap) {
  104. options.file_cache = safe_malloc(argc * sizeof(file_cache_t));
  105. /*
  106. * Initialise each of the file cache structures
  107. */
  108. for (i = 0; i < argc; i++) {
  109. options.file_cache[i].index = i;
  110. options.file_cache[i].cached = FALSE;
  111. options.file_cache[i].packet_cache = NULL;
  112. }
  113. }
  114. for (i = 0; i < argc; i++) {
  115. options.files[i] = safe_strdup(argv[i]);
  116. /* preload our pcap file? */
  117. if (options.preload_pcap) {
  118. preload_pcap_file(i);
  119. }
  120. }
  121. /* init the signal handlers */
  122. init_signal_handlers();
  123. if (gettimeofday(&begin, NULL) < 0)
  124. errx(-1, "gettimeofday() failed: %s", strerror(errno));
  125. /* main loop for non-bridge mode */
  126. if (options.loop > 0) {
  127. while (options.loop--) { /* limited loop */
  128. /* process each pcap file in order */
  129. for (i = 0; i < argc; i++) {
  130. /* reset cache markers for each iteration */
  131. cache_byte = 0;
  132. cache_bit = 0;
  133. replay_file(i);
  134. }
  135. }
  136. }
  137. else {
  138. /* loop forever */
  139. while (1) {
  140. for (i = 0; i < argc; i++) {
  141. /* reset cache markers for each iteration */
  142. cache_byte = 0;
  143. cache_bit = 0;
  144. replay_file(i);
  145. }
  146. }
  147. }
  148. if (bytes_sent > 0) {
  149. if (gettimeofday(&end, NULL) < 0)
  150. errx(-1, "Unable to gettimeofday(): %s", strerror(errno));
  151. packet_stats(&begin, &end, bytes_sent, pkts_sent, failed);
  152. printf("%s", sendpacket_getstat(options.intf1));
  153. if (options.intf2 != NULL)
  154. printf("%s", sendpacket_getstat(options.intf2));
  155. }
  156. return 0;
  157. } /* main() */
  158. /**
  159. * \brief Preloads the memory cache for the given pcap file_idx
  160. *
  161. * Preloading can be used with or without --loop and implies using
  162. * --enable-file-cache
  163. */
  164. void
  165. preload_pcap_file(int file_idx)
  166. {
  167. char *path = options.files[file_idx];
  168. pcap_t *pcap = NULL;
  169. char ebuf[PCAP_ERRBUF_SIZE];
  170. const u_char *pktdata = NULL;
  171. struct pcap_pkthdr pkthdr;
  172. packet_cache_t *cached_packet = NULL;
  173. packet_cache_t **prev_packet = &cached_packet;
  174. COUNTER packetnum = 0;
  175. /* close stdin if reading from it (needed for some OS's) */
  176. if (strncmp(path, "-", 1) == 0)
  177. if (close(1) == -1)
  178. warnx("unable to close stdin: %s", strerror(errno));
  179. if ((pcap = pcap_open_offline(path, ebuf)) == NULL)
  180. errx(-1, "Error opening pcap file: %s", ebuf);
  181. #ifdef HAVE_PCAP_SNAPSHOT
  182. if (pcap_snapshot(pcap) < 65535)
  183. warnx("%s was captured using a snaplen of %d bytes. This may mean you have truncated packets.",
  184. path, pcap_snapshot(pcap));
  185. #endif
  186. /* loop through the pcap. get_next_packet() builds the cache for us! */
  187. while ((pktdata = get_next_packet(pcap, &pkthdr, file_idx, prev_packet)) != NULL) {
  188. packetnum++;
  189. }
  190. /* mark this file as cached */
  191. options.file_cache[file_idx].cached = TRUE;
  192. pcap_close(pcap);
  193. }
  194. /**
  195. * replay a pcap file out an interface
  196. */
  197. void
  198. replay_file(int file_idx)
  199. {
  200. char *path = options.files[file_idx];
  201. pcap_t *pcap = NULL;
  202. char ebuf[PCAP_ERRBUF_SIZE];
  203. int dlt;
  204. if (! HAVE_OPT(QUIET))
  205. notice("processing file: %s", path);
  206. /* close stdin if reading from it (needed for some OS's) */
  207. if (strncmp(path, "-", 1) == 0)
  208. if (close(1) == -1)
  209. warnx("unable to close stdin: %s", strerror(errno));
  210. /* read from pcap file if we haven't cached things yet */
  211. if (! (options.enable_file_cache || options.preload_pcap)) {
  212. if ((pcap = pcap_open_offline(path, ebuf)) == NULL)
  213. errx(-1, "Error opening pcap file: %s", ebuf);
  214. } else {
  215. if (!options.file_cache[file_idx].cached)
  216. if ((pcap = pcap_open_offline(path, ebuf)) == NULL)
  217. errx(-1, "Error opening pcap file: %s", ebuf);
  218. }
  219. #ifdef ENABLE_VERBOSE
  220. if (options.verbose) {
  221. /* in cache mode, we may not have opened the file */
  222. if (pcap == NULL)
  223. if ((pcap = pcap_open_offline(path, ebuf)) == NULL)
  224. errx(-1, "Error opening pcap file: %s", ebuf);
  225. /* init tcpdump */
  226. tcpdump_open(options.tcpdump, pcap);
  227. }
  228. #endif
  229. if (pcap != NULL) {
  230. dlt = sendpacket_get_dlt(options.intf1);
  231. if ((dlt > 0) && (dlt != pcap_datalink(pcap)))
  232. warnx("%s DLT (%s) does not match that of the outbound interface: %s (%s)",
  233. path, pcap_datalink_val_to_name(pcap_datalink(pcap)),
  234. options.intf1->device, pcap_datalink_val_to_name(dlt));
  235. }
  236. send_packets(pcap, file_idx);
  237. if (pcap != NULL)
  238. pcap_close(pcap);
  239. #ifdef ENABLE_VERBOSE
  240. tcpdump_close(options.tcpdump);
  241. #endif
  242. }
  243. /**
  244. * Initialize globals
  245. */
  246. void
  247. init(void)
  248. {
  249. bytes_sent = failed = pkts_sent = 0;
  250. memset(&options, 0, sizeof(options));
  251. /* replay packets only once */
  252. options.loop = 1;
  253. /* Default mode is to replay pcap once in real-time */
  254. options.speed.mode = SPEED_MULTIPLIER;
  255. options.speed.speed = 1.0;
  256. /* Set the default timing method */
  257. #ifdef HAVE_ABSOLUTE_TIME
  258. /* This is always the best (if the OS supports it) */
  259. options.accurate = ACCURATE_ABS_TIME;
  260. #else
  261. /* This is probably the second best solution */
  262. options.accurate = ACCURATE_GTOD;
  263. #endif
  264. /* set the default MTU size */
  265. options.mtu = DEFAULT_MTU;
  266. /* disable limit send */
  267. options.limit_send = -1;
  268. #ifdef ENABLE_VERBOSE
  269. /* clear out tcpdump struct */
  270. options.tcpdump = (tcpdump_t *)safe_malloc(sizeof(tcpdump_t));
  271. #endif
  272. cache_bit = cache_byte = 0;
  273. if (fcntl(STDERR_FILENO, F_SETFL, O_NONBLOCK) < 0)
  274. warnx("Unable to set STDERR to non-blocking: %s", strerror(errno));
  275. }
  276. /**
  277. * post processes the args and puts them into our options
  278. */
  279. void
  280. post_args(void)
  281. {
  282. char *temp, *intname;
  283. char ebuf[SENDPACKET_ERRBUF_SIZE];
  284. int int1dlt, int2dlt;
  285. #ifdef ENABLE_PCAP_FINDALLDEVS
  286. interface_list_t *intlist = get_interface_list();
  287. #else
  288. interface_list_t *intlist = NULL;
  289. #endif
  290. #ifdef DEBUG
  291. if (HAVE_OPT(DBUG))
  292. debug = OPT_VALUE_DBUG;
  293. #else
  294. if (HAVE_OPT(DBUG))
  295. warn("not configured with --enable-debug. Debugging disabled.");
  296. #endif
  297. options.loop = OPT_VALUE_LOOP;
  298. options.sleep_accel = OPT_VALUE_SLEEP_ACCEL;
  299. if (HAVE_OPT(LIMIT))
  300. options.limit_send = OPT_VALUE_LIMIT;
  301. if (HAVE_OPT(TOPSPEED)) {
  302. options.speed.mode = SPEED_TOPSPEED;
  303. options.speed.speed = 0.0;
  304. } else if (HAVE_OPT(PPS)) {
  305. options.speed.mode = SPEED_PACKETRATE;
  306. options.speed.speed = (float)OPT_VALUE_PPS;
  307. options.speed.pps_multi = OPT_VALUE_PPS_MULTI;
  308. } else if (HAVE_OPT(ONEATATIME)) {
  309. options.speed.mode = SPEED_ONEATATIME;
  310. options.speed.speed = 0.0;
  311. } else if (HAVE_OPT(MBPS)) {
  312. options.speed.mode = SPEED_MBPSRATE;
  313. options.speed.speed = atof(OPT_ARG(MBPS));
  314. } else if (HAVE_OPT(MULTIPLIER)) {
  315. options.speed.mode = SPEED_MULTIPLIER;
  316. options.speed.speed = atof(OPT_ARG(MULTIPLIER));
  317. }
  318. if (HAVE_OPT(STATS))
  319. options.stats = OPT_ARG(STATS);
  320. #ifdef ENABLE_VERBOSE
  321. if (HAVE_OPT(VERBOSE))
  322. options.verbose = 1;
  323. if (HAVE_OPT(DECODE))
  324. options.tcpdump->args = safe_strdup(OPT_ARG(DECODE));
  325. #endif
  326. /*
  327. * Check if the file cache should be enabled - if we're looping more than
  328. * once and the command line option has been spec'd
  329. */
  330. if (HAVE_OPT(ENABLE_FILE_CACHE) && (options.loop != 1)) {
  331. options.enable_file_cache = TRUE;
  332. }
  333. if (HAVE_OPT(PRELOAD_PCAP)) {
  334. options.preload_pcap = TRUE;
  335. options.enable_file_cache = TRUE;
  336. }
  337. if (HAVE_OPT(TIMER)) {
  338. if (strcmp(OPT_ARG(TIMER), "select") == 0) {
  339. #ifdef HAVE_SELECT
  340. options.accurate = ACCURATE_SELECT;
  341. #else
  342. err(-1, "tcpreplay not compiled with select support");
  343. #endif
  344. } else if (strcmp(OPT_ARG(TIMER), "rdtsc") == 0) {
  345. #ifdef HAVE_RDTSC
  346. options.accurate = ACCURATE_RDTSC;
  347. #else
  348. err(-1, "tcpreplay not compiled with rdtsc support");
  349. #endif
  350. } else if (strcmp(OPT_ARG(TIMER), "ioport") == 0) {
  351. #if defined HAVE_IOPERM && defined(__i386__)
  352. options.accurate = ACCURATE_IOPORT;
  353. ioport_sleep_init();
  354. #else
  355. err(-1, "tcpreplay not compiled with IO Port 0x80 support");
  356. #endif
  357. } else if (strcmp(OPT_ARG(TIMER), "gtod") == 0) {
  358. options.accurate = ACCURATE_GTOD;
  359. } else if (strcmp(OPT_ARG(TIMER), "nano") == 0) {
  360. options.accurate = ACCURATE_NANOSLEEP;
  361. } else if (strcmp(OPT_ARG(TIMER), "abstime") == 0) {
  362. #ifdef HAVE_ABSOLUTE_TIME
  363. options.accurate = ACCURATE_ABS_TIME;
  364. if (!MPLibraryIsLoaded()) {
  365. err(-1, "The MP library did not load.\n");
  366. }
  367. #else
  368. err(-1, "tcpreplay only supports absolute time on Apple OS X");
  369. #endif
  370. } else {
  371. errx(-1, "Unsupported timer mode: %s", OPT_ARG(TIMER));
  372. }
  373. }
  374. #ifdef HAVE_RDTSC
  375. if (HAVE_OPT(RDTSC_CLICKS)) {
  376. rdtsc_calibrate(OPT_VALUE_RDTSC_CLICKS);
  377. }
  378. #endif
  379. if (HAVE_OPT(PKTLEN))
  380. warn("--pktlen may cause problems. Use with caution.");
  381. if ((intname = get_interface(intlist, OPT_ARG(INTF1))) == NULL)
  382. errx(-1, "Invalid interface name/alias: %s", OPT_ARG(INTF1));
  383. options.intf1_name = safe_strdup(intname);
  384. /* open interfaces for writing */
  385. if ((options.intf1 = sendpacket_open(options.intf1_name, ebuf, TCPR_DIR_C2S)) == NULL)
  386. errx(-1, "Can't open %s: %s", options.intf1_name, ebuf);
  387. int1dlt = sendpacket_get_dlt(options.intf1);
  388. if (HAVE_OPT(INTF2)) {
  389. if ((intname = get_interface(intlist, OPT_ARG(INTF2))) == NULL)
  390. errx(-1, "Invalid interface name/alias: %s", OPT_ARG(INTF2));
  391. options.intf2_name = safe_strdup(intname);
  392. /* open interface for writing */
  393. if ((options.intf2 = sendpacket_open(options.intf2_name, ebuf, TCPR_DIR_S2C)) == NULL)
  394. errx(-1, "Can't open %s: %s", options.intf2_name, ebuf);
  395. int2dlt = sendpacket_get_dlt(options.intf2);
  396. if (int2dlt != int1dlt)
  397. errx(-1, "DLT type missmatch for %s (%s) and %s (%s)",
  398. options.intf1_name, pcap_datalink_val_to_name(int1dlt),
  399. options.intf2_name, pcap_datalink_val_to_name(int2dlt));
  400. }
  401. if (HAVE_OPT(CACHEFILE)) {
  402. temp = safe_strdup(OPT_ARG(CACHEFILE));
  403. options.cache_packets = read_cache(&options.cachedata, temp,
  404. &options.comment);
  405. safe_free(temp);
  406. }
  407. if (! HAVE_OPT(QUIET))
  408. notice("sending out %s %s", options.intf1_name,
  409. options.intf2_name == NULL ? "" : options.intf2_name);
  410. }
  411. /*
  412. Local Variables:
  413. mode:c
  414. indent-tabs-mode:nil
  415. c-basic-offset:4
  416. End:
  417. */