/** * when we're sending only one packet at a time via <ENTER> * then there's no race and we can quit now * also called when didsig is set */ void break_now(int signo) { const tcpreplay_stats_t *stats; if (signo == SIGINT || ctx->abort) { printf("\n"); stats = tcpreplay_get_stats(ctx); packet_stats(stats); exit(1); } }
/** * Main entry point to bridging. Does some initial setup and then calls the * correct loop (unidirectional or bidirectional) */ void do_bridge(tcpbridge_opt_t *options, tcpedit_t *tcpedit) { /* do we apply a bpf filter? */ if (options->bpf.filter != NULL) { /* compile filter */ dbgx(2, "Try to compile pcap bpf filter: %s", options->bpf.filter); if (pcap_compile(options->pcap1, &options->bpf.program, options->bpf.filter, options->bpf.optimize, 0) != 0) { errx(-1, "Error compiling BPF filter: %s", pcap_geterr(options->pcap1)); } /* apply filter */ pcap_setfilter(options->pcap1, &options->bpf.program); /* same for other interface if applicable */ if (options->unidir == 0) { /* compile filter */ dbgx(2, "Try to compile pcap bpf filter: %s", options->bpf.filter); if (pcap_compile(options->pcap2, &options->bpf.program, options->bpf.filter, options->bpf.optimize, 0) != 0) { errx(-1, "Error compiling BPF filter: %s", pcap_geterr(options->pcap2)); } /* apply filter */ pcap_setfilter(options->pcap2, &options->bpf.program); } } /* register signals */ didsig = 0; (void)signal(SIGINT, signal_catcher); if (options->unidir == 1) { do_bridge_unidirectional(options, tcpedit); } else { do_bridge_bidirectional(options, tcpedit); } if (gettimeofday(&stats.end_time, NULL) < 0) errx(-1, "gettimeofday() failed: %s", strerror(errno)); packet_stats(&stats); }
int main(int argc, char *argv[]) { int i, optct = 0; int rcode; ctx = tcpreplay_init(); #ifdef TCPREPLAY optct = optionProcess(&tcpreplayOptions, argc, argv); #elif defined TCPREPLAY_EDIT optct = optionProcess(&tcpreplay_editOptions, argc, argv); #endif argc -= optct; argv += optct; rcode = tcpreplay_post_args(ctx, argc); if (rcode <= -2) { warnx("%s", tcpreplay_getwarn(ctx)); } else if (rcode == -1) { errx(-1, "Unable to parse args: %s", tcpreplay_geterr(ctx)); } #ifdef TCPREPLAY_EDIT /* init tcpedit context */ if (tcpedit_init(&tcpedit, sendpacket_get_dlt(ctx->intf1)) < 0) { errx(-1, "Error initializing tcpedit: %s", tcpedit_geterr(tcpedit)); } /* parse the tcpedit args */ rcode = tcpedit_post_args(tcpedit); if (rcode < 0) { errx(-1, "Unable to parse args: %s", tcpedit_geterr(tcpedit)); } else if (rcode == 1) { warnx("%s", tcpedit_geterr(tcpedit)); } if (tcpedit_validate(tcpedit) < 0) { errx(-1, "Unable to edit packets given options:\n%s", tcpedit_geterr(tcpedit)); } #endif if ((ctx->options->enable_file_cache || ctx->options->preload_pcap) && ! HAVE_OPT(QUIET)) { notice("File Cache is enabled"); } /* * Setup up the file cache, if required */ if (ctx->options->enable_file_cache || ctx->options->preload_pcap) { /* Initialise each of the file cache structures */ for (i = 0; i < argc; i++) { ctx->options->file_cache[i].index = i; ctx->options->file_cache[i].cached = FALSE; ctx->options->file_cache[i].packet_cache = NULL; } } for (i = 0; i < argc; i++) { tcpreplay_add_pcapfile(ctx, argv[i]); /* preload our pcap file? */ if (ctx->options->preload_pcap) { preload_pcap_file(ctx, i); } } /* init the signal handlers */ init_signal_handlers(); if (gettimeofday(&ctx->stats.start_time, NULL) < 0) errx(-1, "gettimeofday() failed: %s", strerror(errno)); /* main loop, when not looping forever */ if (ctx->options->loop > 0) { while (ctx->options->loop--) { /* limited loop */ if (ctx->options->dualfile) { /* process two files at a time for network taps */ for (i = 0; i < argc; i += 2) { tcpr_replay_index(ctx, i); } } else { /* process each pcap file in order */ for (i = 0; i < argc; i++) { /* reset cache markers for each iteration */ ctx->cache_byte = 0; ctx->cache_bit = 0; tcpr_replay_index(ctx, i); } } } } else { /* loop forever */ while (1) { if (ctx->options->dualfile) { /* process two files at a time for network taps */ for (i = 0; i < argc; i += 2) { tcpr_replay_index(ctx, i); } } else { /* process each pcap file in order */ for (i = 0; i < argc; i++) { /* reset cache markers for each iteration */ ctx->cache_byte = 0; ctx->cache_bit = 0; tcpr_replay_index(ctx, i); } } } } if (ctx->stats.bytes_sent > 0) { if (gettimeofday(&ctx->stats.end_time, NULL) < 0) errx(-1, "gettimeofday() failed: %s", strerror(errno)); packet_stats(&ctx->stats); printf("%s", sendpacket_getstat(ctx->intf1)); if (ctx->intf2 != NULL) printf("%s", sendpacket_getstat(ctx->intf2)); } tcpreplay_close(ctx); return 0; } /* main() */