tcpreplay.c 12 KB

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