tcprewrite.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* $Id: tcprewrite.c 1842 2007-04-26 04:34:11Z 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 main(int argc, char *argv[])
  65. {
  66. int optct, rcode;
  67. pcap_t *dlt_pcap;
  68. tcprewrite_init();
  69. /* call autoopts to process arguments */
  70. optct = optionProcess(&tcprewriteOptions, argc, argv);
  71. argc -= optct;
  72. argv += optct;
  73. /* parse the tcprewrite args */
  74. post_args(argc, argv);
  75. /* init tcpedit context */
  76. if (tcpedit_init(&tcpedit, pcap_datalink(options.pin)) < 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. /* open up the output file */
  91. options.outfile = safe_strdup(OPT_ARG(OUTFILE));
  92. dbgx(1, "Rewriting DLT to %s",
  93. pcap_datalink_val_to_name(tcpedit_get_output_dlt(tcpedit)));
  94. if ((dlt_pcap = pcap_open_dead(tcpedit_get_output_dlt(tcpedit), 65535)) == NULL)
  95. err(1, "Unable to open dead pcap handle.");
  96. dbgx(1, "DLT of dlt_pcap is %s",
  97. pcap_datalink_val_to_name(pcap_datalink(dlt_pcap)));
  98. #ifdef ENABLE_VERBOSE
  99. if (options.verbose) {
  100. tcpdump_open(&tcpdump, dlt_pcap);
  101. }
  102. #endif
  103. if ((options.pout = pcap_dump_open(dlt_pcap, options.outfile)) == NULL)
  104. errx(1, "Unable to open output pcap file: %s", pcap_geterr(dlt_pcap));
  105. pcap_close(dlt_pcap);
  106. /* rewrite packets */
  107. if (rewrite_packets(tcpedit, options.pin, options.pout) != 0)
  108. errx(1, "Error rewriting packets: %s", tcpedit_geterr(tcpedit));
  109. /* clean up after ourselves */
  110. pcap_dump_close(options.pout);
  111. pcap_close(options.pin);
  112. #ifdef ENABLE_VERBOSE
  113. tcpdump_close(&tcpdump);
  114. #endif
  115. return 0;
  116. }
  117. void
  118. tcprewrite_init(void)
  119. {
  120. memset(&options, 0, sizeof(options));
  121. #ifdef ENABLE_VERBOSE
  122. /* clear out tcpdump struct */
  123. memset(&tcpdump, '\0', sizeof(tcpdump_t));
  124. #endif
  125. if (fcntl(STDERR_FILENO, F_SETFL, O_NONBLOCK) < 0)
  126. warnx("Unable to set STDERR to non-blocking: %s", strerror(errno));
  127. }
  128. void
  129. post_args(_U_ int argc, _U_ char *argv[])
  130. {
  131. char ebuf[PCAP_ERRBUF_SIZE];
  132. #ifdef DEBUG
  133. if (HAVE_OPT(DBUG))
  134. debug = OPT_VALUE_DBUG;
  135. #else
  136. if (HAVE_OPT(DBUG))
  137. warn("not configured with --enable-debug. Debugging disabled.");
  138. #endif
  139. #ifdef ENABLE_VERBOSE
  140. if (HAVE_OPT(VERBOSE))
  141. options.verbose = 1;
  142. if (HAVE_OPT(DECODE))
  143. tcpdump.args = safe_strdup(OPT_ARG(DECODE));
  144. #endif
  145. /* open up the input file */
  146. options.infile = safe_strdup(OPT_ARG(INFILE));
  147. if ((options.pin = pcap_open_offline(options.infile, ebuf)) == NULL)
  148. errx(1, "Unable to open input pcap file: %s", ebuf);
  149. }
  150. int
  151. rewrite_packets(tcpedit_t *tcpedit, pcap_t *pin, pcap_dumper_t *pout)
  152. {
  153. tcpr_dir_t cache_result = TCPR_DIR_C2S; /* default to primary */
  154. struct pcap_pkthdr pkthdr, *pkthdr_ptr; /* packet header */
  155. const u_char *pktdata = NULL; /* packet from libpcap */
  156. u_char **packet = NULL; /* packet from tcpedit */
  157. COUNTER packetnum = 0;
  158. int rcode;
  159. pkthdr_ptr = &pkthdr;
  160. /* MAIN LOOP
  161. * Keep sending while we have packets or until
  162. * we've sent enough packets
  163. */
  164. while ((pktdata = pcap_next(pin, pkthdr_ptr)) != NULL) {
  165. packetnum++;
  166. dbgx(2, "packet " COUNTER_SPEC " caplen %d", packetnum, pkthdr.caplen);
  167. #ifdef ENABLE_VERBOSE
  168. if (options.verbose)
  169. tcpdump_print(&tcpdump, pkthdr_ptr, pktdata);
  170. #endif
  171. /* Dual nic processing? */
  172. if (options.cachedata != NULL) {
  173. cache_result = check_cache(options.cachedata, packetnum);
  174. }
  175. /* sometimes we should not send the packet, in such cases
  176. * no point in editing this packet at all, just write it to the
  177. * output file (note, we can't just remove it, or the tcpprep cache
  178. * file will loose it's indexing
  179. */
  180. if (cache_result == TCPR_DIR_NOSEND)
  181. goto WRITE_PACKET; /* still need to write it so cache stays in sync */
  182. packet = &pktdata;
  183. if ((rcode = tcpedit_packet(tcpedit, &pkthdr_ptr, packet, cache_result)) == TCPEDIT_ERROR) {
  184. return -1;
  185. } else if ((rcode == TCPEDIT_SOFT_ERROR) && HAVE_OPT(SKIP_SOFT_ERRORS)) {
  186. /* don't write packet */
  187. dbgx(1, "Packet " COUNTER_SPEC " is suppressed from being written due to soft errors", packetnum);
  188. continue;
  189. }
  190. WRITE_PACKET:
  191. /* write the packet */
  192. pcap_dump((u_char *)pout, pkthdr_ptr, *packet);
  193. } /* while() */
  194. return 0;
  195. }
  196. /*
  197. Local Variables:
  198. mode:c
  199. indent-tabs-mode:nil
  200. c-basic-offset:4
  201. End:
  202. */