tcpbridge.c 5.6 KB

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