tcprewrite.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /* $Id: tcprewrite.c 1506 2006-07-17 02:58:59Z 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 "tcprewrite.h"
  47. #include "tcprewrite_opts.h"
  48. #include "tcpedit/tcpedit.h"
  49. #include "tcpedit/parse_args.h"
  50. #ifdef DEBUG
  51. int debug;
  52. #endif
  53. #ifdef HAVE_TCPDUMP
  54. /* tcpdump handle */
  55. tcpdump_t tcpdump;
  56. #endif
  57. tcprewrite_opt_t options;
  58. tcpedit_t tcpedit;
  59. /* local functions */
  60. void 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. char ebuf[LIBNET_ERRBUF_SIZE];
  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. if ((options.l = libnet_init(LIBNET_RAW4, NULL, ebuf)) == NULL)
  89. errx(1, "Unable to open raw socket for libnet: %s", ebuf);
  90. #ifdef HAVE_TCPDUMP
  91. if (options.verbose) {
  92. tcpdump.filename = options.infile;
  93. tcpdump_open(&tcpdump);
  94. }
  95. #endif
  96. if (tcpedit_validate(&tcpedit, pcap_datalink(options.pin),
  97. pcap_datalink(options.pin)) < 0) {
  98. errx(1, "Unable to edit packets given options/DLT types:\n%s",
  99. tcpedit_geterr(&tcpedit));
  100. }
  101. if (rewrite_packets(&tcpedit, options.pin, options.pout) != 0)
  102. errx(1, "Error rewriting packets: %s", tcpedit_geterr(&tcpedit));
  103. /* clean up after ourselves */
  104. libnet_destroy(options.l);
  105. pcap_dump_close(options.pout);
  106. pcap_close(options.pin);
  107. #ifdef HAVE_TCPDUMP
  108. tcpdump_close(&tcpdump);
  109. #endif
  110. return 0;
  111. }
  112. void
  113. init(void)
  114. {
  115. memset(&options, 0, sizeof(options));
  116. memset(&tcpedit, 0, sizeof(tcpedit_t));
  117. #ifdef HAVE_TCPDUMP
  118. /* clear out tcpdump struct */
  119. memset(&tcpdump, '\0', sizeof(tcpdump_t));
  120. #endif
  121. if (fcntl(STDERR_FILENO, F_SETFL, O_NONBLOCK) < 0)
  122. warnx("Unable to set STDERR to non-blocking: %s", strerror(errno));
  123. }
  124. void
  125. post_args(int argc, char *argv[])
  126. {
  127. char ebuf[PCAP_ERRBUF_SIZE];
  128. #ifdef DEBUG
  129. if (HAVE_OPT(DBUG))
  130. debug = OPT_VALUE_DBUG;
  131. #else
  132. if (HAVE_OPT(DBUG))
  133. warn("not configured with --enable-debug. Debugging disabled.");
  134. #endif
  135. #ifdef HAVE_TCPDUMP
  136. if (HAVE_OPT(VERBOSE))
  137. options.verbose = 1;
  138. if (HAVE_OPT(DECODE))
  139. tcpdump.args = safe_strdup(OPT_ARG(DECODE));
  140. #endif
  141. /* open up the input file */
  142. options.infile = safe_strdup(OPT_ARG(INFILE));
  143. if ((options.pin = pcap_open_offline(options.infile, ebuf)) == NULL)
  144. errx(1, "Unable to open input pcap file: %s", ebuf);
  145. /* open up the output file */
  146. options.outfile = safe_strdup(OPT_ARG(OUTFILE));
  147. if ((options.pout = pcap_dump_open(options.pin, options.outfile)) == NULL)
  148. errx(1, "Unable to open output pcap file: %s", pcap_geterr(options.pin));
  149. }
  150. int
  151. rewrite_packets(tcpedit_t *tcpedit, pcap_t *pin, pcap_dumper_t *pout)
  152. {
  153. int cache_result = CACHE_PRIMARY; /* default to primary */
  154. struct pcap_pkthdr *pkthdr = NULL; /* packet header */
  155. const u_char *pktdata = NULL; /* packet from libpcap */
  156. COUNTER packetnum = 0;
  157. #ifdef FORCE_ALIGN
  158. ipbuff = (u_char *)safe_malloc(MAXPACKET);
  159. #endif
  160. /* MAIN LOOP
  161. * Keep sending while we have packets or until
  162. * we've sent enough packets
  163. */
  164. while (pcap_next_ex(pin, &pkthdr, &pktdata) == 1) {
  165. packetnum++;
  166. dbgx(2, "packet " COUNTER_SPEC " caplen %d", packetnum, pkthdr->caplen);
  167. #ifdef HAVE_TCPDUMP
  168. if (options.verbose)
  169. tcpdump_print(&tcpdump, pkthdr, 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 == CACHE_NOSEND)
  181. goto WRITE_PACKET; /* still need to write it so cache stays in sync */
  182. if (tcpedit_packet(tcpedit, &pkthdr, (u_char**)&pktdata, cache_result) == -1) {
  183. return -1;
  184. }
  185. WRITE_PACKET:
  186. /* write the packet */
  187. pcap_dump((u_char *)pout, pkthdr, pktdata);
  188. } /* while() */
  189. return 0;
  190. }
  191. /*
  192. Local Variables:
  193. mode:c
  194. indent-tabs-mode:nil
  195. c-basic-offset:4
  196. End:
  197. */