tcpreplay.c 11 KB

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