tcpbridge.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /* $Id: tcpbridge.c 1573 2006-08-05 20:26:08Z aturner $ */
  2. /*
  3. * Copyright (c) 2004-2005 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 "tcpbridge.h"
  48. #include "tcpbridge_opts.h"
  49. #include "bridge.h"
  50. #include "tcpedit/tcpedit.h"
  51. #include "send_packets.h"
  52. #ifdef DEBUG
  53. int debug;
  54. #endif
  55. #ifdef HAVE_TCPDUMP
  56. /* tcpdump handle */
  57. tcpdump_t tcpdump;
  58. #endif
  59. COUNTER bytes_sent, total_bytes, failed, pkts_sent, cache_packets;
  60. struct timeval begin, end;
  61. volatile int didsig;
  62. tcpbridge_opt_t options;
  63. /* local functions */
  64. void init(void);
  65. void post_args(int argc, char *argv[]);
  66. int
  67. main(int argc, char *argv[])
  68. {
  69. int optct;
  70. init();
  71. /* call autoopts to process arguments */
  72. optct = optionProcess(&tcpbridgeOptions, argc, argv);
  73. argc -= optct;
  74. argv += optct;
  75. post_args(argc, argv);
  76. #ifdef HAVE_TCPDUMP
  77. if (options.verbose) {
  78. tcpdump_open_live(&tcpdump, options.listen1);
  79. }
  80. #endif
  81. /* process packets from one or both interfaces */
  82. do_bridge(options.listen1, options.listen2);
  83. /* clean up after ourselves */
  84. sendpacket_close(options.sp1);
  85. pcap_close(options.listen1);
  86. if (! options.unidir) {
  87. sendpacket_close(options.sp2);
  88. pcap_close(options.listen2);
  89. }
  90. #ifdef HAVE_TCPDUMP
  91. tcpdump_close(&tcpdump);
  92. #endif
  93. return 0;
  94. }
  95. void
  96. init(void)
  97. {
  98. bytes_sent = total_bytes = failed = pkts_sent = cache_packets = 0;
  99. memset(&options, 0, sizeof(options));
  100. options.snaplen = 65535;
  101. options.promisc = 1;
  102. options.to_ms = 1;
  103. /* default is ethernet */
  104. options.l2.dlt = DLT_EN10MB;
  105. total_bytes = 0;
  106. #ifdef HAVE_TCPDUMP
  107. /* clear out tcpdump struct */
  108. memset(&tcpdump, '\0', sizeof(tcpdump_t));
  109. #endif
  110. if (fcntl(STDERR_FILENO, F_SETFL, O_NONBLOCK) < 0)
  111. warnx("Unable to set STDERR to non-blocking: %s", strerror(errno));
  112. }
  113. void
  114. post_args(int argc, char *argv[])
  115. {
  116. char ebuf[SENDPACKET_ERRBUF_SIZE];
  117. #ifdef DEBUG
  118. if (HAVE_OPT(DBUG))
  119. debug = OPT_VALUE_DBUG;
  120. #else
  121. if (HAVE_OPT(DBUG))
  122. warn("not configured with --enable-debug. Debugging disabled.");
  123. #endif
  124. #ifdef HAVE_TCPDUMP
  125. if (HAVE_OPT(VERBOSE))
  126. options.verbose = 1;
  127. if (HAVE_OPT(DECODE))
  128. tcpdump.args = safe_strdup(OPT_ARG(DECODE));
  129. #endif
  130. if (HAVE_OPT(UNIDIR))
  131. options.unidir = 1;
  132. if (HAVE_OPT(LIMIT))
  133. options.limit_send = OPT_VALUE_LIMIT; /* default is -1 */
  134. options.intf1 = safe_strdup(OPT_ARG(INTF1));
  135. if (HAVE_OPT(INTF2))
  136. options.intf2 = safe_strdup(OPT_ARG(INTF2));
  137. /* open up interfaces */
  138. if ((options.sp1 = sendpacket_open(options.intf1, ebuf)) == NULL)
  139. errx(1, "Unable to open interface %s for sending: %s", options.intf1, ebuf);
  140. if ((options.listen1 = pcap_open_live(options.intf1, options.snaplen,
  141. options.promisc, options.to_ms, ebuf)) == NULL)
  142. errx(1, "Unable to open interface %s for recieving: %s", options.intf1, ebuf);
  143. /* open interfaces bi-directionally ?? */
  144. if (!options.unidir) {
  145. if (strcmp(options.intf1, options.intf2) == 0)
  146. errx(1, "Whoa tiger! You don't want to use %s twice!", options.intf1);
  147. if ((options.sp2 = sendpacket_open(options.intf2, ebuf)) == NULL)
  148. errx(1, "Unable to open interface %s for sending: %s", options.intf2, ebuf);
  149. if ((options.listen2 = pcap_open_live(options.intf2, options.snaplen,
  150. options.promisc, options.to_ms, ebuf)) == NULL)
  151. errx(1, "Unable to open interface %s for recieving: %s", options.intf2, ebuf);
  152. }
  153. /*
  154. if (HAVE_OPT(ENDPOINTS)) {
  155. tcpedit->rewrite_ip = TCPEDIT_REWRITE_IP_ON;
  156. if (! parse_endpoints(&tcpedit->cidrmap1, &tcpedit->cidrmap2,
  157. OPT_ARG(ENDPOINTS))) {
  158. tcpedit_seterr(tcpedit,
  159. "Unable to parse --endpoints=%s", OPT_ARG(ENDPOINTS));
  160. return -1;
  161. }
  162. }
  163. */
  164. }
  165. /*
  166. Local Variables:
  167. mode:c
  168. indent-tabs-mode:nil
  169. c-basic-offset:4
  170. End:
  171. */