コード例 #1
0
ファイル: main.c プロジェクト: Alienfeel/tcpcopy
int
main(int argc, char **argv)
{
    int ret;

    settings_init();

    if (set_signal_handler(signals) == -1) {
        return -1;
    }

    tc_time_init();

    if (read_args(argc, argv) == -1) {
        return -1;
    }

    if (srv_settings.log_path == NULL) {
        srv_settings.log_path = "error_intercept.log";  
    }

    if (tc_log_init(srv_settings.log_path) == -1) {
        return -1;
    }

    ret = tc_event_loop_init(&s_event_loop, MAX_FD_NUM);
    if (ret == TC_EVENT_ERROR) {
        tc_log_info(LOG_ERR, 0, "event loop init failed");
        return -1;
    }

    /* output debug info */
    output_for_debug();
    if (set_details() == -1) {
        return -1;
    }

    if (interception_init(&s_event_loop, srv_settings.binded_ip,
                          srv_settings.port) == TC_ERROR)
    {
        return -1;
    }

    if (set_timer() == -1) {
        return -1;
    }

#if (INTERCEPT_COMBINED)
    tc_event_timer_add(&s_event_loop, CHECK_INTERVAL, interception_push);
#endif
    tc_event_timer_add(&s_event_loop, OUTPUT_INTERVAL,
            interception_output_stat);

    /* run now */
    tc_event_process_cycle(&s_event_loop);

    server_release_resources();

    return 0;
}
コード例 #2
0
int
tc_offline_init(tc_event_loop_t *event_loop, char *pcap_file)
{
    int  fd;
    char ebuf[PCAP_ERRBUF_SIZE];

    /* init the raw socket to send */
    if ((fd = tc_raw_socket_out_init()) == TC_INVALID_SOCKET) {
        return TC_ERROR;
    } else {
        tc_raw_socket_out = fd;
    }

    if (pcap_file == NULL) {
        return TC_ERROR;
    }

    if ((clt_settings.pcap = pcap_open_offline(pcap_file, ebuf)) == NULL) {
        tc_log_info(LOG_ERR, 0, "open %s" , ebuf);
        fprintf(stderr, "open %s\n", ebuf);
        return TC_ERROR;
    }

    gettimeofday(&base_time, NULL);
    tc_log_info(LOG_NOTICE, 0, "open pcap success:%s", pcap_file);
    tc_log_info(LOG_NOTICE, 0, "send the first packets here");
    send_packets_from_pcap(1);

    /* register a timer for offline */
    tc_event_timer_add(event_loop, TIMER_INTERVAL, tc_process_offline_packet);

    return TC_OK;
}
コード例 #3
0
ファイル: tc_manager.c プロジェクト: EE-NovRain/tcpcopy
/* initiate TCPCopy client */
int
tcp_copy_init(tc_event_loop_t *event_loop)
{
    int                      i, fd;
    uint32_t                 target_ip;
    ip_port_pair_mapping_t  *pair, **mappings;

    /* register some timer */
    tc_event_timer_add(event_loop, 60000, check_resource_usage);
    tc_event_timer_add(event_loop, 5000, tc_interval_dispose);

    /* init session table */
    init_for_sessions();

    address_init();

    /* add connections to the tested server for exchanging info */
    mappings = clt_settings.transfer.mappings;
    for (i = 0; i < clt_settings.transfer.num; i++) {

        pair = mappings[i];
        target_ip = pair->target_ip;

        fd = tc_message_init(event_loop, target_ip, clt_settings.srv_port);
        if (fd == TC_INVALID_SOCKET) {
            return TC_ERROR;
        }

        address_add_sock(pair->online_ip, pair->online_port, fd);

        tc_log_info(LOG_NOTICE, 0, "add a tunnel for exchanging info:%u:%u",
                    ntohl(target_ip), clt_settings.srv_port);
    }

    /* init packets for processing */
#if (TCPCOPY_OFFLINE)
    if (tc_offline_init(event_loop, clt_settings.pcap_file) == TC_ERROR) {
        return TC_ERROR;
    }
#else
    if (tc_packets_init(event_loop) == TC_ERROR) {
        return TC_ERROR;
    }
#endif

    return TC_OK;
}
コード例 #4
0
/* initiate TCPCopy client */
int
gryphon_init(tc_event_loop_t *event_loop)
{
    int      i, result;
    uint64_t pool_size;

    /* register some timer */
    tc_event_timer_add(event_loop, 60000, check_resource_usage);

    if (connect_to_server(event_loop) == TC_ERROR) {
        return TC_ERROR;
    }

    if (tc_send_init(event_loop) == TC_ERROR) {
        return TC_ERROR;
    }

    for (i = 0; i < clt_settings.num_pcap_files; i++) {
        result = calculate_mem_pool_size(clt_settings.pcap_files[i].file, 
                clt_settings.filter);
        if (result == TC_ERROR) {
            return TC_ERROR;
        }
    }

    tc_log_info(LOG_NOTICE, 0, "pool size:%llu", clt_settings.mem_pool_size);

    pool_size = clt_settings.mem_pool_size;
    if (clt_settings.mem_pool_size > 0) {
        clt_settings.mem_pool = (unsigned char *) calloc(pool_size, 
                sizeof(unsigned char));
        if (clt_settings.mem_pool == NULL) {
            return TC_ERROR;
        }
    }

    for (i = 0; i < clt_settings.num_pcap_files; i++) {
        read_packets_from_pcap(clt_settings.pcap_files[i].file, 
                clt_settings.filter);
    }

    tc_log_info(LOG_NOTICE, 0, "pool used:%llu", clt_settings.mem_pool_index);

    tc_event_timer_add(event_loop, 5000, tc_interval_dispose); 

    return TC_OK;
}
コード例 #5
0
ファイル: tc_manager.c プロジェクト: cnJun/tcpcopy
/* initiate TCPCopy client */
int
tcp_copy_init(tc_event_loop_t *event_loop)
{

    /* register some timer */
    tc_event_timer_add(event_loop, 60000, check_resource_usage);
    tc_event_timer_add(event_loop, 5000, tc_interval_dispose);

#if (TCPCOPY_DR)
    if (clt_settings.lonely) {
        tc_event_timer_add(event_loop, 10000, restore_work);
    }
#endif

    /* init session table */
    init_for_sessions();

#if (!TCPCOPY_DR)
    address_init();
#endif

    if (connect_to_server(event_loop) == TC_ERROR) {
        return TC_ERROR;
    }

    /* init packets for processing */
#if (TCPCOPY_OFFLINE)
    if (tc_offline_init(event_loop, clt_settings.pcap_file) == TC_ERROR) {
        return TC_ERROR;
    }
#else
    if (tc_packets_init(event_loop) == TC_ERROR) {
        return TC_ERROR;
    }
#endif

    return TC_OK;
}
コード例 #6
0
ファイル: tc_manager.c プロジェクト: bleachyin/tcpcopy
/* initiate TCPCopy client */
int
tcp_copy_init(tc_event_loop_t *event_loop)
{
    int                      i, fd;
#if (TCPCOPY_PCAP)
    int                      j, filter_port_num = 0;
    char                    *pt;
    uint16_t                 filter_port[MAX_FILTER_PORTS];
#endif
    uint32_t                 target_ip;
#if (!TCPCOPY_DR)
    ip_port_pair_mapping_t  *pair, **mappings;
#endif

    /* register some timer */
    tc_event_timer_add(event_loop, 60000, check_resource_usage);
    tc_event_timer_add(event_loop, 5000, tc_interval_dispose);

    /* init session table */
    init_for_sessions();


#if (TCPCOPY_PCAP)
    memset((void *) filter_port, 0, MAX_FILTER_PORTS << 1);
#endif

#if (TCPCOPY_DR)
    /* 
     * add connections to the real servers for sending router info 
     * and receiving response packet
     */
    for (i = 0; i < clt_settings.real_servers.num; i++) {

        target_ip = clt_settings.real_servers.ips[i];

        fd = tc_message_init(event_loop, target_ip, clt_settings.srv_port);
        if (fd == TC_INVALID_SOCKET) {
            return TC_ERROR;
        }
        clt_settings.real_servers.active_num++;
        clt_settings.real_servers.active[i] = 1;
        clt_settings.real_servers.fds[i] = fd;

        tc_log_info(LOG_NOTICE, 0, "add a tunnel for exchanging info:%u:%u",
                    ntohl(target_ip), clt_settings.srv_port);
    }
#else
    address_init();

    /* add connections to the tested server for exchanging info */
    mappings = clt_settings.transfer.mappings;
    for (i = 0; i < clt_settings.transfer.num; i++) {

        pair = mappings[i];
        target_ip = pair->target_ip;

#if (TCPCOPY_PCAP)
        for (j = 0; j < MAX_FILTER_PORTS; j++) {
            if (filter_port[j] == 0) {
                filter_port[j] = pair->online_port;
                filter_port_num++;
                break;
            } else if (filter_port[j] == pair->online_port) {
                break;
            }
        }
#endif

        fd = tc_message_init(event_loop, target_ip, clt_settings.srv_port);
        if (fd == TC_INVALID_SOCKET) {
            return TC_ERROR;
        }

        address_add_sock(pair->online_ip, pair->online_port, fd);

        tc_log_info(LOG_NOTICE, 0, "add a tunnel for exchanging info:%u:%u",
                    ntohl(target_ip), clt_settings.srv_port);
    }
#endif

#if (TCPCOPY_PCAP)
    if (filter_port_num == 0) {
        tc_log_info(LOG_ERR, 0, "filter_port_num is zero");
        return TC_ERROR;
    }
    pt = clt_settings.filter;
    strcpy(pt, "tcp dst port ");
    pt = pt + strlen(pt);
    for (i = 0; i < filter_port_num -1; i++) {
        sprintf(pt, "%d or ", ntohs(filter_port[i]));
        pt = pt + strlen(pt);
    }
    sprintf(pt, "%d", ntohs(filter_port[i]));
    tc_log_info(LOG_NOTICE, 0, "filter = %s", clt_settings.filter);
#endif

    /* init packets for processing */
#if (TCPCOPY_OFFLINE)
    if (tc_offline_init(event_loop, clt_settings.pcap_file) == TC_ERROR) {
        return TC_ERROR;
    }
#else
    if (tc_packets_init(event_loop) == TC_ERROR) {
        return TC_ERROR;
    }
#endif

    return TC_OK;
}
コード例 #7
0
ファイル: manager.c プロジェクト: chijiao/tcpcopy
/* Initiate tcpcopy client */
int
tcp_copy_init(tc_event_loop_t *event_loop)
{
    int                      i;
#if (TCPCOPY_OFFLINE)
    char                    *pcap_file, ebuf[PCAP_ERRBUF_SIZE];
#endif
    uint16_t                 online_port, target_port;
    uint32_t                 target_ip;
    tc_event_t              *raw_socket_event;
    ip_port_pair_mapping_t  *pair, **mappings;

    /* keep it temporarily */
    select_server_set_callback(dispose_event);

    /* Register a timer to check resource every minute */
    tc_event_timer_add(event_loop, 60000, check_resource_usage);

    /* Init session table*/
    init_for_sessions();
    localhost = inet_addr("127.0.0.1");

    /* Init output raw socket info */
    send_init();

    /* Add connections to the tested server for exchanging info */
    mappings = clt_settings.transfer.mappings;
    for (i = 0; i < clt_settings.transfer.num; i++) {

        pair = mappings[i];
        online_port = pair->online_port;
        target_ip   = pair->target_ip;
        target_port = pair->target_port;
        if (address_add_msg_conn(event_loop, online_port, target_ip,
                                 clt_settings.srv_port))
        {
            return FAILURE;
        }
        tc_log_info(LOG_NOTICE, 0, "add a tunnel for exchanging info:%u",
                    ntohs(target_port));
    }

#if (!TCPCOPY_OFFLINE)
    /* Init input raw socket info */
    raw_sock = init_input_raw_socket();
#endif
    if (raw_sock != -1) {

        /* Add the input raw socket to select */
        raw_socket_event = tc_event_create(raw_sock, dispose_event_wrapper,
                                           NULL);
        if (raw_socket_event == NULL) {
            return FAILURE;
        }

        if (tc_event_add(event_loop, raw_socket_event, TC_EVENT_READ)
                == TC_EVENT_ERROR)
        {
            tc_log_info(LOG_ERR, 0, "add raw socket(%d) to event loop failed.",
                        raw_socket_event->fd);
            return FAILURE;
        }

        /* Init output raw socket info */
        send_init();

        /* Add connections to the tested server for exchanging info */
        mappings = clt_settings.transfer.mappings;
        for (i = 0; i < clt_settings.transfer.num; i++) {

            pair = mappings[i];
            online_port = pair->online_port;
            target_ip   = pair->target_ip;
            target_port = pair->target_port;

            if (address_add_msg_conn(event_loop, online_port, target_ip,
                                     clt_settings.srv_port) == -1)
            {
                return FAILURE;
            }

            tc_log_info(LOG_NOTICE, 0, "add a tunnel for exchanging info:%u",
                        ntohs(target_port));
        }

        return SUCCESS;
    } else {

#if (TCPCOPY_OFFLINE)
        select_offline_set_callback(send_packets_from_pcap);

        pcap_file = clt_settings.pcap_file;
        if (pcap_file != NULL) {

            if ((pcap = pcap_open_offline(pcap_file, ebuf)) == NULL) {
                tc_log_info(LOG_ERR, 0, "open %s" , ebuf);
                fprintf(stderr, "open %s\n", ebuf);
                return FAILURE;

            } else {

                gettimeofday(&base_time, NULL);
                tc_log_info(LOG_NOTICE, 0, "open pcap success:%s", pcap_file);
                tc_log_info(LOG_NOTICE, 0, "send the first packets here");
                send_packets_from_pcap(1);
            }
        } else {
            return FAILURE;
        }
#else
        return FAILURE;
#endif
    }

    return SUCCESS;
}