static int connect_to_server(tc_event_loop_t *ev_lp) { int i, j, fd; uint32_t target_ip; conns_t *conns; uint16_t target_port; for (i = 0; i < clt_settings.real_servers.num; i++) { conns = &(clt_settings.real_servers.conns[i]); target_ip = conns[i].ip; target_port = conns[i].port; if (target_port == 0) { target_port = clt_settings.srv_port; } if (conns[i].active != 0) { continue; } for (j = 0; j < conns->num; j++) { fd = conns->fds[j]; if (fd > 0) { tc_log_info(LOG_NOTICE, 0, "it close socket:%d", fd); tc_socket_close(fd); tc_event_del(clt_settings.ev[fd]->loop, clt_settings.ev[fd], TC_EVENT_READ); tc_event_destroy(clt_settings.ev[fd], 0); conns->fds[j] = -1; } } clt_settings.real_servers.conns[i].num = 0; clt_settings.real_servers.conns[i].remained_num = 0; for (j = 0; j < clt_settings.par_conns; j++) { fd = tc_message_init(ev_lp, target_ip, target_port); if (fd == TC_INVALID_SOCK) { return TC_ERR; } if (!send_version(fd)) { return TC_ERR; } if (j == 0) { clt_settings.real_servers.active_num++; conns[i].active = 1; } clt_settings.real_servers.conns[i].fds[j] = fd; clt_settings.real_servers.conns[i].num++; clt_settings.real_servers.conns[i].remained_num++; } } return TC_OK; }
/* 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; }
static int connect_to_server(tc_event_loop_t *event_loop) { int i, j, fd; uint32_t target_ip; #if (TCPCOPY_DR) uint16_t target_port; #else ip_port_pair_mapping_t *pair, **mappings; #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]; target_port = clt_settings.real_servers.ports[i]; if (target_port == 0) { target_port = clt_settings.srv_port; } if (clt_settings.real_servers.active[i] != 0) { continue; } clt_settings.real_servers.connections[i].num = 0; clt_settings.real_servers.connections[i].remained_num = 0; for (j = 0; j < clt_settings.par_connections; j++) { fd = tc_message_init(event_loop, target_ip, target_port); if (fd == TC_INVALID_SOCKET) { return TC_ERROR; } if (!send_version(fd)) { return TC_ERROR; } if (j == 0) { clt_settings.real_servers.active_num++; clt_settings.real_servers.active[i] = 1; } clt_settings.real_servers.connections[i].fds[j] = fd; clt_settings.real_servers.connections[i].num++; clt_settings.real_servers.connections[i].remained_num++; } tc_log_info(LOG_NOTICE, 0, "add dr tunnels for exchanging info:%u:%u", target_ip, target_port); } #else mappings = clt_settings.transfer.mappings; for (i = 0; i < clt_settings.transfer.num; i++) { pair = mappings[i]; target_ip = pair->target_ip; for ( j = 0; j < clt_settings.par_connections; j++) { fd = tc_message_init(event_loop, target_ip, clt_settings.srv_port); if (fd == TC_INVALID_SOCKET) { return TC_ERROR; } if (!send_version(fd)) { return TC_ERROR; } address_add_sock(pair->online_ip, pair->online_port, fd); } tc_log_info(LOG_NOTICE, 0, "add tunnels for exchanging info:%u:%u", target_ip, clt_settings.srv_port); } #endif return TC_OK; }
/* 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; }
static int connect_to_server(tc_event_loop_t *event_loop) { int i, j, fd; uint32_t target_ip; uint16_t target_port; connections_t *connections; /* * 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]; target_port = clt_settings.real_servers.ports[i]; if (target_port == 0) { target_port = clt_settings.srv_port; } if (clt_settings.real_servers.active[i] != 0) { continue; } connections = &(clt_settings.real_servers.connections[i]); for (j = 0; j < connections->num; j++) { fd = connections->fds[j]; if (fd > 0) { tc_log_info(LOG_NOTICE, 0, "it close socket:%d", fd); tc_socket_close(fd); tc_event_del(clt_settings.ev[fd]->loop, clt_settings.ev[fd], TC_EVENT_READ); tc_event_destroy(clt_settings.ev[fd]); connections->fds[j] = -1; } } clt_settings.real_servers.connections[i].num = 0; clt_settings.real_servers.connections[i].remained_num = 0; for (j = 0; j < clt_settings.par_connections; j++) { fd = tc_message_init(event_loop, target_ip, target_port); if (fd == TC_INVALID_SOCKET) { return TC_ERROR; } if (!send_version(fd)) { return TC_ERROR; } if (j == 0) { clt_settings.real_servers.active_num++; clt_settings.real_servers.active[i] = 1; } clt_settings.real_servers.connections[i].fds[j] = fd; clt_settings.real_servers.connections[i].num++; clt_settings.real_servers.connections[i].remained_num++; } tc_log_info(LOG_NOTICE, 0, "add dr tunnels for exchanging info:%u:%u", target_ip, target_port); } return TC_OK; }