Example #1
0
test_app::test_app(int argc, char *argv[])
	: help_requested(false)
{
    stringlist args(argv + 1, argv + argc);

    help_requested = contains(args, arg_help, arg_short_help);

    filter_classes = extract_filter(args, arg_filter_classes_list, arg_short_filter_classes_list);
    filter_functions = extract_filter(args, arg_filter_functions_list, arg_short_filter_functions_list);
    filter_type files = extract_filter(args, arg_output_to_file, arg_short_output_to_file);

    if (files.size() > 1)
        throw std::runtime_error("Too much files for output!");

    bool junit_requested = contains(args, arg_junit_output, arg_short_junit_output);
    bool cerr = contains(args, arg_cerr, arg_short_cerr);
    bool use_tiny_mode = contains(args, arg_tiny_mode, arg_short_tiny_mode);
    std::ostream *output_stream = cerr ? &std::cerr : &std::cout;
#if defined(_WIN32) || defined(_WIN64)
    bool use_win_debug = contains(args, arg_windebug_mode, arg_short_windebug_mode);
#endif

    if (!files.empty())
    {
        output_file.open(*files.begin(), std::ofstream::out | std::ofstream::trunc);
        output_stream = &output_file;
    }

    if (junit_requested)
        output = std::make_shared<junit_output>(output_stream);
#if defined(_WIN32) || defined(_WIN64)
	else if (use_win_debug)
        output = std::make_shared<windebug_output>(false, use_tiny_mode);
#endif
	else if (use_tiny_mode)
        output = std::make_shared<tiny_output>(output_stream);
    else
        output = std::make_shared<normal_output>(output_stream);
}
Example #2
0
static int  
set_details()
{
#if (INTERCEPT_ADVANCED && TCPCOPY_PCAP)
    int  len;
#endif

#if (!INTERCEPT_ADVANCED)
    /* retrieve ip address */
    if (srv_settings.raw_ip_list != NULL) {
        tc_log_info(LOG_NOTICE, 0, "-x parameter:%s", 
                srv_settings.raw_ip_list);
        retrieve_ip_addr();
    }
#endif
    
#if (INTERCEPT_ADVANCED)
    if (srv_settings.raw_targets != NULL) {
        tc_log_info(LOG_NOTICE, 0, "-o parameter:%s", 
                srv_settings.raw_targets);
        retrieve_target_addresses(srv_settings.raw_targets,
                &(srv_settings.targets));
    } else {
#if (!TCPCOPY_PCAP)
        tc_log_info(LOG_WARN, 0, "no raw targets for advanced mode");
        return -1;
#else
        tc_log_info(LOG_NOTICE, 0, "no raw targets for advanced mode");
#endif
    }

#if (TCPCOPY_PCAP)
    if (srv_settings.raw_device != NULL) {
        tc_log_info(LOG_NOTICE, 0, "device:%s", srv_settings.raw_device);
        if (strcmp(srv_settings.raw_device, DEFAULT_DEVICE) == 0) {
            srv_settings.raw_device = NULL; 
        } else {
            retrieve_devices(srv_settings.raw_device, &(srv_settings.devices));
        }
    }

    if (srv_settings.user_filter != NULL) {
        tc_log_info(LOG_NOTICE, 0, "user filter:%s", srv_settings.user_filter);
        len = strlen(srv_settings.user_filter);
        if (len >= MAX_FILTER_LENGH) {
            tc_log_info(LOG_ERR, 0, "user filter is too long");
            return -1;
        }
        memcpy(srv_settings.filter, srv_settings.user_filter, len);
    } else {
        extract_filter();
    }
#endif

#endif

    /* daemonize */
    if (srv_settings.do_daemonize) {
        if (sigignore(SIGHUP) == -1) {
            tc_log_info(LOG_ERR, errno, "failed to ignore SIGHUP");
        }
        if (daemonize() == -1) {
            fprintf(stderr, "failed to daemonize() in order to daemonize\n");
            return -1;
        }
    }

    return 0;
}
Example #3
0
static int
set_details()
{
#if (!TCPCOPY_PCAP)
    int            rand_port;
#else
    int            len, rand_port;
#endif
    unsigned int   seed;
    struct timeval tp;

    /* generate a random port number for avoiding port conflicts */
    gettimeofday(&tp, NULL);
    seed = tp.tv_usec;
    rand_port = (int) ((rand_r(&seed)/(RAND_MAX + 1.0))*512);
    clt_settings.rand_port_shifted = rand_port;

    /* set the ip port pair mapping according to settings */
    if (retrieve_target_addresses(clt_settings.raw_transfer,
                              &clt_settings.transfer) == -1)
    {
        return -1;
    }

    if (clt_settings.percentage < 0 || clt_settings.percentage > 99) {
        clt_settings.percentage = 0;
    }

    if (clt_settings.par_connections <= 0) {
        clt_settings.par_connections = 1;
    } else if (clt_settings.par_connections > MAX_CONNECTION_NUM) {
        clt_settings.par_connections = MAX_CONNECTION_NUM;
    }
    tc_log_info(LOG_NOTICE, 0, "parallel connections per target:%d",
            clt_settings.par_connections);

#if (TCPCOPY_OFFLINE)
    if (clt_settings.pcap_file == NULL) {
        tc_log_info(LOG_ERR, 0, "it must have -i argument for offline");
        fprintf(stderr, "no -i argument\n");
        return -1;
    }

    if (clt_settings.accelerated_times < 1) {
        clt_settings.accelerated_times = 1;
    }

    tc_log_info(LOG_NOTICE, 0, "accelerated %d times,interval:%llu ms",
            clt_settings.accelerated_times, clt_settings.interval);

    if (clt_settings.interval > 0) {
        clt_settings.interval = clt_settings.interval * 1000;
    }
#endif

#if (TCPCOPY_PCAP_SEND)
    if (clt_settings.output_if_name != NULL) {
        tc_log_info(LOG_NOTICE, 0, "output device:%s", 
                clt_settings.output_if_name);
    } else {
        tc_log_info(LOG_ERR, 0, "output device is null");
        return -1;
    }
#endif

#if (TCPCOPY_PCAP)
    if (clt_settings.raw_device != NULL) {
        tc_log_info(LOG_NOTICE, 0, "device:%s", clt_settings.raw_device);
        if (strcmp(clt_settings.raw_device, DEFAULT_DEVICE) == 0) {
            clt_settings.raw_device = NULL; 
        } else {
            retrieve_devices(clt_settings.raw_device, &(clt_settings.devices));
        }
    }

    if (clt_settings.user_filter != NULL) {
        tc_log_info(LOG_NOTICE, 0, "user filter:%s", clt_settings.user_filter);
        len = strlen(clt_settings.user_filter);
        if (len >= MAX_FILTER_LENGH) {
            tc_log_info(LOG_ERR, 0, "user filter is too long");
            return -1;
        }
        memcpy(clt_settings.filter, clt_settings.user_filter, len);

    } else {
        extract_filter();
    }
#endif

#if (TCPCOPY_MYSQL_ADVANCED)
    if (clt_settings.user_pwd != NULL) {
        tc_log_info(LOG_NOTICE, 0, "-u argument:%s",clt_settings.user_pwd);
        if (retrieve_mysql_user_pwd_info(clt_settings.user_pwd) == -1) {
            tc_log_info(LOG_ERR, 0, "wrong -u argument");
            return -1;
        }
    } else {
        tc_log_info(LOG_ERR, 0, "it must have -u argument");
        fprintf(stderr, "no -u argument\n");
        return -1;
    }
#endif

#if (TCPCOPY_DR)
    /* retrieve real server ip addresses  */
    if (clt_settings.raw_rs_ip_list != NULL) {
        tc_log_info(LOG_NOTICE, 0, "s parameter:%s", 
                clt_settings.raw_rs_ip_list);
        retrieve_real_servers();
    } else {
        tc_log_info(LOG_WARN, 0, "no real server ip addresses");
        return -1;
    }
#endif

    /* daemonize */
    if (clt_settings.do_daemonize) {
        if (sigignore(SIGHUP) == -1) {
            tc_log_info(LOG_ERR, errno, "Failed to ignore SIGHUP");
        }
        if (daemonize() == -1) {
            fprintf(stderr, "failed to daemonize() in order to daemonize\n");
            return -1;
        }    
    }    

    return 0;
}
Example #4
0
static int
set_details()
{
    int            mtu_list[] = {576, 1492, 1500, 0};
    int            i, len, index, offset, rand_port;
    unsigned int   seed;
    unsigned char  value;
    struct timeval tp;

    for (i = tc_pagesize; i >>= 1; tc_pagesize_shift++) { /* void */ }

    /* generate a random port number for avoiding port conflicts */
    gettimeofday(&tp, NULL);\
    seed = tp.tv_usec;
#if (MINGW32)
    srand(seed);
    rand_port = (int) ((rand() / (RAND_MAX + 1.0)) * 512);
#else
    rand_port = (int) ((rand_r(&seed) / (RAND_MAX + 1.0)) * 512);
#endif
    clt_settings.rand_port_shifted = rand_port;

    if (clt_settings.sess_timeout < 0) {
        clt_settings.sess_timeout = DEFAULT_SESS_TIMEOUT;
    }
    tc_log_info(LOG_NOTICE, 0, "session timeout:%d",
            clt_settings.sess_timeout);

    if (clt_settings.sess_keepalive_timeout <= 0) {
        clt_settings.sess_keepalive_timeout = clt_settings.sess_timeout +
            SESS_KEEPLIVE_ADD;
    }
    tc_log_info(LOG_NOTICE, 0, "keepalive timeout:%d",
            clt_settings.sess_keepalive_timeout);

#if (!TC_UDP)
    if (clt_settings.s_pool_size == 0) {
        clt_settings.s_pool_size = TC_DEFAULT_UPOOL_SIZE;
    }
    tc_log_info(LOG_NOTICE, 0, "min sess pool size:%d", TC_MIN_SESS_POOL_SIZE);
    tc_log_info(LOG_NOTICE, 0, "sess pool size:%d", clt_settings.s_pool_size);

    if (clt_settings.s_pool_size < TC_MIN_SESS_POOL_SIZE) {
        tc_log_info(LOG_NOTICE, 0, "sess pool size is too small");
    }
#endif

    if (clt_settings.replica_num > 1) {
        tc_log_info(LOG_NOTICE, 0, "repl num:%d", clt_settings.replica_num);
    }

    if (clt_settings.gradully) {
        tc_log_info(LOG_NOTICE, 0, "gradully replay");
    }

    /* set the ip port pair mapping according to settings */
    if (retr_target_addrs(clt_settings.raw_tf, &clt_settings.transfer) == -1) {
        return -1;
    }

    if (clt_settings.raw_clt_tf_ip != NULL) {
        /* print out raw_clt_tf_ip */
        tc_log_info(LOG_NOTICE, 0, "raw_clt_tf_ip:%s",
                clt_settings.raw_clt_tf_ip);
        retrieve_clt_tf_ips();
    }

    if (clt_settings.percentage > 99) {
        clt_settings.percentage = 0;
    }

#if (!TC_UDP)
    if (sizeof(tc_sess_t) > TC_UPOOL_MAXV) {
        tc_log_info(LOG_NOTICE, 0, "TC_UPOOL_MAXV is too small");
    }
#endif

    if (clt_settings.par_conns <= 0) {
        clt_settings.par_conns = 1;
    } else if (clt_settings.par_conns > MAX_CONN_NUM) {
        clt_settings.par_conns = MAX_CONN_NUM;
    }
    tc_log_info(LOG_NOTICE, 0, "parallel connections per target:%d",
            clt_settings.par_conns);

    len = sizeof(mtu_list) / sizeof(int) - 1;
    for (i = 0; i < len; i++) {
        if (mtu_list[i] == clt_settings.mtu) {
            break;
        }
    }
    if (i == len) {
        mtu_list[len++] = clt_settings.mtu;
    }
    for (i = 0; i < len; i++) {
        index = mtu_list[i] >> 3;
        offset = mtu_list[i] - (index << 3);
        value = clt_settings.candidate_mtu[index];
        value = value | (1 << offset);
        clt_settings.candidate_mtu[index] = value;
    }

#if (TC_OFFLINE)
    if (clt_settings.pcap_file == NULL) {
        tc_log_info(LOG_ERR, 0, "it must have -i argument for offline");
        fprintf(stderr, "no -i argument\n");
        return -1;
    }

    if (clt_settings.accelerated_times < 1) {
        clt_settings.accelerated_times = 1;
    }

    tc_log_info(LOG_NOTICE, 0, "accelerated %d times,interval:%llu ms",
            clt_settings.accelerated_times, clt_settings.interval);

    if (clt_settings.interval > 0) {
        clt_settings.interval = clt_settings.interval * 1000;
    }
#endif

#if (TC_PCAP_SND)
    if (clt_settings.output_if_name != NULL) {
        tc_log_info(LOG_NOTICE, 0, "output device:%s",
                clt_settings.output_if_name);
    } else {
        tc_log_info(LOG_ERR, 0, "no -o argument");
        fprintf(stderr, "no -o argument\n");
        return -1;
    }
#endif

#if (TC_PCAP)
    if (clt_settings.raw_device != NULL) {
        tc_log_info(LOG_NOTICE, 0, "device:%s", clt_settings.raw_device);
        if (strcmp(clt_settings.raw_device, DEFAULT_DEVICE) == 0) {
            clt_settings.raw_device = NULL;
        } else {
            retrieve_devices(clt_settings.raw_device, &(clt_settings.devices));
        }
    }

    if (clt_settings.user_filter != NULL) {
        tc_log_info(LOG_NOTICE, 0, "user filter:%s", clt_settings.user_filter);
        len = strlen(clt_settings.user_filter);
        if (len >= MAX_FILTER_LENGH) {
            tc_log_info(LOG_ERR, 0, "user filter is too long");
            return -1;
        }
        memcpy(clt_settings.filter, clt_settings.user_filter, len);

    } else {
        extract_filter();
    }

    if (clt_settings.snaplen > PCAP_RCV_BUF_SIZE) {
        clt_settings.snaplen = PCAP_RCV_BUF_SIZE;
    }
#endif

    /* retrieve real server ip addresses  */
    if (clt_settings.raw_rs_list != NULL) {
        tc_log_info(LOG_NOTICE, 0, "s parameter:%s",
                clt_settings.raw_rs_list);
        retrieve_real_servers();
    } else {
        tc_log_info(LOG_WARN, 0, "no -s parameter(intercept addresses)");
        fprintf(stderr, "tcpcopy needs -s paramter(intercept addresses)\n");
        return -1;
    }

#if (TC_PLUGIN)
    /* support only one additional module*/
    clt_settings.plugin = tc_modules[0];
#endif

    if (read_conf_file() != TC_OK) {
        return -1;
    }


    if (clt_settings.do_daemonize) {
        /* ignore
        if (sigignore(SIGHUP) == -1) {
            tc_log_info(LOG_ERR, errno, "Failed to ignore SIGHUP");
        }
        if (daemonize() == -1) {
            fprintf(stderr, "failed to daemonize() in order to daemonize\n");
            return -1;
        }
        */
    }

    return 0;
}