tcprewrite.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /* $Id: tcprewrite.c 1898 2007-08-25 05:10:51Z aturner $ */
  2. /*
  3. * Copyright (c) 2004-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. /*
  32. * Purpose: Modify packets in a pcap file based on rules provided by the
  33. * user to offload work from tcpreplay and provide a easier means of
  34. * reproducing traffic for testing purposes.
  35. */
  36. #include "config.h"
  37. #include "defines.h"
  38. #include "common.h"
  39. #include <ctype.h>
  40. #include <fcntl.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #include <sys/types.h>
  45. #include <unistd.h>
  46. #include <errno.h>
  47. #include "tcprewrite.h"
  48. #include "tcprewrite_opts.h"
  49. #include "tcpedit/tcpedit.h"
  50. #ifdef DEBUG
  51. int debug;
  52. #endif
  53. #ifdef ENABLE_VERBOSE
  54. /* tcpdump handle */
  55. tcpdump_t tcpdump;
  56. #endif
  57. tcprewrite_opt_t options;
  58. tcpedit_t *tcpedit;
  59. /* local functions */
  60. void tcprewrite_init(void);
  61. void post_args(int argc, char *argv[]);
  62. void verify_input_pcap(pcap_t *pcap);
  63. int rewrite_packets(tcpedit_t *tcpedit, pcap_t *pin, pcap_dumper_t *pout);
  64. int
  65. main(int argc, char *argv[])
  66. {
  67. int optct, rcode;
  68. pcap_t *dlt_pcap;
  69. tcprewrite_init();
  70. /* call autoopts to process arguments */
  71. optct = optionProcess(&tcprewriteOptions, argc, argv);
  72. argc -= optct;
  73. argv += optct;
  74. /* parse the tcprewrite args */
  75. post_args(argc, argv);
  76. /* init tcpedit context */
  77. if (tcpedit_init(&tcpedit, pcap_datalink(options.pin)) < 0) {
  78. errx(1, "Error initializing tcpedit: %s", tcpedit_geterr(tcpedit));
  79. }
  80. /* parse the tcpedit args */
  81. rcode = tcpedit_post_args(&tcpedit);
  82. if (rcode < 0) {
  83. errx(1, "Unable to parse args: %s", tcpedit_geterr(tcpedit));
  84. } else if (rcode == 1) {
  85. warnx("%s", tcpedit_geterr(tcpedit));
  86. }
  87. if (tcpedit_validate(tcpedit) < 0) {
  88. errx(1, "Unable to edit packets given options:\n%s",
  89. tcpedit_geterr(tcpedit));
  90. }
  91. /* open up the output file */
  92. options.outfile = safe_strdup(OPT_ARG(OUTFILE));
  93. dbgx(1, "Rewriting DLT to %s",
  94. pcap_datalink_val_to_name(tcpedit_get_output_dlt(tcpedit)));
  95. if ((dlt_pcap = pcap_open_dead(tcpedit_get_output_dlt(tcpedit), 65535)) == NULL)
  96. err(1, "Unable to open dead pcap handle.");
  97. dbgx(1, "DLT of dlt_pcap is %s",
  98. pcap_datalink_val_to_name(pcap_datalink(dlt_pcap)));
  99. #ifdef ENABLE_VERBOSE
  100. if (options.verbose) {
  101. tcpdump_open(&tcpdump, dlt_pcap);
  102. }
  103. #endif
  104. if ((options.pout = pcap_dump_open(dlt_pcap, options.outfile)) == NULL)
  105. errx(1, "Unable to open output pcap file: %s", pcap_geterr(dlt_pcap));
  106. pcap_close(dlt_pcap);
  107. /* rewrite packets */
  108. if (rewrite_packets(tcpedit, options.pin, options.pout) != 0)
  109. errx(1, "Error rewriting packets: %s", tcpedit_geterr(tcpedit));
  110. /* clean up after ourselves */
  111. pcap_dump_close(options.pout);
  112. pcap_close(options.pin);
  113. #ifdef ENABLE_VERBOSE
  114. tcpdump_close(&tcpdump);
  115. #endif
  116. return 0;
  117. }
  118. void
  119. tcprewrite_init(void)
  120. {
  121. memset(&options, 0, sizeof(options));
  122. #ifdef ENABLE_VERBOSE
  123. /* clear out tcpdump struct */
  124. memset(&tcpdump, '\0', sizeof(tcpdump_t));
  125. #endif
  126. if (fcntl(STDERR_FILENO, F_SETFL, O_NONBLOCK) < 0)
  127. warnx("Unable to set STDERR to non-blocking: %s", strerror(errno));
  128. }
  129. /**
  130. * post AutoGen argument processing
  131. */
  132. void
  133. post_args(_U_ int argc, _U_ char *argv[])
  134. {
  135. char ebuf[PCAP_ERRBUF_SIZE];
  136. #ifdef DEBUG
  137. if (HAVE_OPT(DBUG))
  138. debug = OPT_VALUE_DBUG;
  139. #else
  140. if (HAVE_OPT(DBUG))
  141. warn("not configured with --enable-debug. Debugging disabled.");
  142. #endif
  143. #ifdef ENABLE_VERBOSE
  144. if (HAVE_OPT(VERBOSE))
  145. options.verbose = 1;
  146. if (HAVE_OPT(DECODE))
  147. tcpdump.args = safe_strdup(OPT_ARG(DECODE));
  148. #endif
  149. /* open up the input file */
  150. options.infile = safe_strdup(OPT_ARG(INFILE));
  151. if ((options.pin = pcap_open_offline(options.infile, ebuf)) == NULL)
  152. errx(1, "Unable to open input pcap file: %s", ebuf);
  153. }
  154. /**
  155. * Main loop to rewrite packets
  156. */
  157. int
  158. rewrite_packets(tcpedit_t *tcpedit, pcap_t *pin, pcap_dumper_t *pout)
  159. {
  160. tcpr_dir_t cache_result = TCPR_DIR_C2S; /* default to primary */
  161. struct pcap_pkthdr pkthdr, *pkthdr_ptr; /* packet header */
  162. const u_char *pktdata = NULL; /* packet from libpcap */
  163. u_char **packet = NULL; /* packet from tcpedit */
  164. COUNTER packetnum = 0;
  165. int rcode;
  166. pkthdr_ptr = &pkthdr;
  167. /* MAIN LOOP
  168. * Keep sending while we have packets or until
  169. * we've sent enough packets
  170. */
  171. while ((pktdata = pcap_next(pin, pkthdr_ptr)) != NULL) {
  172. packetnum++;
  173. dbgx(2, "packet " COUNTER_SPEC " caplen %d", packetnum, pkthdr.caplen);
  174. #ifdef ENABLE_VERBOSE
  175. if (options.verbose)
  176. tcpdump_print(&tcpdump, pkthdr_ptr, pktdata);
  177. #endif
  178. /* Dual nic processing? */
  179. if (options.cachedata != NULL) {
  180. cache_result = check_cache(options.cachedata, packetnum);
  181. }
  182. /* sometimes we should not send the packet, in such cases
  183. * no point in editing this packet at all, just write it to the
  184. * output file (note, we can't just remove it, or the tcpprep cache
  185. * file will loose it's indexing
  186. */
  187. if (cache_result == TCPR_DIR_NOSEND)
  188. goto WRITE_PACKET; /* still need to write it so cache stays in sync */
  189. packet = &pktdata;
  190. if ((rcode = tcpedit_packet(tcpedit, &pkthdr_ptr, packet, cache_result)) == TCPEDIT_ERROR) {
  191. return -1;
  192. } else if ((rcode == TCPEDIT_SOFT_ERROR) && HAVE_OPT(SKIP_SOFT_ERRORS)) {
  193. /* don't write packet */
  194. dbgx(1, "Packet " COUNTER_SPEC " is suppressed from being written due to soft errors", packetnum);
  195. continue;
  196. }
  197. WRITE_PACKET:
  198. /* write the packet */
  199. pcap_dump((u_char *)pout, pkthdr_ptr, *packet);
  200. } /* while() */
  201. return 0;
  202. }
  203. /*
  204. Local Variables:
  205. mode:c
  206. indent-tabs-mode:nil
  207. c-basic-offset:4
  208. End:
  209. */