Esempio n. 1
0
static int 
tc_device_set(tc_event_loop_t *event_loop, device_t *device) 
{
    int         fd;
    tc_event_t *ev;

    fd = tc_pcap_socket_in_init(&(device->pcap), device->name,
            REP_RCV_BUF_SIZE, TC_PCAP_BUF_SIZE, srv_settings.filter);
    if (fd == TC_INVALID_SOCK) {
        return TC_ERR;
    }

    pcap_map[fd] = device->pcap;

    ev = tc_event_create(event_loop->pool, fd, tc_proc_resp_packet, NULL);
    if (ev == NULL) {
        return TC_ERR;
    }

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

    return TC_OK;
}
Esempio n. 2
0
static int
tc_msg_event_accept(tc_event_t *rev)
{
    int         fd;
    tc_event_t *ev;

    if ((fd = tc_socket_accept(rev->fd)) == TC_INVALID_SOCKET) {
        tc_log_info(LOG_ERR, 0, "msg accept failed, from listen:%d", rev->fd);
        return TC_ERROR;
    }

    if (tc_socket_set_nodelay(fd) == TC_ERROR) {
        tc_log_info(LOG_ERR, 0, "Set no delay to socket(%d) failed.", rev->fd);
        return TC_ERROR;
    }

    ev = tc_event_create(fd, tc_msg_event_process, NULL);
    if (ev == NULL) {
        tc_log_info(LOG_ERR, 0, "Msg event create failed.");
        return TC_ERROR;
    }

    if (tc_event_add(rev->loop, ev, TC_EVENT_READ) == TC_EVENT_ERROR) {
        return TC_ERROR;
    }

    return TC_OK;
}
Esempio n. 3
0
static int 
tc_device_set(tc_event_loop_t *event_loop, device_t *device) 
{
    int         fd;
    tc_event_t *ev;

    fd = tc_pcap_socket_in_init(&(device->pcap), device->name,
            PCAP_RECV_BUF_SIZE, TCPCOPY_PCAP_BUF_SIZE, clt_settings.filter);
    if (fd == TC_INVALID_SOCKET) {
        return TC_ERROR;
    }

    pcap_map[fd] = device->pcap;

    ev = tc_event_create(fd, tc_process_pcap_socket_packet, NULL);
    if (ev == NULL) {
        return TC_ERROR;
    }

    if (tc_event_add(event_loop, ev, TC_EVENT_READ) == TC_EVENT_ERROR) {
        tc_log_info(LOG_ERR, 0, "add socket(%d) to event loop failed.", fd);
        return TC_ERROR;
    }

    return TC_OK;
}
Esempio n. 4
0
static int
tc_msg_event_accept(tc_event_t *rev)
{
    int         fd;
    tc_event_t *ev;

    if ((fd = tc_socket_accept(rev->fd)) == TC_INVALID_SOCKET) {
        tc_log_info(LOG_ERR, 0, "msg accept failed, from listen:%d", rev->fd);
        return TC_ERROR;
    }

    if (tc_socket_set_nodelay(fd) == TC_ERROR) {
        tc_log_info(LOG_ERR, 0, "Set no delay to socket(%d) failed.", rev->fd);
        return TC_ERROR;
    }

    ev = tc_event_create(fd, tc_msg_event_process, NULL);
    if (ev == NULL) {
        tc_log_info(LOG_ERR, 0, "msg event create failed.");
        return TC_ERROR;
    }

    if (tc_event_add(rev->loop, ev, TC_EVENT_READ) == TC_EVENT_ERROR) {
        return TC_ERROR;
    }
#if (TCPCOPY_SINGLE)  
    if (srv_settings.router_fd > 0) {
        tc_log_info(LOG_WARN, 0, "it does not support distributed tcpcopy");
    }
    srv_settings.router_fd = fd;
#endif

    return TC_OK;
}
Esempio n. 5
0
static int
tc_msg_event_accept(tc_event_t *rev)
{
    tc_event_t     *ev;
    register int    fd;
    tunnel_basic_t *tunnel;

    if ((fd = tc_socket_accept(rev->fd)) == TC_INVALID_SOCK) {
        tc_log_info(LOG_ERR, 0, "msg accept failed, from listen:%d", rev->fd);
        return TC_ERR;
    }
    
    tc_log_info(LOG_NOTICE, 0, "it adds fd:%d", fd);

    if (tc_socket_set_nodelay(fd) == TC_ERR) {
        tc_log_info(LOG_ERR, 0, "Set no delay to socket(%d) failed.", rev->fd);
        tc_log_info(LOG_NOTICE, 0, "it close socket:%d", fd);
        tc_socket_close(fd);
        return TC_ERR;
    }

#if (TC_SINGLE)  
    if (!tc_intercept_check_tunnel_for_single(fd)) {
        tc_log_info(LOG_WARN, 0, "sth tries to connect to server.");
        tc_log_info(LOG_NOTICE, 0, "it close socket:%d", fd);
        tc_socket_close(fd);
        return TC_ERR;
    }
#endif   

    ev = tc_event_create(rev->loop->pool, fd, tc_msg_event_proc, NULL);
    if (ev == NULL) {
        tc_log_info(LOG_ERR, 0, "Msg event create failed.");
        return TC_ERR;
    }

    if (tc_event_add(rev->loop, ev, TC_EVENT_READ) == TC_EVENT_ERROR) {
        return TC_ERR;
    }

    tunnel = srv_settings.tunnel;
    tunnel[fd].ev = ev;
    tunnel[fd].first_in = 1;
    tunnel[fd].fd_valid = 1;

    return TC_OK;
}
Esempio n. 6
0
int
tc_message_init(tc_event_loop_t *event_loop, uint32_t ip, uint16_t port)
{
    int            fd;
    tc_event_t    *ev;

#if (TCPCOPY_DR)
    socklen_t      len;
    struct timeval timeout = {3,0}; 
#endif

    if ((fd = tc_socket_init()) == TC_INVALID_SOCKET) {
        return TC_INVALID_SOCKET;
    }

    if (tc_socket_connect(fd, ip, port) == TC_ERROR) {
        return TC_INVALID_SOCKET;
    }

    if (tc_socket_set_nodelay(fd) == TC_ERROR) {
        return TC_INVALID_SOCKET;
    }
#if (TCPCOPY_COMBINED)
    if (tc_socket_set_nonblocking(fd) == TC_ERROR) {
        return TC_INVALID_SOCKET;
    }
#endif

#if (TCPCOPY_DR)
    len = (socklen_t) sizeof(struct timeval);
    setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout, len);
