tcprewrite.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /* $Id: tcprewrite.c 1579 2006-08-08 04:05:48Z aturner $ */
  2. /*
  3. * Copyright (c) 2004-2006 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. #include "tcpedit/parse_args.h"
  51. #ifdef DEBUG
  52. int debug;
  53. #endif
  54. #ifdef HAVE_TCPDUMP
  55. /* tcpdump handle */
  56. tcpdump_t tcpdump;
  57. #endif
  58. tcprewrite_opt_t options;
  59. tcpedit_t tcpedit;
  60. /* local functions */
  61. void init(void);
  62. void post_args(int argc, char *argv[]);
  63. void verify_input_pcap(pcap_t *pcap);
  64. int rewrite_packets (tcpedit_t *tcpedit, pcap_t *pin, pcap_dumper_t *pout);
  65. int main(int argc, char *argv[])
  66. {
  67. int optct, rcode;
  68. tcpedit_t *tcpedit_ptr;
  69. 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. tcpedit_ptr = &tcpedit;
  77. /* init tcpedit context */
  78. if (tcpedit_init(&tcpedit, options.pin, NULL) < 0) {
  79. errx(1, "Error initializing tcpedit: %s", tcpedit_geterr(&tcpedit));
  80. }
  81. /* parse the tcpedit args */
  82. rcode = tcpedit_post_args(&tcpedit_ptr);
  83. if (rcode < 0) {
  84. errx(1, "Unable to parse args: %s", tcpedit_geterr(&tcpedit));
  85. } else if (rcode == 1) {
  86. warnx("%s", tcpedit_geterr(&tcpedit));
  87. }
  88. #ifdef HAVE_TCPDUMP
  89. if (options.verbose) {
  90. tcpdump.filename = options.infile;
  91. tcpdump_open(&tcpdump);
  92. }
  93. #endif
  94. if (tcpedit_validate(&tcpedit, pcap_datalink(options.pin),
  95. pcap_datalink(options.pin)) < 0) {
  96. errx(1, "Unable to edit packets given options/DLT types:\n%s",
  97. tcpedit_geterr(&tcpedit));
  98. }
  99. if (rewrite_packets(&tcpedit, options.pin, options.pout) != 0)
  100. errx(1, "Error rewriting packets: %s", tcpedit_geterr(&tcpedit));
  101. /* clean up after ourselves */
  102. pcap_dump_close(options.pout);
  103. pcap_close(options.pin);
  104. #ifdef HAVE_TCPDUMP
  105. tcpdump_close(&tcpdump);
  106. #endif
  107. return 0;
  108. }
  109. void
  110. init(void)
  111. {
  112. memset(&options, 0, sizeof(options));
  113. memset(&tcpedit, 0, sizeof(tcpedit_t));
  114. #ifdef HAVE_TCPDUMP
  115. /* clear out tcpdump struct */
  116. memset(&tcpdump, '\0', sizeof(tcpdump_t));
  117. #endif
  118. if (fcntl(STDERR_FILENO, F_SETFL, O_NONBLOCK) < 0)
  119. warnx("Unable to set STDERR to non-blocking: %s", strerror(errno));
  120. }
  121. void
  122. post_args(int argc, char *argv[])
  123. {
  124. char ebuf[PCAP_ERRBUF_SIZE];
  125. pcap_t *dlt_pcap;
  126. #ifdef DEBUG
  127. if (HAVE_OPT(DBUG))
  128. debug = OPT_VALUE_DBUG;
  129. #else
  130. if (HAVE_OPT(DBUG))
  131. warn("not configured with --enable-debug. Debugging disabled.");
  132. #endif
  133. #ifdef HAVE_TCPDUMP
  134. if (HAVE_OPT(VERBOSE))
  135. options.verbose = 1;
  136. if (HAVE_OPT(DECODE))
  137. tcpdump.args = safe_strdup(OPT_ARG(DECODE));
  138. #endif
  139. /* open up the input file */
  140. options.infile = safe_strdup(OPT_ARG(INFILE));
  141. if ((options.pin = pcap_open_offline(options.infile, ebuf)) == NULL)
  142. errx(1, "Unable to open input pcap file: %s", ebuf);
  143. /* open up the output file */
  144. options.outfile = safe_strdup(OPT_ARG(OUTFILE));
  145. if (HAVE_OPT(DLT)) {
  146. if ((dlt_pcap = pcap_open_dead(OPT_ARG(DLT), 65535)) == NULL)
  147. err(1, "Unable to open dead pcap handle.");
  148. if ((options.pout = pcap_dump_open(dlt_pcap, options.outfile)) == NULL)
  149. errx(1, "Unable to open output pcap file: %s", pcap_geterr(dlt_pcap));
  150. pcap_close(dlt_pcap);
  151. } else {
  152. if ((options.pout = pcap_dump_open(options.pin, options.outfile)) == NULL)
  153. errx(1, "Unable to open output pcap file: %s", pcap_geterr(options.pin));
  154. }
  155. }
  156. int
  157. rewrite_packets(tcpedit_t *tcpedit, pcap_t *pin, pcap_dumper_t *pout)
  158. {
  159. int cache_result = CACHE_PRIMARY; /* default to primary */
  160. struct pcap_pkthdr *pkthdr = NULL; /* packet header */
  161. const u_char *pktdata = NULL; /* packet from libpcap */
  162. COUNTER packetnum = 0;
  163. #ifdef FORCE_ALIGN
  164. ipbuff = (u_char *)safe_malloc(MAXPACKET);
  165. #endif
  166. /* MAIN LOOP
  167. * Keep sending while we have packets or until
  168. * we've sent enough packets
  169. */
  170. while (pcap_next_ex(pin, &pkthdr, &pktdata) == 1) {
  171. packetnum++;
  172. dbgx(2, "packet " COUNTER_SPEC " caplen %d", packetnum, pkthdr->caplen);
  173. #ifdef HAVE_TCPDUMP
  174. if (options.verbose)
  175. tcpdump_print(&tcpdump, pkthdr, pktdata);
  176. #endif
  177. /* Dual nic processing? */
  178. if (options.cachedata != NULL) {
  179. cache_result = check_cache(options.cachedata, packetnum);
  180. }
  181. /* sometimes we should not send the packet, in such cases
  182. * no point in editing this packet at all, just write it to the
  183. * output file (note, we can't just remove it, or the tcpprep cache
  184. * file will loose it's indexing
  185. */
  186. if (cache_result == CACHE_NOSEND)
  187. goto WRITE_PACKET; /* still need to write it so cache stays in sync */
  188. if (tcpedit_packet(tcpedit, &pkthdr, (u_char**)&pktdata, cache_result) == -1) {
  189. return -1;
  190. }
  191. WRITE_PACKET:
  192. /* write the packet */
  193. pcap_dump((u_char *)pout, pkthdr, pktdata);
  194. } /* while() */
  195. return 0;
  196. }
  197. /*
  198. Local Variables:
  199. mode:c
  200. indent-tabs-mode:nil
  201. c-basic-offset:4
  202. End:
  203. */