int main(int argc, char *argv[]) { struct interface_list *itfc_list; char iface[100]; pd = NULL; gui_register_exit(&exit_fun); gui_register_stop(&stop_fun); itfc_list = get_interface_list(); gui_set_interface_list(itfc_list); guipid = gui_pthread_start(&argc, &argv); cappid = pthread_self(); signal(SIGUSR1, idle_sig); while (gui_wait_capture(iface) == 0) { start_capture(iface); } printf("exit\n"); pcap_freealldevs(if_list); return 0; }
/** * \brief Initialize a new tcpreplay context * * Allocates memory and stuff like that. Always returns a buffer or completely * fails by calling exit() on malloc failure. */ tcpreplay_t * tcpreplay_init() { tcpreplay_t *ctx; ctx = safe_malloc(sizeof(tcpreplay_t)); ctx->options = safe_malloc(sizeof(tcpreplay_opt_t)); /* replay packets only once */ ctx->options->loop = 1; /* Default mode is to replay pcap once in real-time */ ctx->options->speed.mode = speed_multiplier; ctx->options->speed.speed = 1.0; /* Set the default timing method */ #ifdef HAVE_ABSOLUTE_TIME /* This is always the best (if the OS supports it) */ ctx->options->accurate = accurate_abs_time; #else /* This is probably the second best solution */ ctx->options->accurate = accurate_gtod; #endif ctx->options->rdtsc_clicks = -1; /* set the default MTU size */ ctx->options->mtu = DEFAULT_MTU; /* disable limit send */ ctx->options->limit_send = -1; #ifdef ENABLE_VERBOSE /* clear out tcpdump struct */ ctx->options->tcpdump = (tcpdump_t *)safe_malloc(sizeof(tcpdump_t)); #endif if (fcntl(STDERR_FILENO, F_SETFL, O_NONBLOCK) < 0) tcpreplay_setwarn(ctx, "Unable to set STDERR to non-blocking: %s", strerror(errno)); #ifdef ENABLE_PCAP_FINDALLDEVS ctx->intlist = get_interface_list(); #else ctx->intlist = NULL; #endif ctx->abort = false; return ctx; }
void post_args(_U_ int argc, _U_ char *argv[]) { char ebuf[SENDPACKET_ERRBUF_SIZE]; struct tcpr_ether_addr *eth_buff; char *intname; sendpacket_t *sp; #ifdef ENABLE_PCAP_FINDALLDEVS interface_list_t *intlist = get_interface_list(); #else interface_list_t *intlist = NULL; #endif #ifdef DEBUG if (HAVE_OPT(DBUG)) debug = OPT_VALUE_DBUG; #else if (HAVE_OPT(DBUG)) warn("not configured with --enable-debug. Debugging disabled."); #endif #ifdef ENABLE_VERBOSE if (HAVE_OPT(VERBOSE)) options.verbose = 1; if (HAVE_OPT(DECODE)) options.tcpdump->args = safe_strdup(OPT_ARG(DECODE)); #endif if (HAVE_OPT(UNIDIR)) options.unidir = 1; if (HAVE_OPT(LIMIT)) options.limit_send = OPT_VALUE_LIMIT; /* default is -1 */ if ((intname = get_interface(intlist, OPT_ARG(INTF1))) == NULL) errx(-1, "Invalid interface name/alias: %s", OPT_ARG(INTF1)); options.intf1 = safe_strdup(intname); if (HAVE_OPT(INTF2)) { if ((intname = get_interface(intlist, OPT_ARG(INTF2))) == NULL) errx(-1, "Invalid interface name/alias: %s", OPT_ARG(INTF2)); options.intf2 = safe_strdup(intname); } if (HAVE_OPT(MAC)) { int ct = STACKCT_OPT(MAC); char **list = STACKLST_OPT(MAC); int first = 1; do { char *p = *list++; if (first) mac2hex(p, (u_char *)options.intf1_mac, ETHER_ADDR_LEN); else mac2hex(p, (u_char *)options.intf2_mac, ETHER_ADDR_LEN); first = 0; } while (--ct > 0); } /* * Figure out MAC addresses of sending interface(s) * if user doesn't specify MAC address on CLI, query for it */ if (memcmp(options.intf1_mac, "\00\00\00\00\00\00", ETHER_ADDR_LEN) == 0) { if ((sp = sendpacket_open(NULL, options.intf1, ebuf, TCPR_DIR_C2S)) == NULL) errx(-1, "Unable to open interface %s: %s", options.intf1, ebuf); if ((eth_buff = sendpacket_get_hwaddr(sp)) == NULL) { warnx("Unable to get MAC address: %s", sendpacket_geterr(sp)); err(-1, "Please consult the man page for using the -M option."); } sendpacket_close(sp); memcpy(options.intf1_mac, eth_buff, ETHER_ADDR_LEN); } if (memcmp(options.intf2_mac, "\00\00\00\00\00\00", ETHER_ADDR_LEN) == 0) { if ((sp = sendpacket_open(NULL, options.intf2, ebuf, TCPR_DIR_S2C)) == NULL) errx(-1, "Unable to open interface %s: %s", options.intf2, ebuf); if ((eth_buff = sendpacket_get_hwaddr(sp)) == NULL) { warnx("Unable to get MAC address: %s", sendpacket_geterr(sp)); err(-1, "Please consult the man page for using the -M option."); } sendpacket_close(sp); memcpy(options.intf2_mac, eth_buff, ETHER_ADDR_LEN); } /* * Open interfaces for sending & receiving */ if ((options.pcap1 = pcap_open_live(options.intf1, options.snaplen, options.promisc, options.to_ms, ebuf)) == NULL) errx(-1, "Unable to open interface %s: %s", options.intf1, ebuf); if (strcmp(options.intf1, options.intf2) == 0) errx(-1, "Whoa tiger! You don't want to use %s twice!", options.intf1); /* we always have to open the other pcap handle to send, but we may not listen */ if ((options.pcap2 = pcap_open_live(options.intf2, options.snaplen, options.promisc, options.to_ms, ebuf)) == NULL) errx(-1, "Unable to open interface %s: %s", options.intf2, ebuf); /* poll should be -1 to wait indefinitely */ options.poll_timeout = -1; }
/* Main entry point */ int main (int argc, char *argv[]) { int ret, is_local, sock_len; rudp_cli_state_t rudp_cli_state; char buf[MAXLINE]; struct sockaddr_in server_addr, sock_addr; /* Sanity check */ if (argc != 1) { printf("usage: ./client\n"); return -1; } /* Initialize the structure for holding client parameters */ cli_params = (client_params_t *)malloc(sizeof(client_params_t)); if (!cli_params) { printf("main: failed to initialize client parameters\n"); return -1; } /* Read the client parameters from client.in */ ret = read_client_params(&rudp_cli_state); if (ret != 0) { printf("main: failed to read client parameters from client.in\n"); return -1; } /* Initialize the RUDP library */ ret = rudp_cli_init(&rudp_cli_state); if (ret != 0) { printf("main: failed to initialize the RUDP library\n"); return -1; } /* Get the list of interfaces on the system and cache it */ ret = get_interface_list(); if (ret != 0) { printf("main: failed to get interface list\n"); return -1; } is_local = check_server_status(); if (is_local < 0) { printf("main: failed to check status of server\n"); return -1; } if (is_local == 1) { printf("\nServer and Client are on the same network (Local)\n"); } else { printf("\nServer and Client are not on the same network (Non-Local)\n"); } cli_params->server_local = is_local; /* Initialize the connection with server */ ret = client_conn_init(is_local); if (ret != 0) { printf("main: failed to initialize connection wth server\n"); return -1; } printf("Selected Server: %s (%d)\n", cli_params->server_ip, cli_params->server_port); printf("Selected Client: %s (%d)\n", cli_params->client_ip, cli_params->client_port); bzero(&server_addr, sizeof(struct sockaddr_in)); inet_pton(AF_INET, cli_params->server_ip, &server_addr.sin_addr); server_addr.sin_port = htons(cli_params->server_port); /* Send the filename to be transferred to the server */ ret = rudp_cli_conn_send(cli_params->server_fd, &server_addr, cli_params->file, MAX_FILENAME_LEN, buf, MAXLINE, reconnect_to_server); if (ret < 0) { printf("main: failed to send filename to server\n"); return -1; } /* Store the new server port */ cli_params->server_port = atoi(buf); /* Print the new server information post reconnect */ sock_len = sizeof(struct sockaddr); bzero(&sock_addr, sizeof(struct sockaddr_in)); getpeername(cli_params->server_fd, (struct sockaddr *)&sock_addr, &sock_len); printf("\nSocket reconnected to client %s port %d\n", inet_ntop(AF_INET, &sock_addr.sin_addr, buf, IP_LEN), ntohs(sock_addr.sin_port)); printf("\nInitiated File transfer with the Server\n\n"); /* * Create 2 threads, one for reading data from the server and populating * the receive buffer, another for consuming the data and printing to * stdout. */ pthread_create(&rudp_thread, NULL, process_rudp_thread, NULL); pthread_create(&consumer_thread, NULL, process_consumer_thread, NULL); /* Wait till they terminate */ pthread_join(rudp_thread, NULL); pthread_join(consumer_thread, NULL); return 0; }