tcpreplay.c 11 KB

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