tcpbridge.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /* $Id: tcpbridge.c 2027 2008-05-15 16:54:58Z 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. COUNTER bytes_sent, total_bytes, failed, pkts_sent, cache_packets;
  56. struct timeval begin, end;
  57. volatile int didsig;
  58. tcpbridge_opt_t options;
  59. tcpedit_t *tcpedit;
  60. /* local functions */
  61. void init(void);
  62. void post_args(int argc, char *argv[]);
  63. int
  64. main(int argc, char *argv[])
  65. {
  66. int optct, rcode;
  67. init();
  68. /* call autoopts to process arguments */
  69. optct = optionProcess(&tcpbridgeOptions, argc, argv);
  70. argc -= optct;
  71. argv += optct;
  72. post_args(argc, argv);
  73. /* init tcpedit context */
  74. if (tcpedit_init(&tcpedit, pcap_datalink(options.listen1)) < 0) {
  75. errx(1, "Error initializing tcpedit: %s", tcpedit_geterr(tcpedit));
  76. }
  77. /* parse the tcpedit args */
  78. rcode = tcpedit_post_args(&tcpedit);
  79. if (rcode < 0) {
  80. errx(1, "Unable to parse args: %s", tcpedit_geterr(tcpedit));
  81. } else if (rcode == 1) {
  82. warnx("%s", tcpedit_geterr(tcpedit));
  83. }
  84. if (tcpedit_validate(tcpedit) < 0) {
  85. errx(1, "Unable to edit packets given options:\n%s",
  86. tcpedit_geterr(tcpedit));
  87. }
  88. #ifdef ENABLE_VERBOSE
  89. if (options.verbose) {
  90. options.tcpdump = (tcpdump_t*)safe_malloc(sizeof(tcpdump_t));
  91. tcpdump_open(options.tcpdump, options.listen1);
  92. }
  93. #endif
  94. if (gettimeofday(&begin, NULL) < 0)
  95. err(1, "gettimeofday() failed");
  96. /* process packets from one or both interfaces */
  97. do_bridge(tcpedit, options.listen1, options.listen2);
  98. /* clean up after ourselves */
  99. sendpacket_close(options.sp1);
  100. pcap_close(options.listen1);
  101. if (options.unidir) {
  102. sendpacket_close(options.sp2);
  103. pcap_close(options.listen2);
  104. }
  105. #ifdef ENABLE_VERBOSE
  106. tcpdump_close(options.tcpdump);
  107. #endif
  108. return 0;
  109. }
  110. void
  111. init(void)
  112. {
  113. bytes_sent = total_bytes = failed = pkts_sent = cache_packets = 0;
  114. memset(&options, 0, sizeof(options));
  115. options.snaplen = 65535;
  116. options.promisc = 1;
  117. options.to_ms = 1;
  118. total_bytes = 0;
  119. if (fcntl(STDERR_FILENO, F_SETFL, O_NONBLOCK) < 0)
  120. warnx("Unable to set STDERR to non-blocking: %s", strerror(errno));
  121. }
  122. void
  123. post_args(_U_ int argc, _U_ char *argv[])
  124. {
  125. char ebuf[SENDPACKET_ERRBUF_SIZE];
  126. char *intname;
  127. #ifdef ENABLE_PCAP_FINDALLDEVS
  128. interface_list_t *intlist = get_interface_list();
  129. #else
  130. interface_list_t *intlist = NULL;
  131. #endif
  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. options.tcpdump->args = safe_strdup(OPT_ARG(DECODE));
  144. #endif
  145. if (HAVE_OPT(UNIDIR))
  146. options.unidir = 1;
  147. if (HAVE_OPT(LIMIT))
  148. options.limit_send = OPT_VALUE_LIMIT; /* default is -1 */
  149. if ((intname = get_interface(intlist, OPT_ARG(INTF1))) == NULL)
  150. errx(1, "Invalid interface name/alias: %s", OPT_ARG(INTF1));
  151. options.intf1 = safe_strdup(intname);
  152. if (HAVE_OPT(INTF2)) {
  153. if ((intname = get_interface(intlist, OPT_ARG(INTF2))) == NULL)
  154. errx(1, "Invalid interface name/alias: %s", OPT_ARG(INTF2));
  155. options.intf2 = safe_strdup(intname);
  156. }
  157. /* open up interfaces */
  158. if ((options.sp1 = sendpacket_open(options.intf1, ebuf, TCPR_DIR_C2S)) == NULL)
  159. errx(1, "Unable to open interface %s for sending: %s", options.intf1, ebuf);
  160. if ((options.listen1 = pcap_open_live(options.intf1, options.snaplen,
  161. options.promisc, options.to_ms, ebuf)) == NULL)
  162. errx(1, "Unable to open interface %s for recieving: %s", options.intf1, ebuf);
  163. /* open interfaces bi-directionally ?? */
  164. if (!options.unidir) {
  165. if (strcmp(options.intf1, options.intf2) == 0)
  166. errx(1, "Whoa tiger! You don't want to use %s twice!", options.intf1);
  167. if ((options.sp2 = sendpacket_open(options.intf2, ebuf, TCPR_DIR_S2C)) == NULL)
  168. errx(1, "Unable to open interface %s for sending: %s", options.intf2, ebuf);
  169. if ((options.listen2 = pcap_open_live(options.intf2, options.snaplen,
  170. options.promisc, options.to_ms, ebuf)) == NULL)
  171. errx(1, "Unable to open interface %s for recieving: %s", options.intf2, ebuf);
  172. }
  173. }
  174. /*
  175. Local Variables:
  176. mode:c
  177. indent-tabs-mode:nil
  178. c-basic-offset:4
  179. End:
  180. */