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; }
/* 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; }
/* 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; }
/* 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; }
/* 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; }