#endif

    ev = tc_event_create(fd, tc_process_server_msg, NULL);
    if (ev == NULL) {
        return TC_INVALID_SOCKET;
    }

    if (tc_event_add(event_loop, ev, TC_EVENT_READ) == TC_EVENT_ERROR) {
        return TC_INVALID_SOCKET;
    }

    return fd;
}
Esempio n. 7
0
/* initiate for tcpcopy server */
int
interception_init(tc_event_loop_t *event_loop, char *ip, uint16_t port)
{
    int         fd;
    tc_event_t *ev;

    delay_table_init(srv_settings.hash_size);
    if (router_init() != TC_OK) {
        return TC_ERROR;
    }

    /* init the listening socket */
    if ((fd = tc_socket_init()) == TC_INVALID_SOCKET) {
        return TC_ERROR;

    } else {
        if (tc_socket_listen(fd, ip, port) == TC_ERROR) {
            return TC_ERROR;
        }

        tc_log_info(LOG_NOTICE, 0, "msg listen socket:%d", fd);

        ev = tc_event_create(fd, tc_msg_event_accept, NULL);
        if (ev == NULL) {
            return TC_ERROR;
        }

        if (tc_event_add(event_loop, ev, TC_EVENT_READ) == TC_EVENT_ERROR) {
            return TC_ERROR;
        }
    }


    if (sniff_init(event_loop) != TC_OK) {
        return TC_ERROR;
    }

    return TC_OK;
}
Esempio n. 8
0
/* This is for copying multiple ports */
int
address_add_msg_conn(tc_event_loop_t *event_loop, uint16_t local_port,
        uint32_t dst_ip, uint16_t dst_port)
{
    int          fd;
    tc_event_t  *msg_socket_event;

    if ((fd = tc_socket_init()) == TC_INVALID_SOCKET) {
        return TC_ERROR;
    }

    if (tc_socket_connect(fd, dst_ip, dst_port) == TC_ERROR) {
        return TC_ERROR;
    }

    if (tc_socket_set_nodelay(fd) == TC_ERROR) {
        return TC_ERROR;
    }

    msg_socket_event = tc_event_create(fd, dispose_event_wrapper, NULL);
    if (msg_socket_event == NULL) {
        return TC_ERROR;
    }

    if (tc_event_add(event_loop, msg_socket_event, TC_EVENT_READ)
            == TC_EVENT_ERROR)
    {
        return TC_ERROR;
    }

    addr[local_port].ip = dst_ip;
    addr[local_port].port = dst_port;
    addr[local_port].sock = fd;

    return TC_OK;
}
Esempio n. 9
0
int
tc_packets_init(tc_event_loop_t *event_loop)
{
    int         fd;
#if (TCPCOPY_PCAP)
    int         i = 0;
    bool        work;
    char        ebuf[PCAP_ERRBUF_SIZE];
    devices_t  *devices;
    pcap_if_t  *alldevs, *d;
#else
    tc_event_t *ev;
#endif

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

#if (TCPCOPY_PCAP)
    devices = &(clt_settings.devices);
    if (clt_settings.raw_device == NULL) {
        if (pcap_findalldevs(&alldevs, ebuf) == -1) {
            tc_log_info(LOG_ERR, 0, "error in pcap_findalldevs:%s", ebuf);
            return TC_ERROR;
        }
        for (d = alldevs; d; d = d->next)
        {
            if (strcmp(d->name, DEFAULT_DEVICE) == 0) {
                continue;
            }

            if (i >= MAX_DEVICE_NUM) {
                tc_log_info(LOG_ERR, 0, "It has too many devices");
                return TC_ERROR;
            }

            strcpy(devices->device[i++].name, d->name);
        }
        devices->device_num = i;
    }

    for (i = 0; i < devices->device_num; i++) {
        if (tc_device_set(event_loop, &(devices->device[i]))
                == TC_ERROR) 
        {
            tc_log_info(LOG_WARN, 0, "device could not work:%s", d->name);
        } else {
            work = true;
        }
    }

    if (work == false) {
        tc_log_info(LOG_ERR, 0, "no device available for snooping packets");
        return TC_ERROR;
    }

#else
    /* init the raw socket to recv packets */
    if ((fd = tc_raw_socket_in_init()) == TC_INVALID_SOCKET) {
        return TC_ERROR;
    }
    tc_socket_set_nonblocking(fd);

    ev = tc_event_create(fd, tc_process_raw_socket_packet, NULL);
    if (ev == NULL) {
        return TC_ERROR;
    }

    if (tc_event_add(event_loop, ev, TC_EVENT_READ) == TC_EVENT_ERROR) {
        tc_log_info(LOG_ERR, 0, "add socket(%d) to event loop failed.", fd);
        return TC_ERROR;
    }
#endif

    return TC_OK;
}
Esempio n. 10
0
/* initiate for tcpcopy server */
int
interception_init(tc_event_loop_t *event_loop, char *ip, uint16_t port)
{
    int         fd;
#if (INTERCEPT_THREAD)
    pthread_t   thread;
#endif
    tc_event_t *ev;

    router_init(srv_settings.hash_size);

    pid = getpid();

    /* init the listening socket */
    if ((fd = tc_socket_init()) == TC_INVALID_SOCKET) {
        return TC_ERROR;

    } else {
        if (tc_socket_listen(fd, ip, port) == TC_ERROR) {
            return TC_ERROR;
        }

        tc_log_info(LOG_NOTICE, 0, "msg listen socket:%d", fd);

        ev = tc_event_create(fd, tc_msg_event_accept, NULL);
        if (ev == NULL) {
            return TC_ERROR;
        }

        if (tc_event_add(event_loop, ev, TC_EVENT_READ) == TC_EVENT_ERROR) {
            return TC_ERROR;
        }
    }

    /* init the netlink socket */
    if ((fd = tc_nl_socket_init()) == TC_INVALID_SOCKET) {
        return TC_ERROR;

    } else {
        tc_log_info(LOG_NOTICE, 0, "firewall socket:%d", fd);

        ev = tc_event_create(fd, tc_nl_event_process, NULL);
        if (ev == NULL) {
            return TC_ERROR;
        }

        if (tc_event_add(event_loop, ev, TC_EVENT_READ) == TC_EVENT_ERROR) {
            return TC_ERROR;
        }
    }

#if (INTERCEPT_THREAD)
    pthread_mutex_init(&mutex, NULL);
    pthread_cond_init(&full, NULL);
    pthread_cond_init(&empty, NULL);
    pthread_create(&thread, NULL, interception_process_msg, NULL);

    pthread_mutex_init(&nl_mutex, NULL);
    pthread_cond_init(&nl_full, NULL);
    pthread_cond_init(&nl_empty, NULL);
    pthread_create(&thread, NULL, interception_dispose_nl_verdict, NULL);
#endif

    return TC_OK;
}
Esempio n. 11
0
/* initiate for tcpcopy server */
int
interception_init(tc_event_loop_t *event_loop, char *ip, uint16_t port)
{
    int         fd;
    tc_event_t *ev;

#if (!TCPCOPY_SINGLE)
    delay_table_init(srv_settings.hash_size);
    if (router_init() != TC_OK) {
        return TC_ERROR;
    }
#endif

    pid = getpid();

    /* init the listening socket */
    if ((fd = tc_socket_init()) == TC_INVALID_SOCKET) {
        return TC_ERROR;

    } else {
        if (tc_socket_listen(fd, ip, port) == TC_ERROR) {
            return TC_ERROR;
        }

        tc_log_info(LOG_NOTICE, 0, "msg listen socket:%d", fd);

        ev = tc_event_create(fd, tc_msg_event_accept, NULL);
        if (ev == NULL) {
            return TC_ERROR;
        }

        if (tc_event_add(event_loop, ev, TC_EVENT_READ) == TC_EVENT_ERROR) {
            return TC_ERROR;
        }
    }

#if (INTERCEPT_NFQUEUE)   
    /* init the nfq socket */
    if ((fd = tc_nfq_socket_init(&srv_settings.nfq_handler, 
                    &srv_settings.nfq_q_handler, tc_nfq_process_packet)) 
            == TC_INVALID_SOCKET)
    {
        return TC_ERROR;

    } else {
        tc_log_info(LOG_NOTICE, 0, "nfq socket:%d", fd);

        ev = tc_event_create(fd, tc_nfq_event_process, NULL);
        if (ev == NULL) {
            return TC_ERROR;
        }

        if (tc_event_add(event_loop, ev, TC_EVENT_READ) == TC_EVENT_ERROR) {
            return TC_ERROR;
        }
    }
#else
    /* init the netlink socket */
    if ((fd = tc_nl_socket_init()) == TC_INVALID_SOCKET) {
        return TC_ERROR;

    } else {
        tc_log_info(LOG_NOTICE, 0, "firewall socket:%d", fd);

        ev = tc_event_create(fd, tc_nl_event_process, NULL);
        if (ev == NULL) {
            return TC_ERROR;
        }

        if (tc_event_add(event_loop, ev, TC_EVENT_READ) == TC_EVENT_ERROR) {
            return TC_ERROR;
        }
    }

#endif

    return TC_OK;
}
Esempio n. 12
0
/* Initiate udpcopy client */
int udp_copy_init(tc_event_loop_t *event_loop)
{
#if (UDPCOPY_OFFLINE)
    char                   *pcap_file, ebuf[PCAP_ERRBUF_SIZE];
#endif
    tc_event_t             *raw_socket_event;

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

    localhost = inet_addr("127.0.0.1"); 

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

#if (!UDPCOPY_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)
        {
            log_info(LOG_ERR, "add raw socket(%d) to event loop failed.",
                     raw_socket_event->fd);
            return FAILURE;
        }

        /* Init output raw socket info */
        send_init();
        return SUCCESS;
    }else{
#if (UDPCOPY_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){
                log_info(LOG_ERR, "open %s" , ebuf);
                fprintf(stderr, "open %s\n", ebuf);
                return FAILURE;

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

    return SUCCESS;
}
Esempio n. 13
0
/* 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;
}